Mercurial > hg > openttd
changeset 18821:66b1885b930f draft
(svn r23670) -Feature: Add ability to adjust brightness of colour after remapping for 32bpp sprites
author | peter1138 <peter1138@openttd.org> |
---|---|
date | Sat, 24 Dec 2011 23:33:45 +0000 |
parents | a41700432ff5 |
children | 010cf3e799f7 |
files | src/blitter/32bpp_anim.cpp src/blitter/32bpp_anim.hpp src/blitter/32bpp_base.hpp src/blitter/32bpp_optimized.cpp src/blitter/32bpp_simple.cpp src/blitter/32bpp_simple.hpp src/spriteloader/png.cpp |
diffstat | 7 files changed, 130 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- a/src/blitter/32bpp_anim.cpp +++ b/src/blitter/32bpp_anim.cpp @@ -25,27 +25,27 @@ const SpriteData *src = (const SpriteData *)bp->sprite; const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]); - const uint8 *src_n = (const uint8 *)(src->data + src->offset[zoom][1]); + const uint16 *src_n = (const uint16 *)(src->data + src->offset[zoom][1]); for (uint i = bp->skip_top; i != 0; i--) { src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px); - src_n += *(const uint32 *)src_n; + src_n = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n); } uint32 *dst = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left; - uint8 *anim = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_width + bp->left; + uint16 *anim = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_width + bp->left; const byte *remap = bp->remap; // store so we don't have to access it via bp everytime for (int y = 0; y < bp->height; y++) { uint32 *dst_ln = dst + bp->pitch; - uint8 *anim_ln = anim + this->anim_buf_width; + uint16 *anim_ln = anim + this->anim_buf_width; const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px); src_px++; - const uint8 *src_n_ln = src_n + *(const uint32 *)src_n; - src_n += 4; + const uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n); + src_n += 2; uint32 *dst_end = dst + bp->skip_left; @@ -106,9 +106,9 @@ *dst = src_px->data; *anim = 0; } else { - uint r = remap[m]; - *anim = r; - if (r != 0) *dst = this->LookupColourInPalette(r); + uint r = remap[GB(m, 0, 8)]; + *anim = r | (m & 0xFF00); + if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)); } anim++; dst++; @@ -122,9 +122,9 @@ *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst); *anim = 0; } else { - uint r = remap[m]; + uint r = remap[GB(m, 0, 8)]; *anim = 0; - if (r != 0) *dst = ComposeColourPANoCheck(this->LookupColourInPalette(r), src_px->a, *dst); + if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst); } anim++; dst++; @@ -145,14 +145,14 @@ src_px += n; do { *dst = MakeTransparent(*dst, 3, 4); - *anim = remap[*anim]; + *anim = 0; anim++; dst++; } while (--n != 0); } else { do { *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4); - *anim = remap[*anim]; + *anim = 0; anim++; dst++; src_px++; @@ -164,23 +164,25 @@ if (src_px->a == 255) { do { /* Compiler assumes pointer aliasing, can't optimise this on its own */ - uint m = *src_n++; + uint m = GB(*src_n, 0, 8); /* Above PALETTE_ANIM_START is palette animation */ - *anim++ = m; - *dst++ = (m >= PALETTE_ANIM_START) ? this->LookupColourInPalette(m) : src_px->data; + *anim++ = *src_n; + *dst++ = (m >= PALETTE_ANIM_START) ? this->AdjustBrightness(this->LookupColourInPalette(m), GB(*src_n, 8, 8)) : src_px->data; src_px++; + src_n++; } while (--n != 0); } else { do { - uint m = *src_n++; + uint m = GB(*src_n, 0, 8); *anim++ = 0; if (m >= PALETTE_ANIM_START) { - *dst = ComposeColourPANoCheck(this->LookupColourInPalette(m), src_px->a, *dst); + *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(m), GB(*src_n, 8, 8)), src_px->a, *dst); } else { *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst); } dst++; src_px++; + src_n++; } while (--n != 0); } break; @@ -219,7 +221,7 @@ } uint32 *udst = (uint32 *)dst; - uint8 *anim; + uint16 *anim; anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr); @@ -259,7 +261,7 @@ /* Set the colour in the anim-buffer too, if we are rendering to the screen */ if (_screen_disable_anim) return; - this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = colour; + this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = colour | (DEFAULT_BRIGHTNESS << 8); } void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 colour) @@ -271,18 +273,18 @@ } uint32 colour32 = LookupColourInPalette(colour); - uint8 *anim_line; + uint16 *anim_line; anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; do { uint32 *dst = (uint32 *)video; - uint8 *anim = anim_line; + uint16 *anim = anim_line; for (int i = width; i > 0; i--) { *dst = colour32; /* Set the colour in the anim-buffer too */ - *anim = colour; + *anim = colour | (DEFAULT_BRIGHTNESS << 8); dst++; anim++; } @@ -297,19 +299,19 @@ assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); uint32 *dst = (uint32 *)video; const uint32 *usrc = (const uint32 *)src; - uint8 *anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; + uint16 *anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; for (; height > 0; height--) { /* We need to keep those for palette animation. */ uint32 *dst_pal = dst; - uint8 *anim_pal = anim_line; + uint16 *anim_pal = anim_line; memcpy(dst, usrc, width * sizeof(uint32)); usrc += width; dst += _screen.pitch; /* Copy back the anim-buffer */ - memcpy(anim_line, usrc, width * sizeof(uint8)); - usrc = (const uint32 *)((const uint8 *)usrc + width); + memcpy(anim_line, usrc, width * sizeof(uint16)); + usrc = (const uint32 *)((const uint16 *)usrc + width); anim_line += this->anim_buf_width; /* Okay, it is *very* likely that the image we stored is using @@ -320,10 +322,10 @@ * for just the cursor. This just copies the implementation of * palette animation, much cheaper though slightly nastier. */ for (int i = 0; i < width; i++) { - uint colour = *anim_pal; + uint colour = GB(*anim_pal, 0, 8); if (IsInsideBS(colour, PALETTE_ANIM_START, PALETTE_ANIM_SIZE)) { /* Update this pixel */ - *dst_pal = LookupColourInPalette(colour); + *dst_pal = this->AdjustBrightness(LookupColourInPalette(colour), GB(*anim_pal, 8, 8)); } dst_pal++; anim_pal++; @@ -337,7 +339,7 @@ assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); uint32 *udst = (uint32 *)dst; const uint32 *src = (const uint32 *)video; - const uint8 *anim_line; + const uint16 *anim_line; if (this->anim_buf == NULL) return; @@ -348,8 +350,8 @@ src += _screen.pitch; udst += width; /* Copy the anim-buffer */ - memcpy(udst, anim_line, width * sizeof(uint8)); - udst = (uint32 *)((uint8 *)udst + width); + memcpy(udst, anim_line, width * sizeof(uint16)); + udst = (uint32 *)((uint16 *)udst + width); anim_line += this->anim_buf_width; } } @@ -358,7 +360,7 @@ { assert(!_screen_disable_anim); assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); - uint8 *dst, *src; + uint16 *dst, *src; /* We need to scroll the anim-buffer too */ if (scroll_y > 0) { @@ -375,7 +377,7 @@ uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); uint th = height - scroll_y; for (; th > 0; th--) { - memcpy(dst, src, tw * sizeof(uint8)); + memcpy(dst, src, tw * sizeof(uint16)); src -= this->anim_buf_width; dst -= this->anim_buf_width; } @@ -396,7 +398,7 @@ uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); uint th = height + scroll_y; for (; th > 0; th--) { - memmove(dst, src, tw * sizeof(uint8)); + memmove(dst, src, tw * sizeof(uint16)); src += this->anim_buf_width; dst += this->anim_buf_width; } @@ -407,7 +409,7 @@ int Blitter_32bppAnim::BufferSize(int width, int height) { - return width * height * (sizeof(uint32) + sizeof(uint8)); + return width * height * (sizeof(uint32) + sizeof(uint16)); } void Blitter_32bppAnim::PaletteAnimate(const Palette &palette) @@ -421,16 +423,19 @@ this->palette.count_dirty--; } - const uint8 *anim = this->anim_buf; + this->palette.first_dirty = 1; + this->palette.count_dirty = 255; + + const uint16 *anim = this->anim_buf; uint32 *dst = (uint32 *)_screen.dst_ptr; /* Let's walk the anim buffer and try to find the pixels */ for (int y = this->anim_buf_height; y != 0 ; y--) { for (int x = this->anim_buf_width; x != 0 ; x--) { - uint colour = *anim; + uint colour = GB(*anim, 0, 8); if (IsInsideBS(colour, this->palette.first_dirty, this->palette.count_dirty)) { /* Update this pixel */ - *dst = LookupColourInPalette(colour); + *dst = this->AdjustBrightness(LookupColourInPalette(colour), GB(*anim, 8, 8)); } dst++; anim++; @@ -452,7 +457,7 @@ if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) { /* The size of the screen changed; we can assume we can wipe all data from our buffer */ free(this->anim_buf); - this->anim_buf = CallocT<uint8>(_screen.width * _screen.height); + this->anim_buf = CallocT<uint16>(_screen.width * _screen.height); this->anim_buf_width = _screen.width; this->anim_buf_height = _screen.height; }
--- a/src/blitter/32bpp_anim.hpp +++ b/src/blitter/32bpp_anim.hpp @@ -17,7 +17,7 @@ /** The optimised 32 bpp blitter with palette animation. */ class Blitter_32bppAnim FINAL : public Blitter_32bppOptimized { private: - uint8 *anim_buf; ///< In this buffer we keep track of the 8bpp indexes so we can do palette animation + uint16 *anim_buf; ///< In this buffer we keep track of the 8bpp indexes so we can do palette animation int anim_buf_width; ///< The width of the animation buffer. int anim_buf_height; ///< The height of the animation buffer. Palette palette; ///< The current palette.
--- a/src/blitter/32bpp_base.hpp +++ b/src/blitter/32bpp_base.hpp @@ -14,6 +14,7 @@ #include "base.hpp" #include "../core/bitmath_func.hpp" +#include "../core/math_func.hpp" #include "../gfx_func.h" /** Base for all 32bpp blitters. */ @@ -134,6 +135,33 @@ return ComposeColour(0xFF, colour, colour, colour); } + + static const int DEFAULT_BRIGHTNESS = 64; + + static inline uint32 AdjustBrightness(uint32 colour, uint8 brightness) + { + /* Shortcut for normal brightness */ + if (brightness == DEFAULT_BRIGHTNESS) return colour; + + uint16 ob = 0; + uint16 r = GB(colour, 16, 8) * brightness / DEFAULT_BRIGHTNESS; + uint16 g = GB(colour, 8, 8) * brightness / DEFAULT_BRIGHTNESS; + uint16 b = GB(colour, 0, 8) * brightness / DEFAULT_BRIGHTNESS; + + /* Sum overbright */ + if (r > 255) ob += r - 255; + if (g > 255) ob += g - 255; + if (b > 255) ob += b - 255; + + if (ob == 0) return ComposeColour(GB(colour, 24, 8), r, g, b); + + /* Reduce overbright strength */ + ob /= 2; + return ComposeColour(GB(colour, 24, 8), + r >= 255 ? 255 : min(r + ob * (255 - r) / 256, 255), + g >= 255 ? 255 : min(g + ob * (255 - g) / 256, 255), + b >= 255 ? 255 : min(b + ob * (255 - b) / 256, 255)); + } }; #endif /* BLITTER_32BPP_BASE_HPP */
--- a/src/blitter/32bpp_optimized.cpp +++ b/src/blitter/32bpp_optimized.cpp @@ -36,12 +36,12 @@ /* src_n : each line begins with uint32 n = 'number of bytes in this line', * then interleaved stream of 'm' and 'n' channels. 'm' is remap, * 'n' is number of bytes with the same alpha channel class */ - const uint8 *src_n = (const uint8 *)(src->data + src->offset[zoom][1]); + const uint16 *src_n = (const uint16 *)(src->data + src->offset[zoom][1]); /* skip upper lines in src_px and src_n */ for (uint i = bp->skip_top; i != 0; i--) { src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px); - src_n += *(const uint32 *)src_n; + src_n = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n); } /* skip lines in dst */ @@ -59,8 +59,8 @@ src_px++; /* next src_n line begins here */ - const uint8 *src_n_ln = src_n + *(const uint32 *)src_n; - src_n += 4; + const uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n); + src_n += 2; /* we will end this line when we reach this point */ uint32 *dst_end = dst + bp->skip_left; @@ -119,8 +119,8 @@ if (m == 0) { *dst = src_px->data; } else { - uint r = remap[m]; - if (r != 0) *dst = this->LookupColourInPalette(r); + uint r = remap[GB(m, 0, 8)]; + if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)); } dst++; src_px++; @@ -132,8 +132,8 @@ if (m == 0) { *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst); } else { - uint r = remap[m]; - if (r != 0) *dst = ComposeColourPANoCheck(this->LookupColourInPalette(r), src_px->a, *dst); + uint r = remap[GB(m, 0, 8)]; + if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst); } dst++; src_px++; @@ -270,7 +270,7 @@ * * it has to be stored in one stream so fewer registers are used - * x86 has problems with register allocation even with this solution */ - uint8 *dst_n_orig[ZOOM_LVL_COUNT]; + uint16 *dst_n_orig[ZOOM_LVL_COUNT]; /* lengths of streams */ uint32 lengths[ZOOM_LVL_COUNT][2]; @@ -293,7 +293,7 @@ uint size = src_orig->height * src_orig->width; dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2); - dst_n_orig[z] = CallocT<uint8>(size * 2 + src_orig->height * 4 * 2); + dst_n_orig[z] = CallocT<uint16>(size * 2 + src_orig->height * 4 * 2); uint32 *dst_px_ln = (uint32 *)dst_px_orig[z]; uint32 *dst_n_ln = (uint32 *)dst_n_orig[z]; @@ -302,9 +302,9 @@ for (uint y = src_orig->height; y > 0; y--) { Colour *dst_px = (Colour *)(dst_px_ln + 1); - uint8 *dst_n = (uint8 *)(dst_n_ln + 1); + uint16 *dst_n = (uint16 *)(dst_n_ln + 1); - uint8 *dst_len = dst_n++; + uint16 *dst_len = dst_n++; uint last = 3; int len = 0; @@ -313,7 +313,7 @@ uint8 a = src->a; uint t = a > 0 && a < 255 ? 1 : a; - if (last != t || len == 255) { + if (last != t || len == 65535) { if (last != 3) { *dst_len = len; dst_len = dst_n++; @@ -328,8 +328,15 @@ dst_px->a = a; *dst_n = src->m; if (src->m != 0) { + /* Get brightest value */ + uint8 rgb_max = max(src->r, max(src->g, src->b)); + + /* Black pixel (8bpp or old 32bpp image), so use default value */ + if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS; + *dst_n |= rgb_max << 8; + /* Pre-convert the mapping channel to a RGB value */ - uint32 colour = this->LookupColourInPalette(src->m); + uint32 colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max); dst_px->r = GB(colour, 16, 8); dst_px->g = GB(colour, 8, 8); dst_px->b = GB(colour, 0, 8); @@ -354,7 +361,7 @@ } dst_px = (Colour *)AlignPtr(dst_px, 4); - dst_n = (uint8 *)AlignPtr(dst_n, 4); + dst_n = (uint16 *)AlignPtr(dst_n, 4); *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln; *dst_n_ln = (uint8 *)dst_n - (uint8 *)dst_n_ln;
--- a/src/blitter/32bpp_simple.cpp +++ b/src/blitter/32bpp_simple.cpp @@ -20,11 +20,11 @@ void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) { - const SpriteLoader::CommonPixel *src, *src_line; + const Blitter_32bppSimple::Pixel *src, *src_line; uint32 *dst, *dst_line; /* Find where to start reading in the source sprite */ - src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom); + src_line = (const Blitter_32bppSimple::Pixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom); dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left; for (int y = 0; y < bp->height; y++) { @@ -41,7 +41,7 @@ if (src->m == 0) { if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst); } else { - if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst); + if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst); } break; @@ -94,26 +94,42 @@ Sprite *Blitter_32bppSimple::Encode(SpriteLoader::Sprite *sprite, AllocatorProc *allocator) { - Sprite *dest_sprite; - SpriteLoader::CommonPixel *dst; - dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel)); + Blitter_32bppSimple::Pixel *dst; + Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(*dst)); dest_sprite->height = sprite->height; dest_sprite->width = sprite->width; dest_sprite->x_offs = sprite->x_offs; dest_sprite->y_offs = sprite->y_offs; - dst = (SpriteLoader::CommonPixel *)dest_sprite->data; + dst = (Blitter_32bppSimple::Pixel *)dest_sprite->data; + SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite->data; - memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel)); for (int i = 0; i < sprite->height * sprite->width; i++) { - if (dst[i].m != 0) { + if (src->m == 0) { + dst[i].r = src->r; + dst[i].g = src->g; + dst[i].b = src->b; + dst[i].a = src->a; + dst[i].m = 0; + dst[i].v = 0; + } else { + /* Get brightest value */ + uint8 rgb_max = max(src->r, max(src->g, src->b)); + + /* Black pixel (8bpp or old 32bpp image), so use default value */ + if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS; + dst[i].v = rgb_max; + /* Pre-convert the mapping channel to a RGB value */ - uint colour = this->LookupColourInPalette(dst[i].m); + uint colour = this->AdjustBrightness(this->LookupColourInPalette(src[i].m), dst[i].v); dst[i].r = GB(colour, 16, 8); dst[i].g = GB(colour, 8, 8); dst[i].b = GB(colour, 0, 8); + dst[i].a = src->a; + dst[i].m = src->m; } + src++; } return dest_sprite;
--- a/src/blitter/32bpp_simple.hpp +++ b/src/blitter/32bpp_simple.hpp @@ -17,6 +17,14 @@ /** The most trivial 32 bpp blitter (without palette animation). */ class Blitter_32bppSimple : public Blitter_32bppBase { + struct Pixel { + uint8 r; ///< Red-channel + uint8 g; ///< Green-channel + uint8 b; ///< Blue-channel + uint8 a; ///< Alpha-channel + uint8 m; ///< Remap-channel + uint8 v; ///< Brightness-channel + }; public: /* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom); /* virtual */ void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal);
--- a/src/spriteloader/png.cpp +++ b/src/spriteloader/png.cpp @@ -186,9 +186,6 @@ for (uint x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) { if (mask) { if (row_pointer[x * sizeof(uint8)] != 0) { - dst[x].r = 0; - dst[x].g = 0; - dst[x].b = 0; /* Alpha channel is used from the original image (to allow transparency in remap colours) */ extern const byte _palmap_w2d[]; byte colour = row_pointer[x * sizeof(uint8)];