OpenTTD
8bpp_optimized.cpp
Go to the documentation of this file.
1 /* $Id: 8bpp_optimized.cpp 26969 2014-10-06 18:45:51Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../zoom_func.h"
14 #include "../settings_type.h"
15 #include "../core/math_func.hpp"
16 #include "../core/mem_func.hpp"
17 #include "8bpp_optimized.hpp"
18 
19 #include "../safeguards.h"
20 
23 
25 {
26  /* Find the offset of this zoom-level */
27  const SpriteData *sprite_src = (const SpriteData *)bp->sprite;
28  uint offset = sprite_src->offset[zoom];
29 
30  /* Find where to start reading in the source sprite */
31  const uint8 *src = sprite_src->data + offset;
32  uint8 *dst_line = (uint8 *)bp->dst + bp->top * bp->pitch + bp->left;
33 
34  /* Skip over the top lines in the source image */
35  for (int y = 0; y < bp->skip_top; y++) {
36  for (;;) {
37  uint trans = *src++;
38  uint pixels = *src++;
39  if (trans == 0 && pixels == 0) break;
40  src += pixels;
41  }
42  }
43 
44  const uint8 *src_next = src;
45 
46  for (int y = 0; y < bp->height; y++) {
47  uint8 *dst = dst_line;
48  dst_line += bp->pitch;
49 
50  uint skip_left = bp->skip_left;
51  int width = bp->width;
52 
53  for (;;) {
54  src = src_next;
55  uint trans = *src++;
56  uint pixels = *src++;
57  src_next = src + pixels;
58  if (trans == 0 && pixels == 0) break;
59  if (width <= 0) continue;
60 
61  if (skip_left != 0) {
62  if (skip_left < trans) {
63  trans -= skip_left;
64  skip_left = 0;
65  } else {
66  skip_left -= trans;
67  trans = 0;
68  }
69  if (skip_left < pixels) {
70  src += skip_left;
71  pixels -= skip_left;
72  skip_left = 0;
73  } else {
74  src += pixels;
75  skip_left -= pixels;
76  pixels = 0;
77  }
78  }
79  if (skip_left != 0) continue;
80 
81  /* Skip transparent pixels */
82  dst += trans;
83  width -= trans;
84  if (width <= 0 || pixels == 0) continue;
85  pixels = min<uint>(pixels, (uint)width);
86  width -= pixels;
87 
88  switch (mode) {
89  case BM_COLOUR_REMAP:
90  case BM_CRASH_REMAP: {
91  const uint8 *remap = bp->remap;
92  do {
93  uint m = remap[*src];
94  if (m != 0) *dst = m;
95  dst++; src++;
96  } while (--pixels != 0);
97  break;
98  }
99 
100  case BM_BLACK_REMAP:
101  MemSetT(dst, 0, pixels);
102  dst += pixels;
103  break;
104 
105  case BM_TRANSPARENT: {
106  const uint8 *remap = bp->remap;
107  src += pixels;
108  do {
109  *dst = remap[*dst];
110  dst++;
111  } while (--pixels != 0);
112  break;
113  }
114 
115  default:
116  MemCpyT(dst, src, pixels);
117  dst += pixels; src += pixels;
118  break;
119  }
120  }
121  }
122 }
123 
124 Sprite *Blitter_8bppOptimized::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
125 {
126  /* Make memory for all zoom-levels */
127  uint memory = sizeof(SpriteData);
128 
129  ZoomLevel zoom_min;
130  ZoomLevel zoom_max;
131 
132  if (sprite->type == ST_FONT) {
133  zoom_min = ZOOM_LVL_NORMAL;
134  zoom_max = ZOOM_LVL_NORMAL;
135  } else {
136  zoom_min = _settings_client.gui.zoom_min;
137  zoom_max = _settings_client.gui.zoom_max;
138  if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
139  }
140 
141  for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
142  memory += sprite[i].width * sprite[i].height;
143  }
144 
145  /* We have no idea how much memory we really need, so just guess something */
146  memory *= 5;
147 
148  /* Don't allocate memory each time, but just keep some
149  * memory around as this function is called quite often
150  * and the memory usage is quite low. */
151  static ReusableBuffer<byte> temp_buffer;
152  SpriteData *temp_dst = (SpriteData *)temp_buffer.Allocate(memory);
153  memset(temp_dst, 0, sizeof(*temp_dst));
154  byte *dst = temp_dst->data;
155 
156  /* Make the sprites per zoom-level */
157  for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
158  /* Store the index table */
159  uint offset = dst - temp_dst->data;
160  temp_dst->offset[i] = offset;
161 
162  /* cache values, because compiler can't cache it */
163  int scaled_height = sprite[i].height;
164  int scaled_width = sprite[i].width;
165 
166  for (int y = 0; y < scaled_height; y++) {
167  uint trans = 0;
168  uint pixels = 0;
169  uint last_colour = 0;
170  byte *count_dst = NULL;
171 
172  /* Store the scaled image */
173  const SpriteLoader::CommonPixel *src = &sprite[i].data[y * sprite[i].width];
174 
175  for (int x = 0; x < scaled_width; x++) {
176  uint colour = src++->m;
177 
178  if (last_colour == 0 || colour == 0 || pixels == 255) {
179  if (count_dst != NULL) {
180  /* Write how many non-transparent bytes we get */
181  *count_dst = pixels;
182  pixels = 0;
183  count_dst = NULL;
184  }
185  /* As long as we find transparency bytes, keep counting */
186  if (colour == 0 && trans != 255) {
187  last_colour = 0;
188  trans++;
189  continue;
190  }
191  /* No longer transparency, so write the amount of transparent bytes */
192  *dst = trans;
193  dst++;
194  trans = 0;
195  /* Reserve a byte for the pixel counter */
196  count_dst = dst;
197  dst++;
198  }
199  last_colour = colour;
200  if (colour == 0) {
201  trans++;
202  } else {
203  pixels++;
204  *dst = colour;
205  dst++;
206  }
207  }
208 
209  if (count_dst != NULL) *count_dst = pixels;
210 
211  /* Write line-ending */
212  *dst = 0; dst++;
213  *dst = 0; dst++;
214  }
215  }
216 
217  uint size = dst - (byte *)temp_dst;
218 
219  /* Safety check, to make sure we guessed the size correctly */
220  assert(size < memory);
221 
222  /* Allocate the exact amount of memory we need */
223  Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size);
224 
225  dest_sprite->height = sprite->height;
226  dest_sprite->width = sprite->width;
227  dest_sprite->x_offs = sprite->x_offs;
228  dest_sprite->y_offs = sprite->y_offs;
229  memcpy(dest_sprite->data, temp_dst, size);
230 
231  return dest_sprite;
232 }
int left
The left offset in the &#39;dst&#39; in pixels to start drawing.
Definition: base.hpp:43
ZoomLevelByte zoom_max
maximum zoom out level
int height
The height in pixels that needs to be drawn to dst.
Definition: base.hpp:40
Perform transparency colour remapping.
Definition: base.hpp:22
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition: base.hpp:38
Data structure describing a sprite.
Definition: spritecache.h:18
byte data[]
Data, all zoomlevels.
int width
The width in pixels that needs to be drawn to dst.
Definition: base.hpp:39
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition: base.hpp:37
uint32 offset[ZOOM_LVL_COUNT]
Offsets (from .data) to streams for different zoom levels.
Definition of a common pixel in OpenTTD&#39;s realm.
SpriteType type
The sprite type.
Maximum zoom level.
Definition: zoom_type.h:48
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:22
Parameters related to blitting.
Definition: base.hpp:33
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
int pitch
The pitch of the destination buffer.
Definition: base.hpp:47
T * Allocate(size_t count)
Get buffer of at least count times T.
Definition: alloc_type.hpp:107
int16 x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
Perform a crash remapping.
Definition: base.hpp:23
byte data[]
Sprite data.
Definition: spritecache.h:23
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
Definition: alloc_type.hpp:89
Structure for passing information from the sprite loader to the blitter.
Perform remapping to a completely blackened sprite.
Definition: base.hpp:24
uint16 height
Height of the sprite.
Definition: spritecache.h:19
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:25
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
Draw an image to the screen, given an amount of params defined above.
ZoomLevelByte zoom_min
minimum zoom out level
uint8 m
Remap-channel.
uint16 width
Width of the sprite.
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
uint16 width
Width of the sprite.
Definition: spritecache.h:20
int top
The top offset in the &#39;dst&#39; in pixels to start drawing.
Definition: base.hpp:44
GUISettings gui
settings related to the GUI
const byte * remap
XXX – Temporary storage for remap array.
Definition: base.hpp:35
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition: base.hpp:34
Perform a colour remapping.
Definition: base.hpp:21
static FBlitter_8bppOptimized iFBlitter_8bppOptimized
Instantiation of the 8bpp optimised blitter factory.
Data stored about a (single) sprite.
uint16 height
Height of the sprite.
Factory for the 8bpp blitter optimised for speed.
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:21
void * dst
Destination buffer.
Definition: base.hpp:46
int16 y_offs
The y-offset of where the sprite will be drawn.
Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
Convert a sprite from the loader to our own format.
BlitterMode
The modes of blitting we can do.
Definition: base.hpp:19
The normal zoom level.
Definition: zoom_type.h:24
A sprite used for fonts.
Definition: gfx_type.h:300
An optimized 8 bpp blitter.
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:51