Blame gdk-pixbuf/io-pnm.c

Packit a4058c
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Packit a4058c
/* GdkPixbuf library - PNM image loader
Packit a4058c
 *
Packit a4058c
 * Copyright (C) 1999 Red Hat, Inc.
Packit a4058c
 *
Packit a4058c
 * Authors: Jeffrey Stedfast <fejj@helixcode.com>
Packit a4058c
 *          Michael Fulbright <drmike@redhat.com>
Packit a4058c
 *
Packit a4058c
 * This library is free software; you can redistribute it and/or
Packit a4058c
 * modify it under the terms of the GNU Library General Public
Packit a4058c
 * License as published by the Free Software Foundation; either
Packit a4058c
 * version 2 of the License, or (at your option) any later version.
Packit a4058c
 *
Packit a4058c
 * This library is distributed in the hope that it will be useful,
Packit a4058c
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4058c
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4058c
 * Library General Public License for more details.
Packit a4058c
 *
Packit a4058c
 * You should have received a copy of the GNU Library General Public
Packit a4058c
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit a4058c
 */
Packit a4058c
Packit a4058c
#include "config.h"
Packit a4058c
#include <stdio.h>
Packit a4058c
#include <stdlib.h>
Packit a4058c
#include <string.h>
Packit a4058c
#include <setjmp.h>
Packit a4058c
#include "gdk-pixbuf-private.h"
Packit a4058c
Packit a4058c
#define PNM_BUF_SIZE 4096
Packit a4058c
Packit a4058c
#define PNM_FATAL_ERR  -1
Packit a4058c
#define PNM_SUSPEND     0
Packit a4058c
#define PNM_OK          1
Packit a4058c
Packit a4058c
typedef enum {
Packit a4058c
	PNM_FORMAT_PGM = 1,
Packit a4058c
	PNM_FORMAT_PGM_RAW,
Packit a4058c
	PNM_FORMAT_PPM,
Packit a4058c
	PNM_FORMAT_PPM_RAW,
Packit a4058c
	PNM_FORMAT_PBM,
Packit a4058c
	PNM_FORMAT_PBM_RAW
Packit a4058c
} PnmFormat;
Packit a4058c
Packit a4058c
typedef struct {
Packit a4058c
	guchar buffer[PNM_BUF_SIZE];
Packit a4058c
	guchar *byte;
Packit a4058c
	guint nbytes;
Packit a4058c
} PnmIOBuffer;
Packit a4058c
Packit a4058c
typedef struct {
Packit a4058c
	GdkPixbufModuleUpdatedFunc updated_func;
Packit a4058c
	GdkPixbufModulePreparedFunc prepared_func;
Packit a4058c
	GdkPixbufModuleSizeFunc size_func;
Packit a4058c
	gpointer user_data;
Packit a4058c
	
Packit a4058c
	GdkPixbuf *pixbuf;
Packit a4058c
	guchar *pixels;        /* incoming pixel data buffer */
Packit a4058c
	guchar *dptr;          /* current position in pixbuf */
Packit a4058c
	
Packit a4058c
	PnmIOBuffer inbuf;
Packit a4058c
	
Packit a4058c
	guint width;
Packit a4058c
	guint height;
Packit a4058c
	guint maxval;
Packit a4058c
	guint rowstride;
Packit a4058c
	PnmFormat type;
Packit a4058c
	
Packit a4058c
	guint output_row;      /* last row to be completed */
Packit a4058c
	guint output_col;
Packit a4058c
	gboolean did_prescan;  /* are we in image data yet? */
Packit a4058c
	gboolean got_header;   /* have we loaded pnm header? */
Packit a4058c
	
Packit a4058c
	guint scan_state;
Packit a4058c
Packit a4058c
	GError **error;
Packit a4058c
	
Packit a4058c
} PnmLoaderContext;
Packit a4058c
Packit a4058c
static GdkPixbuf   *gdk_pixbuf__pnm_image_load          (FILE *f, GError **error);
Packit a4058c
static gpointer    gdk_pixbuf__pnm_image_begin_load     (GdkPixbufModuleSizeFunc size_func, 
Packit a4058c
                                                         GdkPixbufModulePreparedFunc func, 
Packit a4058c
							 GdkPixbufModuleUpdatedFunc func2,
Packit a4058c
							 gpointer user_data,
Packit a4058c
							 GError **error);
Packit a4058c
static gboolean    gdk_pixbuf__pnm_image_stop_load      (gpointer context, GError **error);
Packit a4058c
static gboolean    gdk_pixbuf__pnm_image_load_increment (gpointer context,
Packit a4058c
							 const guchar *buf, guint size,
Packit a4058c
							 GError **error);
Packit a4058c
Packit a4058c
static void explode_bitmap_into_buf              (PnmLoaderContext *context);
Packit a4058c
static void explode_gray_into_buf                (PnmLoaderContext *context);
Packit a4058c
Packit a4058c
Packit a4058c
/* explode bitmap data into rgb components         */
Packit a4058c
/* we need to know what the row so we can          */
Packit a4058c
/* do sub-byte expansion (since 1 byte = 8 pixels) */
Packit a4058c
/* context->dptr MUST point at first byte in incoming data  */
Packit a4058c
/* which corresponds to first pixel of row y       */
Packit a4058c
static void
Packit a4058c
explode_bitmap_into_buf (PnmLoaderContext *context)
Packit a4058c
{
Packit a4058c
	gint j;
Packit a4058c
	guchar *from, *to, data;
Packit a4058c
	gint bit;
Packit a4058c
	guchar *dptr;
Packit a4058c
	gint wid, x;
Packit a4058c
	
Packit a4058c
	g_return_if_fail (context != NULL);
Packit a4058c
	g_return_if_fail (context->dptr != NULL);
Packit a4058c
	
Packit a4058c
	/* I'm no clever bit-hacker so I'm sure this can be optimized */
Packit a4058c
	dptr = context->dptr;
Packit a4058c
	wid  = context->width;
Packit a4058c
	
Packit a4058c
	from = dptr + ((wid - 1) / 8);
Packit a4058c
	to   = dptr + (wid - 1) * 3;
Packit a4058c
	bit  = 7 - ((wid-1) % 8);
Packit a4058c
	
Packit a4058c
	/* get first byte and align properly */
Packit a4058c
	data = from[0];
Packit a4058c
	for (j = 0; j < bit; j++, data >>= 1);
Packit a4058c
	
Packit a4058c
	for (x = wid-1; x >= 0; x--) {
Packit a4058c
/*		g_print ("%c",  (data & 1) ? '*' : ' '); */
Packit a4058c
		
Packit a4058c
		to[0] = to[1] = to[2] = (data & 0x01) ? 0x00 : 0xff;
Packit a4058c
		
Packit a4058c
		to -= 3;
Packit a4058c
		bit++;
Packit a4058c
		
Packit a4058c
		if (bit > 7 && x > 0) {
Packit a4058c
			from--;
Packit a4058c
			data = from[0];
Packit a4058c
			bit = 0;
Packit a4058c
		} else {
Packit a4058c
			data >>= 1;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
/*	g_print ("\n"); */
Packit a4058c
}
Packit a4058c
Packit a4058c
/* explode gray image row into rgb components in pixbuf */
Packit a4058c
static void
Packit a4058c
explode_gray_into_buf (PnmLoaderContext *context)
Packit a4058c
{
Packit a4058c
	gint j;
Packit a4058c
	guchar *from, *to;
Packit a4058c
	guint w;
Packit a4058c
	
Packit a4058c
	g_return_if_fail (context != NULL);
Packit a4058c
	g_return_if_fail (context->dptr != NULL);
Packit a4058c
	
Packit a4058c
	/* Expand grey->colour.  Expand from the end of the
Packit a4058c
	 * memory down, so we can use the same buffer.
Packit a4058c
	 */
Packit a4058c
	w = context->width;
Packit a4058c
	from = context->dptr + w - 1;
Packit a4058c
	to = context->dptr + (w - 1) * 3;
Packit a4058c
	for (j = w - 1; j >= 0; j--) {
Packit a4058c
		to[0] = from[0];
Packit a4058c
		to[1] = from[0];
Packit a4058c
		to[2] = from[0];
Packit a4058c
		to -= 3;
Packit a4058c
		from--;
Packit a4058c
	}
Packit a4058c
}
Packit a4058c
Packit a4058c
/* skip over whitespace and comments in input buffer */
Packit a4058c
static gint
Packit a4058c
pnm_skip_whitespace (PnmIOBuffer *inbuf, GError **error)
Packit a4058c
{
Packit a4058c
	register guchar *inptr;
Packit a4058c
	guchar *inend;
Packit a4058c
	
Packit a4058c
	g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
Packit a4058c
	g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
Packit a4058c
	
Packit a4058c
	inend = inbuf->byte + inbuf->nbytes;
Packit a4058c
	inptr = inbuf->byte;
Packit a4058c
	
Packit a4058c
	for ( ; inptr < inend; inptr++) {
Packit a4058c
		if (*inptr == '#') {
Packit a4058c
			/* in comment - skip to the end of this line */
Packit a4058c
			for ( ; *inptr != '\n' && inptr < inend; inptr++)
Packit a4058c
				;
Packit a4058c
			
Packit a4058c
			if ( inptr == inend || *inptr != '\n' ) {
Packit a4058c
				/* couldn't read whole comment */
Packit a4058c
				return PNM_SUSPEND;
Packit a4058c
			}
Packit a4058c
			
Packit a4058c
		} else if (!g_ascii_isspace (*inptr)) {
Packit a4058c
			inbuf->byte = inptr;
Packit a4058c
			inbuf->nbytes = (guint) (inend - inptr);
Packit a4058c
			return PNM_OK;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	inbuf->byte = inptr;
Packit a4058c
	inbuf->nbytes = (guint) (inend - inptr);
Packit a4058c
	
Packit a4058c
	return PNM_SUSPEND;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* read next number from buffer */
Packit a4058c
static gint
Packit a4058c
pnm_read_next_value (PnmIOBuffer *inbuf, gint max_length, guint *value, GError **error)
Packit a4058c
{
Packit a4058c
	register guchar *inptr, *word, *p;
Packit a4058c
	guchar *inend, buf[129];
Packit a4058c
	gchar *endptr;
Packit a4058c
	gint retval;
Packit a4058c
	glong result;
Packit a4058c
	
Packit a4058c
	g_return_val_if_fail (inbuf != NULL, PNM_FATAL_ERR);
Packit a4058c
	g_return_val_if_fail (inbuf->byte != NULL, PNM_FATAL_ERR);
Packit a4058c
	g_return_val_if_fail (value != NULL, PNM_FATAL_ERR);
Packit a4058c
	
Packit a4058c
	if (max_length < 0)
Packit a4058c
		max_length = 128;
Packit a4058c
Packit a4058c
	/* skip white space */
Packit a4058c
	if ((retval = pnm_skip_whitespace (inbuf, error)) != PNM_OK)
Packit a4058c
		return retval;
Packit a4058c
	
Packit a4058c
	inend = inbuf->byte + inbuf->nbytes;
Packit a4058c
	inptr = inbuf->byte;
Packit a4058c
	
Packit a4058c
	/* copy this pnm 'word' into a temp buffer */
Packit a4058c
	for (p = inptr, word = buf; (p < inend) && !g_ascii_isspace (*p) && (*p != '#') && (p - inptr < max_length); p++, word++)
Packit a4058c
		*word = *p;
Packit a4058c
	*word = '\0';
Packit a4058c
	
Packit a4058c
	/* hmmm, there must be more data to this 'word' */
Packit a4058c
	if (p == inend || (!g_ascii_isspace (*p) && (*p != '#')  && (p - inptr < max_length)))
Packit a4058c
	    return PNM_SUSPEND;
Packit a4058c
	
Packit a4058c
	/* get the value */
Packit a4058c
	result = strtol ((gchar *)buf, &endptr, 10);
Packit a4058c
	if (*endptr != '\0' || result < 0 || result > G_MAXUINT) {
Packit a4058c
		g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("PNM loader expected to find an integer, but didn't"));
Packit a4058c
		return PNM_FATAL_ERR;
Packit a4058c
	}
Packit a4058c
	*value = result;
Packit a4058c
Packit a4058c
	inbuf->byte = p;
Packit a4058c
	inbuf->nbytes = (guint) (inend - p);
Packit a4058c
	
Packit a4058c
	return PNM_OK;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* returns PNM_OK, PNM_SUSPEND, or PNM_FATAL_ERR */
Packit a4058c
static gint
Packit a4058c
pnm_read_header (PnmLoaderContext *context)
Packit a4058c
{
Packit a4058c
	PnmIOBuffer *inbuf;
Packit a4058c
	gint retval;
Packit a4058c
	
Packit a4058c
	g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
Packit a4058c
	
Packit a4058c
	inbuf = &context->inbuf;
Packit a4058c
	
Packit a4058c
	if (!context->type) {
Packit a4058c
		/* file must start with a 'P' followed by a numeral  */
Packit a4058c
		/* so loop till we get enough data to determine type */
Packit a4058c
		if (inbuf->nbytes < 2)
Packit a4058c
			return PNM_SUSPEND;
Packit a4058c
		
Packit a4058c
		if (*inbuf->byte != 'P') {
Packit a4058c
			g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("PNM file has an incorrect initial byte"));
Packit a4058c
			return PNM_FATAL_ERR;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		inbuf->byte++;
Packit a4058c
		inbuf->nbytes--;
Packit a4058c
		
Packit a4058c
		switch (*inbuf->byte) {
Packit a4058c
		case '1':
Packit a4058c
			context->type = PNM_FORMAT_PBM;
Packit a4058c
			break;
Packit a4058c
		case '2':
Packit a4058c
			context->type = PNM_FORMAT_PGM;
Packit a4058c
			break;
Packit a4058c
		case '3':
Packit a4058c
			context->type = PNM_FORMAT_PPM;
Packit a4058c
			break;
Packit a4058c
		case '4':
Packit a4058c
			context->type = PNM_FORMAT_PBM_RAW;
Packit a4058c
			break;
Packit a4058c
		case '5':
Packit a4058c
			context->type = PNM_FORMAT_PGM_RAW;
Packit a4058c
			break;
Packit a4058c
		case '6':
Packit a4058c
			context->type = PNM_FORMAT_PPM_RAW;
Packit a4058c
			break;
Packit a4058c
		default:
Packit a4058c
			g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("PNM file is not in a recognized PNM subformat"));
Packit a4058c
			return PNM_FATAL_ERR;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		if (!inbuf->nbytes)
Packit a4058c
			return PNM_SUSPEND;
Packit a4058c
		
Packit a4058c
		inbuf->byte++;
Packit a4058c
		inbuf->nbytes--;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	if (!context->width) {
Packit a4058c
		/* read the pixmap width */
Packit a4058c
		guint width = 0;
Packit a4058c
		
Packit a4058c
		retval = pnm_read_next_value (inbuf, -1, &width,
Packit a4058c
					      context->error);
Packit a4058c
		
Packit a4058c
		if (retval != PNM_OK) 
Packit a4058c
			return retval;
Packit a4058c
Packit a4058c
		if (width > G_MAXINT) {
Packit a4058c
			g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("PNM file has an invalid width"));
Packit a4058c
			return PNM_FATAL_ERR;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		if (!width) {
Packit a4058c
			g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("PNM file has an image width of 0"));
Packit a4058c
			return PNM_FATAL_ERR;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		context->width = width;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	if (!context->height) {
Packit a4058c
		/* read the pixmap height */
Packit a4058c
		guint height = 0;
Packit a4058c
		
Packit a4058c
		retval = pnm_read_next_value (inbuf, -1, &height,
Packit a4058c
					      context->error);
Packit a4058c
		
Packit a4058c
		if (retval != PNM_OK)
Packit a4058c
			return retval;
Packit a4058c
Packit a4058c
		if (height > G_MAXINT) {
Packit a4058c
			g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("PNM file has an invalid height"));
Packit a4058c
			return PNM_FATAL_ERR;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		if (!height) {
Packit a4058c
			g_set_error_literal (context->error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("PNM file has an image height of 0"));
Packit a4058c
			return PNM_FATAL_ERR;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		context->height = height;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	switch (context->type) {
Packit a4058c
	case PNM_FORMAT_PPM:
Packit a4058c
	case PNM_FORMAT_PPM_RAW:
Packit a4058c
	case PNM_FORMAT_PGM:
Packit a4058c
	case PNM_FORMAT_PGM_RAW:
Packit a4058c
		if (!context->maxval) {
Packit a4058c
			retval = pnm_read_next_value (inbuf, -1, &context->maxval,
Packit a4058c
						      context->error);
Packit a4058c
			
Packit a4058c
			if (retval != PNM_OK)
Packit a4058c
				return retval;
Packit a4058c
			
Packit a4058c
			if (context->maxval == 0) {
Packit a4058c
				g_set_error_literal (context->error,
Packit a4058c
                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                                     _("Maximum color value in PNM file is 0"));
Packit a4058c
				return PNM_FATAL_ERR;
Packit a4058c
			}
Packit a4058c
Packit a4058c
			if (context->maxval > 65535) {
Packit a4058c
				g_set_error_literal (context->error,
Packit a4058c
                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                                     _("Maximum color value in PNM file is too large"));
Packit a4058c
				return PNM_FATAL_ERR;
Packit a4058c
			}
Packit a4058c
Packit a4058c
		}
Packit a4058c
		break;
Packit a4058c
	default:
Packit a4058c
		break;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	return PNM_OK;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
pnm_read_raw_scanline (PnmLoaderContext *context)
Packit a4058c
{
Packit a4058c
	PnmIOBuffer *inbuf;
Packit a4058c
	guint numbytes, offset;
Packit a4058c
	guint numpix = 0;
Packit a4058c
	guchar *dest;
Packit a4058c
	guint i;
Packit a4058c
	
Packit a4058c
	g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
Packit a4058c
	
Packit a4058c
	inbuf = &context->inbuf;
Packit a4058c
	
Packit a4058c
	switch (context->type) {
Packit a4058c
	case PNM_FORMAT_PBM_RAW:
Packit a4058c
		numpix = inbuf->nbytes * 8;
Packit a4058c
		break;
Packit a4058c
	case PNM_FORMAT_PGM_RAW:
Packit a4058c
		numpix = inbuf->nbytes;
Packit a4058c
		break;
Packit a4058c
	case PNM_FORMAT_PPM_RAW:
Packit a4058c
		numpix = inbuf->nbytes / 3;
Packit a4058c
		break;
Packit a4058c
	default:
Packit a4058c
		g_set_error_literal (context->error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("Raw PNM image type is invalid"));
Packit a4058c
		return PNM_FATAL_ERR;
Packit a4058c
	}
Packit a4058c
	if(context->maxval>255) 
Packit a4058c
		numpix/=2;
Packit a4058c
	
Packit a4058c
	numpix = MIN (numpix, context->width - context->output_col);
Packit a4058c
	
Packit a4058c
	if (!numpix)
Packit a4058c
		return PNM_SUSPEND;
Packit a4058c
	
Packit a4058c
	context->dptr = context->pixels + context->output_row * context->rowstride;
Packit a4058c
	
Packit a4058c
	switch (context->type) {
Packit a4058c
	case PNM_FORMAT_PBM_RAW:
Packit a4058c
		numbytes = (numpix / 8) + ((numpix % 8) ? 1 : 0);
Packit a4058c
		offset = context->output_col / 8;
Packit a4058c
		break;
Packit a4058c
	case PNM_FORMAT_PGM_RAW:
Packit a4058c
		numbytes = numpix;
Packit a4058c
		offset = context->output_col;
Packit a4058c
		break;
Packit a4058c
	case PNM_FORMAT_PPM_RAW:
Packit a4058c
		numbytes = numpix * 3;
Packit a4058c
		offset = context->output_col * 3;
Packit a4058c
		break;
Packit a4058c
	default:
Packit a4058c
		g_set_error_literal (context->error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("Raw PNM image type is invalid"));
Packit a4058c
		return PNM_FATAL_ERR;
Packit a4058c
	}
Packit a4058c
	if(context->maxval>255) 
Packit a4058c
		numbytes*=2;				
Packit a4058c
	
Packit a4058c
	switch (context->type) {
Packit a4058c
	case PNM_FORMAT_PBM_RAW:
Packit a4058c
		dest = context->dptr + offset;		
Packit a4058c
		memcpy (dest, inbuf->byte, numbytes);
Packit a4058c
		break;
Packit a4058c
	case PNM_FORMAT_PGM_RAW:
Packit a4058c
	case PNM_FORMAT_PPM_RAW:
Packit a4058c
		dest = context->dptr + offset;
Packit a4058c
		
Packit a4058c
		if (context->maxval == 255) {
Packit a4058c
			/* special-case optimization */
Packit a4058c
			memcpy (dest, inbuf->byte, numbytes);
Packit a4058c
		} else if(context->maxval == 65535) {
Packit a4058c
			/* optimized version of the next case */
Packit a4058c
			for(i=0; i < numbytes ; i+=2) {
Packit a4058c
				*dest++=inbuf->byte[i];
Packit a4058c
			}
Packit a4058c
		} else if(context->maxval > 255) {
Packit a4058c
			/* scale down to 256 colors */
Packit a4058c
			for(i=0; i < numbytes ; i+=2) {
Packit a4058c
				guint v=inbuf->byte[i]*256+inbuf->byte[i+1];
Packit a4058c
				*dest++=v*255/context->maxval;
Packit a4058c
			}
Packit a4058c
		} else {
Packit a4058c
			for (i = 0; i < numbytes; i++) {
Packit a4058c
				guchar *byte = inbuf->byte + i;
Packit a4058c
				
Packit a4058c
				/* scale the color to an 8-bit color depth */
Packit a4058c
				if (*byte > context->maxval)
Packit a4058c
					*dest++ = 255;
Packit a4058c
				else
Packit a4058c
					*dest++ = (guchar) (255 * *byte / context->maxval);
Packit a4058c
			}
Packit a4058c
		}
Packit a4058c
		break;
Packit a4058c
	default:
Packit a4058c
		g_set_error_literal (context->error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("Raw PNM image type is invalid"));
Packit a4058c
		return PNM_FATAL_ERR;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	inbuf->byte += numbytes;
Packit a4058c
	inbuf->nbytes -= numbytes;
Packit a4058c
	
Packit a4058c
	context->output_col += numpix;
Packit a4058c
	if (context->output_col == context->width) {
Packit a4058c
		if (context->type == PNM_FORMAT_PBM_RAW)
Packit a4058c
			explode_bitmap_into_buf (context);
Packit a4058c
		else if (context->type == PNM_FORMAT_PGM_RAW)
Packit a4058c
			explode_gray_into_buf (context);
Packit a4058c
		
Packit a4058c
		context->output_col = 0;
Packit a4058c
		context->output_row++;
Packit a4058c
	} else {
Packit a4058c
		return PNM_SUSPEND;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	return PNM_OK;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
pnm_read_ascii_mono_scanline (PnmLoaderContext *context)
Packit a4058c
{
Packit a4058c
	PnmIOBuffer *inbuf;
Packit a4058c
	guint value;
Packit a4058c
	gint retval;
Packit a4058c
	guchar *dptr;
Packit a4058c
	gint max_length;
Packit a4058c
Packit a4058c
	if (context->type == PNM_FORMAT_PBM)
Packit a4058c
		max_length = 1;
Packit a4058c
	else
Packit a4058c
		max_length = -1;
Packit a4058c
Packit a4058c
	inbuf = &context->inbuf;
Packit a4058c
Packit a4058c
	context->dptr = context->pixels + context->output_row * context->rowstride;
Packit a4058c
Packit a4058c
	dptr = context->dptr + context->output_col * 3;
Packit a4058c
Packit a4058c
	while (TRUE) {
Packit a4058c
		retval = pnm_read_next_value (inbuf, max_length, &value, context->error);
Packit a4058c
		if (retval != PNM_OK)
Packit a4058c
			return retval;
Packit a4058c
Packit a4058c
		if (context->type == PNM_FORMAT_PBM) {
Packit a4058c
			value = value ? 0 : 0xff;
Packit a4058c
		}
Packit a4058c
		else {
Packit a4058c
			/* scale the color up or down to an 8-bit color depth */
Packit a4058c
			if (value > context->maxval)
Packit a4058c
				value = 255;
Packit a4058c
			else
Packit a4058c
				value = (guchar)(255 * value / context->maxval);
Packit a4058c
		}
Packit a4058c
			
Packit a4058c
		*dptr++ = value;
Packit a4058c
		*dptr++ = value;
Packit a4058c
		*dptr++ = value;
Packit a4058c
Packit a4058c
		context->output_col++;
Packit a4058c
Packit a4058c
		if (context->output_col == context->width) {
Packit a4058c
			context->output_col = 0;
Packit a4058c
			context->output_row++;
Packit a4058c
			break;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
Packit a4058c
	return PNM_OK;
Packit a4058c
}
Packit a4058c
Packit a4058c
static gint
Packit a4058c
pnm_read_ascii_color_scanline (PnmLoaderContext *context)
Packit a4058c
{
Packit a4058c
	PnmIOBuffer *inbuf;
Packit a4058c
	guint value, i;
Packit a4058c
	guchar *dptr;
Packit a4058c
	gint retval;
Packit a4058c
	
Packit a4058c
	inbuf = &context->inbuf;
Packit a4058c
	
Packit a4058c
	context->dptr = context->pixels + context->output_row * context->rowstride;
Packit a4058c
	
Packit a4058c
	dptr = context->dptr + context->output_col * 3 + context->scan_state;
Packit a4058c
	
Packit a4058c
	while (TRUE) {
Packit a4058c
		for (i = context->scan_state; i < 3; i++) {
Packit a4058c
			retval = pnm_read_next_value (inbuf, -1, &value, context->error);
Packit a4058c
			if (retval != PNM_OK) {
Packit a4058c
				/* save state and return */
Packit a4058c
				context->scan_state = i;
Packit a4058c
				return retval;
Packit a4058c
			}
Packit a4058c
			
Packit a4058c
			if (value > context->maxval)
Packit a4058c
				*dptr++ = 255;
Packit a4058c
			else
Packit a4058c
				*dptr++ = (guchar)(255 * value / context->maxval);
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		context->scan_state = 0;
Packit a4058c
		context->output_col++;
Packit a4058c
		
Packit a4058c
		if (context->output_col == context->width) {
Packit a4058c
			context->output_col = 0;
Packit a4058c
			context->output_row++;
Packit a4058c
			break;
Packit a4058c
		}
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	return PNM_OK;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* returns 1 if a scanline was converted, 0 means we ran out of data */
Packit a4058c
static gint
Packit a4058c
pnm_read_scanline (PnmLoaderContext *context)
Packit a4058c
{
Packit a4058c
	gint retval;
Packit a4058c
	
Packit a4058c
	g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
Packit a4058c
	
Packit a4058c
	/* read in image data */
Packit a4058c
	/* for raw formats this is trivial */
Packit a4058c
	switch (context->type) {
Packit a4058c
	case PNM_FORMAT_PBM_RAW:
Packit a4058c
	case PNM_FORMAT_PGM_RAW:
Packit a4058c
	case PNM_FORMAT_PPM_RAW:
Packit a4058c
		retval = pnm_read_raw_scanline (context);
Packit a4058c
		if (retval != PNM_OK)
Packit a4058c
			return retval;
Packit a4058c
		break;
Packit a4058c
	case PNM_FORMAT_PBM:
Packit a4058c
	case PNM_FORMAT_PGM:
Packit a4058c
		retval = pnm_read_ascii_mono_scanline (context);
Packit a4058c
		if (retval != PNM_OK)
Packit a4058c
			return retval;
Packit a4058c
		break;		
Packit a4058c
	case PNM_FORMAT_PPM:
Packit a4058c
		retval = pnm_read_ascii_color_scanline (context);
Packit a4058c
		if (retval != PNM_OK)
Packit a4058c
			return retval;
Packit a4058c
		break;
Packit a4058c
	default:
Packit a4058c
		g_set_error_literal (context->error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
Packit a4058c
                                     _("PNM image loader does not support this PNM subformat"));
Packit a4058c
Packit a4058c
		return PNM_FATAL_ERR;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	return PNM_OK;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* Shared library entry point */
Packit a4058c
static GdkPixbuf *
Packit a4058c
gdk_pixbuf__pnm_image_load (FILE *f, GError **error)
Packit a4058c
{
Packit a4058c
	PnmLoaderContext context;
Packit a4058c
	PnmIOBuffer *inbuf;
Packit a4058c
	gint nbytes;
Packit a4058c
	gint retval;
Packit a4058c
	
Packit a4058c
	/* pretend to be doing progressive loading */
Packit a4058c
	context.updated_func = NULL;
Packit a4058c
	context.prepared_func = NULL;
Packit a4058c
	context.user_data = NULL;
Packit a4058c
	context.type = 0;
Packit a4058c
	context.inbuf.nbytes = 0;
Packit a4058c
	context.inbuf.byte = NULL;
Packit a4058c
	context.width = 0;
Packit a4058c
	context.height = 0;
Packit a4058c
	context.maxval = 0;
Packit a4058c
	context.pixels = NULL;
Packit a4058c
	context.pixbuf = NULL;
Packit a4058c
	context.got_header = FALSE;
Packit a4058c
	context.did_prescan = FALSE;
Packit a4058c
	context.scan_state = 0;
Packit a4058c
	context.error = error;
Packit a4058c
	
Packit a4058c
	inbuf = &context.inbuf;
Packit a4058c
	
Packit a4058c
	while (TRUE) {
Packit a4058c
		guint num_to_read;
Packit a4058c
		
Packit a4058c
		/* keep buffer as full as possible */
Packit a4058c
		num_to_read = PNM_BUF_SIZE - inbuf->nbytes;
Packit a4058c
		
Packit a4058c
		if (inbuf->byte != NULL && inbuf->nbytes > 0)
Packit a4058c
			memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
Packit a4058c
		
Packit a4058c
		nbytes = fread (inbuf->buffer + inbuf->nbytes, 1, num_to_read, f);
Packit a4058c
		
Packit a4058c
		/* error checking */
Packit a4058c
		if (nbytes == 0) {
Packit a4058c
			/* we ran out of data? */
Packit a4058c
			if (context.pixbuf)
Packit a4058c
				g_object_unref (context.pixbuf);
Packit a4058c
			g_set_error_literal (error,
Packit a4058c
                                             GDK_PIXBUF_ERROR,
Packit a4058c
                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                             _("Premature end-of-file encountered"));
Packit a4058c
			return NULL;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		inbuf->nbytes += nbytes;
Packit a4058c
		inbuf->byte = inbuf->buffer;
Packit a4058c
		
Packit a4058c
		/* get header if needed */
Packit a4058c
		if (!context.got_header) {
Packit a4058c
			retval = pnm_read_header (&context);
Packit a4058c
			if (retval == PNM_FATAL_ERR)
Packit a4058c
				return NULL;
Packit a4058c
			else if (retval == PNM_SUSPEND)
Packit a4058c
				continue;
Packit a4058c
			
Packit a4058c
			context.got_header = TRUE;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		/* scan until we hit image data */
Packit a4058c
		if (!context.did_prescan) {
Packit a4058c
			switch (context.type) {
Packit a4058c
			case PNM_FORMAT_PBM_RAW:
Packit a4058c
			case PNM_FORMAT_PGM_RAW:
Packit a4058c
			case PNM_FORMAT_PPM_RAW:
Packit a4058c
				if (inbuf->nbytes <= 0)
Packit a4058c
					continue;
Packit a4058c
				/* raw formats require exactly one whitespace */
Packit a4058c
				if (!g_ascii_isspace(*(inbuf->byte))) 
Packit a4058c
					{
Packit a4058c
						g_set_error_literal (error,
Packit a4058c
                                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                                                     _("Raw PNM formats require exactly one whitespace before sample data"));
Packit a4058c
						return NULL;
Packit a4058c
					}
Packit a4058c
				inbuf->nbytes--;
Packit a4058c
				inbuf->byte++;
Packit a4058c
				break;
Packit a4058c
			default:
Packit a4058c
				retval = pnm_skip_whitespace (inbuf,
Packit a4058c
							      context.error);
Packit a4058c
				if (retval == PNM_FATAL_ERR)
Packit a4058c
					return NULL;
Packit a4058c
				else if (retval == PNM_SUSPEND)
Packit a4058c
					continue;
Packit a4058c
				break;
Packit a4058c
			}
Packit a4058c
			context.did_prescan = TRUE;
Packit a4058c
			context.output_row = 0;
Packit a4058c
			context.output_col = 0;
Packit a4058c
			
Packit a4058c
			context.pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
Packit a4058c
							 context.width, context.height);
Packit a4058c
			
Packit a4058c
			if (!context.pixbuf) {
Packit a4058c
				/* Failed to allocate memory */
Packit a4058c
				g_set_error_literal (error,
Packit a4058c
                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                                     _("Cannot allocate memory for loading PNM image"));
Packit a4058c
				return NULL;
Packit a4058c
			}
Packit a4058c
Packit a4058c
			context.rowstride = context.pixbuf->rowstride;
Packit a4058c
			context.pixels = context.pixbuf->pixels;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		/* if we got here we're reading image data */
Packit a4058c
		while (context.output_row < context.height) {
Packit a4058c
			retval = pnm_read_scanline (&context);
Packit a4058c
			
Packit a4058c
			if (retval == PNM_SUSPEND) {
Packit a4058c
				break;
Packit a4058c
			} else if (retval == PNM_FATAL_ERR) {
Packit a4058c
				if (context.pixbuf)
Packit a4058c
					g_object_unref (context.pixbuf);
Packit a4058c
Packit a4058c
				return NULL;
Packit a4058c
			}
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		if (context.output_row < context.height)
Packit a4058c
			continue;
Packit a4058c
		else
Packit a4058c
			break;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	return context.pixbuf;
Packit a4058c
}
Packit a4058c
Packit a4058c
/* 
Packit a4058c
 * func - called when we have pixmap created (but no image data)
Packit a4058c
 * user_data - passed as arg 1 to func
Packit a4058c
 * return context (opaque to user)
Packit a4058c
 */
Packit a4058c
Packit a4058c
static gpointer
Packit a4058c
gdk_pixbuf__pnm_image_begin_load (GdkPixbufModuleSizeFunc size_func, 
Packit a4058c
                                  GdkPixbufModulePreparedFunc prepared_func, 
Packit a4058c
				  GdkPixbufModuleUpdatedFunc  updated_func,
Packit a4058c
				  gpointer user_data,
Packit a4058c
				  GError **error)
Packit a4058c
{
Packit a4058c
	PnmLoaderContext *context;
Packit a4058c
	
Packit a4058c
	context = g_try_malloc (sizeof (PnmLoaderContext));
Packit a4058c
	if (!context) {
Packit a4058c
		g_set_error_literal (error, GDK_PIXBUF_ERROR, 
Packit a4058c
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                     _("Insufficient memory to load PNM context struct"));
Packit a4058c
		return NULL;
Packit a4058c
	}
Packit a4058c
	memset (context, 0, sizeof (PnmLoaderContext));
Packit a4058c
	context->size_func = size_func;
Packit a4058c
	context->prepared_func = prepared_func;
Packit a4058c
	context->updated_func  = updated_func;
Packit a4058c
	context->user_data = user_data;
Packit a4058c
	context->width = 0;
Packit a4058c
	context->height = 0;
Packit a4058c
	context->maxval = 0;
Packit a4058c
	context->pixbuf = NULL;
Packit a4058c
	context->pixels = NULL;
Packit a4058c
	context->got_header = FALSE;
Packit a4058c
	context->did_prescan = FALSE;
Packit a4058c
	context->scan_state = 0;
Packit a4058c
	
Packit a4058c
	context->inbuf.nbytes = 0;
Packit a4058c
	context->inbuf.byte  = NULL;
Packit a4058c
Packit a4058c
	context->error = error;
Packit a4058c
	
Packit a4058c
	return (gpointer) context;
Packit a4058c
}
Packit a4058c
Packit a4058c
/*
Packit a4058c
 * context - returned from image_begin_load
Packit a4058c
 *
Packit a4058c
 * free context, unref gdk_pixbuf
Packit a4058c
 */
Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__pnm_image_stop_load (gpointer data,
Packit a4058c
				 GError **error)
Packit a4058c
{
Packit a4058c
	PnmLoaderContext *context = (PnmLoaderContext *) data;
Packit a4058c
	gboolean retval = TRUE;
Packit a4058c
	
Packit a4058c
	g_return_val_if_fail (context != NULL, TRUE);
Packit a4058c
	
Packit a4058c
	if (context->pixbuf)
Packit a4058c
		g_object_unref (context->pixbuf);
Packit a4058c
Packit a4058c
#if 0
Packit a4058c
	/* We should ignore trailing newlines and we can't
Packit a4058c
	   generally complain about trailing stuff at all, since 
Packit a4058c
	   pnm allows to put multiple images in a file
Packit a4058c
	*/
Packit a4058c
	if (context->inbuf.nbytes > 0) {
Packit a4058c
		g_set_error_literal (error,
Packit a4058c
                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                     _("Unexpected end of PNM image data"));
Packit a4058c
		retval = FALSE;
Packit a4058c
	}
Packit a4058c
#endif
Packit a4058c
	
Packit a4058c
	g_free (context);
Packit a4058c
Packit a4058c
	return retval;
Packit a4058c
}
Packit a4058c
Packit a4058c
/*
Packit a4058c
 * context - from image_begin_load
Packit a4058c
 * buf - new image data
Packit a4058c
 * size - length of new image data
Packit a4058c
 *
Packit a4058c
 * append image data onto inrecrementally built output image
Packit a4058c
 */
Packit a4058c
static gboolean
Packit a4058c
gdk_pixbuf__pnm_image_load_increment (gpointer data,
Packit a4058c
				      const guchar *buf, guint size,
Packit a4058c
				      GError **error)
Packit a4058c
{
Packit a4058c
	PnmLoaderContext *context = (PnmLoaderContext *)data;
Packit a4058c
	PnmIOBuffer *inbuf;
Packit a4058c
	const guchar *bufhd;
Packit a4058c
	guint num_left, spinguard;
Packit a4058c
	gint retval;
Packit a4058c
	
Packit a4058c
	g_return_val_if_fail (context != NULL, FALSE);
Packit a4058c
	g_return_val_if_fail (buf != NULL, FALSE);
Packit a4058c
Packit a4058c
	context->error = error;
Packit a4058c
	
Packit a4058c
	bufhd = buf;
Packit a4058c
	inbuf = &context->inbuf;
Packit a4058c
	
Packit a4058c
	num_left = size;
Packit a4058c
	spinguard = 0;
Packit a4058c
	while (TRUE) {
Packit a4058c
		guint num_to_copy;
Packit a4058c
		
Packit a4058c
		/* keep buffer as full as possible */
Packit a4058c
		num_to_copy = MIN (PNM_BUF_SIZE - inbuf->nbytes, num_left);
Packit a4058c
		
Packit a4058c
		if (num_to_copy == 0)
Packit a4058c
			spinguard++;
Packit a4058c
		
Packit a4058c
		if (spinguard > 1)
Packit a4058c
			return TRUE;
Packit a4058c
		
Packit a4058c
		if (inbuf->byte != NULL && inbuf->nbytes > 0)
Packit a4058c
			memmove (inbuf->buffer, inbuf->byte, inbuf->nbytes);
Packit a4058c
		
Packit a4058c
		memcpy (inbuf->buffer + inbuf->nbytes, bufhd, num_to_copy);
Packit a4058c
		bufhd += num_to_copy;
Packit a4058c
		inbuf->nbytes += num_to_copy;
Packit a4058c
		inbuf->byte = inbuf->buffer;
Packit a4058c
		num_left -= num_to_copy;
Packit a4058c
		
Packit a4058c
		/* ran out of data and we haven't exited main loop */
Packit a4058c
		if (inbuf->nbytes == 0)
Packit a4058c
			return TRUE;
Packit a4058c
		
Packit a4058c
		/* get header if needed */
Packit a4058c
		if (!context->got_header) {
Packit a4058c
			retval = pnm_read_header (context);
Packit a4058c
			
Packit a4058c
			if (retval == PNM_FATAL_ERR)
Packit a4058c
				return FALSE;
Packit a4058c
			else if (retval == PNM_SUSPEND)
Packit a4058c
				continue;
Packit a4058c
			
Packit a4058c
			context->got_header = TRUE;
Packit a4058c
		}
Packit a4058c
Packit a4058c
		if (context->size_func) {
Packit a4058c
			gint w = context->width;
Packit a4058c
			gint h = context->height;
Packit a4058c
			(*context->size_func) (&w, &h, context->user_data);
Packit a4058c
			
Packit a4058c
			if (w == 0 || h == 0) 
Packit a4058c
				return FALSE;
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		
Packit a4058c
		/* scan until we hit image data */
Packit a4058c
		if (!context->did_prescan) {
Packit a4058c
			switch (context->type) {
Packit a4058c
			case PNM_FORMAT_PBM_RAW:
Packit a4058c
			case PNM_FORMAT_PGM_RAW:
Packit a4058c
			case PNM_FORMAT_PPM_RAW:
Packit a4058c
				if (inbuf->nbytes <= 0)
Packit a4058c
					continue;
Packit a4058c
				/* raw formats require exactly one whitespace */
Packit a4058c
				if (!g_ascii_isspace(*(inbuf->byte)))
Packit a4058c
					{
Packit a4058c
						g_set_error_literal (error,
Packit a4058c
                                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Packit a4058c
                                                                     _("Raw PNM formats require exactly one whitespace before sample data"));
Packit a4058c
						return FALSE;
Packit a4058c
					}
Packit a4058c
				inbuf->nbytes--;
Packit a4058c
				inbuf->byte++;
Packit a4058c
				break;
Packit a4058c
			default:
Packit a4058c
				retval = pnm_skip_whitespace (inbuf,
Packit a4058c
							      context->error);
Packit a4058c
				if (retval == PNM_FATAL_ERR)
Packit a4058c
					return FALSE;
Packit a4058c
				else if (retval == PNM_SUSPEND)
Packit a4058c
					continue;
Packit a4058c
				break;
Packit a4058c
			}
Packit a4058c
			context->did_prescan = TRUE;
Packit a4058c
			context->output_row = 0;
Packit a4058c
			context->output_col = 0;
Packit a4058c
			
Packit a4058c
			context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 
Packit a4058c
							  FALSE,
Packit a4058c
							  8, 
Packit a4058c
							  context->width,
Packit a4058c
							  context->height);
Packit a4058c
			
Packit a4058c
			if (context->pixbuf == NULL) {
Packit a4058c
				g_set_error_literal (error,
Packit a4058c
                                                     GDK_PIXBUF_ERROR,
Packit a4058c
                                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Packit a4058c
                                                     _("Insufficient memory to load PNM file"));
Packit a4058c
				return FALSE;
Packit a4058c
			}
Packit a4058c
			
Packit a4058c
			context->pixels = context->pixbuf->pixels;
Packit a4058c
			context->rowstride = context->pixbuf->rowstride;
Packit a4058c
			
Packit a4058c
			/* Notify the client that we are ready to go */
Packit a4058c
			if (context->prepared_func)
Packit a4058c
				(* context->prepared_func) (context->pixbuf,
Packit a4058c
							    NULL,
Packit a4058c
							    context->user_data);
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		/* if we got here we're reading image data */
Packit a4058c
		while (context->output_row < context->height) {
Packit a4058c
			retval = pnm_read_scanline (context);
Packit a4058c
			
Packit a4058c
			if (retval == PNM_SUSPEND) {
Packit a4058c
				break;
Packit a4058c
			} else if (retval == PNM_FATAL_ERR) {
Packit a4058c
				return FALSE;
Packit a4058c
			} else if (retval == PNM_OK && context->updated_func) {	
Packit a4058c
				/* send updated signal */
Packit a4058c
				(* context->updated_func) (context->pixbuf,
Packit a4058c
							   0, 
Packit a4058c
							   context->output_row-1,
Packit a4058c
							   context->width, 
Packit a4058c
							   1,
Packit a4058c
							   context->user_data);
Packit a4058c
			}
Packit a4058c
		}
Packit a4058c
		
Packit a4058c
		if (context->output_row < context->height)
Packit a4058c
			continue;
Packit a4058c
		else
Packit a4058c
			break;
Packit a4058c
	}
Packit a4058c
	
Packit a4058c
	return TRUE;
Packit a4058c
}
Packit a4058c
Packit a4058c
#ifndef INCLUDE_pnm
Packit a4058c
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
Packit a4058c
#else
Packit a4058c
#define MODULE_ENTRY(function) void _gdk_pixbuf__pnm_ ## function
Packit a4058c
#endif
Packit a4058c
Packit a4058c
MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
Packit a4058c
{
Packit a4058c
	module->load = gdk_pixbuf__pnm_image_load;
Packit a4058c
	module->begin_load = gdk_pixbuf__pnm_image_begin_load;
Packit a4058c
	module->stop_load = gdk_pixbuf__pnm_image_stop_load;
Packit a4058c
	module->load_increment = gdk_pixbuf__pnm_image_load_increment;
Packit a4058c
}
Packit a4058c
Packit a4058c
MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
Packit a4058c
{
Packit a4058c
	static const GdkPixbufModulePattern signature[] = {
Packit a4058c
		{ "P1", NULL, 100 },
Packit a4058c
		{ "P2", NULL, 100 },
Packit a4058c
		{ "P3", NULL, 100 },
Packit a4058c
		{ "P4", NULL, 100 },
Packit a4058c
		{ "P5", NULL, 100 },
Packit a4058c
		{ "P6", NULL, 100 },
Packit a4058c
		{ NULL, NULL, 0 }
Packit a4058c
	};
Packit a4058c
	static const gchar *mime_types[] = {
Packit a4058c
		"image/x-portable-anymap",
Packit a4058c
		"image/x-portable-bitmap",
Packit a4058c
		"image/x-portable-graymap",
Packit a4058c
		"image/x-portable-pixmap",
Packit a4058c
		NULL
Packit a4058c
	};
Packit a4058c
	static const gchar *extensions[] = {
Packit a4058c
		"pnm",
Packit a4058c
		"pbm",
Packit a4058c
		"pgm",
Packit a4058c
		"ppm",
Packit a4058c
		NULL
Packit a4058c
	};
Packit a4058c
Packit a4058c
	info->name = "pnm";
Packit a4058c
	info->signature = (GdkPixbufModulePattern *) signature;
Packit a4058c
	info->description = NC_("image format", "PNM/PBM/PGM/PPM");
Packit a4058c
	info->mime_types = (gchar **) mime_types;
Packit a4058c
	info->extensions = (gchar **) extensions;
Packit a4058c
	info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
Packit a4058c
	info->license = "LGPL";
Packit a4058c
}