src/video/SDL_RLEaccel.c
author Darren Alton <dalton@stevens.edu>
Tue, 10 Jun 2008 06:45:39 +0000
branchgsoc2008_nds
changeset 2669 e27bdcc80744
parent 2328 91e601d9df8b
child 2798 5f3831f1c3ea
permissions -rw-r--r--
First commit. Added header configs for DS as well as a Makefile. Initial work on framebuffer video driver, currently not functioning as desired.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/*
slouken@0
    25
 * RLE encoding for software colorkey and alpha-channel acceleration
slouken@0
    26
 *
slouken@0
    27
 * Original version by Sam Lantinga
slouken@0
    28
 *
slouken@0
    29
 * Mattias Engdegård (Yorick): Rewrite. New encoding format, encoder and
slouken@0
    30
 * decoder. Added per-surface alpha blitter. Added per-pixel alpha
slouken@0
    31
 * format, encoder and blitter.
slouken@0
    32
 *
slouken@0
    33
 * Many thanks to Xark and johns for hints, benchmarks and useful comments
slouken@0
    34
 * leading to this code.
slouken@0
    35
 *
slouken@0
    36
 * Welcome to Macro Mayhem.
slouken@0
    37
 */
slouken@0
    38
slouken@0
    39
/*
slouken@0
    40
 * The encoding translates the image data to a stream of segments of the form
slouken@0
    41
 *
slouken@0
    42
 * <skip> <run> <data>
slouken@0
    43
 *
slouken@0
    44
 * where <skip> is the number of transparent pixels to skip,
slouken@0
    45
 *       <run>  is the number of opaque pixels to blit,
slouken@0
    46
 * and   <data> are the pixels themselves.
slouken@0
    47
 *
slouken@0
    48
 * This basic structure is used both for colorkeyed surfaces, used for simple
slouken@0
    49
 * binary transparency and for per-surface alpha blending, and for surfaces
slouken@0
    50
 * with per-pixel alpha. The details differ, however:
slouken@0
    51
 *
slouken@0
    52
 * Encoding of colorkeyed surfaces:
slouken@0
    53
 *
slouken@0
    54
 *   Encoded pixels always have the same format as the target surface.
slouken@0
    55
 *   <skip> and <run> are unsigned 8 bit integers, except for 32 bit depth
slouken@0
    56
 *   where they are 16 bit. This makes the pixel data aligned at all times.
slouken@0
    57
 *   Segments never wrap around from one scan line to the next.
slouken@0
    58
 *
slouken@0
    59
 *   The end of the sequence is marked by a zero <skip>,<run> pair at the *
slouken@0
    60
 *   beginning of a line.
slouken@0
    61
 *
slouken@0
    62
 * Encoding of surfaces with per-pixel alpha:
slouken@0
    63
 *
slouken@0
    64
 *   The sequence begins with a struct RLEDestFormat describing the target
slouken@0
    65
 *   pixel format, to provide reliable un-encoding.
slouken@0
    66
 *
slouken@0
    67
 *   Each scan line is encoded twice: First all completely opaque pixels,
slouken@0
    68
 *   encoded in the target format as described above, and then all
slouken@0
    69
 *   partially transparent (translucent) pixels (where 1 <= alpha <= 254),
slouken@0
    70
 *   in the following 32-bit format:
slouken@0
    71
 *
slouken@0
    72
 *   For 32-bit targets, each pixel has the target RGB format but with
slouken@0
    73
 *   the alpha value occupying the highest 8 bits. The <skip> and <run>
slouken@0
    74
 *   counts are 16 bit.
slouken@0
    75
 * 
slouken@0
    76
 *   For 16-bit targets, each pixel has the target RGB format, but with
slouken@0
    77
 *   the middle component (usually green) shifted 16 steps to the left,
slouken@0
    78
 *   and the hole filled with the 5 most significant bits of the alpha value.
slouken@0
    79
 *   i.e. if the target has the format         rrrrrggggggbbbbb,
slouken@0
    80
 *   the encoded pixel will be 00000gggggg00000rrrrr0aaaaabbbbb.
slouken@0
    81
 *   The <skip> and <run> counts are 8 bit for the opaque lines, 16 bit
slouken@0
    82
 *   for the translucent lines. Two padding bytes may be inserted
slouken@0
    83
 *   before each translucent line to keep them 32-bit aligned.
slouken@0
    84
 *
slouken@0
    85
 *   The end of the sequence is marked by a zero <skip>,<run> pair at the
slouken@0
    86
 *   beginning of an opaque line.
slouken@0
    87
 */
slouken@0
    88
slouken@0
    89
#include "SDL_video.h"
slouken@0
    90
#include "SDL_sysvideo.h"
slouken@0
    91
#include "SDL_blit.h"
slouken@0
    92
#include "SDL_RLEaccel_c.h"
slouken@0
    93
slouken@1402
    94
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && SDL_ASSEMBLY_ROUTINES
slouken@880
    95
#define MMX_ASMBLIT
slouken@879
    96
#endif
slouken@879
    97
slouken@880
    98
#ifdef MMX_ASMBLIT
slouken@689
    99
#include "mmx.h"
slouken@739
   100
#include "SDL_cpuinfo.h"
slouken@689
   101
#endif
slouken@689
   102
slouken@0
   103
#ifndef MAX
slouken@0
   104
#define MAX(a, b) ((a) > (b) ? (a) : (b))
slouken@0
   105
#endif
slouken@0
   106
#ifndef MIN
slouken@0
   107
#define MIN(a, b) ((a) < (b) ? (a) : (b))
slouken@0
   108
#endif
slouken@0
   109
slouken@1
   110
#define PIXEL_COPY(to, from, len, bpp)			\
slouken@1
   111
do {							\
slouken@1
   112
    if(bpp == 4) {					\
slouken@1428
   113
	SDL_memcpy4(to, from, (size_t)(len));		\
slouken@1
   114
    } else {						\
slouken@1428
   115
	SDL_memcpy(to, from, (size_t)(len) * (bpp));	\
slouken@1
   116
    }							\
slouken@1
   117
} while(0)
slouken@1
   118
slouken@0
   119
/*
slouken@0
   120
 * Various colorkey blit methods, for opaque and per-surface alpha
slouken@0
   121
 */
slouken@0
   122
slouken@0
   123
#define OPAQUE_BLIT(to, from, length, bpp, alpha)	\
slouken@1
   124
    PIXEL_COPY(to, from, length, bpp)
slouken@0
   125
slouken@880
   126
#ifdef MMX_ASMBLIT
slouken@689
   127
slouken@689
   128
#define ALPHA_BLIT32_888MMX(to, from, length, bpp, alpha)	\
slouken@689
   129
    do {							\
slouken@689
   130
	Uint32 *srcp = (Uint32 *)(from);			\
slouken@689
   131
	Uint32 *dstp = (Uint32 *)(to);				\
slouken@689
   132
        int i = 0x00FF00FF;					\
slouken@689
   133
        movd_m2r(*(&i), mm3);					\
slouken@689
   134
        punpckldq_r2r(mm3, mm3);				\
slouken@689
   135
        i = 0xFF000000;						\
slouken@689
   136
        movd_m2r(*(&i), mm7);					\
slouken@689
   137
        punpckldq_r2r(mm7, mm7);				\
slouken@689
   138
        i = alpha | alpha << 16;				\
slouken@689
   139
        movd_m2r(*(&i), mm4);					\
slouken@689
   140
        punpckldq_r2r(mm4, mm4);				\
slouken@689
   141
	pcmpeqd_r2r(mm5,mm5); /* set mm5 to "1" */		\
slouken@689
   142
	pxor_r2r(mm7, mm5); /* make clear alpha mask */		\
slouken@689
   143
        i = length;						\
slouken@689
   144
	if(i & 1) {						\
slouken@689
   145
          movd_m2r((*srcp), mm1); /* src -> mm1 */		\
slouken@689
   146
          punpcklbw_r2r(mm1, mm1);				\
slouken@689
   147
          pand_r2r(mm3, mm1);					\
slouken@689
   148
	  movd_m2r((*dstp), mm2); /* dst -> mm2 */		\
slouken@689
   149
          punpcklbw_r2r(mm2, mm2);				\
slouken@689
   150
          pand_r2r(mm3, mm2);					\
slouken@689
   151
	  psubw_r2r(mm2, mm1);					\
slouken@689
   152
	  pmullw_r2r(mm4, mm1);					\
slouken@689
   153
	  psrlw_i2r(8, mm1);					\
slouken@689
   154
	  paddw_r2r(mm1, mm2);					\
slouken@689
   155
	  pand_r2r(mm3, mm2);					\
slouken@689
   156
	  packuswb_r2r(mm2, mm2);				\
slouken@689
   157
	  pand_r2r(mm5, mm2); /* 00000RGB -> mm2 */		\
slouken@689
   158
	  movd_r2m(mm2, *dstp);					\
slouken@689
   159
	  ++srcp;						\
slouken@689
   160
	  ++dstp;						\
slouken@689
   161
	  i--;							\
slouken@689
   162
	}							\
slouken@689
   163
	for(; i > 0; --i) {					\
slouken@689
   164
          movq_m2r((*srcp), mm0);				\
slouken@689
   165
	  movq_r2r(mm0, mm1);					\
slouken@689
   166
          punpcklbw_r2r(mm0, mm0);				\
slouken@689
   167
	  movq_m2r((*dstp), mm2);				\
slouken@689
   168
	  punpckhbw_r2r(mm1, mm1);				\
slouken@689
   169
	  movq_r2r(mm2, mm6);					\
slouken@689
   170
          pand_r2r(mm3, mm0);					\
slouken@689
   171
          punpcklbw_r2r(mm2, mm2);				\
slouken@689
   172
	  pand_r2r(mm3, mm1);					\
slouken@689
   173
	  punpckhbw_r2r(mm6, mm6);				\
slouken@689
   174
          pand_r2r(mm3, mm2);					\
slouken@689
   175
	  psubw_r2r(mm2, mm0);					\
slouken@689
   176
	  pmullw_r2r(mm4, mm0);					\
slouken@689
   177
	  pand_r2r(mm3, mm6);					\
slouken@689
   178
	  psubw_r2r(mm6, mm1);					\
slouken@689
   179
	  pmullw_r2r(mm4, mm1);					\
slouken@689
   180
	  psrlw_i2r(8, mm0);					\
slouken@689
   181
	  paddw_r2r(mm0, mm2);					\
slouken@689
   182
	  psrlw_i2r(8, mm1);					\
slouken@689
   183
	  paddw_r2r(mm1, mm6);					\
slouken@689
   184
	  pand_r2r(mm3, mm2);					\
slouken@689
   185
	  pand_r2r(mm3, mm6);					\
slouken@689
   186
	  packuswb_r2r(mm2, mm2);				\
slouken@689
   187
	  packuswb_r2r(mm6, mm6);				\
slouken@689
   188
	  psrlq_i2r(32, mm2);					\
slouken@689
   189
	  psllq_i2r(32, mm6);					\
slouken@689
   190
	  por_r2r(mm6, mm2);					\
slouken@689
   191
	  pand_r2r(mm5, mm2); /* 00000RGB -> mm2 */		\
slouken@689
   192
         movq_r2m(mm2, *dstp);					\
slouken@689
   193
	  srcp += 2;						\
slouken@689
   194
	  dstp += 2;						\
slouken@689
   195
	  i--;							\
slouken@689
   196
	}							\
slouken@689
   197
	emms();							\
slouken@689
   198
    } while(0)
slouken@689
   199
slouken@689
   200
#define ALPHA_BLIT16_565MMX(to, from, length, bpp, alpha)	\
slouken@689
   201
    do {						\
slouken@689
   202
        int i, n = 0;					\
slouken@689
   203
	Uint16 *srcp = (Uint16 *)(from);		\
slouken@689
   204
	Uint16 *dstp = (Uint16 *)(to);			\
slouken@689
   205
        Uint32 ALPHA = 0xF800;				\
slouken@689
   206
	movd_m2r(*(&ALPHA), mm1);			\
slouken@689
   207
        punpcklwd_r2r(mm1, mm1);			\
slouken@689
   208
        punpcklwd_r2r(mm1, mm1);			\
slouken@689
   209
	ALPHA = 0x07E0;					\
slouken@689
   210
	movd_m2r(*(&ALPHA), mm4);			\
slouken@689
   211
        punpcklwd_r2r(mm4, mm4);			\
slouken@689
   212
        punpcklwd_r2r(mm4, mm4);			\
slouken@689
   213
	ALPHA = 0x001F;					\
slouken@689
   214
	movd_m2r(*(&ALPHA), mm7);			\
slouken@689
   215
        punpcklwd_r2r(mm7, mm7);			\
slouken@689
   216
        punpcklwd_r2r(mm7, mm7);			\
slouken@689
   217
	alpha &= ~(1+2+4);				\
slouken@689
   218
        i = (Uint32)alpha | (Uint32)alpha << 16;	\
slouken@689
   219
        movd_m2r(*(&i), mm0);				\
slouken@689
   220
        punpckldq_r2r(mm0, mm0);			\
slouken@689
   221
        ALPHA = alpha >> 3;				\
slouken@689
   222
        i = ((int)(length) & 3);			\
slouken@689
   223
	for(; i > 0; --i) {				\
slouken@689
   224
	    Uint32 s = *srcp++;				\
slouken@689
   225
	    Uint32 d = *dstp;				\
slouken@689
   226
	    s = (s | s << 16) & 0x07e0f81f;		\
slouken@689
   227
	    d = (d | d << 16) & 0x07e0f81f;		\
slouken@689
   228
	    d += (s - d) * ALPHA >> 5;			\
slouken@689
   229
	    d &= 0x07e0f81f;				\
slouken@689
   230
	    *dstp++ = d | d >> 16;			\
slouken@689
   231
	    n++;					\
slouken@689
   232
	}						\
slouken@689
   233
	i = (int)(length) - n;				\
slouken@689
   234
	for(; i > 0; --i) {				\
slouken@689
   235
	  movq_m2r((*dstp), mm3);			\
slouken@689
   236
	  movq_m2r((*srcp), mm2);			\
slouken@689
   237
	  movq_r2r(mm2, mm5);				\
slouken@689
   238
	  pand_r2r(mm1 , mm5);				\
slouken@689
   239
	  psrlq_i2r(11, mm5);				\
slouken@689
   240
	  movq_r2r(mm3, mm6);				\
slouken@689
   241
	  pand_r2r(mm1 , mm6);				\
slouken@689
   242
	  psrlq_i2r(11, mm6);				\
slouken@689
   243
	  psubw_r2r(mm6, mm5);				\
slouken@689
   244
	  pmullw_r2r(mm0, mm5);				\
slouken@689
   245
	  psrlw_i2r(8, mm5);				\
slouken@689
   246
	  paddw_r2r(mm5, mm6);				\
slouken@689
   247
	  psllq_i2r(11, mm6);				\
slouken@689
   248
	  pand_r2r(mm1, mm6);				\
slouken@689
   249
	  movq_r2r(mm4, mm5);				\
slouken@689
   250
	  por_r2r(mm7, mm5);				\
slouken@689
   251
	  pand_r2r(mm5, mm3);				\
slouken@689
   252
	  por_r2r(mm6, mm3);				\
slouken@689
   253
	  movq_r2r(mm2, mm5);				\
slouken@689
   254
	  pand_r2r(mm4 , mm5);				\
slouken@689
   255
	  psrlq_i2r(5, mm5);				\
slouken@689
   256
	  movq_r2r(mm3, mm6);				\
slouken@689
   257
	  pand_r2r(mm4 , mm6);				\
slouken@689
   258
	  psrlq_i2r(5, mm6);				\
slouken@689
   259
	  psubw_r2r(mm6, mm5);				\
slouken@689
   260
	  pmullw_r2r(mm0, mm5);				\
slouken@689
   261
	  psrlw_i2r(8, mm5);				\
slouken@689
   262
	  paddw_r2r(mm5, mm6);				\
slouken@689
   263
	  psllq_i2r(5, mm6);				\
slouken@689
   264
	  pand_r2r(mm4, mm6);				\
slouken@689
   265
	  movq_r2r(mm1, mm5);				\
slouken@689
   266
	  por_r2r(mm7, mm5);				\
slouken@689
   267
	  pand_r2r(mm5, mm3);				\
slouken@689
   268
	  por_r2r(mm6, mm3);				\
slouken@689
   269
	  movq_r2r(mm2, mm5);				\
slouken@689
   270
	  pand_r2r(mm7 , mm5);				\
slouken@689
   271
          movq_r2r(mm3, mm6);				\
slouken@689
   272
	  pand_r2r(mm7 , mm6);				\
slouken@689
   273
	  psubw_r2r(mm6, mm5);				\
slouken@689
   274
	  pmullw_r2r(mm0, mm5);				\
slouken@689
   275
	  psrlw_i2r(8, mm5);				\
slouken@689
   276
	  paddw_r2r(mm5, mm6);				\
slouken@689
   277
	  pand_r2r(mm7, mm6);				\
slouken@689
   278
	  movq_r2r(mm1, mm5);				\
slouken@689
   279
	  por_r2r(mm4, mm5);				\
slouken@689
   280
	  pand_r2r(mm5, mm3);				\
slouken@689
   281
	  por_r2r(mm6, mm3);				\
slouken@689
   282
	  movq_r2m(mm3, *dstp);				\
slouken@689
   283
	  srcp += 4;					\
slouken@689
   284
	  dstp += 4;					\
slouken@689
   285
	  i -= 3;					\
slouken@689
   286
	}						\
slouken@689
   287
	emms();						\
slouken@689
   288
    } while(0)
slouken@689
   289
slouken@689
   290
#define ALPHA_BLIT16_555MMX(to, from, length, bpp, alpha)	\
slouken@689
   291
    do {						\
slouken@689
   292
        int i, n = 0;					\
slouken@689
   293
	Uint16 *srcp = (Uint16 *)(from);		\
slouken@689
   294
	Uint16 *dstp = (Uint16 *)(to);			\
slouken@689
   295
        Uint32 ALPHA = 0x7C00;				\
slouken@689
   296
	movd_m2r(*(&ALPHA), mm1);			\
slouken@689
   297
        punpcklwd_r2r(mm1, mm1);			\
slouken@689
   298
        punpcklwd_r2r(mm1, mm1);			\
slouken@689
   299
	ALPHA = 0x03E0;					\
slouken@689
   300
        movd_m2r(*(&ALPHA), mm4);			\
slouken@689
   301
        punpcklwd_r2r(mm4, mm4);			\
slouken@689
   302
        punpcklwd_r2r(mm4, mm4);			\
slouken@689
   303
	ALPHA = 0x001F;					\
slouken@689
   304
	movd_m2r(*(&ALPHA), mm7);			\
slouken@689
   305
        punpcklwd_r2r(mm7, mm7);			\
slouken@689
   306
        punpcklwd_r2r(mm7, mm7);			\
slouken@689
   307
	alpha &= ~(1+2+4);				\
slouken@689
   308
        i = (Uint32)alpha | (Uint32)alpha << 16;	\
slouken@689
   309
        movd_m2r(*(&i), mm0);				\
slouken@689
   310
        punpckldq_r2r(mm0, mm0);			\
slouken@689
   311
        i = ((int)(length) & 3);				\
slouken@689
   312
        ALPHA = alpha >> 3;				\
slouken@689
   313
	for(; i > 0; --i) {				\
slouken@689
   314
	    Uint32 s = *srcp++;				\
slouken@689
   315
	    Uint32 d = *dstp;				\
slouken@689
   316
	    s = (s | s << 16) & 0x03e07c1f;		\
slouken@689
   317
	    d = (d | d << 16) & 0x03e07c1f;		\
slouken@689
   318
	    d += (s - d) * ALPHA >> 5;			\
slouken@689
   319
	    d &= 0x03e07c1f;				\
slouken@689
   320
	    *dstp++ = d | d >> 16;			\
slouken@689
   321
	    n++;					\
slouken@689
   322
	}						\
slouken@689
   323
	i = (int)(length) - n;				\
slouken@689
   324
	for(; i > 0; --i) {				\
slouken@689
   325
	  movq_m2r((*dstp), mm3);			\
slouken@689
   326
	  movq_m2r((*srcp), mm2);			\
slouken@689
   327
	  movq_r2r(mm2, mm5);				\
slouken@689
   328
	  pand_r2r(mm1 , mm5);				\
slouken@689
   329
	  psrlq_i2r(10, mm5);				\
slouken@689
   330
	  movq_r2r(mm3, mm6);				\
slouken@689
   331
	  pand_r2r(mm1 , mm6);				\
slouken@689
   332
	  psrlq_i2r(10, mm6);				\
slouken@689
   333
	  psubw_r2r(mm6, mm5);				\
slouken@689
   334
	  pmullw_r2r(mm0, mm5);				\
slouken@689
   335
	  psrlw_i2r(8, mm5);				\
slouken@689
   336
	  paddw_r2r(mm5, mm6);				\
slouken@689
   337
	  psllq_i2r(10, mm6);				\
slouken@689
   338
	  pand_r2r(mm1, mm6);				\
slouken@689
   339
	  movq_r2r(mm4, mm5);				\
slouken@689
   340
	  por_r2r(mm7, mm5);				\
slouken@689
   341
	  pand_r2r(mm5, mm3);				\
slouken@689
   342
	  por_r2r(mm6, mm3);				\
slouken@689
   343
	  movq_r2r(mm2, mm5);				\
slouken@689
   344
	  pand_r2r(mm4 , mm5);				\
slouken@689
   345
	  psrlq_i2r(5, mm5);				\
slouken@689
   346
	  movq_r2r(mm3, mm6);				\
slouken@689
   347
	  pand_r2r(mm4 , mm6);				\
slouken@689
   348
	  psrlq_i2r(5, mm6);				\
slouken@689
   349
	  psubw_r2r(mm6, mm5);				\
slouken@689
   350
	  pmullw_r2r(mm0, mm5);				\
slouken@689
   351
	  psrlw_i2r(8, mm5);				\
slouken@689
   352
	  paddw_r2r(mm5, mm6);				\
slouken@689
   353
	  psllq_i2r(5, mm6);				\
slouken@689
   354
	  pand_r2r(mm4, mm6);				\
slouken@689
   355
	  movq_r2r(mm1, mm5);				\
slouken@689
   356
	  por_r2r(mm7, mm5);				\
slouken@689
   357
	  pand_r2r(mm5, mm3);				\
slouken@689
   358
	  por_r2r(mm6, mm3);				\
slouken@689
   359
	  movq_r2r(mm2, mm5);				\
slouken@689
   360
	  pand_r2r(mm7 , mm5);				\
slouken@689
   361
          movq_r2r(mm3, mm6);				\
slouken@689
   362
	  pand_r2r(mm7 , mm6);				\
slouken@689
   363
	  psubw_r2r(mm6, mm5);				\
slouken@689
   364
	  pmullw_r2r(mm0, mm5);				\
slouken@689
   365
	  psrlw_i2r(8, mm5);				\
slouken@689
   366
	  paddw_r2r(mm5, mm6);				\
slouken@689
   367
	  pand_r2r(mm7, mm6);				\
slouken@689
   368
	  movq_r2r(mm1, mm5);				\
slouken@689
   369
	  por_r2r(mm4, mm5);				\
slouken@689
   370
	  pand_r2r(mm5, mm3);				\
slouken@689
   371
	  por_r2r(mm6, mm3);				\
slouken@689
   372
	  movq_r2m(mm3, *dstp);				\
slouken@689
   373
	  srcp += 4;					\
slouken@689
   374
	  dstp += 4;					\
slouken@689
   375
	  i -= 3;					\
slouken@689
   376
	}						\
slouken@689
   377
	emms();						\
slouken@689
   378
    } while(0)
slouken@689
   379
slouken@689
   380
#endif
slouken@689
   381
slouken@0
   382
/*
slouken@0
   383
 * For 32bpp pixels on the form 0x00rrggbb:
slouken@0
   384
 * If we treat the middle component separately, we can process the two
slouken@0
   385
 * remaining in parallel. This is safe to do because of the gap to the left
slouken@0
   386
 * of each component, so the bits from the multiplication don't collide.
slouken@0
   387
 * This can be used for any RGB permutation of course.
slouken@0
   388
 */
slouken@0
   389
#define ALPHA_BLIT32_888(to, from, length, bpp, alpha)		\
slouken@0
   390
    do {							\
slouken@0
   391
        int i;							\
slouken@0
   392
	Uint32 *src = (Uint32 *)(from);				\
slouken@0
   393
	Uint32 *dst = (Uint32 *)(to);				\
slouken@0
   394
	for(i = 0; i < (int)(length); i++) {			\
slouken@0
   395
	    Uint32 s = *src++;					\
slouken@0
   396
	    Uint32 d = *dst;					\
slouken@0
   397
	    Uint32 s1 = s & 0xff00ff;				\
slouken@0
   398
	    Uint32 d1 = d & 0xff00ff;				\
slouken@0
   399
	    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;	\
slouken@0
   400
	    s &= 0xff00;					\
slouken@0
   401
	    d &= 0xff00;					\
slouken@0
   402
	    d = (d + ((s - d) * alpha >> 8)) & 0xff00;		\
slouken@0
   403
	    *dst++ = d1 | d;					\
slouken@0
   404
	}							\
slouken@0
   405
    } while(0)
slouken@0
   406
slouken@0
   407
/*
slouken@0
   408
 * For 16bpp pixels we can go a step further: put the middle component
slouken@0
   409
 * in the high 16 bits of a 32 bit word, and process all three RGB
slouken@0
   410
 * components at the same time. Since the smallest gap is here just
slouken@0
   411
 * 5 bits, we have to scale alpha down to 5 bits as well.
slouken@0
   412
 */
slouken@0
   413
#define ALPHA_BLIT16_565(to, from, length, bpp, alpha)	\
slouken@0
   414
    do {						\
slouken@0
   415
        int i;						\
slouken@0
   416
	Uint16 *src = (Uint16 *)(from);			\
slouken@0
   417
	Uint16 *dst = (Uint16 *)(to);			\
slouken@689
   418
	Uint32 ALPHA = alpha >> 3;			\
slouken@0
   419
	for(i = 0; i < (int)(length); i++) {		\
slouken@0
   420
	    Uint32 s = *src++;				\
slouken@0
   421
	    Uint32 d = *dst;				\
slouken@0
   422
	    s = (s | s << 16) & 0x07e0f81f;		\
slouken@0
   423
	    d = (d | d << 16) & 0x07e0f81f;		\
slouken@689
   424
	    d += (s - d) * ALPHA >> 5;			\
slouken@0
   425
	    d &= 0x07e0f81f;				\
slouken@1428
   426
	    *dst++ = (Uint16)(d | d >> 16);			\
slouken@0
   427
	}						\
slouken@0
   428
    } while(0)
slouken@0
   429
slouken@0
   430
#define ALPHA_BLIT16_555(to, from, length, bpp, alpha)	\
slouken@0
   431
    do {						\
slouken@0
   432
        int i;						\
slouken@0
   433
	Uint16 *src = (Uint16 *)(from);			\
slouken@0
   434
	Uint16 *dst = (Uint16 *)(to);			\
slouken@689
   435
	Uint32 ALPHA = alpha >> 3;			\
slouken@0
   436
	for(i = 0; i < (int)(length); i++) {		\
slouken@0
   437
	    Uint32 s = *src++;				\
slouken@0
   438
	    Uint32 d = *dst;				\
slouken@0
   439
	    s = (s | s << 16) & 0x03e07c1f;		\
slouken@0
   440
	    d = (d | d << 16) & 0x03e07c1f;		\
slouken@689
   441
	    d += (s - d) * ALPHA >> 5;			\
slouken@0
   442
	    d &= 0x03e07c1f;				\
slouken@1428
   443
	    *dst++ = (Uint16)(d | d >> 16);			\
slouken@0
   444
	}						\
slouken@0
   445
    } while(0)
slouken@0
   446
slouken@0
   447
/*
slouken@0
   448
 * The general slow catch-all function, for remaining depths and formats
slouken@0
   449
 */
slouken@0
   450
#define ALPHA_BLIT_ANY(to, from, length, bpp, alpha)			\
slouken@0
   451
    do {								\
slouken@0
   452
        int i;								\
slouken@0
   453
	Uint8 *src = from;						\
slouken@0
   454
	Uint8 *dst = to;						\
slouken@0
   455
	for(i = 0; i < (int)(length); i++) {				\
slouken@0
   456
	    Uint32 s, d;						\
slouken@0
   457
	    unsigned rs, gs, bs, rd, gd, bd;				\
slouken@0
   458
	    switch(bpp) {						\
slouken@0
   459
	    case 2:							\
slouken@0
   460
		s = *(Uint16 *)src;					\
slouken@0
   461
		d = *(Uint16 *)dst;					\
slouken@0
   462
		break;							\
slouken@0
   463
	    case 3:							\
slouken@0
   464
		if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {			\
slouken@0
   465
		    s = (src[0] << 16) | (src[1] << 8) | src[2];	\
slouken@0
   466
		    d = (dst[0] << 16) | (dst[1] << 8) | dst[2];	\
slouken@0
   467
		} else {						\
slouken@0
   468
		    s = (src[2] << 16) | (src[1] << 8) | src[0];	\
slouken@0
   469
		    d = (dst[2] << 16) | (dst[1] << 8) | dst[0];	\
slouken@0
   470
		}							\
slouken@0
   471
		break;							\
slouken@0
   472
	    case 4:							\
slouken@0
   473
		s = *(Uint32 *)src;					\
slouken@0
   474
		d = *(Uint32 *)dst;					\
slouken@0
   475
		break;							\
slouken@0
   476
	    }								\
slouken@0
   477
	    RGB_FROM_PIXEL(s, fmt, rs, gs, bs);				\
slouken@0
   478
	    RGB_FROM_PIXEL(d, fmt, rd, gd, bd);				\
slouken@0
   479
	    rd += (rs - rd) * alpha >> 8;				\
slouken@0
   480
	    gd += (gs - gd) * alpha >> 8;				\
slouken@0
   481
	    bd += (bs - bd) * alpha >> 8;				\
slouken@0
   482
	    PIXEL_FROM_RGB(d, fmt, rd, gd, bd);				\
slouken@0
   483
	    switch(bpp) {						\
slouken@0
   484
	    case 2:							\
slouken@1428
   485
		*(Uint16 *)dst = (Uint16)d;					\
slouken@0
   486
		break;							\
slouken@0
   487
	    case 3:							\
slouken@0
   488
		if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {			\
slouken@1428
   489
		    dst[0] = (Uint8)(d >> 16);					\
slouken@1428
   490
		    dst[1] = (Uint8)(d >> 8);					\
slouken@1428
   491
		    dst[2] = (Uint8)(d);						\
slouken@0
   492
		} else {						\
slouken@1428
   493
		    dst[0] = (Uint8)d;						\
slouken@1428
   494
		    dst[1] = (Uint8)(d >> 8);					\
slouken@1428
   495
		    dst[2] = (Uint8)(d >> 16);					\
slouken@0
   496
		}							\
slouken@0
   497
		break;							\
slouken@0
   498
	    case 4:							\
slouken@0
   499
		*(Uint32 *)dst = d;					\
slouken@0
   500
		break;							\
slouken@0
   501
	    }								\
slouken@0
   502
	    src += bpp;							\
slouken@0
   503
	    dst += bpp;							\
slouken@0
   504
	}								\
slouken@0
   505
    } while(0)
slouken@0
   506
slouken@880
   507
#ifdef MMX_ASMBLIT
slouken@0
   508
slouken@689
   509
#define ALPHA_BLIT32_888_50MMX(to, from, length, bpp, alpha)		\
slouken@689
   510
    do {								\
slouken@689
   511
	Uint32 *srcp = (Uint32 *)(from);				\
slouken@689
   512
	Uint32 *dstp = (Uint32 *)(to);					\
slouken@689
   513
        int i = 0x00fefefe;						\
slouken@689
   514
        movd_m2r(*(&i), mm4);						\
slouken@689
   515
        punpckldq_r2r(mm4, mm4);					\
slouken@689
   516
        i = 0x00010101;							\
slouken@689
   517
        movd_m2r(*(&i), mm3);						\
slouken@689
   518
        punpckldq_r2r(mm3, mm3);					\
slouken@689
   519
        i = (int)(length);						\
slouken@689
   520
        if( i & 1 ) {							\
slouken@689
   521
	  Uint32 s = *srcp++;						\
slouken@689
   522
	  Uint32 d = *dstp;						\
slouken@689
   523
	  *dstp++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)	\
slouken@689
   524
		     + (s & d & 0x00010101);				\
slouken@689
   525
	  i--;								\
slouken@689
   526
	}								\
slouken@689
   527
	for(; i > 0; --i) {						\
slouken@689
   528
	    movq_m2r((*dstp), mm2); /* dst -> mm2 */			\
slouken@689
   529
	    movq_r2r(mm2, mm6);	/* dst -> mm6 */			\
slouken@689
   530
	    movq_m2r((*srcp), mm1); /* src -> mm1 */			\
slouken@689
   531
	    movq_r2r(mm1, mm5);	/* src -> mm5 */			\
slouken@689
   532
	    pand_r2r(mm4, mm6);	/* dst & 0x00fefefe -> mm6 */		\
slouken@689
   533
	    pand_r2r(mm4, mm5); /* src & 0x00fefefe -> mm5 */		\
slouken@689
   534
	    paddd_r2r(mm6, mm5); /* (dst & 0x00fefefe) + (dst & 0x00fefefe) -> mm5 */	\
slouken@689
   535
	    psrld_i2r(1, mm5);						\
slouken@689
   536
	    pand_r2r(mm1, mm2);	/* s & d -> mm2 */			\
slouken@689
   537
	    pand_r2r(mm3, mm2);	/* s & d & 0x00010101 -> mm2 */		\
slouken@689
   538
	    paddd_r2r(mm5, mm2);					\
slouken@689
   539
	    movq_r2m(mm2, (*dstp));					\
slouken@689
   540
	    dstp += 2;							\
slouken@689
   541
	    srcp += 2;							\
slouken@689
   542
	    i--;							\
slouken@689
   543
	}								\
slouken@689
   544
	emms();								\
slouken@689
   545
    } while(0)
slouken@689
   546
slouken@689
   547
#endif
slouken@1895
   548
slouken@0
   549
/*
slouken@0
   550
 * Special case: 50% alpha (alpha=128)
slouken@0
   551
 * This is treated specially because it can be optimized very well, and
slouken@0
   552
 * since it is good for many cases of semi-translucency.
slouken@0
   553
 * The theory is to do all three components at the same time:
slouken@0
   554
 * First zero the lowest bit of each component, which gives us room to
slouken@0
   555
 * add them. Then shift right and add the sum of the lowest bits.
slouken@0
   556
 */
slouken@0
   557
#define ALPHA_BLIT32_888_50(to, from, length, bpp, alpha)		\
slouken@0
   558
    do {								\
slouken@0
   559
        int i;								\
slouken@0
   560
	Uint32 *src = (Uint32 *)(from);					\
slouken@0
   561
	Uint32 *dst = (Uint32 *)(to);					\
slouken@0
   562
	for(i = 0; i < (int)(length); i++) {				\
slouken@0
   563
	    Uint32 s = *src++;						\
slouken@0
   564
	    Uint32 d = *dst;						\
slouken@0
   565
	    *dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)	\
slouken@0
   566
		     + (s & d & 0x00010101);				\
slouken@0
   567
	}								\
slouken@0
   568
    } while(0)
slouken@0
   569
slouken@0
   570
/*
slouken@0
   571
 * For 16bpp, we can actually blend two pixels in parallel, if we take
slouken@0
   572
 * care to shift before we add, not after.
slouken@0
   573
 */
slouken@0
   574
slouken@0
   575
/* helper: blend a single 16 bit pixel at 50% */
slouken@0
   576
#define BLEND16_50(dst, src, mask)			\
slouken@0
   577
    do {						\
slouken@1428
   578
	Uint32 s = *src++;				\
slouken@0
   579
	Uint32 d = *dst;				\
slouken@1428
   580
	*dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) +	\
slouken@1428
   581
	                  (s & d & (~mask & 0xffff)));		\
slouken@0
   582
    } while(0)
slouken@0
   583
slouken@0
   584
/* basic 16bpp blender. mask is the pixels to keep when adding. */
slouken@0
   585
#define ALPHA_BLIT16_50(to, from, length, bpp, alpha, mask)		\
slouken@0
   586
    do {								\
slouken@0
   587
	unsigned n = (length);						\
slouken@0
   588
	Uint16 *src = (Uint16 *)(from);					\
slouken@0
   589
	Uint16 *dst = (Uint16 *)(to);					\
slouken@1456
   590
	if(((uintptr_t)src ^ (uintptr_t)dst) & 3) {			\
slouken@0
   591
	    /* source and destination not in phase, blit one by one */	\
slouken@0
   592
	    while(n--)							\
slouken@0
   593
		BLEND16_50(dst, src, mask);				\
slouken@0
   594
	} else {							\
slouken@1456
   595
	    if((uintptr_t)src & 3) {					\
slouken@0
   596
		/* first odd pixel */					\
slouken@0
   597
		BLEND16_50(dst, src, mask);				\
slouken@0
   598
		n--;							\
slouken@0
   599
	    }								\
slouken@0
   600
	    for(; n > 1; n -= 2) {					\
slouken@0
   601
		Uint32 s = *(Uint32 *)src;				\
slouken@0
   602
		Uint32 d = *(Uint32 *)dst;				\
slouken@0
   603
		*(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1)	\
slouken@0
   604
		               + ((d & (mask | mask << 16)) >> 1)	\
slouken@0
   605
		               + (s & d & (~(mask | mask << 16)));	\
slouken@0
   606
		src += 2;						\
slouken@0
   607
		dst += 2;						\
slouken@0
   608
	    }								\
slouken@0
   609
	    if(n)							\
slouken@0
   610
		BLEND16_50(dst, src, mask); /* last odd pixel */	\
slouken@0
   611
	}								\
slouken@0
   612
    } while(0)
slouken@0
   613
slouken@0
   614
#define ALPHA_BLIT16_565_50(to, from, length, bpp, alpha)	\
slouken@0
   615
    ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xf7de)
slouken@0
   616
slouken@0
   617
#define ALPHA_BLIT16_555_50(to, from, length, bpp, alpha)	\
slouken@0
   618
    ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xfbde)
slouken@0
   619
slouken@880
   620
#ifdef MMX_ASMBLIT
slouken@0
   621
slouken@0
   622
#define CHOOSE_BLIT(blitter, alpha, fmt)				\
slouken@0
   623
    do {								\
slouken@0
   624
        if(alpha == 255) {						\
slouken@0
   625
	    switch(fmt->BytesPerPixel) {				\
slouken@0
   626
	    case 1: blitter(1, Uint8, OPAQUE_BLIT); break;		\
slouken@0
   627
	    case 2: blitter(2, Uint8, OPAQUE_BLIT); break;		\
slouken@0
   628
	    case 3: blitter(3, Uint8, OPAQUE_BLIT); break;		\
slouken@0
   629
	    case 4: blitter(4, Uint16, OPAQUE_BLIT); break;		\
slouken@0
   630
	    }								\
slouken@0
   631
	} else {							\
slouken@0
   632
	    switch(fmt->BytesPerPixel) {				\
slouken@0
   633
	    case 1:							\
slouken@0
   634
		/* No 8bpp alpha blitting */				\
slouken@0
   635
		break;							\
slouken@0
   636
									\
slouken@0
   637
	    case 2:							\
slouken@0
   638
		switch(fmt->Rmask | fmt->Gmask | fmt->Bmask) {		\
slouken@0
   639
		case 0xffff:						\
slouken@0
   640
		    if(fmt->Gmask == 0x07e0				\
slouken@0
   641
		       || fmt->Rmask == 0x07e0				\
slouken@0
   642
		       || fmt->Bmask == 0x07e0) {			\
slouken@0
   643
			if(alpha == 128)				\
slouken@0
   644
			    blitter(2, Uint8, ALPHA_BLIT16_565_50);	\
slouken@0
   645
			else {						\
slouken@739
   646
			    if(SDL_HasMMX())				\
slouken@689
   647
				blitter(2, Uint8, ALPHA_BLIT16_565MMX);	\
slouken@689
   648
			    else					\
slouken@689
   649
				blitter(2, Uint8, ALPHA_BLIT16_565);	\
slouken@689
   650
			}						\
slouken@689
   651
		    } else						\
slouken@689
   652
			goto general16;					\
slouken@689
   653
		    break;						\
slouken@689
   654
									\
slouken@689
   655
		case 0x7fff:						\
slouken@689
   656
		    if(fmt->Gmask == 0x03e0				\
slouken@689
   657
		       || fmt->Rmask == 0x03e0				\
slouken@689
   658
		       || fmt->Bmask == 0x03e0) {			\
slouken@689
   659
			if(alpha == 128)				\
slouken@689
   660
			    blitter(2, Uint8, ALPHA_BLIT16_555_50);	\
slouken@689
   661
			else {						\
slouken@739
   662
			    if(SDL_HasMMX())				\
slouken@689
   663
				blitter(2, Uint8, ALPHA_BLIT16_555MMX);	\
slouken@689
   664
			    else					\
slouken@689
   665
				blitter(2, Uint8, ALPHA_BLIT16_555);	\
slouken@689
   666
			}						\
slouken@689
   667
			break;						\
slouken@689
   668
		    }							\
slouken@689
   669
		    /* fallthrough */					\
slouken@689
   670
									\
slouken@689
   671
		default:						\
slouken@689
   672
		general16:						\
slouken@689
   673
		    blitter(2, Uint8, ALPHA_BLIT_ANY);			\
slouken@689
   674
		}							\
slouken@689
   675
		break;							\
slouken@689
   676
									\
slouken@689
   677
	    case 3:							\
slouken@689
   678
		blitter(3, Uint8, ALPHA_BLIT_ANY);			\
slouken@689
   679
		break;							\
slouken@689
   680
									\
slouken@689
   681
	    case 4:							\
slouken@689
   682
		if((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff	\
slouken@689
   683
		   && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00	\
slouken@689
   684
		       || fmt->Bmask == 0xff00)) {			\
slouken@689
   685
		    if(alpha == 128)					\
slouken@689
   686
		    {							\
slouken@739
   687
			if(SDL_HasMMX())				\
slouken@689
   688
				blitter(4, Uint16, ALPHA_BLIT32_888_50MMX);\
slouken@689
   689
			else						\
slouken@689
   690
				blitter(4, Uint16, ALPHA_BLIT32_888_50);\
slouken@689
   691
		    }							\
slouken@689
   692
		    else						\
slouken@689
   693
		    {							\
slouken@739
   694
			if(SDL_HasMMX())				\
slouken@689
   695
				blitter(4, Uint16, ALPHA_BLIT32_888MMX);\
slouken@689
   696
			else						\
slouken@689
   697
				blitter(4, Uint16, ALPHA_BLIT32_888);	\
slouken@689
   698
		    }							\
slouken@689
   699
		} else							\
slouken@689
   700
		    blitter(4, Uint16, ALPHA_BLIT_ANY);			\
slouken@689
   701
		break;							\
slouken@689
   702
	    }								\
slouken@689
   703
	}								\
slouken@689
   704
    } while(0)
slouken@689
   705
slouken@689
   706
#else
slouken@1895
   707
slouken@689
   708
#define CHOOSE_BLIT(blitter, alpha, fmt)				\
slouken@689
   709
    do {								\
slouken@689
   710
        if(alpha == 255) {						\
slouken@689
   711
	    switch(fmt->BytesPerPixel) {				\
slouken@689
   712
	    case 1: blitter(1, Uint8, OPAQUE_BLIT); break;		\
slouken@689
   713
	    case 2: blitter(2, Uint8, OPAQUE_BLIT); break;		\
slouken@689
   714
	    case 3: blitter(3, Uint8, OPAQUE_BLIT); break;		\
slouken@689
   715
	    case 4: blitter(4, Uint16, OPAQUE_BLIT); break;		\
slouken@689
   716
	    }								\
slouken@689
   717
	} else {							\
slouken@689
   718
	    switch(fmt->BytesPerPixel) {				\
slouken@689
   719
	    case 1:							\
slouken@689
   720
		/* No 8bpp alpha blitting */				\
slouken@689
   721
		break;							\
slouken@689
   722
									\
slouken@689
   723
	    case 2:							\
slouken@689
   724
		switch(fmt->Rmask | fmt->Gmask | fmt->Bmask) {		\
slouken@689
   725
		case 0xffff:						\
slouken@689
   726
		    if(fmt->Gmask == 0x07e0				\
slouken@689
   727
		       || fmt->Rmask == 0x07e0				\
slouken@689
   728
		       || fmt->Bmask == 0x07e0) {			\
slouken@689
   729
			if(alpha == 128)				\
slouken@689
   730
			    blitter(2, Uint8, ALPHA_BLIT16_565_50);	\
slouken@689
   731
			else {						\
slouken@0
   732
			    blitter(2, Uint8, ALPHA_BLIT16_565);	\
slouken@0
   733
			}						\
slouken@0
   734
		    } else						\
slouken@0
   735
			goto general16;					\
slouken@0
   736
		    break;						\
slouken@0
   737
									\
slouken@0
   738
		case 0x7fff:						\
slouken@0
   739
		    if(fmt->Gmask == 0x03e0				\
slouken@0
   740
		       || fmt->Rmask == 0x03e0				\
slouken@0
   741
		       || fmt->Bmask == 0x03e0) {			\
slouken@0
   742
			if(alpha == 128)				\
slouken@0
   743
			    blitter(2, Uint8, ALPHA_BLIT16_555_50);	\
slouken@0
   744
			else {						\
slouken@0
   745
			    blitter(2, Uint8, ALPHA_BLIT16_555);	\
slouken@0
   746
			}						\
slouken@0
   747
			break;						\
slouken@0
   748
		    }							\
slouken@0
   749
		    /* fallthrough */					\
slouken@0
   750
									\
slouken@0
   751
		default:						\
slouken@0
   752
		general16:						\
slouken@0
   753
		    blitter(2, Uint8, ALPHA_BLIT_ANY);			\
slouken@0
   754
		}							\
slouken@0
   755
		break;							\
slouken@0
   756
									\
slouken@0
   757
	    case 3:							\
slouken@0
   758
		blitter(3, Uint8, ALPHA_BLIT_ANY);			\
slouken@0
   759
		break;							\
slouken@0
   760
									\
slouken@0
   761
	    case 4:							\
slouken@0
   762
		if((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff	\
slouken@0
   763
		   && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00	\
slouken@0
   764
		       || fmt->Bmask == 0xff00)) {			\
slouken@0
   765
		    if(alpha == 128)					\
slouken@0
   766
			blitter(4, Uint16, ALPHA_BLIT32_888_50);	\
slouken@0
   767
		    else						\
slouken@0
   768
			blitter(4, Uint16, ALPHA_BLIT32_888);		\
slouken@0
   769
		} else							\
slouken@0
   770
		    blitter(4, Uint16, ALPHA_BLIT_ANY);			\
slouken@0
   771
		break;							\
slouken@0
   772
	    }								\
slouken@0
   773
	}								\
slouken@0
   774
    } while(0)
slouken@0
   775
slouken@689
   776
#endif
slouken@0
   777
slouken@0
   778
/*
slouken@0
   779
 * This takes care of the case when the surface is clipped on the left and/or
slouken@0
   780
 * right. Top clipping has already been taken care of.
slouken@0
   781
 */
slouken@1895
   782
static void
slouken@1895
   783
RLEClipBlit(int w, Uint8 * srcbuf, SDL_Surface * dst,
slouken@1895
   784
            Uint8 * dstbuf, SDL_Rect * srcrect, unsigned alpha)
slouken@0
   785
{
slouken@0
   786
    SDL_PixelFormat *fmt = dst->format;
slouken@0
   787
slouken@0
   788
#define RLECLIPBLIT(bpp, Type, do_blit)					   \
slouken@0
   789
    do {								   \
slouken@0
   790
	int linecount = srcrect->h;					   \
slouken@0
   791
	int ofs = 0;							   \
slouken@0
   792
	int left = srcrect->x;						   \
slouken@0
   793
	int right = left + srcrect->w;					   \
slouken@0
   794
	dstbuf -= left * bpp;						   \
slouken@0
   795
	for(;;) {							   \
slouken@0
   796
	    int run;							   \
slouken@0
   797
	    ofs += *(Type *)srcbuf;					   \
slouken@0
   798
	    run = ((Type *)srcbuf)[1];					   \
slouken@0
   799
	    srcbuf += 2 * sizeof(Type);					   \
slouken@0
   800
	    if(run) {							   \
slouken@0
   801
		/* clip to left and right borders */			   \
slouken@0
   802
		if(ofs < right) {					   \
slouken@0
   803
		    int start = 0;					   \
slouken@0
   804
		    int len = run;					   \
slouken@0
   805
		    int startcol;					   \
slouken@0
   806
		    if(left - ofs > 0) {				   \
slouken@0
   807
			start = left - ofs;				   \
slouken@0
   808
			len -= start;					   \
slouken@0
   809
			if(len <= 0)					   \
slouken@0
   810
			    goto nocopy ## bpp ## do_blit;		   \
slouken@0
   811
		    }							   \
slouken@0
   812
		    startcol = ofs + start;				   \
slouken@0
   813
		    if(len > right - startcol)				   \
slouken@0
   814
			len = right - startcol;				   \
slouken@0
   815
		    do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
slouken@0
   816
			    len, bpp, alpha);				   \
slouken@0
   817
		}							   \
slouken@0
   818
	    nocopy ## bpp ## do_blit:					   \
slouken@0
   819
		srcbuf += run * bpp;					   \
slouken@0
   820
		ofs += run;						   \
slouken@0
   821
	    } else if(!ofs)						   \
slouken@0
   822
		break;							   \
slouken@0
   823
	    if(ofs == w) {						   \
slouken@0
   824
		ofs = 0;						   \
slouken@0
   825
		dstbuf += dst->pitch;					   \
slouken@0
   826
		if(!--linecount)					   \
slouken@0
   827
		    break;						   \
slouken@0
   828
	    }								   \
slouken@0
   829
	}								   \
slouken@0
   830
    } while(0)
slouken@0
   831
slouken@0
   832
    CHOOSE_BLIT(RLECLIPBLIT, alpha, fmt);
slouken@0
   833
slouken@0
   834
#undef RLECLIPBLIT
slouken@0
   835
slouken@0
   836
}
slouken@0
   837
slouken@0
   838
slouken@0
   839
/* blit a colorkeyed RLE surface */
slouken@1895
   840
int
slouken@1895
   841
SDL_RLEBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   842
            SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   843
{
slouken@1895
   844
    Uint8 *dstbuf;
slouken@1895
   845
    Uint8 *srcbuf;
slouken@1895
   846
    int x, y;
slouken@1895
   847
    int w = src->w;
slouken@1895
   848
    unsigned alpha;
slouken@0
   849
slouken@1895
   850
    /* Lock the destination if necessary */
slouken@1895
   851
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
   852
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
   853
            return (-1);
slouken@1895
   854
        }
slouken@1895
   855
    }
slouken@0
   856
slouken@1895
   857
    /* Set up the source and destination pointers */
slouken@1895
   858
    x = dstrect->x;
slouken@1895
   859
    y = dstrect->y;
slouken@1895
   860
    dstbuf = (Uint8 *) dst->pixels
slouken@1895
   861
        + y * dst->pitch + x * src->format->BytesPerPixel;
slouken@2257
   862
    srcbuf = (Uint8 *) src->map->data;
slouken@0
   863
slouken@1895
   864
    {
slouken@1895
   865
        /* skip lines at the top if neccessary */
slouken@1895
   866
        int vskip = srcrect->y;
slouken@1895
   867
        int ofs = 0;
slouken@1895
   868
        if (vskip) {
slouken@0
   869
slouken@0
   870
#define RLESKIP(bpp, Type)			\
slouken@0
   871
		for(;;) {			\
slouken@0
   872
		    int run;			\
slouken@0
   873
		    ofs += *(Type *)srcbuf;	\
slouken@0
   874
		    run = ((Type *)srcbuf)[1];	\
slouken@0
   875
		    srcbuf += sizeof(Type) * 2;	\
slouken@0
   876
		    if(run) {			\
slouken@0
   877
			srcbuf += run * bpp;	\
slouken@0
   878
			ofs += run;		\
slouken@0
   879
		    } else if(!ofs)		\
slouken@0
   880
			goto done;		\
slouken@0
   881
		    if(ofs == w) {		\
slouken@0
   882
			ofs = 0;		\
slouken@0
   883
			if(!--vskip)		\
slouken@0
   884
			    break;		\
slouken@0
   885
		    }				\
slouken@0
   886
		}
slouken@0
   887
slouken@1895
   888
            switch (src->format->BytesPerPixel) {
slouken@1895
   889
            case 1:
slouken@1895
   890
                RLESKIP(1, Uint8);
slouken@1895
   891
                break;
slouken@1895
   892
            case 2:
slouken@1895
   893
                RLESKIP(2, Uint8);
slouken@1895
   894
                break;
slouken@1895
   895
            case 3:
slouken@1895
   896
                RLESKIP(3, Uint8);
slouken@1895
   897
                break;
slouken@1895
   898
            case 4:
slouken@1895
   899
                RLESKIP(4, Uint16);
slouken@1895
   900
                break;
slouken@1895
   901
            }
slouken@0
   902
slouken@0
   903
#undef RLESKIP
slouken@0
   904
slouken@1895
   905
        }
slouken@1895
   906
    }
slouken@0
   907
slouken@2266
   908
    alpha = src->map->info.a;
slouken@1895
   909
    /* if left or right edge clipping needed, call clip blit */
slouken@1895
   910
    if (srcrect->x || srcrect->w != src->w) {
slouken@1895
   911
        RLEClipBlit(w, srcbuf, dst, dstbuf, srcrect, alpha);
slouken@1895
   912
    } else {
slouken@1895
   913
        SDL_PixelFormat *fmt = src->format;
slouken@0
   914
slouken@0
   915
#define RLEBLIT(bpp, Type, do_blit)					      \
slouken@0
   916
	    do {							      \
slouken@0
   917
		int linecount = srcrect->h;				      \
slouken@0
   918
		int ofs = 0;						      \
slouken@0
   919
		for(;;) {						      \
slouken@0
   920
		    unsigned run;					      \
slouken@0
   921
		    ofs += *(Type *)srcbuf;				      \
slouken@0
   922
		    run = ((Type *)srcbuf)[1];				      \
slouken@0
   923
		    srcbuf += 2 * sizeof(Type);				      \
slouken@0
   924
		    if(run) {						      \
slouken@0
   925
			do_blit(dstbuf + ofs * bpp, srcbuf, run, bpp, alpha); \
slouken@0
   926
			srcbuf += run * bpp;				      \
slouken@0
   927
			ofs += run;					      \
slouken@0
   928
		    } else if(!ofs)					      \
slouken@0
   929
			break;						      \
slouken@0
   930
		    if(ofs == w) {					      \
slouken@0
   931
			ofs = 0;					      \
slouken@0
   932
			dstbuf += dst->pitch;				      \
slouken@0
   933
			if(!--linecount)				      \
slouken@0
   934
			    break;					      \
slouken@0
   935
		    }							      \
slouken@0
   936
		}							      \
slouken@0
   937
	    } while(0)
slouken@0
   938
slouken@1895
   939
        CHOOSE_BLIT(RLEBLIT, alpha, fmt);
slouken@0
   940
slouken@0
   941
#undef RLEBLIT
slouken@1895
   942
    }
slouken@0
   943
slouken@1895
   944
  done:
slouken@1895
   945
    /* Unlock the destination if necessary */
slouken@1895
   946
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
   947
        SDL_UnlockSurface(dst);
slouken@1895
   948
    }
slouken@1895
   949
    return (0);
slouken@0
   950
}
slouken@0
   951
slouken@0
   952
#undef OPAQUE_BLIT
slouken@0
   953
slouken@0
   954
/*
slouken@0
   955
 * Per-pixel blitting macros for translucent pixels:
slouken@0
   956
 * These use the same techniques as the per-surface blitting macros
slouken@0
   957
 */
slouken@0
   958
slouken@0
   959
/*
slouken@0
   960
 * For 32bpp pixels, we have made sure the alpha is stored in the top
slouken@0
   961
 * 8 bits, so proceed as usual
slouken@0
   962
 */
slouken@0
   963
#define BLIT_TRANSL_888(src, dst)				\
slouken@0
   964
    do {							\
slouken@0
   965
        Uint32 s = src;						\
slouken@0
   966
	Uint32 d = dst;						\
slouken@0
   967
	unsigned alpha = s >> 24;				\
slouken@0
   968
	Uint32 s1 = s & 0xff00ff;				\
slouken@0
   969
	Uint32 d1 = d & 0xff00ff;				\
slouken@0
   970
	d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;	\
slouken@0
   971
	s &= 0xff00;						\
slouken@0
   972
	d &= 0xff00;						\
slouken@0
   973
	d = (d + ((s - d) * alpha >> 8)) & 0xff00;		\
slouken@0
   974
	dst = d1 | d;						\
slouken@0
   975
    } while(0)
slouken@0
   976
slouken@0
   977
/*
slouken@0
   978
 * For 16bpp pixels, we have stored the 5 most significant alpha bits in
slouken@0
   979
 * bits 5-10. As before, we can process all 3 RGB components at the same time.
slouken@0
   980
 */
slouken@0
   981
#define BLIT_TRANSL_565(src, dst)		\
slouken@0
   982
    do {					\
slouken@1428
   983
	Uint32 s = src;				\
slouken@0
   984
	Uint32 d = dst;				\
slouken@0
   985
	unsigned alpha = (s & 0x3e0) >> 5;	\
slouken@0
   986
	s &= 0x07e0f81f;			\
slouken@0
   987
	d = (d | d << 16) & 0x07e0f81f;		\
slouken@0
   988
	d += (s - d) * alpha >> 5;		\
slouken@0
   989
	d &= 0x07e0f81f;			\
slouken@1428
   990
	dst = (Uint16)(d | d >> 16);			\
slouken@0
   991
    } while(0)
slouken@0
   992
slouken@0
   993
#define BLIT_TRANSL_555(src, dst)		\
slouken@0
   994
    do {					\
slouken@1428
   995
	Uint32 s = src;				\
slouken@0
   996
	Uint32 d = dst;				\
slouken@0
   997
	unsigned alpha = (s & 0x3e0) >> 5;	\
slouken@0
   998
	s &= 0x03e07c1f;			\
slouken@0
   999
	d = (d | d << 16) & 0x03e07c1f;		\
slouken@0
  1000
	d += (s - d) * alpha >> 5;		\
slouken@0
  1001
	d &= 0x03e07c1f;			\
slouken@1428
  1002
	dst = (Uint16)(d | d >> 16);			\
slouken@0
  1003
    } while(0)
slouken@0
  1004
slouken@0
  1005
/* used to save the destination format in the encoding. Designed to be
slouken@0
  1006
   macro-compatible with SDL_PixelFormat but without the unneeded fields */
slouken@1895
  1007
typedef struct
slouken@1895
  1008
{
slouken@1895
  1009
    Uint8 BytesPerPixel;
slouken@1895
  1010
    Uint8 Rloss;
slouken@1895
  1011
    Uint8 Gloss;
slouken@1895
  1012
    Uint8 Bloss;
slouken@1895
  1013
    Uint8 Rshift;
slouken@1895
  1014
    Uint8 Gshift;
slouken@1895
  1015
    Uint8 Bshift;
slouken@1895
  1016
    Uint8 Ashift;
slouken@1895
  1017
    Uint32 Rmask;
slouken@1895
  1018
    Uint32 Gmask;
slouken@1895
  1019
    Uint32 Bmask;
slouken@1895
  1020
    Uint32 Amask;
slouken@0
  1021
} RLEDestFormat;
slouken@0
  1022
slouken@0
  1023
/* blit a pixel-alpha RLE surface clipped at the right and/or left edges */
slouken@1895
  1024
static void
slouken@1895
  1025
RLEAlphaClipBlit(int w, Uint8 * srcbuf, SDL_Surface * dst,
slouken@1895
  1026
                 Uint8 * dstbuf, SDL_Rect * srcrect)
slouken@0
  1027
{
slouken@0
  1028
    SDL_PixelFormat *df = dst->format;
slouken@0
  1029
    /*
slouken@0
  1030
     * clipped blitter: Ptype is the destination pixel type,
slouken@0
  1031
     * Ctype the translucent count type, and do_blend the macro
slouken@0
  1032
     * to blend one pixel.
slouken@0
  1033
     */
slouken@0
  1034
#define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend)			  \
slouken@0
  1035
    do {								  \
slouken@0
  1036
	int linecount = srcrect->h;					  \
slouken@0
  1037
	int left = srcrect->x;						  \
slouken@0
  1038
	int right = left + srcrect->w;					  \
slouken@0
  1039
	dstbuf -= left * sizeof(Ptype);					  \
slouken@0
  1040
	do {								  \
slouken@0
  1041
	    int ofs = 0;						  \
slouken@0
  1042
	    /* blit opaque pixels on one line */			  \
slouken@0
  1043
	    do {							  \
slouken@0
  1044
		unsigned run;						  \
slouken@0
  1045
		ofs += ((Ctype *)srcbuf)[0];				  \
slouken@0
  1046
		run = ((Ctype *)srcbuf)[1];				  \
slouken@0
  1047
		srcbuf += 2 * sizeof(Ctype);				  \
slouken@0
  1048
		if(run) {						  \
slouken@0
  1049
		    /* clip to left and right borders */		  \
slouken@0
  1050
		    int cofs = ofs;					  \
slouken@0
  1051
		    int crun = run;					  \
slouken@0
  1052
		    if(left - cofs > 0) {				  \
slouken@0
  1053
			crun -= left - cofs;				  \
slouken@0
  1054
			cofs = left;					  \
slouken@0
  1055
		    }							  \
slouken@0
  1056
		    if(crun > right - cofs)				  \
slouken@0
  1057
			crun = right - cofs;				  \
slouken@0
  1058
		    if(crun > 0)					  \
slouken@1
  1059
			PIXEL_COPY(dstbuf + cofs * sizeof(Ptype),	  \
slouken@0
  1060
				   srcbuf + (cofs - ofs) * sizeof(Ptype), \
slouken@1
  1061
				   (unsigned)crun, sizeof(Ptype));	  \
slouken@0
  1062
		    srcbuf += run * sizeof(Ptype);			  \
slouken@0
  1063
		    ofs += run;						  \
slouken@0
  1064
		} else if(!ofs)						  \
slouken@0
  1065
		    return;						  \
slouken@0
  1066
	    } while(ofs < w);						  \
slouken@0
  1067
	    /* skip padding if necessary */				  \
slouken@0
  1068
	    if(sizeof(Ptype) == 2)					  \
slouken@1456
  1069
		srcbuf += (uintptr_t)srcbuf & 2;			  \
slouken@0
  1070
	    /* blit translucent pixels on the same line */		  \
slouken@0
  1071
	    ofs = 0;							  \
slouken@0
  1072
	    do {							  \
slouken@0
  1073
		unsigned run;						  \
slouken@0
  1074
		ofs += ((Uint16 *)srcbuf)[0];				  \
slouken@0
  1075
		run = ((Uint16 *)srcbuf)[1];				  \
slouken@0
  1076
		srcbuf += 4;						  \
slouken@0
  1077
		if(run) {						  \
slouken@0
  1078
		    /* clip to left and right borders */		  \
slouken@0
  1079
		    int cofs = ofs;					  \
slouken@0
  1080
		    int crun = run;					  \
slouken@0
  1081
		    if(left - cofs > 0) {				  \
slouken@0
  1082
			crun -= left - cofs;				  \
slouken@0
  1083
			cofs = left;					  \
slouken@0
  1084
		    }							  \
slouken@0
  1085
		    if(crun > right - cofs)				  \
slouken@0
  1086
			crun = right - cofs;				  \
slouken@0
  1087
		    if(crun > 0) {					  \
slouken@0
  1088
			Ptype *dst = (Ptype *)dstbuf + cofs;		  \
slouken@0
  1089
			Uint32 *src = (Uint32 *)srcbuf + (cofs - ofs);	  \
slouken@0
  1090
			int i;						  \
slouken@0
  1091
			for(i = 0; i < crun; i++)			  \
slouken@0
  1092
			    do_blend(src[i], dst[i]);			  \
slouken@0
  1093
		    }							  \
slouken@0
  1094
		    srcbuf += run * 4;					  \
slouken@0
  1095
		    ofs += run;						  \
slouken@0
  1096
		}							  \
slouken@0
  1097
	    } while(ofs < w);						  \
slouken@0
  1098
	    dstbuf += dst->pitch;					  \
slouken@0
  1099
	} while(--linecount);						  \
slouken@0
  1100
    } while(0)
slouken@0
  1101
slouken@1895
  1102
    switch (df->BytesPerPixel) {
slouken@0
  1103
    case 2:
slouken@1895
  1104
        if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0)
slouken@1895
  1105
            RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565);
slouken@1895
  1106
        else
slouken@1895
  1107
            RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555);
slouken@1895
  1108
        break;
slouken@0
  1109
    case 4:
slouken@1895
  1110
        RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888);
slouken@1895
  1111
        break;
slouken@0
  1112
    }
slouken@0
  1113
}
slouken@0
  1114
slouken@0
  1115
/* blit a pixel-alpha RLE surface */
slouken@1895
  1116
int
slouken@1895
  1117
SDL_RLEAlphaBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
  1118
                 SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
  1119
{
slouken@0
  1120
    int x, y;
slouken@0
  1121
    int w = src->w;
slouken@0
  1122
    Uint8 *srcbuf, *dstbuf;
slouken@0
  1123
    SDL_PixelFormat *df = dst->format;
slouken@0
  1124
slouken@0
  1125
    /* Lock the destination if necessary */
slouken@1895
  1126
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
  1127
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
  1128
            return -1;
slouken@1895
  1129
        }
slouken@0
  1130
    }
slouken@0
  1131
slouken@0
  1132
    x = dstrect->x;
slouken@0
  1133
    y = dstrect->y;
slouken@1895
  1134
    dstbuf = (Uint8 *) dst->pixels + y * dst->pitch + x * df->BytesPerPixel;
slouken@2257
  1135
    srcbuf = (Uint8 *) src->map->data + sizeof(RLEDestFormat);
slouken@0
  1136
slouken@0
  1137
    {
slouken@1895
  1138
        /* skip lines at the top if necessary */
slouken@1895
  1139
        int vskip = srcrect->y;
slouken@1895
  1140
        if (vskip) {
slouken@1895
  1141
            int ofs;
slouken@1895
  1142
            if (df->BytesPerPixel == 2) {
slouken@1895
  1143
                /* the 16/32 interleaved format */
slouken@1895
  1144
                do {
slouken@1895
  1145
                    /* skip opaque line */
slouken@1895
  1146
                    ofs = 0;
slouken@1895
  1147
                    do {
slouken@1895
  1148
                        int run;
slouken@1895
  1149
                        ofs += srcbuf[0];
slouken@1895
  1150
                        run = srcbuf[1];
slouken@1895
  1151
                        srcbuf += 2;
slouken@1895
  1152
                        if (run) {
slouken@1895
  1153
                            srcbuf += 2 * run;
slouken@1895
  1154
                            ofs += run;
slouken@1895
  1155
                        } else if (!ofs)
slouken@1895
  1156
                            goto done;
dalton@2669
  1157
                    } while (ofs < w);
slouken@0
  1158
slouken@1895
  1159
                    /* skip padding */
slouken@1895
  1160
                    srcbuf += (uintptr_t) srcbuf & 2;
slouken@0
  1161
slouken@1895
  1162
                    /* skip translucent line */
slouken@1895
  1163
                    ofs = 0;
slouken@1895
  1164
                    do {
slouken@1895
  1165
                        int run;
slouken@1895
  1166
                        ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1167
                        run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1168
                        srcbuf += 4 * (run + 1);
slouken@1895
  1169
                        ofs += run;
dalton@2669
  1170
                    } while (ofs < w);
dalton@2669
  1171
                } while (--vskip);
slouken@1895
  1172
            } else {
slouken@1895
  1173
                /* the 32/32 interleaved format */
slouken@1895
  1174
                vskip <<= 1;    /* opaque and translucent have same format */
slouken@1895
  1175
                do {
slouken@1895
  1176
                    ofs = 0;
slouken@1895
  1177
                    do {
slouken@1895
  1178
                        int run;
slouken@1895
  1179
                        ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1180
                        run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1181
                        srcbuf += 4;
slouken@1895
  1182
                        if (run) {
slouken@1895
  1183
                            srcbuf += 4 * run;
slouken@1895
  1184
                            ofs += run;
slouken@1895
  1185
                        } else if (!ofs)
slouken@1895
  1186
                            goto done;
dalton@2669
  1187
                    } while (ofs < w);
dalton@2669
  1188
                } while (--vskip);
slouken@1895
  1189
            }
slouken@1895
  1190
        }
slouken@0
  1191
    }
slouken@0
  1192
slouken@0
  1193
    /* if left or right edge clipping needed, call clip blit */
slouken@1895
  1194
    if (srcrect->x || srcrect->w != src->w) {
slouken@1895
  1195
        RLEAlphaClipBlit(w, srcbuf, dst, dstbuf, srcrect);
slouken@0
  1196
    } else {
slouken@0
  1197
slouken@1895
  1198
        /*
slouken@1895
  1199
         * non-clipped blitter. Ptype is the destination pixel type,
slouken@1895
  1200
         * Ctype the translucent count type, and do_blend the
slouken@1895
  1201
         * macro to blend one pixel.
slouken@1895
  1202
         */
slouken@0
  1203
#define RLEALPHABLIT(Ptype, Ctype, do_blend)				 \
slouken@0
  1204
	do {								 \
slouken@0
  1205
	    int linecount = srcrect->h;					 \
slouken@0
  1206
	    do {							 \
slouken@0
  1207
		int ofs = 0;						 \
slouken@0
  1208
		/* blit opaque pixels on one line */			 \
slouken@0
  1209
		do {							 \
slouken@0
  1210
		    unsigned run;					 \
slouken@0
  1211
		    ofs += ((Ctype *)srcbuf)[0];			 \
slouken@0
  1212
		    run = ((Ctype *)srcbuf)[1];				 \
slouken@0
  1213
		    srcbuf += 2 * sizeof(Ctype);			 \
slouken@0
  1214
		    if(run) {						 \
slouken@1
  1215
			PIXEL_COPY(dstbuf + ofs * sizeof(Ptype), srcbuf, \
slouken@1
  1216
				   run, sizeof(Ptype));			 \
slouken@0
  1217
			srcbuf += run * sizeof(Ptype);			 \
slouken@0
  1218
			ofs += run;					 \
slouken@0
  1219
		    } else if(!ofs)					 \
slouken@0
  1220
			goto done;					 \
slouken@0
  1221
		} while(ofs < w);					 \
slouken@0
  1222
		/* skip padding if necessary */				 \
slouken@0
  1223
		if(sizeof(Ptype) == 2)					 \
slouken@1456
  1224
		    srcbuf += (uintptr_t)srcbuf & 2;		 	 \
slouken@0
  1225
		/* blit translucent pixels on the same line */		 \
slouken@0
  1226
		ofs = 0;						 \
slouken@0
  1227
		do {							 \
slouken@0
  1228
		    unsigned run;					 \
slouken@0
  1229
		    ofs += ((Uint16 *)srcbuf)[0];			 \
slouken@0
  1230
		    run = ((Uint16 *)srcbuf)[1];			 \
slouken@0
  1231
		    srcbuf += 4;					 \
slouken@0
  1232
		    if(run) {						 \
slouken@0
  1233
			Ptype *dst = (Ptype *)dstbuf + ofs;		 \
slouken@0
  1234
			unsigned i;					 \
slouken@0
  1235
			for(i = 0; i < run; i++) {			 \
slouken@0
  1236
			    Uint32 src = *(Uint32 *)srcbuf;		 \
slouken@0
  1237
			    do_blend(src, *dst);			 \
slouken@0
  1238
			    srcbuf += 4;				 \
slouken@0
  1239
			    dst++;					 \
slouken@0
  1240
			}						 \
slouken@0
  1241
			ofs += run;					 \
slouken@0
  1242
		    }							 \
slouken@0
  1243
		} while(ofs < w);					 \
slouken@0
  1244
		dstbuf += dst->pitch;					 \
slouken@0
  1245
	    } while(--linecount);					 \
slouken@0
  1246
	} while(0)
slouken@0
  1247
slouken@1895
  1248
        switch (df->BytesPerPixel) {
slouken@1895
  1249
        case 2:
slouken@1895
  1250
            if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0
slouken@1895
  1251
                || df->Bmask == 0x07e0)
slouken@1895
  1252
                RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
slouken@1895
  1253
            else
slouken@1895
  1254
                RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
slouken@1895
  1255
            break;
slouken@1895
  1256
        case 4:
slouken@1895
  1257
            RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
slouken@1895
  1258
            break;
slouken@1895
  1259
        }
slouken@0
  1260
    }
slouken@0
  1261
slouken@1895
  1262
  done:
slouken@0
  1263
    /* Unlock the destination if necessary */
slouken@1895
  1264
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
  1265
        SDL_UnlockSurface(dst);
slouken@0
  1266
    }
slouken@0
  1267
    return 0;
slouken@0
  1268
}
slouken@0
  1269
slouken@0
  1270
/*
slouken@0
  1271
 * Auxiliary functions:
slouken@0
  1272
 * The encoding functions take 32bpp rgb + a, and
slouken@0
  1273
 * return the number of bytes copied to the destination.
slouken@0
  1274
 * The decoding functions copy to 32bpp rgb + a, and
slouken@0
  1275
 * return the number of bytes copied from the source.
slouken@0
  1276
 * These are only used in the encoder and un-RLE code and are therefore not
slouken@0
  1277
 * highly optimised.
slouken@0
  1278
 */
slouken@0
  1279
slouken@0
  1280
/* encode 32bpp rgb + a into 16bpp rgb, losing alpha */
slouken@1895
  1281
static int
slouken@1895
  1282
copy_opaque_16(void *dst, Uint32 * src, int n,
slouken@1895
  1283
               SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
  1284
{
slouken@0
  1285
    int i;
slouken@0
  1286
    Uint16 *d = dst;
slouken@1895
  1287
    for (i = 0; i < n; i++) {
slouken@1895
  1288
        unsigned r, g, b;
slouken@1895
  1289
        RGB_FROM_PIXEL(*src, sfmt, r, g, b);
slouken@1895
  1290
        PIXEL_FROM_RGB(*d, dfmt, r, g, b);
slouken@1895
  1291
        src++;
slouken@1895
  1292
        d++;
slouken@0
  1293
    }
slouken@0
  1294
    return n * 2;
slouken@0
  1295
}
slouken@0
  1296
slouken@0
  1297
/* decode opaque pixels from 16bpp to 32bpp rgb + a */
slouken@1895
  1298
static int
slouken@1895
  1299
uncopy_opaque_16(Uint32 * dst, void *src, int n,
slouken@1895
  1300
                 RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
  1301
{
slouken@0
  1302
    int i;
slouken@0
  1303
    Uint16 *s = src;
slouken@0
  1304
    unsigned alpha = dfmt->Amask ? 255 : 0;
slouken@1895
  1305
    for (i = 0; i < n; i++) {
slouken@1895
  1306
        unsigned r, g, b;
slouken@1895
  1307
        RGB_FROM_PIXEL(*s, sfmt, r, g, b);
slouken@1895
  1308
        PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, alpha);
slouken@1895
  1309
        s++;
slouken@1895
  1310
        dst++;
slouken@0
  1311
    }
slouken@0
  1312
    return n * 2;
slouken@0
  1313
}
slouken@0
  1314
slouken@0
  1315
slouken@0
  1316
slouken@0
  1317
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565 */
slouken@1895
  1318
static int
slouken@1895
  1319
copy_transl_565(void *dst, Uint32 * src, int n,
slouken@1895
  1320
                SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
  1321
{
slouken@0
  1322
    int i;
slouken@0
  1323
    Uint32 *d = dst;
slouken@1895
  1324
    for (i = 0; i < n; i++) {
slouken@1895
  1325
        unsigned r, g, b, a;
slouken@1895
  1326
        Uint16 pix;
slouken@1895
  1327
        RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@1895
  1328
        PIXEL_FROM_RGB(pix, dfmt, r, g, b);
slouken@1895
  1329
        *d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
slouken@1895
  1330
        src++;
slouken@1895
  1331
        d++;
slouken@0
  1332
    }
slouken@0
  1333
    return n * 4;
slouken@0
  1334
}
slouken@0
  1335
slouken@0
  1336
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555 */
slouken@1895
  1337
static int
slouken@1895
  1338
copy_transl_555(void *dst, Uint32 * src, int n,
slouken@1895
  1339
                SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
  1340
{
slouken@0
  1341
    int i;
slouken@0
  1342
    Uint32 *d = dst;
slouken@1895
  1343
    for (i = 0; i < n; i++) {
slouken@1895
  1344
        unsigned r, g, b, a;
slouken@1895
  1345
        Uint16 pix;
slouken@1895
  1346
        RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@1895
  1347
        PIXEL_FROM_RGB(pix, dfmt, r, g, b);
slouken@1895
  1348
        *d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
slouken@1895
  1349
        src++;
slouken@1895
  1350
        d++;
slouken@0
  1351
    }
slouken@0
  1352
    return n * 4;
slouken@0
  1353
}
slouken@0
  1354
slouken@0
  1355
/* decode translucent pixels from 32bpp GORAB to 32bpp rgb + a */
slouken@1895
  1356
static int
slouken@1895
  1357
uncopy_transl_16(Uint32 * dst, void *src, int n,
slouken@1895
  1358
                 RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
  1359
{
slouken@0
  1360
    int i;
slouken@0
  1361
    Uint32 *s = src;
slouken@1895
  1362
    for (i = 0; i < n; i++) {
slouken@1895
  1363
        unsigned r, g, b, a;
slouken@1895
  1364
        Uint32 pix = *s++;
slouken@1895
  1365
        a = (pix & 0x3e0) >> 2;
slouken@1895
  1366
        pix = (pix & ~0x3e0) | pix >> 16;
slouken@1895
  1367
        RGB_FROM_PIXEL(pix, sfmt, r, g, b);
slouken@1895
  1368
        PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
slouken@1895
  1369
        dst++;
slouken@0
  1370
    }
slouken@0
  1371
    return n * 4;
slouken@0
  1372
}
slouken@0
  1373
slouken@0
  1374
/* encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
slouken@1895
  1375
static int
slouken@1895
  1376
copy_32(void *dst, Uint32 * src, int n,
slouken@1895
  1377
        SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
  1378
{
slouken@0
  1379
    int i;
slouken@0
  1380
    Uint32 *d = dst;
slouken@1895
  1381
    for (i = 0; i < n; i++) {
slouken@1895
  1382
        unsigned r, g, b, a;
slouken@1895
  1383
        Uint32 pixel;
slouken@1895
  1384
        RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@1895
  1385
        PIXEL_FROM_RGB(pixel, dfmt, r, g, b);
slouken@1895
  1386
        *d++ = pixel | a << 24;
slouken@1895
  1387
        src++;
slouken@0
  1388
    }
slouken@0
  1389
    return n * 4;
slouken@0
  1390
}
slouken@0
  1391
slouken@0
  1392
/* decode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
slouken@1895
  1393
static int
slouken@1895
  1394
uncopy_32(Uint32 * dst, void *src, int n,
slouken@1895
  1395
          RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
  1396
{
slouken@0
  1397
    int i;
slouken@0
  1398
    Uint32 *s = src;
slouken@1895
  1399
    for (i = 0; i < n; i++) {
slouken@1895
  1400
        unsigned r, g, b, a;
slouken@1895
  1401
        Uint32 pixel = *s++;
slouken@1895
  1402
        RGB_FROM_PIXEL(pixel, sfmt, r, g, b);
slouken@1895
  1403
        a = pixel >> 24;
slouken@1895
  1404
        PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
slouken@1895
  1405
        dst++;
slouken@0
  1406
    }
slouken@0
  1407
    return n * 4;
slouken@0
  1408
}
slouken@0
  1409
slouken@0
  1410
#define ISOPAQUE(pixel, fmt) ((((pixel) & fmt->Amask) >> fmt->Ashift) == 255)
slouken@0
  1411
slouken@0
  1412
#define ISTRANSL(pixel, fmt)	\
slouken@0
  1413
    ((unsigned)((((pixel) & fmt->Amask) >> fmt->Ashift) - 1U) < 254U)
slouken@0
  1414
slouken@0
  1415
/* convert surface to be quickly alpha-blittable onto dest, if possible */
slouken@1895
  1416
static int
slouken@1895
  1417
RLEAlphaSurface(SDL_Surface * surface)
slouken@0
  1418
{
slouken@0
  1419
    SDL_Surface *dest;
slouken@0
  1420
    SDL_PixelFormat *df;
slouken@0
  1421
    int maxsize = 0;
slouken@0
  1422
    int max_opaque_run;
slouken@0
  1423
    int max_transl_run = 65535;
slouken@0
  1424
    unsigned masksum;
slouken@0
  1425
    Uint8 *rlebuf, *dst;
slouken@1895
  1426
    int (*copy_opaque) (void *, Uint32 *, int,
slouken@1895
  1427
                        SDL_PixelFormat *, SDL_PixelFormat *);
slouken@1895
  1428
    int (*copy_transl) (void *, Uint32 *, int,
slouken@1895
  1429
                        SDL_PixelFormat *, SDL_PixelFormat *);
slouken@0
  1430
slouken@0
  1431
    dest = surface->map->dst;
slouken@1895
  1432
    if (!dest)
slouken@1895
  1433
        return -1;
slouken@0
  1434
    df = dest->format;
slouken@1895
  1435
    if (surface->format->BitsPerPixel != 32)
slouken@1895
  1436
        return -1;              /* only 32bpp source supported */
slouken@0
  1437
slouken@0
  1438
    /* find out whether the destination is one we support,
slouken@0
  1439
       and determine the max size of the encoded result */
slouken@0
  1440
    masksum = df->Rmask | df->Gmask | df->Bmask;
slouken@1895
  1441
    switch (df->BytesPerPixel) {
slouken@0
  1442
    case 2:
slouken@1895
  1443
        /* 16bpp: only support 565 and 555 formats */
slouken@1895
  1444
        switch (masksum) {
slouken@1895
  1445
        case 0xffff:
slouken@1895
  1446
            if (df->Gmask == 0x07e0
slouken@1895
  1447
                || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
slouken@1895
  1448
                copy_opaque = copy_opaque_16;
slouken@1895
  1449
                copy_transl = copy_transl_565;
slouken@1895
  1450
            } else
slouken@1895
  1451
                return -1;
slouken@1895
  1452
            break;
slouken@1895
  1453
        case 0x7fff:
slouken@1895
  1454
            if (df->Gmask == 0x03e0
slouken@1895
  1455
                || df->Rmask == 0x03e0 || df->Bmask == 0x03e0) {
slouken@1895
  1456
                copy_opaque = copy_opaque_16;
slouken@1895
  1457
                copy_transl = copy_transl_555;
slouken@1895
  1458
            } else
slouken@1895
  1459
                return -1;
slouken@1895
  1460
            break;
slouken@1895
  1461
        default:
slouken@1895
  1462
            return -1;
slouken@1895
  1463
        }
slouken@1895
  1464
        max_opaque_run = 255;   /* runs stored as bytes */
slouken@0
  1465
slouken@1895
  1466
        /* worst case is alternating opaque and translucent pixels,
slouken@1895
  1467
           with room for alignment padding between lines */
slouken@1895
  1468
        maxsize = surface->h * (2 + (4 + 2) * (surface->w + 1)) + 2;
slouken@1895
  1469
        break;
slouken@0
  1470
    case 4:
slouken@1895
  1471
        if (masksum != 0x00ffffff)
slouken@1895
  1472
            return -1;          /* requires unused high byte */
slouken@1895
  1473
        copy_opaque = copy_32;
slouken@1895
  1474
        copy_transl = copy_32;
slouken@1895
  1475
        max_opaque_run = 255;   /* runs stored as short ints */
slouken@0
  1476
slouken@1895
  1477
        /* worst case is alternating opaque and translucent pixels */
slouken@1895
  1478
        maxsize = surface->h * 2 * 4 * (surface->w + 1) + 4;
slouken@1895
  1479
        break;
slouken@0
  1480
    default:
slouken@1895
  1481
        return -1;              /* anything else unsupported right now */
slouken@0
  1482
    }
slouken@0
  1483
slouken@0
  1484
    maxsize += sizeof(RLEDestFormat);
slouken@1895
  1485
    rlebuf = (Uint8 *) SDL_malloc(maxsize);
slouken@1895
  1486
    if (!rlebuf) {
slouken@1895
  1487
        SDL_OutOfMemory();
slouken@1895
  1488
        return -1;
slouken@0
  1489
    }
slouken@0
  1490
    {
slouken@1895
  1491
        /* save the destination format so we can undo the encoding later */
slouken@1895
  1492
        RLEDestFormat *r = (RLEDestFormat *) rlebuf;
slouken@1895
  1493
        r->BytesPerPixel = df->BytesPerPixel;
slouken@1895
  1494
        r->Rloss = df->Rloss;
slouken@1895
  1495
        r->Gloss = df->Gloss;
slouken@1895
  1496
        r->Bloss = df->Bloss;
slouken@1895
  1497
        r->Rshift = df->Rshift;
slouken@1895
  1498
        r->Gshift = df->Gshift;
slouken@1895
  1499
        r->Bshift = df->Bshift;
slouken@1895
  1500
        r->Ashift = df->Ashift;
slouken@1895
  1501
        r->Rmask = df->Rmask;
slouken@1895
  1502
        r->Gmask = df->Gmask;
slouken@1895
  1503
        r->Bmask = df->Bmask;
slouken@1895
  1504
        r->Amask = df->Amask;
slouken@0
  1505
    }
slouken@0
  1506
    dst = rlebuf + sizeof(RLEDestFormat);
slouken@0
  1507
slouken@0
  1508
    /* Do the actual encoding */
slouken@0
  1509
    {
slouken@1895
  1510
        int x, y;
slouken@1895
  1511
        int h = surface->h, w = surface->w;
slouken@1895
  1512
        SDL_PixelFormat *sf = surface->format;
slouken@1895
  1513
        Uint32 *src = (Uint32 *) surface->pixels;
slouken@1895
  1514
        Uint8 *lastline = dst;  /* end of last non-blank line */
slouken@0
  1515
slouken@1895
  1516
        /* opaque counts are 8 or 16 bits, depending on target depth */
slouken@0
  1517
#define ADD_OPAQUE_COUNTS(n, m)			\
slouken@0
  1518
	if(df->BytesPerPixel == 4) {		\
slouken@0
  1519
	    ((Uint16 *)dst)[0] = n;		\
slouken@0
  1520
	    ((Uint16 *)dst)[1] = m;		\
slouken@0
  1521
	    dst += 4;				\
slouken@0
  1522
	} else {				\
slouken@0
  1523
	    dst[0] = n;				\
slouken@0
  1524
	    dst[1] = m;				\
slouken@0
  1525
	    dst += 2;				\
slouken@0
  1526
	}
slouken@0
  1527
slouken@1895
  1528
        /* translucent counts are always 16 bit */
slouken@0
  1529
#define ADD_TRANSL_COUNTS(n, m)		\
slouken@0
  1530
	(((Uint16 *)dst)[0] = n, ((Uint16 *)dst)[1] = m, dst += 4)
slouken@0
  1531
slouken@1895
  1532
        for (y = 0; y < h; y++) {
slouken@1895
  1533
            int runstart, skipstart;
slouken@1895
  1534
            int blankline = 0;
slouken@1895
  1535
            /* First encode all opaque pixels of a scan line */
slouken@1895
  1536
            x = 0;
slouken@1895
  1537
            do {
slouken@1895
  1538
                int run, skip, len;
slouken@1895
  1539
                skipstart = x;
slouken@1895
  1540
                while (x < w && !ISOPAQUE(src[x], sf))
slouken@1895
  1541
                    x++;
slouken@1895
  1542
                runstart = x;
slouken@1895
  1543
                while (x < w && ISOPAQUE(src[x], sf))
slouken@1895
  1544
                    x++;
slouken@1895
  1545
                skip = runstart - skipstart;
slouken@1895
  1546
                if (skip == w)
slouken@1895
  1547
                    blankline = 1;
slouken@1895
  1548
                run = x - runstart;
slouken@1895
  1549
                while (skip > max_opaque_run) {
slouken@1895
  1550
                    ADD_OPAQUE_COUNTS(max_opaque_run, 0);
slouken@1895
  1551
                    skip -= max_opaque_run;
slouken@1895
  1552
                }
slouken@1895
  1553
                len = MIN(run, max_opaque_run);
slouken@1895
  1554
                ADD_OPAQUE_COUNTS(skip, len);
slouken@1895
  1555
                dst += copy_opaque(dst, src + runstart, len, sf, df);
slouken@1895
  1556
                runstart += len;
slouken@1895
  1557
                run -= len;
slouken@1895
  1558
                while (run) {
slouken@1895
  1559
                    len = MIN(run, max_opaque_run);
slouken@1895
  1560
                    ADD_OPAQUE_COUNTS(0, len);
slouken@1895
  1561
                    dst += copy_opaque(dst, src + runstart, len, sf, df);
slouken@1895
  1562
                    runstart += len;
slouken@1895
  1563
                    run -= len;
slouken@1895
  1564
                }
dalton@2669
  1565
            } while (x < w);
slouken@0
  1566
slouken@1895
  1567
            /* Make sure the next output address is 32-bit aligned */
slouken@1895
  1568
            dst += (uintptr_t) dst & 2;
slouken@0
  1569
slouken@1895
  1570
            /* Next, encode all translucent pixels of the same scan line */
slouken@1895
  1571
            x = 0;
slouken@1895
  1572
            do {
slouken@1895
  1573
                int run, skip, len;
slouken@1895
  1574
                skipstart = x;
slouken@1895
  1575
                while (x < w && !ISTRANSL(src[x], sf))
slouken@1895
  1576
                    x++;
slouken@1895
  1577
                runstart = x;
slouken@1895
  1578
                while (x < w && ISTRANSL(src[x], sf))
slouken@1895
  1579
                    x++;
slouken@1895
  1580
                skip = runstart - skipstart;
slouken@1895
  1581
                blankline &= (skip == w);
slouken@1895
  1582
                run = x - runstart;
slouken@1895
  1583
                while (skip > max_transl_run) {
slouken@1895
  1584
                    ADD_TRANSL_COUNTS(max_transl_run, 0);
slouken@1895
  1585
                    skip -= max_transl_run;
slouken@1895
  1586
                }
slouken@1895
  1587
                len = MIN(run, max_transl_run);
slouken@1895
  1588
                ADD_TRANSL_COUNTS(skip, len);
slouken@1895
  1589
                dst += copy_transl(dst, src + runstart, len, sf, df);
slouken@1895
  1590
                runstart += len;
slouken@1895
  1591
                run -= len;
slouken@1895
  1592
                while (run) {
slouken@1895
  1593
                    len = MIN(run, max_transl_run);
slouken@1895
  1594
                    ADD_TRANSL_COUNTS(0, len);
slouken@1895
  1595
                    dst += copy_transl(dst, src + runstart, len, sf, df);
slouken@1895
  1596
                    runstart += len;
slouken@1895
  1597
                    run -= len;
slouken@1895
  1598
                }
slouken@1895
  1599
                if (!blankline)
slouken@1895
  1600
                    lastline = dst;
dalton@2669
  1601
            } while (x < w);
slouken@0
  1602
slouken@1895
  1603
            src += surface->pitch >> 2;
slouken@1895
  1604
        }
slouken@1895
  1605
        dst = lastline;         /* back up past trailing blank lines */
slouken@1895
  1606
        ADD_OPAQUE_COUNTS(0, 0);
slouken@0
  1607
    }
slouken@0
  1608
slouken@0
  1609
#undef ADD_OPAQUE_COUNTS
slouken@0
  1610
#undef ADD_TRANSL_COUNTS
slouken@0
  1611
slouken@0
  1612
    /* Now that we have it encoded, release the original pixels */
slouken@2222
  1613
    if (!(surface->flags & SDL_PREALLOC)) {
slouken@1895
  1614
        SDL_free(surface->pixels);
slouken@1895
  1615
        surface->pixels = NULL;
slouken@0
  1616
    }
slouken@0
  1617
slouken@0
  1618
    /* realloc the buffer to release unused memory */
slouken@0
  1619
    {
slouken@1895
  1620
        Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf);
slouken@1895
  1621
        if (!p)
slouken@1895
  1622
            p = rlebuf;
slouken@2257
  1623
        surface->map->data = p;
slouken@0
  1624
    }
slouken@0
  1625
slouken@0
  1626
    return 0;
slouken@0
  1627
}
slouken@0
  1628
slouken@1895
  1629
static Uint32
slouken@1895
  1630
getpix_8(Uint8 * srcbuf)
slouken@0
  1631
{
slouken@0
  1632
    return *srcbuf;
slouken@0
  1633
}
slouken@0
  1634
slouken@1895
  1635
static Uint32
slouken@1895
  1636
getpix_16(Uint8 * srcbuf)
slouken@0
  1637
{
slouken@1895
  1638
    return *(Uint16 *) srcbuf;
slouken@0
  1639
}
slouken@0
  1640
slouken@1895
  1641
static Uint32
slouken@1895
  1642
getpix_24(Uint8 * srcbuf)
slouken@0
  1643
{
icculus@1155
  1644
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
icculus@1155
  1645
    return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16);
icculus@1155
  1646
#else
icculus@1155
  1647
    return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];
icculus@1155
  1648
#endif
slouken@0
  1649
}
slouken@0
  1650
slouken@1895
  1651
static Uint32
slouken@1895
  1652
getpix_32(Uint8 * srcbuf)
slouken@0
  1653
{
slouken@1895
  1654
    return *(Uint32 *) srcbuf;
slouken@0
  1655
}
slouken@0
  1656
slouken@1895
  1657
typedef Uint32(*getpix_func) (Uint8 *);
slouken@0
  1658
slouken@0
  1659
static getpix_func getpixes[4] = {
slouken@0
  1660
    getpix_8, getpix_16, getpix_24, getpix_32
slouken@0
  1661
};
slouken@0
  1662
slouken@1895
  1663
static int
slouken@1895
  1664
RLEColorkeySurface(SDL_Surface * surface)
slouken@0
  1665
{
slouken@1895
  1666
    Uint8 *rlebuf, *dst;
slouken@1895
  1667
    int maxn;
slouken@1895
  1668
    int y;
slouken@1895
  1669
    Uint8 *srcbuf, *curbuf, *lastline;
slouken@1895
  1670
    int maxsize = 0;
slouken@1895
  1671
    int skip, run;
slouken@1895
  1672
    int bpp = surface->format->BytesPerPixel;
slouken@1895
  1673
    getpix_func getpix;
slouken@1895
  1674
    Uint32 ckey, rgbmask;
slouken@1895
  1675
    int w, h;
slouken@0
  1676
slouken@1895
  1677
    /* calculate the worst case size for the compressed surface */
slouken@1895
  1678
    switch (bpp) {
slouken@1895
  1679
    case 1:
slouken@1895
  1680
        /* worst case is alternating opaque and transparent pixels,
slouken@1895
  1681
           starting with an opaque pixel */
slouken@1895
  1682
        maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2;
slouken@1895
  1683
        break;
slouken@1895
  1684
    case 2:
slouken@1895
  1685
    case 3:
slouken@1895
  1686
        /* worst case is solid runs, at most 255 pixels wide */
slouken@1895
  1687
        maxsize = surface->h * (2 * (surface->w / 255 + 1)
slouken@1895
  1688
                                + surface->w * bpp) + 2;
slouken@1895
  1689
        break;
slouken@1895
  1690
    case 4:
slouken@1895
  1691
        /* worst case is solid runs, at most 65535 pixels wide */
slouken@1895
  1692
        maxsize = surface->h * (4 * (surface->w / 65535 + 1)
slouken@1895
  1693
                                + surface->w * 4) + 4;
slouken@1895
  1694
        break;
slouken@1895
  1695
    }
slouken@0
  1696
slouken@1895
  1697
    rlebuf = (Uint8 *) SDL_malloc(maxsize);
slouken@1895
  1698
    if (rlebuf == NULL) {
slouken@1895
  1699
        SDL_OutOfMemory();
slouken@1895
  1700
        return (-1);
slouken@1895
  1701
    }
slouken@0
  1702
slouken@1895
  1703
    /* Set up the conversion */
slouken@1895
  1704
    srcbuf = (Uint8 *) surface->pixels;
slouken@1895
  1705
    curbuf = srcbuf;
slouken@1895
  1706
    maxn = bpp == 4 ? 65535 : 255;
slouken@1895
  1707
    skip = run = 0;
slouken@1895
  1708
    dst = rlebuf;
slouken@1895
  1709
    rgbmask = ~surface->format->Amask;
slouken@2262
  1710
    ckey = surface->map->info.colorkey & rgbmask;
slouken@1895
  1711
    lastline = dst;
slouken@1895
  1712
    getpix = getpixes[bpp - 1];
slouken@1895
  1713
    w = surface->w;
slouken@1895
  1714
    h = surface->h;
slouken@0
  1715
slouken@0
  1716
#define ADD_COUNTS(n, m)			\
slouken@0
  1717
	if(bpp == 4) {				\
slouken@0
  1718
	    ((Uint16 *)dst)[0] = n;		\
slouken@0
  1719
	    ((Uint16 *)dst)[1] = m;		\
slouken@0
  1720
	    dst += 4;				\
slouken@0
  1721
	} else {				\
slouken@0
  1722
	    dst[0] = n;				\
slouken@0
  1723
	    dst[1] = m;				\
slouken@0
  1724
	    dst += 2;				\
slouken@0
  1725
	}
slouken@0
  1726
slouken@1895
  1727
    for (y = 0; y < h; y++) {
slouken@1895
  1728
        int x = 0;
slouken@1895
  1729
        int blankline = 0;
slouken@1895
  1730
        do {
slouken@1895
  1731
            int run, skip, len;
slouken@1895
  1732
            int runstart;
slouken@1895
  1733
            int skipstart = x;
slouken@0
  1734
slouken@1895
  1735
            /* find run of transparent, then opaque pixels */
slouken@1895
  1736
            while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey)
slouken@1895
  1737
                x++;
slouken@1895
  1738
            runstart = x;
slouken@1895
  1739
            while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey)
slouken@1895
  1740
                x++;
slouken@1895
  1741
            skip = runstart - skipstart;
slouken@1895
  1742
            if (skip == w)
slouken@1895
  1743
                blankline = 1;
slouken@1895
  1744
            run = x - runstart;
slouken@0
  1745
slouken@1895
  1746
            /* encode segment */
slouken@1895
  1747
            while (skip > maxn) {
slouken@1895
  1748
                ADD_COUNTS(maxn, 0);
slouken@1895
  1749
                skip -= maxn;
slouken@1895
  1750
            }
slouken@1895
  1751
            len = MIN(run, maxn);
slouken@1895
  1752
            ADD_COUNTS(skip, len);
slouken@1895
  1753
            SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@1895
  1754
            dst += len * bpp;
slouken@1895
  1755
            run -= len;
slouken@1895
  1756
            runstart += len;
slouken@1895
  1757
            while (run) {
slouken@1895
  1758
                len = MIN(run, maxn);
slouken@1895
  1759
                ADD_COUNTS(0, len);
slouken@1895
  1760
                SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@1895
  1761
                dst += len * bpp;
slouken@1895
  1762
                runstart += len;
slouken@1895
  1763
                run -= len;
slouken@1895
  1764
            }
slouken@1895
  1765
            if (!blankline)
slouken@1895
  1766
                lastline = dst;
dalton@2669
  1767
        } while (x < w);
slouken@0
  1768
slouken@1895
  1769
        srcbuf += surface->pitch;
slouken@1895
  1770
    }
slouken@1895
  1771
    dst = lastline;             /* back up bast trailing blank lines */
slouken@1895
  1772
    ADD_COUNTS(0, 0);
slouken@0
  1773
slouken@0
  1774
#undef ADD_COUNTS
slouken@0
  1775
slouken@1895
  1776
    /* Now that we have it encoded, release the original pixels */
slouken@2222
  1777
    if (!(surface->flags & SDL_PREALLOC)) {
slouken@1895
  1778
        SDL_free(surface->pixels);
slouken@1895
  1779
        surface->pixels = NULL;
slouken@1895
  1780
    }
slouken@0
  1781
slouken@1895
  1782
    /* realloc the buffer to release unused memory */
slouken@1895
  1783
    {
slouken@1895
  1784
        /* If realloc returns NULL, the original block is left intact */
slouken@1895
  1785
        Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf);
slouken@1895
  1786
        if (!p)
slouken@1895
  1787
            p = rlebuf;
slouken@2257
  1788
        surface->map->data = p;
slouken@1895
  1789
    }
slouken@0
  1790
slouken@1895
  1791
    return (0);
slouken@0
  1792
}
slouken@0
  1793
slouken@1895
  1794
int
slouken@1895
  1795
SDL_RLESurface(SDL_Surface * surface)
slouken@0
  1796
{
slouken@2266
  1797
    int flags;
slouken@0
  1798
slouken@1895
  1799
    /* Clear any previous RLE conversion */
slouken@1895
  1800
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
  1801
        SDL_UnRLESurface(surface, 1);
slouken@1895
  1802
    }
slouken@0
  1803
slouken@1895
  1804
    /* We don't support RLE encoding of bitmaps */
slouken@1895
  1805
    if (surface->format->BitsPerPixel < 8) {
slouken@2266
  1806
        return -1;
slouken@1895
  1807
    }
slouken@0
  1808
slouken@2266
  1809
    /* Make sure the pixels are available */
slouken@2266
  1810
    if (!surface->pixels) {
slouken@2266
  1811
        return -1;
slouken@1895
  1812
    }
slouken@0
  1813
slouken@2266
  1814
    /* If we don't have colorkey or blending, nothing to do... */
slouken@2266
  1815
    flags = surface->map->info.flags;
slouken@2267
  1816
    if (!(flags & (SDL_COPY_COLORKEY | SDL_COPY_BLEND))) {
slouken@2266
  1817
        return -1;
slouken@1895
  1818
    }
slouken@0
  1819
slouken@2266
  1820
    /* Pass on combinations not supported */
slouken@2266
  1821
    if ((flags & SDL_COPY_MODULATE_COLOR) ||
slouken@2267
  1822
        (flags & (SDL_COPY_ADD | SDL_COPY_MOD)) ||
slouken@2266
  1823
        (flags & SDL_COPY_NEAREST)) {
slouken@2266
  1824
        return -1;
slouken@1895
  1825
    }
slouken@0
  1826
slouken@2266
  1827
    /* Encode and set up the blit */
slouken@2266
  1828
    if (!surface->format->Amask || !(flags & SDL_COPY_BLEND)) {
slouken@2266
  1829
        if (!surface->map->identity) {
slouken@2266
  1830
            return -1;
slouken@2266
  1831
        }
slouken@2266
  1832
        if (RLEColorkeySurface(surface) < 0) {
slouken@2266
  1833
            return -1;
slouken@2266
  1834
        }
slouken@2266
  1835
        surface->map->blit = SDL_RLEBlit;
slouken@2266
  1836
        surface->map->info.flags |= SDL_COPY_RLE_COLORKEY;
slouken@2266
  1837
    } else {
slouken@2266
  1838
        if (RLEAlphaSurface(surface) < 0) {
slouken@2266
  1839
            return -1;
slouken@2266
  1840
        }
slouken@2266
  1841
        surface->map->blit = SDL_RLEAlphaBlit;
slouken@2266
  1842
        surface->map->info.flags |= SDL_COPY_RLE_ALPHAKEY;
slouken@2266
  1843
    }
slouken@0
  1844
slouken@1895
  1845
    /* The surface is now accelerated */
slouken@1895
  1846
    surface->flags |= SDL_RLEACCEL;
slouken@0
  1847
slouken@1895
  1848
    return (0);
slouken@0
  1849
}
slouken@0
  1850
slouken@0
  1851
/*
slouken@0
  1852
 * Un-RLE a surface with pixel alpha
slouken@0
  1853
 * This may not give back exactly the image before RLE-encoding; all
slouken@0
  1854
 * completely transparent pixels will be lost, and colour and alpha depth
slouken@0
  1855
 * may have been reduced (when encoding for 16bpp targets).
slouken@0
  1856
 */
slouken@1895
  1857
static SDL_bool
slouken@1895
  1858
UnRLEAlpha(SDL_Surface * surface)
slouken@0
  1859
{
slouken@0
  1860
    Uint8 *srcbuf;
slouken@0
  1861
    Uint32 *dst;
slouken@0
  1862
    SDL_PixelFormat *sf = surface->format;
slouken@2257
  1863
    RLEDestFormat *df = surface->map->data;
slouken@1895
  1864
    int (*uncopy_opaque) (Uint32 *, void *, int,
slouken@1895
  1865
                          RLEDestFormat *, SDL_PixelFormat *);
slouken@1895
  1866
    int (*uncopy_transl) (Uint32 *, void *, int,
slouken@1895
  1867
                          RLEDestFormat *, SDL_PixelFormat *);
slouken@0
  1868
    int w = surface->w;
slouken@0
  1869
    int bpp = df->BytesPerPixel;
slouken@0
  1870
slouken@1895
  1871
    if (bpp == 2) {
slouken@1895
  1872
        uncopy_opaque = uncopy_opaque_16;
slouken@1895
  1873
        uncopy_transl = uncopy_transl_16;
slouken@0
  1874
    } else {
slouken@1895
  1875
        uncopy_opaque = uncopy_transl = uncopy_32;
slouken@0
  1876
    }
slouken@0
  1877
slouken@1336
  1878
    surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
  1879
    if (!surface->pixels) {
slouken@1895
  1880
        return (SDL_FALSE);
slouken@944
  1881
    }
slouken@0
  1882
    /* fill background with transparent pixels */
slouken@1336
  1883
    SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@0
  1884
slouken@0
  1885
    dst = surface->pixels;
slouken@1895
  1886
    srcbuf = (Uint8 *) (df + 1);
slouken@1895
  1887
    for (;;) {
slouken@1895
  1888
        /* copy opaque pixels */
slouken@1895
  1889
        int ofs = 0;
slouken@1895
  1890
        do {
slouken@1895
  1891
            unsigned run;
slouken@1895
  1892
            if (bpp == 2) {
slouken@1895
  1893
                ofs += srcbuf[0];
slouken@1895
  1894
                run = srcbuf[1];
slouken@1895
  1895
                srcbuf += 2;
slouken@1895
  1896
            } else {
slouken@1895
  1897
                ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1898
                run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1899
                srcbuf += 4;
slouken@1895
  1900
            }
slouken@1895
  1901
            if (run) {
slouken@1895
  1902
                srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf);
slouken@1895
  1903
                ofs += run;
slouken@1895
  1904
            } else if (!ofs)
slouken@1895
  1905
                return (SDL_TRUE);
dalton@2669
  1906
        } while (ofs < w);
slouken@0
  1907
slouken@1895
  1908
        /* skip padding if needed */
slouken@1895
  1909
        if (bpp == 2)
slouken@1895
  1910
            srcbuf += (uintptr_t) srcbuf & 2;
slouken@1895
  1911
slouken@1895
  1912
        /* copy translucent pixels */
slouken@1895
  1913
        ofs = 0;
slouken@1895
  1914
        do {
slouken@1895
  1915
            unsigned run;
slouken@1895
  1916
            ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1917
            run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1918
            srcbuf += 4;
slouken@1895
  1919
            if (run) {
slouken@1895
  1920
                srcbuf += uncopy_transl(dst + ofs, srcbuf, run, df, sf);
slouken@1895
  1921
                ofs += run;
slouken@1895
  1922
            }
dalton@2669
  1923
        } while (ofs < w);
slouken@1895
  1924
        dst += surface->pitch >> 2;
slouken@0
  1925
    }
slouken@944
  1926
    /* Make the compiler happy */
slouken@1895
  1927
    return (SDL_TRUE);
slouken@0
  1928
}
slouken@0
  1929
slouken@1895
  1930
void
slouken@1895
  1931
SDL_UnRLESurface(SDL_Surface * surface, int recode)
slouken@0
  1932
{
slouken@2266
  1933
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
  1934
        surface->flags &= ~SDL_RLEACCEL;
slouken@0
  1935
slouken@2222
  1936
        if (recode && !(surface->flags & SDL_PREALLOC)) {
slouken@2266
  1937
            if (surface->map->info.flags & SDL_COPY_RLE_COLORKEY) {
slouken@1895
  1938
                SDL_Rect full;
slouken@0
  1939
slouken@1895
  1940
                /* re-create the original surface */
slouken@1895
  1941
                surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
  1942
                if (!surface->pixels) {
slouken@1895
  1943
                    /* Oh crap... */
slouken@1895
  1944
                    surface->flags |= SDL_RLEACCEL;
slouken@1895
  1945
                    return;
slouken@1895
  1946
                }
slouken@0
  1947
slouken@1895
  1948
                /* fill it with the background colour */
slouken@2262
  1949
                SDL_FillRect(surface, NULL, surface->map->info.colorkey);
slouken@0
  1950
slouken@1895
  1951
                /* now render the encoded surface */
slouken@1895
  1952
                full.x = full.y = 0;
slouken@1895
  1953
                full.w = surface->w;
slouken@1895
  1954
                full.h = surface->h;
slouken@1895
  1955
                SDL_RLEBlit(surface, &full, surface, &full);
slouken@1895
  1956
            } else {
slouken@1895
  1957
                if (!UnRLEAlpha(surface)) {
slouken@1895
  1958
                    /* Oh crap... */
slouken@1895
  1959
                    surface->flags |= SDL_RLEACCEL;
slouken@1895
  1960
                    return;
slouken@1895
  1961
                }
slouken@1895
  1962
            }
slouken@1895
  1963
        }
slouken@2267
  1964
        surface->map->info.flags &=
slouken@2267
  1965
            (SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY);
bob@2328
  1966
        surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
slouken@0
  1967
slouken@2266
  1968
        if (surface->map->data) {
slouken@2257
  1969
            SDL_free(surface->map->data);
slouken@2257
  1970
            surface->map->data = NULL;
slouken@1895
  1971
        }
slouken@0
  1972
    }
slouken@0
  1973
}
slouken@0
  1974
slouken@1895
  1975
/* vi: set ts=4 sw=4 expandtab: */