#include "image-private.h"
#ifdef HAVE_LIBJPEG
# include <jpeglib.h>
int
_cupsImageReadJPEG(
cups_image_t *img,
FILE *fp,
cups_icspace_t primary,
cups_icspace_t secondary,
int saturation,
int hue,
const cups_ib_t *lut)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cups_ib_t *in,
*out;
jpeg_saved_marker_ptr marker;
int psjpeg = 0;
static const char *cspaces[] =
{
"JCS_UNKNOWN",
"JCS_GRAYSCALE",
"JCS_RGB",
"JCS_YCbCr",
"JCS_CMYK",
"JCS_YCCK"
};
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_save_markers(&cinfo, JPEG_APP0 + 14, 0xffff);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, 1);
for (marker = cinfo.marker_list; marker; marker = marker->next)
if (marker->marker == (JPEG_APP0 + 14) && marker->data_length >= 12 &&
!memcmp(marker->data, "Adobe", 5) && marker->data[11] == 2)
{
fputs("DEBUG: Adobe CMYK JPEG detected (inverting color values)\n",
stderr);
psjpeg = 1;
}
cinfo.quantize_colors = 0;
fprintf(stderr, "DEBUG: num_components = %d\n", cinfo.num_components);
fprintf(stderr, "DEBUG: jpeg_color_space = %s\n",
cspaces[cinfo.jpeg_color_space]);
if (cinfo.num_components == 1)
{
fputs("DEBUG: Converting image to grayscale...\n", stderr);
cinfo.out_color_space = JCS_GRAYSCALE;
cinfo.out_color_components = 1;
cinfo.output_components = 1;
img->colorspace = secondary;
}
else if (cinfo.num_components == 4)
{
fputs("DEBUG: Converting image to CMYK...\n", stderr);
cinfo.out_color_space = JCS_CMYK;
cinfo.out_color_components = 4;
cinfo.output_components = 4;
img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_CMYK : primary;
}
else
{
fputs("DEBUG: Converting image to RGB...\n", stderr);
cinfo.out_color_space = JCS_RGB;
cinfo.out_color_components = 3;
cinfo.output_components = 3;
img->colorspace = (primary == CUPS_IMAGE_RGB_CMYK) ? CUPS_IMAGE_RGB : primary;
}
jpeg_calc_output_dimensions(&cinfo);
if (cinfo.output_width <= 0 || cinfo.output_width > CUPS_IMAGE_MAX_WIDTH ||
cinfo.output_height <= 0 || cinfo.output_height > CUPS_IMAGE_MAX_HEIGHT)
{
fprintf(stderr, "DEBUG: Bad JPEG dimensions %dx%d!\n",
cinfo.output_width, cinfo.output_height);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
return (1);
}
img->xsize = cinfo.output_width;
img->ysize = cinfo.output_height;
if (cinfo.X_density > 0 && cinfo.Y_density > 0 && cinfo.density_unit > 0)
{
if (cinfo.density_unit == 1)
{
img->xppi = cinfo.X_density;
img->yppi = cinfo.Y_density;
}
else
{
img->xppi = (int)((float)cinfo.X_density * 2.54);
img->yppi = (int)((float)cinfo.Y_density * 2.54);
}
if (img->xppi == 0 || img->yppi == 0)
{
fprintf(stderr, "DEBUG: Bad JPEG image resolution %dx%d PPI.\n",
img->xppi, img->yppi);
img->xppi = img->yppi = 128;
}
}
fprintf(stderr, "DEBUG: JPEG image %dx%dx%d, %dx%d PPI\n",
img->xsize, img->ysize, cinfo.output_components,
img->xppi, img->yppi);
cupsImageSetMaxTiles(img, 0);
in = malloc(img->xsize * cinfo.output_components);
out = malloc(img->xsize * cupsImageGetDepth(img));
jpeg_start_decompress(&cinfo);
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, (JSAMPROW *)&in, (JDIMENSION)1);
if (psjpeg && cinfo.output_components == 4)
{
cups_ib_t *ptr;
int i;
for (ptr = in, i = img->xsize * 4; i > 0; i --, ptr ++)
*ptr = 255 - *ptr;
}
if ((saturation != 100 || hue != 0) && cinfo.output_components == 3)
cupsImageRGBAdjust(in, img->xsize, saturation, hue);
if ((img->colorspace == CUPS_IMAGE_WHITE && cinfo.out_color_space == JCS_GRAYSCALE) ||
(img->colorspace == CUPS_IMAGE_CMYK && cinfo.out_color_space == JCS_CMYK))
{
#ifdef DEBUG
int i, j;
cups_ib_t *ptr;
fputs("DEBUG: Direct Data...\n", stderr);
fputs("DEBUG:", stderr);
for (i = 0, ptr = in; i < img->xsize; i ++)
{
putc(' ', stderr);
for (j = 0; j < cinfo.output_components; j ++, ptr ++)
fprintf(stderr, "%02X", *ptr & 255);
}
putc('\n', stderr);
#endif
if (lut)
cupsImageLut(in, img->xsize * cupsImageGetDepth(img), lut);
_cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, in);
}
else if (cinfo.out_color_space == JCS_GRAYSCALE)
{
switch (img->colorspace)
{
default :
break;
case CUPS_IMAGE_BLACK :
cupsImageWhiteToBlack(in, out, img->xsize);
break;
case CUPS_IMAGE_RGB :
cupsImageWhiteToRGB(in, out, img->xsize);
break;
case CUPS_IMAGE_CMY :
cupsImageWhiteToCMY(in, out, img->xsize);
break;
case CUPS_IMAGE_CMYK :
cupsImageWhiteToCMYK(in, out, img->xsize);
break;
}
if (lut)
cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
_cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
}
else if (cinfo.out_color_space == JCS_RGB)
{
switch (img->colorspace)
{
default :
break;
case CUPS_IMAGE_RGB :
cupsImageRGBToRGB(in, out, img->xsize);
break;
case CUPS_IMAGE_WHITE :
cupsImageRGBToWhite(in, out, img->xsize);
break;
case CUPS_IMAGE_BLACK :
cupsImageRGBToBlack(in, out, img->xsize);
break;
case CUPS_IMAGE_CMY :
cupsImageRGBToCMY(in, out, img->xsize);
break;
case CUPS_IMAGE_CMYK :
cupsImageRGBToCMYK(in, out, img->xsize);
break;
}
if (lut)
cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
_cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
}
else
{
fputs("DEBUG: JCS_CMYK\n", stderr);
switch (img->colorspace)
{
default :
break;
case CUPS_IMAGE_WHITE :
cupsImageCMYKToWhite(in, out, img->xsize);
break;
case CUPS_IMAGE_BLACK :
cupsImageCMYKToBlack(in, out, img->xsize);
break;
case CUPS_IMAGE_CMY :
cupsImageCMYKToCMY(in, out, img->xsize);
break;
case CUPS_IMAGE_RGB :
cupsImageCMYKToRGB(in, out, img->xsize);
break;
}
if (lut)
cupsImageLut(out, img->xsize * cupsImageGetDepth(img), lut);
_cupsImagePutRow(img, 0, cinfo.output_scanline - 1, img->xsize, out);
}
}
free(in);
free(out);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
return (0);
}
#endif