# 32bpp_optimized.cpp

Go to the documentation of this file.

    00001 /* $Id: 32bpp_optimized.cpp 24111 2012-04-10 20:16:51Z rubidium $ */
    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_TRANSPARENT:
    00145           /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
    00146            *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
    00147            *  we produce a result the newgrf maker didn't expect ;) */
    00148
    00149           /* Make the current colour a bit more black, so it looks like this image is transparent */
    00150           src_n += n;
    00151           if (src_px->a == 255) {
    00152             src_px += n;
    00153             do {
    00154               *dst = MakeTransparent(*dst, 3, 4);
    00155               dst++;
    00156             } while (--n != 0);
    00157           } else {
    00158             do {
    00159               *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
    00160               dst++;
    00161               src_px++;
    00162             } while (--n != 0);
    00163           }
    00164           break;
    00165
    00166         default:
    00167           if (src_px->a == 255) {
    00168             /* faster than memcpy(), n is usually low */
    00169             src_n += n;
    00170             do {
    00171               *dst = src_px->data;
    00172               dst++;
    00173               src_px++;
    00174             } while (--n != 0);
    00175           } else {
    00176             src_n += n;
    00177             do {
    00178               *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
    00179               dst++;
    00180               src_px++;
    00181             } while (--n != 0);
    00182           }
    00183           break;
    00184       }
    00185     }
    00186
    00187     dst = dst_ln;
    00188     src_px = src_px_ln;
    00189     src_n  = src_n_ln;
    00190   }
    00191 }
    00192
    00200 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
    00201 {
    00202   switch (mode) {
    00203     default: NOT_REACHED();
    00204     case BM_NORMAL:       Draw<BM_NORMAL>      (bp, zoom); return;
    00205     case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
    00206     case BM_TRANSPARENT:  Draw<BM_TRANSPARENT> (bp, zoom); return;
    00207   }
    00208 }
    00209
    00210 Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
    00211 {
    00212   /* streams of pixels (a, r, g, b channels)
    00213    *
    00214    * stored in separated stream so data are always aligned on 4B boundary */
    00215   Colour *dst_px_orig[ZOOM_LVL_COUNT];
    00216
    00217   /* interleaved stream of 'm' channel and 'n' channel
    00218    * 'n' is number of following pixels with the same alpha channel class
    00219    * there are 3 classes: 0, 255, others
    00220    *
    00221    * it has to be stored in one stream so fewer registers are used -
    00222    * x86 has problems with register allocation even with this solution */
    00223   uint16 *dst_n_orig[ZOOM_LVL_COUNT];
    00224
    00225   /* lengths of streams */
    00226   uint32 lengths[ZOOM_LVL_COUNT][2];
    00227
    00228   ZoomLevel zoom_min;
    00229   ZoomLevel zoom_max;
    00230
    00231   if (sprite->type == ST_FONT) {
    00232     zoom_min = ZOOM_LVL_NORMAL;
    00233     zoom_max = ZOOM_LVL_NORMAL;
    00234   } else {
    00235     zoom_min = _settings_client.gui.zoom_min;
    00236     zoom_max = _settings_client.gui.zoom_max;
    00237     if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
    00238   }
    00239
    00240   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
    00241     const SpriteLoader::Sprite *src_orig = &sprite[z];
    00242
    00243     uint size = src_orig->height * src_orig->width;
    00244
    00245     dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
    00246     dst_n_orig[z]  = CallocT<uint16>(size * 2 + src_orig->height * 4 * 2);
    00247
    00248     uint32 *dst_px_ln = (uint32 *)dst_px_orig[z];
    00249     uint32 *dst_n_ln  = (uint32 *)dst_n_orig[z];
    00250
    00251     const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
    00252
    00253     for (uint y = src_orig->height; y > 0; y--) {
    00254       Colour *dst_px = (Colour *)(dst_px_ln + 1);
    00255       uint16 *dst_n = (uint16 *)(dst_n_ln + 1);
    00256
    00257       uint16 *dst_len = dst_n++;
    00258
    00259       uint last = 3;
    00260       int len = 0;
    00261
    00262       for (uint x = src_orig->width; x > 0; x--) {
    00263         uint8 a = src->a;
    00264         uint t = a > 0 && a < 255 ? 1 : a;
    00265
    00266         if (last != t || len == 65535) {
    00267           if (last != 3) {
    00268             *dst_len = len;
    00269             dst_len = dst_n++;
    00270           }
    00271           len = 0;
    00272         }
    00273
    00274         last = t;
    00275         len++;
    00276
    00277         if (a != 0) {
    00278           dst_px->a = a;
    00279           *dst_n = src->m;
    00280           if (src->m != 0) {
    00281             /* Get brightest value */
    00282             uint8 rgb_max = max(src->r, max(src->g, src->b));
    00283
    00284             /* Black pixel (8bpp or old 32bpp image), so use default value */
    00285             if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
    00286             *dst_n |= rgb_max << 8;
    00287
    00288             /* Pre-convert the mapping channel to a RGB value */
    00289             Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max);
    00290             dst_px->r = colour.r;
    00291             dst_px->g = colour.g;
    00292             dst_px->b = colour.b;
    00293           } else {
    00294             dst_px->r = src->r;
    00295             dst_px->g = src->g;
    00296             dst_px->b = src->b;
    00297           }
    00298           dst_px++;
    00299           dst_n++;
    00300         } else if (len == 1) {
    00301           dst_px++;
    00302           *dst_n = src->m;
    00303           dst_n++;
    00304         }
    00305
    00306         src++;
    00307       }
    00308
    00309       if (last != 3) {
    00310         *dst_len = len;
    00311       }
    00312
    00313       dst_px = (Colour *)AlignPtr(dst_px, 4);
    00314       dst_n  = (uint16 *)AlignPtr(dst_n, 4);
    00315
    00316       *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln;
    00317       *dst_n_ln  = (uint8 *)dst_n  - (uint8 *)dst_n_ln;
    00318
    00319       dst_px_ln = (uint32 *)dst_px;
    00320       dst_n_ln =  (uint32 *)dst_n;
    00321     }
    00322
    00323     lengths[z][0] = (byte *)dst_px_ln - (byte *)dst_px_orig[z]; // all are aligned to 4B boundary
    00324     lengths[z][1] = (byte *)dst_n_ln  - (byte *)dst_n_orig[z];
    00325   }
    00326
    00327   uint len = 0; // total length of data
    00328   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
    00329     len += lengths[z][0] + lengths[z][1];
    00330   }
    00331
    00332   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(SpriteData) + len);
    00333
    00334   dest_sprite->height = sprite->height;
    00335   dest_sprite->width  = sprite->width;
    00336   dest_sprite->x_offs = sprite->x_offs;
    00337   dest_sprite->y_offs = sprite->y_offs;
    00338
    00339   SpriteData *dst = (SpriteData *)dest_sprite->data;
    00340   memset(dst, 0, sizeof(*dst));
    00341
    00342   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
    00343     dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
    00344     dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
    00345
    00346     memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
    00347     memcpy(dst->data + dst->offset[z][1], dst_n_orig[z],  lengths[z][1]);
    00348
    00349     free(dst_px_orig[z]);
    00350     free(dst_n_orig[z]);
    00351   }
    00352
    00353   return dest_sprite;
    00354 }