+ free (buf);
+ return ximage;
+
+ FAIL:
+ if (buf) free (buf);
+ if (ximage && ximage->data)
+ {
+ free (ximage->data);
+ ximage->data = 0;
+ }
+ if (ximage) XDestroyImage (ximage);
+ fseek (in, 0, SEEK_SET);
+ return 0;
+}
+
+
+typedef struct {
+ struct jpeg_error_mgr pub; /* this is what passes for subclassing in C */
+ const char *filename;
+ Screen *screen;
+ Visual *visual;
+ Drawable drawable;
+ Colormap cmap;
+} getimg_jpg_error_mgr;
+
+
+static void
+jpg_output_message (j_common_ptr cinfo)
+{
+ getimg_jpg_error_mgr *err = (getimg_jpg_error_mgr *) cinfo->err;
+ char buf[JMSG_LENGTH_MAX];
+ cinfo->err->format_message (cinfo, buf);
+ fprintf (stderr, "%s: %s: %s\n", progname, err->filename, buf);
+}
+
+
+static void
+jpg_error_exit (j_common_ptr cinfo)
+{
+ getimg_jpg_error_mgr *err = (getimg_jpg_error_mgr *) cinfo->err;
+ cinfo->err->output_message (cinfo);
+ draw_colorbars (err->screen, err->visual, err->drawable, err->cmap,
+ 0, 0, 0, 0);
+ XSync (DisplayOfScreen (err->screen), False);
+ exit (1);
+}
+
+
+/* Reads a JPEG file, returns an RGB XImage of it.
+ */
+static XImage *
+read_jpeg_ximage (Screen *screen, Visual *visual, Drawable drawable,
+ Colormap cmap, const char *filename, Bool verbose_p)
+{
+ Display *dpy = DisplayOfScreen (screen);
+ int depth = visual_depth (screen, visual);
+
+ FILE *in = 0;
+ XImage *ximage = 0;
+ struct jpeg_decompress_struct cinfo;
+ getimg_jpg_error_mgr jerr;
+ JSAMPARRAY scanbuf = 0;
+ int y;
+
+ jerr.filename = filename;
+ jerr.screen = screen;
+ jerr.visual = visual;
+ jerr.drawable = drawable;
+ jerr.cmap = cmap;
+
+ if (! (depth >= 15 || depth == 12 || depth == 8))
+ {
+ fprintf (stderr, "%s: unsupported depth: %d\n", progname, depth);
+ goto FAIL;
+ }
+
+ in = fopen (filename, "rb");
+ if (!in)
+ {
+ fprintf (stderr, "%s: %s: unreadable\n", progname, filename);
+ goto FAIL;
+ }
+
+ /* Check to see if it's a PPM, and if so, read that instead of using
+ the JPEG library. Yeah, this is all modular and stuff.
+ */
+ if ((ximage = maybe_read_ppm (screen, visual, filename, in, verbose_p)))
+ {
+ fclose (in);
+ return ximage;
+ }
+
+ cinfo.err = jpeg_std_error (&jerr.pub);
+ jerr.pub.output_message = jpg_output_message;
+ jerr.pub.error_exit = jpg_error_exit;
+
+ jpeg_create_decompress (&cinfo);
+ jpeg_stdio_src (&cinfo, in);
+ jpeg_read_header (&cinfo, TRUE);
+
+ /* set some decode parameters */
+ cinfo.out_color_space = JCS_RGB;
+ cinfo.quantize_colors = FALSE;
+
+ jpeg_start_decompress (&cinfo);
+
+ ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
+ cinfo.output_width, cinfo.output_height,
+ 8, 0);
+ if (ximage)
+ ximage->data = (char *) calloc (ximage->height, ximage->bytes_per_line);
+
+ if (ximage && ximage->data)
+ scanbuf = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE,
+ cinfo.rec_outbuf_height *
+ cinfo.output_width *
+ cinfo.output_components,
+ 1);
+ if (!ximage || !ximage->data || !scanbuf)
+ {
+ fprintf (stderr, "%s: out of memory loading %dx%d file %s\n",
+ progname, ximage->width, ximage->height, filename);
+ goto FAIL;
+ }
+
+ y = 0;
+ while (cinfo.output_scanline < cinfo.output_height)
+ {
+ int n = jpeg_read_scanlines (&cinfo, scanbuf, 1);
+ int i;
+ for (i = 0; i < n; i++)