summaryrefslogtreecommitdiff
path: root/gdk-pixbuf
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2006-09-06 05:11:20 +0000
committerMatthias Clasen <matthiasc@src.gnome.org>2006-09-06 05:11:20 +0000
commitf3b20fedf99ff668384311f16dbd3c4222278524 (patch)
tree813b5dfe576c65863e46ed783400d8e57258af70 /gdk-pixbuf
parentdd0311dc2f7ef1816289159f21dca30744983ba4 (diff)
downloadgtk+-f3b20fedf99ff668384311f16dbd3c4222278524.tar.gz
Simplify and fix reading of ASCII images.
2006-09-06 Matthias Clasen <mclasen@redhat.com> * io-pnm.c: Simplify and fix reading of ASCII images.
Diffstat (limited to 'gdk-pixbuf')
-rw-r--r--gdk-pixbuf/ChangeLog4
-rw-r--r--gdk-pixbuf/io-pnm.c149
2 files changed, 71 insertions, 82 deletions
diff --git a/gdk-pixbuf/ChangeLog b/gdk-pixbuf/ChangeLog
index 58029e44fe..b72ed39b2f 100644
--- a/gdk-pixbuf/ChangeLog
+++ b/gdk-pixbuf/ChangeLog
@@ -1,3 +1,7 @@
+2006-09-06 Matthias Clasen <mclasen@redhat.com>
+
+ * io-pnm.c: Simplify and fix reading of ASCII images.
+
2006-08-17 Matthias Clasen <mclasen@redhat.com>
* === Released 2.10.2 ===
diff --git a/gdk-pixbuf/io-pnm.c b/gdk-pixbuf/io-pnm.c
index 3af9794c25..af40f0fe25 100644
--- a/gdk-pixbuf/io-pnm.c
+++ b/gdk-pixbuf/io-pnm.c
@@ -527,109 +527,90 @@ pnm_read_raw_scanline (PnmLoaderContext *context)
}
static gint
-pnm_read_ascii_scanline (PnmLoaderContext *context)
+pnm_read_ascii_mono_scanline (PnmLoaderContext *context)
{
PnmIOBuffer *inbuf;
- guint offset;
- guint value, numval, i;
- guchar data;
- guchar mask;
- guchar *dptr;
+ guint value;
gint retval;
+ guchar *dptr;
gint max_length;
-
- g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
-
- data = mask = 0;
-
- inbuf = &context->inbuf;
-
- context->dptr = context->pixels + context->output_row * context->rowstride;
-
- switch (context->type) {
- case PNM_FORMAT_PBM:
+
+ if (context->type == PNM_TYPE_PBM)
max_length = 1;
- numval = MIN (8, context->width - context->output_col);
- offset = context->output_col / 8;
- break;
- case PNM_FORMAT_PGM:
- max_length = -1;
- numval = 1;
- offset = context->output_col;
- break;
- case PNM_FORMAT_PPM:
+ else
max_length = -1;
- numval = 3;
- offset = context->output_col * 3;
- break;
-
- default:
- g_set_error (context->error,
- GDK_PIXBUF_ERROR,
- GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
- _("PNM image format is invalid"));
- return PNM_FATAL_ERR;
+ inbuf = &context->inbuf;
+
+ context->dptr = context->pixels + context->output_row * context->rowstride;
+
+ dptr = context->dptr + context->output_col * 3;
+
+ while (TRUE) {
+ retval = pnm_read_next_value (inbuf, max_length, &value, context->error);
+ if (retval != PNM_OK)
+ return retval;
+
+ if (context->type == PNM_TYPE_PBM) {
+ value = value ? 0 : 0xff;
+ }
+ else {
+ /* scale the color up or down to an 8-bit color depth */
+ if (value > context->maxval)
+ value = 255;
+ else
+ value = (guchar)(255 * value / context->maxval);
+ }
+
+ *dptr++ = value;
+ *dptr++ = value;
+ *dptr++ = value;
+
+ context->output_col++;
+
+ if (context->output_col == context->width) {
+ context->output_col = 0;
+ context->output_row++;
+ break;
+ }
}
+
+ return PNM_OK;
+}
+
+static gint
+pnm_read_ascii_color_scanline (PnmLoaderContext *context)
+{
+ PnmIOBuffer *inbuf;
+ guint value, i;
+ guchar *dptr;
+ gint retval;
+
+ inbuf = &context->inbuf;
+
+ context->dptr = context->pixels + context->output_row * context->rowstride;
- dptr = context->dptr + offset + context->scan_state;
+ dptr = context->dptr + context->output_col * 3 + context->scan_state;
while (TRUE) {
- if (context->type == PNM_FORMAT_PBM) {
- mask = 0x80;
- data = 0;
- numval = MIN (8, context->width - context->output_col);
- }
-
- for (i = context->scan_state; i < numval; i++) {
- retval = pnm_read_next_value (inbuf, max_length,
- &value, context->error);
+ for (i = context->scan_state; i < 3; i++) {
+ retval = pnm_read_next_value (inbuf, -1, &value, context->error);
if (retval != PNM_OK) {
/* save state and return */
context->scan_state = i;
return retval;
}
- switch (context->type) {
- case PNM_FORMAT_PBM:
- if (value)
- data |= mask;
- mask >>= 1;
-
- break;
- case PNM_FORMAT_PGM:
- case PNM_FORMAT_PPM:
- /* scale the color up or down to an 8-bit color depth */
- if (value > context->maxval)
- *dptr++ = 255;
- else
- *dptr++ = (guchar)(255 * value / context->maxval);
- break;
- default:
- g_set_error (context->error,
- GDK_PIXBUF_ERROR,
- GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
- _("PNM image format is invalid"));
- return PNM_FATAL_ERR;
- break;
- }
+ if (value > context->maxval)
+ *dptr++ = 255;
+ else
+ *dptr++ = (guchar)(255 * value / context->maxval);
}
context->scan_state = 0;
-
- if (context->type == PNM_FORMAT_PBM) {
- *dptr++ = data;
- context->output_col += numval;
- } else {
- context->output_col++;
- }
+ context->output_col++;
if (context->output_col == context->width) {
- if (context->type == PNM_FORMAT_PBM)
- explode_bitmap_into_buf (context);
- else if (context->type == PNM_FORMAT_PGM)
- explode_gray_into_buf (context);
-
context->output_col = 0;
context->output_row++;
break;
@@ -659,8 +640,12 @@ pnm_read_scanline (PnmLoaderContext *context)
break;
case PNM_FORMAT_PBM:
case PNM_FORMAT_PGM:
+ retval = pnm_read_ascii_mono_scanline (context);
+ if (retval != PNM_OK)
+ return retval;
+ break;
case PNM_FORMAT_PPM:
- retval = pnm_read_ascii_scanline (context);
+ retval = pnm_read_ascii_color_scanline (context);
if (retval != PNM_OK)
return retval;
break;