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.
30 #include "gdk-pixbuf.h"
31 #include "gdk-pixbuf-io.h"
35 #define PNM_BUF_SIZE 4096
39 #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 jpeg header? */
79 GdkPixbuf *image_load (FILE *f);
80 gpointer image_begin_load (ModulePreparedNotifyFunc func,
81 ModuleUpdatedNotifyFunc func2, gpointer user_data);
82 void image_stop_load (gpointer context);
83 gboolean image_load_increment(gpointer context, guchar *buf, guint size);
85 static void explode_bitmap_into_buf (PnmLoaderContext *context);
86 static void explode_gray_into_buf (PnmLoaderContext *context);
88 /* Destroy notification function for the libart pixbuf */
90 free_buffer (gpointer user_data, gpointer data)
96 /* explode bitmap data into rgb components */
97 /* we need to know what the row so we can */
98 /* do sub-byte expansion (since 1 byte = 8 pixels) */
99 /* context->dptr MUST point at first byte in incoming data */
100 /* which corresponds to first pixel of row y */
102 explode_bitmap_into_buf (PnmLoaderContext *context)
105 guchar *from, *to, data;
110 g_return_if_fail (context != NULL);
111 g_return_if_fail (context->dptr != NULL);
113 /* I'm no clever bit-hacker so I'm sure this can be optimized */
114 dptr = context->dptr;
115 y = context->output_row;
116 wid = context->width;
118 from = dptr + (wid - 1)/8;
119 to = dptr + (wid - 1) * 3;
120 /* bit = 7 - (((y+1)*wid-1) % 8); */
121 bit = 7 - ((wid-1) % 8);
123 /* get first byte and align properly */
125 for (j = 0; j < bit; j++, data >>= 1);
127 for (x = wid-1; x >= 0; x--) {
129 /* g_print ("%c", (data & 1) ? '*' : ' '); */
131 to[0] = to[1] = to[2] = (data & 1) ? 0x00 : 0xff;
145 /* g_print ("\n"); */
148 /* explode gray image row into rgb components in pixbuf */
150 explode_gray_into_buf (PnmLoaderContext *context)
156 g_return_if_fail (context != NULL);
157 g_return_if_fail (context->dptr != NULL);
159 /* Expand grey->colour. Expand from the end of the
160 * memory down, so we can use the same buffer.
163 from = context->dptr + w - 1;
164 to = context->dptr + (w - 1) * 3;
165 for (j = w - 1; j >= 0; j--) {
174 /* skip over whitespace in file from current pos. */
175 /* also skips comments */
176 /* returns pointer to first non-whitespace char hit or, or NULL if */
177 /* we ran out of data w/o hitting a whitespace */
178 /* internal pointer in inbuf isnt moved ahead in this case */
180 skip_ahead_whitespace (PnmIOBuffer *inbuf)
186 g_return_val_if_fail (inbuf != NULL, NULL);
187 g_return_val_if_fail (inbuf->next_byte != NULL, NULL);
190 num_left = inbuf->bytes_left;
191 ptr = inbuf->next_byte;
192 while (num_left > 0) {
196 } else if (*ptr == '#') {
198 } else if (!isspace (*ptr)) {
199 inbuf->bytes_left -= (ptr-inbuf->next_byte);
200 inbuf->next_byte = ptr;
209 /* reads into buffer until we hit whitespace in file from current pos, */
210 /* return NULL if ran out of data */
211 /* advances inbuf if successful */
213 read_til_whitespace (PnmIOBuffer *inbuf, guchar *buf, guint size)
219 g_return_val_if_fail (inbuf != NULL, NULL);
220 g_return_val_if_fail (inbuf->next_byte != NULL, NULL);
223 num_left = inbuf->bytes_left;
224 ptr = inbuf->next_byte;
225 while (num_left > 0 && (p-buf)+1 < size) {
226 if (isspace (*ptr)) {
228 inbuf->bytes_left = num_left;
229 inbuf->next_byte = ptr;
241 /* read next number from buffer */
242 /* -1 if failed, 0 if successful */
244 read_next_number (PnmIOBuffer *inbuf, guint *value)
247 guchar *old_next_byte;
249 guint old_bytes_left;
252 g_return_val_if_fail (inbuf != NULL, -1);
253 g_return_val_if_fail (inbuf->next_byte != NULL, -1);
254 g_return_val_if_fail (value != NULL, -1);
256 old_next_byte = inbuf->next_byte;
257 old_bytes_left = inbuf->bytes_left;
259 if ((tmpptr = skip_ahead_whitespace (inbuf)) == NULL)
262 if ((tmpptr = read_til_whitespace (inbuf, buf, 128)) == NULL) {
263 inbuf->next_byte = old_next_byte;
264 inbuf->bytes_left = old_bytes_left;
268 *value = strtol (buf, &errptr, 10);
270 if (*errptr != '\0') {
271 inbuf->next_byte = old_next_byte;
272 inbuf->bytes_left = old_bytes_left;
279 /* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
281 pnm_read_header (PnmLoaderContext *context)
283 guchar *old_next_byte;
284 guint old_bytes_left;
290 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
292 inbuf = &context->inbuf;
293 old_bytes_left = inbuf->bytes_left;
294 old_next_byte = inbuf->next_byte;
296 /* file must start with a 'P' followed by a numeral */
297 /* so loop till we get enough data to determine type*/
298 if (inbuf->bytes_left < 2)
301 if (*inbuf->next_byte != 'P')
302 return PNM_FATAL_ERR;
304 switch (*(inbuf->next_byte+1)) {
306 type = PNM_FORMAT_PBM;
309 type = PNM_FORMAT_PGM;
312 type = PNM_FORMAT_PPM;
315 type = PNM_FORMAT_PBM_RAW;
318 type = PNM_FORMAT_PGM_RAW;
321 type = PNM_FORMAT_PPM_RAW;
324 return PNM_FATAL_ERR;
327 context->type = type;
329 inbuf->next_byte += 2;
330 inbuf->bytes_left -= 2;
332 /* now read remainder of header */
333 if ((rc = read_next_number (inbuf, &w))) {
334 inbuf->next_byte = old_next_byte;
335 inbuf->bytes_left = old_bytes_left;
339 if ((rc = read_next_number (inbuf, &h))) {
340 inbuf->next_byte = old_next_byte;
341 inbuf->bytes_left = old_bytes_left;
350 case PNM_FORMAT_PPM_RAW:
352 case PNM_FORMAT_PGM_RAW:
353 if ((rc = read_next_number (inbuf, &context->maxval)) < 0) {
354 inbuf->next_byte = old_next_byte;
355 inbuf->bytes_left = old_bytes_left;
368 pnm_read_raw_scanline (PnmLoaderContext *context)
371 guint numbytes, offset;
374 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
378 inbuf = &context->inbuf;
380 switch (context->type) {
381 case PNM_FORMAT_PBM_RAW:
382 numpix = inbuf->bytes_left * 8;
384 case PNM_FORMAT_PGM_RAW:
385 numpix = inbuf->bytes_left;
387 case PNM_FORMAT_PPM_RAW:
388 numpix = inbuf->bytes_left/3;
391 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
392 return PNM_FATAL_ERR;
396 numpix = MIN (numpix, context->width - context->output_col);
401 context->dptr = context->pixels +
402 context->output_row * context->rowstride;
404 switch (context->type) {
405 case PNM_FORMAT_PBM_RAW:
406 numbytes = numpix/8 + ((numpix % 8) ? 1 : 0);
407 offset = context->output_col/8;
409 case PNM_FORMAT_PGM_RAW:
411 offset = context->output_col;
413 case PNM_FORMAT_PPM_RAW:
415 offset = context->output_col*3;
418 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
422 memcpy (context->dptr + offset, inbuf->next_byte, numbytes);
424 inbuf->next_byte += numbytes;
425 inbuf->bytes_left -= numbytes;
427 context->output_col += numpix;
428 if (context->output_col == context->width) {
429 if ( context->type == PNM_FORMAT_PBM_RAW )
430 explode_bitmap_into_buf(context);
431 else if ( context->type == PNM_FORMAT_PGM_RAW )
432 explode_gray_into_buf (context);
434 context->output_col = 0;
435 context->output_row++;
446 pnm_read_ascii_scanline (PnmLoaderContext *context)
450 guint value, numval, i;
453 guchar *old_next_byte, *dptr;
454 guint old_bytes_left;
457 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
459 inbuf = &context->inbuf;
461 context->dptr = context->pixels +
462 context->output_row * context->rowstride;
464 switch (context->type) {
466 numval = MIN (8, context->width - context->output_col);
467 offset = context->output_col/8;
471 offset = context->output_col;
475 offset = context->output_col*3;
479 g_warning ("Can't happen\n");
480 return PNM_FATAL_ERR;
483 dptr = context->dptr + offset;
486 if (context->type == PNM_FORMAT_PBM) {
489 numval = MIN (8, context->width - context->output_col);
492 old_next_byte = inbuf->next_byte;
493 old_bytes_left = inbuf->bytes_left;
495 for (i=0; i<numval; i++) {
496 if ((rc = read_next_number (inbuf, &value))) {
497 inbuf->next_byte = old_next_byte;
498 inbuf->bytes_left = old_bytes_left;
501 switch (context->type) {
509 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
512 *dptr++ = (guchar)(255.0*((double)value/(double)context->maxval));
515 g_warning ("io-pnm.c: Illegal raw pnm type!\n");
520 if (context->type == PNM_FORMAT_PBM) {
522 context->output_col += 8;
524 context->output_col++;
527 if (context->output_col == context->width) {
528 if ( context->type == PNM_FORMAT_PBM )
529 explode_bitmap_into_buf(context);
530 else if ( context->type == PNM_FORMAT_PGM )
531 explode_gray_into_buf (context);
533 context->output_col = 0;
534 context->output_row++;
543 /* returns 1 if a scanline was converted, 0 means we ran out of data */
545 pnm_read_scanline (PnmLoaderContext *context)
549 g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
551 /* read in image data */
552 /* for raw formats this is trivial */
553 switch (context->type) {
554 case PNM_FORMAT_PBM_RAW:
555 case PNM_FORMAT_PGM_RAW:
556 case PNM_FORMAT_PPM_RAW:
557 rc = pnm_read_raw_scanline (context);
558 if (rc == PNM_SUSPEND)
565 rc = pnm_read_ascii_scanline (context);
566 if (rc == PNM_SUSPEND)
571 g_warning ("Cannot load these image types (yet)\n");
572 return PNM_FATAL_ERR;
578 /* Shared library entry point */
585 PnmLoaderContext context;
588 /* pretend to be doing progressive loading */
589 context.updated_func = NULL;
590 context.prepared_func = NULL;
591 context.user_data = NULL;
592 context.inbuf.bytes_left = 0;
593 context.inbuf.next_byte = NULL;
594 context.pixels = NULL;
595 context.pixbuf = NULL;
596 context.got_header = context.did_prescan = FALSE;
598 inbuf = &context.inbuf;
603 /* keep buffer as full as possible */
604 num_to_read = PNM_BUF_SIZE - inbuf->bytes_left;
606 if (inbuf->next_byte != NULL && inbuf->bytes_left > 0)
607 memmove (inbuf->buffer, inbuf->next_byte,
610 nbytes = fread (inbuf->buffer+inbuf->bytes_left,
612 inbuf->bytes_left += nbytes;
613 inbuf->next_byte = inbuf->buffer;
615 /* ran out of data and we haven't exited main loop */
616 if (inbuf->bytes_left == 0) {
618 gdk_pixbuf_unref (context.pixbuf);
619 g_warning ("io-pnm.c: Ran out of data...\n");
623 /* get header if needed */
624 if (!context.got_header) {
626 rc = pnm_read_header (&context);
627 if (rc == PNM_FATAL_ERR)
629 else if (rc == PNM_SUSPEND)
632 context.got_header = TRUE;
635 /* scan until we hit image data */
636 if (!context.did_prescan) {
638 if (skip_ahead_whitespace (inbuf) == NULL)
641 context.did_prescan = TRUE;
642 context.output_row = 0;
643 context.output_col = 0;
645 context.rowstride = context.width * 3;
646 context.pixels = g_malloc (context.height *
648 if (!context.pixels) {
649 /* Failed to allocate memory */
650 g_error ("Couldn't allocate pixel buf");
654 /* if we got here we're reading image data */
655 while (context.output_row < context.height) {
657 rc = pnm_read_scanline (&context);
659 if (rc == PNM_SUSPEND) {
661 } else if (rc == PNM_FATAL_ERR) {
663 gdk_pixbuf_unref (context.pixbuf);
664 g_warning ("io-pnm.c: error reading rows..\n");
669 if (context.output_row < context.height)
675 return gdk_pixbuf_new_from_data (context.pixels, ART_PIX_RGB, FALSE,
676 context.width, context.height,
677 context.width * 3, free_buffer, NULL);
682 * func - called when we have pixmap created (but no image data)
683 * user_data - passed as arg 1 to func
684 * return context (opaque to user)
688 image_begin_load (ModulePreparedNotifyFunc prepared_func,
689 ModuleUpdatedNotifyFunc updated_func,
692 PnmLoaderContext *context;
694 context = g_new0 (PnmLoaderContext, 1);
695 context->prepared_func = prepared_func;
696 context->updated_func = updated_func;
697 context->user_data = user_data;
698 context->pixbuf = NULL;
699 context->pixels = NULL;
700 context->got_header = FALSE;
701 context->did_prescan = FALSE;
703 context->inbuf.bytes_left = 0;
704 context->inbuf.next_byte = NULL;
706 return (gpointer) context;
710 * context - returned from image_begin_load
712 * free context, unref gdk_pixbuf
715 image_stop_load (gpointer data)
717 PnmLoaderContext *context = (PnmLoaderContext *) data;
719 g_return_if_fail (context != NULL);
722 gdk_pixbuf_unref (context->pixbuf);
731 * context - from image_begin_load
732 * buf - new image data
733 * size - length of new image data
735 * append image data onto inrecrementally built output image
738 image_load_increment (gpointer data, guchar *buf, guint size)
740 PnmLoaderContext *context = (PnmLoaderContext *)data;
743 guchar *old_next_byte;
744 guint old_bytes_left;
746 guint num_left, spinguard;
749 g_return_val_if_fail (context != NULL, FALSE);
750 g_return_val_if_fail (buf != NULL, FALSE);
753 inbuf = &context->inbuf;
754 old_bytes_left = inbuf->bytes_left;
755 old_next_byte = inbuf->next_byte;
762 /* keep buffer as full as possible */
763 num_to_copy = MIN (PNM_BUF_SIZE - inbuf->bytes_left, num_left);
765 if (num_to_copy == 0)
771 if (inbuf->next_byte != NULL && inbuf->bytes_left > 0)
772 memmove (inbuf->buffer, inbuf->next_byte,
775 memcpy (inbuf->buffer + inbuf->bytes_left, bufhd, num_to_copy);
776 bufhd += num_to_copy;
777 inbuf->bytes_left += num_to_copy;
778 inbuf->next_byte = inbuf->buffer;
779 num_left -= num_to_copy;
781 /* ran out of data and we haven't exited main loop */
782 if (inbuf->bytes_left == 0)
785 /* get header if needed */
786 if (!context->got_header) {
787 rc = pnm_read_header (context);
788 if (rc == PNM_FATAL_ERR)
790 else if (rc == PNM_SUSPEND)
793 context->got_header = TRUE;
796 /* scan until we hit image data */
797 if (!context->did_prescan) {
798 if (skip_ahead_whitespace (inbuf) == NULL)
801 context->did_prescan = TRUE;
802 context->output_row = 0;
803 context->output_col = 0;
805 context->pixbuf = gdk_pixbuf_new(ART_PIX_RGB,
806 /*have_alpha*/ FALSE,
811 if (context->pixbuf == NULL) {
812 /* Failed to allocate memory */
813 g_error ("Couldn't allocate gdkpixbuf");
816 context->pixels = context->pixbuf->art_pixbuf->pixels;
817 context->rowstride = context->pixbuf->art_pixbuf->rowstride;
819 /* Notify the client that we are ready to go */
820 (* context->prepared_func) (context->pixbuf,
825 /* if we got here we're reading image data */
826 while (context->output_row < context->height) {
827 rc = pnm_read_scanline (context);
829 if (rc == PNM_SUSPEND) {
831 } else if (rc == PNM_FATAL_ERR) {
833 gdk_pixbuf_unref (context->pixbuf);
834 g_warning ("io-pnm.c: error reading rows..\n");
836 } else if (rc == PNM_OK) {
838 /* send updated signal */
839 (* context->updated_func) (context->pixbuf,
842 context->output_row-1,
848 if (context->output_row < context->height)