summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/io-tga.c
diff options
context:
space:
mode:
authorBenjamin Otte <otte@redhat.com>2015-09-20 18:54:30 +0200
committerBenjamin Otte <otte@redhat.com>2015-10-13 02:56:47 +0200
commit97f6199b234fc1314595ad9467aa7c4a340cc142 (patch)
treebc40138847d49f78d6052d776e730d265e66541c /gdk-pixbuf/io-tga.c
parent16d8b34905b8ea788da733fed57fd39d3e043242 (diff)
downloadgdk-pixbuf-97f6199b234fc1314595ad9467aa7c4a340cc142.tar.gz
tga: Add tga_read_pixel() function and use it.
Diffstat (limited to 'gdk-pixbuf/io-tga.c')
-rw-r--r--gdk-pixbuf/io-tga.c84
1 files changed, 57 insertions, 27 deletions
diff --git a/gdk-pixbuf/io-tga.c b/gdk-pixbuf/io-tga.c
index b4ab70b73..0ee1a5bc7 100644
--- a/gdk-pixbuf/io-tga.c
+++ b/gdk-pixbuf/io-tga.c
@@ -261,6 +261,43 @@ tga_format_supported (guint type,
}
}
+static inline void
+tga_read_pixel (TGAContext *ctx,
+ const guchar *data,
+ TGAColor *color)
+{
+ switch (ctx->hdr->type)
+ {
+ case TGA_TYPE_PSEUDOCOLOR:
+ case TGA_TYPE_RLE_PSEUDOCOLOR:
+ *color = *colormap_get_color (ctx->cmap, data[0]);
+ break;
+
+ case TGA_TYPE_TRUECOLOR:
+ case TGA_TYPE_RLE_TRUECOLOR:
+ color->b = data[0];
+ color->g = data[1];
+ color->r = data[2];
+ if (ctx->hdr->bpp == 32)
+ color->a = data[3];
+ else
+ color->a = 255;
+ break;
+
+ case TGA_TYPE_GRAYSCALE:
+ case TGA_TYPE_RLE_GRAYSCALE:
+ color->r = color->g = color->b = data[0];
+ if (ctx->hdr->bpp == 16)
+ color->a = data[1];
+ else
+ color->a = 255;
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+}
+
static gboolean fill_in_context(TGAContext *ctx, GError **err)
{
gboolean alpha;
@@ -307,6 +344,7 @@ static gboolean fill_in_context(TGAContext *ctx, GError **err)
static gboolean
parse_data_pseudocolor (TGAContext *ctx)
{
+ TGAColor color;
GBytes *bytes;
gsize i;
const guchar *data;
@@ -319,7 +357,9 @@ parse_data_pseudocolor (TGAContext *ctx)
for (i = 0; i < ctx->pbuf->width; i++)
{
- tga_write_pixel (ctx, colormap_get_color (ctx->cmap, data[i]));
+ tga_read_pixel (ctx, data, &color);
+ tga_write_pixel (ctx, &color);
+ data++;
}
g_bytes_unref (bytes);
@@ -338,17 +378,11 @@ parse_data_truecolor (TGAContext *ctx)
if (bytes == NULL)
return FALSE;
- color.a = 255;
data = g_bytes_get_data (bytes, NULL);
for (i = 0; i < ctx->pbuf->width; i++)
{
- color.r = data[2];
- color.g = data[1];
- color.b = data[0];
- if (ctx->pbuf->n_channels == 4)
- color.a = data[3];
-
+ tga_read_pixel (ctx, data, &color);
tga_write_pixel (ctx, &color);
data += ctx->pbuf->n_channels;
}
@@ -378,10 +412,9 @@ parse_data_grayscale (TGAContext *ctx)
for (i = 0; i < ctx->pbuf->width; i++)
{
- color.r = color.g = color.b = *s++;
- if (has_alpha)
- color.a = *s++;
+ tga_read_pixel (ctx, s, &color);
tga_write_pixel (ctx, &color);
+ s += has_alpha ? 2 : 1;
}
g_bytes_unref (bytes);
@@ -429,6 +462,7 @@ static void write_rle_data(TGAContext *ctx, const TGAColor *color, guint *rle_co
static void
parse_rle_data_pseudocolor (TGAContext *ctx)
{
+ TGAColor color;
GBytes *bytes;
guint rle_num, raw_num;
const guchar *s;
@@ -447,7 +481,8 @@ parse_rle_data_pseudocolor (TGAContext *ctx)
break;
} else {
rle_num = (tag & 0x7f) + 1;
- write_rle_data(ctx, colormap_get_color (ctx->cmap, *s), &rle_num);
+ tga_read_pixel (ctx, s, &color);
+ write_rle_data(ctx, &color, &rle_num);
s++, n++;
if (tga_all_pixels_written (ctx))
break;
@@ -459,7 +494,8 @@ parse_rle_data_pseudocolor (TGAContext *ctx)
break;
} else {
for (; raw_num; raw_num--) {
- tga_write_pixel (ctx, colormap_get_color (ctx->cmap, *s));
+ tga_read_pixel (ctx, s, &color);
+ tga_write_pixel (ctx, &color);
s++, n++;
if (tga_all_pixels_written (ctx))
break;
@@ -498,11 +534,8 @@ parse_rle_data_truecolor (TGAContext *ctx)
break;
} else {
rle_num = (tag & 0x7f) + 1;
- col.b = *s++;
- col.g = *s++;
- col.r = *s++;
- if (ctx->hdr->bpp == 32)
- col.a = *s++;
+ tga_read_pixel (ctx, s, &col);
+ s += ctx->pbuf->n_channels;
n += ctx->pbuf->n_channels;
write_rle_data(ctx, &col, &rle_num);
if (tga_all_pixels_written (ctx))
@@ -515,11 +548,8 @@ parse_rle_data_truecolor (TGAContext *ctx)
break;
} else {
for (; raw_num; raw_num--) {
- col.b = *s++;
- col.g = *s++;
- col.r = *s++;
- if (ctx->hdr->bpp == 32)
- col.a = *s++;
+ tga_read_pixel (ctx, s, &col);
+ s += ctx->pbuf->n_channels;
n += ctx->pbuf->n_channels;
tga_write_pixel (ctx, &col);
if (tga_all_pixels_written (ctx))
@@ -561,10 +591,10 @@ parse_rle_data_grayscale (TGAContext *ctx)
break;
} else {
rle_num = (tag & 0x7f) + 1;
- tone.r = tone.g = tone.b = *s;
+ tga_read_pixel (ctx, s, &tone);
s++, n++;
if (ctx->pbuf->n_channels == 4) {
- tone.a = *s++;
+ s++;
n++;
}
write_rle_data(ctx, &tone, &rle_num);
@@ -578,10 +608,10 @@ parse_rle_data_grayscale (TGAContext *ctx)
break;
} else {
for (; raw_num; raw_num--) {
- tone.r = tone.g = tone.b = *s;
+ tga_read_pixel (ctx, s, &tone);
s++, n++;
if (ctx->pbuf->n_channels == 4) {
- tone.a = *s++;
+ s++;
n++;
}
tga_write_pixel (ctx, &tone);