comparison src/video/SDL_RLEaccel.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents 84de7511f79f
children 926294b2bb4e
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
543 } \ 543 } \
544 emms(); \ 544 emms(); \
545 } while(0) 545 } while(0)
546 546
547 #endif 547 #endif
548 548
549 /* 549 /*
550 * Special case: 50% alpha (alpha=128) 550 * Special case: 50% alpha (alpha=128)
551 * This is treated specially because it can be optimized very well, and 551 * This is treated specially because it can be optimized very well, and
552 * since it is good for many cases of semi-translucency. 552 * since it is good for many cases of semi-translucency.
553 * The theory is to do all three components at the same time: 553 * The theory is to do all three components at the same time:
702 } \ 702 } \
703 } \ 703 } \
704 } while(0) 704 } while(0)
705 705
706 #else 706 #else
707 707
708 #define CHOOSE_BLIT(blitter, alpha, fmt) \ 708 #define CHOOSE_BLIT(blitter, alpha, fmt) \
709 do { \ 709 do { \
710 if(alpha == 255) { \ 710 if(alpha == 255) { \
711 switch(fmt->BytesPerPixel) { \ 711 switch(fmt->BytesPerPixel) { \
712 case 1: blitter(1, Uint8, OPAQUE_BLIT); break; \ 712 case 1: blitter(1, Uint8, OPAQUE_BLIT); break; \
777 777
778 /* 778 /*
779 * This takes care of the case when the surface is clipped on the left and/or 779 * This takes care of the case when the surface is clipped on the left and/or
780 * right. Top clipping has already been taken care of. 780 * right. Top clipping has already been taken care of.
781 */ 781 */
782 static void RLEClipBlit(int w, Uint8 *srcbuf, SDL_Surface *dst, 782 static void
783 Uint8 *dstbuf, SDL_Rect *srcrect, unsigned alpha) 783 RLEClipBlit(int w, Uint8 * srcbuf, SDL_Surface * dst,
784 Uint8 * dstbuf, SDL_Rect * srcrect, unsigned alpha)
784 { 785 {
785 SDL_PixelFormat *fmt = dst->format; 786 SDL_PixelFormat *fmt = dst->format;
786 787
787 #define RLECLIPBLIT(bpp, Type, do_blit) \ 788 #define RLECLIPBLIT(bpp, Type, do_blit) \
788 do { \ 789 do { \
834 835
835 } 836 }
836 837
837 838
838 /* blit a colorkeyed RLE surface */ 839 /* blit a colorkeyed RLE surface */
839 int SDL_RLEBlit(SDL_Surface *src, SDL_Rect *srcrect, 840 int
840 SDL_Surface *dst, SDL_Rect *dstrect) 841 SDL_RLEBlit(SDL_Surface * src, SDL_Rect * srcrect,
841 { 842 SDL_Surface * dst, SDL_Rect * dstrect)
842 Uint8 *dstbuf; 843 {
843 Uint8 *srcbuf; 844 Uint8 *dstbuf;
844 int x, y; 845 Uint8 *srcbuf;
845 int w = src->w; 846 int x, y;
846 unsigned alpha; 847 int w = src->w;
847 848 unsigned alpha;
848 /* Lock the destination if necessary */ 849
849 if ( SDL_MUSTLOCK(dst) ) { 850 /* Lock the destination if necessary */
850 if ( SDL_LockSurface(dst) < 0 ) { 851 if (SDL_MUSTLOCK(dst)) {
851 return(-1); 852 if (SDL_LockSurface(dst) < 0) {
852 } 853 return (-1);
853 } 854 }
854 855 }
855 /* Set up the source and destination pointers */ 856
856 x = dstrect->x; 857 /* Set up the source and destination pointers */
857 y = dstrect->y; 858 x = dstrect->x;
858 dstbuf = (Uint8 *)dst->pixels 859 y = dstrect->y;
859 + y * dst->pitch + x * src->format->BytesPerPixel; 860 dstbuf = (Uint8 *) dst->pixels
860 srcbuf = (Uint8 *)src->map->sw_data->aux_data; 861 + y * dst->pitch + x * src->format->BytesPerPixel;
861 862 srcbuf = (Uint8 *) src->map->sw_data->aux_data;
862 { 863
863 /* skip lines at the top if neccessary */ 864 {
864 int vskip = srcrect->y; 865 /* skip lines at the top if neccessary */
865 int ofs = 0; 866 int vskip = srcrect->y;
866 if(vskip) { 867 int ofs = 0;
868 if (vskip) {
867 869
868 #define RLESKIP(bpp, Type) \ 870 #define RLESKIP(bpp, Type) \
869 for(;;) { \ 871 for(;;) { \
870 int run; \ 872 int run; \
871 ofs += *(Type *)srcbuf; \ 873 ofs += *(Type *)srcbuf; \
881 if(!--vskip) \ 883 if(!--vskip) \
882 break; \ 884 break; \
883 } \ 885 } \
884 } 886 }
885 887
886 switch(src->format->BytesPerPixel) { 888 switch (src->format->BytesPerPixel) {
887 case 1: RLESKIP(1, Uint8); break; 889 case 1:
888 case 2: RLESKIP(2, Uint8); break; 890 RLESKIP(1, Uint8);
889 case 3: RLESKIP(3, Uint8); break; 891 break;
890 case 4: RLESKIP(4, Uint16); break; 892 case 2:
891 } 893 RLESKIP(2, Uint8);
894 break;
895 case 3:
896 RLESKIP(3, Uint8);
897 break;
898 case 4:
899 RLESKIP(4, Uint16);
900 break;
901 }
892 902
893 #undef RLESKIP 903 #undef RLESKIP
894 904
895 } 905 }
896 } 906 }
897 907
898 alpha = (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA 908 alpha = (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA
899 ? src->format->alpha : 255; 909 ? src->format->alpha : 255;
900 /* if left or right edge clipping needed, call clip blit */ 910 /* if left or right edge clipping needed, call clip blit */
901 if ( srcrect->x || srcrect->w != src->w ) { 911 if (srcrect->x || srcrect->w != src->w) {
902 RLEClipBlit(w, srcbuf, dst, dstbuf, srcrect, alpha); 912 RLEClipBlit(w, srcbuf, dst, dstbuf, srcrect, alpha);
903 } else { 913 } else {
904 SDL_PixelFormat *fmt = src->format; 914 SDL_PixelFormat *fmt = src->format;
905 915
906 #define RLEBLIT(bpp, Type, do_blit) \ 916 #define RLEBLIT(bpp, Type, do_blit) \
907 do { \ 917 do { \
908 int linecount = srcrect->h; \ 918 int linecount = srcrect->h; \
909 int ofs = 0; \ 919 int ofs = 0; \
925 break; \ 935 break; \
926 } \ 936 } \
927 } \ 937 } \
928 } while(0) 938 } while(0)
929 939
930 CHOOSE_BLIT(RLEBLIT, alpha, fmt); 940 CHOOSE_BLIT(RLEBLIT, alpha, fmt);
931 941
932 #undef RLEBLIT 942 #undef RLEBLIT
933 } 943 }
934 944
935 done: 945 done:
936 /* Unlock the destination if necessary */ 946 /* Unlock the destination if necessary */
937 if ( SDL_MUSTLOCK(dst) ) { 947 if (SDL_MUSTLOCK(dst)) {
938 SDL_UnlockSurface(dst); 948 SDL_UnlockSurface(dst);
939 } 949 }
940 return(0); 950 return (0);
941 } 951 }
942 952
943 #undef OPAQUE_BLIT 953 #undef OPAQUE_BLIT
944 954
945 /* 955 /*
993 dst = (Uint16)(d | d >> 16); \ 1003 dst = (Uint16)(d | d >> 16); \
994 } while(0) 1004 } while(0)
995 1005
996 /* used to save the destination format in the encoding. Designed to be 1006 /* used to save the destination format in the encoding. Designed to be
997 macro-compatible with SDL_PixelFormat but without the unneeded fields */ 1007 macro-compatible with SDL_PixelFormat but without the unneeded fields */
998 typedef struct { 1008 typedef struct
999 Uint8 BytesPerPixel; 1009 {
1000 Uint8 Rloss; 1010 Uint8 BytesPerPixel;
1001 Uint8 Gloss; 1011 Uint8 Rloss;
1002 Uint8 Bloss; 1012 Uint8 Gloss;
1003 Uint8 Rshift; 1013 Uint8 Bloss;
1004 Uint8 Gshift; 1014 Uint8 Rshift;
1005 Uint8 Bshift; 1015 Uint8 Gshift;
1006 Uint8 Ashift; 1016 Uint8 Bshift;
1007 Uint32 Rmask; 1017 Uint8 Ashift;
1008 Uint32 Gmask; 1018 Uint32 Rmask;
1009 Uint32 Bmask; 1019 Uint32 Gmask;
1010 Uint32 Amask; 1020 Uint32 Bmask;
1021 Uint32 Amask;
1011 } RLEDestFormat; 1022 } RLEDestFormat;
1012 1023
1013 /* blit a pixel-alpha RLE surface clipped at the right and/or left edges */ 1024 /* blit a pixel-alpha RLE surface clipped at the right and/or left edges */
1014 static void RLEAlphaClipBlit(int w, Uint8 *srcbuf, SDL_Surface *dst, 1025 static void
1015 Uint8 *dstbuf, SDL_Rect *srcrect) 1026 RLEAlphaClipBlit(int w, Uint8 * srcbuf, SDL_Surface * dst,
1027 Uint8 * dstbuf, SDL_Rect * srcrect)
1016 { 1028 {
1017 SDL_PixelFormat *df = dst->format; 1029 SDL_PixelFormat *df = dst->format;
1018 /* 1030 /*
1019 * clipped blitter: Ptype is the destination pixel type, 1031 * clipped blitter: Ptype is the destination pixel type,
1020 * Ctype the translucent count type, and do_blend the macro 1032 * Ctype the translucent count type, and do_blend the macro
1086 } while(ofs < w); \ 1098 } while(ofs < w); \
1087 dstbuf += dst->pitch; \ 1099 dstbuf += dst->pitch; \
1088 } while(--linecount); \ 1100 } while(--linecount); \
1089 } while(0) 1101 } while(0)
1090 1102
1091 switch(df->BytesPerPixel) { 1103 switch (df->BytesPerPixel) {
1092 case 2: 1104 case 2:
1093 if(df->Gmask == 0x07e0 || df->Rmask == 0x07e0 1105 if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0)
1094 || df->Bmask == 0x07e0) 1106 RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565);
1095 RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565); 1107 else
1096 else 1108 RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555);
1097 RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555); 1109 break;
1098 break;
1099 case 4: 1110 case 4:
1100 RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888); 1111 RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888);
1101 break; 1112 break;
1102 } 1113 }
1103 } 1114 }
1104 1115
1105 /* blit a pixel-alpha RLE surface */ 1116 /* blit a pixel-alpha RLE surface */
1106 int SDL_RLEAlphaBlit(SDL_Surface *src, SDL_Rect *srcrect, 1117 int
1107 SDL_Surface *dst, SDL_Rect *dstrect) 1118 SDL_RLEAlphaBlit(SDL_Surface * src, SDL_Rect * srcrect,
1119 SDL_Surface * dst, SDL_Rect * dstrect)
1108 { 1120 {
1109 int x, y; 1121 int x, y;
1110 int w = src->w; 1122 int w = src->w;
1111 Uint8 *srcbuf, *dstbuf; 1123 Uint8 *srcbuf, *dstbuf;
1112 SDL_PixelFormat *df = dst->format; 1124 SDL_PixelFormat *df = dst->format;
1113 1125
1114 /* Lock the destination if necessary */ 1126 /* Lock the destination if necessary */
1115 if ( SDL_MUSTLOCK(dst) ) { 1127 if (SDL_MUSTLOCK(dst)) {
1116 if ( SDL_LockSurface(dst) < 0 ) { 1128 if (SDL_LockSurface(dst) < 0) {
1117 return -1; 1129 return -1;
1118 } 1130 }
1119 } 1131 }
1120 1132
1121 x = dstrect->x; 1133 x = dstrect->x;
1122 y = dstrect->y; 1134 y = dstrect->y;
1123 dstbuf = (Uint8 *)dst->pixels 1135 dstbuf = (Uint8 *) dst->pixels + y * dst->pitch + x * df->BytesPerPixel;
1124 + y * dst->pitch + x * df->BytesPerPixel; 1136 srcbuf = (Uint8 *) src->map->sw_data->aux_data + sizeof(RLEDestFormat);
1125 srcbuf = (Uint8 *)src->map->sw_data->aux_data + sizeof(RLEDestFormat);
1126 1137
1127 { 1138 {
1128 /* skip lines at the top if necessary */ 1139 /* skip lines at the top if necessary */
1129 int vskip = srcrect->y; 1140 int vskip = srcrect->y;
1130 if(vskip) { 1141 if (vskip) {
1131 int ofs; 1142 int ofs;
1132 if(df->BytesPerPixel == 2) { 1143 if (df->BytesPerPixel == 2) {
1133 /* the 16/32 interleaved format */ 1144 /* the 16/32 interleaved format */
1134 do { 1145 do {
1135 /* skip opaque line */ 1146 /* skip opaque line */
1136 ofs = 0; 1147 ofs = 0;
1137 do { 1148 do {
1138 int run; 1149 int run;
1139 ofs += srcbuf[0]; 1150 ofs += srcbuf[0];
1140 run = srcbuf[1]; 1151 run = srcbuf[1];
1141 srcbuf += 2; 1152 srcbuf += 2;
1142 if(run) { 1153 if (run) {
1143 srcbuf += 2 * run; 1154 srcbuf += 2 * run;
1144 ofs += run; 1155 ofs += run;
1145 } else if(!ofs) 1156 } else if (!ofs)
1146 goto done; 1157 goto done;
1147 } while(ofs < w); 1158 }
1148 1159 while (ofs < w);
1149 /* skip padding */ 1160
1150 srcbuf += (uintptr_t)srcbuf & 2; 1161 /* skip padding */
1151 1162 srcbuf += (uintptr_t) srcbuf & 2;
1152 /* skip translucent line */ 1163
1153 ofs = 0; 1164 /* skip translucent line */
1154 do { 1165 ofs = 0;
1155 int run; 1166 do {
1156 ofs += ((Uint16 *)srcbuf)[0]; 1167 int run;
1157 run = ((Uint16 *)srcbuf)[1]; 1168 ofs += ((Uint16 *) srcbuf)[0];
1158 srcbuf += 4 * (run + 1); 1169 run = ((Uint16 *) srcbuf)[1];
1159 ofs += run; 1170 srcbuf += 4 * (run + 1);
1160 } while(ofs < w); 1171 ofs += run;
1161 } while(--vskip); 1172 }
1162 } else { 1173 while (ofs < w);
1163 /* the 32/32 interleaved format */ 1174 }
1164 vskip <<= 1; /* opaque and translucent have same format */ 1175 while (--vskip);
1165 do { 1176 } else {
1166 ofs = 0; 1177 /* the 32/32 interleaved format */
1167 do { 1178 vskip <<= 1; /* opaque and translucent have same format */
1168 int run; 1179 do {
1169 ofs += ((Uint16 *)srcbuf)[0]; 1180 ofs = 0;
1170 run = ((Uint16 *)srcbuf)[1]; 1181 do {
1171 srcbuf += 4; 1182 int run;
1172 if(run) { 1183 ofs += ((Uint16 *) srcbuf)[0];
1173 srcbuf += 4 * run; 1184 run = ((Uint16 *) srcbuf)[1];
1174 ofs += run; 1185 srcbuf += 4;
1175 } else if(!ofs) 1186 if (run) {
1176 goto done; 1187 srcbuf += 4 * run;
1177 } while(ofs < w); 1188 ofs += run;
1178 } while(--vskip); 1189 } else if (!ofs)
1179 } 1190 goto done;
1180 } 1191 }
1192 while (ofs < w);
1193 }
1194 while (--vskip);
1195 }
1196 }
1181 } 1197 }
1182 1198
1183 /* if left or right edge clipping needed, call clip blit */ 1199 /* if left or right edge clipping needed, call clip blit */
1184 if(srcrect->x || srcrect->w != src->w) { 1200 if (srcrect->x || srcrect->w != src->w) {
1185 RLEAlphaClipBlit(w, srcbuf, dst, dstbuf, srcrect); 1201 RLEAlphaClipBlit(w, srcbuf, dst, dstbuf, srcrect);
1186 } else { 1202 } else {
1187 1203
1188 /* 1204 /*
1189 * non-clipped blitter. Ptype is the destination pixel type, 1205 * non-clipped blitter. Ptype is the destination pixel type,
1190 * Ctype the translucent count type, and do_blend the 1206 * Ctype the translucent count type, and do_blend the
1191 * macro to blend one pixel. 1207 * macro to blend one pixel.
1192 */ 1208 */
1193 #define RLEALPHABLIT(Ptype, Ctype, do_blend) \ 1209 #define RLEALPHABLIT(Ptype, Ctype, do_blend) \
1194 do { \ 1210 do { \
1195 int linecount = srcrect->h; \ 1211 int linecount = srcrect->h; \
1196 do { \ 1212 do { \
1197 int ofs = 0; \ 1213 int ofs = 0; \
1233 } while(ofs < w); \ 1249 } while(ofs < w); \
1234 dstbuf += dst->pitch; \ 1250 dstbuf += dst->pitch; \
1235 } while(--linecount); \ 1251 } while(--linecount); \
1236 } while(0) 1252 } while(0)
1237 1253
1238 switch(df->BytesPerPixel) { 1254 switch (df->BytesPerPixel) {
1239 case 2: 1255 case 2:
1240 if(df->Gmask == 0x07e0 || df->Rmask == 0x07e0 1256 if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0
1241 || df->Bmask == 0x07e0) 1257 || df->Bmask == 0x07e0)
1242 RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565); 1258 RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
1243 else 1259 else
1244 RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555); 1260 RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
1245 break; 1261 break;
1246 case 4: 1262 case 4:
1247 RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888); 1263 RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
1248 break; 1264 break;
1249 } 1265 }
1250 } 1266 }
1251 1267
1252 done: 1268 done:
1253 /* Unlock the destination if necessary */ 1269 /* Unlock the destination if necessary */
1254 if ( SDL_MUSTLOCK(dst) ) { 1270 if (SDL_MUSTLOCK(dst)) {
1255 SDL_UnlockSurface(dst); 1271 SDL_UnlockSurface(dst);
1256 } 1272 }
1257 return 0; 1273 return 0;
1258 } 1274 }
1259 1275
1260 /* 1276 /*
1266 * These are only used in the encoder and un-RLE code and are therefore not 1282 * These are only used in the encoder and un-RLE code and are therefore not
1267 * highly optimised. 1283 * highly optimised.
1268 */ 1284 */
1269 1285
1270 /* encode 32bpp rgb + a into 16bpp rgb, losing alpha */ 1286 /* encode 32bpp rgb + a into 16bpp rgb, losing alpha */
1271 static int copy_opaque_16(void *dst, Uint32 *src, int n, 1287 static int
1272 SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt) 1288 copy_opaque_16(void *dst, Uint32 * src, int n,
1289 SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
1273 { 1290 {
1274 int i; 1291 int i;
1275 Uint16 *d = dst; 1292 Uint16 *d = dst;
1276 for(i = 0; i < n; i++) { 1293 for (i = 0; i < n; i++) {
1277 unsigned r, g, b; 1294 unsigned r, g, b;
1278 RGB_FROM_PIXEL(*src, sfmt, r, g, b); 1295 RGB_FROM_PIXEL(*src, sfmt, r, g, b);
1279 PIXEL_FROM_RGB(*d, dfmt, r, g, b); 1296 PIXEL_FROM_RGB(*d, dfmt, r, g, b);
1280 src++; 1297 src++;
1281 d++; 1298 d++;
1282 } 1299 }
1283 return n * 2; 1300 return n * 2;
1284 } 1301 }
1285 1302
1286 /* decode opaque pixels from 16bpp to 32bpp rgb + a */ 1303 /* decode opaque pixels from 16bpp to 32bpp rgb + a */
1287 static int uncopy_opaque_16(Uint32 *dst, void *src, int n, 1304 static int
1288 RLEDestFormat *sfmt, SDL_PixelFormat *dfmt) 1305 uncopy_opaque_16(Uint32 * dst, void *src, int n,
1306 RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
1289 { 1307 {
1290 int i; 1308 int i;
1291 Uint16 *s = src; 1309 Uint16 *s = src;
1292 unsigned alpha = dfmt->Amask ? 255 : 0; 1310 unsigned alpha = dfmt->Amask ? 255 : 0;
1293 for(i = 0; i < n; i++) { 1311 for (i = 0; i < n; i++) {
1294 unsigned r, g, b; 1312 unsigned r, g, b;
1295 RGB_FROM_PIXEL(*s, sfmt, r, g, b); 1313 RGB_FROM_PIXEL(*s, sfmt, r, g, b);
1296 PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, alpha); 1314 PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, alpha);
1297 s++; 1315 s++;
1298 dst++; 1316 dst++;
1299 } 1317 }
1300 return n * 2; 1318 return n * 2;
1301 } 1319 }
1302 1320
1303 1321
1304 1322
1305 /* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565 */ 1323 /* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565 */
1306 static int copy_transl_565(void *dst, Uint32 *src, int n, 1324 static int
1307 SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt) 1325 copy_transl_565(void *dst, Uint32 * src, int n,
1326 SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
1308 { 1327 {
1309 int i; 1328 int i;
1310 Uint32 *d = dst; 1329 Uint32 *d = dst;
1311 for(i = 0; i < n; i++) { 1330 for (i = 0; i < n; i++) {
1312 unsigned r, g, b, a; 1331 unsigned r, g, b, a;
1313 Uint16 pix; 1332 Uint16 pix;
1314 RGBA_FROM_8888(*src, sfmt, r, g, b, a); 1333 RGBA_FROM_8888(*src, sfmt, r, g, b, a);
1315 PIXEL_FROM_RGB(pix, dfmt, r, g, b); 1334 PIXEL_FROM_RGB(pix, dfmt, r, g, b);
1316 *d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0); 1335 *d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
1317 src++; 1336 src++;
1318 d++; 1337 d++;
1319 } 1338 }
1320 return n * 4; 1339 return n * 4;
1321 } 1340 }
1322 1341
1323 /* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555 */ 1342 /* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555 */
1324 static int copy_transl_555(void *dst, Uint32 *src, int n, 1343 static int
1325 SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt) 1344 copy_transl_555(void *dst, Uint32 * src, int n,
1345 SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
1326 { 1346 {
1327 int i; 1347 int i;
1328 Uint32 *d = dst; 1348 Uint32 *d = dst;
1329 for(i = 0; i < n; i++) { 1349 for (i = 0; i < n; i++) {
1330 unsigned r, g, b, a; 1350 unsigned r, g, b, a;
1331 Uint16 pix; 1351 Uint16 pix;
1332 RGBA_FROM_8888(*src, sfmt, r, g, b, a); 1352 RGBA_FROM_8888(*src, sfmt, r, g, b, a);
1333 PIXEL_FROM_RGB(pix, dfmt, r, g, b); 1353 PIXEL_FROM_RGB(pix, dfmt, r, g, b);
1334 *d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0); 1354 *d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
1335 src++; 1355 src++;
1336 d++; 1356 d++;
1337 } 1357 }
1338 return n * 4; 1358 return n * 4;
1339 } 1359 }
1340 1360
1341 /* decode translucent pixels from 32bpp GORAB to 32bpp rgb + a */ 1361 /* decode translucent pixels from 32bpp GORAB to 32bpp rgb + a */
1342 static int uncopy_transl_16(Uint32 *dst, void *src, int n, 1362 static int
1343 RLEDestFormat *sfmt, SDL_PixelFormat *dfmt) 1363 uncopy_transl_16(Uint32 * dst, void *src, int n,
1364 RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
1344 { 1365 {
1345 int i; 1366 int i;
1346 Uint32 *s = src; 1367 Uint32 *s = src;
1347 for(i = 0; i < n; i++) { 1368 for (i = 0; i < n; i++) {
1348 unsigned r, g, b, a; 1369 unsigned r, g, b, a;
1349 Uint32 pix = *s++; 1370 Uint32 pix = *s++;
1350 a = (pix & 0x3e0) >> 2; 1371 a = (pix & 0x3e0) >> 2;
1351 pix = (pix & ~0x3e0) | pix >> 16; 1372 pix = (pix & ~0x3e0) | pix >> 16;
1352 RGB_FROM_PIXEL(pix, sfmt, r, g, b); 1373 RGB_FROM_PIXEL(pix, sfmt, r, g, b);
1353 PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a); 1374 PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
1354 dst++; 1375 dst++;
1355 } 1376 }
1356 return n * 4; 1377 return n * 4;
1357 } 1378 }
1358 1379
1359 /* encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */ 1380 /* encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
1360 static int copy_32(void *dst, Uint32 *src, int n, 1381 static int
1361 SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt) 1382 copy_32(void *dst, Uint32 * src, int n,
1383 SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
1362 { 1384 {
1363 int i; 1385 int i;
1364 Uint32 *d = dst; 1386 Uint32 *d = dst;
1365 for(i = 0; i < n; i++) { 1387 for (i = 0; i < n; i++) {
1366 unsigned r, g, b, a; 1388 unsigned r, g, b, a;
1367 Uint32 pixel; 1389 Uint32 pixel;
1368 RGBA_FROM_8888(*src, sfmt, r, g, b, a); 1390 RGBA_FROM_8888(*src, sfmt, r, g, b, a);
1369 PIXEL_FROM_RGB(pixel, dfmt, r, g, b); 1391 PIXEL_FROM_RGB(pixel, dfmt, r, g, b);
1370 *d++ = pixel | a << 24; 1392 *d++ = pixel | a << 24;
1371 src++; 1393 src++;
1372 } 1394 }
1373 return n * 4; 1395 return n * 4;
1374 } 1396 }
1375 1397
1376 /* decode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */ 1398 /* decode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
1377 static int uncopy_32(Uint32 *dst, void *src, int n, 1399 static int
1378 RLEDestFormat *sfmt, SDL_PixelFormat *dfmt) 1400 uncopy_32(Uint32 * dst, void *src, int n,
1401 RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
1379 { 1402 {
1380 int i; 1403 int i;
1381 Uint32 *s = src; 1404 Uint32 *s = src;
1382 for(i = 0; i < n; i++) { 1405 for (i = 0; i < n; i++) {
1383 unsigned r, g, b, a; 1406 unsigned r, g, b, a;
1384 Uint32 pixel = *s++; 1407 Uint32 pixel = *s++;
1385 RGB_FROM_PIXEL(pixel, sfmt, r, g, b); 1408 RGB_FROM_PIXEL(pixel, sfmt, r, g, b);
1386 a = pixel >> 24; 1409 a = pixel >> 24;
1387 PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a); 1410 PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
1388 dst++; 1411 dst++;
1389 } 1412 }
1390 return n * 4; 1413 return n * 4;
1391 } 1414 }
1392 1415
1393 #define ISOPAQUE(pixel, fmt) ((((pixel) & fmt->Amask) >> fmt->Ashift) == 255) 1416 #define ISOPAQUE(pixel, fmt) ((((pixel) & fmt->Amask) >> fmt->Ashift) == 255)
1394 1417
1395 #define ISTRANSL(pixel, fmt) \ 1418 #define ISTRANSL(pixel, fmt) \
1396 ((unsigned)((((pixel) & fmt->Amask) >> fmt->Ashift) - 1U) < 254U) 1419 ((unsigned)((((pixel) & fmt->Amask) >> fmt->Ashift) - 1U) < 254U)
1397 1420
1398 /* convert surface to be quickly alpha-blittable onto dest, if possible */ 1421 /* convert surface to be quickly alpha-blittable onto dest, if possible */
1399 static int RLEAlphaSurface(SDL_Surface *surface) 1422 static int
1423 RLEAlphaSurface(SDL_Surface * surface)
1400 { 1424 {
1401 SDL_Surface *dest; 1425 SDL_Surface *dest;
1402 SDL_PixelFormat *df; 1426 SDL_PixelFormat *df;
1403 int maxsize = 0; 1427 int maxsize = 0;
1404 int max_opaque_run; 1428 int max_opaque_run;
1405 int max_transl_run = 65535; 1429 int max_transl_run = 65535;
1406 unsigned masksum; 1430 unsigned masksum;
1407 Uint8 *rlebuf, *dst; 1431 Uint8 *rlebuf, *dst;
1408 int (*copy_opaque)(void *, Uint32 *, int, 1432 int (*copy_opaque) (void *, Uint32 *, int,
1409 SDL_PixelFormat *, SDL_PixelFormat *); 1433 SDL_PixelFormat *, SDL_PixelFormat *);
1410 int (*copy_transl)(void *, Uint32 *, int, 1434 int (*copy_transl) (void *, Uint32 *, int,
1411 SDL_PixelFormat *, SDL_PixelFormat *); 1435 SDL_PixelFormat *, SDL_PixelFormat *);
1412 1436
1413 dest = surface->map->dst; 1437 dest = surface->map->dst;
1414 if(!dest) 1438 if (!dest)
1415 return -1; 1439 return -1;
1416 df = dest->format; 1440 df = dest->format;
1417 if(surface->format->BitsPerPixel != 32) 1441 if (surface->format->BitsPerPixel != 32)
1418 return -1; /* only 32bpp source supported */ 1442 return -1; /* only 32bpp source supported */
1419 1443
1420 /* find out whether the destination is one we support, 1444 /* find out whether the destination is one we support,
1421 and determine the max size of the encoded result */ 1445 and determine the max size of the encoded result */
1422 masksum = df->Rmask | df->Gmask | df->Bmask; 1446 masksum = df->Rmask | df->Gmask | df->Bmask;
1423 switch(df->BytesPerPixel) { 1447 switch (df->BytesPerPixel) {
1424 case 2: 1448 case 2:
1425 /* 16bpp: only support 565 and 555 formats */ 1449 /* 16bpp: only support 565 and 555 formats */
1426 switch(masksum) { 1450 switch (masksum) {
1427 case 0xffff: 1451 case 0xffff:
1428 if(df->Gmask == 0x07e0 1452 if (df->Gmask == 0x07e0
1429 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) { 1453 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
1430 copy_opaque = copy_opaque_16; 1454 copy_opaque = copy_opaque_16;
1431 copy_transl = copy_transl_565; 1455 copy_transl = copy_transl_565;
1432 } else 1456 } else
1433 return -1; 1457 return -1;
1434 break; 1458 break;
1435 case 0x7fff: 1459 case 0x7fff:
1436 if(df->Gmask == 0x03e0 1460 if (df->Gmask == 0x03e0
1437 || df->Rmask == 0x03e0 || df->Bmask == 0x03e0) { 1461 || df->Rmask == 0x03e0 || df->Bmask == 0x03e0) {
1438 copy_opaque = copy_opaque_16; 1462 copy_opaque = copy_opaque_16;
1439 copy_transl = copy_transl_555; 1463 copy_transl = copy_transl_555;
1440 } else 1464 } else
1441 return -1; 1465 return -1;
1442 break; 1466 break;
1443 default: 1467 default:
1444 return -1; 1468 return -1;
1445 } 1469 }
1446 max_opaque_run = 255; /* runs stored as bytes */ 1470 max_opaque_run = 255; /* runs stored as bytes */
1447 1471
1448 /* worst case is alternating opaque and translucent pixels, 1472 /* worst case is alternating opaque and translucent pixels,
1449 with room for alignment padding between lines */ 1473 with room for alignment padding between lines */
1450 maxsize = surface->h * (2 + (4 + 2) * (surface->w + 1)) + 2; 1474 maxsize = surface->h * (2 + (4 + 2) * (surface->w + 1)) + 2;
1451 break; 1475 break;
1452 case 4: 1476 case 4:
1453 if(masksum != 0x00ffffff) 1477 if (masksum != 0x00ffffff)
1454 return -1; /* requires unused high byte */ 1478 return -1; /* requires unused high byte */
1455 copy_opaque = copy_32; 1479 copy_opaque = copy_32;
1456 copy_transl = copy_32; 1480 copy_transl = copy_32;
1457 max_opaque_run = 255; /* runs stored as short ints */ 1481 max_opaque_run = 255; /* runs stored as short ints */
1458 1482
1459 /* worst case is alternating opaque and translucent pixels */ 1483 /* worst case is alternating opaque and translucent pixels */
1460 maxsize = surface->h * 2 * 4 * (surface->w + 1) + 4; 1484 maxsize = surface->h * 2 * 4 * (surface->w + 1) + 4;
1461 break; 1485 break;
1462 default: 1486 default:
1463 return -1; /* anything else unsupported right now */ 1487 return -1; /* anything else unsupported right now */
1464 } 1488 }
1465 1489
1466 maxsize += sizeof(RLEDestFormat); 1490 maxsize += sizeof(RLEDestFormat);
1467 rlebuf = (Uint8 *)SDL_malloc(maxsize); 1491 rlebuf = (Uint8 *) SDL_malloc(maxsize);
1468 if(!rlebuf) { 1492 if (!rlebuf) {
1469 SDL_OutOfMemory(); 1493 SDL_OutOfMemory();
1470 return -1; 1494 return -1;
1471 } 1495 }
1472 { 1496 {
1473 /* save the destination format so we can undo the encoding later */ 1497 /* save the destination format so we can undo the encoding later */
1474 RLEDestFormat *r = (RLEDestFormat *)rlebuf; 1498 RLEDestFormat *r = (RLEDestFormat *) rlebuf;
1475 r->BytesPerPixel = df->BytesPerPixel; 1499 r->BytesPerPixel = df->BytesPerPixel;
1476 r->Rloss = df->Rloss; 1500 r->Rloss = df->Rloss;
1477 r->Gloss = df->Gloss; 1501 r->Gloss = df->Gloss;
1478 r->Bloss = df->Bloss; 1502 r->Bloss = df->Bloss;
1479 r->Rshift = df->Rshift; 1503 r->Rshift = df->Rshift;
1480 r->Gshift = df->Gshift; 1504 r->Gshift = df->Gshift;
1481 r->Bshift = df->Bshift; 1505 r->Bshift = df->Bshift;
1482 r->Ashift = df->Ashift; 1506 r->Ashift = df->Ashift;
1483 r->Rmask = df->Rmask; 1507 r->Rmask = df->Rmask;
1484 r->Gmask = df->Gmask; 1508 r->Gmask = df->Gmask;
1485 r->Bmask = df->Bmask; 1509 r->Bmask = df->Bmask;
1486 r->Amask = df->Amask; 1510 r->Amask = df->Amask;
1487 } 1511 }
1488 dst = rlebuf + sizeof(RLEDestFormat); 1512 dst = rlebuf + sizeof(RLEDestFormat);
1489 1513
1490 /* Do the actual encoding */ 1514 /* Do the actual encoding */
1491 { 1515 {
1492 int x, y; 1516 int x, y;
1493 int h = surface->h, w = surface->w; 1517 int h = surface->h, w = surface->w;
1494 SDL_PixelFormat *sf = surface->format; 1518 SDL_PixelFormat *sf = surface->format;
1495 Uint32 *src = (Uint32 *)surface->pixels; 1519 Uint32 *src = (Uint32 *) surface->pixels;
1496 Uint8 *lastline = dst; /* end of last non-blank line */ 1520 Uint8 *lastline = dst; /* end of last non-blank line */
1497 1521
1498 /* opaque counts are 8 or 16 bits, depending on target depth */ 1522 /* opaque counts are 8 or 16 bits, depending on target depth */
1499 #define ADD_OPAQUE_COUNTS(n, m) \ 1523 #define ADD_OPAQUE_COUNTS(n, m) \
1500 if(df->BytesPerPixel == 4) { \ 1524 if(df->BytesPerPixel == 4) { \
1501 ((Uint16 *)dst)[0] = n; \ 1525 ((Uint16 *)dst)[0] = n; \
1502 ((Uint16 *)dst)[1] = m; \ 1526 ((Uint16 *)dst)[1] = m; \
1503 dst += 4; \ 1527 dst += 4; \
1505 dst[0] = n; \ 1529 dst[0] = n; \
1506 dst[1] = m; \ 1530 dst[1] = m; \
1507 dst += 2; \ 1531 dst += 2; \
1508 } 1532 }
1509 1533
1510 /* translucent counts are always 16 bit */ 1534 /* translucent counts are always 16 bit */
1511 #define ADD_TRANSL_COUNTS(n, m) \ 1535 #define ADD_TRANSL_COUNTS(n, m) \
1512 (((Uint16 *)dst)[0] = n, ((Uint16 *)dst)[1] = m, dst += 4) 1536 (((Uint16 *)dst)[0] = n, ((Uint16 *)dst)[1] = m, dst += 4)
1513 1537
1514 for(y = 0; y < h; y++) { 1538 for (y = 0; y < h; y++) {
1515 int runstart, skipstart; 1539 int runstart, skipstart;
1516 int blankline = 0; 1540 int blankline = 0;
1517 /* First encode all opaque pixels of a scan line */ 1541 /* First encode all opaque pixels of a scan line */
1518 x = 0; 1542 x = 0;
1519 do { 1543 do {
1520 int run, skip, len; 1544 int run, skip, len;
1521 skipstart = x; 1545 skipstart = x;
1522 while(x < w && !ISOPAQUE(src[x], sf)) 1546 while (x < w && !ISOPAQUE(src[x], sf))
1523 x++; 1547 x++;
1524 runstart = x; 1548 runstart = x;
1525 while(x < w && ISOPAQUE(src[x], sf)) 1549 while (x < w && ISOPAQUE(src[x], sf))
1526 x++; 1550 x++;
1527 skip = runstart - skipstart; 1551 skip = runstart - skipstart;
1528 if(skip == w) 1552 if (skip == w)
1529 blankline = 1; 1553 blankline = 1;
1530 run = x - runstart; 1554 run = x - runstart;
1531 while(skip > max_opaque_run) { 1555 while (skip > max_opaque_run) {
1532 ADD_OPAQUE_COUNTS(max_opaque_run, 0); 1556 ADD_OPAQUE_COUNTS(max_opaque_run, 0);
1533 skip -= max_opaque_run; 1557 skip -= max_opaque_run;
1534 } 1558 }
1535 len = MIN(run, max_opaque_run); 1559 len = MIN(run, max_opaque_run);
1536 ADD_OPAQUE_COUNTS(skip, len); 1560 ADD_OPAQUE_COUNTS(skip, len);
1537 dst += copy_opaque(dst, src + runstart, len, sf, df); 1561 dst += copy_opaque(dst, src + runstart, len, sf, df);
1538 runstart += len; 1562 runstart += len;
1539 run -= len; 1563 run -= len;
1540 while(run) { 1564 while (run) {
1541 len = MIN(run, max_opaque_run); 1565 len = MIN(run, max_opaque_run);
1542 ADD_OPAQUE_COUNTS(0, len); 1566 ADD_OPAQUE_COUNTS(0, len);
1543 dst += copy_opaque(dst, src + runstart, len, sf, df); 1567 dst += copy_opaque(dst, src + runstart, len, sf, df);
1544 runstart += len; 1568 runstart += len;
1545 run -= len; 1569 run -= len;
1546 } 1570 }
1547 } while(x < w); 1571 }
1548 1572 while (x < w);
1549 /* Make sure the next output address is 32-bit aligned */ 1573
1550 dst += (uintptr_t)dst & 2; 1574 /* Make sure the next output address is 32-bit aligned */
1551 1575 dst += (uintptr_t) dst & 2;
1552 /* Next, encode all translucent pixels of the same scan line */ 1576
1553 x = 0; 1577 /* Next, encode all translucent pixels of the same scan line */
1554 do { 1578 x = 0;
1555 int run, skip, len; 1579 do {
1556 skipstart = x; 1580 int run, skip, len;
1557 while(x < w && !ISTRANSL(src[x], sf)) 1581 skipstart = x;
1558 x++; 1582 while (x < w && !ISTRANSL(src[x], sf))
1559 runstart = x; 1583 x++;
1560 while(x < w && ISTRANSL(src[x], sf)) 1584 runstart = x;
1561 x++; 1585 while (x < w && ISTRANSL(src[x], sf))
1562 skip = runstart - skipstart; 1586 x++;
1563 blankline &= (skip == w); 1587 skip = runstart - skipstart;
1564 run = x - runstart; 1588 blankline &= (skip == w);
1565 while(skip > max_transl_run) { 1589 run = x - runstart;
1566 ADD_TRANSL_COUNTS(max_transl_run, 0); 1590 while (skip > max_transl_run) {
1567 skip -= max_transl_run; 1591 ADD_TRANSL_COUNTS(max_transl_run, 0);
1568 } 1592 skip -= max_transl_run;
1569 len = MIN(run, max_transl_run); 1593 }
1570 ADD_TRANSL_COUNTS(skip, len); 1594 len = MIN(run, max_transl_run);
1571 dst += copy_transl(dst, src + runstart, len, sf, df); 1595 ADD_TRANSL_COUNTS(skip, len);
1572 runstart += len; 1596 dst += copy_transl(dst, src + runstart, len, sf, df);
1573 run -= len; 1597 runstart += len;
1574 while(run) { 1598 run -= len;
1575 len = MIN(run, max_transl_run); 1599 while (run) {
1576 ADD_TRANSL_COUNTS(0, len); 1600 len = MIN(run, max_transl_run);
1577 dst += copy_transl(dst, src + runstart, len, sf, df); 1601 ADD_TRANSL_COUNTS(0, len);
1578 runstart += len; 1602 dst += copy_transl(dst, src + runstart, len, sf, df);
1579 run -= len; 1603 runstart += len;
1580 } 1604 run -= len;
1581 if(!blankline) 1605 }
1582 lastline = dst; 1606 if (!blankline)
1583 } while(x < w); 1607 lastline = dst;
1584 1608 }
1585 src += surface->pitch >> 2; 1609 while (x < w);
1586 } 1610
1587 dst = lastline; /* back up past trailing blank lines */ 1611 src += surface->pitch >> 2;
1588 ADD_OPAQUE_COUNTS(0, 0); 1612 }
1613 dst = lastline; /* back up past trailing blank lines */
1614 ADD_OPAQUE_COUNTS(0, 0);
1589 } 1615 }
1590 1616
1591 #undef ADD_OPAQUE_COUNTS 1617 #undef ADD_OPAQUE_COUNTS
1592 #undef ADD_TRANSL_COUNTS 1618 #undef ADD_TRANSL_COUNTS
1593 1619
1594 /* Now that we have it encoded, release the original pixels */ 1620 /* Now that we have it encoded, release the original pixels */
1595 if((surface->flags & SDL_PREALLOC) != SDL_PREALLOC 1621 if (!(surface->flags & SDL_PREALLOC) && !(surface->flags & SDL_HWSURFACE)) {
1596 && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) { 1622 SDL_free(surface->pixels);
1597 SDL_free( surface->pixels ); 1623 surface->pixels = NULL;
1598 surface->pixels = NULL;
1599 } 1624 }
1600 1625
1601 /* realloc the buffer to release unused memory */ 1626 /* realloc the buffer to release unused memory */
1602 { 1627 {
1603 Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf); 1628 Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf);
1604 if(!p) 1629 if (!p)
1605 p = rlebuf; 1630 p = rlebuf;
1606 surface->map->sw_data->aux_data = p; 1631 surface->map->sw_data->aux_data = p;
1607 } 1632 }
1608 1633
1609 return 0; 1634 return 0;
1610 } 1635 }
1611 1636
1612 static Uint32 getpix_8(Uint8 *srcbuf) 1637 static Uint32
1638 getpix_8(Uint8 * srcbuf)
1613 { 1639 {
1614 return *srcbuf; 1640 return *srcbuf;
1615 } 1641 }
1616 1642
1617 static Uint32 getpix_16(Uint8 *srcbuf) 1643 static Uint32
1618 { 1644 getpix_16(Uint8 * srcbuf)
1619 return *(Uint16 *)srcbuf; 1645 {
1646 return *(Uint16 *) srcbuf;
1620 } 1647 }
1621 1648
1622 static Uint32 getpix_24(Uint8 *srcbuf) 1649 static Uint32
1650 getpix_24(Uint8 * srcbuf)
1623 { 1651 {
1624 #if SDL_BYTEORDER == SDL_LIL_ENDIAN 1652 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
1625 return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16); 1653 return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16);
1626 #else 1654 #else
1627 return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2]; 1655 return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];
1628 #endif 1656 #endif
1629 } 1657 }
1630 1658
1631 static Uint32 getpix_32(Uint8 *srcbuf) 1659 static Uint32
1632 { 1660 getpix_32(Uint8 * srcbuf)
1633 return *(Uint32 *)srcbuf; 1661 {
1662 return *(Uint32 *) srcbuf;
1634 } 1663 }
1635 1664
1636 typedef Uint32 (*getpix_func)(Uint8 *); 1665 typedef Uint32(*getpix_func) (Uint8 *);
1637 1666
1638 static getpix_func getpixes[4] = { 1667 static getpix_func getpixes[4] = {
1639 getpix_8, getpix_16, getpix_24, getpix_32 1668 getpix_8, getpix_16, getpix_24, getpix_32
1640 }; 1669 };
1641 1670
1642 static int RLEColorkeySurface(SDL_Surface *surface) 1671 static int
1643 { 1672 RLEColorkeySurface(SDL_Surface * surface)
1644 Uint8 *rlebuf, *dst; 1673 {
1645 int maxn; 1674 Uint8 *rlebuf, *dst;
1646 int y; 1675 int maxn;
1647 Uint8 *srcbuf, *curbuf, *lastline; 1676 int y;
1648 int maxsize = 0; 1677 Uint8 *srcbuf, *curbuf, *lastline;
1649 int skip, run; 1678 int maxsize = 0;
1650 int bpp = surface->format->BytesPerPixel; 1679 int skip, run;
1651 getpix_func getpix; 1680 int bpp = surface->format->BytesPerPixel;
1652 Uint32 ckey, rgbmask; 1681 getpix_func getpix;
1653 int w, h; 1682 Uint32 ckey, rgbmask;
1654 1683 int w, h;
1655 /* calculate the worst case size for the compressed surface */ 1684
1656 switch(bpp) { 1685 /* calculate the worst case size for the compressed surface */
1657 case 1: 1686 switch (bpp) {
1658 /* worst case is alternating opaque and transparent pixels, 1687 case 1:
1659 starting with an opaque pixel */ 1688 /* worst case is alternating opaque and transparent pixels,
1660 maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2; 1689 starting with an opaque pixel */
1661 break; 1690 maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2;
1662 case 2: 1691 break;
1663 case 3: 1692 case 2:
1664 /* worst case is solid runs, at most 255 pixels wide */ 1693 case 3:
1665 maxsize = surface->h * (2 * (surface->w / 255 + 1) 1694 /* worst case is solid runs, at most 255 pixels wide */
1666 + surface->w * bpp) + 2; 1695 maxsize = surface->h * (2 * (surface->w / 255 + 1)
1667 break; 1696 + surface->w * bpp) + 2;
1668 case 4: 1697 break;
1669 /* worst case is solid runs, at most 65535 pixels wide */ 1698 case 4:
1670 maxsize = surface->h * (4 * (surface->w / 65535 + 1) 1699 /* worst case is solid runs, at most 65535 pixels wide */
1671 + surface->w * 4) + 4; 1700 maxsize = surface->h * (4 * (surface->w / 65535 + 1)
1672 break; 1701 + surface->w * 4) + 4;
1673 } 1702 break;
1674 1703 }
1675 rlebuf = (Uint8 *)SDL_malloc(maxsize); 1704
1676 if ( rlebuf == NULL ) { 1705 rlebuf = (Uint8 *) SDL_malloc(maxsize);
1677 SDL_OutOfMemory(); 1706 if (rlebuf == NULL) {
1678 return(-1); 1707 SDL_OutOfMemory();
1679 } 1708 return (-1);
1680 1709 }
1681 /* Set up the conversion */ 1710
1682 srcbuf = (Uint8 *)surface->pixels; 1711 /* Set up the conversion */
1683 curbuf = srcbuf; 1712 srcbuf = (Uint8 *) surface->pixels;
1684 maxn = bpp == 4 ? 65535 : 255; 1713 curbuf = srcbuf;
1685 skip = run = 0; 1714 maxn = bpp == 4 ? 65535 : 255;
1686 dst = rlebuf; 1715 skip = run = 0;
1687 rgbmask = ~surface->format->Amask; 1716 dst = rlebuf;
1688 ckey = surface->format->colorkey & rgbmask; 1717 rgbmask = ~surface->format->Amask;
1689 lastline = dst; 1718 ckey = surface->format->colorkey & rgbmask;
1690 getpix = getpixes[bpp - 1]; 1719 lastline = dst;
1691 w = surface->w; 1720 getpix = getpixes[bpp - 1];
1692 h = surface->h; 1721 w = surface->w;
1722 h = surface->h;
1693 1723
1694 #define ADD_COUNTS(n, m) \ 1724 #define ADD_COUNTS(n, m) \
1695 if(bpp == 4) { \ 1725 if(bpp == 4) { \
1696 ((Uint16 *)dst)[0] = n; \ 1726 ((Uint16 *)dst)[0] = n; \
1697 ((Uint16 *)dst)[1] = m; \ 1727 ((Uint16 *)dst)[1] = m; \
1700 dst[0] = n; \ 1730 dst[0] = n; \
1701 dst[1] = m; \ 1731 dst[1] = m; \
1702 dst += 2; \ 1732 dst += 2; \
1703 } 1733 }
1704 1734
1705 for(y = 0; y < h; y++) { 1735 for (y = 0; y < h; y++) {
1706 int x = 0; 1736 int x = 0;
1707 int blankline = 0; 1737 int blankline = 0;
1708 do { 1738 do {
1709 int run, skip, len; 1739 int run, skip, len;
1710 int runstart; 1740 int runstart;
1711 int skipstart = x; 1741 int skipstart = x;
1712 1742
1713 /* find run of transparent, then opaque pixels */ 1743 /* find run of transparent, then opaque pixels */
1714 while(x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey) 1744 while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey)
1715 x++; 1745 x++;
1716 runstart = x; 1746 runstart = x;
1717 while(x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey) 1747 while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey)
1718 x++; 1748 x++;
1719 skip = runstart - skipstart; 1749 skip = runstart - skipstart;
1720 if(skip == w) 1750 if (skip == w)
1721 blankline = 1; 1751 blankline = 1;
1722 run = x - runstart; 1752 run = x - runstart;
1723 1753
1724 /* encode segment */ 1754 /* encode segment */
1725 while(skip > maxn) { 1755 while (skip > maxn) {
1726 ADD_COUNTS(maxn, 0); 1756 ADD_COUNTS(maxn, 0);
1727 skip -= maxn; 1757 skip -= maxn;
1728 } 1758 }
1729 len = MIN(run, maxn); 1759 len = MIN(run, maxn);
1730 ADD_COUNTS(skip, len); 1760 ADD_COUNTS(skip, len);
1731 SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp); 1761 SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
1732 dst += len * bpp; 1762 dst += len * bpp;
1733 run -= len; 1763 run -= len;
1734 runstart += len; 1764 runstart += len;
1735 while(run) { 1765 while (run) {
1736 len = MIN(run, maxn); 1766 len = MIN(run, maxn);
1737 ADD_COUNTS(0, len); 1767 ADD_COUNTS(0, len);
1738 SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp); 1768 SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
1739 dst += len * bpp; 1769 dst += len * bpp;
1740 runstart += len; 1770 runstart += len;
1741 run -= len; 1771 run -= len;
1742 } 1772 }
1743 if(!blankline) 1773 if (!blankline)
1744 lastline = dst; 1774 lastline = dst;
1745 } while(x < w); 1775 }
1746 1776 while (x < w);
1747 srcbuf += surface->pitch; 1777
1748 } 1778 srcbuf += surface->pitch;
1749 dst = lastline; /* back up bast trailing blank lines */ 1779 }
1750 ADD_COUNTS(0, 0); 1780 dst = lastline; /* back up bast trailing blank lines */
1781 ADD_COUNTS(0, 0);
1751 1782
1752 #undef ADD_COUNTS 1783 #undef ADD_COUNTS
1753 1784
1754 /* Now that we have it encoded, release the original pixels */ 1785 /* Now that we have it encoded, release the original pixels */
1755 if((surface->flags & SDL_PREALLOC) != SDL_PREALLOC 1786 if (!(surface->flags & SDL_PREALLOC) && !(surface->flags & SDL_HWSURFACE)) {
1756 && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) { 1787 SDL_free(surface->pixels);
1757 SDL_free( surface->pixels ); 1788 surface->pixels = NULL;
1758 surface->pixels = NULL; 1789 }
1759 } 1790
1760 1791 /* realloc the buffer to release unused memory */
1761 /* realloc the buffer to release unused memory */ 1792 {
1762 { 1793 /* If realloc returns NULL, the original block is left intact */
1763 /* If realloc returns NULL, the original block is left intact */ 1794 Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf);
1764 Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf); 1795 if (!p)
1765 if(!p) 1796 p = rlebuf;
1766 p = rlebuf; 1797 surface->map->sw_data->aux_data = p;
1767 surface->map->sw_data->aux_data = p; 1798 }
1768 } 1799
1769 1800 return (0);
1770 return(0);
1771 } 1801 }
1772 1802
1773 int SDL_RLESurface(SDL_Surface *surface) 1803 int
1774 { 1804 SDL_RLESurface(SDL_Surface * surface)
1775 int retcode; 1805 {
1776 1806 int retcode;
1777 /* Clear any previous RLE conversion */ 1807
1778 if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { 1808 /* Clear any previous RLE conversion */
1779 SDL_UnRLESurface(surface, 1); 1809 if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
1780 } 1810 SDL_UnRLESurface(surface, 1);
1781 1811 }
1782 /* We don't support RLE encoding of bitmaps */ 1812
1783 if ( surface->format->BitsPerPixel < 8 ) { 1813 /* We don't support RLE encoding of bitmaps */
1784 return(-1); 1814 if (surface->format->BitsPerPixel < 8) {
1785 } 1815 return (-1);
1786 1816 }
1787 /* Lock the surface if it's in hardware */ 1817
1788 if ( SDL_MUSTLOCK(surface) ) { 1818 /* Lock the surface if it's in hardware */
1789 if ( SDL_LockSurface(surface) < 0 ) { 1819 if (SDL_MUSTLOCK(surface)) {
1790 return(-1); 1820 if (SDL_LockSurface(surface) < 0) {
1791 } 1821 return (-1);
1792 } 1822 }
1793 1823 }
1794 /* Encode */ 1824
1795 if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) { 1825 /* Encode */
1796 retcode = RLEColorkeySurface(surface); 1826 if ((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
1797 } else { 1827 retcode = RLEColorkeySurface(surface);
1798 if((surface->flags & SDL_SRCALPHA) == SDL_SRCALPHA 1828 } else {
1799 && surface->format->Amask != 0) 1829 if ((surface->flags & SDL_SRCALPHA) == SDL_SRCALPHA
1800 retcode = RLEAlphaSurface(surface); 1830 && surface->format->Amask != 0)
1801 else 1831 retcode = RLEAlphaSurface(surface);
1802 retcode = -1; /* no RLE for per-surface alpha sans ckey */ 1832 else
1803 } 1833 retcode = -1; /* no RLE for per-surface alpha sans ckey */
1804 1834 }
1805 /* Unlock the surface if it's in hardware */ 1835
1806 if ( SDL_MUSTLOCK(surface) ) { 1836 /* Unlock the surface if it's in hardware */
1807 SDL_UnlockSurface(surface); 1837 if (SDL_MUSTLOCK(surface)) {
1808 } 1838 SDL_UnlockSurface(surface);
1809 1839 }
1810 if(retcode < 0) 1840
1811 return -1; 1841 if (retcode < 0)
1812 1842 return -1;
1813 /* The surface is now accelerated */ 1843
1814 surface->flags |= SDL_RLEACCEL; 1844 /* The surface is now accelerated */
1815 1845 surface->flags |= SDL_RLEACCEL;
1816 return(0); 1846
1847 return (0);
1817 } 1848 }
1818 1849
1819 /* 1850 /*
1820 * Un-RLE a surface with pixel alpha 1851 * Un-RLE a surface with pixel alpha
1821 * This may not give back exactly the image before RLE-encoding; all 1852 * This may not give back exactly the image before RLE-encoding; all
1822 * completely transparent pixels will be lost, and colour and alpha depth 1853 * completely transparent pixels will be lost, and colour and alpha depth
1823 * may have been reduced (when encoding for 16bpp targets). 1854 * may have been reduced (when encoding for 16bpp targets).
1824 */ 1855 */
1825 static SDL_bool UnRLEAlpha(SDL_Surface *surface) 1856 static SDL_bool
1857 UnRLEAlpha(SDL_Surface * surface)
1826 { 1858 {
1827 Uint8 *srcbuf; 1859 Uint8 *srcbuf;
1828 Uint32 *dst; 1860 Uint32 *dst;
1829 SDL_PixelFormat *sf = surface->format; 1861 SDL_PixelFormat *sf = surface->format;
1830 RLEDestFormat *df = surface->map->sw_data->aux_data; 1862 RLEDestFormat *df = surface->map->sw_data->aux_data;
1831 int (*uncopy_opaque)(Uint32 *, void *, int, 1863 int (*uncopy_opaque) (Uint32 *, void *, int,
1832 RLEDestFormat *, SDL_PixelFormat *); 1864 RLEDestFormat *, SDL_PixelFormat *);
1833 int (*uncopy_transl)(Uint32 *, void *, int, 1865 int (*uncopy_transl) (Uint32 *, void *, int,
1834 RLEDestFormat *, SDL_PixelFormat *); 1866 RLEDestFormat *, SDL_PixelFormat *);
1835 int w = surface->w; 1867 int w = surface->w;
1836 int bpp = df->BytesPerPixel; 1868 int bpp = df->BytesPerPixel;
1837 1869
1838 if(bpp == 2) { 1870 if (bpp == 2) {
1839 uncopy_opaque = uncopy_opaque_16; 1871 uncopy_opaque = uncopy_opaque_16;
1840 uncopy_transl = uncopy_transl_16; 1872 uncopy_transl = uncopy_transl_16;
1841 } else { 1873 } else {
1842 uncopy_opaque = uncopy_transl = uncopy_32; 1874 uncopy_opaque = uncopy_transl = uncopy_32;
1843 } 1875 }
1844 1876
1845 surface->pixels = SDL_malloc(surface->h * surface->pitch); 1877 surface->pixels = SDL_malloc(surface->h * surface->pitch);
1846 if ( !surface->pixels ) { 1878 if (!surface->pixels) {
1847 return(SDL_FALSE); 1879 return (SDL_FALSE);
1848 } 1880 }
1849 /* fill background with transparent pixels */ 1881 /* fill background with transparent pixels */
1850 SDL_memset(surface->pixels, 0, surface->h * surface->pitch); 1882 SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
1851 1883
1852 dst = surface->pixels; 1884 dst = surface->pixels;
1853 srcbuf = (Uint8 *)(df + 1); 1885 srcbuf = (Uint8 *) (df + 1);
1854 for(;;) { 1886 for (;;) {
1855 /* copy opaque pixels */ 1887 /* copy opaque pixels */
1856 int ofs = 0; 1888 int ofs = 0;
1857 do { 1889 do {
1858 unsigned run; 1890 unsigned run;
1859 if(bpp == 2) { 1891 if (bpp == 2) {
1860 ofs += srcbuf[0]; 1892 ofs += srcbuf[0];
1861 run = srcbuf[1]; 1893 run = srcbuf[1];
1862 srcbuf += 2; 1894 srcbuf += 2;
1863 } else { 1895 } else {
1864 ofs += ((Uint16 *)srcbuf)[0]; 1896 ofs += ((Uint16 *) srcbuf)[0];
1865 run = ((Uint16 *)srcbuf)[1]; 1897 run = ((Uint16 *) srcbuf)[1];
1866 srcbuf += 4; 1898 srcbuf += 4;
1867 } 1899 }
1868 if(run) { 1900 if (run) {
1869 srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf); 1901 srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf);
1870 ofs += run; 1902 ofs += run;
1871 } else if(!ofs) 1903 } else if (!ofs)
1872 return(SDL_TRUE); 1904 return (SDL_TRUE);
1873 } while(ofs < w); 1905 }
1874 1906 while (ofs < w);
1875 /* skip padding if needed */ 1907
1876 if(bpp == 2) 1908 /* skip padding if needed */
1877 srcbuf += (uintptr_t)srcbuf & 2; 1909 if (bpp == 2)
1878 1910 srcbuf += (uintptr_t) srcbuf & 2;
1879 /* copy translucent pixels */ 1911
1880 ofs = 0; 1912 /* copy translucent pixels */
1881 do { 1913 ofs = 0;
1882 unsigned run; 1914 do {
1883 ofs += ((Uint16 *)srcbuf)[0]; 1915 unsigned run;
1884 run = ((Uint16 *)srcbuf)[1]; 1916 ofs += ((Uint16 *) srcbuf)[0];
1885 srcbuf += 4; 1917 run = ((Uint16 *) srcbuf)[1];
1886 if(run) { 1918 srcbuf += 4;
1887 srcbuf += uncopy_transl(dst + ofs, srcbuf, run, df, sf); 1919 if (run) {
1888 ofs += run; 1920 srcbuf += uncopy_transl(dst + ofs, srcbuf, run, df, sf);
1889 } 1921 ofs += run;
1890 } while(ofs < w); 1922 }
1891 dst += surface->pitch >> 2; 1923 }
1924 while (ofs < w);
1925 dst += surface->pitch >> 2;
1892 } 1926 }
1893 /* Make the compiler happy */ 1927 /* Make the compiler happy */
1894 return(SDL_TRUE); 1928 return (SDL_TRUE);
1895 } 1929 }
1896 1930
1897 void SDL_UnRLESurface(SDL_Surface *surface, int recode) 1931 void
1898 { 1932 SDL_UnRLESurface(SDL_Surface * surface, int recode)
1899 if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { 1933 {
1900 surface->flags &= ~SDL_RLEACCEL; 1934 if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
1901 1935 surface->flags &= ~SDL_RLEACCEL;
1902 if(recode && (surface->flags & SDL_PREALLOC) != SDL_PREALLOC 1936
1903 && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) { 1937 if (recode && !(surface->flags & SDL_PREALLOC)
1904 if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) { 1938 && !(surface->flags & SDL_HWSURFACE)) {
1905 SDL_Rect full; 1939 if ((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
1906 unsigned alpha_flag; 1940 SDL_Rect full;
1907 1941 unsigned alpha_flag;
1908 /* re-create the original surface */ 1942
1909 surface->pixels = SDL_malloc(surface->h * surface->pitch); 1943 /* re-create the original surface */
1910 if ( !surface->pixels ) { 1944 surface->pixels = SDL_malloc(surface->h * surface->pitch);
1911 /* Oh crap... */ 1945 if (!surface->pixels) {
1912 surface->flags |= SDL_RLEACCEL; 1946 /* Oh crap... */
1913 return; 1947 surface->flags |= SDL_RLEACCEL;
1914 } 1948 return;
1915 1949 }
1916 /* fill it with the background colour */ 1950
1917 SDL_FillRect(surface, NULL, surface->format->colorkey); 1951 /* fill it with the background colour */
1918 1952 SDL_FillRect(surface, NULL, surface->format->colorkey);
1919 /* now render the encoded surface */ 1953
1920 full.x = full.y = 0; 1954 /* now render the encoded surface */
1921 full.w = surface->w; 1955 full.x = full.y = 0;
1922 full.h = surface->h; 1956 full.w = surface->w;
1923 alpha_flag = surface->flags & SDL_SRCALPHA; 1957 full.h = surface->h;
1924 surface->flags &= ~SDL_SRCALPHA; /* opaque blit */ 1958 alpha_flag = surface->flags & SDL_SRCALPHA;
1925 SDL_RLEBlit(surface, &full, surface, &full); 1959 surface->flags &= ~SDL_SRCALPHA; /* opaque blit */
1926 surface->flags |= alpha_flag; 1960 SDL_RLEBlit(surface, &full, surface, &full);
1927 } else { 1961 surface->flags |= alpha_flag;
1928 if ( !UnRLEAlpha(surface) ) { 1962 } else {
1929 /* Oh crap... */ 1963 if (!UnRLEAlpha(surface)) {
1930 surface->flags |= SDL_RLEACCEL; 1964 /* Oh crap... */
1931 return; 1965 surface->flags |= SDL_RLEACCEL;
1932 } 1966 return;
1933 } 1967 }
1934 } 1968 }
1935 1969 }
1936 if ( surface->map && surface->map->sw_data->aux_data ) { 1970
1937 SDL_free(surface->map->sw_data->aux_data); 1971 if (surface->map && surface->map->sw_data->aux_data) {
1938 surface->map->sw_data->aux_data = NULL; 1972 SDL_free(surface->map->sw_data->aux_data);
1939 } 1973 surface->map->sw_data->aux_data = NULL;
1974 }
1940 } 1975 }
1941 } 1976 }
1942 1977
1943 1978 /* vi: set ts=4 sw=4 expandtab: */