summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Shaw <jshaw@netsurf-browser.org>2007-11-26 17:54:10 (GMT)
committer James Shaw <jshaw@netsurf-browser.org>2007-11-26 17:54:10 (GMT)
commit8c6e6d78d38544ec7922fea54478255d03018f1e (patch)
tree7f8bdb7e697a653de7eeaf063d255f585be0848e
parent8bc9fe4114622ca16f171b094098810f5eb61bb0 (diff)
downloadlibrosprite-8c6e6d78d38544ec7922fea54478255d03018f1e.tar.gz
librosprite-8c6e6d78d38544ec7922fea54478255d03018f1e.tar.bz2
Implement CMYK translation thanks to John-Mark Bell
svn path=/import/jshaw/libsprite/; revision=9993
-rw-r--r--trunk/example.c1
-rw-r--r--trunk/libsprite.c39
-rw-r--r--trunk/libsprite.h4
3 files changed, 39 insertions, 5 deletions
diff --git a/trunk/example.c b/trunk/example.c
index e425608..d1d8853 100644
--- a/trunk/example.c
+++ b/trunk/example.c
@@ -55,6 +55,7 @@ int main(int argc, char *argv[])
for (uint32_t i = 0; i < sprite_area->sprite_count; i++) {
struct sprite* sprite = sprite_area->sprites[i];
printf("\nname %s\n", sprite->name);
+ printf("color_model %s\n", sprite->mode->color_model == SPRITE_RGB ? "RGB" : "CMYK");
printf("colorbpp %u\n", sprite->mode->colorbpp);
printf("xdpi %u\n", sprite->mode->xdpi);
printf("ydpi %u\n", sprite->mode->ydpi);
diff --git a/trunk/libsprite.c b/trunk/libsprite.c
index ae653c9..f52e200 100644
--- a/trunk/libsprite.c
+++ b/trunk/libsprite.c
@@ -208,6 +208,8 @@ struct sprite_mode* sprite_get_mode(uint32_t spriteMode)
mode->maskbpp = (hasEightBitAlpha ? 8 : 1);
mode->xdpi = (spriteMode & 0x07ffc000) >> 14; /* preserve bits 14-26 only */
mode->ydpi = (spriteMode & 0x00003ffe) >> 1; /* preserve bits 1-13 only */
+
+ mode->color_model = SPRITE_RGB;
switch (spriteType) {
case 1:
mode->colorbpp = 1; break;
@@ -222,7 +224,9 @@ struct sprite_mode* sprite_get_mode(uint32_t spriteMode)
case 6:
mode->colorbpp = 32; break;
case 7:
- mode->colorbpp = 32; break; /* CMYK */
+ mode->colorbpp = 32;
+ mode->color_model = SPRITE_CMYK;
+ break;
case 8:
mode->colorbpp = 24; break;
}
@@ -267,10 +271,36 @@ uint32_t sprite_palette_lookup(struct sprite* sprite, uint32_t pixel)
return translated_pixel;
}
-uint32_t sprite_upscale_color(uint32_t pixel, uint32_t bpp)
+uint32_t sprite_cmyk_to_rgb(uint32_t cmyk)
+{
+ const uint8_t c = cmyk & 0xff;
+ const uint8_t m = (cmyk & 0xff00) >> 8;
+ const uint8_t y = (cmyk & 0xff0000) >> 16;
+ const uint8_t k = cmyk >> 24;
+
+ /* Convert to CMY colourspace */
+ const uint8_t C = c + k;
+ const uint8_t M = m + k;
+ const uint8_t Y = y + k;
+
+ /* And to RGB */
+ const uint8_t r = 255 - C;
+ const uint8_t g = 255 - M;
+ const uint8_t b = 255 - Y;
+
+ return r << 24 | g << 16 | b << 8;
+}
+
+uint32_t sprite_upscale_color(uint32_t pixel, struct sprite_mode* mode)
{
- switch (bpp) {
+ switch (mode->colorbpp) {
case 32:
+ if (mode->color_model == SPRITE_RGB) {
+ /* swap from 0xAABBGGRR to 0xRRGGBBAA */
+ return BSWAP(pixel);
+ } else {
+ return sprite_cmyk_to_rgb(pixel);
+ }
case 24:
/* reverse byte order */
return BSWAP(pixel);
@@ -298,7 +328,6 @@ uint32_t sprite_upscale_color(uint32_t pixel, uint32_t bpp)
assert(false); /* shouldn't need to call for <= 8bpp, since a palette lookup will return 32bpp */
default:
assert(false); /* unknown bpp */
- break;
}
}
@@ -325,7 +354,7 @@ void sprite_load_high_color(uint8_t* image_in, uint8_t* mask, struct sprite* spr
pixel = pixel | (b << (j * 8));
}
- pixel = sprite_upscale_color(pixel, bpp);
+ pixel = sprite_upscale_color(pixel, sprite->mode);
sprite->image[y*sprite->width + x_pixels] = pixel;
x_pixels++;
}
diff --git a/trunk/libsprite.h b/trunk/libsprite.h
index 81287fb..3fdc6c8 100644
--- a/trunk/libsprite.h
+++ b/trunk/libsprite.h
@@ -4,6 +4,9 @@
#include <stdint.h>
#include <stdbool.h>
+#define SPRITE_RGB 0
+#define SPRITE_CMYK 1
+
struct sprite_area {
uint32_t extension_size; /* size of extension_words in bytes */
uint8_t* extension_words;
@@ -16,6 +19,7 @@ struct sprite_mode {
uint32_t maskbpp;
uint32_t xdpi;
uint32_t ydpi;
+ uint32_t color_model;
};
struct sprite_palette {