src/video/SDL_RLEaccel.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 6227 e11fd9609d10
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

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