# 32bpp_optimized.cpp

Go to the documentation of this file.

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