32bpp_optimized.cpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_optimized.cpp 26541 2014-04-29 18:18:52Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../zoom_func.h"
00014 #include "../settings_type.h"
00015 #include "32bpp_optimized.hpp"
00016 
00018 static FBlitter_32bppOptimized iFBlitter_32bppOptimized;
00019 
00027 template <BlitterMode mode>
00028 inline void Blitter_32bppOptimized::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
00029 {
00030   const SpriteData *src = (const SpriteData *)bp->sprite;
00031 
00032   /* src_px : each line begins with uint32 n = 'number of bytes in this line',
00033    *          then n times is the Colour struct for this line */
00034   const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]);
00035   /* src_n  : each line begins with uint32 n = 'number of bytes in this line',
00036    *          then interleaved stream of 'm' and 'n' channels. 'm' is remap,
00037    *          'n' is number of bytes with the same alpha channel class */
00038   const uint16 *src_n  = (const uint16 *)(src->data + src->offset[zoom][1]);
00039 
00040   /* skip upper lines in src_px and src_n */
00041   for (uint i = bp->skip_top; i != 0; i--) {
00042     src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
00043     src_n = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
00044   }
00045 
00046   /* skip lines in dst */
00047   Colour *dst = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
00048 
00049   /* store so we don't have to access it via bp everytime (compiler assumes pointer aliasing) */
00050   const byte *remap = bp->remap;
00051 
00052   for (int y = 0; y < bp->height; y++) {
00053     /* next dst line begins here */
00054     Colour *dst_ln = dst + bp->pitch;
00055 
00056     /* next src line begins here */
00057     const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
00058     src_px++;
00059 
00060     /* next src_n line begins here */
00061     const uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
00062     src_n += 2;
00063 
00064     /* we will end this line when we reach this point */
00065     Colour *dst_end = dst + bp->skip_left;
00066 
00067     /* number of pixels with the same aplha channel class */
00068     uint n;
00069 
00070     while (dst < dst_end) {
00071       n = *src_n++;
00072 
00073       if (src_px->a == 0) {
00074         dst += n;
00075         src_px ++;
00076         src_n++;
00077       } else {
00078         if (dst + n > dst_end) {
00079           uint d = dst_end - dst;
00080           src_px += d;
00081           src_n += d;
00082 
00083           dst = dst_end - bp->skip_left;
00084           dst_end = dst + bp->width;
00085 
00086           n = min<uint>(n - d, (uint)bp->width);
00087           goto draw;
00088         }
00089         dst += n;
00090         src_px += n;
00091         src_n += n;
00092       }
00093     }
00094 
00095     dst -= bp->skip_left;
00096     dst_end -= bp->skip_left;
00097 
00098     dst_end += bp->width;
00099 
00100     while (dst < dst_end) {
00101       n = min<uint>(*src_n++, (uint)(dst_end - dst));
00102 
00103       if (src_px->a == 0) {
00104         dst += n;
00105         src_px++;
00106         src_n++;
00107         continue;
00108       }
00109 
00110       draw:;
00111 
00112       switch (mode) {
00113         case BM_COLOUR_REMAP:
00114           if (src_px->a == 255) {
00115             do {
00116               uint m = *src_n;
00117               /* In case the m-channel is zero, do not remap this pixel in any way */
00118               if (m == 0) {
00119                 *dst = src_px->data;
00120               } else {
00121                 uint r = remap[GB(m, 0, 8)];
00122                 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
00123               }
00124               dst++;
00125               src_px++;
00126               src_n++;
00127             } while (--n != 0);
00128           } else {
00129             do {
00130               uint m = *src_n;
00131               if (m == 0) {
00132                 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
00133               } else {
00134                 uint r = remap[GB(m, 0, 8)];
00135                 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
00136               }
00137               dst++;
00138               src_px++;
00139               src_n++;
00140             } while (--n != 0);
00141           }
00142           break;
00143 
00144         case BM_CRASH_REMAP:
00145           if (src_px->a == 255) {
00146             do {
00147               uint m = *src_n;
00148               if (m == 0) {
00149                 uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
00150                 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
00151               } else {
00152                 uint r = remap[GB(m, 0, 8)];
00153                 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
00154               }
00155               dst++;
00156               src_px++;
00157               src_n++;
00158             } while (--n != 0);
00159           } else {
00160             do {
00161               uint m = *src_n;
00162               if (m == 0) {
00163                 if (src_px->a != 0) {
00164                   uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
00165                   *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
00166                 }
00167               } else {
00168                 uint r = remap[GB(m, 0, 8)];
00169                 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
00170               }
00171               dst++;
00172               src_px++;
00173               src_n++;
00174             } while (--n != 0);
00175           }
00176           break;
00177 
00178         case BM_TRANSPARENT:
00179           /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
00180            *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
00181            *  we produce a result the newgrf maker didn't expect ;) */
00182 
00183           /* Make the current colour a bit more black, so it looks like this image is transparent */
00184           src_n += n;
00185           if (src_px->a == 255) {
00186             src_px += n;
00187             do {
00188               *dst = MakeTransparent(*dst, 3, 4);
00189               dst++;
00190             } while (--n != 0);
00191           } else {
00192             do {
00193               *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
00194               dst++;
00195               src_px++;
00196             } while (--n != 0);
00197           }
00198           break;
00199 
00200         default:
00201           if (src_px->a == 255) {
00202             /* faster than memcpy(), n is usually low */
00203             src_n += n;
00204             do {
00205               *dst = src_px->data;
00206               dst++;
00207               src_px++;
00208             } while (--n != 0);
00209           } else {
00210             src_n += n;
00211             do {
00212               *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
00213               dst++;
00214               src_px++;
00215             } while (--n != 0);
00216           }
00217           break;
00218       }
00219     }
00220 
00221     dst = dst_ln;
00222     src_px = src_px_ln;
00223     src_n  = src_n_ln;
00224   }
00225 }
00226 
00234 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
00235 {
00236   switch (mode) {
00237     default: NOT_REACHED();
00238     case BM_NORMAL:       Draw<BM_NORMAL>      (bp, zoom); return;
00239     case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
00240     case BM_TRANSPARENT:  Draw<BM_TRANSPARENT> (bp, zoom); return;
00241     case BM_CRASH_REMAP:  Draw<BM_CRASH_REMAP> (bp, zoom); return;
00242   }
00243 }
00244 
00245 Sprite *Blitter_32bppOptimized::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
00246 {
00247   /* streams of pixels (a, r, g, b channels)
00248    *
00249    * stored in separated stream so data are always aligned on 4B boundary */
00250   Colour *dst_px_orig[ZOOM_LVL_COUNT];
00251 
00252   /* interleaved stream of 'm' channel and 'n' channel
00253    * 'n' is number of following pixels with the same alpha channel class
00254    * there are 3 classes: 0, 255, others
00255    *
00256    * it has to be stored in one stream so fewer registers are used -
00257    * x86 has problems with register allocation even with this solution */
00258   uint16 *dst_n_orig[ZOOM_LVL_COUNT];
00259 
00260   /* lengths of streams */
00261   uint32 lengths[ZOOM_LVL_COUNT][2];
00262 
00263   ZoomLevel zoom_min;
00264   ZoomLevel zoom_max;
00265 
00266   if (sprite->type == ST_FONT) {
00267     zoom_min = ZOOM_LVL_NORMAL;
00268     zoom_max = ZOOM_LVL_NORMAL;
00269   } else {
00270     zoom_min = _settings_client.gui.zoom_min;
00271     zoom_max = _settings_client.gui.zoom_max;
00272     if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
00273   }
00274 
00275   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
00276     const SpriteLoader::Sprite *src_orig = &sprite[z];
00277 
00278     uint size = src_orig->height * src_orig->width;
00279 
00280     dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
00281     dst_n_orig[z]  = CallocT<uint16>(size * 2 + src_orig->height * 4 * 2);
00282 
00283     uint32 *dst_px_ln = (uint32 *)dst_px_orig[z];
00284     uint32 *dst_n_ln  = (uint32 *)dst_n_orig[z];
00285 
00286     const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
00287 
00288     for (uint y = src_orig->height; y > 0; y--) {
00289       Colour *dst_px = (Colour *)(dst_px_ln + 1);
00290       uint16 *dst_n = (uint16 *)(dst_n_ln + 1);
00291 
00292       uint16 *dst_len = dst_n++;
00293 
00294       uint last = 3;
00295       int len = 0;
00296 
00297       for (uint x = src_orig->width; x > 0; x--) {
00298         uint8 a = src->a;
00299         uint t = a > 0 && a < 255 ? 1 : a;
00300 
00301         if (last != t || len == 65535) {
00302           if (last != 3) {
00303             *dst_len = len;
00304             dst_len = dst_n++;
00305           }
00306           len = 0;
00307         }
00308 
00309         last = t;
00310         len++;
00311 
00312         if (a != 0) {
00313           dst_px->a = a;
00314           *dst_n = src->m;
00315           if (src->m != 0) {
00316             /* Get brightest value */
00317             uint8 rgb_max = max(src->r, max(src->g, src->b));
00318 
00319             /* Black pixel (8bpp or old 32bpp image), so use default value */
00320             if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
00321             *dst_n |= rgb_max << 8;
00322 
00323             /* Pre-convert the mapping channel to a RGB value */
00324             Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max);
00325             dst_px->r = colour.r;
00326             dst_px->g = colour.g;
00327             dst_px->b = colour.b;
00328           } else {
00329             dst_px->r = src->r;
00330             dst_px->g = src->g;
00331             dst_px->b = src->b;
00332           }
00333           dst_px++;
00334           dst_n++;
00335         } else if (len == 1) {
00336           dst_px++;
00337           *dst_n = src->m;
00338           dst_n++;
00339         }
00340 
00341         src++;
00342       }
00343 
00344       if (last != 3) {
00345         *dst_len = len;
00346       }
00347 
00348       dst_px = (Colour *)AlignPtr(dst_px, 4);
00349       dst_n  = (uint16 *)AlignPtr(dst_n, 4);
00350 
00351       *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln;
00352       *dst_n_ln  = (uint8 *)dst_n  - (uint8 *)dst_n_ln;
00353 
00354       dst_px_ln = (uint32 *)dst_px;
00355       dst_n_ln =  (uint32 *)dst_n;
00356     }
00357 
00358     lengths[z][0] = (byte *)dst_px_ln - (byte *)dst_px_orig[z]; // all are aligned to 4B boundary
00359     lengths[z][1] = (byte *)dst_n_ln  - (byte *)dst_n_orig[z];
00360   }
00361 
00362   uint len = 0; // total length of data
00363   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
00364     len += lengths[z][0] + lengths[z][1];
00365   }
00366 
00367   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(SpriteData) + len);
00368 
00369   dest_sprite->height = sprite->height;
00370   dest_sprite->width  = sprite->width;
00371   dest_sprite->x_offs = sprite->x_offs;
00372   dest_sprite->y_offs = sprite->y_offs;
00373 
00374   SpriteData *dst = (SpriteData *)dest_sprite->data;
00375   memset(dst, 0, sizeof(*dst));
00376 
00377   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
00378     dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
00379     dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
00380 
00381     memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
00382     memcpy(dst->data + dst->offset[z][1], dst_n_orig[z],  lengths[z][1]);
00383 
00384     free(dst_px_orig[z]);
00385     free(dst_n_orig[z]);
00386   }
00387 
00388   return dest_sprite;
00389 }