src/video/SDL_blit_slow.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 02 Dec 2008 17:14:04 +0000
changeset 2824 4dba7aa7ea77
parent 2256 e893d24ad8db
child 2859 99210400e8b9
permissions -rw-r--r--
Added slow but complete blit fallback
Don't try to RLE encode surfaces that have alpha channel and alpha modulation
Don't turn on blending when converting an RGB surface to RGBA format
Do turn on blending when converting colorkey to alpha channel
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_video.h"
    25 #include "SDL_blit.h"
    26 
    27 /* The ONE TRUE BLITTER
    28  * This puppy has to handle all the unoptimized cases - yes, it's slow.
    29  */
    30 void
    31 SDL_Blit_Slow(SDL_BlitInfo * info)
    32 {
    33     const int flags = info->flags;
    34     const Uint32 modulateR = info->r;
    35     const Uint32 modulateG = info->g;
    36     const Uint32 modulateB = info->b;
    37     const Uint32 modulateA = info->a;
    38     Uint32 srcpixel;
    39     Uint32 srcR, srcG, srcB, srcA;
    40     Uint32 dstpixel;
    41     Uint32 dstR, dstG, dstB, dstA;
    42     int srcy, srcx;
    43     int posy, posx;
    44     int incy, incx;
    45     SDL_PixelFormat *src_fmt = info->src_fmt;
    46     SDL_PixelFormat *dst_fmt = info->dst_fmt;
    47     int srcbpp = src_fmt->BytesPerPixel;
    48     int dstbpp = dst_fmt->BytesPerPixel;
    49 
    50     srcy = 0;
    51     posy = 0;
    52     incy = (info->src_h << 16) / info->dst_h;
    53     incx = (info->src_w << 16) / info->dst_w;
    54 
    55     while (info->dst_h--) {
    56         Uint8 *src;
    57         Uint8 *dst = (Uint8 *) info->dst;
    58         int n = info->dst_w;
    59         srcx = -1;
    60         posx = 0x10000L;
    61         while (posy >= 0x10000L) {
    62             ++srcy;
    63             posy -= 0x10000L;
    64         }
    65         while (n--) {
    66             if (posx >= 0x10000L) {
    67                 while (posx >= 0x10000L) {
    68                     ++srcx;
    69                     posx -= 0x10000L;
    70                 }
    71                 src =
    72                     (info->src + (srcy * info->src_pitch) + (srcx * srcbpp));
    73             }
    74             if (src_fmt->Amask) {
    75                 DISEMBLE_RGBA(src, srcbpp, src_fmt, srcpixel, srcR, srcG,
    76                               srcB, srcA);
    77             } else {
    78                 DISEMBLE_RGB(src, srcbpp, src_fmt, srcpixel, srcR, srcG,
    79                              srcB);
    80                 srcA = 0xFF;
    81             }
    82             if (flags & SDL_COPY_COLORKEY) {
    83                 /* srcpixel isn't set for 24 bpp */
    84                 if (srcbpp == 24) {
    85                     srcpixel = (srcR << src_fmt->Rshift) |
    86                         (srcG << src_fmt->Gshift) | (srcB << src_fmt->Bshift);
    87                 }
    88                 if (srcpixel == info->colorkey) {
    89                     posx += incx;
    90                     dst += dstbpp;
    91                     continue;
    92                 }
    93             }
    94             if (dst_fmt->Amask) {
    95                 DISEMBLE_RGBA(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG,
    96                               dstB, dstA);
    97             } else {
    98                 DISEMBLE_RGB(dst, dstbpp, dst_fmt, dstpixel, dstR, dstG,
    99                              dstB);
   100                 dstA = 0xFF;
   101             }
   102 
   103             if (flags & SDL_COPY_MODULATE_COLOR) {
   104                 srcR = (srcR * modulateR) / 255;
   105                 srcG = (srcG * modulateG) / 255;
   106                 srcB = (srcB * modulateB) / 255;
   107             }
   108             if (flags & SDL_COPY_MODULATE_ALPHA) {
   109                 srcA = (srcA * modulateA) / 255;
   110             }
   111             if (flags & (SDL_COPY_BLEND | SDL_COPY_ADD)) {
   112                 /* This goes away if we ever use premultiplied alpha */
   113                 if (srcA < 255) {
   114                     srcR = (srcR * srcA) / 255;
   115                     srcG = (srcG * srcA) / 255;
   116                     srcB = (srcB * srcA) / 255;
   117                 }
   118             }
   119             switch (flags &
   120                     (SDL_COPY_MASK | SDL_COPY_BLEND | SDL_COPY_ADD |
   121                      SDL_COPY_MOD)) {
   122             case 0:
   123                 dstR = srcR;
   124                 dstG = srcG;
   125                 dstB = srcB;
   126                 dstA = srcA;
   127                 break;
   128             case SDL_COPY_MASK:
   129                 if (srcA) {
   130                     dstR = srcR;
   131                     dstG = srcG;
   132                     dstB = srcB;
   133                 }
   134                 break;
   135             case SDL_COPY_BLEND:
   136                 dstR = srcR + ((255 - srcA) * dstR) / 255;
   137                 dstG = srcG + ((255 - srcA) * dstG) / 255;
   138                 dstB = srcB + ((255 - srcA) * dstB) / 255;
   139                 break;
   140             case SDL_COPY_ADD:
   141                 dstR = srcR + dstR;
   142                 if (dstR > 255)
   143                     dstR = 255;
   144                 dstG = srcG + dstG;
   145                 if (dstG > 255)
   146                     dstG = 255;
   147                 dstB = srcB + dstB;
   148                 if (dstB > 255)
   149                     dstB = 255;
   150                 break;
   151             case SDL_COPY_MOD:
   152                 dstR = (srcR * dstR) / 255;
   153                 dstG = (srcG * dstG) / 255;
   154                 dstB = (srcB * dstB) / 255;
   155                 break;
   156             }
   157             if (dst_fmt->Amask) {
   158                 ASSEMBLE_RGBA(dst, dstbpp, dst_fmt, dstR, dstG, dstB, dstA);
   159             } else {
   160                 ASSEMBLE_RGB(dst, dstbpp, dst_fmt, dstR, dstG, dstB);
   161             }
   162             posx += incx;
   163             dst += dstbpp;
   164         }
   165         posy += incy;
   166         info->dst += info->dst_pitch;
   167     }
   168 }
   169 
   170 /* vi: set ts=4 sw=4 expandtab: */