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;
397 numpix = MIN (numpix, context->width - context->output_col);
402 context->dptr = context->pixels +
403 context->output_row * context->rowstride;
405 switch (context->type) {
406 case PNM_FORMAT_PBM_RAW:
407 numbytes = numpix/8 + ((numpix % 8) ? 1 : 0);
408 offset = context->output_col/8;
410 case PNM_FORMAT_PGM_RAW:
412 offset = context->output_col;
414 case PNM_FORMAT_PPM_RAW:
416 offset = context->output_col*3;
419 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
420 return PNM_FATAL_ERR;
423 memcpy (context->dptr + offset, inbuf->next_byte, numbytes);
425 inbuf->next_byte += numbytes;
426 inbuf->bytes_left -= numbytes;
428 context->output_col += numpix;
429 if (context->output_col == context->width) {
430 if ( context->type == PNM_FORMAT_PBM_RAW )
431 explode_bitmap_into_buf(context);
432 else if ( context->type == PNM_FORMAT_PGM_RAW )
433 explode_gray_into_buf (context);
435 context->output_col = 0;
436 context->output_row++;
447 pnm_read_ascii_scanline (PnmLoaderContext *context)
451 guint value, numval, i;
454 guchar *old_next_byte, *dptr;
455 guint old_bytes_left;
458 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
462 inbuf = &context->inbuf;
464 context->dptr = context->pixels +
465 context->output_row * context->rowstride;
467 switch (context->type) {
469 numval = MIN (8, context->width - context->output_col);
470 offset = context->output_col/8;
474 offset = context->output_col;
478 offset = context->output_col*3;
482 g_warning ("Can't happen\n");
483 return PNM_FATAL_ERR;
486 dptr = context->dptr + offset;
489 if (context->type == PNM_FORMAT_PBM) {
492 numval = MIN (8, context->width - context->output_col);
495 old_next_byte = inbuf->next_byte;
496 old_bytes_left = inbuf->bytes_left;
498 for (i=0; i<numval; i++) {
499 if ((rc = read_next_number (inbuf, &value))) {
500 inbuf->next_byte = old_next_byte;
501 inbuf->bytes_left = old_bytes_left;
504 switch (context->type) {
512 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
515 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
518 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
523 if (context->type == PNM_FORMAT_PBM) {
525 context->output_col += numval;
527 context->output_col++;
530 if (context->output_col == context->width) {
531 if ( context->type == PNM_FORMAT_PBM )
532 explode_bitmap_into_buf(context);
533 else if ( context->type == PNM_FORMAT_PGM )
534 explode_gray_into_buf (context);
536 context->output_col = 0;
537 context->output_row++;
546 /* returns 1 if a scanline was converted, 0 means we ran out of data */
548 pnm_read_scanline (PnmLoaderContext *context)
552 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
554 /* read in image data */
555 /* for raw formats this is trivial */
556 switch (context->type) {
557 case PNM_FORMAT_PBM_RAW:
558 case PNM_FORMAT_PGM_RAW:
559 case PNM_FORMAT_PPM_RAW:
560 rc = pnm_read_raw_scanline (context);
561 if (rc == PNM_SUSPEND)
568 rc = pnm_read_ascii_scanline (context);
569 if (rc == PNM_SUSPEND)
574 g_warning ("Cannot load these image types (yet)\n");
575 return PNM_FATAL_ERR;
581 /* Shared library entry point */
588 PnmLoaderContext context;
591 /* pretend to be doing progressive loading */
592 context.updated_func = NULL;
593 context.prepared_func = NULL;
594 context.user_data = NULL;
595 context.inbuf.bytes_left = 0;
596 context.inbuf.next_byte = NULL;
597 context.pixels = NULL;
598 context.pixbuf = NULL;
599 context.got_header = context.did_prescan = FALSE;
601 inbuf = &context.inbuf;
606 /* keep buffer as full as possible */
607 num_to_read = PNM_BUF_SIZE - inbuf->bytes_left;
609 if (inbuf->next_byte != NULL && inbuf->bytes_left > 0)
610 memmove (inbuf->buffer, inbuf->next_byte,
613 nbytes = fread (inbuf->buffer+inbuf->bytes_left,
615 inbuf->bytes_left += nbytes;
616 inbuf->next_byte = inbuf->buffer;
618 /* ran out of data and we haven't exited main loop */
619 if (inbuf->bytes_left == 0) {
621 gdk_pixbuf_unref (context.pixbuf);
622 g_warning ("io-pnm.c: Ran out of data...\n");
626 /* get header if needed */
627 if (!context.got_header) {
629 rc = pnm_read_header (&context);
630 if (rc == PNM_FATAL_ERR)
632 else if (rc == PNM_SUSPEND)
635 context.got_header = TRUE;
638 /* scan until we hit image data */
639 if (!context.did_prescan) {
641 if (skip_ahead_whitespace (inbuf) == NULL)
644 context.did_prescan = TRUE;
645 context.output_row = 0;
646 context.output_col = 0;
648 context.rowstride = context.width * 3;
649 context.pixels = g_malloc (context.height *
651 if (!context.pixels) {
652 /* Failed to allocate memory */
653 g_error ("Couldn't allocate pixel buf");
657 /* if we got here we're reading image data */
658 while (context.output_row < context.height) {
660 rc = pnm_read_scanline (&context);
662 if (rc == PNM_SUSPEND) {
664 } else if (rc == PNM_FATAL_ERR) {
666 gdk_pixbuf_unref (context.pixbuf);
667 g_warning ("io-pnm.c: error reading rows..\n");
672 if (context.output_row < context.height)
678 return gdk_pixbuf_new_from_data (context.pixels, ART_PIX_RGB, FALSE,
679 context.width, context.height,
680 context.width * 3, free_buffer, NULL);
685 * func - called when we have pixmap created (but no image data)
686 * user_data - passed as arg 1 to func
687 * return context (opaque to user)
691 image_begin_load (ModulePreparedNotifyFunc prepared_func,
692 ModuleUpdatedNotifyFunc updated_func,
693 ModuleFrameDoneNotifyFunc frame_done_func,
694 ModuleAnimationDoneNotifyFunc anim_done_func,
697 PnmLoaderContext *context;
699 context = g_new0 (PnmLoaderContext, 1);
700 context->prepared_func = prepared_func;
701 context->updated_func = updated_func;
702 context->user_data = user_data;
703 context->pixbuf = NULL;
704 context->pixels = NULL;
705 context->got_header = FALSE;
706 context->did_prescan = FALSE;
708 context->inbuf.bytes_left = 0;
709 context->inbuf.next_byte = NULL;
711 return (gpointer) context;
715 * context - returned from image_begin_load
717 * free context, unref gdk_pixbuf
720 image_stop_load (gpointer data)
722 PnmLoaderContext *context = (PnmLoaderContext *) data;
724 g_return_if_fail (context != NULL);
727 gdk_pixbuf_unref (context->pixbuf);
736 * context - from image_begin_load
737 * buf - new image data
738 * size - length of new image data
740 * append image data onto inrecrementally built output image
743 image_load_increment (gpointer data, guchar *buf, guint size)
745 PnmLoaderContext *context = (PnmLoaderContext *)data;
748 guchar *old_next_byte;
749 guint old_bytes_left;
751 guint num_left, spinguard;
754 g_return_val_if_fail (context != NULL, FALSE);
755 g_return_val_if_fail (buf != NULL, FALSE);
758 inbuf = &context->inbuf;
759 old_bytes_left = inbuf->bytes_left;
760 old_next_byte = inbuf->next_byte;
767 /* keep buffer as full as possible */
768 num_to_copy = MIN (PNM_BUF_SIZE - inbuf->bytes_left, num_left);
770 if (num_to_copy == 0)
776 if (inbuf->next_byte != NULL && inbuf->bytes_left > 0)
777 memmove (inbuf->buffer, inbuf->next_byte,
780 memcpy (inbuf->buffer + inbuf->bytes_left, bufhd, num_to_copy);
781 bufhd += num_to_copy;
782 inbuf->bytes_left += num_to_copy;
783 inbuf->next_byte = inbuf->buffer;
784 num_left -= num_to_copy;
786 /* ran out of data and we haven't exited main loop */
787 if (inbuf->bytes_left == 0)
790 /* get header if needed */
791 if (!context->got_header) {
792 rc = pnm_read_header (context);
793 if (rc == PNM_FATAL_ERR)
795 else if (rc == PNM_SUSPEND)
798 context->got_header = TRUE;
801 /* scan until we hit image data */
802 if (!context->did_prescan) {
803 if (skip_ahead_whitespace (inbuf) == NULL)
806 context->did_prescan = TRUE;
807 context->output_row = 0;
808 context->output_col = 0;
810 context->pixbuf = gdk_pixbuf_new(ART_PIX_RGB,
811 /*have_alpha*/ FALSE,
816 if (context->pixbuf == NULL) {
817 /* Failed to allocate memory */
818 g_error ("Couldn't allocate gdkpixbuf");
821 context->pixels = context->pixbuf->art_pixbuf->pixels;
822 context->rowstride = context->pixbuf->art_pixbuf->rowstride;
824 /* Notify the client that we are ready to go */
825 (* context->prepared_func) (context->pixbuf,
830 /* if we got here we're reading image data */
831 while (context->output_row < context->height) {
832 rc = pnm_read_scanline (context);
834 if (rc == PNM_SUSPEND) {
836 } else if (rc == PNM_FATAL_ERR) {
838 gdk_pixbuf_unref (context->pixbuf);
839 g_warning ("io-pnm.c: error reading rows..\n");
841 } else if (rc == PNM_OK) {
843 /* send updated signal */
844 (* context->updated_func) (context->pixbuf,
846 context->output_row-1,
853 if (context->output_row < context->height)