src/video/SDL_blendline.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 Dec 2008 08:55:06 +0000
changeset 2899 a0c837a16e4c
parent 2898 e40448bc7727
child 2900 3a9636c83849
permissions -rw-r--r--
Added ARGB optimized case for Mac OS X
slouken@2888
     1
/*
slouken@2888
     2
    SDL - Simple DirectMedia Layer
slouken@2888
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@2888
     4
slouken@2888
     5
    This library is free software; you can redistribute it and/or
slouken@2888
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2888
     7
    License as published by the Free Software Foundation; either
slouken@2888
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2888
     9
slouken@2888
    10
    This library is distributed in the hope that it will be useful,
slouken@2888
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2888
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2888
    13
    Lesser General Public License for more details.
slouken@2888
    14
slouken@2888
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2888
    16
    License along with this library; if not, write to the Free Software
slouken@2888
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2888
    18
slouken@2888
    19
    Sam Lantinga
slouken@2888
    20
    slouken@libsdl.org
slouken@2888
    21
*/
slouken@2888
    22
#include "SDL_config.h"
slouken@2888
    23
slouken@2898
    24
#include "SDL_draw.h"
slouken@2898
    25
slouken@2898
    26
static int
slouken@2898
    27
SDL_BlendLine_RGB555(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@2898
    28
                     int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2898
    29
{
slouken@2898
    30
    unsigned inva = 0xff - a;
slouken@2888
    31
slouken@2898
    32
    switch (blendMode) {
slouken@2898
    33
    case SDL_BLENDMODE_BLEND:
slouken@2898
    34
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_BLEND_RGB555);
slouken@2898
    35
        break;
slouken@2898
    36
    case SDL_BLENDMODE_ADD:
slouken@2898
    37
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ADD_RGB555);
slouken@2898
    38
        break;
slouken@2898
    39
    case SDL_BLENDMODE_MOD:
slouken@2898
    40
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_MOD_RGB555);
slouken@2898
    41
        break;
slouken@2898
    42
    default:
slouken@2898
    43
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_RGB555);
slouken@2898
    44
        break;
slouken@2898
    45
    }
slouken@2898
    46
    return 0;
slouken@2898
    47
}
slouken@2896
    48
slouken@2898
    49
static int
slouken@2898
    50
SDL_BlendLine_RGB565(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@2898
    51
                     int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2898
    52
{
slouken@2898
    53
    unsigned inva = 0xff - a;
slouken@2896
    54
slouken@2898
    55
    switch (blendMode) {
slouken@2898
    56
    case SDL_BLENDMODE_BLEND:
slouken@2898
    57
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_BLEND_RGB565);
slouken@2898
    58
        break;
slouken@2898
    59
    case SDL_BLENDMODE_ADD:
slouken@2898
    60
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ADD_RGB565);
slouken@2898
    61
        break;
slouken@2898
    62
    case SDL_BLENDMODE_MOD:
slouken@2898
    63
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_MOD_RGB565);
slouken@2898
    64
        break;
slouken@2898
    65
    default:
slouken@2898
    66
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_RGB565);
slouken@2898
    67
        break;
slouken@2898
    68
    }
slouken@2898
    69
    return 0;
slouken@2898
    70
}
slouken@2898
    71
slouken@2898
    72
static int
slouken@2898
    73
SDL_BlendLine_RGB888(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@2898
    74
                     int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2898
    75
{
slouken@2898
    76
    unsigned inva = 0xff - a;
slouken@2898
    77
slouken@2898
    78
    switch (blendMode) {
slouken@2898
    79
    case SDL_BLENDMODE_BLEND:
slouken@2898
    80
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_BLEND_RGB888);
slouken@2898
    81
        break;
slouken@2898
    82
    case SDL_BLENDMODE_ADD:
slouken@2898
    83
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ADD_RGB888);
slouken@2898
    84
        break;
slouken@2898
    85
    case SDL_BLENDMODE_MOD:
slouken@2898
    86
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_MOD_RGB888);
slouken@2898
    87
        break;
slouken@2898
    88
    default:
slouken@2898
    89
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_RGB888);
slouken@2898
    90
        break;
slouken@2898
    91
    }
slouken@2898
    92
    return 0;
slouken@2896
    93
}
slouken@2896
    94
slouken@2898
    95
static int
slouken@2899
    96
SDL_BlendLine_ARGB8888(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@2899
    97
                       int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2899
    98
{
slouken@2899
    99
    unsigned inva = 0xff - a;
slouken@2899
   100
slouken@2899
   101
    switch (blendMode) {
slouken@2899
   102
    case SDL_BLENDMODE_BLEND:
slouken@2899
   103
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_BLEND_ARGB8888);
slouken@2899
   104
        break;
slouken@2899
   105
    case SDL_BLENDMODE_ADD:
slouken@2899
   106
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ADD_ARGB8888);
slouken@2899
   107
        break;
slouken@2899
   108
    case SDL_BLENDMODE_MOD:
slouken@2899
   109
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_MOD_ARGB8888);
slouken@2899
   110
        break;
slouken@2899
   111
    default:
slouken@2899
   112
        BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY_ARGB8888);
slouken@2899
   113
        break;
slouken@2899
   114
    }
slouken@2899
   115
    return 0;
slouken@2899
   116
}
slouken@2899
   117
slouken@2899
   118
static int
slouken@2898
   119
SDL_BlendLine_RGB(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@2898
   120
                  int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2898
   121
{
slouken@2898
   122
    SDL_PixelFormat *fmt = dst->format;
slouken@2898
   123
    unsigned inva = 0xff - a;
slouken@2896
   124
slouken@2898
   125
    switch (fmt->BytesPerPixel) {
slouken@2898
   126
    case 2:
slouken@2898
   127
        switch (blendMode) {
slouken@2898
   128
        case SDL_BLENDMODE_BLEND:
slouken@2898
   129
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_BLEND_RGB);
slouken@2898
   130
            break;
slouken@2898
   131
        case SDL_BLENDMODE_ADD:
slouken@2898
   132
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_ADD_RGB);
slouken@2898
   133
            break;
slouken@2898
   134
        case SDL_BLENDMODE_MOD:
slouken@2898
   135
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_MOD_RGB);
slouken@2898
   136
            break;
slouken@2898
   137
        default:
slouken@2898
   138
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY2_RGB);
slouken@2898
   139
            break;
slouken@2898
   140
        }
slouken@2898
   141
        return 0;
slouken@2898
   142
    case 4:
slouken@2898
   143
        switch (blendMode) {
slouken@2898
   144
        case SDL_BLENDMODE_BLEND:
slouken@2898
   145
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_BLEND_RGB);
slouken@2898
   146
            break;
slouken@2898
   147
        case SDL_BLENDMODE_ADD:
slouken@2898
   148
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_ADD_RGB);
slouken@2898
   149
            break;
slouken@2898
   150
        case SDL_BLENDMODE_MOD:
slouken@2898
   151
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_MOD_RGB);
slouken@2898
   152
            break;
slouken@2898
   153
        default:
slouken@2898
   154
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_RGB);
slouken@2898
   155
            break;
slouken@2898
   156
        }
slouken@2898
   157
        return 0;
slouken@2898
   158
    default:
slouken@2898
   159
        SDL_Unsupported();
slouken@2898
   160
        return -1;
slouken@2898
   161
    }
slouken@2898
   162
}
slouken@2896
   163
slouken@2898
   164
static int
slouken@2898
   165
SDL_BlendLine_RGBA(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@2898
   166
                   int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2898
   167
{
slouken@2898
   168
    SDL_PixelFormat *fmt = dst->format;
slouken@2898
   169
    unsigned inva = 0xff - a;
slouken@2896
   170
slouken@2898
   171
    switch (fmt->BytesPerPixel) {
slouken@2898
   172
    case 4:
slouken@2898
   173
        switch (blendMode) {
slouken@2898
   174
        case SDL_BLENDMODE_BLEND:
slouken@2898
   175
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_BLEND_RGBA);
slouken@2898
   176
            break;
slouken@2898
   177
        case SDL_BLENDMODE_ADD:
slouken@2898
   178
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_ADD_RGBA);
slouken@2898
   179
            break;
slouken@2898
   180
        case SDL_BLENDMODE_MOD:
slouken@2898
   181
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_MOD_RGBA);
slouken@2898
   182
            break;
slouken@2898
   183
        default:
slouken@2898
   184
            BRESENHAM(x1, y1, x2, y2, DRAW_SETPIXELXY4_RGBA);
slouken@2898
   185
            break;
slouken@2898
   186
        }
slouken@2898
   187
        return 0;
slouken@2898
   188
    default:
slouken@2898
   189
        SDL_Unsupported();
slouken@2898
   190
        return -1;
slouken@2898
   191
    }
slouken@2898
   192
}
slouken@2888
   193
slouken@2888
   194
int
slouken@2888
   195
SDL_BlendLine(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@2888
   196
              int blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2888
   197
{
slouken@2898
   198
    SDL_PixelFormat *fmt = dst->format;
slouken@2898
   199
slouken@2888
   200
    /* This function doesn't work on surfaces < 8 bpp */
slouken@2888
   201
    if (dst->format->BitsPerPixel < 8) {
slouken@2888
   202
        SDL_SetError("SDL_BlendLine(): Unsupported surface format");
slouken@2888
   203
        return (-1);
slouken@2888
   204
    }
slouken@2888
   205
slouken@2888
   206
    /* Perform clipping */
slouken@2888
   207
    /* FIXME
slouken@2888
   208
       if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
slouken@2888
   209
       return (0);
slouken@2888
   210
       }
slouken@2888
   211
     */
slouken@2888
   212
slouken@2896
   213
    if ((blendMode == SDL_BLENDMODE_BLEND)
slouken@2896
   214
        || (blendMode == SDL_BLENDMODE_ADD)) {
slouken@2898
   215
        r = DRAW_MUL(r, a);
slouken@2898
   216
        g = DRAW_MUL(g, a);
slouken@2898
   217
        b = DRAW_MUL(b, a);
slouken@2896
   218
    }
slouken@2898
   219
slouken@2898
   220
    switch (fmt->BitsPerPixel) {
slouken@2896
   221
    case 15:
slouken@2898
   222
        switch (fmt->Rmask) {
slouken@2898
   223
        case 0x7C00:
slouken@2898
   224
            return SDL_BlendLine_RGB555(dst, x1, y1, x2, y2, blendMode, r, g,
slouken@2898
   225
                                        b, a);
slouken@2896
   226
        }
slouken@2896
   227
        break;
slouken@2896
   228
    case 16:
slouken@2898
   229
        switch (fmt->Rmask) {
slouken@2898
   230
        case 0xF800:
slouken@2898
   231
            return SDL_BlendLine_RGB565(dst, x1, y1, x2, y2, blendMode, r, g,
slouken@2898
   232
                                        b, a);
slouken@2896
   233
        }
slouken@2896
   234
        break;
slouken@2896
   235
    case 32:
slouken@2898
   236
        switch (fmt->Rmask) {
slouken@2898
   237
        case 0x00FF0000:
slouken@2898
   238
            if (!fmt->Amask) {
slouken@2898
   239
                return SDL_BlendLine_RGB888(dst, x1, y1, x2, y2, blendMode, r,
slouken@2898
   240
                                            g, b, a);
slouken@2899
   241
            } else {
slouken@2899
   242
                return SDL_BlendLine_ARGB8888(dst, x1, y1, x2, y2, blendMode,
slouken@2899
   243
                                              r, g, b, a);
slouken@2898
   244
            }
slouken@2896
   245
            break;
slouken@2896
   246
        }
slouken@2898
   247
    default:
slouken@2896
   248
        break;
slouken@2896
   249
    }
slouken@2898
   250
slouken@2898
   251
    if (!fmt->Amask) {
slouken@2898
   252
        return SDL_BlendLine_RGB(dst, x1, y1, x2, y2, blendMode, r, g, b, a);
slouken@2898
   253
    } else {
slouken@2898
   254
        return SDL_BlendLine_RGBA(dst, x1, y1, x2, y2, blendMode, r, g, b, a);
slouken@2898
   255
    }
slouken@2888
   256
}
slouken@2888
   257
slouken@2888
   258
/* vi: set ts=4 sw=4 expandtab: */