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 == inend || *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, gint max_length, 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);
230 /* skip white space */
231 if ((retval = pnm_skip_whitespace (inbuf, error)) != PNM_OK)
234 inend = inbuf->byte + inbuf->nbytes;
237 /* copy this pnm 'word' into a temp buffer */
238 for (p = inptr, word = buf; (p < inend) && !g_ascii_isspace (*p) && (*p != '#') && (p - inptr < max_length); p++, word++)
242 /* hmmm, there must be more data to this 'word' */
243 if (p == inend || (!g_ascii_isspace (*p) && (*p != '#') && (p - inptr < max_length)))
247 result = strtol (buf, &endptr, 10);
248 if (*endptr != '\0' || result < 0 || result > G_MAXUINT) {
251 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
252 _("PNM loader expected to find an integer, but didn't"));
253 return PNM_FATAL_ERR;
258 inbuf->nbytes = (guint) (inend - p);
263 /* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
265 pnm_read_header (PnmLoaderContext *context)
270 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
272 inbuf = &context->inbuf;
274 if (!context->type) {
275 /* file must start with a 'P' followed by a numeral */
276 /* so loop till we get enough data to determine type */
277 if (inbuf->nbytes < 2)
280 if (*inbuf->byte != 'P') {
281 g_set_error (context->error,
283 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
284 _("PNM file has an incorrect initial byte"));
285 return PNM_FATAL_ERR;
291 switch (*inbuf->byte) {
293 context->type = PNM_FORMAT_PBM;
296 context->type = PNM_FORMAT_PGM;
299 context->type = PNM_FORMAT_PPM;
302 context->type = PNM_FORMAT_PBM_RAW;
305 context->type = PNM_FORMAT_PGM_RAW;
308 context->type = PNM_FORMAT_PPM_RAW;
311 g_set_error (context->error,
313 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
314 _("PNM file is not in a recognized PNM subformat"));
315 return PNM_FATAL_ERR;
325 if (!context->width) {
326 /* read the pixmap width */
329 retval = pnm_read_next_value (inbuf, -1, &width,
332 if (retval != PNM_OK)
336 g_set_error (context->error,
338 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
339 _("PNM file has an image width of 0"));
340 return PNM_FATAL_ERR;
343 context->width = width;
346 if (!context->height) {
347 /* read the pixmap height */
350 retval = pnm_read_next_value (inbuf, -1, &height,
353 if (retval != PNM_OK)
357 g_set_error (context->error,
359 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
360 _("PNM file has an image height of 0"));
361 return PNM_FATAL_ERR;
364 context->height = height;
367 switch (context->type) {
369 case PNM_FORMAT_PPM_RAW:
371 case PNM_FORMAT_PGM_RAW:
372 if (!context->maxval) {
373 retval = pnm_read_next_value (inbuf, -1, &context->maxval,
376 if (retval != PNM_OK)
379 if (context->maxval == 0) {
380 g_set_error (context->error,
382 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
383 _("Maximum color value in PNM file is 0"));
384 return PNM_FATAL_ERR;
387 if (context->maxval > 65535) {
388 g_set_error (context->error,
390 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
391 _("Maximum color value in PNM file is too large"));
392 return PNM_FATAL_ERR;
405 pnm_read_raw_scanline (PnmLoaderContext *context)
408 guint numbytes, offset;
413 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
415 inbuf = &context->inbuf;
417 switch (context->type) {
418 case PNM_FORMAT_PBM_RAW:
419 numpix = inbuf->nbytes * 8;
421 case PNM_FORMAT_PGM_RAW:
422 numpix = inbuf->nbytes;
424 case PNM_FORMAT_PPM_RAW:
425 numpix = inbuf->nbytes / 3;
428 g_set_error (context->error,
430 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
431 _("Raw PNM image type is invalid"));
432 return PNM_FATAL_ERR;
434 if(context->maxval>255)
437 numpix = MIN (numpix, context->width - context->output_col);
442 context->dptr = context->pixels + context->output_row * context->rowstride;
444 switch (context->type) {
445 case PNM_FORMAT_PBM_RAW:
446 numbytes = (numpix / 8) + ((numpix % 8) ? 1 : 0);
447 offset = context->output_col / 8;
449 case PNM_FORMAT_PGM_RAW:
451 offset = context->output_col;
453 case PNM_FORMAT_PPM_RAW:
454 numbytes = numpix * 3;
455 offset = context->output_col * 3;
458 g_set_error (context->error,
460 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
461 _("Raw PNM image type is invalid"));
462 return PNM_FATAL_ERR;
464 if(context->maxval>255)
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);
479 } else if(context->maxval == 65535) {
480 /* optimized version of the next case */
481 for(i=0; i < numbytes ; i+=2) {
482 *dest++=inbuf->byte[i];
484 } else if(context->maxval > 255) {
485 /* scale down to 256 colors */
486 for(i=0; i < numbytes ; i+=2) {
487 guint v=inbuf->byte[i]*256+inbuf->byte[i+1];
488 *dest++=v*255/context->maxval;
491 for (i = 0; i < numbytes; i++) {
492 guchar *byte = inbuf->byte + i;
494 /* scale the color to an 8-bit color depth */
495 if (*byte > context->maxval)
498 *dest++ = (guchar) (255 * *byte / context->maxval);
503 g_set_error (context->error,
505 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
506 _("Raw PNM image type is invalid"));
507 return PNM_FATAL_ERR;
510 inbuf->byte += numbytes;
511 inbuf->nbytes -= numbytes;
513 context->output_col += numpix;
514 if (context->output_col == context->width) {
515 if (context->type == PNM_FORMAT_PBM_RAW)
516 explode_bitmap_into_buf (context);
517 else if (context->type == PNM_FORMAT_PGM_RAW)
518 explode_gray_into_buf (context);
520 context->output_col = 0;
521 context->output_row++;
530 pnm_read_ascii_mono_scanline (PnmLoaderContext *context)
538 if (context->type == PNM_FORMAT_PBM)
543 inbuf = &context->inbuf;
545 context->dptr = context->pixels + context->output_row * context->rowstride;
547 dptr = context->dptr + context->output_col * 3;
550 retval = pnm_read_next_value (inbuf, max_length, &value, context->error);
551 if (retval != PNM_OK)
554 if (context->type == PNM_FORMAT_PBM) {
555 value = value ? 0 : 0xff;
558 /* scale the color up or down to an 8-bit color depth */
559 if (value > context->maxval)
562 value = (guchar)(255 * value / context->maxval);
569 context->output_col++;
571 if (context->output_col == context->width) {
572 context->output_col = 0;
573 context->output_row++;
582 pnm_read_ascii_color_scanline (PnmLoaderContext *context)
589 inbuf = &context->inbuf;
591 context->dptr = context->pixels + context->output_row * context->rowstride;
593 dptr = context->dptr + context->output_col * 3 + context->scan_state;
596 for (i = context->scan_state; i < 3; i++) {
597 retval = pnm_read_next_value (inbuf, -1, &value, context->error);
598 if (retval != PNM_OK) {
599 /* save state and return */
600 context->scan_state = i;
604 if (value > context->maxval)
607 *dptr++ = (guchar)(255 * value / context->maxval);
610 context->scan_state = 0;
611 context->output_col++;
613 if (context->output_col == context->width) {
614 context->output_col = 0;
615 context->output_row++;
623 /* returns 1 if a scanline was converted, 0 means we ran out of data */
625 pnm_read_scanline (PnmLoaderContext *context)
629 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
631 /* read in image data */
632 /* for raw formats this is trivial */
633 switch (context->type) {
634 case PNM_FORMAT_PBM_RAW:
635 case PNM_FORMAT_PGM_RAW:
636 case PNM_FORMAT_PPM_RAW:
637 retval = pnm_read_raw_scanline (context);
638 if (retval != PNM_OK)
643 retval = pnm_read_ascii_mono_scanline (context);
644 if (retval != PNM_OK)
648 retval = pnm_read_ascii_color_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 if (context->prepared_func)
1012 (* context->prepared_func) (context->pixbuf,
1014 context->user_data);
1017 /* if we got here we're reading image data */
1018 while (context->output_row < context->height) {
1019 retval = pnm_read_scanline (context);
1021 if (retval == PNM_SUSPEND) {
1023 } else if (retval == PNM_FATAL_ERR) {
1025 } else if (retval == PNM_OK && context->updated_func) {
1026 /* send updated signal */
1027 (* context->updated_func) (context->pixbuf,
1029 context->output_row-1,
1032 context->user_data);
1036 if (context->output_row < context->height)
1046 MODULE_ENTRY (pnm, fill_vtable) (GdkPixbufModule *module)
1048 module->load = gdk_pixbuf__pnm_image_load;
1049 module->begin_load = gdk_pixbuf__pnm_image_begin_load;
1050 module->stop_load = gdk_pixbuf__pnm_image_stop_load;
1051 module->load_increment = gdk_pixbuf__pnm_image_load_increment;
1055 MODULE_ENTRY (pnm, fill_info) (GdkPixbufFormat *info)
1057 static GdkPixbufModulePattern signature[] = {
1058 { "P1", NULL, 100 },
1059 { "P2", NULL, 100 },
1060 { "P3", NULL, 100 },
1061 { "P4", NULL, 100 },
1062 { "P5", NULL, 100 },
1063 { "P6", NULL, 100 },
1066 static gchar * mime_types[] = {
1067 "image/x-portable-anymap",
1068 "image/x-portable-bitmap",
1069 "image/x-portable-graymap",
1070 "image/x-portable-pixmap",
1073 static gchar * extensions[] = {
1082 info->signature = signature;
1083 info->description = N_("The PNM/PBM/PGM/PPM image format family");
1084 info->mime_types = mime_types;
1085 info->extensions = extensions;
1086 info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
1087 info->license = "LGPL";