diff --git a/plug-ins/common/file-tga.c b/plug-ins/common/file-tga.c index 7b11baa465..8c67eed3de 100644 --- a/plug-ins/common/file-tga.c +++ b/plug-ins/common/file-tga.c @@ -864,7 +864,8 @@ apply_colormap (guchar *dest, const guchar *src, guint width, const guchar *cmap, - gboolean alpha) + gboolean alpha, + guint16 index) { guint x; @@ -872,10 +873,10 @@ apply_colormap (guchar *dest, { for (x = 0; x < width; x++) { - *(dest++) = cmap[*src * 4]; - *(dest++) = cmap[*src * 4 + 1]; - *(dest++) = cmap[*src * 4 + 2]; - *(dest++) = cmap[*src * 4 + 3]; + *(dest++) = cmap[(*src - index) * 4]; + *(dest++) = cmap[(*src - index) * 4 + 1]; + *(dest++) = cmap[(*src - index) * 4 + 2]; + *(dest++) = cmap[(*src - index) * 4 + 3]; src++; } @@ -884,15 +885,29 @@ apply_colormap (guchar *dest, { for (x = 0; x < width; x++) { - *(dest++) = cmap[*src * 3]; - *(dest++) = cmap[*src * 3 + 1]; - *(dest++) = cmap[*src * 3 + 2]; + *(dest++) = cmap[(*src - index) * 3]; + *(dest++) = cmap[(*src - index) * 3 + 1]; + *(dest++) = cmap[(*src - index) * 3 + 2]; src++; } } } +static void +apply_index (guchar *dest, + const guchar *src, + guint width, + guint16 index) +{ + guint x; + + for (x = 0; x < width; x++) + { + *(dest++) = *(src++) - index; + } +} + static void read_line (FILE *fp, guchar *row, @@ -930,7 +945,14 @@ read_line (FILE *fp, { gboolean has_alpha = (info->alphaBits > 0); - apply_colormap (row, buf, info->width, convert_cmap, has_alpha); + apply_colormap (row, buf, info->width, convert_cmap, has_alpha, + info->colorMapIndex); + } + else if (info->imageType == TGA_TYPE_MAPPED) + { + g_assert(bpp == 1); + + apply_index (row, buf, info->width, info->colorMapIndex); } else { @@ -970,7 +992,7 @@ ReadImage (FILE *fp, dtype = GIMP_RGBA_IMAGE; convert_cmap = g_new (guchar, info->colorMapLength * 4); } - else if (info->colorMapLength > 256) + else if (info->colorMapIndex + info->colorMapLength > 256) { /* more than 256 colormap entries => promoted to RGB */ itype = GIMP_RGB;