1 /* GdkPixbuf library - PNM image loader
3 * Copyright (C) 1999 Red Hat, Inc.
5 * Authors: Michael Fulbright <drmike@redhat.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
29 #include "gdk-pixbuf.h"
30 #include "gdk-pixbuf-io.h"
34 #define PNM_BUF_SIZE 4096
38 #define PNM_FATAL_ERR -1
50 guchar buffer[PNM_BUF_SIZE];
56 ModuleUpdatedNotifyFunc updated_func;
57 ModulePreparedNotifyFunc prepared_func;
61 guchar *pixels; /* incoming pixel data buffer */
62 guchar *dptr; /* current position in pixbuf */
72 guint output_row; /* last row to be completed */
74 gboolean did_prescan; /* are we in image data yet? */
75 gboolean got_header; /* have we loaded jpeg header? */
78 GdkPixbuf *image_load (FILE *f);
79 gpointer image_begin_load (ModulePreparedNotifyFunc func,
80 ModuleUpdatedNotifyFunc func2,
81 ModuleFrameDoneNotifyFunc frame_done_func,
82 ModuleAnimationDoneNotifyFunc anim_done_func,
84 void image_stop_load (gpointer context);
85 gboolean image_load_increment(gpointer context, guchar *buf, guint size);
87 static void explode_bitmap_into_buf (PnmLoaderContext *context);
88 static void explode_gray_into_buf (PnmLoaderContext *context);
90 /* Destroy notification function for the libart pixbuf */
92 free_buffer (gpointer user_data, gpointer data)
98 /* explode bitmap data into rgb components */
99 /* we need to know what the row so we can */
100 /* do sub-byte expansion (since 1 byte = 8 pixels) */
101 /* context->dptr MUST point at first byte in incoming data */
102 /* which corresponds to first pixel of row y */
104 explode_bitmap_into_buf (PnmLoaderContext *context)
107 guchar *from, *to, data;
112 g_return_if_fail (context != NULL);
113 g_return_if_fail (context->dptr != NULL);
115 /* I'm no clever bit-hacker so I'm sure this can be optimized */
116 dptr = context->dptr;
117 y = context->output_row;
118 wid = context->width;
120 from = dptr + (wid - 1)/8;
121 to = dptr + (wid - 1) * 3;
122 /* bit = 7 - (((y+1)*wid-1) % 8); */
123 bit = 7 - ((wid-1) % 8);
125 /* get first byte and align properly */
127 for (j = 0; j < bit; j++, data >>= 1);
129 for (x = wid-1; x >= 0; x--) {
131 /* g_print ("%c", (data & 1) ? '*' : ' '); */
133 to[0] = to[1] = to[2] = (data & 1) ? 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 in file from current pos. */
177 /* also skips comments */
178 /* returns pointer to first non-whitespace char hit or, or NULL if */
179 /* we ran out of data w/o hitting a whitespace */
180 /* internal pointer in inbuf isnt moved ahead in this case */
182 skip_ahead_whitespace (PnmIOBuffer *inbuf)
188 g_return_val_if_fail (inbuf != NULL, NULL);
189 g_return_val_if_fail (inbuf->next_byte != NULL, NULL);
192 num_left = inbuf->bytes_left;
193 ptr = inbuf->next_byte;
194 while (num_left > 0) {
198 } else if (*ptr == '#') {
200 } else if (!isspace (*ptr)) {
201 inbuf->bytes_left -= (ptr-inbuf->next_byte);
202 inbuf->next_byte = ptr;
211 /* reads into buffer until we hit whitespace in file from current pos, */
212 /* return NULL if ran out of data */
213 /* advances inbuf if successful */
215 read_til_whitespace (PnmIOBuffer *inbuf, guchar *buf, guint size)
221 g_return_val_if_fail (inbuf != NULL, NULL);
222 g_return_val_if_fail (inbuf->next_byte != NULL, NULL);
225 num_left = inbuf->bytes_left;
226 ptr = inbuf->next_byte;
227 while (num_left > 0 && (p-buf)+1 < size) {
228 if (isspace (*ptr)) {
230 inbuf->bytes_left = num_left;
231 inbuf->next_byte = ptr;
243 /* read next number from buffer */
244 /* -1 if failed, 0 if successful */
246 read_next_number (PnmIOBuffer *inbuf, guint *value)
249 guchar *old_next_byte;
251 guint old_bytes_left;
254 g_return_val_if_fail (inbuf != NULL, -1);
255 g_return_val_if_fail (inbuf->next_byte != NULL, -1);
256 g_return_val_if_fail (value != NULL, -1);
258 old_next_byte = inbuf->next_byte;
259 old_bytes_left = inbuf->bytes_left;
261 if ((tmpptr = skip_ahead_whitespace (inbuf)) == NULL)
264 if ((tmpptr = read_til_whitespace (inbuf, buf, 128)) == NULL) {
265 inbuf->next_byte = old_next_byte;
266 inbuf->bytes_left = old_bytes_left;
270 *value = strtol (buf, &errptr, 10);
272 if (*errptr != '\0') {
273 inbuf->next_byte = old_next_byte;
274 inbuf->bytes_left = old_bytes_left;
281 /* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
283 pnm_read_header (PnmLoaderContext *context)
285 guchar *old_next_byte;
286 guint old_bytes_left;
292 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
294 inbuf = &context->inbuf;
295 old_bytes_left = inbuf->bytes_left;
296 old_next_byte = inbuf->next_byte;
298 /* file must start with a 'P' followed by a numeral */
299 /* so loop till we get enough data to determine type*/
300 if (inbuf->bytes_left < 2)
303 if (*inbuf->next_byte != 'P')
304 return PNM_FATAL_ERR;
306 switch (*(inbuf->next_byte+1)) {
308 type = PNM_FORMAT_PBM;
311 type = PNM_FORMAT_PGM;
314 type = PNM_FORMAT_PPM;
317 type = PNM_FORMAT_PBM_RAW;
320 type = PNM_FORMAT_PGM_RAW;
323 type = PNM_FORMAT_PPM_RAW;
326 return PNM_FATAL_ERR;
329 context->type = type;
331 inbuf->next_byte += 2;
332 inbuf->bytes_left -= 2;
334 /* now read remainder of header */
335 if ((rc = read_next_number (inbuf, &w))) {
336 inbuf->next_byte = old_next_byte;
337 inbuf->bytes_left = old_bytes_left;
341 if ((rc = read_next_number (inbuf, &h))) {
342 inbuf->next_byte = old_next_byte;
343 inbuf->bytes_left = old_bytes_left;
352 case PNM_FORMAT_PPM_RAW:
354 case PNM_FORMAT_PGM_RAW:
355 if ((rc = read_next_number (inbuf, &context->maxval)) < 0) {
356 inbuf->next_byte = old_next_byte;
357 inbuf->bytes_left = old_bytes_left;
370 pnm_read_raw_scanline (PnmLoaderContext *context)
373 guint numbytes, offset;
376 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
380 inbuf = &context->inbuf;
382 switch (context->type) {
383 case PNM_FORMAT_PBM_RAW:
384 numpix = inbuf->bytes_left * 8;
386 case PNM_FORMAT_PGM_RAW:
387 numpix = inbuf->bytes_left;
389 case PNM_FORMAT_PPM_RAW:
390 numpix = inbuf->bytes_left/3;
393 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
394 return PNM_FATAL_ERR;
398 numpix = MIN (numpix, context->width - context->output_col);
403 context->dptr = context->pixels +
404 context->output_row * context->rowstride;
406 switch (context->type) {
407 case PNM_FORMAT_PBM_RAW:
408 numbytes = numpix/8 + ((numpix % 8) ? 1 : 0);
409 offset = context->output_col/8;
411 case PNM_FORMAT_PGM_RAW:
413 offset = context->output_col;
415 case PNM_FORMAT_PPM_RAW:
417 offset = context->output_col*3;
420 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
424 memcpy (context->dptr + offset, inbuf->next_byte, numbytes);
426 inbuf->next_byte += numbytes;
427 inbuf->bytes_left -= numbytes;
429 context->output_col += numpix;
430 if (context->output_col == context->width) {
431 if ( context->type == PNM_FORMAT_PBM_RAW )
432 explode_bitmap_into_buf(context);
433 else if ( context->type == PNM_FORMAT_PGM_RAW )
434 explode_gray_into_buf (context);
436 context->output_col = 0;
437 context->output_row++;
448 pnm_read_ascii_scanline (PnmLoaderContext *context)
452 guint value, numval, i;
455 guchar *old_next_byte, *dptr;
456 guint old_bytes_left;
459 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
461 inbuf = &context->inbuf;
463 context->dptr = context->pixels +
464 context->output_row * context->rowstride;
466 switch (context->type) {
468 numval = MIN (8, context->width - context->output_col);
469 offset = context->output_col/8;
473 offset = context->output_col;
477 offset = context->output_col*3;
481 g_warning ("Can't happen\n");
482 return PNM_FATAL_ERR;
485 dptr = context->dptr + offset;
488 if (context->type == PNM_FORMAT_PBM) {
491 numval = MIN (8, context->width - context->output_col);
494 old_next_byte = inbuf->next_byte;
495 old_bytes_left = inbuf->bytes_left;
497 for (i=0; i<numval; i++) {
498 if ((rc = read_next_number (inbuf, &value))) {
499 inbuf->next_byte = old_next_byte;
500 inbuf->bytes_left = old_bytes_left;
503 switch (context->type) {
511 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
514 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
517 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
522 if (context->type == PNM_FORMAT_PBM) {
524 context->output_col += numval;
526 context->output_col++;
529 if (context->output_col == context->width) {
530 if ( context->type == PNM_FORMAT_PBM )
531 explode_bitmap_into_buf(context);
532 else if ( context->type == PNM_FORMAT_PGM )
533 explode_gray_into_buf (context);
535 context->output_col = 0;
536 context->output_row++;
545 /* returns 1 if a scanline was converted, 0 means we ran out of data */
547 pnm_read_scanline (PnmLoaderContext *context)
551 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
553 /* read in image data */
554 /* for raw formats this is trivial */
555 switch (context->type) {
556 case PNM_FORMAT_PBM_RAW:
557 case PNM_FORMAT_PGM_RAW:
558 case PNM_FORMAT_PPM_RAW:
559 rc = pnm_read_raw_scanline (context);
560 if (rc == PNM_SUSPEND)
567 rc = pnm_read_ascii_scanline (context);
568 if (rc == PNM_SUSPEND)
573 g_warning ("Cannot load these image types (yet)\n");
574 return PNM_FATAL_ERR;
580 /* Shared library entry point */
587 PnmLoaderContext context;
590 /* pretend to be doing progressive loading */
591 context.updated_func = NULL;
592 context.prepared_func = NULL;
593 context.user_data = NULL;
594 context.inbuf.bytes_left = 0;
595 context.inbuf.next_byte = NULL;
596 context.pixels = NULL;
597 context.pixbuf = NULL;
598 context.got_header = context.did_prescan = FALSE;
600 inbuf = &context.inbuf;
605 /* keep buffer as full as possible */
606 num_to_read = PNM_BUF_SIZE - inbuf->bytes_left;
608 if (inbuf->next_byte != NULL && inbuf->bytes_left > 0)
609 memmove (inbuf->buffer, inbuf->next_byte,
612 nbytes = fread (inbuf->buffer+inbuf->bytes_left,
614 inbuf->bytes_left += nbytes;
615 inbuf->next_byte = inbuf->buffer;
617 /* ran out of data and we haven't exited main loop */
618 if (inbuf->bytes_left == 0) {
620 gdk_pixbuf_unref (context.pixbuf);
621 g_warning ("io-pnm.c: Ran out of data...\n");
625 /* get header if needed */
626 if (!context.got_header) {
628 rc = pnm_read_header (&context);
629 if (rc == PNM_FATAL_ERR)
631 else if (rc == PNM_SUSPEND)
634 context.got_header = TRUE;
637 /* scan until we hit image data */
638 if (!context.did_prescan) {
640 if (skip_ahead_whitespace (inbuf) == NULL)
643 context.did_prescan = TRUE;
644 context.output_row = 0;
645 context.output_col = 0;
647 context.rowstride = context.width * 3;
648 context.pixels = g_malloc (context.height *
650 if (!context.pixels) {
651 /* Failed to allocate memory */
652 g_error ("Couldn't allocate pixel buf");
656 /* if we got here we're reading image data */
657 while (context.output_row < context.height) {
659 rc = pnm_read_scanline (&context);
661 if (rc == PNM_SUSPEND) {
663 } else if (rc == PNM_FATAL_ERR) {
665 gdk_pixbuf_unref (context.pixbuf);
666 g_warning ("io-pnm.c: error reading rows..\n");
671 if (context.output_row < context.height)
677 return gdk_pixbuf_new_from_data (context.pixels, ART_PIX_RGB, FALSE,
678 context.width, context.height,
679 context.width * 3, free_buffer, NULL);
684 * func - called when we have pixmap created (but no image data)
685 * user_data - passed as arg 1 to func
686 * return context (opaque to user)
690 image_begin_load (ModulePreparedNotifyFunc prepared_func,
691 ModuleUpdatedNotifyFunc updated_func,
692 ModuleFrameDoneNotifyFunc frame_done_func,
693 ModuleAnimationDoneNotifyFunc anim_done_func,
696 PnmLoaderContext *context;
698 context = g_new0 (PnmLoaderContext, 1);
699 context->prepared_func = prepared_func;
700 context->updated_func = updated_func;
701 context->user_data = user_data;
702 context->pixbuf = NULL;
703 context->pixels = NULL;
704 context->got_header = FALSE;
705 context->did_prescan = FALSE;
707 context->inbuf.bytes_left = 0;
708 context->inbuf.next_byte = NULL;
710 return (gpointer) context;
714 * context - returned from image_begin_load
716 * free context, unref gdk_pixbuf
719 image_stop_load (gpointer data)
721 PnmLoaderContext *context = (PnmLoaderContext *) data;
723 g_return_if_fail (context != NULL);
726 gdk_pixbuf_unref (context->pixbuf);
735 * context - from image_begin_load
736 * buf - new image data
737 * size - length of new image data
739 * append image data onto inrecrementally built output image
742 image_load_increment (gpointer data, guchar *buf, guint size)
744 PnmLoaderContext *context = (PnmLoaderContext *)data;
747 guchar *old_next_byte;
748 guint old_bytes_left;
750 guint num_left, spinguard;
753 g_return_val_if_fail (context != NULL, FALSE);
754 g_return_val_if_fail (buf != NULL, FALSE);
757 inbuf = &context->inbuf;
758 old_bytes_left = inbuf->bytes_left;
759 old_next_byte = inbuf->next_byte;
766 /* keep buffer as full as possible */
767 num_to_copy = MIN (PNM_BUF_SIZE - inbuf->bytes_left, num_left);
769 if (num_to_copy == 0)
775 if (inbuf->next_byte != NULL && inbuf->bytes_left > 0)
776 memmove (inbuf->buffer, inbuf->next_byte,
779 memcpy (inbuf->buffer + inbuf->bytes_left, bufhd, num_to_copy);
780 bufhd += num_to_copy;
781 inbuf->bytes_left += num_to_copy;
782 inbuf->next_byte = inbuf->buffer;
783 num_left -= num_to_copy;
785 /* ran out of data and we haven't exited main loop */
786 if (inbuf->bytes_left == 0)
789 /* get header if needed */
790 if (!context->got_header) {
791 rc = pnm_read_header (context);
792 if (rc == PNM_FATAL_ERR)
794 else if (rc == PNM_SUSPEND)
797 context->got_header = TRUE;
800 /* scan until we hit image data */
801 if (!context->did_prescan) {
802 if (skip_ahead_whitespace (inbuf) == NULL)
805 context->did_prescan = TRUE;
806 context->output_row = 0;
807 context->output_col = 0;
809 context->pixbuf = gdk_pixbuf_new(ART_PIX_RGB,
810 /*have_alpha*/ FALSE,
815 if (context->pixbuf == NULL) {
816 /* Failed to allocate memory */
817 g_error ("Couldn't allocate gdkpixbuf");
820 context->pixels = context->pixbuf->art_pixbuf->pixels;
821 context->rowstride = context->pixbuf->art_pixbuf->rowstride;
823 /* Notify the client that we are ready to go */
824 (* context->prepared_func) (context->pixbuf,
829 /* if we got here we're reading image data */
830 while (context->output_row < context->height) {
831 rc = pnm_read_scanline (context);
833 if (rc == PNM_SUSPEND) {
835 } else if (rc == PNM_FATAL_ERR) {
837 gdk_pixbuf_unref (context->pixbuf);
838 g_warning ("io-pnm.c: error reading rows..\n");
840 } else if (rc == PNM_OK) {
842 /* send updated signal */
843 (* context->updated_func) (context->pixbuf,
845 context->output_row-1,
852 if (context->output_row < context->height)