FILE *fp = NULL;
JSAMPARRAY buffer;
int row_stride, x, y;
- int width, height;
- int i, ir, ig, ib;
- unsigned long *colors;
+ int width, height, ncomp;
+ int ir, ig, ib;
Emacs_Pix_Container volatile ximg_volatile = NULL;
/* Open the JPEG file. */
jpeg_read_header (&mgr->cinfo, 1);
- /* Customize decompression so that color quantization will be used.
- Start decompression. */
- mgr->cinfo.quantize_colors = 1;
+ /* Start decompression. */
jpeg_start_decompress (&mgr->cinfo);
width = img->width = mgr->cinfo.output_width;
height = img->height = mgr->cinfo.output_height;
+ ncomp = mgr->cinfo.output_components;
+ if (ncomp > 2)
+ ir = 0, ig = 1, ib = 2;
+ else if (ncomp > 1)
+ ir = 0, ig = 1, ib = 0;
+ else
+ ir = 0, ig = 0, ib = 0;
if (!check_image_size (f, width, height))
{
sys_longjmp (mgr->setjmp_buffer, 1);
}
- /* Allocate colors. When color quantization is used,
- mgr->cinfo.actual_number_of_colors has been set with the number of
- colors generated, and mgr->cinfo.colormap is a two-dimensional array
- of color indices in the range 0..mgr->cinfo.actual_number_of_colors.
- No more than 255 colors will be generated. */
- USE_SAFE_ALLOCA;
- {
- if (mgr->cinfo.out_color_components > 2)
- ir = 0, ig = 1, ib = 2;
- else if (mgr->cinfo.out_color_components > 1)
- ir = 0, ig = 1, ib = 0;
- else
- ir = 0, ig = 0, ib = 0;
-
- /* Use the color table mechanism because it handles colors that
- cannot be allocated nicely. Such colors will be replaced with
- a default color, and we don't have to care about which colors
- can be freed safely, and which can't. */
- init_color_table ();
- SAFE_NALLOCA (colors, 1, mgr->cinfo.actual_number_of_colors);
-
- for (i = 0; i < mgr->cinfo.actual_number_of_colors; ++i)
- {
- /* Multiply RGB values with 255 because X expects RGB values
- in the range 0..0xffff. */
- int r = mgr->cinfo.colormap[ir][i] << 8;
- int g = mgr->cinfo.colormap[ig][i] << 8;
- int b = mgr->cinfo.colormap[ib][i] << 8;
- colors[i] = lookup_rgb_color (f, r, g, b);
- }
-
-#ifdef COLOR_TABLE_SUPPORT
- /* Remember those colors actually allocated. */
- img->colors = colors_in_color_table (&img->ncolors);
- free_color_table ();
-#endif /* COLOR_TABLE_SUPPORT */
- }
-
- /* Read pixels. */
- row_stride = width * mgr->cinfo.output_components;
+ /* Allocate scanlines buffer and Emacs color table. */
+ row_stride = width * ncomp;
buffer = mgr->cinfo.mem->alloc_sarray ((j_common_ptr) &mgr->cinfo,
JPOOL_IMAGE, row_stride, 1);
+ init_color_table ();
+
+ /* Fill the X image from JPEG data. */
for (y = 0; y < height; ++y)
{
jpeg_read_scanlines (&mgr->cinfo, buffer, 1);
- for (x = 0; x < mgr->cinfo.output_width; ++x)
- PUT_PIXEL (ximg, x, y, colors[buffer[0][x]]);
+ for (x = 0; x < width; ++x)
+ {
+ int off = x * ncomp;
+ /* Multiply RGB values with 255 because X expects RGB values
+ in the range 0..0xffff. */
+ int r = buffer[0][off + ir] << 8;
+ int g = buffer[0][off + ig] << 8;
+ int b = buffer[0][off + ib] << 8;
+ PUT_PIXEL (ximg, x, y, lookup_rgb_color (f, r, g, b));
+ }
}
+#ifdef COLOR_TABLE_SUPPORT
+ /* Remember those colors actually allocated. */
+ img->colors = colors_in_color_table (&img->ncolors);
+ free_color_table ();
+#endif /* COLOR_TABLE_SUPPORT */
+
/* Clean up. */
jpeg_finish_decompress (&mgr->cinfo);
jpeg_destroy_decompress (&mgr->cinfo);
/* Put ximg into the image. */
image_put_x_image (f, img, ximg, 0);
- SAFE_FREE ();
return 1;
}