src/video/SDL_draw.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 Dec 2008 08:28:25 +0000
changeset 2898 e40448bc7727
child 2899 a0c837a16e4c
permissions -rw-r--r--
Share code between fill and line drawing
Added general RGB surface format fallbacks to drawing code
Fixed issues with destination surface alpha channel
slouken@2898
     1
/*
slouken@2898
     2
    SDL - Simple DirectMedia Layer
slouken@2898
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@2898
     4
slouken@2898
     5
    This library is free software; you can redistribute it and/or
slouken@2898
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2898
     7
    License as published by the Free Software Foundation; either
slouken@2898
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2898
     9
slouken@2898
    10
    This library is distributed in the hope that it will be useful,
slouken@2898
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2898
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2898
    13
    Lesser General Public License for more details.
slouken@2898
    14
slouken@2898
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2898
    16
    License along with this library; if not, write to the Free Software
slouken@2898
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2898
    18
slouken@2898
    19
    Sam Lantinga
slouken@2898
    20
    slouken@libsdl.org
slouken@2898
    21
*/
slouken@2898
    22
#include "SDL_config.h"
slouken@2898
    23
slouken@2898
    24
#include "SDL_blit.h"
slouken@2898
    25
slouken@2898
    26
/* This code assumes that r, g, b, a are the source color,
slouken@2898
    27
 * and in the blend and add case, the RGB values are premultiplied by a.
slouken@2898
    28
 */
slouken@2898
    29
slouken@2898
    30
#define DRAW_MUL(_a, _b) (((unsigned)(_a)*(_b))/255)
slouken@2898
    31
slouken@2898
    32
#define DRAW_SETPIXEL(setpixel) \
slouken@2898
    33
do { \
slouken@2898
    34
    unsigned sr = r, sg = g, sb = b, sa = a; \
slouken@2898
    35
    setpixel; \
slouken@2898
    36
} while (0)
slouken@2898
    37
slouken@2898
    38
#define DRAW_SETPIXEL_BLEND(getpixel, setpixel) \
slouken@2898
    39
do { \
slouken@2898
    40
    unsigned sr, sg, sb, sa; sa; \
slouken@2898
    41
    getpixel; \
slouken@2898
    42
    sr = DRAW_MUL(inva, sr) + r; \
slouken@2898
    43
    sg = DRAW_MUL(inva, sg) + g; \
slouken@2898
    44
    sb = DRAW_MUL(inva, sb) + b; \
slouken@2898
    45
    setpixel; \
slouken@2898
    46
} while (0)
slouken@2898
    47
slouken@2898
    48
#define DRAW_SETPIXEL_ADD(getpixel, setpixel) \
slouken@2898
    49
do { \
slouken@2898
    50
    unsigned sr, sg, sb, sa; sa; \
slouken@2898
    51
    getpixel; \
slouken@2898
    52
    sr += r; if (sr > 0xff) sr = 0xff; \
slouken@2898
    53
    sg += g; if (sg > 0xff) sg = 0xff; \
slouken@2898
    54
    sb += b; if (sb > 0xff) sb = 0xff; \
slouken@2898
    55
    setpixel; \
slouken@2898
    56
} while (0)
slouken@2898
    57
slouken@2898
    58
#define DRAW_SETPIXEL_MOD(getpixel, setpixel) \
slouken@2898
    59
do { \
slouken@2898
    60
    unsigned sr, sg, sb, sa; sa; \
slouken@2898
    61
    getpixel; \
slouken@2898
    62
    sr = DRAW_MUL(sr, r); \
slouken@2898
    63
    sg = DRAW_MUL(sg, g); \
slouken@2898
    64
    sb = DRAW_MUL(sb, b); \
slouken@2898
    65
    setpixel; \
slouken@2898
    66
} while (0)
slouken@2898
    67
slouken@2898
    68
#define DRAW_SETPIXELXY(x, y, type, bpp, op) \
slouken@2898
    69
do { \
slouken@2898
    70
    type *pixel = (type *)(dst->pixels + y * dst->pitch + x * bpp); \
slouken@2898
    71
    op; \
slouken@2898
    72
} while (0)
slouken@2898
    73
slouken@2898
    74
/*
slouken@2898
    75
 * Define draw operators for RGB555
slouken@2898
    76
 */
slouken@2898
    77
slouken@2898
    78
#define DRAW_SETPIXEL_RGB555 \
slouken@2898
    79
    DRAW_SETPIXEL(RGB555_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
    80
slouken@2898
    81
#define DRAW_SETPIXEL_BLEND_RGB555 \
slouken@2898
    82
    DRAW_SETPIXEL_BLEND(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
slouken@2898
    83
                        RGB555_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
    84
slouken@2898
    85
#define DRAW_SETPIXEL_ADD_RGB555 \
slouken@2898
    86
    DRAW_SETPIXEL_ADD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
slouken@2898
    87
                      RGB555_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
    88
slouken@2898
    89
#define DRAW_SETPIXEL_MOD_RGB555 \
slouken@2898
    90
    DRAW_SETPIXEL_MOD(RGB_FROM_RGB555(*pixel, sr, sg, sb), \
slouken@2898
    91
                      RGB555_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
    92
slouken@2898
    93
#define DRAW_SETPIXELXY_RGB555(x, y) \
slouken@2898
    94
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB555)
slouken@2898
    95
slouken@2898
    96
#define DRAW_SETPIXELXY_BLEND_RGB555(x, y) \
slouken@2898
    97
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB555)
slouken@2898
    98
slouken@2898
    99
#define DRAW_SETPIXELXY_ADD_RGB555(x, y) \
slouken@2898
   100
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB555)
slouken@2898
   101
slouken@2898
   102
#define DRAW_SETPIXELXY_MOD_RGB555(x, y) \
slouken@2898
   103
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB555)
slouken@2898
   104
slouken@2898
   105
/*
slouken@2898
   106
 * Define draw operators for RGB565
slouken@2898
   107
 */
slouken@2898
   108
slouken@2898
   109
#define DRAW_SETPIXEL_RGB565 \
slouken@2898
   110
    DRAW_SETPIXEL(RGB565_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   111
slouken@2898
   112
#define DRAW_SETPIXEL_BLEND_RGB565 \
slouken@2898
   113
    DRAW_SETPIXEL_BLEND(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
slouken@2898
   114
                        RGB565_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   115
slouken@2898
   116
#define DRAW_SETPIXEL_ADD_RGB565 \
slouken@2898
   117
    DRAW_SETPIXEL_ADD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
slouken@2898
   118
                      RGB565_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   119
slouken@2898
   120
#define DRAW_SETPIXEL_MOD_RGB565 \
slouken@2898
   121
    DRAW_SETPIXEL_MOD(RGB_FROM_RGB565(*pixel, sr, sg, sb), \
slouken@2898
   122
                      RGB565_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   123
slouken@2898
   124
#define DRAW_SETPIXELXY_RGB565(x, y) \
slouken@2898
   125
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB565)
slouken@2898
   126
slouken@2898
   127
#define DRAW_SETPIXELXY_BLEND_RGB565(x, y) \
slouken@2898
   128
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB565)
slouken@2898
   129
slouken@2898
   130
#define DRAW_SETPIXELXY_ADD_RGB565(x, y) \
slouken@2898
   131
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB565)
slouken@2898
   132
slouken@2898
   133
#define DRAW_SETPIXELXY_MOD_RGB565(x, y) \
slouken@2898
   134
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB565)
slouken@2898
   135
slouken@2898
   136
/*
slouken@2898
   137
 * Define draw operators for RGB888
slouken@2898
   138
 */
slouken@2898
   139
slouken@2898
   140
#define DRAW_SETPIXEL_RGB888 \
slouken@2898
   141
    DRAW_SETPIXEL(RGB888_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   142
slouken@2898
   143
#define DRAW_SETPIXEL_BLEND_RGB888 \
slouken@2898
   144
    DRAW_SETPIXEL_BLEND(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
slouken@2898
   145
                        RGB888_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   146
slouken@2898
   147
#define DRAW_SETPIXEL_ADD_RGB888 \
slouken@2898
   148
    DRAW_SETPIXEL_ADD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
slouken@2898
   149
                      RGB888_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   150
slouken@2898
   151
#define DRAW_SETPIXEL_MOD_RGB888 \
slouken@2898
   152
    DRAW_SETPIXEL_MOD(RGB_FROM_RGB888(*pixel, sr, sg, sb), \
slouken@2898
   153
                      RGB888_FROM_RGB(*pixel, sr, sg, sb))
slouken@2898
   154
slouken@2898
   155
#define DRAW_SETPIXELXY_RGB888(x, y) \
slouken@2898
   156
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB888)
slouken@2898
   157
slouken@2898
   158
#define DRAW_SETPIXELXY_BLEND_RGB888(x, y) \
slouken@2898
   159
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB888)
slouken@2898
   160
slouken@2898
   161
#define DRAW_SETPIXELXY_ADD_RGB888(x, y) \
slouken@2898
   162
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB888)
slouken@2898
   163
slouken@2898
   164
#define DRAW_SETPIXELXY_MOD_RGB888(x, y) \
slouken@2898
   165
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB888)
slouken@2898
   166
slouken@2898
   167
/*
slouken@2898
   168
 * Define draw operators for general RGB
slouken@2898
   169
 */
slouken@2898
   170
slouken@2898
   171
#define DRAW_SETPIXEL_RGB \
slouken@2898
   172
    DRAW_SETPIXEL(PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
slouken@2898
   173
slouken@2898
   174
#define DRAW_SETPIXEL_BLEND_RGB \
slouken@2898
   175
    DRAW_SETPIXEL_BLEND(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
slouken@2898
   176
                        PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
slouken@2898
   177
slouken@2898
   178
#define DRAW_SETPIXEL_ADD_RGB \
slouken@2898
   179
    DRAW_SETPIXEL_ADD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
slouken@2898
   180
                      PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
slouken@2898
   181
slouken@2898
   182
#define DRAW_SETPIXEL_MOD_RGB \
slouken@2898
   183
    DRAW_SETPIXEL_MOD(RGB_FROM_PIXEL(*pixel, fmt, sr, sg, sb), \
slouken@2898
   184
                      PIXEL_FROM_RGB(*pixel, fmt, sr, sg, sb))
slouken@2898
   185
slouken@2898
   186
#define DRAW_SETPIXELXY2_RGB(x, y) \
slouken@2898
   187
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_RGB)
slouken@2898
   188
slouken@2898
   189
#define DRAW_SETPIXELXY4_RGB(x, y) \
slouken@2898
   190
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGB)
slouken@2898
   191
slouken@2898
   192
#define DRAW_SETPIXELXY2_BLEND_RGB(x, y) \
slouken@2898
   193
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_BLEND_RGB)
slouken@2898
   194
slouken@2898
   195
#define DRAW_SETPIXELXY4_BLEND_RGB(x, y) \
slouken@2898
   196
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGB)
slouken@2898
   197
slouken@2898
   198
#define DRAW_SETPIXELXY2_ADD_RGB(x, y) \
slouken@2898
   199
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_ADD_RGB)
slouken@2898
   200
slouken@2898
   201
#define DRAW_SETPIXELXY4_ADD_RGB(x, y) \
slouken@2898
   202
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGB)
slouken@2898
   203
slouken@2898
   204
#define DRAW_SETPIXELXY2_MOD_RGB(x, y) \
slouken@2898
   205
    DRAW_SETPIXELXY(x, y, Uint16, 2, DRAW_SETPIXEL_MOD_RGB)
slouken@2898
   206
slouken@2898
   207
#define DRAW_SETPIXELXY4_MOD_RGB(x, y) \
slouken@2898
   208
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGB)
slouken@2898
   209
slouken@2898
   210
slouken@2898
   211
/*
slouken@2898
   212
 * Define draw operators for general RGBA
slouken@2898
   213
 */
slouken@2898
   214
slouken@2898
   215
#define DRAW_SETPIXEL_RGBA \
slouken@2898
   216
    DRAW_SETPIXEL(PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
slouken@2898
   217
slouken@2898
   218
#define DRAW_SETPIXEL_BLEND_RGBA \
slouken@2898
   219
    DRAW_SETPIXEL_BLEND(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
slouken@2898
   220
                        PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
slouken@2898
   221
slouken@2898
   222
#define DRAW_SETPIXEL_ADD_RGBA \
slouken@2898
   223
    DRAW_SETPIXEL_ADD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
slouken@2898
   224
                      PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
slouken@2898
   225
slouken@2898
   226
#define DRAW_SETPIXEL_MOD_RGBA \
slouken@2898
   227
    DRAW_SETPIXEL_MOD(RGBA_FROM_PIXEL(*pixel, fmt, sr, sg, sb, sa), \
slouken@2898
   228
                      PIXEL_FROM_RGBA(*pixel, fmt, sr, sg, sb, sa))
slouken@2898
   229
slouken@2898
   230
#define DRAW_SETPIXELXY4_RGBA(x, y) \
slouken@2898
   231
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_RGBA)
slouken@2898
   232
slouken@2898
   233
#define DRAW_SETPIXELXY4_BLEND_RGBA(x, y) \
slouken@2898
   234
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_BLEND_RGBA)
slouken@2898
   235
slouken@2898
   236
#define DRAW_SETPIXELXY4_ADD_RGBA(x, y) \
slouken@2898
   237
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_ADD_RGBA)
slouken@2898
   238
slouken@2898
   239
#define DRAW_SETPIXELXY4_MOD_RGBA(x, y) \
slouken@2898
   240
    DRAW_SETPIXELXY(x, y, Uint32, 4, DRAW_SETPIXEL_MOD_RGBA)
slouken@2898
   241
slouken@2898
   242
/*
slouken@2898
   243
 * Define line drawing macro
slouken@2898
   244
 */
slouken@2898
   245
slouken@2898
   246
#define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
slouken@2898
   247
slouken@2898
   248
#define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0)
slouken@2898
   249
slouken@2898
   250
#define BRESENHAM(x0, y0, x1, y1, op) \
slouken@2898
   251
{ \
slouken@2898
   252
    int deltax, deltay, steep, error, xstep, ystep, x, y; \
slouken@2898
   253
 \
slouken@2898
   254
    deltax = ABS(x1 - x0); \
slouken@2898
   255
    deltay = ABS(y1 - y0); \
slouken@2898
   256
    steep = (deltay > deltax); \
slouken@2898
   257
    if (steep) { \
slouken@2898
   258
        SWAP(x0, y0); \
slouken@2898
   259
        SWAP(x1, y1); \
slouken@2898
   260
        SWAP(deltax, deltay); \
slouken@2898
   261
    } \
slouken@2898
   262
    error = (x1 - x0) / 2; \
slouken@2898
   263
    y = y0; \
slouken@2898
   264
    if (x0 > x1) { \
slouken@2898
   265
        xstep = -1; \
slouken@2898
   266
    } else { \
slouken@2898
   267
        xstep = 1; \
slouken@2898
   268
    } \
slouken@2898
   269
    if (y0 < y1) { \
slouken@2898
   270
        ystep = 1; \
slouken@2898
   271
    } else { \
slouken@2898
   272
        ystep = -1; \
slouken@2898
   273
    } \
slouken@2898
   274
    if (!steep) { \
slouken@2898
   275
        for (x = x0; x != x1; x += xstep) { \
slouken@2898
   276
            op(x, y); \
slouken@2898
   277
            error -= deltay; \
slouken@2898
   278
            if (error < 0) { \
slouken@2898
   279
                y += ystep; \
slouken@2898
   280
                error += deltax; \
slouken@2898
   281
            } \
slouken@2898
   282
        } \
slouken@2898
   283
    } else { \
slouken@2898
   284
        for (x = x0; x != x1; x += xstep) { \
slouken@2898
   285
            op(y, x); \
slouken@2898
   286
            error -= deltay; \
slouken@2898
   287
            if (error < 0) { \
slouken@2898
   288
                y += ystep; \
slouken@2898
   289
                error += deltax; \
slouken@2898
   290
            } \
slouken@2898
   291
        } \
slouken@2898
   292
    } \
slouken@2898
   293
}
slouken@2898
   294
slouken@2898
   295
/*
slouken@2898
   296
 * Define blend fill macro
slouken@2898
   297
 */
slouken@2898
   298
slouken@2898
   299
#define BLENDRECT(type, op) \
slouken@2898
   300
do { \
slouken@2898
   301
    int w; \
slouken@2898
   302
    int width = dstrect->w; \
slouken@2898
   303
    int height = dstrect->h; \
slouken@2898
   304
    int pitch = (dst->pitch / dst->format->BytesPerPixel); \
slouken@2898
   305
    int skip = pitch - width; \
slouken@2898
   306
    type *pixel = (type *)dst->pixels + dstrect->y * pitch + dstrect->x; \
slouken@2898
   307
    while (height--) { \
slouken@2898
   308
        { int n = (width+3)/4; \
slouken@2898
   309
            switch (width & 3) { \
slouken@2898
   310
            case 0: do {   op; pixel++; \
slouken@2898
   311
            case 3:        op; pixel++; \
slouken@2898
   312
            case 2:        op; pixel++; \
slouken@2898
   313
            case 1:        op; pixel++; \
slouken@2898
   314
                    } while ( --n > 0 ); \
slouken@2898
   315
            } \
slouken@2898
   316
        } \
slouken@2898
   317
        pixel += skip; \
slouken@2898
   318
    } \
slouken@2898
   319
} while (0)
slouken@2898
   320
slouken@2898
   321
/*
slouken@2898
   322
 * Define blend line macro
slouken@2898
   323
 */
slouken@2898
   324
slouken@2898
   325
/* vi: set ts=4 sw=4 expandtab: */