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.
This commit is contained in:
Matthias Clasen 2006-09-06 05:11:20 +00:00 committed by Matthias Clasen
parent dd0311dc2f
commit f3b20fedf9
2 changed files with 71 additions and 82 deletions

View File

@ -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 ===

View File

@ -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;
guint value;
gint retval;
guchar *dptr;
gint max_length;
if (context->type == PNM_TYPE_PBM)
max_length = 1;
else
max_length = -1;
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;
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:
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:
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;
}
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;