# 32bpp_optimized.cpp

Go to the documentation of this file.

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