1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - PNM image loader
4 * Copyright (C) 1999 Red Hat, Inc.
6 * Authors: Jeffrey Stedfast <fejj@helixcode.com>
7 * Michael Fulbright <drmike@redhat.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
31 #include "gdk-pixbuf-private.h"
32 #include "gdk-pixbuf-io.h"
35 #define PNM_BUF_SIZE 4096
37 #define PNM_FATAL_ERR -1
51 guchar buffer[PNM_BUF_SIZE];
57 ModuleUpdatedNotifyFunc updated_func;
58 ModulePreparedNotifyFunc prepared_func;
62 guchar *pixels; /* incoming pixel data buffer */
63 guchar *dptr; /* current position in pixbuf */
73 guint output_row; /* last row to be completed */
75 gboolean did_prescan; /* are we in image data yet? */
76 gboolean got_header; /* have we loaded pnm header? */
84 GdkPixbuf *gdk_pixbuf__pnm_image_load (FILE *f, GError **error);
85 gpointer gdk_pixbuf__pnm_image_begin_load (ModulePreparedNotifyFunc func,
86 ModuleUpdatedNotifyFunc func2,
87 ModuleFrameDoneNotifyFunc frame_done_func,
88 ModuleAnimationDoneNotifyFunc anim_done_func,
91 void gdk_pixbuf__pnm_image_stop_load (gpointer context);
92 gboolean gdk_pixbuf__pnm_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 pixbuf */
100 free_buffer (guchar *pixels, 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); */
131 bit = 7 - ((wid-1) % 8);
133 /* get first byte and align properly */
135 for (j = 0; j < bit; j++, data >>= 1);
137 for (x = wid-1; x >= 0; x--) {
138 /* g_print ("%c", (data & 1) ? '*' : ' '); */
140 to[0] = to[1] = to[2] = (data & 0x01) ? 0x00 : 0xff;
154 /* g_print ("\n"); */
157 /* explode gray image row into rgb components in pixbuf */
159 explode_gray_into_buf (PnmLoaderContext *context)
165 g_return_if_fail (context != NULL);
166 g_return_if_fail (context->dptr != NULL);
168 /* Expand grey->colour. Expand from the end of the
169 * memory down, so we can use the same buffer.
172 from = context->dptr + w - 1;
173 to = context->dptr + (w - 1) * 3;
174 for (j = w - 1; j >= 0; j--) {
183 /* skip over whitespace and comments in input buffer */
185 pnm_skip_whitespace (PnmIOBuffer *inbuf, GError **error)
187 register guchar *inptr;
190 g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
191 g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
193 inend = inbuf->byte + inbuf->nbytes;
196 for ( ; inptr < inend; inptr++) {
198 /* in comment - skip to the end of this line */
199 for ( ; *inptr != '\n' && inptr < inend; inptr++);
200 } else if (!isspace (*inptr)) {
202 inbuf->nbytes = (guint) (inend - inptr);
208 inbuf->nbytes = (guint) (inend - inptr);
213 /* read next number from buffer */
215 pnm_read_next_value (PnmIOBuffer *inbuf, guint *value, GError **error)
217 register guchar *inptr, *word, *p;
218 guchar *inend, buf[128];
222 g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
223 g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
224 g_return_val_if_fail (value != NULL, PNM_FATAL_ERR);
226 /* skip white space */
227 if ((retval = pnm_skip_whitespace (inbuf, error)) != PNM_OK)
230 inend = inbuf->byte + inbuf->nbytes;
233 /* copy this pnm 'word' into a temp buffer */
234 for (p = inptr, word = buf; (p < inend) && !isspace (*p) && (p - inptr < 128); p++, word++)
238 /* hmmm, there must be more data to this 'word' */
243 *value = strtol (buf, &endptr, 10);
244 if (*endptr != '\0') {
247 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
248 _("PNM loader expected to find an integer, but didn't"));
249 return PNM_FATAL_ERR;
253 inbuf->nbytes = (guint) (inend - p);
258 /* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
260 pnm_read_header (PnmLoaderContext *context)
265 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
267 inbuf = &context->inbuf;
269 if (!context->type) {
270 /* file must start with a 'P' followed by a numeral */
271 /* so loop till we get enough data to determine type */
272 if (inbuf->nbytes < 2)
275 if (*inbuf->byte != 'P') {
276 g_set_error (context->error,
278 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
279 _("PNM file has an incorrect initial byte"));
280 return PNM_FATAL_ERR;
286 switch (*inbuf->byte) {
288 context->type = PNM_FORMAT_PBM;
291 context->type = PNM_FORMAT_PGM;
294 context->type = PNM_FORMAT_PPM;
297 context->type = PNM_FORMAT_PBM_RAW;
300 context->type = PNM_FORMAT_PGM_RAW;
303 context->type = PNM_FORMAT_PPM_RAW;
306 g_set_error (context->error,
308 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
309 _("PNM file is not in a recognized PNM subformat"));
310 return PNM_FATAL_ERR;
320 if (!context->width) {
321 /* read the pixmap width */
324 retval = pnm_read_next_value (inbuf, &width,
327 if (retval != PNM_OK)
331 g_set_error (context->error,
333 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
334 _("PNM file has an image width of 0"));
335 return PNM_FATAL_ERR;
338 context->width = width;
341 if (!context->height) {
342 /* read the pixmap height */
345 retval = pnm_read_next_value (inbuf, &height,
348 if (retval != PNM_OK)
352 g_set_error (context->error,
354 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
355 _("PNM file has an image height of 0"));
356 return PNM_FATAL_ERR;
359 context->height = height;
362 switch (context->type) {
364 case PNM_FORMAT_PPM_RAW:
366 case PNM_FORMAT_PGM_RAW:
367 if (!context->maxval) {
368 retval = pnm_read_next_value (inbuf, &context->maxval,
371 if (retval != PNM_OK)
374 if (context->maxval == 0) {
375 g_set_error (context->error,
377 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
378 _("Maximum color value in PNM file is 0"));
379 return PNM_FATAL_ERR;
391 pnm_read_raw_scanline (PnmLoaderContext *context)
394 guint numbytes, offset;
399 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
401 inbuf = &context->inbuf;
403 switch (context->type) {
404 case PNM_FORMAT_PBM_RAW:
405 numpix = inbuf->nbytes * 8;
407 case PNM_FORMAT_PGM_RAW:
408 numpix = inbuf->nbytes;
410 case PNM_FORMAT_PPM_RAW:
411 numpix = inbuf->nbytes / 3;
414 g_set_error (context->error,
416 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
417 _("Raw PNM image type is invalid"));
418 return PNM_FATAL_ERR;
421 numpix = MIN (numpix, context->width - context->output_col);
426 context->dptr = context->pixels + context->output_row * context->rowstride;
428 switch (context->type) {
429 case PNM_FORMAT_PBM_RAW:
430 numbytes = (numpix / 8) + ((numpix % 8) ? 1 : 0);
431 offset = context->output_col / 8;
433 case PNM_FORMAT_PGM_RAW:
435 offset = context->output_col;
437 case PNM_FORMAT_PPM_RAW:
438 numbytes = numpix * 3;
439 offset = context->output_col * 3;
442 g_set_error (context->error,
444 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
445 _("Raw PNM image type is invalid"));
446 return PNM_FATAL_ERR;
449 switch (context->type) {
450 case PNM_FORMAT_PBM_RAW:
451 dest = context->dptr + offset;
452 memcpy (dest, inbuf->byte, numbytes);
454 case PNM_FORMAT_PGM_RAW:
455 case PNM_FORMAT_PPM_RAW:
456 dest = context->dptr + offset;
458 if (context->maxval == 255) {
459 /* special-case optimization */
460 memcpy (dest, inbuf->byte, numbytes);
462 for (i = 0; i < numbytes; i++) {
463 guchar *byte = inbuf->byte + i;
465 /* scale the color to an 8-bit color depth */
466 if (*byte > context->maxval)
469 *dest++ = (guchar) (255 * *byte / context->maxval);
474 g_set_error (context->error,
476 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
477 _("Raw PNM image type is invalid"));
478 return PNM_FATAL_ERR;
481 inbuf->byte += numbytes;
482 inbuf->nbytes -= numbytes;
484 context->output_col += numpix;
485 if (context->output_col == context->width) {
486 if (context->type == PNM_FORMAT_PBM_RAW)
487 explode_bitmap_into_buf (context);
488 else if (context->type == PNM_FORMAT_PGM_RAW)
489 explode_gray_into_buf (context);
491 context->output_col = 0;
492 context->output_row++;
501 pnm_read_ascii_scanline (PnmLoaderContext *context)
505 guint value, numval, i;
511 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
515 inbuf = &context->inbuf;
517 context->dptr = context->pixels + context->output_row * context->rowstride;
519 switch (context->type) {
521 numval = MIN (8, context->width - context->output_col);
522 offset = context->output_col / 8;
526 offset = context->output_col;
530 offset = context->output_col * 3;
534 g_set_error (context->error,
536 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
537 _("PNM image format is invalid"));
539 return PNM_FATAL_ERR;
542 dptr = context->dptr + offset + context->scan_state;
545 if (context->type == PNM_FORMAT_PBM) {
548 numval = MIN (8, context->width - context->output_col);
551 for (i = context->scan_state; i < numval; i++) {
552 retval = pnm_read_next_value (inbuf, &value,
554 if (retval != PNM_OK) {
555 /* save state and return */
556 context->scan_state = i;
560 switch (context->type) {
569 /* scale the color to an 8-bit color depth */
570 if (value > context->maxval)
573 *dptr++ = (guchar)(255 * value / context->maxval);
576 g_set_error (context->error,
578 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
579 _("PNM image format is invalid"));
580 return PNM_FATAL_ERR;
585 context->scan_state = 0;
587 if (context->type == PNM_FORMAT_PBM) {
589 context->output_col += numval;
591 context->output_col++;
594 if (context->output_col == context->width) {
595 if (context->type == PNM_FORMAT_PBM)
596 explode_bitmap_into_buf (context);
597 else if (context->type == PNM_FORMAT_PGM)
598 explode_gray_into_buf (context);
600 context->output_col = 0;
601 context->output_row++;
609 /* returns 1 if a scanline was converted, 0 means we ran out of data */
611 pnm_read_scanline (PnmLoaderContext *context)
615 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
617 /* read in image data */
618 /* for raw formats this is trivial */
619 switch (context->type) {
620 case PNM_FORMAT_PBM_RAW:
621 case PNM_FORMAT_PGM_RAW:
622 case PNM_FORMAT_PPM_RAW:
623 retval = pnm_read_raw_scanline (context);
624 if (retval != PNM_OK)
630 retval = pnm_read_ascii_scanline (context);
631 if (retval != PNM_OK)
635 g_set_error (context->error,
637 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
638 _("PNM image loader does not support this PNM subformat"));
640 return PNM_FATAL_ERR;
646 /* Shared library entry point */
648 gdk_pixbuf__pnm_image_load (FILE *f, GError **error)
650 PnmLoaderContext context;
655 /* pretend to be doing progressive loading */
656 context.updated_func = NULL;
657 context.prepared_func = NULL;
658 context.user_data = NULL;
660 context.inbuf.nbytes = 0;
661 context.inbuf.byte = NULL;
665 context.pixels = NULL;
666 context.pixbuf = NULL;
667 context.got_header = FALSE;
668 context.did_prescan = FALSE;
669 context.scan_state = 0;
670 context.error = error;
672 inbuf = &context.inbuf;
677 /* keep buffer as full as possible */
678 num_to_read = PNM_BUF_SIZE - inbuf->nbytes;
680 if (inbuf->byte != NULL && inbuf->nbytes > 0)
681 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
683 nbytes = fread (inbuf->buffer + inbuf->nbytes, 1, num_to_read, f);
686 if (nbytes == 0 && ferror (f)) {
687 /* we ran out of data? */
689 gdk_pixbuf_unref (context.pixbuf);
690 g_warning ("io-pnm.c: Ran out of data.\n");
694 inbuf->nbytes += nbytes;
695 inbuf->byte = inbuf->buffer;
697 /* get header if needed */
698 if (!context.got_header) {
699 retval = pnm_read_header (&context);
700 if (retval == PNM_FATAL_ERR)
702 else if (retval == PNM_SUSPEND)
705 context.got_header = TRUE;
708 /* scan until we hit image data */
709 if (!context.did_prescan) {
710 retval = pnm_skip_whitespace (inbuf,
712 if (retval == PNM_FATAL_ERR)
714 else if (retval == PNM_SUSPEND)
717 context.did_prescan = TRUE;
718 context.output_row = 0;
719 context.output_col = 0;
721 context.rowstride = context.width * 3;
722 context.pixels = g_malloc (context.height * context.width * 3);
724 if (!context.pixels) {
725 /* Failed to allocate memory */
726 g_warning ("Couldn't allocate pixel buf");
730 /* if we got here we're reading image data */
731 while (context.output_row < context.height) {
732 retval = pnm_read_scanline (&context);
734 if (retval == PNM_SUSPEND) {
736 } else if (retval == PNM_FATAL_ERR) {
738 gdk_pixbuf_unref (context.pixbuf);
744 if (context.output_row < context.height)
750 return gdk_pixbuf_new_from_data (context.pixels, GDK_COLORSPACE_RGB, FALSE, 8,
751 context.width, context.height,
752 context.width * 3, free_buffer, NULL);
757 * func - called when we have pixmap created (but no image data)
758 * user_data - passed as arg 1 to func
759 * return context (opaque to user)
763 gdk_pixbuf__pnm_image_begin_load (ModulePreparedNotifyFunc prepared_func,
764 ModuleUpdatedNotifyFunc updated_func,
765 ModuleFrameDoneNotifyFunc frame_done_func,
766 ModuleAnimationDoneNotifyFunc anim_done_func,
770 PnmLoaderContext *context;
772 context = g_new0 (PnmLoaderContext, 1);
773 context->prepared_func = prepared_func;
774 context->updated_func = updated_func;
775 context->user_data = user_data;
779 context->pixbuf = NULL;
780 context->pixels = NULL;
781 context->got_header = FALSE;
782 context->did_prescan = FALSE;
783 context->scan_state = 0;
785 context->inbuf.nbytes = 0;
786 context->inbuf.byte = NULL;
788 context->error = error;
790 return (gpointer) context;
794 * context - returned from image_begin_load
796 * free context, unref gdk_pixbuf
799 gdk_pixbuf__pnm_image_stop_load (gpointer data)
801 PnmLoaderContext *context = (PnmLoaderContext *) data;
803 g_return_if_fail (context != NULL);
806 gdk_pixbuf_unref (context->pixbuf);
812 * context - from image_begin_load
813 * buf - new image data
814 * size - length of new image data
816 * append image data onto inrecrementally built output image
819 gdk_pixbuf__pnm_image_load_increment (gpointer data, guchar *buf, guint size,
822 PnmLoaderContext *context = (PnmLoaderContext *)data;
827 guint num_left, spinguard;
830 g_return_val_if_fail (context != NULL, FALSE);
831 g_return_val_if_fail (buf != NULL, FALSE);
833 context->error = error;
836 inbuf = &context->inbuf;
837 old_nbytes = inbuf->nbytes;
838 old_byte = inbuf->byte;
845 /* keep buffer as full as possible */
846 num_to_copy = MIN (PNM_BUF_SIZE - inbuf->nbytes, num_left);
848 if (num_to_copy == 0)
854 if (inbuf->byte != NULL && inbuf->nbytes > 0)
855 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
857 memcpy (inbuf->buffer + inbuf->nbytes, bufhd, num_to_copy);
858 bufhd += num_to_copy;
859 inbuf->nbytes += num_to_copy;
860 inbuf->byte = inbuf->buffer;
861 num_left -= num_to_copy;
863 /* ran out of data and we haven't exited main loop */
864 if (inbuf->nbytes == 0)
867 /* get header if needed */
868 if (!context->got_header) {
869 retval = pnm_read_header (context);
871 if (retval == PNM_FATAL_ERR)
873 else if (retval == PNM_SUSPEND)
876 context->got_header = TRUE;
879 /* scan until we hit image data */
880 if (!context->did_prescan) {
881 retval = pnm_skip_whitespace (inbuf,
884 if (retval == PNM_FATAL_ERR)
886 else if (retval == PNM_SUSPEND)
889 context->did_prescan = TRUE;
890 context->output_row = 0;
891 context->output_col = 0;
893 context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
899 if (context->pixbuf == NULL) {
900 /* Failed to allocate memory */
901 g_error ("Couldn't allocate gdkpixbuf");
904 context->pixels = context->pixbuf->pixels;
905 context->rowstride = context->pixbuf->rowstride;
907 /* Notify the client that we are ready to go */
908 (* context->prepared_func) (context->pixbuf,
912 /* if we got here we're reading image data */
913 while (context->output_row < context->height) {
914 retval = pnm_read_scanline (context);
916 if (retval == PNM_SUSPEND) {
918 } else if (retval == PNM_FATAL_ERR) {
920 gdk_pixbuf_unref (context->pixbuf);
922 } else if (retval == PNM_OK) {
923 /* send updated signal */
924 (* context->updated_func) (context->pixbuf,
926 context->output_row-1,
933 if (context->output_row < context->height)