summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2011-10-06 19:17:49 +0000
committerfrosch <frosch@openttd.org>2011-10-06 19:17:49 +0000
commitca1df3ddab2c70cef9edcdc462e07fdad17f4ab9 (patch)
tree4c884c5fa0ad7bcf5261ba0a5b003443263f1af2
parent7762bceadb8034a737730677f0104dc75eb0ffa2 (diff)
downloadopenttd-ca1df3ddab2c70cef9edcdc462e07fdad17f4ab9.tar.xz
(svn r23008) -Add: Palette detection and conversion for the M part of 32bpp sprites.
-rw-r--r--src/spriteloader/png.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/spriteloader/png.cpp b/src/spriteloader/png.cpp
index 693623ead..790b2a2c9 100644
--- a/src/spriteloader/png.cpp
+++ b/src/spriteloader/png.cpp
@@ -132,6 +132,7 @@ static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 i
return true;
}
+ bool win_palette = false;
if (!mask) {
if (bit_depth == 16) png_set_strip_16(png_ptr);
@@ -156,6 +157,23 @@ static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 i
pixelsize = sizeof(uint32);
} else {
pixelsize = sizeof(uint8);
+
+ /* Check whether the DOS greyscale is in place. If not, we assume WIN palette */
+ png_colorp palette;
+ int num_palette;
+ if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette) && num_palette == 256) {
+ for (uint i = 3; i <= 5; i++) {
+ /* Allow differences between -4 and +4 */
+ uint8 intensity = i * 16 - 4;
+ uint8 diff_r = palette[i].red - intensity;
+ uint8 diff_g = palette[i].green - intensity;
+ uint8 diff_b = palette[i].blue - intensity;
+ if (diff_r > 8 || diff_g > 8 || diff_b > 8) {
+ win_palette = true;
+ break;
+ }
+ }
+ }
}
png_bytep row_pointer = AllocaM(png_byte, png_get_image_width(png_ptr, info_ptr) * pixelsize);
@@ -172,7 +190,9 @@ static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 i
dst[x].g = 0;
dst[x].b = 0;
/* Alpha channel is used from the original image (to allow transparency in remap colours) */
- dst[x].m = row_pointer[x * sizeof(uint8)];
+ extern const byte _palmap_w2d[];
+ byte color = row_pointer[x * sizeof(uint8)];
+ dst[x].m = win_palette ? _palmap_w2d[color] : color;
}
} else {
dst[x].r = row_pointer[x * sizeof(uint32) + 0];