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.
30 #include "gdk-pixbuf-private.h"
31 #include "gdk-pixbuf-io.h"
34 #define PNM_BUF_SIZE 4096
36 #define PNM_FATAL_ERR -1
50 guchar buffer[PNM_BUF_SIZE];
56 GdkPixbufModuleUpdatedFunc updated_func;
57 GdkPixbufModulePreparedFunc prepared_func;
58 GdkPixbufModuleSizeFunc size_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 static GdkPixbuf *gdk_pixbuf__pnm_image_load (FILE *f, GError **error);
85 static gpointer gdk_pixbuf__pnm_image_begin_load (GdkPixbufModuleSizeFunc size_func,
86 GdkPixbufModulePreparedFunc func,
87 GdkPixbufModuleUpdatedFunc func2,
90 static gboolean gdk_pixbuf__pnm_image_stop_load (gpointer context, GError **error);
91 static gboolean gdk_pixbuf__pnm_image_load_increment (gpointer context,
92 const guchar *buf, guint size,
95 static void explode_bitmap_into_buf (PnmLoaderContext *context);
96 static void explode_gray_into_buf (PnmLoaderContext *context);
99 /* explode bitmap data into rgb components */
100 /* we need to know what the row so we can */
101 /* do sub-byte expansion (since 1 byte = 8 pixels) */
102 /* context->dptr MUST point at first byte in incoming data */
103 /* which corresponds to first pixel of row y */
105 explode_bitmap_into_buf (PnmLoaderContext *context)
108 guchar *from, *to, data;
113 g_return_if_fail (context != NULL);
114 g_return_if_fail (context->dptr != NULL);
116 /* I'm no clever bit-hacker so I'm sure this can be optimized */
117 dptr = context->dptr;
118 y = context->output_row;
119 wid = context->width;
121 from = dptr + ((wid - 1) / 8);
122 to = dptr + (wid - 1) * 3;
123 /* bit = 7 - (((y+1)*wid-1) % 8); */
124 bit = 7 - ((wid-1) % 8);
126 /* get first byte and align properly */
128 for (j = 0; j < bit; j++, data >>= 1);
130 for (x = wid-1; x >= 0; x--) {
131 /* g_print ("%c", (data & 1) ? '*' : ' '); */
133 to[0] = to[1] = to[2] = (data & 0x01) ? 0x00 : 0xff;
147 /* g_print ("\n"); */
150 /* explode gray image row into rgb components in pixbuf */
152 explode_gray_into_buf (PnmLoaderContext *context)
158 g_return_if_fail (context != NULL);
159 g_return_if_fail (context->dptr != NULL);
161 /* Expand grey->colour. Expand from the end of the
162 * memory down, so we can use the same buffer.
165 from = context->dptr + w - 1;
166 to = context->dptr + (w - 1) * 3;
167 for (j = w - 1; j >= 0; j--) {
176 /* skip over whitespace and comments in input buffer */
178 pnm_skip_whitespace (PnmIOBuffer *inbuf, GError **error)
180 register guchar *inptr;
183 g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
184 g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
186 inend = inbuf->byte + inbuf->nbytes;
189 for ( ; inptr < inend; inptr++) {
191 /* in comment - skip to the end of this line */
192 for ( ; *inptr != '\n' && inptr < inend; inptr++)
195 if ( *inptr != '\n' ) {
196 /* couldn't read whole comment */
200 } else if (!g_ascii_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[129];
223 g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
224 g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
225 g_return_val_if_fail (value != NULL, PNM_FATAL_ERR);
227 /* skip white space */
228 if ((retval = pnm_skip_whitespace (inbuf, error)) != PNM_OK)
231 inend = inbuf->byte + inbuf->nbytes;
234 /* copy this pnm 'word' into a temp buffer */
235 for (p = inptr, word = buf; (p < inend) && !g_ascii_isspace (*p) && (*p != '#') && (p - inptr < 128); p++, word++)
239 /* hmmm, there must be more data to this 'word' */
240 if (p == inend || (!g_ascii_isspace (*p) && (*p != '#') && (p - inptr < 128)))
244 result = strtol (buf, &endptr, 10);
245 if (*endptr != '\0' || result < 0) {
248 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
249 _("PNM loader expected to find an integer, but didn't"));
250 return PNM_FATAL_ERR;
255 inbuf->nbytes = (guint) (inend - p);
260 /* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
262 pnm_read_header (PnmLoaderContext *context)
267 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
269 inbuf = &context->inbuf;
271 if (!context->type) {
272 /* file must start with a 'P' followed by a numeral */
273 /* so loop till we get enough data to determine type */
274 if (inbuf->nbytes < 2)
277 if (*inbuf->byte != 'P') {
278 g_set_error (context->error,
280 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
281 _("PNM file has an incorrect initial byte"));
282 return PNM_FATAL_ERR;
288 switch (*inbuf->byte) {
290 context->type = PNM_FORMAT_PBM;
293 context->type = PNM_FORMAT_PGM;
296 context->type = PNM_FORMAT_PPM;
299 context->type = PNM_FORMAT_PBM_RAW;
302 context->type = PNM_FORMAT_PGM_RAW;
305 context->type = PNM_FORMAT_PPM_RAW;
308 g_set_error (context->error,
310 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
311 _("PNM file is not in a recognized PNM subformat"));
312 return PNM_FATAL_ERR;
322 if (!context->width) {
323 /* read the pixmap width */
326 retval = pnm_read_next_value (inbuf, &width,
329 if (retval != PNM_OK)
333 g_set_error (context->error,
335 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
336 _("PNM file has an image width of 0"));
337 return PNM_FATAL_ERR;
340 context->width = width;
343 if (!context->height) {
344 /* read the pixmap height */
347 retval = pnm_read_next_value (inbuf, &height,
350 if (retval != PNM_OK)
354 g_set_error (context->error,
356 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
357 _("PNM file has an image height of 0"));
358 return PNM_FATAL_ERR;
361 context->height = height;
364 switch (context->type) {
366 case PNM_FORMAT_PPM_RAW:
368 case PNM_FORMAT_PGM_RAW:
369 if (!context->maxval) {
370 retval = pnm_read_next_value (inbuf, &context->maxval,
373 if (retval != PNM_OK)
376 if (context->maxval == 0) {
377 g_set_error (context->error,
379 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
380 _("Maximum color value in PNM file is 0"));
381 return PNM_FATAL_ERR;
384 if (context->maxval > 65535) {
385 g_set_error (context->error,
387 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
388 _("Maximum color value in PNM file is too large"));
389 return PNM_FATAL_ERR;
392 if (context->maxval > 255) {
393 g_set_error (context->error,
395 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
396 _("Cannot handle PNM files with maximum color values greater than 255"));
397 return PNM_FATAL_ERR;
409 pnm_read_raw_scanline (PnmLoaderContext *context)
412 guint numbytes, offset;
417 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
419 inbuf = &context->inbuf;
421 switch (context->type) {
422 case PNM_FORMAT_PBM_RAW:
423 numpix = inbuf->nbytes * 8;
425 case PNM_FORMAT_PGM_RAW:
426 numpix = inbuf->nbytes;
428 case PNM_FORMAT_PPM_RAW:
429 numpix = inbuf->nbytes / 3;
432 g_set_error (context->error,
434 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
435 _("Raw PNM image type is invalid"));
436 return PNM_FATAL_ERR;
439 numpix = MIN (numpix, context->width - context->output_col);
444 context->dptr = context->pixels + context->output_row * context->rowstride;
446 switch (context->type) {
447 case PNM_FORMAT_PBM_RAW:
448 numbytes = (numpix / 8) + ((numpix % 8) ? 1 : 0);
449 offset = context->output_col / 8;
451 case PNM_FORMAT_PGM_RAW:
453 offset = context->output_col;
455 case PNM_FORMAT_PPM_RAW:
456 numbytes = numpix * 3;
457 offset = context->output_col * 3;
460 g_set_error (context->error,
462 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
463 _("Raw PNM image type is invalid"));
464 return PNM_FATAL_ERR;
467 switch (context->type) {
468 case PNM_FORMAT_PBM_RAW:
469 dest = context->dptr + offset;
470 memcpy (dest, inbuf->byte, numbytes);
472 case PNM_FORMAT_PGM_RAW:
473 case PNM_FORMAT_PPM_RAW:
474 dest = context->dptr + offset;
476 if (context->maxval == 255) {
477 /* special-case optimization */
478 memcpy (dest, inbuf->byte, numbytes);
480 for (i = 0; i < numbytes; i++) {
481 guchar *byte = inbuf->byte + i;
483 /* scale the color to an 8-bit color depth */
484 if (*byte > context->maxval)
487 *dest++ = (guchar) (255 * *byte / context->maxval);
492 g_set_error (context->error,
494 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
495 _("Raw PNM image type is invalid"));
496 return PNM_FATAL_ERR;
499 inbuf->byte += numbytes;
500 inbuf->nbytes -= numbytes;
502 context->output_col += numpix;
503 if (context->output_col == context->width) {
504 if (context->type == PNM_FORMAT_PBM_RAW)
505 explode_bitmap_into_buf (context);
506 else if (context->type == PNM_FORMAT_PGM_RAW)
507 explode_gray_into_buf (context);
509 context->output_col = 0;
510 context->output_row++;
519 pnm_read_ascii_scanline (PnmLoaderContext *context)
523 guint value, numval, i;
529 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
533 inbuf = &context->inbuf;
535 context->dptr = context->pixels + context->output_row * context->rowstride;
537 switch (context->type) {
539 numval = MIN (8, context->width - context->output_col);
540 offset = context->output_col / 8;
544 offset = context->output_col;
548 offset = context->output_col * 3;
552 g_set_error (context->error,
554 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
555 _("PNM image format is invalid"));
557 return PNM_FATAL_ERR;
560 dptr = context->dptr + offset + context->scan_state;
563 if (context->type == PNM_FORMAT_PBM) {
566 numval = MIN (8, context->width - context->output_col);
569 for (i = context->scan_state; i < numval; i++) {
570 retval = pnm_read_next_value (inbuf, &value,
572 if (retval != PNM_OK) {
573 /* save state and return */
574 context->scan_state = i;
578 switch (context->type) {
587 /* scale the color to an 8-bit color depth */
588 if (value > context->maxval)
591 *dptr++ = (guchar)(255 * value / context->maxval);
594 g_set_error (context->error,
596 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
597 _("PNM image format is invalid"));
598 return PNM_FATAL_ERR;
603 context->scan_state = 0;
605 if (context->type == PNM_FORMAT_PBM) {
607 context->output_col += numval;
609 context->output_col++;
612 if (context->output_col == context->width) {
613 if (context->type == PNM_FORMAT_PBM)
614 explode_bitmap_into_buf (context);
615 else if (context->type == PNM_FORMAT_PGM)
616 explode_gray_into_buf (context);
618 context->output_col = 0;
619 context->output_row++;
627 /* returns 1 if a scanline was converted, 0 means we ran out of data */
629 pnm_read_scanline (PnmLoaderContext *context)
633 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
635 /* read in image data */
636 /* for raw formats this is trivial */
637 switch (context->type) {
638 case PNM_FORMAT_PBM_RAW:
639 case PNM_FORMAT_PGM_RAW:
640 case PNM_FORMAT_PPM_RAW:
641 retval = pnm_read_raw_scanline (context);
642 if (retval != PNM_OK)
648 retval = pnm_read_ascii_scanline (context);
649 if (retval != PNM_OK)
653 g_set_error (context->error,
655 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
656 _("PNM image loader does not support this PNM subformat"));
658 return PNM_FATAL_ERR;
664 /* Shared library entry point */
666 gdk_pixbuf__pnm_image_load (FILE *f, GError **error)
668 PnmLoaderContext context;
673 /* pretend to be doing progressive loading */
674 context.updated_func = NULL;
675 context.prepared_func = NULL;
676 context.user_data = NULL;
678 context.inbuf.nbytes = 0;
679 context.inbuf.byte = NULL;
683 context.pixels = NULL;
684 context.pixbuf = NULL;
685 context.got_header = FALSE;
686 context.did_prescan = FALSE;
687 context.scan_state = 0;
688 context.error = error;
690 inbuf = &context.inbuf;
695 /* keep buffer as full as possible */
696 num_to_read = PNM_BUF_SIZE - inbuf->nbytes;
698 if (inbuf->byte != NULL && inbuf->nbytes > 0)
699 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
701 nbytes = fread (inbuf->buffer + inbuf->nbytes, 1, num_to_read, f);
705 /* we ran out of data? */
707 g_object_unref (context.pixbuf);
710 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
711 _("Premature end-of-file encountered"));
715 inbuf->nbytes += nbytes;
716 inbuf->byte = inbuf->buffer;
718 /* get header if needed */
719 if (!context.got_header) {
720 retval = pnm_read_header (&context);
721 if (retval == PNM_FATAL_ERR)
723 else if (retval == PNM_SUSPEND)
726 context.got_header = TRUE;
729 /* scan until we hit image data */
730 if (!context.did_prescan) {
731 switch (context.type) {
732 case PNM_FORMAT_PBM_RAW:
733 case PNM_FORMAT_PGM_RAW:
734 case PNM_FORMAT_PPM_RAW:
735 if (inbuf->nbytes <= 0)
737 /* raw formats require exactly one whitespace */
738 if (!g_ascii_isspace(*(inbuf->byte)))
742 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
743 _("Raw PNM formats require exactly one whitespace before sample data"));
750 retval = pnm_skip_whitespace (inbuf,
752 if (retval == PNM_FATAL_ERR)
754 else if (retval == PNM_SUSPEND)
758 context.did_prescan = TRUE;
759 context.output_row = 0;
760 context.output_col = 0;
762 context.pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
763 context.width, context.height);
765 if (!context.pixbuf) {
766 /* Failed to allocate memory */
769 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
770 _("Cannot allocate memory for loading PNM image"));
774 context.rowstride = context.pixbuf->rowstride;
775 context.pixels = context.pixbuf->pixels;
778 /* if we got here we're reading image data */
779 while (context.output_row < context.height) {
780 retval = pnm_read_scanline (&context);
782 if (retval == PNM_SUSPEND) {
784 } else if (retval == PNM_FATAL_ERR) {
786 g_object_unref (context.pixbuf);
792 if (context.output_row < context.height)
798 return context.pixbuf;
802 * func - called when we have pixmap created (but no image data)
803 * user_data - passed as arg 1 to func
804 * return context (opaque to user)
808 gdk_pixbuf__pnm_image_begin_load (GdkPixbufModuleSizeFunc size_func,
809 GdkPixbufModulePreparedFunc prepared_func,
810 GdkPixbufModuleUpdatedFunc updated_func,
814 PnmLoaderContext *context;
816 context = g_try_malloc (sizeof (PnmLoaderContext));
818 g_set_error(error, GDK_PIXBUF_ERROR,
819 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
820 _("Insufficient memory to load PNM context struct"));
823 memset (context, 0, sizeof (PnmLoaderContext));
824 context->size_func = size_func;
825 context->prepared_func = prepared_func;
826 context->updated_func = updated_func;
827 context->user_data = user_data;
831 context->pixbuf = NULL;
832 context->pixels = NULL;
833 context->got_header = FALSE;
834 context->did_prescan = FALSE;
835 context->scan_state = 0;
837 context->inbuf.nbytes = 0;
838 context->inbuf.byte = NULL;
840 context->error = error;
842 return (gpointer) context;
846 * context - returned from image_begin_load
848 * free context, unref gdk_pixbuf
851 gdk_pixbuf__pnm_image_stop_load (gpointer data,
854 PnmLoaderContext *context = (PnmLoaderContext *) data;
855 gboolean retval = TRUE;
857 g_return_val_if_fail (context != NULL, TRUE);
860 g_object_unref (context->pixbuf);
863 /* We should ignore trailing newlines and we can't
864 generally complain about trailing stuff at all, since
865 pnm allows to put multiple images in a file
867 if (context->inbuf.nbytes > 0) {
870 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
871 _("Unexpected end of PNM image data"));
882 * context - from image_begin_load
883 * buf - new image data
884 * size - length of new image data
886 * append image data onto inrecrementally built output image
889 gdk_pixbuf__pnm_image_load_increment (gpointer data,
890 const guchar *buf, guint size,
893 PnmLoaderContext *context = (PnmLoaderContext *)data;
898 guint num_left, spinguard;
901 g_return_val_if_fail (context != NULL, FALSE);
902 g_return_val_if_fail (buf != NULL, FALSE);
904 context->error = error;
907 inbuf = &context->inbuf;
908 old_nbytes = inbuf->nbytes;
909 old_byte = inbuf->byte;
916 /* keep buffer as full as possible */
917 num_to_copy = MIN (PNM_BUF_SIZE - inbuf->nbytes, num_left);
919 if (num_to_copy == 0)
925 if (inbuf->byte != NULL && inbuf->nbytes > 0)
926 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
928 memcpy (inbuf->buffer + inbuf->nbytes, bufhd, num_to_copy);
929 bufhd += num_to_copy;
930 inbuf->nbytes += num_to_copy;
931 inbuf->byte = inbuf->buffer;
932 num_left -= num_to_copy;
934 /* ran out of data and we haven't exited main loop */
935 if (inbuf->nbytes == 0)
938 /* get header if needed */
939 if (!context->got_header) {
940 retval = pnm_read_header (context);
942 if (retval == PNM_FATAL_ERR)
944 else if (retval == PNM_SUSPEND)
947 context->got_header = TRUE;
950 if (context->size_func) {
951 gint w = context->width;
952 gint h = context->height;
953 (*context->size_func) (&w, &h, context->user_data);
955 if (w == 0 || h == 0)
960 /* scan until we hit image data */
961 if (!context->did_prescan) {
962 switch (context->type) {
963 case PNM_FORMAT_PBM_RAW:
964 case PNM_FORMAT_PGM_RAW:
965 case PNM_FORMAT_PPM_RAW:
966 if (inbuf->nbytes <= 0)
968 /* raw formats require exactly one whitespace */
969 if (!g_ascii_isspace(*(inbuf->byte)))
973 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
974 _("Raw PNM formats require exactly one whitespace before sample data"));
981 retval = pnm_skip_whitespace (inbuf,
983 if (retval == PNM_FATAL_ERR)
985 else if (retval == PNM_SUSPEND)
989 context->did_prescan = TRUE;
990 context->output_row = 0;
991 context->output_col = 0;
993 context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
999 if (context->pixbuf == NULL) {
1002 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1003 _("Insufficient memory to load PNM file"));
1007 context->pixels = context->pixbuf->pixels;
1008 context->rowstride = context->pixbuf->rowstride;
1010 /* Notify the client that we are ready to go */
1011 (* context->prepared_func) (context->pixbuf,
1013 context->user_data);
1016 /* if we got here we're reading image data */
1017 while (context->output_row < context->height) {
1018 retval = pnm_read_scanline (context);
1020 if (retval == PNM_SUSPEND) {
1022 } else if (retval == PNM_FATAL_ERR) {
1024 } else if (retval == PNM_OK) {
1025 /* send updated signal */
1026 (* context->updated_func) (context->pixbuf,
1028 context->output_row-1,
1031 context->user_data);
1035 if (context->output_row < context->height)
1045 MODULE_ENTRY (pnm, fill_vtable) (GdkPixbufModule *module)
1047 module->load = gdk_pixbuf__pnm_image_load;
1048 module->begin_load = gdk_pixbuf__pnm_image_begin_load;
1049 module->stop_load = gdk_pixbuf__pnm_image_stop_load;
1050 module->load_increment = gdk_pixbuf__pnm_image_load_increment;
1054 MODULE_ENTRY (pnm, fill_info) (GdkPixbufFormat *info)
1056 static GdkPixbufModulePattern signature[] = {
1057 { "P1", NULL, 100 },
1058 { "P2", NULL, 100 },
1059 { "P3", NULL, 100 },
1060 { "P4", NULL, 100 },
1061 { "P5", NULL, 100 },
1062 { "P6", NULL, 100 },
1065 static gchar * mime_types[] = {
1066 "image/x-portable-anymap",
1067 "image/x-portable-bitmap",
1068 "image/x-portable-graymap",
1069 "image/x-portable-pixmap",
1072 static gchar * extensions[] = {
1081 info->signature = signature;
1082 info->description = N_("The PNM/PBM/PGM/PPM image format family");
1083 info->mime_types = mime_types;
1084 info->extensions = extensions;