# 32bpp_optimized.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "../gfx_func.h"
    00015 #include "../core/math_func.hpp"
    00016 #include "32bpp_optimized.hpp"
    00017
    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 uint8  *src_n  = (const uint8  *)(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 += *(uint32 *)src_n;
    00044   }
    00045
    00046   /* skip lines in dst */
    00047   uint32 *dst = (uint32 *)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     uint32 *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 uint8 *src_n_ln = src_n + *(uint32 *)src_n;
    00062     src_n += 4;
    00063
    00064     /* we will end this line when we reach this point */
    00065     uint32 *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[m];
    00122                 if (r != 0) *dst = this->LookupColourInPalette(r);
    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[m];
    00135                 if (r != 0) *dst = ComposeColourPANoCheck(this->LookupColourInPalette(r), 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
    00217 static const SpriteLoader::Sprite *ResizeSprite(const SpriteLoader::Sprite *sprite_src, ZoomLevel zoom)
    00218 {
    00219   SpriteLoader::Sprite *sprite = MallocT<SpriteLoader::Sprite>(1);
    00220
    00221   if (zoom == ZOOM_LVL_NORMAL) {
    00222     memcpy(sprite, sprite_src, sizeof(*sprite));
    00223     uint size = sprite_src->height * sprite_src->width;
    00224     sprite->data = MallocT<SpriteLoader::CommonPixel>(size);
    00225     memcpy(sprite->data, sprite_src->data, size * sizeof(SpriteLoader::CommonPixel));
    00226     return sprite;
    00227   }
    00228
    00229   sprite->height = UnScaleByZoom(sprite_src->height, zoom);
    00230   sprite->width  = UnScaleByZoom(sprite_src->width,  zoom);
    00231   sprite->x_offs = UnScaleByZoom(sprite_src->x_offs, zoom);
    00232   sprite->y_offs = UnScaleByZoom(sprite_src->y_offs, zoom);
    00233
    00234   uint size = sprite->height * sprite->width;
    00235   SpriteLoader::CommonPixel *dst = sprite->data = CallocT<SpriteLoader::CommonPixel>(size);
    00236
    00237   const SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite_src->data;
    00238   const SpriteLoader::CommonPixel *src_end = src + sprite_src->height * sprite_src->width;
    00239
    00240   uint scaled_1 = ScaleByZoom(1, zoom);
    00241
    00242   for (uint y = 0; y < sprite->height; y++) {
    00243     if (src >= src_end) src = src_end - sprite_src->width;
    00244
    00245     const SpriteLoader::CommonPixel *src_ln = src + sprite_src->width * scaled_1;
    00246     for (uint x = 0; x < sprite->width; x++) {
    00247       if (src >= src_ln) src = src_ln - 1;
    00248       *dst = *src;
    00249       dst++;
    00250       src += scaled_1;
    00251     }
    00252
    00253     src = src_ln;
    00254   }
    00255
    00256   return sprite;
    00257 }
    00258
    00259 Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
    00260 {
    00261   /* streams of pixels (a, r, g, b channels)
    00262    *
    00263    * stored in separated stream so data are always aligned on 4B boundary */
    00264   Colour *dst_px_orig[ZOOM_LVL_COUNT];
    00265
    00266   /* interleaved stream of 'm' channel and 'n' channel
    00267    * 'n' is number if following pixels with the same alpha channel class
    00268    * there are 3 classes: 0, 255, others
    00269    *
    00270    * it has to be stored in one stream so fewer registers are used -
    00271    * x86 has problems with register allocation even with this solution */
    00272   uint8  *dst_n_orig[ZOOM_LVL_COUNT];
    00273
    00274   /* lengths of streams */
    00275   uint32 lengths[ZOOM_LVL_COUNT][2];
    00276
    00277   for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) {
    00278     const SpriteLoader::Sprite *src_orig = ResizeSprite(sprite, z);
    00279
    00280     uint size = src_orig->height * src_orig->width;
    00281
    00282     dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
    00283     dst_n_orig[z]  = CallocT<uint8>(size * 2 + src_orig->height * 4 * 2);
    00284
    00285     uint32 *dst_px_ln = (uint32 *)dst_px_orig[z];
    00286     uint32 *dst_n_ln  = (uint32 *)dst_n_orig[z];
    00287
    00288     const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
    00289
    00290     for (uint y = src_orig->height; y > 0; y--) {
    00291       Colour *dst_px = (Colour *)(dst_px_ln + 1);
    00292       uint8 *dst_n = (uint8 *)(dst_n_ln + 1);
    00293
    00294       uint8 *dst_len = dst_n++;
    00295
    00296       uint last = 3;
    00297       int len = 0;
    00298
    00299       for (uint x = src_orig->width; x > 0; x--) {
    00300         uint8 a = src->a;
    00301         uint t = a > 0 && a < 255 ? 1 : a;
    00302
    00303         if (last != t || len == 255) {
    00304           if (last != 3) {
    00305             *dst_len = len;
    00306             dst_len = dst_n++;
    00307           }
    00308           len = 0;
    00309         }
    00310
    00311         last = t;
    00312         len++;
    00313
    00314         if (a != 0) {
    00315           dst_px->a = a;
    00316           *dst_n = src->m;
    00317           if (src->m != 0) {
    00318             /* Pre-convert the mapping channel to a RGB value */
    00319             uint32 colour = this->LookupColourInPalette(src->m);
    00320             dst_px->r = GB(colour, 16, 8);
    00321             dst_px->g = GB(colour, 8,  8);
    00322             dst_px->b = GB(colour, 0,  8);
    00323           } else {
    00324             dst_px->r = src->r;
    00325             dst_px->g = src->g;
    00326             dst_px->b = src->b;
    00327           }
    00328           dst_px++;
    00329           dst_n++;
    00330         } else if (len == 1) {
    00331           dst_px++;
    00332           *dst_n = src->m;
    00333           dst_n++;
    00334         }
    00335
    00336         src++;
    00337       }
    00338
    00339       if (last != 3) {
    00340         *dst_len = len;
    00341       }
    00342
    00343       dst_px = (Colour *)AlignPtr(dst_px, 4);
    00344       dst_n  = (uint8 *)AlignPtr(dst_n, 4);
    00345
    00346       *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln;
    00347       *dst_n_ln  = (uint8 *)dst_n  - (uint8 *)dst_n_ln;
    00348
    00349       dst_px_ln = (uint32 *)dst_px;
    00350       dst_n_ln =  (uint32 *)dst_n;
    00351     }
    00352
    00353     lengths[z][0] = (byte *)dst_px_ln - (byte *)dst_px_orig[z]; // all are aligned to 4B boundary
    00354     lengths[z][1] = (byte *)dst_n_ln  - (byte *)dst_n_orig[z];
    00355
    00356     free(src_orig->data);
    00357     free((void *)src_orig);
    00358   }
    00359
    00360   uint len = 0; // total length of data
    00361   for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) {
    00362     len += lengths[z][0] + lengths[z][1];
    00363   }
    00364
    00365   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(SpriteData) + len);
    00366
    00367   dest_sprite->height = sprite->height;
    00368   dest_sprite->width  = sprite->width;
    00369   dest_sprite->x_offs = sprite->x_offs;
    00370   dest_sprite->y_offs = sprite->y_offs;
    00371
    00372   SpriteData *dst = (SpriteData *)dest_sprite->data;
    00373
    00374   for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) {
    00375     dst->offset[z][0] = z == ZOOM_LVL_BEGIN ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
    00376     dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
    00377
    00378     memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
    00379     memcpy(dst->data + dst->offset[z][1], dst_n_orig[z],  lengths[z][1]);
    00380
    00381     free(dst_px_orig[z]);
    00382     free(dst_n_orig[z]);
    00383   }
    00384
    00385   return dest_sprite;
    00386 }