mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-09-30 02:57:38 +00:00
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:
parent
dd0311dc2f
commit
f3b20fedf9
@ -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 ===
|
||||
|
@ -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;
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
while (TRUE) {
|
||||
if (context->type == PNM_FORMAT_PBM) {
|
||||
mask = 0x80;
|
||||
data = 0;
|
||||
numval = MIN (8, context->width - context->output_col);
|
||||
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);
|
||||
}
|
||||
|
||||
for (i = context->scan_state; i < numval; i++) {
|
||||
retval = pnm_read_next_value (inbuf, max_length,
|
||||
&value, context->error);
|
||||
*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 + context->output_col * 3 + context->scan_state;
|
||||
|
||||
while (TRUE) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
context->scan_state = 0;
|
||||
|
||||
if (context->type == PNM_FORMAT_PBM) {
|
||||
*dptr++ = data;
|
||||
context->output_col += numval;
|
||||
} else {
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user