static gboolean gdk_pixbuf__jpeg_image_load_lines (JpegProgContext *context, GError **error) { struct jpeg_decompress_struct *cinfo = &context->cinfo; guchar *lines[4]; guchar **lptr; guchar *rowptr; gint nlines, i; /* keep going until we've done all scanlines */ while (cinfo->output_scanline < cinfo->output_height) { lptr = lines; rowptr = context->dptr; for (i=0; i < cinfo->rec_outbuf_height; i++) { *lptr++ = rowptr; rowptr += context->pixbuf->rowstride; } nlines = jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height); if (nlines == 0) break; switch (cinfo->out_color_space) { case JCS_GRAYSCALE: explode_gray_into_buf (cinfo, lines); break; case JCS_RGB: /* do nothing */ break; case JCS_CMYK: convert_cmyk_to_rgb (cinfo, lines); break; default: if (error && *error == NULL) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Unsupported JPEG color space (%s)"), colorspace_name (cinfo->out_color_space)); } return FALSE; } context->dptr += nlines * context->pixbuf->rowstride; /* send updated signal */ if (context->updated_func) (* context->updated_func) (context->pixbuf, 0, cinfo->output_scanline - 1, cinfo->image_width, nlines, context->user_data); } return TRUE; }
/* * context - from image_begin_load * buf - new image data * size - length of new image data * * append image data onto inrecrementally built output image */ gboolean gdk_pixbuf__jpeg_image_load_increment (gpointer data, guchar *buf, guint size) { JpegProgContext *context = (JpegProgContext *)data; struct jpeg_decompress_struct *cinfo; my_src_ptr src; guint num_left, num_copy; guint last_bytes_left; guint spinguard; gboolean first; const guchar *bufhd; g_return_val_if_fail (context != NULL, FALSE); g_return_val_if_fail (buf != NULL, FALSE); src = (my_src_ptr) context->cinfo.src; cinfo = &context->cinfo; /* check for fatal error */ if (sigsetjmp (context->jerr.setjmp_buffer, 1)) { return FALSE; } /* skip over data if requested, handle unsigned int sizes cleanly */ /* only can happen if we've already called jpeg_get_header once */ if (context->src_initialized && src->skip_next) { if (src->skip_next > size) { src->skip_next -= size; return TRUE; } else { num_left = size - src->skip_next; bufhd = buf + src->skip_next; src->skip_next = 0; } } else { num_left = size; bufhd = buf; } if (num_left == 0) return TRUE; last_bytes_left = 0; spinguard = 0; first = TRUE; while (TRUE) { /* handle any data from caller we haven't processed yet */ if (num_left > 0) { if(src->pub.bytes_in_buffer && src->pub.next_input_byte != src->buffer) memmove(src->buffer, src->pub.next_input_byte, src->pub.bytes_in_buffer); num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer, num_left); memcpy(src->buffer + src->pub.bytes_in_buffer, bufhd,num_copy); src->pub.next_input_byte = src->buffer; src->pub.bytes_in_buffer += num_copy; bufhd += num_copy; num_left -= num_copy; } else { /* did anything change from last pass, if not return */ if (first) { last_bytes_left = src->pub.bytes_in_buffer; first = FALSE; } else if (src->pub.bytes_in_buffer == last_bytes_left) spinguard++; else last_bytes_left = src->pub.bytes_in_buffer; } /* should not go through twice and not pull bytes out of buf */ if (spinguard > 2) return TRUE; /* try to load jpeg header */ if (!context->got_header) { int rc; rc = jpeg_read_header (cinfo, TRUE); context->src_initialized = TRUE; if (rc == JPEG_SUSPENDED) continue; context->got_header = TRUE; } else if (!context->did_prescan) { int rc; /* start decompression */ cinfo->buffered_image = TRUE; rc = jpeg_start_decompress (cinfo); cinfo->do_fancy_upsampling = FALSE; cinfo->do_block_smoothing = FALSE; context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, cinfo->output_components == 4 ? TRUE : FALSE, 8, cinfo->image_width, cinfo->image_height); if (context->pixbuf == NULL) { return FALSE; } /* Use pixbuf buffer to store decompressed data */ context->dptr = context->pixbuf->pixels; /* Notify the client that we are ready to go */ (* context->prepared_func) (context->pixbuf, context->user_data); if (rc == JPEG_SUSPENDED) continue; context->did_prescan = TRUE; } else { /* we're decompressing so feed jpeg lib scanlines */ guchar *lines[4]; guchar **lptr; guchar *rowptr; gint nlines, i; /* keep going until we've done all passes */ while (!jpeg_input_complete (cinfo)) { if (!context->in_output) { if (jpeg_start_output (cinfo, cinfo->input_scan_number)) { context->in_output = TRUE; context->dptr = context->pixbuf->pixels; } else break; } /* keep going until we've done all scanlines */ while (cinfo->output_scanline < cinfo->output_height) { lptr = lines; rowptr = context->dptr; for (i=0; i < cinfo->rec_outbuf_height; i++) { *lptr++ = rowptr; rowptr += context->pixbuf->rowstride; } nlines = jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height); if (nlines == 0) break; switch (cinfo->out_color_space) { case JCS_GRAYSCALE: explode_gray_into_buf (cinfo, lines); break; case JCS_RGB: /* do nothing */ break; case JCS_CMYK: convert_cmyk_to_rgb (cinfo, lines); break; default: return FALSE; } context->dptr += nlines * context->pixbuf->rowstride; /* send updated signal */ (* context->updated_func) (context->pixbuf, 0, cinfo->output_scanline-1, cinfo->image_width, nlines, context->user_data); } if (cinfo->output_scanline >= cinfo->output_height && jpeg_finish_output (cinfo)) context->in_output = FALSE; else break; } if (jpeg_input_complete (cinfo)) /* did entire image */ return TRUE; else continue; } } return TRUE; }
/* Shared library entry point */ static GdkPixbuf * gdk_pixbuf__jpeg_image_load (FILE *f, GError **error) { gint i; int is_otag; char otag_str[5]; GdkPixbuf * volatile pixbuf = NULL; guchar *dptr; guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height, * from the header file: * " Usually rec_outbuf_height will be 1 or 2, * at most 4." */ guchar **lptr; struct jpeg_decompress_struct cinfo; struct error_handler_data jerr; stdio_src_ptr src; /* setup error handler */ cinfo.err = jpeg_std_error (&jerr.pub); jerr.pub.error_exit = fatal_error_handler; jerr.pub.output_message = output_message_handler; jerr.error = error; if (sigsetjmp (jerr.setjmp_buffer, 1)) { /* Whoops there was a jpeg error */ if (pixbuf) g_object_unref (pixbuf); jpeg_destroy_decompress (&cinfo); /* error should have been set by fatal_error_handler () */ return NULL; } /* load header, setup */ jpeg_create_decompress (&cinfo); cinfo.src = (struct jpeg_source_mgr *) (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT, sizeof (stdio_source_mgr)); src = (stdio_src_ptr) cinfo.src; src->buffer = (JOCTET *) (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT, JPEG_PROG_BUF_SIZE * sizeof (JOCTET)); src->pub.init_source = stdio_init_source; src->pub.fill_input_buffer = stdio_fill_input_buffer; src->pub.skip_input_data = stdio_skip_input_data; src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ src->pub.term_source = stdio_term_source; src->infile = f; src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ src->pub.next_input_byte = NULL; /* until buffer loaded */ jpeg_save_markers (&cinfo, EXIF_JPEG_MARKER, 0xffff); jpeg_read_header (&cinfo, TRUE); /* check for orientation tag */ is_otag = get_orientation (&cinfo); jpeg_start_decompress (&cinfo); cinfo.do_fancy_upsampling = FALSE; cinfo.do_block_smoothing = FALSE; pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.out_color_components == 4 ? TRUE : FALSE, 8, cinfo.output_width, cinfo.output_height); if (!pixbuf) { jpeg_destroy_decompress (&cinfo); /* broken check for *error == NULL for robustness against * crappy JPEG library */ if (error && *error == NULL) { g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Insufficient memory to load image, try exiting some applications to free memory")); } return NULL; } /* if orientation tag was found set an option to remember its value */ if (is_otag) { g_snprintf (otag_str, sizeof (otag_str), "%d", is_otag); gdk_pixbuf_set_option (pixbuf, "orientation", otag_str); } dptr = pixbuf->pixels; /* decompress all the lines, a few at a time */ while (cinfo.output_scanline < cinfo.output_height) { lptr = lines; for (i = 0; i < cinfo.rec_outbuf_height; i++) { *lptr++ = dptr; dptr += pixbuf->rowstride; } jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height); switch (cinfo.out_color_space) { case JCS_GRAYSCALE: explode_gray_into_buf (&cinfo, lines); break; case JCS_RGB: /* do nothing */ break; case JCS_CMYK: convert_cmyk_to_rgb (&cinfo, lines); break; default: g_object_unref (pixbuf); if (error && *error == NULL) { g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Unsupported JPEG color space (%s)"), colorspace_name (cinfo.out_color_space)); } jpeg_destroy_decompress (&cinfo); return NULL; } } jpeg_finish_decompress (&cinfo); jpeg_destroy_decompress (&cinfo); return pixbuf; }
/* Shared library entry point */ GdkPixbuf * gdk_pixbuf__jpeg_image_load (FILE *f) { gint i; GdkPixbuf * volatile pixbuf = NULL; guchar *dptr; guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height, * from the header file: * " Usually rec_outbuf_height will be 1 or 2, * at most 4." */ guchar **lptr; struct jpeg_decompress_struct cinfo; struct error_handler_data jerr; stdio_src_ptr src; /* setup error handler */ cinfo.err = jpeg_std_error (&jerr.pub); jerr.pub.error_exit = fatal_error_handler; if (sigsetjmp (jerr.setjmp_buffer, 1)) { /* Whoops there was a jpeg error */ if (pixbuf) gdk_pixbuf_unref (pixbuf); jpeg_destroy_decompress (&cinfo); return NULL; } /* load header, setup */ jpeg_create_decompress (&cinfo); cinfo.src = (struct jpeg_source_mgr *) (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT, sizeof (stdio_source_mgr)); src = (stdio_src_ptr) cinfo.src; src->buffer = (JOCTET *) (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT, JPEG_PROG_BUF_SIZE * sizeof (JOCTET)); src->pub.init_source = stdio_init_source; src->pub.fill_input_buffer = stdio_fill_input_buffer; src->pub.skip_input_data = stdio_skip_input_data; src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ src->pub.term_source = stdio_term_source; src->infile = f; src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ src->pub.next_input_byte = NULL; /* until buffer loaded */ jpeg_read_header (&cinfo, TRUE); jpeg_start_decompress (&cinfo); cinfo.do_fancy_upsampling = FALSE; cinfo.do_block_smoothing = FALSE; pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.out_color_components == 4 ? TRUE : FALSE, 8, cinfo.output_width, cinfo.output_height); if (!pixbuf) { jpeg_destroy_decompress (&cinfo); return NULL; } dptr = pixbuf->pixels; /* decompress all the lines, a few at a time */ while (cinfo.output_scanline < cinfo.output_height) { lptr = lines; for (i = 0; i < cinfo.rec_outbuf_height; i++) { *lptr++ = dptr; dptr += pixbuf->rowstride; } jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height); switch (cinfo.out_color_space) { case JCS_GRAYSCALE: explode_gray_into_buf (&cinfo, lines); break; case JCS_RGB: /* do nothing */ break; case JCS_CMYK: convert_cmyk_to_rgb (&cinfo, lines); break; default: gdk_pixbuf_unref (pixbuf); jpeg_destroy_decompress (&cinfo); return NULL; } } jpeg_finish_decompress (&cinfo); jpeg_destroy_decompress (&cinfo); return pixbuf; }