Mercurial > sdl-ios-xcode
comparison src/video/SDL_pixels.c @ 1662:782fd950bd46 SDL-1.3
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.
The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce
The headers are being converted to automatically generate doxygen documentation.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 28 May 2006 13:04:16 +0000 |
parents | 61c237f69076 |
children | 4da1ee79c9af |
comparison
equal
deleted
inserted
replaced
1661:281d3f4870e5 | 1662:782fd950bd46 |
---|---|
29 #include "SDL_blit.h" | 29 #include "SDL_blit.h" |
30 #include "SDL_pixels_c.h" | 30 #include "SDL_pixels_c.h" |
31 #include "SDL_RLEaccel_c.h" | 31 #include "SDL_RLEaccel_c.h" |
32 | 32 |
33 /* Helper functions */ | 33 /* Helper functions */ |
34 | |
35 SDL_bool | |
36 SDL_PixelFormatEnumToMasks (Uint32 format, int *bpp, Uint32 * Rmask, | |
37 Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask) | |
38 { | |
39 Uint32 masks[4]; | |
40 | |
41 /* Initialize the values here */ | |
42 if (SDL_BITSPERPIXEL (format) == 24) { | |
43 *bpp = SDL_BYTESPERPIXEL (format) * 8; | |
44 } else { | |
45 *bpp = SDL_BITSPERPIXEL (format); | |
46 } | |
47 *Rmask = *Gmask = *Bmask = *Amask = 0; | |
48 | |
49 if (SDL_PIXELTYPE (format) != SDL_PixelType_Packed8 && | |
50 SDL_PIXELTYPE (format) != SDL_PixelType_Packed16 && | |
51 SDL_PIXELTYPE (format) != SDL_PixelType_Packed32) { | |
52 /* Not a format that uses masks */ | |
53 return SDL_TRUE; | |
54 } | |
55 | |
56 switch (SDL_PIXELLAYOUT (format)) { | |
57 case SDL_PackedLayout_332: | |
58 masks[0] = 0x00000000; | |
59 masks[1] = 0x000000E0; | |
60 masks[2] = 0x0000001C; | |
61 masks[3] = 0x00000003; | |
62 break; | |
63 case SDL_PackedLayout_4444: | |
64 masks[0] = 0x0000F000; | |
65 masks[1] = 0x00000F00; | |
66 masks[2] = 0x000000F0; | |
67 masks[3] = 0x0000000F; | |
68 break; | |
69 case SDL_PackedLayout_1555: | |
70 masks[0] = 0x00008000; | |
71 masks[1] = 0x00007C00; | |
72 masks[2] = 0x000003E0; | |
73 masks[3] = 0x0000001F; | |
74 break; | |
75 case SDL_PackedLayout_565: | |
76 masks[0] = 0x00000000; | |
77 masks[1] = 0x0000F800; | |
78 masks[2] = 0x000007E0; | |
79 masks[3] = 0x0000001F; | |
80 break; | |
81 case SDL_PackedLayout_8888: | |
82 masks[0] = 0xFF000000; | |
83 masks[1] = 0x00FF0000; | |
84 masks[2] = 0x0000FF00; | |
85 masks[3] = 0x000000FF; | |
86 break; | |
87 case SDL_PackedLayout_2101010: | |
88 masks[0] = 0xC0000000; | |
89 masks[1] = 0x3FF00000; | |
90 masks[2] = 0x000FFC00; | |
91 masks[3] = 0x000003FF; | |
92 break; | |
93 default: | |
94 /* Unknown layout */ | |
95 return SDL_FALSE; | |
96 } | |
97 | |
98 switch (SDL_PIXELORDER (format)) { | |
99 case SDL_PackedOrder_XRGB: | |
100 *Rmask = masks[1]; | |
101 *Gmask = masks[2]; | |
102 *Bmask = masks[3]; | |
103 break; | |
104 case SDL_PackedOrder_RGBX: | |
105 *Rmask = masks[0]; | |
106 *Gmask = masks[1]; | |
107 *Bmask = masks[2]; | |
108 break; | |
109 case SDL_PackedOrder_ARGB: | |
110 *Amask = masks[0]; | |
111 *Rmask = masks[1]; | |
112 *Gmask = masks[2]; | |
113 *Bmask = masks[3]; | |
114 break; | |
115 case SDL_PackedOrder_RGBA: | |
116 *Rmask = masks[0]; | |
117 *Gmask = masks[1]; | |
118 *Bmask = masks[2]; | |
119 *Amask = masks[3]; | |
120 break; | |
121 case SDL_PackedOrder_XBGR: | |
122 *Bmask = masks[1]; | |
123 *Gmask = masks[2]; | |
124 *Rmask = masks[3]; | |
125 break; | |
126 case SDL_PackedOrder_BGRX: | |
127 *Bmask = masks[0]; | |
128 *Gmask = masks[1]; | |
129 *Rmask = masks[2]; | |
130 break; | |
131 case SDL_PackedOrder_BGRA: | |
132 *Bmask = masks[0]; | |
133 *Gmask = masks[1]; | |
134 *Rmask = masks[2]; | |
135 *Amask = masks[3]; | |
136 break; | |
137 case SDL_PackedOrder_ABGR: | |
138 *Amask = masks[0]; | |
139 *Bmask = masks[1]; | |
140 *Gmask = masks[2]; | |
141 *Rmask = masks[3]; | |
142 break; | |
143 default: | |
144 /* Unknown order */ | |
145 return SDL_FALSE; | |
146 } | |
147 return SDL_TRUE; | |
148 } | |
149 | |
150 Uint32 | |
151 SDL_MasksToPixelFormatEnum (int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, | |
152 Uint32 Amask) | |
153 { | |
154 switch (bpp) { | |
155 case 8: | |
156 switch (Rmask) { | |
157 case 0: | |
158 return SDL_PixelFormat_Index8; | |
159 case 0xE0: | |
160 return SDL_PixelFormat_RGB332; | |
161 } | |
162 break; | |
163 case 12: | |
164 switch (Rmask) { | |
165 case 0x0F00: | |
166 return SDL_PixelFormat_RGB444; | |
167 } | |
168 break; | |
169 case 15: | |
170 switch (Rmask) { | |
171 case 0x7C00: | |
172 return SDL_PixelFormat_RGB555; | |
173 } | |
174 break; | |
175 case 16: | |
176 switch (Rmask) { | |
177 case 0x0F00: | |
178 return SDL_PixelFormat_ARGB4444; | |
179 case 0x7C00: | |
180 return SDL_PixelFormat_ARGB1555; | |
181 case 0xF800: | |
182 return SDL_PixelFormat_RGB565; | |
183 } | |
184 break; | |
185 case 32: | |
186 switch (Rmask) { | |
187 case 0xFF000000: | |
188 if (Amask == 0x000000FF) { | |
189 return SDL_PixelFormat_RGBA8888; | |
190 } | |
191 break; | |
192 case 0x00FF0000: | |
193 if (Amask == 0xFF000000) { | |
194 return SDL_PixelFormat_ARGB8888; | |
195 } else { | |
196 return SDL_PixelFormat_RGB888; | |
197 } | |
198 break; | |
199 case 0x0000FF00: | |
200 if (Amask == 0x000000FF) { | |
201 return SDL_PixelFormat_BGRA8888; | |
202 } | |
203 break; | |
204 case 0x000000FF: | |
205 if (Amask == 0xFF000000) { | |
206 return SDL_PixelFormat_ABGR8888; | |
207 } else { | |
208 return SDL_PixelFormat_BGR888; | |
209 } | |
210 break; | |
211 case 0x3FF00000: | |
212 return SDL_PixelFormat_ARGB2101010; | |
213 } | |
214 } | |
215 return SDL_PixelFormat_Unknown; | |
216 } | |
217 | |
34 /* | 218 /* |
35 * Allocate a pixel format structure and fill it according to the given info. | 219 * Allocate a pixel format structure and fill it according to the given info. |
36 */ | 220 */ |
37 SDL_PixelFormat *SDL_AllocFormat(int bpp, | 221 SDL_PixelFormat * |
38 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | 222 SDL_AllocFormat (int bpp, |
39 { | 223 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) |
40 SDL_PixelFormat *format; | 224 { |
41 Uint32 mask; | 225 SDL_PixelFormat *format; |
42 | 226 Uint32 mask; |
43 /* Allocate an empty pixel format structure */ | 227 |
44 format = SDL_malloc(sizeof(*format)); | 228 /* Allocate an empty pixel format structure */ |
45 if ( format == NULL ) { | 229 format = SDL_malloc (sizeof (*format)); |
46 SDL_OutOfMemory(); | 230 if (format == NULL) { |
47 return(NULL); | 231 SDL_OutOfMemory (); |
48 } | 232 return (NULL); |
49 SDL_memset(format, 0, sizeof(*format)); | 233 } |
50 format->alpha = SDL_ALPHA_OPAQUE; | 234 SDL_memset (format, 0, sizeof (*format)); |
51 | 235 format->alpha = SDL_ALPHA_OPAQUE; |
52 /* Set up the format */ | 236 |
53 format->BitsPerPixel = bpp; | 237 /* Set up the format */ |
54 format->BytesPerPixel = (bpp+7)/8; | 238 format->BitsPerPixel = bpp; |
55 if ( Rmask || Bmask || Gmask ) { /* Packed pixels with custom mask */ | 239 format->BytesPerPixel = (bpp + 7) / 8; |
56 format->palette = NULL; | 240 if (Rmask || Bmask || Gmask) { /* Packed pixels with custom mask */ |
57 format->Rshift = 0; | 241 format->palette = NULL; |
58 format->Rloss = 8; | 242 format->Rshift = 0; |
59 if ( Rmask ) { | 243 format->Rloss = 8; |
60 for ( mask = Rmask; !(mask&0x01); mask >>= 1 ) | 244 if (Rmask) { |
61 ++format->Rshift; | 245 for (mask = Rmask; !(mask & 0x01); mask >>= 1) |
62 for ( ; (mask&0x01); mask >>= 1 ) | 246 ++format->Rshift; |
63 --format->Rloss; | 247 for (; (mask & 0x01); mask >>= 1) |
64 } | 248 --format->Rloss; |
65 format->Gshift = 0; | 249 } |
66 format->Gloss = 8; | 250 format->Gshift = 0; |
67 if ( Gmask ) { | 251 format->Gloss = 8; |
68 for ( mask = Gmask; !(mask&0x01); mask >>= 1 ) | 252 if (Gmask) { |
69 ++format->Gshift; | 253 for (mask = Gmask; !(mask & 0x01); mask >>= 1) |
70 for ( ; (mask&0x01); mask >>= 1 ) | 254 ++format->Gshift; |
71 --format->Gloss; | 255 for (; (mask & 0x01); mask >>= 1) |
72 } | 256 --format->Gloss; |
73 format->Bshift = 0; | 257 } |
74 format->Bloss = 8; | 258 format->Bshift = 0; |
75 if ( Bmask ) { | 259 format->Bloss = 8; |
76 for ( mask = Bmask; !(mask&0x01); mask >>= 1 ) | 260 if (Bmask) { |
77 ++format->Bshift; | 261 for (mask = Bmask; !(mask & 0x01); mask >>= 1) |
78 for ( ; (mask&0x01); mask >>= 1 ) | 262 ++format->Bshift; |
79 --format->Bloss; | 263 for (; (mask & 0x01); mask >>= 1) |
80 } | 264 --format->Bloss; |
81 format->Ashift = 0; | 265 } |
82 format->Aloss = 8; | 266 format->Ashift = 0; |
83 if ( Amask ) { | 267 format->Aloss = 8; |
84 for ( mask = Amask; !(mask&0x01); mask >>= 1 ) | 268 if (Amask) { |
85 ++format->Ashift; | 269 for (mask = Amask; !(mask & 0x01); mask >>= 1) |
86 for ( ; (mask&0x01); mask >>= 1 ) | 270 ++format->Ashift; |
87 --format->Aloss; | 271 for (; (mask & 0x01); mask >>= 1) |
88 } | 272 --format->Aloss; |
89 format->Rmask = Rmask; | 273 } |
90 format->Gmask = Gmask; | 274 format->Rmask = Rmask; |
91 format->Bmask = Bmask; | 275 format->Gmask = Gmask; |
92 format->Amask = Amask; | 276 format->Bmask = Bmask; |
93 } else if ( bpp > 8 ) { /* Packed pixels with standard mask */ | 277 format->Amask = Amask; |
94 /* R-G-B */ | 278 } else if (bpp > 8) { /* Packed pixels with standard mask */ |
95 if ( bpp > 24 ) | 279 /* R-G-B */ |
96 bpp = 24; | 280 if (bpp > 24) |
97 format->Rloss = 8-(bpp/3); | 281 bpp = 24; |
98 format->Gloss = 8-(bpp/3)-(bpp%3); | 282 format->Rloss = 8 - (bpp / 3); |
99 format->Bloss = 8-(bpp/3); | 283 format->Gloss = 8 - (bpp / 3) - (bpp % 3); |
100 format->Rshift = ((bpp/3)+(bpp%3))+(bpp/3); | 284 format->Bloss = 8 - (bpp / 3); |
101 format->Gshift = (bpp/3); | 285 format->Rshift = ((bpp / 3) + (bpp % 3)) + (bpp / 3); |
102 format->Bshift = 0; | 286 format->Gshift = (bpp / 3); |
103 format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift); | 287 format->Bshift = 0; |
104 format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift); | 288 format->Rmask = ((0xFF >> format->Rloss) << format->Rshift); |
105 format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift); | 289 format->Gmask = ((0xFF >> format->Gloss) << format->Gshift); |
106 } else { | 290 format->Bmask = ((0xFF >> format->Bloss) << format->Bshift); |
107 /* Palettized formats have no mask info */ | 291 } else { |
108 format->Rloss = 8; | 292 /* Palettized formats have no mask info */ |
109 format->Gloss = 8; | 293 format->Rloss = 8; |
110 format->Bloss = 8; | 294 format->Gloss = 8; |
111 format->Aloss = 8; | 295 format->Bloss = 8; |
112 format->Rshift = 0; | 296 format->Aloss = 8; |
113 format->Gshift = 0; | 297 format->Rshift = 0; |
114 format->Bshift = 0; | 298 format->Gshift = 0; |
115 format->Ashift = 0; | 299 format->Bshift = 0; |
116 format->Rmask = 0; | 300 format->Ashift = 0; |
117 format->Gmask = 0; | 301 format->Rmask = 0; |
118 format->Bmask = 0; | 302 format->Gmask = 0; |
119 format->Amask = 0; | 303 format->Bmask = 0; |
120 } | 304 format->Amask = 0; |
121 if ( bpp <= 8 ) { /* Palettized mode */ | 305 } |
122 int ncolors = 1<<bpp; | 306 if (bpp <= 8) { /* Palettized mode */ |
307 int ncolors = 1 << bpp; | |
123 #ifdef DEBUG_PALETTE | 308 #ifdef DEBUG_PALETTE |
124 fprintf(stderr,"bpp=%d ncolors=%d\n",bpp,ncolors); | 309 fprintf (stderr, "bpp=%d ncolors=%d\n", bpp, ncolors); |
125 #endif | 310 #endif |
126 format->palette = (SDL_Palette *)SDL_malloc(sizeof(SDL_Palette)); | 311 format->palette = (SDL_Palette *) SDL_malloc (sizeof (SDL_Palette)); |
127 if ( format->palette == NULL ) { | 312 if (format->palette == NULL) { |
128 SDL_FreeFormat(format); | 313 SDL_FreeFormat (format); |
129 SDL_OutOfMemory(); | 314 SDL_OutOfMemory (); |
130 return(NULL); | 315 return (NULL); |
131 } | 316 } |
132 (format->palette)->ncolors = ncolors; | 317 (format->palette)->ncolors = ncolors; |
133 (format->palette)->colors = (SDL_Color *)SDL_malloc( | 318 (format->palette)->colors = (SDL_Color *) SDL_malloc ((format-> |
134 (format->palette)->ncolors*sizeof(SDL_Color)); | 319 palette)-> |
135 if ( (format->palette)->colors == NULL ) { | 320 ncolors * |
136 SDL_FreeFormat(format); | 321 sizeof |
137 SDL_OutOfMemory(); | 322 (SDL_Color)); |
138 return(NULL); | 323 if ((format->palette)->colors == NULL) { |
139 } | 324 SDL_FreeFormat (format); |
140 if ( Rmask || Bmask || Gmask ) { | 325 SDL_OutOfMemory (); |
141 /* create palette according to masks */ | 326 return (NULL); |
142 int i; | 327 } |
143 int Rm=0,Gm=0,Bm=0; | 328 if (Rmask || Bmask || Gmask) { |
144 int Rw=0,Gw=0,Bw=0; | 329 /* create palette according to masks */ |
330 int i; | |
331 int Rm = 0, Gm = 0, Bm = 0; | |
332 int Rw = 0, Gw = 0, Bw = 0; | |
145 #ifdef ENABLE_PALETTE_ALPHA | 333 #ifdef ENABLE_PALETTE_ALPHA |
146 int Am=0,Aw=0; | 334 int Am = 0, Aw = 0; |
147 #endif | 335 #endif |
148 if(Rmask) | 336 if (Rmask) { |
149 { | 337 Rw = 8 - format->Rloss; |
150 Rw=8-format->Rloss; | 338 for (i = format->Rloss; i > 0; i -= Rw) |
151 for(i=format->Rloss;i>0;i-=Rw) | 339 Rm |= 1 << i; |
152 Rm|=1<<i; | 340 } |
153 } | |
154 #ifdef DEBUG_PALETTE | 341 #ifdef DEBUG_PALETTE |
155 fprintf(stderr,"Rw=%d Rm=0x%02X\n",Rw,Rm); | 342 fprintf (stderr, "Rw=%d Rm=0x%02X\n", Rw, Rm); |
156 #endif | 343 #endif |
157 if(Gmask) | 344 if (Gmask) { |
158 { | 345 Gw = 8 - format->Gloss; |
159 Gw=8-format->Gloss; | 346 for (i = format->Gloss; i > 0; i -= Gw) |
160 for(i=format->Gloss;i>0;i-=Gw) | 347 Gm |= 1 << i; |
161 Gm|=1<<i; | 348 } |
162 } | |
163 #ifdef DEBUG_PALETTE | 349 #ifdef DEBUG_PALETTE |
164 fprintf(stderr,"Gw=%d Gm=0x%02X\n",Gw,Gm); | 350 fprintf (stderr, "Gw=%d Gm=0x%02X\n", Gw, Gm); |
165 #endif | 351 #endif |
166 if(Bmask) | 352 if (Bmask) { |
167 { | 353 Bw = 8 - format->Bloss; |
168 Bw=8-format->Bloss; | 354 for (i = format->Bloss; i > 0; i -= Bw) |
169 for(i=format->Bloss;i>0;i-=Bw) | 355 Bm |= 1 << i; |
170 Bm|=1<<i; | 356 } |
171 } | |
172 #ifdef DEBUG_PALETTE | 357 #ifdef DEBUG_PALETTE |
173 fprintf(stderr,"Bw=%d Bm=0x%02X\n",Bw,Bm); | 358 fprintf (stderr, "Bw=%d Bm=0x%02X\n", Bw, Bm); |
174 #endif | 359 #endif |
175 #ifdef ENABLE_PALETTE_ALPHA | 360 #ifdef ENABLE_PALETTE_ALPHA |
176 if(Amask) | 361 if (Amask) { |
177 { | 362 Aw = 8 - format->Aloss; |
178 Aw=8-format->Aloss; | 363 for (i = format->Aloss; i > 0; i -= Aw) |
179 for(i=format->Aloss;i>0;i-=Aw) | 364 Am |= 1 << i; |
180 Am|=1<<i; | 365 } |
181 } | |
182 # ifdef DEBUG_PALETTE | 366 # ifdef DEBUG_PALETTE |
183 fprintf(stderr,"Aw=%d Am=0x%02X\n",Aw,Am); | 367 fprintf (stderr, "Aw=%d Am=0x%02X\n", Aw, Am); |
184 # endif | 368 # endif |
185 #endif | 369 #endif |
186 for(i=0; i < ncolors; ++i) { | 370 for (i = 0; i < ncolors; ++i) { |
187 int r,g,b; | 371 int r, g, b; |
188 r=(i&Rmask)>>format->Rshift; | 372 r = (i & Rmask) >> format->Rshift; |
189 r=(r<<format->Rloss)|((r*Rm)>>Rw); | 373 r = (r << format->Rloss) | ((r * Rm) >> Rw); |
190 format->palette->colors[i].r=r; | 374 format->palette->colors[i].r = r; |
191 | 375 |
192 g=(i&Gmask)>>format->Gshift; | 376 g = (i & Gmask) >> format->Gshift; |
193 g=(g<<format->Gloss)|((g*Gm)>>Gw); | 377 g = (g << format->Gloss) | ((g * Gm) >> Gw); |
194 format->palette->colors[i].g=g; | 378 format->palette->colors[i].g = g; |
195 | 379 |
196 b=(i&Bmask)>>format->Bshift; | 380 b = (i & Bmask) >> format->Bshift; |
197 b=(b<<format->Bloss)|((b*Bm)>>Bw); | 381 b = (b << format->Bloss) | ((b * Bm) >> Bw); |
198 format->palette->colors[i].b=b; | 382 format->palette->colors[i].b = b; |
199 | 383 |
200 #ifdef ENABLE_PALETTE_ALPHA | 384 #ifdef ENABLE_PALETTE_ALPHA |
201 a=(i&Amask)>>format->Ashift; | 385 a = (i & Amask) >> format->Ashift; |
202 a=(a<<format->Aloss)|((a*Am)>>Aw); | 386 a = (a << format->Aloss) | ((a * Am) >> Aw); |
203 format->palette->colors[i].unused=a; | 387 format->palette->colors[i].unused = a; |
204 #else | 388 #else |
205 format->palette->colors[i].unused=0; | 389 format->palette->colors[i].unused = 0; |
206 #endif | 390 #endif |
207 } | 391 } |
208 } else if ( ncolors == 2 ) { | 392 } else if (ncolors == 2) { |
209 /* Create a black and white bitmap palette */ | 393 /* Create a black and white bitmap palette */ |
210 format->palette->colors[0].r = 0xFF; | 394 format->palette->colors[0].r = 0xFF; |
211 format->palette->colors[0].g = 0xFF; | 395 format->palette->colors[0].g = 0xFF; |
212 format->palette->colors[0].b = 0xFF; | 396 format->palette->colors[0].b = 0xFF; |
213 format->palette->colors[1].r = 0x00; | 397 format->palette->colors[1].r = 0x00; |
214 format->palette->colors[1].g = 0x00; | 398 format->palette->colors[1].g = 0x00; |
215 format->palette->colors[1].b = 0x00; | 399 format->palette->colors[1].b = 0x00; |
216 } else { | 400 } else { |
217 /* Create an empty palette */ | 401 /* Create an empty palette */ |
218 SDL_memset((format->palette)->colors, 0, | 402 SDL_memset ((format->palette)->colors, 0, |
219 (format->palette)->ncolors*sizeof(SDL_Color)); | 403 (format->palette)->ncolors * sizeof (SDL_Color)); |
220 } | 404 } |
221 } | 405 } |
222 return(format); | 406 return (format); |
223 } | 407 } |
224 SDL_PixelFormat *SDL_ReallocFormat(SDL_Surface *surface, int bpp, | 408 |
225 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | 409 SDL_PixelFormat * |
226 { | 410 SDL_ReallocFormat (SDL_Surface * surface, int bpp, |
227 if ( surface->format ) { | 411 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) |
228 SDL_FreeFormat(surface->format); | 412 { |
229 SDL_FormatChanged(surface); | 413 if (surface->format) { |
230 } | 414 SDL_FreeFormat (surface->format); |
231 surface->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask); | 415 SDL_FormatChanged (surface); |
232 return surface->format; | 416 } |
417 surface->format = SDL_AllocFormat (bpp, Rmask, Gmask, Bmask, Amask); | |
418 return surface->format; | |
233 } | 419 } |
234 | 420 |
235 /* | 421 /* |
236 * Change any previous mappings from/to the new surface format | 422 * Change any previous mappings from/to the new surface format |
237 */ | 423 */ |
238 void SDL_FormatChanged(SDL_Surface *surface) | 424 void |
239 { | 425 SDL_FormatChanged (SDL_Surface * surface) |
240 static int format_version = 0; | 426 { |
241 ++format_version; | 427 static int format_version = 0; |
242 if ( format_version < 0 ) { /* It wrapped... */ | 428 ++format_version; |
243 format_version = 1; | 429 if (format_version < 0) { /* It wrapped... */ |
244 } | 430 format_version = 1; |
245 surface->format_version = format_version; | 431 } |
246 SDL_InvalidateMap(surface->map); | 432 surface->format_version = format_version; |
247 } | 433 SDL_InvalidateMap (surface->map); |
434 } | |
435 | |
248 /* | 436 /* |
249 * Free a previously allocated format structure | 437 * Free a previously allocated format structure |
250 */ | 438 */ |
251 void SDL_FreeFormat(SDL_PixelFormat *format) | 439 void |
252 { | 440 SDL_FreeFormat (SDL_PixelFormat * format) |
253 if ( format ) { | 441 { |
254 if ( format->palette ) { | 442 if (format) { |
255 if ( format->palette->colors ) { | 443 if (format->palette) { |
256 SDL_free(format->palette->colors); | 444 if (format->palette->colors) { |
257 } | 445 SDL_free (format->palette->colors); |
258 SDL_free(format->palette); | 446 } |
259 } | 447 SDL_free (format->palette); |
260 SDL_free(format); | 448 } |
261 } | 449 SDL_free (format); |
262 } | 450 } |
451 } | |
452 | |
263 /* | 453 /* |
264 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors | 454 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors |
265 */ | 455 */ |
266 void SDL_DitherColors(SDL_Color *colors, int bpp) | 456 void |
267 { | 457 SDL_DitherColors (SDL_Color * colors, int bpp) |
268 int i; | 458 { |
269 if(bpp != 8) | 459 int i; |
270 return; /* only 8bpp supported right now */ | 460 if (bpp != 8) |
271 | 461 return; /* only 8bpp supported right now */ |
272 for(i = 0; i < 256; i++) { | 462 |
273 int r, g, b; | 463 for (i = 0; i < 256; i++) { |
274 /* map each bit field to the full [0, 255] interval, | 464 int r, g, b; |
275 so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */ | 465 /* map each bit field to the full [0, 255] interval, |
276 r = i & 0xe0; | 466 so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */ |
277 r |= r >> 3 | r >> 6; | 467 r = i & 0xe0; |
278 colors[i].r = r; | 468 r |= r >> 3 | r >> 6; |
279 g = (i << 3) & 0xe0; | 469 colors[i].r = r; |
280 g |= g >> 3 | g >> 6; | 470 g = (i << 3) & 0xe0; |
281 colors[i].g = g; | 471 g |= g >> 3 | g >> 6; |
282 b = i & 0x3; | 472 colors[i].g = g; |
283 b |= b << 2; | 473 b = i & 0x3; |
284 b |= b << 4; | 474 b |= b << 2; |
285 colors[i].b = b; | 475 b |= b << 4; |
286 } | 476 colors[i].b = b; |
287 } | 477 } |
478 } | |
479 | |
288 /* | 480 /* |
289 * Calculate the pad-aligned scanline width of a surface | 481 * Calculate the pad-aligned scanline width of a surface |
290 */ | 482 */ |
291 Uint16 SDL_CalculatePitch(SDL_Surface *surface) | 483 Uint16 |
292 { | 484 SDL_CalculatePitch (SDL_Surface * surface) |
293 Uint16 pitch; | 485 { |
294 | 486 Uint16 pitch; |
295 /* Surface should be 4-byte aligned for speed */ | 487 |
296 pitch = surface->w*surface->format->BytesPerPixel; | 488 /* Surface should be 4-byte aligned for speed */ |
297 switch (surface->format->BitsPerPixel) { | 489 pitch = surface->w * surface->format->BytesPerPixel; |
298 case 1: | 490 switch (surface->format->BitsPerPixel) { |
299 pitch = (pitch+7)/8; | 491 case 1: |
300 break; | 492 pitch = (pitch + 7) / 8; |
301 case 4: | 493 break; |
302 pitch = (pitch+1)/2; | 494 case 4: |
303 break; | 495 pitch = (pitch + 1) / 2; |
304 default: | 496 break; |
305 break; | 497 default: |
306 } | 498 break; |
307 pitch = (pitch + 3) & ~3; /* 4-byte aligning */ | 499 } |
308 return(pitch); | 500 pitch = (pitch + 3) & ~3; /* 4-byte aligning */ |
309 } | 501 return (pitch); |
502 } | |
503 | |
310 /* | 504 /* |
311 * Match an RGB value to a particular palette index | 505 * Match an RGB value to a particular palette index |
312 */ | 506 */ |
313 Uint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b) | 507 Uint8 |
314 { | 508 SDL_FindColor (SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b) |
315 /* Do colorspace distance matching */ | 509 { |
316 unsigned int smallest; | 510 /* Do colorspace distance matching */ |
317 unsigned int distance; | 511 unsigned int smallest; |
318 int rd, gd, bd; | 512 unsigned int distance; |
319 int i; | 513 int rd, gd, bd; |
320 Uint8 pixel=0; | 514 int i; |
321 | 515 Uint8 pixel = 0; |
322 smallest = ~0; | 516 |
323 for ( i=0; i<pal->ncolors; ++i ) { | 517 smallest = ~0; |
324 rd = pal->colors[i].r - r; | 518 for (i = 0; i < pal->ncolors; ++i) { |
325 gd = pal->colors[i].g - g; | 519 rd = pal->colors[i].r - r; |
326 bd = pal->colors[i].b - b; | 520 gd = pal->colors[i].g - g; |
327 distance = (rd*rd)+(gd*gd)+(bd*bd); | 521 bd = pal->colors[i].b - b; |
328 if ( distance < smallest ) { | 522 distance = (rd * rd) + (gd * gd) + (bd * bd); |
329 pixel = i; | 523 if (distance < smallest) { |
330 if ( distance == 0 ) { /* Perfect match! */ | 524 pixel = i; |
331 break; | 525 if (distance == 0) { /* Perfect match! */ |
332 } | 526 break; |
333 smallest = distance; | 527 } |
334 } | 528 smallest = distance; |
335 } | 529 } |
336 return(pixel); | 530 } |
531 return (pixel); | |
337 } | 532 } |
338 | 533 |
339 /* Find the opaque pixel value corresponding to an RGB triple */ | 534 /* Find the opaque pixel value corresponding to an RGB triple */ |
340 Uint32 SDL_MapRGB(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b) | 535 Uint32 |
341 { | 536 SDL_MapRGB (SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b) |
342 if ( format->palette == NULL ) { | 537 { |
343 return (r >> format->Rloss) << format->Rshift | 538 if (format->palette == NULL) { |
344 | (g >> format->Gloss) << format->Gshift | 539 return (r >> format->Rloss) << format->Rshift |
345 | (b >> format->Bloss) << format->Bshift | 540 | (g >> format->Gloss) << format->Gshift |
346 | format->Amask; | 541 | (b >> format->Bloss) << format->Bshift | format->Amask; |
347 } else { | 542 } else { |
348 return SDL_FindColor(format->palette, r, g, b); | 543 return SDL_FindColor (format->palette, r, g, b); |
349 } | 544 } |
350 } | 545 } |
351 | 546 |
352 /* Find the pixel value corresponding to an RGBA quadruple */ | 547 /* Find the pixel value corresponding to an RGBA quadruple */ |
353 Uint32 SDL_MapRGBA(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b, Uint8 a) | 548 Uint32 |
354 { | 549 SDL_MapRGBA (SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b, Uint8 a) |
355 if ( format->palette == NULL ) { | 550 { |
356 return (r >> format->Rloss) << format->Rshift | 551 if (format->palette == NULL) { |
357 | (g >> format->Gloss) << format->Gshift | 552 return (r >> format->Rloss) << format->Rshift |
358 | (b >> format->Bloss) << format->Bshift | 553 | (g >> format->Gloss) << format->Gshift |
359 | ((a >> format->Aloss) << format->Ashift & format->Amask); | 554 | (b >> format->Bloss) << format->Bshift |
360 } else { | 555 | ((a >> format->Aloss) << format->Ashift & format->Amask); |
361 return SDL_FindColor(format->palette, r, g, b); | 556 } else { |
362 } | 557 return SDL_FindColor (format->palette, r, g, b); |
363 } | 558 } |
364 | 559 } |
365 void SDL_GetRGBA(Uint32 pixel, SDL_PixelFormat *fmt, | 560 |
366 Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) | 561 void |
367 { | 562 SDL_GetRGBA (Uint32 pixel, SDL_PixelFormat * fmt, |
368 if ( fmt->palette == NULL ) { | 563 Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a) |
369 /* | 564 { |
370 * This makes sure that the result is mapped to the | 565 if (fmt->palette == NULL) { |
371 * interval [0..255], and the maximum value for each | 566 /* |
372 * component is 255. This is important to make sure | 567 * This makes sure that the result is mapped to the |
373 * that white is indeed reported as (255, 255, 255), | 568 * interval [0..255], and the maximum value for each |
374 * and that opaque alpha is 255. | 569 * component is 255. This is important to make sure |
375 * This only works for RGB bit fields at least 4 bit | 570 * that white is indeed reported as (255, 255, 255), |
376 * wide, which is almost always the case. | 571 * and that opaque alpha is 255. |
377 */ | 572 * This only works for RGB bit fields at least 4 bit |
378 unsigned v; | 573 * wide, which is almost always the case. |
379 v = (pixel & fmt->Rmask) >> fmt->Rshift; | 574 */ |
380 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1))); | 575 unsigned v; |
381 v = (pixel & fmt->Gmask) >> fmt->Gshift; | 576 v = (pixel & fmt->Rmask) >> fmt->Rshift; |
382 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1))); | 577 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1))); |
383 v = (pixel & fmt->Bmask) >> fmt->Bshift; | 578 v = (pixel & fmt->Gmask) >> fmt->Gshift; |
384 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1))); | 579 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1))); |
385 if(fmt->Amask) { | 580 v = (pixel & fmt->Bmask) >> fmt->Bshift; |
386 v = (pixel & fmt->Amask) >> fmt->Ashift; | 581 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1))); |
387 *a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1))); | 582 if (fmt->Amask) { |
388 } else { | 583 v = (pixel & fmt->Amask) >> fmt->Ashift; |
389 *a = SDL_ALPHA_OPAQUE; | 584 *a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1))); |
585 } else { | |
586 *a = SDL_ALPHA_OPAQUE; | |
587 } | |
588 } else { | |
589 *r = fmt->palette->colors[pixel].r; | |
590 *g = fmt->palette->colors[pixel].g; | |
591 *b = fmt->palette->colors[pixel].b; | |
592 *a = SDL_ALPHA_OPAQUE; | |
593 } | |
594 } | |
595 | |
596 void | |
597 SDL_GetRGB (Uint32 pixel, SDL_PixelFormat * fmt, Uint8 * r, Uint8 * g, | |
598 Uint8 * b) | |
599 { | |
600 if (fmt->palette == NULL) { | |
601 /* the note for SDL_GetRGBA above applies here too */ | |
602 unsigned v; | |
603 v = (pixel & fmt->Rmask) >> fmt->Rshift; | |
604 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1))); | |
605 v = (pixel & fmt->Gmask) >> fmt->Gshift; | |
606 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1))); | |
607 v = (pixel & fmt->Bmask) >> fmt->Bshift; | |
608 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1))); | |
609 } else { | |
610 *r = fmt->palette->colors[pixel].r; | |
611 *g = fmt->palette->colors[pixel].g; | |
612 *b = fmt->palette->colors[pixel].b; | |
613 } | |
614 } | |
615 | |
616 /* Apply gamma to a set of colors - this is easy. :) */ | |
617 void | |
618 SDL_ApplyGamma (Uint16 * gamma, SDL_Color * colors, SDL_Color * output, | |
619 int ncolors) | |
620 { | |
621 int i; | |
622 | |
623 for (i = 0; i < ncolors; ++i) { | |
624 output[i].r = gamma[0 * 256 + colors[i].r] >> 8; | |
625 output[i].g = gamma[1 * 256 + colors[i].g] >> 8; | |
626 output[i].b = gamma[2 * 256 + colors[i].b] >> 8; | |
627 } | |
628 } | |
629 | |
630 /* Map from Palette to Palette */ | |
631 static Uint8 * | |
632 Map1to1 (SDL_Palette * src, SDL_Palette * dst, int *identical) | |
633 { | |
634 Uint8 *map; | |
635 int i; | |
636 | |
637 if (identical) { | |
638 if (src->ncolors <= dst->ncolors) { | |
639 /* If an identical palette, no need to map */ | |
640 if (SDL_memcmp (src->colors, dst->colors, src->ncolors * | |
641 sizeof (SDL_Color)) == 0) { | |
642 *identical = 1; | |
643 return (NULL); | |
644 } | |
645 } | |
646 *identical = 0; | |
647 } | |
648 map = (Uint8 *) SDL_malloc (src->ncolors); | |
649 if (map == NULL) { | |
650 SDL_OutOfMemory (); | |
651 return (NULL); | |
652 } | |
653 for (i = 0; i < src->ncolors; ++i) { | |
654 map[i] = SDL_FindColor (dst, | |
655 src->colors[i].r, src->colors[i].g, | |
656 src->colors[i].b); | |
657 } | |
658 return (map); | |
659 } | |
660 | |
661 /* Map from Palette to BitField */ | |
662 static Uint8 * | |
663 Map1toN (SDL_PixelFormat * src, SDL_PixelFormat * dst) | |
664 { | |
665 Uint8 *map; | |
666 int i; | |
667 int bpp; | |
668 unsigned alpha; | |
669 SDL_Palette *pal = src->palette; | |
670 | |
671 bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel); | |
672 map = (Uint8 *) SDL_malloc (pal->ncolors * bpp); | |
673 if (map == NULL) { | |
674 SDL_OutOfMemory (); | |
675 return (NULL); | |
676 } | |
677 | |
678 alpha = dst->Amask ? src->alpha : 0; | |
679 /* We memory copy to the pixel map so the endianness is preserved */ | |
680 for (i = 0; i < pal->ncolors; ++i) { | |
681 ASSEMBLE_RGBA (&map[i * bpp], dst->BytesPerPixel, dst, | |
682 pal->colors[i].r, pal->colors[i].g, | |
683 pal->colors[i].b, alpha); | |
684 } | |
685 return (map); | |
686 } | |
687 | |
688 /* Map from BitField to Dithered-Palette to Palette */ | |
689 static Uint8 * | |
690 MapNto1 (SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical) | |
691 { | |
692 /* Generate a 256 color dither palette */ | |
693 SDL_Palette dithered; | |
694 SDL_Color colors[256]; | |
695 SDL_Palette *pal = dst->palette; | |
696 | |
697 /* SDL_DitherColors does not initialize the 'unused' component of colors, | |
698 but Map1to1 compares it against pal, so we should initialize it. */ | |
699 SDL_memset (colors, 0, sizeof (colors)); | |
700 | |
701 dithered.ncolors = 256; | |
702 SDL_DitherColors (colors, 8); | |
703 dithered.colors = colors; | |
704 return (Map1to1 (&dithered, pal, identical)); | |
705 } | |
706 | |
707 SDL_BlitMap * | |
708 SDL_AllocBlitMap (void) | |
709 { | |
710 SDL_BlitMap *map; | |
711 | |
712 /* Allocate the empty map */ | |
713 map = (SDL_BlitMap *) SDL_malloc (sizeof (*map)); | |
714 if (map == NULL) { | |
715 SDL_OutOfMemory (); | |
716 return (NULL); | |
717 } | |
718 SDL_memset (map, 0, sizeof (*map)); | |
719 | |
720 /* Allocate the software blit data */ | |
721 map->sw_data = | |
722 (struct private_swaccel *) SDL_malloc (sizeof (*map->sw_data)); | |
723 if (map->sw_data == NULL) { | |
724 SDL_FreeBlitMap (map); | |
725 SDL_OutOfMemory (); | |
726 return (NULL); | |
727 } | |
728 SDL_memset (map->sw_data, 0, sizeof (*map->sw_data)); | |
729 | |
730 /* It's ready to go */ | |
731 return (map); | |
732 } | |
733 | |
734 void | |
735 SDL_InvalidateMap (SDL_BlitMap * map) | |
736 { | |
737 if (!map) { | |
738 return; | |
739 } | |
740 map->dst = NULL; | |
741 map->format_version = (unsigned int) -1; | |
742 if (map->table) { | |
743 SDL_free (map->table); | |
744 map->table = NULL; | |
745 } | |
746 } | |
747 int | |
748 SDL_MapSurface (SDL_Surface * src, SDL_Surface * dst) | |
749 { | |
750 SDL_PixelFormat *srcfmt; | |
751 SDL_PixelFormat *dstfmt; | |
752 SDL_BlitMap *map; | |
753 | |
754 /* Clear out any previous mapping */ | |
755 map = src->map; | |
756 if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) { | |
757 SDL_UnRLESurface (src, 1); | |
758 } | |
759 SDL_InvalidateMap (map); | |
760 | |
761 /* Figure out what kind of mapping we're doing */ | |
762 map->identity = 0; | |
763 srcfmt = src->format; | |
764 dstfmt = dst->format; | |
765 switch (srcfmt->BytesPerPixel) { | |
766 case 1: | |
767 switch (dstfmt->BytesPerPixel) { | |
768 case 1: | |
769 /* Palette --> Palette */ | |
770 /* If both SDL_HWSURFACE, assume have same palette */ | |
771 if (((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) && | |
772 ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE)) { | |
773 map->identity = 1; | |
774 } else { | |
775 map->table = Map1to1 (srcfmt->palette, | |
776 dstfmt->palette, &map->identity); | |
777 } | |
778 if (!map->identity) { | |
779 if (map->table == NULL) { | |
780 return (-1); | |
390 } | 781 } |
391 } else { | 782 } |
392 *r = fmt->palette->colors[pixel].r; | 783 if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel) |
393 *g = fmt->palette->colors[pixel].g; | 784 map->identity = 0; |
394 *b = fmt->palette->colors[pixel].b; | 785 break; |
395 *a = SDL_ALPHA_OPAQUE; | 786 |
396 } | 787 default: |
397 } | 788 /* Palette --> BitField */ |
398 | 789 map->table = Map1toN (srcfmt, dstfmt); |
399 void SDL_GetRGB(Uint32 pixel, SDL_PixelFormat *fmt, Uint8 *r,Uint8 *g,Uint8 *b) | 790 if (map->table == NULL) { |
400 { | 791 return (-1); |
401 if ( fmt->palette == NULL ) { | 792 } |
402 /* the note for SDL_GetRGBA above applies here too */ | 793 break; |
403 unsigned v; | 794 } |
404 v = (pixel & fmt->Rmask) >> fmt->Rshift; | 795 break; |
405 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1))); | 796 default: |
406 v = (pixel & fmt->Gmask) >> fmt->Gshift; | 797 switch (dstfmt->BytesPerPixel) { |
407 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1))); | 798 case 1: |
408 v = (pixel & fmt->Bmask) >> fmt->Bshift; | 799 /* BitField --> Palette */ |
409 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1))); | 800 map->table = MapNto1 (srcfmt, dstfmt, &map->identity); |
410 } else { | 801 if (!map->identity) { |
411 *r = fmt->palette->colors[pixel].r; | 802 if (map->table == NULL) { |
412 *g = fmt->palette->colors[pixel].g; | 803 return (-1); |
413 *b = fmt->palette->colors[pixel].b; | 804 } |
414 } | 805 } |
415 } | 806 map->identity = 0; /* Don't optimize to copy */ |
416 | 807 break; |
417 /* Apply gamma to a set of colors - this is easy. :) */ | 808 default: |
418 void SDL_ApplyGamma(Uint16 *gamma, SDL_Color *colors, SDL_Color *output, | 809 /* BitField --> BitField */ |
419 int ncolors) | 810 if (FORMAT_EQUAL (srcfmt, dstfmt)) |
420 { | 811 map->identity = 1; |
421 int i; | 812 break; |
422 | 813 } |
423 for ( i=0; i<ncolors; ++i ) { | 814 break; |
424 output[i].r = gamma[0*256 + colors[i].r] >> 8; | 815 } |
425 output[i].g = gamma[1*256 + colors[i].g] >> 8; | 816 |
426 output[i].b = gamma[2*256 + colors[i].b] >> 8; | 817 map->dst = dst; |
427 } | 818 map->format_version = dst->format_version; |
428 } | 819 |
429 | 820 /* Choose your blitters wisely */ |
430 /* Map from Palette to Palette */ | 821 return (SDL_CalculateBlit (src)); |
431 static Uint8 *Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical) | 822 } |
432 { | 823 |
433 Uint8 *map; | 824 void |
434 int i; | 825 SDL_FreeBlitMap (SDL_BlitMap * map) |
435 | 826 { |
436 if ( identical ) { | 827 if (map) { |
437 if ( src->ncolors <= dst->ncolors ) { | 828 SDL_InvalidateMap (map); |
438 /* If an identical palette, no need to map */ | 829 if (map->sw_data != NULL) { |
439 if ( SDL_memcmp(src->colors, dst->colors, src->ncolors* | 830 SDL_free (map->sw_data); |
440 sizeof(SDL_Color)) == 0 ) { | 831 } |
441 *identical = 1; | 832 SDL_free (map); |
442 return(NULL); | 833 } |
443 } | 834 } |
444 } | 835 |
445 *identical = 0; | 836 /* vi: set ts=4 sw=4 expandtab: */ |
446 } | |
447 map = (Uint8 *)SDL_malloc(src->ncolors); | |
448 if ( map == NULL ) { | |
449 SDL_OutOfMemory(); | |
450 return(NULL); | |
451 } | |
452 for ( i=0; i<src->ncolors; ++i ) { | |
453 map[i] = SDL_FindColor(dst, | |
454 src->colors[i].r, src->colors[i].g, src->colors[i].b); | |
455 } | |
456 return(map); | |
457 } | |
458 /* Map from Palette to BitField */ | |
459 static Uint8 *Map1toN(SDL_PixelFormat *src, SDL_PixelFormat *dst) | |
460 { | |
461 Uint8 *map; | |
462 int i; | |
463 int bpp; | |
464 unsigned alpha; | |
465 SDL_Palette *pal = src->palette; | |
466 | |
467 bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel); | |
468 map = (Uint8 *)SDL_malloc(pal->ncolors*bpp); | |
469 if ( map == NULL ) { | |
470 SDL_OutOfMemory(); | |
471 return(NULL); | |
472 } | |
473 | |
474 alpha = dst->Amask ? src->alpha : 0; | |
475 /* We memory copy to the pixel map so the endianness is preserved */ | |
476 for ( i=0; i<pal->ncolors; ++i ) { | |
477 ASSEMBLE_RGBA(&map[i*bpp], dst->BytesPerPixel, dst, | |
478 pal->colors[i].r, pal->colors[i].g, | |
479 pal->colors[i].b, alpha); | |
480 } | |
481 return(map); | |
482 } | |
483 /* Map from BitField to Dithered-Palette to Palette */ | |
484 static Uint8 *MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical) | |
485 { | |
486 /* Generate a 256 color dither palette */ | |
487 SDL_Palette dithered; | |
488 SDL_Color colors[256]; | |
489 SDL_Palette *pal = dst->palette; | |
490 | |
491 /* SDL_DitherColors does not initialize the 'unused' component of colors, | |
492 but Map1to1 compares it against pal, so we should initialize it. */ | |
493 SDL_memset(colors, 0, sizeof(colors)); | |
494 | |
495 dithered.ncolors = 256; | |
496 SDL_DitherColors(colors, 8); | |
497 dithered.colors = colors; | |
498 return(Map1to1(&dithered, pal, identical)); | |
499 } | |
500 | |
501 SDL_BlitMap *SDL_AllocBlitMap(void) | |
502 { | |
503 SDL_BlitMap *map; | |
504 | |
505 /* Allocate the empty map */ | |
506 map = (SDL_BlitMap *)SDL_malloc(sizeof(*map)); | |
507 if ( map == NULL ) { | |
508 SDL_OutOfMemory(); | |
509 return(NULL); | |
510 } | |
511 SDL_memset(map, 0, sizeof(*map)); | |
512 | |
513 /* Allocate the software blit data */ | |
514 map->sw_data = (struct private_swaccel *)SDL_malloc(sizeof(*map->sw_data)); | |
515 if ( map->sw_data == NULL ) { | |
516 SDL_FreeBlitMap(map); | |
517 SDL_OutOfMemory(); | |
518 return(NULL); | |
519 } | |
520 SDL_memset(map->sw_data, 0, sizeof(*map->sw_data)); | |
521 | |
522 /* It's ready to go */ | |
523 return(map); | |
524 } | |
525 void SDL_InvalidateMap(SDL_BlitMap *map) | |
526 { | |
527 if ( ! map ) { | |
528 return; | |
529 } | |
530 map->dst = NULL; | |
531 map->format_version = (unsigned int)-1; | |
532 if ( map->table ) { | |
533 SDL_free(map->table); | |
534 map->table = NULL; | |
535 } | |
536 } | |
537 int SDL_MapSurface (SDL_Surface *src, SDL_Surface *dst) | |
538 { | |
539 SDL_PixelFormat *srcfmt; | |
540 SDL_PixelFormat *dstfmt; | |
541 SDL_BlitMap *map; | |
542 | |
543 /* Clear out any previous mapping */ | |
544 map = src->map; | |
545 if ( (src->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { | |
546 SDL_UnRLESurface(src, 1); | |
547 } | |
548 SDL_InvalidateMap(map); | |
549 | |
550 /* Figure out what kind of mapping we're doing */ | |
551 map->identity = 0; | |
552 srcfmt = src->format; | |
553 dstfmt = dst->format; | |
554 switch (srcfmt->BytesPerPixel) { | |
555 case 1: | |
556 switch (dstfmt->BytesPerPixel) { | |
557 case 1: | |
558 /* Palette --> Palette */ | |
559 /* If both SDL_HWSURFACE, assume have same palette */ | |
560 if ( ((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) && | |
561 ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) ) { | |
562 map->identity = 1; | |
563 } else { | |
564 map->table = Map1to1(srcfmt->palette, | |
565 dstfmt->palette, &map->identity); | |
566 } | |
567 if ( ! map->identity ) { | |
568 if ( map->table == NULL ) { | |
569 return(-1); | |
570 } | |
571 } | |
572 if (srcfmt->BitsPerPixel!=dstfmt->BitsPerPixel) | |
573 map->identity = 0; | |
574 break; | |
575 | |
576 default: | |
577 /* Palette --> BitField */ | |
578 map->table = Map1toN(srcfmt, dstfmt); | |
579 if ( map->table == NULL ) { | |
580 return(-1); | |
581 } | |
582 break; | |
583 } | |
584 break; | |
585 default: | |
586 switch (dstfmt->BytesPerPixel) { | |
587 case 1: | |
588 /* BitField --> Palette */ | |
589 map->table = MapNto1(srcfmt, dstfmt, &map->identity); | |
590 if ( ! map->identity ) { | |
591 if ( map->table == NULL ) { | |
592 return(-1); | |
593 } | |
594 } | |
595 map->identity = 0; /* Don't optimize to copy */ | |
596 break; | |
597 default: | |
598 /* BitField --> BitField */ | |
599 if ( FORMAT_EQUAL(srcfmt, dstfmt) ) | |
600 map->identity = 1; | |
601 break; | |
602 } | |
603 break; | |
604 } | |
605 | |
606 map->dst = dst; | |
607 map->format_version = dst->format_version; | |
608 | |
609 /* Choose your blitters wisely */ | |
610 return(SDL_CalculateBlit(src)); | |
611 } | |
612 void SDL_FreeBlitMap(SDL_BlitMap *map) | |
613 { | |
614 if ( map ) { | |
615 SDL_InvalidateMap(map); | |
616 if ( map->sw_data != NULL ) { | |
617 SDL_free(map->sw_data); | |
618 } | |
619 SDL_free(map); | |
620 } | |
621 } |