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;
395 if (context->maxval > 255) {
396 g_set_error (context->error,
398 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
399 _("Cannot handle PNM files with maximum color values greater than 255"));
400 return PNM_FATAL_ERR;
412 pnm_read_raw_scanline (PnmLoaderContext *context)
415 guint numbytes, offset;
420 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
422 inbuf = &context->inbuf;
424 switch (context->type) {
425 case PNM_FORMAT_PBM_RAW:
426 numpix = inbuf->nbytes * 8;
428 case PNM_FORMAT_PGM_RAW:
429 numpix = inbuf->nbytes;
431 case PNM_FORMAT_PPM_RAW:
432 numpix = inbuf->nbytes / 3;
435 g_set_error (context->error,
437 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
438 _("Raw PNM image type is invalid"));
439 return PNM_FATAL_ERR;
442 numpix = MIN (numpix, context->width - context->output_col);
447 context->dptr = context->pixels + context->output_row * context->rowstride;
449 switch (context->type) {
450 case PNM_FORMAT_PBM_RAW:
451 numbytes = (numpix / 8) + ((numpix % 8) ? 1 : 0);
452 offset = context->output_col / 8;
454 case PNM_FORMAT_PGM_RAW:
456 offset = context->output_col;
458 case PNM_FORMAT_PPM_RAW:
459 numbytes = numpix * 3;
460 offset = context->output_col * 3;
463 g_set_error (context->error,
465 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
466 _("Raw PNM image type is invalid"));
467 return PNM_FATAL_ERR;
470 switch (context->type) {
471 case PNM_FORMAT_PBM_RAW:
472 dest = context->dptr + offset;
473 memcpy (dest, inbuf->byte, numbytes);
475 case PNM_FORMAT_PGM_RAW:
476 case PNM_FORMAT_PPM_RAW:
477 dest = context->dptr + offset;
479 if (context->maxval == 255) {
480 /* special-case optimization */
481 memcpy (dest, inbuf->byte, numbytes);
483 for (i = 0; i < numbytes; i++) {
484 guchar *byte = inbuf->byte + i;
486 /* scale the color to an 8-bit color depth */
487 if (*byte > context->maxval)
490 *dest++ = (guchar) (255 * *byte / context->maxval);
495 g_set_error (context->error,
497 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
498 _("Raw PNM image type is invalid"));
499 return PNM_FATAL_ERR;
502 inbuf->byte += numbytes;
503 inbuf->nbytes -= numbytes;
505 context->output_col += numpix;
506 if (context->output_col == context->width) {
507 if (context->type == PNM_FORMAT_PBM_RAW)
508 explode_bitmap_into_buf (context);
509 else if (context->type == PNM_FORMAT_PGM_RAW)
510 explode_gray_into_buf (context);
512 context->output_col = 0;
513 context->output_row++;
522 pnm_read_ascii_scanline (PnmLoaderContext *context)
526 guint value, numval, i;
533 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
537 inbuf = &context->inbuf;
539 context->dptr = context->pixels + context->output_row * context->rowstride;
541 switch (context->type) {
544 numval = MIN (8, context->width - context->output_col);
545 offset = context->output_col / 8;
550 offset = context->output_col;
555 offset = context->output_col * 3;
559 g_set_error (context->error,
561 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
562 _("PNM image format is invalid"));
564 return PNM_FATAL_ERR;
567 dptr = context->dptr + offset + context->scan_state;
570 if (context->type == PNM_FORMAT_PBM) {
573 numval = MIN (8, context->width - context->output_col);
576 for (i = context->scan_state; i < numval; i++) {
577 retval = pnm_read_next_value (inbuf, max_length,
578 &value, context->error);
579 if (retval != PNM_OK) {
580 /* save state and return */
581 context->scan_state = i;
585 switch (context->type) {
594 /* scale the color to an 8-bit color depth */
595 if (value > context->maxval)
598 *dptr++ = (guchar)(255 * value / context->maxval);
601 g_set_error (context->error,
603 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
604 _("PNM image format is invalid"));
605 return PNM_FATAL_ERR;
610 context->scan_state = 0;
612 if (context->type == PNM_FORMAT_PBM) {
614 context->output_col += numval;
616 context->output_col++;
619 if (context->output_col == context->width) {
620 if (context->type == PNM_FORMAT_PBM)
621 explode_bitmap_into_buf (context);
622 else if (context->type == PNM_FORMAT_PGM)
623 explode_gray_into_buf (context);
625 context->output_col = 0;
626 context->output_row++;
634 /* returns 1 if a scanline was converted, 0 means we ran out of data */
636 pnm_read_scanline (PnmLoaderContext *context)
640 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
642 /* read in image data */
643 /* for raw formats this is trivial */
644 switch (context->type) {
645 case PNM_FORMAT_PBM_RAW:
646 case PNM_FORMAT_PGM_RAW:
647 case PNM_FORMAT_PPM_RAW:
648 retval = pnm_read_raw_scanline (context);
649 if (retval != PNM_OK)
655 retval = pnm_read_ascii_scanline (context);
656 if (retval != PNM_OK)
660 g_set_error (context->error,
662 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
663 _("PNM image loader does not support this PNM subformat"));
665 return PNM_FATAL_ERR;
671 /* Shared library entry point */
673 gdk_pixbuf__pnm_image_load (FILE *f, GError **error)
675 PnmLoaderContext context;
680 /* pretend to be doing progressive loading */
681 context.updated_func = NULL;
682 context.prepared_func = NULL;
683 context.user_data = NULL;
685 context.inbuf.nbytes = 0;
686 context.inbuf.byte = NULL;
690 context.pixels = NULL;
691 context.pixbuf = NULL;
692 context.got_header = FALSE;
693 context.did_prescan = FALSE;
694 context.scan_state = 0;
695 context.error = error;
697 inbuf = &context.inbuf;
702 /* keep buffer as full as possible */
703 num_to_read = PNM_BUF_SIZE - inbuf->nbytes;
705 if (inbuf->byte != NULL && inbuf->nbytes > 0)
706 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
708 nbytes = fread (inbuf->buffer + inbuf->nbytes, 1, num_to_read, f);
712 /* we ran out of data? */
714 g_object_unref (context.pixbuf);
717 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
718 _("Premature end-of-file encountered"));
722 inbuf->nbytes += nbytes;
723 inbuf->byte = inbuf->buffer;
725 /* get header if needed */
726 if (!context.got_header) {
727 retval = pnm_read_header (&context);
728 if (retval == PNM_FATAL_ERR)
730 else if (retval == PNM_SUSPEND)
733 context.got_header = TRUE;
736 /* scan until we hit image data */
737 if (!context.did_prescan) {
738 switch (context.type) {
739 case PNM_FORMAT_PBM_RAW:
740 case PNM_FORMAT_PGM_RAW:
741 case PNM_FORMAT_PPM_RAW:
742 if (inbuf->nbytes <= 0)
744 /* raw formats require exactly one whitespace */
745 if (!g_ascii_isspace(*(inbuf->byte)))
749 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
750 _("Raw PNM formats require exactly one whitespace before sample data"));
757 retval = pnm_skip_whitespace (inbuf,
759 if (retval == PNM_FATAL_ERR)
761 else if (retval == PNM_SUSPEND)
765 context.did_prescan = TRUE;
766 context.output_row = 0;
767 context.output_col = 0;
769 context.pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
770 context.width, context.height);
772 if (!context.pixbuf) {
773 /* Failed to allocate memory */
776 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
777 _("Cannot allocate memory for loading PNM image"));
781 context.rowstride = context.pixbuf->rowstride;
782 context.pixels = context.pixbuf->pixels;
785 /* if we got here we're reading image data */
786 while (context.output_row < context.height) {
787 retval = pnm_read_scanline (&context);
789 if (retval == PNM_SUSPEND) {
791 } else if (retval == PNM_FATAL_ERR) {
793 g_object_unref (context.pixbuf);
799 if (context.output_row < context.height)
805 return context.pixbuf;
809 * func - called when we have pixmap created (but no image data)
810 * user_data - passed as arg 1 to func
811 * return context (opaque to user)
815 gdk_pixbuf__pnm_image_begin_load (GdkPixbufModuleSizeFunc size_func,
816 GdkPixbufModulePreparedFunc prepared_func,
817 GdkPixbufModuleUpdatedFunc updated_func,
821 PnmLoaderContext *context;
823 context = g_try_malloc (sizeof (PnmLoaderContext));
825 g_set_error(error, GDK_PIXBUF_ERROR,
826 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
827 _("Insufficient memory to load PNM context struct"));
830 memset (context, 0, sizeof (PnmLoaderContext));
831 context->size_func = size_func;
832 context->prepared_func = prepared_func;
833 context->updated_func = updated_func;
834 context->user_data = user_data;
838 context->pixbuf = NULL;
839 context->pixels = NULL;
840 context->got_header = FALSE;
841 context->did_prescan = FALSE;
842 context->scan_state = 0;
844 context->inbuf.nbytes = 0;
845 context->inbuf.byte = NULL;
847 context->error = error;
849 return (gpointer) context;
853 * context - returned from image_begin_load
855 * free context, unref gdk_pixbuf
858 gdk_pixbuf__pnm_image_stop_load (gpointer data,
861 PnmLoaderContext *context = (PnmLoaderContext *) data;
862 gboolean retval = TRUE;
864 g_return_val_if_fail (context != NULL, TRUE);
867 g_object_unref (context->pixbuf);
870 /* We should ignore trailing newlines and we can't
871 generally complain about trailing stuff at all, since
872 pnm allows to put multiple images in a file
874 if (context->inbuf.nbytes > 0) {
877 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
878 _("Unexpected end of PNM image data"));
889 * context - from image_begin_load
890 * buf - new image data
891 * size - length of new image data
893 * append image data onto inrecrementally built output image
896 gdk_pixbuf__pnm_image_load_increment (gpointer data,
897 const guchar *buf, guint size,
900 PnmLoaderContext *context = (PnmLoaderContext *)data;
905 guint num_left, spinguard;
908 g_return_val_if_fail (context != NULL, FALSE);
909 g_return_val_if_fail (buf != NULL, FALSE);
911 context->error = error;
914 inbuf = &context->inbuf;
915 old_nbytes = inbuf->nbytes;
916 old_byte = inbuf->byte;
923 /* keep buffer as full as possible */
924 num_to_copy = MIN (PNM_BUF_SIZE - inbuf->nbytes, num_left);
926 if (num_to_copy == 0)
932 if (inbuf->byte != NULL && inbuf->nbytes > 0)
933 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
935 memcpy (inbuf->buffer + inbuf->nbytes, bufhd, num_to_copy);
936 bufhd += num_to_copy;
937 inbuf->nbytes += num_to_copy;
938 inbuf->byte = inbuf->buffer;
939 num_left -= num_to_copy;
941 /* ran out of data and we haven't exited main loop */
942 if (inbuf->nbytes == 0)
945 /* get header if needed */
946 if (!context->got_header) {
947 retval = pnm_read_header (context);
949 if (retval == PNM_FATAL_ERR)
951 else if (retval == PNM_SUSPEND)
954 context->got_header = TRUE;
957 if (context->size_func) {
958 gint w = context->width;
959 gint h = context->height;
960 (*context->size_func) (&w, &h, context->user_data);
962 if (w == 0 || h == 0)
967 /* scan until we hit image data */
968 if (!context->did_prescan) {
969 switch (context->type) {
970 case PNM_FORMAT_PBM_RAW:
971 case PNM_FORMAT_PGM_RAW:
972 case PNM_FORMAT_PPM_RAW:
973 if (inbuf->nbytes <= 0)
975 /* raw formats require exactly one whitespace */
976 if (!g_ascii_isspace(*(inbuf->byte)))
980 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
981 _("Raw PNM formats require exactly one whitespace before sample data"));
988 retval = pnm_skip_whitespace (inbuf,
990 if (retval == PNM_FATAL_ERR)
992 else if (retval == PNM_SUSPEND)
996 context->did_prescan = TRUE;
997 context->output_row = 0;
998 context->output_col = 0;
1000 context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
1006 if (context->pixbuf == NULL) {
1009 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1010 _("Insufficient memory to load PNM file"));
1014 context->pixels = context->pixbuf->pixels;
1015 context->rowstride = context->pixbuf->rowstride;
1017 /* Notify the client that we are ready to go */
1018 if (context->prepared_func)
1019 (* context->prepared_func) (context->pixbuf,
1021 context->user_data);
1024 /* if we got here we're reading image data */
1025 while (context->output_row < context->height) {
1026 retval = pnm_read_scanline (context);
1028 if (retval == PNM_SUSPEND) {
1030 } else if (retval == PNM_FATAL_ERR) {
1032 } else if (retval == PNM_OK && context->updated_func) {
1033 /* send updated signal */
1034 (* context->updated_func) (context->pixbuf,
1036 context->output_row-1,
1039 context->user_data);
1043 if (context->output_row < context->height)
1053 MODULE_ENTRY (pnm, fill_vtable) (GdkPixbufModule *module)
1055 module->load = gdk_pixbuf__pnm_image_load;
1056 module->begin_load = gdk_pixbuf__pnm_image_begin_load;
1057 module->stop_load = gdk_pixbuf__pnm_image_stop_load;
1058 module->load_increment = gdk_pixbuf__pnm_image_load_increment;
1062 MODULE_ENTRY (pnm, fill_info) (GdkPixbufFormat *info)
1064 static GdkPixbufModulePattern signature[] = {
1065 { "P1", NULL, 100 },
1066 { "P2", NULL, 100 },
1067 { "P3", NULL, 100 },
1068 { "P4", NULL, 100 },
1069 { "P5", NULL, 100 },
1070 { "P6", NULL, 100 },
1073 static gchar * mime_types[] = {
1074 "image/x-portable-anymap",
1075 "image/x-portable-bitmap",
1076 "image/x-portable-graymap",
1077 "image/x-portable-pixmap",
1080 static gchar * extensions[] = {
1089 info->signature = signature;
1090 info->description = N_("The PNM/PBM/PGM/PPM image format family");
1091 info->mime_types = mime_types;
1092 info->extensions = extensions;
1093 info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
1094 info->license = "LGPL";