1 /* -*- mode: C; c-file-style: "linux" -*- */
2 /* GdkPixbuf library - JPEG image loader
4 * Copyright (C) 1999 Michael Zucchi
5 * Copyright (C) 1999 The Free Software Foundation
7 * Progressive loading code Copyright (C) 1999 Red Hat, Inc.
9 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
10 * Federico Mena-Quintero <federico@gimp.org>
11 * Michael Fulbright <drmike@redhat.com>
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the
25 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
37 #include "gdk-pixbuf-private.h"
38 #include "gdk-pixbuf-io.h"
40 #ifndef HAVE_SIGSETJMP
41 #define sigjmp_buf jmp_buf
42 #define sigsetjmp(jb, x) setjmp(jb)
43 #define siglongjmp longjmp
47 /* we are a "source manager" as far as libjpeg is concerned */
48 #define JPEG_PROG_BUF_SIZE 65536
51 struct jpeg_source_mgr pub; /* public fields */
53 JOCTET buffer[JPEG_PROG_BUF_SIZE]; /* start of buffer */
54 long skip_next; /* number of bytes to skip next read */
58 typedef my_source_mgr * my_src_ptr;
60 /* error handler data */
61 struct error_handler_data {
62 struct jpeg_error_mgr pub;
63 sigjmp_buf setjmp_buffer;
67 /* progressive loader context */
69 GdkPixbufModuleSizeFunc size_func;
70 GdkPixbufModuleUpdatedFunc updated_func;
71 GdkPixbufModulePreparedFunc prepared_func;
75 guchar *dptr; /* current position in pixbuf */
77 gboolean did_prescan; /* are we in image data yet? */
78 gboolean got_header; /* have we loaded jpeg header? */
79 gboolean src_initialized;/* TRUE when jpeg lib initialized */
80 gboolean in_output; /* did we get suspended in an output pass? */
81 struct jpeg_decompress_struct cinfo;
82 struct error_handler_data jerr;
85 static GdkPixbuf *gdk_pixbuf__jpeg_image_load (FILE *f, GError **error);
86 static gpointer gdk_pixbuf__jpeg_image_begin_load (GdkPixbufModuleSizeFunc func0,
87 GdkPixbufModulePreparedFunc func1,
88 GdkPixbufModuleUpdatedFunc func2,
91 static gboolean gdk_pixbuf__jpeg_image_stop_load (gpointer context, GError **error);
92 static gboolean gdk_pixbuf__jpeg_image_load_increment(gpointer context,
93 const guchar *buf, guint size,
98 fatal_error_handler (j_common_ptr cinfo)
100 struct error_handler_data *errmgr;
101 char buffer[JMSG_LENGTH_MAX];
103 errmgr = (struct error_handler_data *) cinfo->err;
105 /* Create the message */
106 (* cinfo->err->format_message) (cinfo, buffer);
108 /* broken check for *error == NULL for robustness against
109 * crappy JPEG library
111 if (errmgr->error && *errmgr->error == NULL) {
112 g_set_error (errmgr->error,
114 cinfo->err->msg_code == JERR_OUT_OF_MEMORY
115 ? GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY
116 : GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
117 _("Error interpreting JPEG image file (%s)"),
121 siglongjmp (errmgr->setjmp_buffer, 1);
123 g_assert_not_reached ();
127 output_message_handler (j_common_ptr cinfo)
129 /* This method keeps libjpeg from dumping crap to stderr */
134 /* explode gray image data from jpeg library into rgb components in pixbuf */
136 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
142 g_return_if_fail (cinfo != NULL);
143 g_return_if_fail (cinfo->output_components == 1);
144 g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
146 /* Expand grey->colour. Expand from the end of the
147 * memory down, so we can use the same buffer.
149 w = cinfo->output_width;
150 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
153 from = lines[i] + w - 1;
154 to = lines[i] + (w - 1) * 3;
155 for (j = w - 1; j >= 0; j--) {
167 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
172 g_return_if_fail (cinfo != NULL);
173 g_return_if_fail (cinfo->output_components == 4);
174 g_return_if_fail (cinfo->out_color_space == JCS_CMYK);
176 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
180 for (j = 0; j < cinfo->output_width; j++) {
186 if (cinfo->saw_Adobe_marker) {
192 p[0] = (255 - k)*(255 - c) / 255;
193 p[1] = (255 - k)*(255 - m) / 255;
194 p[2] = (255 - k)*(255 - y) / 255;
203 struct jpeg_source_mgr pub; /* public fields */
205 FILE * infile; /* source stream */
206 JOCTET * buffer; /* start of buffer */
207 boolean start_of_file; /* have we gotten any data yet? */
210 typedef stdio_source_mgr * stdio_src_ptr;
213 stdio_init_source (j_decompress_ptr cinfo)
215 stdio_src_ptr src = (stdio_src_ptr)cinfo->src;
216 src->start_of_file = FALSE;
220 stdio_fill_input_buffer (j_decompress_ptr cinfo)
222 stdio_src_ptr src = (stdio_src_ptr) cinfo->src;
225 nbytes = fread (src->buffer, 1, JPEG_PROG_BUF_SIZE, src->infile);
229 if (src->start_of_file) /* Treat empty input file as fatal error */
230 ERREXIT(cinfo, JERR_INPUT_EMPTY);
231 WARNMS(cinfo, JWRN_JPEG_EOF);
233 /* Insert a fake EOI marker */
234 src->buffer[0] = (JOCTET) 0xFF;
235 src->buffer[1] = (JOCTET) JPEG_EOI;
239 src->pub.next_input_byte = src->buffer;
240 src->pub.bytes_in_buffer = nbytes;
241 src->start_of_file = FALSE;
247 stdio_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
249 stdio_src_ptr src = (stdio_src_ptr) cinfo->src;
252 while (num_bytes > (long) src->pub.bytes_in_buffer) {
253 num_bytes -= (long) src->pub.bytes_in_buffer;
254 (void)stdio_fill_input_buffer(cinfo);
256 src->pub.next_input_byte += (size_t) num_bytes;
257 src->pub.bytes_in_buffer -= (size_t) num_bytes;
262 stdio_term_source (j_decompress_ptr cinfo)
267 colorspace_name (const J_COLOR_SPACE jpeg_color_space)
269 switch (jpeg_color_space) {
270 case JCS_UNKNOWN: return "UNKNOWN";
271 case JCS_GRAYSCALE: return "GRAYSCALE";
272 case JCS_RGB: return "RGB";
273 case JCS_YCbCr: return "YCbCr";
274 case JCS_CMYK: return "CMYK";
275 case JCS_YCCK: return "YCCK";
276 default: return "invalid";
280 /* Shared library entry point */
282 gdk_pixbuf__jpeg_image_load (FILE *f, GError **error)
285 GdkPixbuf * volatile pixbuf = NULL;
287 guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height,
288 * from the header file:
289 * " Usually rec_outbuf_height will be 1 or 2,
293 struct jpeg_decompress_struct cinfo;
294 struct error_handler_data jerr;
297 /* setup error handler */
298 cinfo.err = jpeg_std_error (&jerr.pub);
299 jerr.pub.error_exit = fatal_error_handler;
300 jerr.pub.output_message = output_message_handler;
304 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
305 /* Whoops there was a jpeg error */
307 g_object_unref (pixbuf);
309 jpeg_destroy_decompress (&cinfo);
311 /* error should have been set by fatal_error_handler () */
315 /* load header, setup */
316 jpeg_create_decompress (&cinfo);
318 cinfo.src = (struct jpeg_source_mgr *)
319 (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
320 sizeof (stdio_source_mgr));
321 src = (stdio_src_ptr) cinfo.src;
322 src->buffer = (JOCTET *)
323 (*cinfo.mem->alloc_small) ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
324 JPEG_PROG_BUF_SIZE * sizeof (JOCTET));
326 src->pub.init_source = stdio_init_source;
327 src->pub.fill_input_buffer = stdio_fill_input_buffer;
328 src->pub.skip_input_data = stdio_skip_input_data;
329 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
330 src->pub.term_source = stdio_term_source;
332 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
333 src->pub.next_input_byte = NULL; /* until buffer loaded */
335 jpeg_read_header (&cinfo, TRUE);
336 jpeg_start_decompress (&cinfo);
337 cinfo.do_fancy_upsampling = FALSE;
338 cinfo.do_block_smoothing = FALSE;
340 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
341 cinfo.out_color_components == 4 ? TRUE : FALSE,
342 8, cinfo.output_width, cinfo.output_height);
345 jpeg_destroy_decompress (&cinfo);
347 /* broken check for *error == NULL for robustness against
348 * crappy JPEG library
350 if (error && *error == NULL) {
353 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
354 _("Insufficient memory to load image, try exiting some applications to free memory"));
360 dptr = pixbuf->pixels;
362 /* decompress all the lines, a few at a time */
363 while (cinfo.output_scanline < cinfo.output_height) {
365 for (i = 0; i < cinfo.rec_outbuf_height; i++) {
367 dptr += pixbuf->rowstride;
370 jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height);
372 switch (cinfo.out_color_space) {
374 explode_gray_into_buf (&cinfo, lines);
380 convert_cmyk_to_rgb (&cinfo, lines);
383 g_object_unref (pixbuf);
384 if (error && *error == NULL) {
387 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
388 _("Unsupported JPEG color space (%s)"),
389 colorspace_name (cinfo.out_color_space));
392 jpeg_destroy_decompress (&cinfo);
397 jpeg_finish_decompress (&cinfo);
398 jpeg_destroy_decompress (&cinfo);
404 /**** Progressive image loading handling *****/
406 /* these routines required because we are acting as a source manager for */
409 init_source (j_decompress_ptr cinfo)
411 my_src_ptr src = (my_src_ptr) cinfo->src;
418 term_source (j_decompress_ptr cinfo)
420 /* XXXX - probably should scream something has happened */
424 /* for progressive loading (called "I/O Suspension" by libjpeg docs) */
425 /* we do nothing except return "FALSE" */
427 fill_input_buffer (j_decompress_ptr cinfo)
434 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
436 my_src_ptr src = (my_src_ptr) cinfo->src;
439 /* move as far as we can into current buffer */
440 /* then set skip_next to catch the rest */
442 num_can_do = MIN (src->pub.bytes_in_buffer, num_bytes);
443 src->pub.next_input_byte += (size_t) num_can_do;
444 src->pub.bytes_in_buffer -= (size_t) num_can_do;
446 src->skip_next = num_bytes - num_can_do;
452 * func - called when we have pixmap created (but no image data)
453 * user_data - passed as arg 1 to func
454 * return context (opaque to user)
458 gdk_pixbuf__jpeg_image_begin_load (GdkPixbufModuleSizeFunc size_func,
459 GdkPixbufModulePreparedFunc prepared_func,
460 GdkPixbufModuleUpdatedFunc updated_func,
464 JpegProgContext *context;
467 context = g_new0 (JpegProgContext, 1);
468 context->size_func = size_func;
469 context->prepared_func = prepared_func;
470 context->updated_func = updated_func;
471 context->user_data = user_data;
472 context->pixbuf = NULL;
473 context->got_header = FALSE;
474 context->did_prescan = FALSE;
475 context->src_initialized = FALSE;
476 context->in_output = FALSE;
478 /* create libjpeg structures */
479 jpeg_create_decompress (&context->cinfo);
481 context->cinfo.src = (struct jpeg_source_mgr *) g_try_malloc (sizeof (my_source_mgr));
482 if (!context->cinfo.src) {
485 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
486 _("Couldn't allocate memory for loading JPEG file"));
489 memset (context->cinfo.src, 0, sizeof (my_source_mgr));
491 src = (my_src_ptr) context->cinfo.src;
493 context->cinfo.err = jpeg_std_error (&context->jerr.pub);
494 context->jerr.pub.error_exit = fatal_error_handler;
495 context->jerr.pub.output_message = output_message_handler;
496 context->jerr.error = error;
498 src = (my_src_ptr) context->cinfo.src;
499 src->pub.init_source = init_source;
500 src->pub.fill_input_buffer = fill_input_buffer;
501 src->pub.skip_input_data = skip_input_data;
502 src->pub.resync_to_restart = jpeg_resync_to_restart;
503 src->pub.term_source = term_source;
504 src->pub.bytes_in_buffer = 0;
505 src->pub.next_input_byte = NULL;
507 context->jerr.error = NULL;
509 return (gpointer) context;
513 * context - returned from image_begin_load
515 * free context, unref gdk_pixbuf
518 gdk_pixbuf__jpeg_image_stop_load (gpointer data, GError **error)
520 JpegProgContext *context = (JpegProgContext *) data;
523 g_return_val_if_fail (context != NULL, TRUE);
525 /* FIXME this thing needs to report errors if
526 * we have unused image data
530 g_object_unref (context->pixbuf);
532 /* if we have an error? */
533 if (sigsetjmp (context->jerr.setjmp_buffer, 1)) {
536 jpeg_finish_decompress (&context->cinfo);
540 jpeg_destroy_decompress (&context->cinfo);
542 if (context->cinfo.src) {
543 my_src_ptr src = (my_src_ptr) context->cinfo.src;
555 gdk_pixbuf__jpeg_image_load_lines (JpegProgContext *context,
558 struct jpeg_decompress_struct *cinfo = &context->cinfo;
564 /* keep going until we've done all scanlines */
565 while (cinfo->output_scanline < cinfo->output_height) {
567 rowptr = context->dptr;
568 for (i=0; i < cinfo->rec_outbuf_height; i++) {
570 rowptr += context->pixbuf->rowstride;
573 nlines = jpeg_read_scanlines (cinfo, lines,
574 cinfo->rec_outbuf_height);
578 switch (cinfo->out_color_space) {
580 explode_gray_into_buf (cinfo, lines);
586 convert_cmyk_to_rgb (cinfo, lines);
589 if (error && *error == NULL) {
592 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
593 _("Unsupported JPEG color space (%s)"),
594 colorspace_name (cinfo->out_color_space));
600 context->dptr += nlines * context->pixbuf->rowstride;
602 /* send updated signal */
603 if (context->updated_func)
604 (* context->updated_func) (context->pixbuf,
606 cinfo->output_scanline - 1,
617 * context - from image_begin_load
618 * buf - new image data
619 * size - length of new image data
621 * append image data onto inrecrementally built output image
624 gdk_pixbuf__jpeg_image_load_increment (gpointer data,
625 const guchar *buf, guint size,
628 JpegProgContext *context = (JpegProgContext *)data;
629 struct jpeg_decompress_struct *cinfo;
631 guint num_left, num_copy;
632 guint last_bytes_left;
638 g_return_val_if_fail (context != NULL, FALSE);
639 g_return_val_if_fail (buf != NULL, FALSE);
641 src = (my_src_ptr) context->cinfo.src;
643 cinfo = &context->cinfo;
645 context->jerr.error = error;
647 /* check for fatal error */
648 if (sigsetjmp (context->jerr.setjmp_buffer, 1)) {
652 /* skip over data if requested, handle unsigned int sizes cleanly */
653 /* only can happen if we've already called jpeg_get_header once */
654 if (context->src_initialized && src->skip_next) {
655 if (src->skip_next > size) {
656 src->skip_next -= size;
659 num_left = size - src->skip_next;
660 bufhd = buf + src->skip_next;
676 /* handle any data from caller we haven't processed yet */
678 if(src->pub.bytes_in_buffer &&
679 src->pub.next_input_byte != src->buffer)
680 memmove(src->buffer, src->pub.next_input_byte,
681 src->pub.bytes_in_buffer);
684 num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
687 memcpy(src->buffer + src->pub.bytes_in_buffer, bufhd,num_copy);
688 src->pub.next_input_byte = src->buffer;
689 src->pub.bytes_in_buffer += num_copy;
691 num_left -= num_copy;
694 /* did anything change from last pass, if not return */
696 last_bytes_left = src->pub.bytes_in_buffer;
698 } else if (src->pub.bytes_in_buffer == last_bytes_left)
701 last_bytes_left = src->pub.bytes_in_buffer;
703 /* should not go through twice and not pull bytes out of buf */
707 /* try to load jpeg header */
708 if (!context->got_header) {
711 rc = jpeg_read_header (cinfo, TRUE);
712 context->src_initialized = TRUE;
714 if (rc == JPEG_SUSPENDED)
717 context->got_header = TRUE;
719 width = cinfo->image_width;
720 height = cinfo->image_height;
721 if (context->size_func) {
722 (* context->size_func) (&width, &height, context->user_data);
723 if (width == 0 || height == 0) {
726 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
727 _("Transformed JPEG has zero width or height."));
732 for (cinfo->scale_denom = 2; cinfo->scale_denom <= 8; cinfo->scale_denom *= 2) {
733 jpeg_calc_output_dimensions (cinfo);
734 if (cinfo->output_width < width || cinfo->output_height < height) {
735 cinfo->scale_denom /= 2;
739 jpeg_calc_output_dimensions (cinfo);
741 context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
742 cinfo->output_components == 4 ? TRUE : FALSE,
745 cinfo->output_height);
747 if (context->pixbuf == NULL) {
750 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
751 _("Couldn't allocate memory for loading JPEG file"));
755 /* Use pixbuf buffer to store decompressed data */
756 context->dptr = context->pixbuf->pixels;
758 /* Notify the client that we are ready to go */
759 if (context->prepared_func)
760 (* context->prepared_func) (context->pixbuf,
764 } else if (!context->did_prescan) {
767 /* start decompression */
768 cinfo->buffered_image = cinfo->progressive_mode;
769 rc = jpeg_start_decompress (cinfo);
770 cinfo->do_fancy_upsampling = FALSE;
771 cinfo->do_block_smoothing = FALSE;
773 if (rc == JPEG_SUSPENDED)
776 context->did_prescan = TRUE;
777 } else if (!cinfo->buffered_image) {
778 /* we're decompressing unbuffered so
779 * simply get scanline by scanline from jpeg lib
781 if (! gdk_pixbuf__jpeg_image_load_lines (context,
785 if (cinfo->output_scanline >= cinfo->output_height)
788 /* we're decompressing buffered (progressive)
789 * so feed jpeg lib scanlines
792 /* keep going until we've done all passes */
793 while (!jpeg_input_complete (cinfo)) {
794 if (!context->in_output) {
795 if (jpeg_start_output (cinfo, cinfo->input_scan_number)) {
796 context->in_output = TRUE;
797 context->dptr = context->pixbuf->pixels;
803 /* get scanlines from jpeg lib */
804 if (! gdk_pixbuf__jpeg_image_load_lines (context,
808 if (cinfo->output_scanline >= cinfo->output_height &&
809 jpeg_finish_output (cinfo))
810 context->in_output = FALSE;
814 if (jpeg_input_complete (cinfo))
815 /* did entire image */
825 #define TO_FUNCTION_BUF_SIZE 4096
828 struct jpeg_destination_mgr pub;
830 GdkPixbufSaveFunc save_func;
833 } ToFunctionDestinationManager;
836 to_callback_init (j_compress_ptr cinfo)
838 ToFunctionDestinationManager *destmgr;
840 destmgr = (ToFunctionDestinationManager*) cinfo->dest;
841 destmgr->pub.next_output_byte = destmgr->buffer;
842 destmgr->pub.free_in_buffer = TO_FUNCTION_BUF_SIZE;
846 to_callback_do_write (j_compress_ptr cinfo, gsize length)
848 ToFunctionDestinationManager *destmgr;
850 destmgr = (ToFunctionDestinationManager*) cinfo->dest;
851 if (!destmgr->save_func (destmgr->buffer,
854 destmgr->user_data)) {
855 struct error_handler_data *errmgr;
857 errmgr = (struct error_handler_data *) cinfo->err;
858 /* Use a default error message if the callback didn't set one,
859 * which it should have.
861 if (errmgr->error && *errmgr->error == NULL) {
862 g_set_error (errmgr->error,
864 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
865 "write function failed");
867 siglongjmp (errmgr->setjmp_buffer, 1);
868 g_assert_not_reached ();
873 to_callback_empty_output_buffer (j_compress_ptr cinfo)
875 ToFunctionDestinationManager *destmgr;
877 destmgr = (ToFunctionDestinationManager*) cinfo->dest;
878 to_callback_do_write (cinfo, TO_FUNCTION_BUF_SIZE);
879 destmgr->pub.next_output_byte = destmgr->buffer;
880 destmgr->pub.free_in_buffer = TO_FUNCTION_BUF_SIZE;
885 to_callback_terminate (j_compress_ptr cinfo)
887 ToFunctionDestinationManager *destmgr;
889 destmgr = (ToFunctionDestinationManager*) cinfo->dest;
890 to_callback_do_write (cinfo, TO_FUNCTION_BUF_SIZE - destmgr->pub.free_in_buffer);
894 real_save_jpeg (GdkPixbuf *pixbuf,
898 gboolean to_callback,
900 GdkPixbufSaveFunc save_func,
903 /* FIXME error handling is broken */
905 struct jpeg_compress_struct cinfo;
908 guchar *pixels = NULL;
911 volatile int quality = 75; /* default; must be between 0 and 100 */
916 struct error_handler_data jerr;
917 ToFunctionDestinationManager to_callback_destmgr;
919 to_callback_destmgr.buffer = NULL;
922 gchar **kiter = keys;
923 gchar **viter = values;
926 if (strcmp (*kiter, "quality") == 0) {
928 quality = strtol (*viter, &endptr, 10);
930 if (endptr == *viter) {
933 GDK_PIXBUF_ERROR_BAD_OPTION,
934 _("JPEG quality must be a value between 0 and 100; value '%s' could not be parsed."),
942 /* This is a user-visible error;
943 * lets people skip the range-checking
948 GDK_PIXBUF_ERROR_BAD_OPTION,
949 _("JPEG quality must be a value between 0 and 100; value '%d' is not allowed."),
955 g_warning ("Bad option name '%s' passed to JPEG saver",
965 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
966 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
968 w = gdk_pixbuf_get_width (pixbuf);
969 h = gdk_pixbuf_get_height (pixbuf);
971 /* no image data? abort */
972 pixels = gdk_pixbuf_get_pixels (pixbuf);
973 g_return_val_if_fail (pixels != NULL, FALSE);
975 /* Allocate a small buffer to convert image data,
976 * and a larger buffer if doing to_callback save.
978 buf = g_try_malloc (w * 3 * sizeof (guchar));
982 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
983 _("Couldn't allocate memory for loading JPEG file"));
987 to_callback_destmgr.buffer = g_try_malloc (TO_FUNCTION_BUF_SIZE);
988 if (!to_callback_destmgr.buffer) {
991 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
992 _("Couldn't allocate memory for loading JPEG file"));
997 /* set up error handling */
998 jerr.pub.error_exit = fatal_error_handler;
999 jerr.pub.output_message = output_message_handler;
1002 cinfo.err = jpeg_std_error (&(jerr.pub));
1003 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
1004 jpeg_destroy_compress (&cinfo);
1006 g_free (to_callback_destmgr.buffer);
1010 /* setup compress params */
1011 jpeg_create_compress (&cinfo);
1013 to_callback_destmgr.pub.init_destination = to_callback_init;
1014 to_callback_destmgr.pub.empty_output_buffer = to_callback_empty_output_buffer;
1015 to_callback_destmgr.pub.term_destination = to_callback_terminate;
1016 to_callback_destmgr.error = error;
1017 to_callback_destmgr.save_func = save_func;
1018 to_callback_destmgr.user_data = user_data;
1019 cinfo.dest = (struct jpeg_destination_mgr*) &to_callback_destmgr;
1021 jpeg_stdio_dest (&cinfo, f);
1023 cinfo.image_width = w;
1024 cinfo.image_height = h;
1025 cinfo.input_components = 3;
1026 cinfo.in_color_space = JCS_RGB;
1028 /* set up jepg compression parameters */
1029 jpeg_set_defaults (&cinfo);
1030 jpeg_set_quality (&cinfo, quality, TRUE);
1031 jpeg_start_compress (&cinfo, TRUE);
1032 /* get the start pointer */
1034 /* go one scanline at a time... and save */
1036 while (cinfo.next_scanline < cinfo.image_height) {
1037 /* convert scanline from ARGB to RGB packed */
1038 for (j = 0; j < w; j++)
1039 memcpy (&(buf[j*3]), &(ptr[i*rowstride + j*n_channels]), 3);
1041 /* write scanline */
1042 jbuf = (JSAMPROW *)(&buf);
1043 jpeg_write_scanlines (&cinfo, jbuf, 1);
1050 jpeg_finish_compress (&cinfo);
1051 jpeg_destroy_compress(&cinfo);
1053 g_free (to_callback_destmgr.buffer);
1058 gdk_pixbuf__jpeg_image_save (FILE *f,
1064 return real_save_jpeg (pixbuf, keys, values, error,
1065 FALSE, f, NULL, NULL);
1069 gdk_pixbuf__jpeg_image_save_to_callback (GdkPixbufSaveFunc save_func,
1076 return real_save_jpeg (pixbuf, keys, values, error,
1077 TRUE, NULL, save_func, user_data);
1080 #ifndef INCLUDE_jpeg
1081 #define MODULE_ENTRY(type,function) function
1083 #define MODULE_ENTRY(type,function) _gdk_pixbuf__ ## type ## _ ## function
1087 MODULE_ENTRY (jpeg, fill_vtable) (GdkPixbufModule *module)
1089 module->load = gdk_pixbuf__jpeg_image_load;
1090 module->begin_load = gdk_pixbuf__jpeg_image_begin_load;
1091 module->stop_load = gdk_pixbuf__jpeg_image_stop_load;
1092 module->load_increment = gdk_pixbuf__jpeg_image_load_increment;
1093 module->save = gdk_pixbuf__jpeg_image_save;
1094 module->save_to_callback = gdk_pixbuf__jpeg_image_save_to_callback;
1098 MODULE_ENTRY (jpeg, fill_info) (GdkPixbufFormat *info)
1100 static GdkPixbufModulePattern signature[] = {
1101 { "\xff\xd8", NULL, 100 },
1104 static gchar * mime_types[] = {
1108 static gchar * extensions[] = {
1115 info->name = "jpeg";
1116 info->signature = signature;
1117 info->description = N_("The JPEG image format");
1118 info->mime_types = mime_types;
1119 info->extensions = extensions;
1120 info->flags = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
1121 info->license = "LGPL";