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? */
82 GdkPixbuf *gdk_pixbuf__pnm_image_load (FILE *f);
83 gpointer gdk_pixbuf__pnm_image_begin_load (ModulePreparedNotifyFunc func,
84 ModuleUpdatedNotifyFunc func2,
85 ModuleFrameDoneNotifyFunc frame_done_func,
86 ModuleAnimationDoneNotifyFunc anim_done_func,
88 void gdk_pixbuf__pnm_image_stop_load (gpointer context);
89 gboolean gdk_pixbuf__pnm_image_load_increment (gpointer context, guchar *buf, guint size);
91 static void explode_bitmap_into_buf (PnmLoaderContext *context);
92 static void explode_gray_into_buf (PnmLoaderContext *context);
94 /* Destroy notification function for the pixbuf */
96 free_buffer (guchar *pixels, gpointer data)
102 /* explode bitmap data into rgb components */
103 /* we need to know what the row so we can */
104 /* do sub-byte expansion (since 1 byte = 8 pixels) */
105 /* context->dptr MUST point at first byte in incoming data */
106 /* which corresponds to first pixel of row y */
108 explode_bitmap_into_buf (PnmLoaderContext *context)
111 guchar *from, *to, data;
116 g_return_if_fail (context != NULL);
117 g_return_if_fail (context->dptr != NULL);
119 /* I'm no clever bit-hacker so I'm sure this can be optimized */
120 dptr = context->dptr;
121 y = context->output_row;
122 wid = context->width;
124 from = dptr + ((wid - 1) / 8);
125 to = dptr + (wid - 1) * 3;
126 /* bit = 7 - (((y+1)*wid-1) % 8); */
127 bit = 7 - ((wid-1) % 8);
129 /* get first byte and align properly */
131 for (j = 0; j < bit; j++, data >>= 1);
133 for (x = wid-1; x >= 0; x--) {
134 /* g_print ("%c", (data & 1) ? '*' : ' '); */
136 to[0] = to[1] = to[2] = (data & 0x01) ? 0x00 : 0xff;
150 /* g_print ("\n"); */
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 and comments in input buffer */
181 pnm_skip_whitespace (PnmIOBuffer *inbuf)
183 register guchar *inptr;
186 g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
187 g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
189 inend = inbuf->byte + inbuf->nbytes;
192 for ( ; inptr < inend; inptr++) {
194 /* in comment - skip to the end of this line */
195 for ( ; *inptr != '\n' && inptr < inend; inptr++);
196 } else if (!isspace (*inptr)) {
198 inbuf->nbytes = (guint) (inend - inptr);
204 inbuf->nbytes = (guint) (inend - inptr);
209 /* read next number from buffer */
211 pnm_read_next_value (PnmIOBuffer *inbuf, guint *value)
213 register guchar *inptr, *word, *p;
214 guchar *inend, buf[128];
218 g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
219 g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
220 g_return_val_if_fail (value != NULL, PNM_FATAL_ERR);
222 /* skip white space */
223 if ((retval = pnm_skip_whitespace (inbuf)) != PNM_OK)
226 inend = inbuf->byte + inbuf->nbytes;
229 /* copy this pnm 'word' into a temp buffer */
230 for (p = inptr, word = buf; (p < inend) && !isspace (*p) && (p - inptr < 128); p++, word++)
234 /* hmmm, there must be more data to this 'word' */
239 *value = strtol (buf, &endptr, 10);
241 return PNM_FATAL_ERR;
244 inbuf->nbytes = (guint) (inend - p);
249 /* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
251 pnm_read_header (PnmLoaderContext *context)
256 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
258 inbuf = &context->inbuf;
260 if (!context->type) {
261 /* file must start with a 'P' followed by a numeral */
262 /* so loop till we get enough data to determine type */
263 if (inbuf->nbytes < 2)
266 if (*inbuf->byte != 'P')
267 return PNM_FATAL_ERR;
272 switch (*inbuf->byte) {
274 context->type = PNM_FORMAT_PBM;
277 context->type = PNM_FORMAT_PGM;
280 context->type = PNM_FORMAT_PPM;
283 context->type = PNM_FORMAT_PBM_RAW;
286 context->type = PNM_FORMAT_PGM_RAW;
289 context->type = PNM_FORMAT_PPM_RAW;
292 return PNM_FATAL_ERR;
302 if (!context->width) {
303 /* read the pixmap width */
306 retval = pnm_read_next_value (inbuf, &width);
308 if (retval != PNM_OK)
312 return PNM_FATAL_ERR;
314 context->width = width;
317 if (!context->height) {
318 /* read the pixmap height */
321 retval = pnm_read_next_value (inbuf, &height);
323 if (retval != PNM_OK)
327 return PNM_FATAL_ERR;
329 context->height = height;
332 switch (context->type) {
334 case PNM_FORMAT_PPM_RAW:
336 case PNM_FORMAT_PGM_RAW:
337 if (!context->maxval) {
338 retval = pnm_read_next_value (inbuf, &context->maxval);
340 if (retval != PNM_OK)
343 if (context->maxval == 0)
344 return PNM_FATAL_ERR;
355 pnm_read_raw_scanline (PnmLoaderContext *context)
358 guint numbytes, offset;
363 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
365 inbuf = &context->inbuf;
367 switch (context->type) {
368 case PNM_FORMAT_PBM_RAW:
369 numpix = inbuf->nbytes * 8;
371 case PNM_FORMAT_PGM_RAW:
372 numpix = inbuf->nbytes;
374 case PNM_FORMAT_PPM_RAW:
375 numpix = inbuf->nbytes / 3;
378 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
379 return PNM_FATAL_ERR;
382 numpix = MIN (numpix, context->width - context->output_col);
387 context->dptr = context->pixels + context->output_row * context->rowstride;
389 switch (context->type) {
390 case PNM_FORMAT_PBM_RAW:
391 numbytes = (numpix / 8) + ((numpix % 8) ? 1 : 0);
392 offset = context->output_col / 8;
394 case PNM_FORMAT_PGM_RAW:
396 offset = context->output_col;
398 case PNM_FORMAT_PPM_RAW:
399 numbytes = numpix * 3;
400 offset = context->output_col * 3;
403 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
404 return PNM_FATAL_ERR;
407 switch (context->type) {
408 case PNM_FORMAT_PBM_RAW:
409 dest = context->dptr + offset;
410 memcpy (dest, inbuf->byte, numbytes);
412 case PNM_FORMAT_PGM_RAW:
413 case PNM_FORMAT_PPM_RAW:
414 dest = context->dptr + offset;
416 if (context->maxval == 255) {
417 /* special-case optimization */
418 memcpy (dest, inbuf->byte, numbytes);
420 for (i = 0; i < numbytes; i++) {
421 guchar *byte = inbuf->byte + i;
423 /* scale the color to an 8-bit color depth */
424 if (*byte > context->maxval)
427 *dest++ = (guchar) (255 * *byte / context->maxval);
432 g_warning ("Invalid raw pnm format!");
435 inbuf->byte += numbytes;
436 inbuf->nbytes -= numbytes;
438 context->output_col += numpix;
439 if (context->output_col == context->width) {
440 if (context->type == PNM_FORMAT_PBM_RAW)
441 explode_bitmap_into_buf (context);
442 else if (context->type == PNM_FORMAT_PGM_RAW)
443 explode_gray_into_buf (context);
445 context->output_col = 0;
446 context->output_row++;
455 pnm_read_ascii_scanline (PnmLoaderContext *context)
459 guint value, numval, i;
465 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
469 inbuf = &context->inbuf;
471 context->dptr = context->pixels + context->output_row * context->rowstride;
473 switch (context->type) {
475 numval = MIN (8, context->width - context->output_col);
476 offset = context->output_col / 8;
480 offset = context->output_col;
484 offset = context->output_col * 3;
488 g_warning ("Can't happen\n");
489 return PNM_FATAL_ERR;
492 dptr = context->dptr + offset + context->scan_state;
495 if (context->type == PNM_FORMAT_PBM) {
498 numval = MIN (8, context->width - context->output_col);
501 for (i = context->scan_state; i < numval; i++) {
502 retval = pnm_read_next_value (inbuf, &value);
503 if (retval != PNM_OK) {
504 /* save state and return */
505 context->scan_state = i;
509 switch (context->type) {
518 /* scale the color to an 8-bit color depth */
519 if (value > context->maxval)
522 *dptr++ = (guchar)(255 * value / context->maxval);
525 g_warning ("io-pnm.c: Illegal ascii pnm type!\n");
530 context->scan_state = 0;
532 if (context->type == PNM_FORMAT_PBM) {
534 context->output_col += numval;
536 context->output_col++;
539 if (context->output_col == context->width) {
540 if (context->type == PNM_FORMAT_PBM)
541 explode_bitmap_into_buf (context);
542 else if (context->type == PNM_FORMAT_PGM)
543 explode_gray_into_buf (context);
545 context->output_col = 0;
546 context->output_row++;
554 /* returns 1 if a scanline was converted, 0 means we ran out of data */
556 pnm_read_scanline (PnmLoaderContext *context)
560 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
562 /* read in image data */
563 /* for raw formats this is trivial */
564 switch (context->type) {
565 case PNM_FORMAT_PBM_RAW:
566 case PNM_FORMAT_PGM_RAW:
567 case PNM_FORMAT_PPM_RAW:
568 retval = pnm_read_raw_scanline (context);
569 if (retval != PNM_OK)
575 retval = pnm_read_ascii_scanline (context);
576 if (retval != PNM_OK)
580 g_warning ("Cannot load these image types (yet)\n");
581 return PNM_FATAL_ERR;
587 /* Shared library entry point */
589 gdk_pixbuf__pnm_image_load (FILE *f)
591 PnmLoaderContext context;
596 /* pretend to be doing progressive loading */
597 context.updated_func = NULL;
598 context.prepared_func = NULL;
599 context.user_data = NULL;
601 context.inbuf.nbytes = 0;
602 context.inbuf.byte = NULL;
606 context.pixels = NULL;
607 context.pixbuf = NULL;
608 context.got_header = FALSE;
609 context.did_prescan = FALSE;
610 context.scan_state = 0;
612 inbuf = &context.inbuf;
617 /* keep buffer as full as possible */
618 num_to_read = PNM_BUF_SIZE - inbuf->nbytes;
620 if (inbuf->byte != NULL && inbuf->nbytes > 0)
621 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
623 nbytes = fread (inbuf->buffer + inbuf->nbytes, 1, num_to_read, f);
626 if (nbytes == 0 && ferror (f)) {
627 /* we ran out of data? */
629 gdk_pixbuf_unref (context.pixbuf);
630 g_warning ("io-pnm.c: Ran out of data.\n");
634 inbuf->nbytes += nbytes;
635 inbuf->byte = inbuf->buffer;
637 /* get header if needed */
638 if (!context.got_header) {
639 retval = pnm_read_header (&context);
640 if (retval == PNM_FATAL_ERR)
642 else if (retval == PNM_SUSPEND)
645 context.got_header = TRUE;
648 /* scan until we hit image data */
649 if (!context.did_prescan) {
650 retval = pnm_skip_whitespace (inbuf);
651 if (retval == PNM_FATAL_ERR)
653 else if (retval == PNM_SUSPEND)
656 context.did_prescan = TRUE;
657 context.output_row = 0;
658 context.output_col = 0;
660 context.rowstride = context.width * 3;
661 context.pixels = g_malloc (context.height * context.width * 3);
663 if (!context.pixels) {
664 /* Failed to allocate memory */
665 g_warning ("Couldn't allocate pixel buf");
669 /* if we got here we're reading image data */
670 while (context.output_row < context.height) {
671 retval = pnm_read_scanline (&context);
673 if (retval == PNM_SUSPEND) {
675 } else if (retval == PNM_FATAL_ERR) {
677 gdk_pixbuf_unref (context.pixbuf);
678 g_warning ("io-pnm.c: error reading rows..\n");
683 if (context.output_row < context.height)
689 return gdk_pixbuf_new_from_data (context.pixels, GDK_COLORSPACE_RGB, FALSE, 8,
690 context.width, context.height,
691 context.width * 3, free_buffer, NULL);
696 * func - called when we have pixmap created (but no image data)
697 * user_data - passed as arg 1 to func
698 * return context (opaque to user)
702 gdk_pixbuf__pnm_image_begin_load (ModulePreparedNotifyFunc prepared_func,
703 ModuleUpdatedNotifyFunc updated_func,
704 ModuleFrameDoneNotifyFunc frame_done_func,
705 ModuleAnimationDoneNotifyFunc anim_done_func,
708 PnmLoaderContext *context;
710 context = g_new0 (PnmLoaderContext, 1);
711 context->prepared_func = prepared_func;
712 context->updated_func = updated_func;
713 context->user_data = user_data;
717 context->pixbuf = NULL;
718 context->pixels = NULL;
719 context->got_header = FALSE;
720 context->did_prescan = FALSE;
721 context->scan_state = 0;
723 context->inbuf.nbytes = 0;
724 context->inbuf.byte = NULL;
726 return (gpointer) context;
730 * context - returned from image_begin_load
732 * free context, unref gdk_pixbuf
735 gdk_pixbuf__pnm_image_stop_load (gpointer data)
737 PnmLoaderContext *context = (PnmLoaderContext *) data;
739 g_return_if_fail (context != NULL);
742 gdk_pixbuf_unref (context->pixbuf);
748 * context - from image_begin_load
749 * buf - new image data
750 * size - length of new image data
752 * append image data onto inrecrementally built output image
755 gdk_pixbuf__pnm_image_load_increment (gpointer data, guchar *buf, guint size)
757 PnmLoaderContext *context = (PnmLoaderContext *)data;
762 guint num_left, spinguard;
765 g_return_val_if_fail (context != NULL, FALSE);
766 g_return_val_if_fail (buf != NULL, FALSE);
769 inbuf = &context->inbuf;
770 old_nbytes = inbuf->nbytes;
771 old_byte = inbuf->byte;
778 /* keep buffer as full as possible */
779 num_to_copy = MIN (PNM_BUF_SIZE - inbuf->nbytes, num_left);
781 if (num_to_copy == 0)
787 if (inbuf->byte != NULL && inbuf->nbytes > 0)
788 memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
790 memcpy (inbuf->buffer + inbuf->nbytes, bufhd, num_to_copy);
791 bufhd += num_to_copy;
792 inbuf->nbytes += num_to_copy;
793 inbuf->byte = inbuf->buffer;
794 num_left -= num_to_copy;
796 /* ran out of data and we haven't exited main loop */
797 if (inbuf->nbytes == 0)
800 /* get header if needed */
801 if (!context->got_header) {
802 retval = pnm_read_header (context);
804 if (retval == PNM_FATAL_ERR)
806 else if (retval == PNM_SUSPEND)
809 context->got_header = TRUE;
812 /* scan until we hit image data */
813 if (!context->did_prescan) {
814 retval = pnm_skip_whitespace (inbuf);
816 if (retval == PNM_FATAL_ERR)
818 else if (retval == PNM_SUSPEND)
821 context->did_prescan = TRUE;
822 context->output_row = 0;
823 context->output_col = 0;
825 context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
831 if (context->pixbuf == NULL) {
832 /* Failed to allocate memory */
833 g_error ("Couldn't allocate gdkpixbuf");
836 context->pixels = context->pixbuf->pixels;
837 context->rowstride = context->pixbuf->rowstride;
839 /* Notify the client that we are ready to go */
840 (* context->prepared_func) (context->pixbuf,
844 /* if we got here we're reading image data */
845 while (context->output_row < context->height) {
846 retval = pnm_read_scanline (context);
848 if (retval == PNM_SUSPEND) {
850 } else if (retval == PNM_FATAL_ERR) {
852 gdk_pixbuf_unref (context->pixbuf);
853 g_warning ("io-pnm.c: error reading rows.\n");
855 } else if (retval == PNM_OK) {
856 /* send updated signal */
857 (* context->updated_func) (context->pixbuf,
859 context->output_row-1,
866 if (context->output_row < context->height)