OpenTTD Source  12.0-beta2
32bpp_optimized.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../zoom_func.h"
12 #include "../settings_type.h"
13 #include "32bpp_optimized.hpp"
14 
15 #include "../safeguards.h"
16 
19 
27 template <BlitterMode mode, bool Tpal_to_rgb>
29 {
30  const SpriteData *src = (const SpriteData *)bp->sprite;
31 
32  /* src_px : each line begins with uint32 n = 'number of bytes in this line',
33  * then n times is the Colour struct for this line */
34  const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]);
35  /* src_n : each line begins with uint32 n = 'number of bytes in this line',
36  * then interleaved stream of 'm' and 'n' channels. 'm' is remap,
37  * 'n' is number of bytes with the same alpha channel class */
38  const uint16 *src_n = (const uint16 *)(src->data + src->offset[zoom][1]);
39 
40  /* skip upper lines in src_px and src_n */
41  for (uint i = bp->skip_top; i != 0; i--) {
42  src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
43  src_n = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
44  }
45 
46  /* skip lines in dst */
47  Colour *dst = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
48 
49  /* store so we don't have to access it via bp every time (compiler assumes pointer aliasing) */
50  const byte *remap = bp->remap;
51 
52  for (int y = 0; y < bp->height; y++) {
53  /* next dst line begins here */
54  Colour *dst_ln = dst + bp->pitch;
55 
56  /* next src line begins here */
57  const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
58  src_px++;
59 
60  /* next src_n line begins here */
61  const uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
62  src_n += 2;
63 
64  /* we will end this line when we reach this point */
65  Colour *dst_end = dst + bp->skip_left;
66 
67  /* number of pixels with the same alpha channel class */
68  uint n;
69 
70  while (dst < dst_end) {
71  n = *src_n++;
72 
73  if (src_px->a == 0) {
74  dst += n;
75  src_px ++;
76  src_n++;
77  } else {
78  if (dst + n > dst_end) {
79  uint d = dst_end - dst;
80  src_px += d;
81  src_n += d;
82 
83  dst = dst_end - bp->skip_left;
84  dst_end = dst + bp->width;
85 
86  n = std::min(n - d, (uint)bp->width);
87  goto draw;
88  }
89  dst += n;
90  src_px += n;
91  src_n += n;
92  }
93  }
94 
95  dst -= bp->skip_left;
96  dst_end -= bp->skip_left;
97 
98  dst_end += bp->width;
99 
100  while (dst < dst_end) {
101  n = std::min<uint>(*src_n++, dst_end - dst);
102 
103  if (src_px->a == 0) {
104  dst += n;
105  src_px++;
106  src_n++;
107  continue;
108  }
109 
110  draw:;
111 
112  switch (mode) {
113  case BM_COLOUR_REMAP:
114  if (src_px->a == 255) {
115  do {
116  uint m = *src_n;
117  /* In case the m-channel is zero, do not remap this pixel in any way */
118  if (m == 0) {
119  *dst = src_px->data;
120  } else {
121  uint r = remap[GB(m, 0, 8)];
122  if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
123  }
124  dst++;
125  src_px++;
126  src_n++;
127  } while (--n != 0);
128  } else {
129  do {
130  uint m = *src_n;
131  if (m == 0) {
132  *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
133  } else {
134  uint r = remap[GB(m, 0, 8)];
135  if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
136  }
137  dst++;
138  src_px++;
139  src_n++;
140  } while (--n != 0);
141  }
142  break;
143 
144  case BM_CRASH_REMAP:
145  if (src_px->a == 255) {
146  do {
147  uint m = *src_n;
148  if (m == 0) {
149  uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
150  *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
151  } else {
152  uint r = remap[GB(m, 0, 8)];
153  if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
154  }
155  dst++;
156  src_px++;
157  src_n++;
158  } while (--n != 0);
159  } else {
160  do {
161  uint m = *src_n;
162  if (m == 0) {
163  if (src_px->a != 0) {
164  uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
165  *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
166  }
167  } else {
168  uint r = remap[GB(m, 0, 8)];
169  if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
170  }
171  dst++;
172  src_px++;
173  src_n++;
174  } while (--n != 0);
175  }
176  break;
177 
178  case BM_BLACK_REMAP:
179  do {
180  *dst = Colour(0, 0, 0);
181  dst++;
182  src_px++;
183  src_n++;
184  } while (--n != 0);
185  break;
186 
187  case BM_TRANSPARENT:
188  /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
189  * This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
190  * we produce a result the newgrf maker didn't expect ;) */
191 
192  /* Make the current colour a bit more black, so it looks like this image is transparent */
193  src_n += n;
194  if (src_px->a == 255) {
195  src_px += n;
196  do {
197  *dst = MakeTransparent(*dst, 3, 4);
198  dst++;
199  } while (--n != 0);
200  } else {
201  do {
202  *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
203  dst++;
204  src_px++;
205  } while (--n != 0);
206  }
207  break;
208 
209  default:
210  if (src_px->a == 255) {
211  /* faster than memcpy(), n is usually low */
212  do {
213  if (Tpal_to_rgb && *src_n != 0) {
214  /* Convert the mapping channel to a RGB value */
215  *dst = this->AdjustBrightness(this->LookupColourInPalette(GB(*src_n, 0, 8)), GB(*src_n, 8, 8)).data;
216  } else {
217  *dst = src_px->data;
218  }
219  dst++;
220  src_px++;
221  src_n++;
222  } while (--n != 0);
223  } else {
224  do {
225  if (Tpal_to_rgb && *src_n != 0) {
226  /* Convert the mapping channel to a RGB value */
227  Colour colour = this->AdjustBrightness(this->LookupColourInPalette(GB(*src_n, 0, 8)), GB(*src_n, 8, 8));
228  *dst = ComposeColourRGBANoCheck(colour.r, colour.g, colour.b, src_px->a, *dst);
229  } else {
230  *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
231  }
232  dst++;
233  src_px++;
234  src_n++;
235  } while (--n != 0);
236  }
237  break;
238  }
239  }
240 
241  dst = dst_ln;
242  src_px = src_px_ln;
243  src_n = src_n_ln;
244  }
245 }
246 
247 template <bool Tpal_to_rgb>
249 {
250  switch (mode) {
251  default: NOT_REACHED();
252  case BM_NORMAL: Draw<BM_NORMAL, Tpal_to_rgb>(bp, zoom); return;
253  case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP, Tpal_to_rgb>(bp, zoom); return;
254  case BM_TRANSPARENT: Draw<BM_TRANSPARENT, Tpal_to_rgb>(bp, zoom); return;
255  case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP, Tpal_to_rgb>(bp, zoom); return;
256  case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP, Tpal_to_rgb>(bp, zoom); return;
257  }
258 }
259 
260 template void Blitter_32bppOptimized::Draw<true>(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
261 template void Blitter_32bppOptimized::Draw<false>(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
262 
271 {
272  this->Draw<false>(bp, mode, zoom);
273 }
274 
275 template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
276 {
277  /* streams of pixels (a, r, g, b channels)
278  *
279  * stored in separated stream so data are always aligned on 4B boundary */
280  Colour *dst_px_orig[ZOOM_LVL_COUNT];
281 
282  /* interleaved stream of 'm' channel and 'n' channel
283  * 'n' is number of following pixels with the same alpha channel class
284  * there are 3 classes: 0, 255, others
285  *
286  * it has to be stored in one stream so fewer registers are used -
287  * x86 has problems with register allocation even with this solution */
288  uint16 *dst_n_orig[ZOOM_LVL_COUNT];
289 
290  /* lengths of streams */
291  uint32 lengths[ZOOM_LVL_COUNT][2];
292 
293  ZoomLevel zoom_min;
294  ZoomLevel zoom_max;
295 
296  if (sprite->type == ST_FONT) {
297  zoom_min = ZOOM_LVL_NORMAL;
298  zoom_max = ZOOM_LVL_NORMAL;
299  } else {
300  zoom_min = _settings_client.gui.zoom_min;
301  zoom_max = _settings_client.gui.zoom_max;
302  if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
303  }
304 
305  for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
306  const SpriteLoader::Sprite *src_orig = &sprite[z];
307 
308  uint size = src_orig->height * src_orig->width;
309 
310  dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
311  dst_n_orig[z] = CallocT<uint16>(size * 2 + src_orig->height * 4 * 2);
312 
313  uint32 *dst_px_ln = (uint32 *)dst_px_orig[z];
314  uint32 *dst_n_ln = (uint32 *)dst_n_orig[z];
315 
316  const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
317 
318  for (uint y = src_orig->height; y > 0; y--) {
319  Colour *dst_px = (Colour *)(dst_px_ln + 1);
320  uint16 *dst_n = (uint16 *)(dst_n_ln + 1);
321 
322  uint16 *dst_len = dst_n++;
323 
324  uint last = 3;
325  int len = 0;
326 
327  for (uint x = src_orig->width; x > 0; x--) {
328  uint8 a = src->a;
329  uint t = a > 0 && a < 255 ? 1 : a;
330 
331  if (last != t || len == 65535) {
332  if (last != 3) {
333  *dst_len = len;
334  dst_len = dst_n++;
335  }
336  len = 0;
337  }
338 
339  last = t;
340  len++;
341 
342  if (a != 0) {
343  dst_px->a = a;
344  *dst_n = src->m;
345  if (src->m != 0) {
346  /* Get brightest value */
347  uint8 rgb_max = std::max({ src->r, src->g, src->b });
348 
349  /* Black pixel (8bpp or old 32bpp image), so use default value */
350  if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
351  *dst_n |= rgb_max << 8;
352 
353  if (Tpal_to_rgb) {
354  /* Pre-convert the mapping channel to a RGB value */
355  Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max);
356  dst_px->r = colour.r;
357  dst_px->g = colour.g;
358  dst_px->b = colour.b;
359  } else {
360  dst_px->r = src->r;
361  dst_px->g = src->g;
362  dst_px->b = src->b;
363  }
364  } else {
365  dst_px->r = src->r;
366  dst_px->g = src->g;
367  dst_px->b = src->b;
368  }
369  dst_px++;
370  dst_n++;
371  } else if (len == 1) {
372  dst_px++;
373  *dst_n = src->m;
374  dst_n++;
375  }
376 
377  src++;
378  }
379 
380  if (last != 3) {
381  *dst_len = len;
382  }
383 
384  dst_px = (Colour *)AlignPtr(dst_px, 4);
385  dst_n = (uint16 *)AlignPtr(dst_n, 4);
386 
387  *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln;
388  *dst_n_ln = (uint8 *)dst_n - (uint8 *)dst_n_ln;
389 
390  dst_px_ln = (uint32 *)dst_px;
391  dst_n_ln = (uint32 *)dst_n;
392  }
393 
394  lengths[z][0] = (byte *)dst_px_ln - (byte *)dst_px_orig[z]; // all are aligned to 4B boundary
395  lengths[z][1] = (byte *)dst_n_ln - (byte *)dst_n_orig[z];
396  }
397 
398  uint len = 0; // total length of data
399  for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
400  len += lengths[z][0] + lengths[z][1];
401  }
402 
403  Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(SpriteData) + len);
404 
405  dest_sprite->height = sprite->height;
406  dest_sprite->width = sprite->width;
407  dest_sprite->x_offs = sprite->x_offs;
408  dest_sprite->y_offs = sprite->y_offs;
409 
410  SpriteData *dst = (SpriteData *)dest_sprite->data;
411  memset(dst, 0, sizeof(*dst));
412 
413  for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
414  dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
415  dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
416 
417  memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
418  memcpy(dst->data + dst->offset[z][1], dst_n_orig[z], lengths[z][1]);
419 
420  free(dst_px_orig[z]);
421  free(dst_n_orig[z]);
422  }
423 
424  return dest_sprite;
425 }
426 
427 template Sprite *Blitter_32bppOptimized::EncodeInternal<true>(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator);
428 template Sprite *Blitter_32bppOptimized::EncodeInternal<false>(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator);
429 
430 Sprite *Blitter_32bppOptimized::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
431 {
432  return this->EncodeInternal<true>(sprite, allocator);
433 }
SpriteLoader::CommonPixel::m
uint8 m
Remap-channel.
Definition: spriteloader.hpp:39
SpriteLoader::CommonPixel::r
uint8 r
Red-channel.
Definition: spriteloader.hpp:35
Colour::data
uint32 data
Conversion of the channel information to a 32 bit number.
Definition: gfx_type.h:164
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:304
Blitter::BlitterParams::top
int top
The top offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:42
BM_TRANSPARENT
@ BM_TRANSPARENT
Perform transparency colour remapping.
Definition: base.hpp:20
Blitter::BlitterParams::skip_left
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition: base.hpp:35
BlitterMode
BlitterMode
The modes of blitting we can do.
Definition: base.hpp:17
Blitter::BlitterParams::width
int width
The width in pixels that needs to be drawn to dst.
Definition: base.hpp:37
Sprite::data
byte data[]
Sprite data.
Definition: spritecache.h:22
Blitter_32bppOptimized::Encode
Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override
Convert a sprite from the loader to our own format.
Definition: 32bpp_optimized.cpp:430
ZOOM_LVL_COUNT
@ ZOOM_LVL_COUNT
Number of zoom levels.
Definition: zoom_type.h:32
Blitter::BlitterParams::dst
void * dst
Destination buffer.
Definition: base.hpp:44
Sprite::height
uint16 height
Height of the sprite.
Definition: spritecache.h:18
Blitter_32bppBase::ComposeColourPANoCheck
static Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
Compose a colour based on Pixel value, alpha value, and the current pixel value.
Definition: 32bpp_base.hpp:74
Sprite::x_offs
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
Blitter::BlitterParams::pitch
int pitch
The pitch of the destination buffer.
Definition: base.hpp:45
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
ZOOM_LVL_MAX
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition: zoom_type.h:48
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:130
BM_NORMAL
@ BM_NORMAL
Perform the simple blitting.
Definition: base.hpp:18
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition: spriteloader.hpp:55
Blitter::BlitterParams::sprite
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition: base.hpp:32
32bpp_optimized.hpp
Blitter_32bppOptimized::SpriteData::data
byte data[]
Data, all zoomlevels.
Definition: 32bpp_optimized.hpp:21
SpriteLoader::Sprite::type
SpriteType type
The sprite type.
Definition: spriteloader.hpp:53
BM_COLOUR_REMAP
@ BM_COLOUR_REMAP
Perform a colour remapping.
Definition: base.hpp:19
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:34
BM_CRASH_REMAP
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition: base.hpp:21
Sprite::width
uint16 width
Width of the sprite.
Definition: spritecache.h:19
Blitter_32bppBase::LookupColourInPalette
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:38
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
BM_BLACK_REMAP
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition: base.hpp:22
Blitter_32bppBase::MakeDark
static uint8 MakeDark(uint8 r, uint8 g, uint8 b)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:121
Blitter_32bppOptimized::Draw
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draws a sprite to a (screen) buffer.
Definition: 32bpp_optimized.cpp:270
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:163
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:129
Blitter_32bppBase::ComposeColourRGBA
static Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
Compose a colour based on RGBA values and the current pixel value.
Definition: 32bpp_base.hpp:63
SpriteLoader::CommonPixel::b
uint8 b
Blue-channel.
Definition: spriteloader.hpp:37
SpriteLoader::Sprite::width
uint16 width
Width of the sprite.
Definition: spriteloader.hpp:50
Colour::a
uint8 a
colour channels in LE order
Definition: gfx_type.h:171
FBlitter_32bppOptimized
Factory for the optimised 32 bpp blitter (without palette animation).
Definition: 32bpp_optimized.hpp:37
Blitter::BlitterParams::left
int left
The left offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:41
Blitter_32bppOptimized::SpriteData::offset
uint32 offset[ZOOM_LVL_COUNT][2]
Offsets (from .data) to streams for different zoom levels, and the normal and remap image information...
Definition: 32bpp_optimized.hpp:20
Blitter_32bppBase::ComposeColourRGBANoCheck
static Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
Compose a colour based on RGBA values and the current pixel value.
Definition: 32bpp_base.hpp:46
Sprite::y_offs
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:48
Blitter_32bppBase::MakeTransparent
static Colour MakeTransparent(Colour colour, uint nom, uint denom=256)
Make a pixel looks like it is transparent.
Definition: 32bpp_base.hpp:105
Blitter::BlitterParams
Parameters related to blitting.
Definition: base.hpp:31
SpriteLoader::CommonPixel::g
uint8 g
Green-channel.
Definition: spriteloader.hpp:36
SpriteLoader::CommonPixel::a
uint8 a
Alpha-channel.
Definition: spriteloader.hpp:38
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:24
Blitter::BlitterParams::height
int height
The height in pixels that needs to be drawn to dst.
Definition: base.hpp:38
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:52
Blitter_32bppOptimized::SpriteData
Data stored about a (single) sprite.
Definition: 32bpp_optimized.hpp:19
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:49
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
Blitter::BlitterParams::skip_top
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition: base.hpp:36
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
AlignPtr
static T * AlignPtr(T *x, uint n)
Return the smallest multiple of n equal or greater than x Applies to pointers only.
Definition: math_func.hpp:53
Blitter::BlitterParams::remap
const byte * remap
XXX – Temporary storage for remap array.
Definition: base.hpp:33
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
iFBlitter_32bppOptimized
static FBlitter_32bppOptimized iFBlitter_32bppOptimized
Instantiation of the optimized 32bpp blitter factory.
Definition: 32bpp_optimized.cpp:18