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