1 /* GdkPixbuf library - PNM image loader
3 * Copyright (C) 1999 Red Hat, Inc.
5 * Authors: Michael Fulbright <drmike@redhat.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * raw pnm, pgm, and pbm files will load now.
27 * ascii support coming soon.
29 * next will be incremental loading.
40 #include "gdk-pixbuf.h"
41 #include "gdk-pixbuf-io.h"
46 #define PNM_BUF_SIZE 4096
50 #define PNM_FATAL_ERR -1
62 guchar buffer[PNM_BUF_SIZE];
68 ModuleUpdatedNotifyFunc updated_func;
69 ModulePreparedNotifyFunc prepared_func;
73 guchar *pixels; /* incoming pixel data buffer */
74 guchar *dptr; /* current position in pixbuf */
83 guint output_row; /* last row to be completed */
85 gboolean did_prescan; /* are we in image data yet? */
86 gboolean got_header; /* have we loaded jpeg header? */
89 GdkPixbuf *image_load (FILE *f);
90 gpointer image_begin_load (ModulePreparedNotifyFunc func,
91 ModuleUpdatedNotifyFunc func2, gpointer user_data);
92 void image_stop_load (gpointer context);
93 gboolean image_load_increment(gpointer context, guchar *buf, guint size);
95 static void explode_bitmap_into_buf (PnmLoaderContext *context);
96 static void explode_gray_into_buf (PnmLoaderContext *context);
98 /* Destroy notification function for the libart pixbuf */
100 free_buffer (gpointer user_data, gpointer data)
106 /* explode bitmap data into rgb components */
107 /* we need to know what the row so we can */
108 /* do sub-byte expansion (since 1 byte = 8 pixels) */
109 /* context->dptr MUST point at first byte in incoming data */
110 /* which corresponds to first pixel of row y */
112 explode_bitmap_into_buf (PnmLoaderContext *context)
115 guchar *from, *to, data;
120 g_return_if_fail (context != NULL);
121 g_return_if_fail (context->dptr != NULL);
123 /* I'm no clever bit-hacker so I'm sure this can be optimized */
124 dptr = context->dptr;
125 y = context->output_row;
126 wid = context->width;
128 from = dptr + (wid - 1)/8;
129 to = dptr + (wid - 1) * 3;
130 bit = 7 - (((y+1)*wid - 1) % 8);
132 /* get first byte and align properly */
134 for (j = 0; j < bit; j++, data >>= 1);
136 for (x = wid-1; x >= 0; x--) {
138 to[0] = to[1] = to[2] = (data & 1) ? 0x00 : 0xff;
153 /* explode gray image row into rgb components in pixbuf */
155 explode_gray_into_buf (PnmLoaderContext *context)
161 g_return_if_fail (context != NULL);
162 g_return_if_fail (context->dptr != NULL);
164 /* Expand grey->colour. Expand from the end of the
165 * memory down, so we can use the same buffer.
168 from = context->dptr + w - 1;
169 to = context->dptr + (w - 1) * 3;
170 for (j = w - 1; j >= 0; j--) {
179 /* skip over whitespace in file from current pos. */
180 /* also skips comments */
181 /* returns pointer to first non-whitespace char hit or, or NULL if */
182 /* we ran out of data w/o hitting a whitespace */
183 /* internal pointer in inbuf isnt moved ahead in this case */
185 skip_ahead_whitespace (PnmIOBuffer *inbuf)
191 g_return_val_if_fail (inbuf != NULL, NULL);
192 g_return_val_if_fail (inbuf->next_byte != NULL, NULL);
195 num_left = inbuf->bytes_left;
196 ptr = inbuf->next_byte;
197 while (num_left > 0) {
201 } else if (*ptr == '#') {
203 } else if (!isspace (*ptr)) {
204 inbuf->bytes_left -= (ptr-inbuf->next_byte);
205 inbuf->next_byte = ptr;
214 /* reads into buffer until we hit whitespace in file from current pos, */
215 /* return NULL if ran out of data */
216 /* advances inbuf if successful */
218 read_til_whitespace (PnmIOBuffer *inbuf, guchar *buf, guint size)
224 g_return_val_if_fail (inbuf != NULL, NULL);
225 g_return_val_if_fail (inbuf->next_byte != NULL, NULL);
228 num_left = inbuf->bytes_left;
229 ptr = inbuf->next_byte;
230 while (num_left > 0 && (p-buf)+1 < size) {
231 if (isspace (*ptr)) {
233 inbuf->bytes_left = num_left;
234 inbuf->next_byte = ptr;
246 /* read next number from buffer */
247 /* -1 if failed, 0 if successful */
249 read_next_number (PnmIOBuffer *inbuf, guint *value)
252 guchar *old_next_byte;
254 guint old_bytes_left;
257 g_return_val_if_fail (inbuf != NULL, -1);
258 g_return_val_if_fail (inbuf->next_byte != NULL, -1);
259 g_return_val_if_fail (value != NULL, -1);
261 old_next_byte = inbuf->next_byte;
262 old_bytes_left = inbuf->bytes_left;
264 if ((tmpptr = skip_ahead_whitespace (inbuf)) == NULL)
267 if ((tmpptr = read_til_whitespace (inbuf, buf, 128)) == NULL) {
268 inbuf->next_byte = old_next_byte;
269 inbuf->bytes_left = old_bytes_left;
273 *value = strtol (buf, &errptr, 10);
275 if (*errptr != '\0') {
276 inbuf->next_byte = old_next_byte;
277 inbuf->bytes_left = old_bytes_left;
284 /* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
286 pnm_read_header (PnmLoaderContext *context)
288 guchar *old_next_byte;
289 guint old_bytes_left;
295 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
297 inbuf = &context->inbuf;
298 old_bytes_left = inbuf->bytes_left;
299 old_next_byte = inbuf->next_byte;
301 /* file must start with a 'P' followed by a numeral */
302 /* so loop till we get enough data to determine type*/
303 if (inbuf->bytes_left < 2)
306 if (*inbuf->next_byte != 'P')
307 return PNM_FATAL_ERR;
309 switch (*(inbuf->next_byte+1)) {
311 type = PNM_FORMAT_PBM;
314 type = PNM_FORMAT_PGM;
317 type = PNM_FORMAT_PPM;
320 type = PNM_FORMAT_PBM_RAW;
323 type = PNM_FORMAT_PGM_RAW;
326 type = PNM_FORMAT_PPM_RAW;
329 return PNM_FATAL_ERR;
332 context->type = type;
334 inbuf->next_byte += 2;
335 inbuf->bytes_left -= 2;
337 /* now read remainder of header */
338 if ((rc = read_next_number (inbuf, &w))) {
339 inbuf->next_byte = old_next_byte;
340 inbuf->bytes_left = old_bytes_left;
344 if ((rc = read_next_number (inbuf, &h))) {
345 inbuf->next_byte = old_next_byte;
346 inbuf->bytes_left = old_bytes_left;
355 case PNM_FORMAT_PPM_RAW:
357 case PNM_FORMAT_PGM_RAW:
358 if ((rc = read_next_number (inbuf, &context->maxval)) < 0) {
359 inbuf->next_byte = old_next_byte;
360 inbuf->bytes_left = old_bytes_left;
373 pnm_read_raw_scanline (PnmLoaderContext *context)
376 guint numbytes, offset;
379 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
381 inbuf = &context->inbuf;
383 switch (context->type) {
384 case PNM_FORMAT_PBM_RAW:
385 numpix = inbuf->bytes_left * 8;
387 case PNM_FORMAT_PGM_RAW:
388 numpix = inbuf->bytes_left;
390 case PNM_FORMAT_PPM_RAW:
391 numpix = inbuf->bytes_left/3;
394 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
395 return PNM_FATAL_ERR;
399 numpix = MIN (numpix, context->width - context->output_col);
404 context->dptr = context->pixels +
405 context->output_row * context->width * 3;
407 switch (context->type) {
408 case PNM_FORMAT_PBM_RAW:
410 offset = context->output_col/8;
412 case PNM_FORMAT_PGM_RAW:
414 offset = context->output_col;
416 case PNM_FORMAT_PPM_RAW:
418 offset = context->output_col*3;
421 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
425 memcpy (context->dptr + offset, inbuf->next_byte, numbytes);
427 inbuf->next_byte += numbytes;
428 inbuf->bytes_left -= numbytes;
430 context->output_col += numpix;
431 if (context->output_col == context->width) {
432 if ( context->type == PNM_FORMAT_PBM_RAW )
433 explode_bitmap_into_buf(context);
434 else if ( context->type == PNM_FORMAT_PGM_RAW )
435 explode_gray_into_buf (context);
437 context->output_col = 0;
438 context->output_row++;
449 pnm_read_ascii_scanline (PnmLoaderContext *context)
453 guint value, numval, i;
456 guchar *old_next_byte, *dptr;
457 guint old_bytes_left;
460 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
462 inbuf = &context->inbuf;
464 context->dptr = context->pixels +
465 context->output_row * context->width * 3;
467 switch (context->type) {
470 offset = context->output_col/8;
474 offset = context->output_col;
478 offset = context->output_col*3;
482 g_warning ("Can't happen\n");
483 return PNM_FATAL_ERR;
486 dptr = context->dptr + offset;
489 if (context->type == PNM_FORMAT_PBM) {
494 old_next_byte = inbuf->next_byte;
495 old_bytes_left = inbuf->bytes_left;
497 for (i=0; i<numval; i++) {
498 if ((rc = read_next_number (inbuf, &value))) {
499 inbuf->next_byte = old_next_byte;
500 inbuf->bytes_left = old_bytes_left;
503 switch (context->type) {
511 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
514 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
517 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
522 if (context->type == PNM_FORMAT_PBM)
525 context->output_col++;
526 if (context->output_col == context->width) {
527 if ( context->type == PNM_FORMAT_PBM )
528 explode_bitmap_into_buf(context);
529 else if ( context->type == PNM_FORMAT_PGM )
530 explode_gray_into_buf (context);
532 context->output_col = 0;
533 context->output_row++;
542 /* returns 1 if a scanline was converted, 0 means we ran out of data */
544 pnm_read_scanline (PnmLoaderContext *context)
548 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
550 /* read in image data */
551 /* for raw formats this is trivial */
552 switch (context->type) {
553 case PNM_FORMAT_PBM_RAW:
554 case PNM_FORMAT_PGM_RAW:
555 case PNM_FORMAT_PPM_RAW:
556 rc = pnm_read_raw_scanline (context);
557 if (rc == PNM_SUSPEND)
564 rc = pnm_read_ascii_scanline (context);
565 if (rc == PNM_SUSPEND)
570 g_print ("Cannot load these image types (yet)\n");
571 return PNM_FATAL_ERR;
577 /* Shared library entry point */
584 PnmLoaderContext context;
587 /* pretend to be doing progressive loading */
588 context.updated_func = context.prepared_func = NULL;
589 context.user_data = NULL;
590 context.inbuf.bytes_left = 0;
591 context.inbuf.next_byte = NULL;
592 context.pixels = context.pixbuf = NULL;
593 context.got_header = context.did_prescan = FALSE;
595 inbuf = &context.inbuf;
600 /* keep buffer as full as possible */
601 num_to_read = PNM_BUF_SIZE - inbuf->bytes_left;
603 if (inbuf->next_byte != NULL && inbuf->bytes_left > 0)
604 memmove (inbuf->buffer, inbuf->next_byte,
607 nbytes = fread (inbuf->buffer+inbuf->bytes_left,
609 inbuf->bytes_left += nbytes;
610 inbuf->next_byte = inbuf->buffer;
612 /* ran out of data and we haven't exited main loop */
613 /* something is wrong */
614 if (inbuf->bytes_left == 0) {
616 g_free (context.pixels);
617 g_warning ("io-pnm.c: Ran out of data...\n");
621 /* get header if needed */
622 if (!context.got_header) {
624 rc = pnm_read_header (&context);
625 if (rc == PNM_FATAL_ERR)
627 else if (rc == PNM_SUSPEND)
630 context.got_header = TRUE;
633 /* scan until we hit image data */
634 if (!context.did_prescan) {
635 if (skip_ahead_whitespace (inbuf) == NULL)
638 context.did_prescan = TRUE;
639 context.output_row = 0;
640 context.output_col = 0;
642 context.pixels = g_malloc (context.height *
648 /* if we got here we're reading image data */
649 while (context.output_row < context.height) {
650 rc = pnm_read_scanline (&context);
652 if (rc == PNM_SUSPEND) {
654 } else if (rc == PNM_FATAL_ERR) {
656 g_free (context.pixels);
657 g_warning ("io-pnm.c: error reading rows..\n");
662 if (context.output_row < context.height)
668 return gdk_pixbuf_new_from_data (context.pixels, ART_PIX_RGB, FALSE,
669 context.width, context.height,
670 context.width * 3, free_buffer, NULL);
675 /**** Progressive image loading handling *****/
677 /* these routines required because we are acting as a source manager for */
680 init_source (j_decompress_ptr cinfo)
682 my_src_ptr src = (my_src_ptr) cinfo->src;
689 term_source (j_decompress_ptr cinfo)
691 /* XXXX - probably should scream something has happened */
695 /* for progressive loading (called "I/O Suspension" by libjpeg docs) */
696 /* we do nothing except return "FALSE" */
698 fill_input_buffer (j_decompress_ptr cinfo)
705 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
707 my_src_ptr src = (my_src_ptr) cinfo->src;
710 /* move as far as we can into current buffer */
711 /* then set skip_next to catch the rest */
713 num_can_do = MIN (src->pub.bytes_in_buffer, num_bytes);
714 src->pub.next_input_byte += (size_t) num_can_do;
715 src->pub.bytes_in_buffer -= (size_t) num_can_do;
717 src->skip_next = num_bytes - num_can_do;
723 * func - called when we have pixmap created (but no image data)
724 * user_data - passed as arg 1 to func
725 * return context (opaque to user)
729 image_begin_load (ModulePreparedNotifyFunc prepared_func,
730 ModuleUpdatedNotifyFunc updated_func,
733 JpegProgContext *context;
736 context = g_new0 (JpegProgContext, 1);
737 context->prepared_func = prepared_func;
738 context->updated_func = updated_func;
739 context->user_data = user_data;
740 context->pixbuf = NULL;
741 context->got_header = FALSE;
742 context->did_prescan = FALSE;
743 context->src_initialized = FALSE;
745 /* create libjpeg structures */
746 jpeg_create_decompress (&context->cinfo);
748 context->cinfo.src = (struct jpeg_source_mgr *) g_new0 (my_source_mgr, 1);
749 src = (my_src_ptr) context->cinfo.src;
751 context->cinfo.err = jpeg_std_error (&context->jerr.pub);
753 src = (my_src_ptr) context->cinfo.src;
754 src->pub.init_source = init_source;
755 src->pub.fill_input_buffer = fill_input_buffer;
756 src->pub.skip_input_data = skip_input_data;
757 src->pub.resync_to_restart = jpeg_resync_to_restart;
758 src->pub.term_source = term_source;
759 src->pub.bytes_in_buffer = 0;
760 src->pub.next_input_byte = NULL;
762 return (gpointer) context;
766 * context - returned from image_begin_load
768 * free context, unref gdk_pixbuf
771 image_stop_load (gpointer data)
773 JpegProgContext *context = (JpegProgContext *) data;
775 g_return_if_fail (context != NULL);
778 gdk_pixbuf_unref (context->pixbuf);
780 jpeg_finish_decompress(&context->cinfo);
781 jpeg_destroy_decompress(&context->cinfo);
783 if (context->cinfo.src) {
784 my_src_ptr src = (my_src_ptr) context->cinfo.src;
796 * context - from image_begin_load
797 * buf - new image data
798 * size - length of new image data
800 * append image data onto inrecrementally built output image
803 image_load_increment (gpointer data, guchar *buf, guint size)
805 JpegProgContext *context = (JpegProgContext *)data;
806 struct jpeg_decompress_struct *cinfo;
808 guint num_left, num_copy;
809 guint last_bytes_left;
813 g_return_val_if_fail (context != NULL, FALSE);
814 g_return_val_if_fail (buf != NULL, FALSE);
816 src = (my_src_ptr) context->cinfo.src;
818 cinfo = &context->cinfo;
820 /* XXXXXXX (drmike) - loop(s) below need to be recoded now I
821 * have a grasp of what the flow needs to be!
825 /* skip over data if requested, handle unsigned int sizes cleanly */
826 /* only can happen if we've already called jpeg_get_header once */
827 if (context->src_initialized && src->skip_next) {
828 if (src->skip_next > size) {
829 src->skip_next -= size;
832 num_left = size - src->skip_next;
833 bufhd = buf + src->skip_next;
844 while (src->pub.bytes_in_buffer != 0 || num_left != 0) {
846 /* handle any data from caller we haven't processed yet */
848 if(src->pub.bytes_in_buffer &&
849 src->pub.next_input_byte != src->buffer)
850 memmove(src->buffer, src->pub.next_input_byte,
851 src->pub.bytes_in_buffer);
854 num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
857 /* if (num_copy == 0)
858 g_error ("Buffer overflow!");
860 memcpy(src->buffer + src->pub.bytes_in_buffer, bufhd,num_copy);
861 src->pub.next_input_byte = src->buffer;
862 src->pub.bytes_in_buffer += num_copy;
864 num_left -= num_copy;
866 /* did anything change from last pass, if not return */
867 if (last_bytes_left == 0)
868 last_bytes_left = src->pub.bytes_in_buffer;
869 else if (src->pub.bytes_in_buffer == last_bytes_left)
872 last_bytes_left = src->pub.bytes_in_buffer;
875 /* should not go through twice and not pull bytes out of buf */
879 /* try to load jpeg header */
880 if (!context->got_header) {
883 rc = jpeg_read_header (cinfo, TRUE);
884 context->src_initialized = TRUE;
886 if (rc == JPEG_SUSPENDED)
889 context->got_header = TRUE;
891 if (jpeg_has_multiple_scans (cinfo)) {
892 g_print ("io-jpeg.c: Does not currently "
893 "support progressive jpeg files.\n");
897 context->pixbuf = gdk_pixbuf_new(ART_PIX_RGB,
898 /*have_alpha*/ FALSE,
901 cinfo->image_height);
903 if (context->pixbuf == NULL) {
904 /* Failed to allocate memory */
905 g_error ("Couldn't allocate gdkpixbuf");
908 /* Use pixbuf buffer to store decompressed data */
909 context->dptr = context->pixbuf->art_pixbuf->pixels;
911 /* Notify the client that we are ready to go */
912 (* context->prepared_func) (context->pixbuf,
915 } else if (!context->did_prescan) {
918 /* start decompression */
919 rc = jpeg_start_decompress (cinfo);
920 cinfo->do_fancy_upsampling = FALSE;
921 cinfo->do_block_smoothing = FALSE;
923 if (rc == JPEG_SUSPENDED)
926 context->did_prescan = TRUE;
928 /* we're decompressing so feed jpeg lib scanlines */
935 /* keep going until we've done all scanlines */
936 while (cinfo->output_scanline < cinfo->output_height) {
937 start_scanline = cinfo->output_scanline;
939 rowptr = context->dptr;
940 for (i=0; i < cinfo->rec_outbuf_height; i++) {
942 rowptr += context->pixbuf->art_pixbuf->rowstride;;
945 #ifdef IO_JPEG_DEBUG_GREY
946 for (p=lines[0],i=0; i< context->pixbuf->art_pixbuf->rowstride;i++, p++)
950 nlines = jpeg_read_scanlines (cinfo, lines,
951 cinfo->rec_outbuf_height);
956 if (cinfo->output_components == 1)
957 explode_gray_into_buf (cinfo, lines);
959 context->dptr += nlines * context->pixbuf->art_pixbuf->rowstride;
961 /* send updated signal */
962 (* context->updated_func) (context->pixbuf,
965 cinfo->output_scanline-1,
969 #undef DEBUG_JPEG_PROGRESSIVE
970 #ifdef DEBUG_JPEG_PROGRESSIVE
972 if (start_scanline != cinfo->output_scanline)
973 g_print("jpeg: Input pass=%2d, next input scanline=%3d,"
974 " emitted %3d - %3d\n",
975 cinfo->input_scan_number, cinfo->input_iMCU_row * 16,
976 start_scanline, cinfo->output_scanline - 1);
980 g_print ("Scanline %d of %d - ",
981 cinfo->output_scanline,
982 cinfo->output_height);
983 /* g_print ("rec_height %d -", cinfo->rec_outbuf_height); */
984 g_print ("Processed %d lines - bytes left = %d\n",
985 nlines, cinfo->src->bytes_in_buffer);
988 /* did entire image */
989 if (cinfo->output_scanline >= cinfo->output_height)