Mercurial > sdl-ios-xcode
comparison src/video/SDL_pixels.c @ 1668:4da1ee79c9af SDL-1.3
more tweaking indent options
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 29 May 2006 04:04:35 +0000 |
parents | 782fd950bd46 |
children | eef792d31de8 |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
31 #include "SDL_RLEaccel_c.h" | 31 #include "SDL_RLEaccel_c.h" |
32 | 32 |
33 /* Helper functions */ | 33 /* Helper functions */ |
34 | 34 |
35 SDL_bool | 35 SDL_bool |
36 SDL_PixelFormatEnumToMasks (Uint32 format, int *bpp, Uint32 * Rmask, | 36 SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 * Rmask, |
37 Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask) | 37 Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask) |
38 { | 38 { |
39 Uint32 masks[4]; | 39 Uint32 masks[4]; |
40 | 40 |
41 /* Initialize the values here */ | 41 /* Initialize the values here */ |
42 if (SDL_BITSPERPIXEL (format) == 24) { | 42 if (SDL_BITSPERPIXEL(format) == 24) { |
43 *bpp = SDL_BYTESPERPIXEL (format) * 8; | 43 *bpp = SDL_BYTESPERPIXEL(format) * 8; |
44 } else { | 44 } else { |
45 *bpp = SDL_BITSPERPIXEL (format); | 45 *bpp = SDL_BITSPERPIXEL(format); |
46 } | 46 } |
47 *Rmask = *Gmask = *Bmask = *Amask = 0; | 47 *Rmask = *Gmask = *Bmask = *Amask = 0; |
48 | 48 |
49 if (SDL_PIXELTYPE (format) != SDL_PixelType_Packed8 && | 49 if (SDL_PIXELTYPE(format) != SDL_PixelType_Packed8 && |
50 SDL_PIXELTYPE (format) != SDL_PixelType_Packed16 && | 50 SDL_PIXELTYPE(format) != SDL_PixelType_Packed16 && |
51 SDL_PIXELTYPE (format) != SDL_PixelType_Packed32) { | 51 SDL_PIXELTYPE(format) != SDL_PixelType_Packed32) { |
52 /* Not a format that uses masks */ | 52 /* Not a format that uses masks */ |
53 return SDL_TRUE; | 53 return SDL_TRUE; |
54 } | 54 } |
55 | 55 |
56 switch (SDL_PIXELLAYOUT (format)) { | 56 switch (SDL_PIXELLAYOUT(format)) { |
57 case SDL_PackedLayout_332: | 57 case SDL_PackedLayout_332: |
58 masks[0] = 0x00000000; | 58 masks[0] = 0x00000000; |
59 masks[1] = 0x000000E0; | 59 masks[1] = 0x000000E0; |
60 masks[2] = 0x0000001C; | 60 masks[2] = 0x0000001C; |
61 masks[3] = 0x00000003; | 61 masks[3] = 0x00000003; |
93 default: | 93 default: |
94 /* Unknown layout */ | 94 /* Unknown layout */ |
95 return SDL_FALSE; | 95 return SDL_FALSE; |
96 } | 96 } |
97 | 97 |
98 switch (SDL_PIXELORDER (format)) { | 98 switch (SDL_PIXELORDER(format)) { |
99 case SDL_PackedOrder_XRGB: | 99 case SDL_PackedOrder_XRGB: |
100 *Rmask = masks[1]; | 100 *Rmask = masks[1]; |
101 *Gmask = masks[2]; | 101 *Gmask = masks[2]; |
102 *Bmask = masks[3]; | 102 *Bmask = masks[3]; |
103 break; | 103 break; |
146 } | 146 } |
147 return SDL_TRUE; | 147 return SDL_TRUE; |
148 } | 148 } |
149 | 149 |
150 Uint32 | 150 Uint32 |
151 SDL_MasksToPixelFormatEnum (int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, | 151 SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, |
152 Uint32 Amask) | 152 Uint32 Amask) |
153 { | 153 { |
154 switch (bpp) { | 154 switch (bpp) { |
155 case 8: | 155 case 8: |
156 switch (Rmask) { | 156 switch (Rmask) { |
157 case 0: | 157 case 0: |
217 | 217 |
218 /* | 218 /* |
219 * 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. |
220 */ | 220 */ |
221 SDL_PixelFormat * | 221 SDL_PixelFormat * |
222 SDL_AllocFormat (int bpp, | 222 SDL_AllocFormat(int bpp, |
223 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | 223 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) |
224 { | 224 { |
225 SDL_PixelFormat *format; | 225 SDL_PixelFormat *format; |
226 Uint32 mask; | 226 Uint32 mask; |
227 | 227 |
228 /* Allocate an empty pixel format structure */ | 228 /* Allocate an empty pixel format structure */ |
229 format = SDL_malloc (sizeof (*format)); | 229 format = SDL_malloc(sizeof(*format)); |
230 if (format == NULL) { | 230 if (format == NULL) { |
231 SDL_OutOfMemory (); | 231 SDL_OutOfMemory(); |
232 return (NULL); | 232 return (NULL); |
233 } | 233 } |
234 SDL_memset (format, 0, sizeof (*format)); | 234 SDL_memset(format, 0, sizeof(*format)); |
235 format->alpha = SDL_ALPHA_OPAQUE; | 235 format->alpha = SDL_ALPHA_OPAQUE; |
236 | 236 |
237 /* Set up the format */ | 237 /* Set up the format */ |
238 format->BitsPerPixel = bpp; | 238 format->BitsPerPixel = bpp; |
239 format->BytesPerPixel = (bpp + 7) / 8; | 239 format->BytesPerPixel = (bpp + 7) / 8; |
304 format->Amask = 0; | 304 format->Amask = 0; |
305 } | 305 } |
306 if (bpp <= 8) { /* Palettized mode */ | 306 if (bpp <= 8) { /* Palettized mode */ |
307 int ncolors = 1 << bpp; | 307 int ncolors = 1 << bpp; |
308 #ifdef DEBUG_PALETTE | 308 #ifdef DEBUG_PALETTE |
309 fprintf (stderr, "bpp=%d ncolors=%d\n", bpp, ncolors); | 309 fprintf(stderr, "bpp=%d ncolors=%d\n", bpp, ncolors); |
310 #endif | 310 #endif |
311 format->palette = (SDL_Palette *) SDL_malloc (sizeof (SDL_Palette)); | 311 format->palette = (SDL_Palette *) SDL_malloc(sizeof(SDL_Palette)); |
312 if (format->palette == NULL) { | 312 if (format->palette == NULL) { |
313 SDL_FreeFormat (format); | 313 SDL_FreeFormat(format); |
314 SDL_OutOfMemory (); | 314 SDL_OutOfMemory(); |
315 return (NULL); | 315 return (NULL); |
316 } | 316 } |
317 (format->palette)->ncolors = ncolors; | 317 (format->palette)->ncolors = ncolors; |
318 (format->palette)->colors = (SDL_Color *) SDL_malloc ((format-> | 318 (format->palette)->colors = (SDL_Color *) SDL_malloc((format-> |
319 palette)-> | 319 palette)-> |
320 ncolors * | 320 ncolors * |
321 sizeof | 321 sizeof |
322 (SDL_Color)); | 322 (SDL_Color)); |
323 if ((format->palette)->colors == NULL) { | 323 if ((format->palette)->colors == NULL) { |
324 SDL_FreeFormat (format); | 324 SDL_FreeFormat(format); |
325 SDL_OutOfMemory (); | 325 SDL_OutOfMemory(); |
326 return (NULL); | 326 return (NULL); |
327 } | 327 } |
328 if (Rmask || Bmask || Gmask) { | 328 if (Rmask || Bmask || Gmask) { |
329 /* create palette according to masks */ | 329 /* create palette according to masks */ |
330 int i; | 330 int i; |
337 Rw = 8 - format->Rloss; | 337 Rw = 8 - format->Rloss; |
338 for (i = format->Rloss; i > 0; i -= Rw) | 338 for (i = format->Rloss; i > 0; i -= Rw) |
339 Rm |= 1 << i; | 339 Rm |= 1 << i; |
340 } | 340 } |
341 #ifdef DEBUG_PALETTE | 341 #ifdef DEBUG_PALETTE |
342 fprintf (stderr, "Rw=%d Rm=0x%02X\n", Rw, Rm); | 342 fprintf(stderr, "Rw=%d Rm=0x%02X\n", Rw, Rm); |
343 #endif | 343 #endif |
344 if (Gmask) { | 344 if (Gmask) { |
345 Gw = 8 - format->Gloss; | 345 Gw = 8 - format->Gloss; |
346 for (i = format->Gloss; i > 0; i -= Gw) | 346 for (i = format->Gloss; i > 0; i -= Gw) |
347 Gm |= 1 << i; | 347 Gm |= 1 << i; |
348 } | 348 } |
349 #ifdef DEBUG_PALETTE | 349 #ifdef DEBUG_PALETTE |
350 fprintf (stderr, "Gw=%d Gm=0x%02X\n", Gw, Gm); | 350 fprintf(stderr, "Gw=%d Gm=0x%02X\n", Gw, Gm); |
351 #endif | 351 #endif |
352 if (Bmask) { | 352 if (Bmask) { |
353 Bw = 8 - format->Bloss; | 353 Bw = 8 - format->Bloss; |
354 for (i = format->Bloss; i > 0; i -= Bw) | 354 for (i = format->Bloss; i > 0; i -= Bw) |
355 Bm |= 1 << i; | 355 Bm |= 1 << i; |
356 } | 356 } |
357 #ifdef DEBUG_PALETTE | 357 #ifdef DEBUG_PALETTE |
358 fprintf (stderr, "Bw=%d Bm=0x%02X\n", Bw, Bm); | 358 fprintf(stderr, "Bw=%d Bm=0x%02X\n", Bw, Bm); |
359 #endif | 359 #endif |
360 #ifdef ENABLE_PALETTE_ALPHA | 360 #ifdef ENABLE_PALETTE_ALPHA |
361 if (Amask) { | 361 if (Amask) { |
362 Aw = 8 - format->Aloss; | 362 Aw = 8 - format->Aloss; |
363 for (i = format->Aloss; i > 0; i -= Aw) | 363 for (i = format->Aloss; i > 0; i -= Aw) |
364 Am |= 1 << i; | 364 Am |= 1 << i; |
365 } | 365 } |
366 # ifdef DEBUG_PALETTE | 366 # ifdef DEBUG_PALETTE |
367 fprintf (stderr, "Aw=%d Am=0x%02X\n", Aw, Am); | 367 fprintf(stderr, "Aw=%d Am=0x%02X\n", Aw, Am); |
368 # endif | 368 # endif |
369 #endif | 369 #endif |
370 for (i = 0; i < ncolors; ++i) { | 370 for (i = 0; i < ncolors; ++i) { |
371 int r, g, b; | 371 int r, g, b; |
372 r = (i & Rmask) >> format->Rshift; | 372 r = (i & Rmask) >> format->Rshift; |
397 format->palette->colors[1].r = 0x00; | 397 format->palette->colors[1].r = 0x00; |
398 format->palette->colors[1].g = 0x00; | 398 format->palette->colors[1].g = 0x00; |
399 format->palette->colors[1].b = 0x00; | 399 format->palette->colors[1].b = 0x00; |
400 } else { | 400 } else { |
401 /* Create an empty palette */ | 401 /* Create an empty palette */ |
402 SDL_memset ((format->palette)->colors, 0, | 402 SDL_memset((format->palette)->colors, 0, |
403 (format->palette)->ncolors * sizeof (SDL_Color)); | 403 (format->palette)->ncolors * sizeof(SDL_Color)); |
404 } | 404 } |
405 } | 405 } |
406 return (format); | 406 return (format); |
407 } | 407 } |
408 | 408 |
409 SDL_PixelFormat * | 409 SDL_PixelFormat * |
410 SDL_ReallocFormat (SDL_Surface * surface, int bpp, | 410 SDL_ReallocFormat(SDL_Surface * surface, int bpp, |
411 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | 411 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) |
412 { | 412 { |
413 if (surface->format) { | 413 if (surface->format) { |
414 SDL_FreeFormat (surface->format); | 414 SDL_FreeFormat(surface->format); |
415 SDL_FormatChanged (surface); | 415 SDL_FormatChanged(surface); |
416 } | 416 } |
417 surface->format = SDL_AllocFormat (bpp, Rmask, Gmask, Bmask, Amask); | 417 surface->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask); |
418 return surface->format; | 418 return surface->format; |
419 } | 419 } |
420 | 420 |
421 /* | 421 /* |
422 * Change any previous mappings from/to the new surface format | 422 * Change any previous mappings from/to the new surface format |
423 */ | 423 */ |
424 void | 424 void |
425 SDL_FormatChanged (SDL_Surface * surface) | 425 SDL_FormatChanged(SDL_Surface * surface) |
426 { | 426 { |
427 static int format_version = 0; | 427 static int format_version = 0; |
428 ++format_version; | 428 ++format_version; |
429 if (format_version < 0) { /* It wrapped... */ | 429 if (format_version < 0) { /* It wrapped... */ |
430 format_version = 1; | 430 format_version = 1; |
431 } | 431 } |
432 surface->format_version = format_version; | 432 surface->format_version = format_version; |
433 SDL_InvalidateMap (surface->map); | 433 SDL_InvalidateMap(surface->map); |
434 } | 434 } |
435 | 435 |
436 /* | 436 /* |
437 * Free a previously allocated format structure | 437 * Free a previously allocated format structure |
438 */ | 438 */ |
439 void | 439 void |
440 SDL_FreeFormat (SDL_PixelFormat * format) | 440 SDL_FreeFormat(SDL_PixelFormat * format) |
441 { | 441 { |
442 if (format) { | 442 if (format) { |
443 if (format->palette) { | 443 if (format->palette) { |
444 if (format->palette->colors) { | 444 if (format->palette->colors) { |
445 SDL_free (format->palette->colors); | 445 SDL_free(format->palette->colors); |
446 } | 446 } |
447 SDL_free (format->palette); | 447 SDL_free(format->palette); |
448 } | 448 } |
449 SDL_free (format); | 449 SDL_free(format); |
450 } | 450 } |
451 } | 451 } |
452 | 452 |
453 /* | 453 /* |
454 * 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 |
455 */ | 455 */ |
456 void | 456 void |
457 SDL_DitherColors (SDL_Color * colors, int bpp) | 457 SDL_DitherColors(SDL_Color * colors, int bpp) |
458 { | 458 { |
459 int i; | 459 int i; |
460 if (bpp != 8) | 460 if (bpp != 8) |
461 return; /* only 8bpp supported right now */ | 461 return; /* only 8bpp supported right now */ |
462 | 462 |
479 | 479 |
480 /* | 480 /* |
481 * Calculate the pad-aligned scanline width of a surface | 481 * Calculate the pad-aligned scanline width of a surface |
482 */ | 482 */ |
483 Uint16 | 483 Uint16 |
484 SDL_CalculatePitch (SDL_Surface * surface) | 484 SDL_CalculatePitch(SDL_Surface * surface) |
485 { | 485 { |
486 Uint16 pitch; | 486 Uint16 pitch; |
487 | 487 |
488 /* Surface should be 4-byte aligned for speed */ | 488 /* Surface should be 4-byte aligned for speed */ |
489 pitch = surface->w * surface->format->BytesPerPixel; | 489 pitch = surface->w * surface->format->BytesPerPixel; |
503 | 503 |
504 /* | 504 /* |
505 * Match an RGB value to a particular palette index | 505 * Match an RGB value to a particular palette index |
506 */ | 506 */ |
507 Uint8 | 507 Uint8 |
508 SDL_FindColor (SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b) | 508 SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b) |
509 { | 509 { |
510 /* Do colorspace distance matching */ | 510 /* Do colorspace distance matching */ |
511 unsigned int smallest; | 511 unsigned int smallest; |
512 unsigned int distance; | 512 unsigned int distance; |
513 int rd, gd, bd; | 513 int rd, gd, bd; |
531 return (pixel); | 531 return (pixel); |
532 } | 532 } |
533 | 533 |
534 /* Find the opaque pixel value corresponding to an RGB triple */ | 534 /* Find the opaque pixel value corresponding to an RGB triple */ |
535 Uint32 | 535 Uint32 |
536 SDL_MapRGB (SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b) | 536 SDL_MapRGB(SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b) |
537 { | 537 { |
538 if (format->palette == NULL) { | 538 if (format->palette == NULL) { |
539 return (r >> format->Rloss) << format->Rshift | 539 return (r >> format->Rloss) << format->Rshift |
540 | (g >> format->Gloss) << format->Gshift | 540 | (g >> format->Gloss) << format->Gshift |
541 | (b >> format->Bloss) << format->Bshift | format->Amask; | 541 | (b >> format->Bloss) << format->Bshift | format->Amask; |
542 } else { | 542 } else { |
543 return SDL_FindColor (format->palette, r, g, b); | 543 return SDL_FindColor(format->palette, r, g, b); |
544 } | 544 } |
545 } | 545 } |
546 | 546 |
547 /* Find the pixel value corresponding to an RGBA quadruple */ | 547 /* Find the pixel value corresponding to an RGBA quadruple */ |
548 Uint32 | 548 Uint32 |
549 SDL_MapRGBA (SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b, Uint8 a) | 549 SDL_MapRGBA(SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b, Uint8 a) |
550 { | 550 { |
551 if (format->palette == NULL) { | 551 if (format->palette == NULL) { |
552 return (r >> format->Rloss) << format->Rshift | 552 return (r >> format->Rloss) << format->Rshift |
553 | (g >> format->Gloss) << format->Gshift | 553 | (g >> format->Gloss) << format->Gshift |
554 | (b >> format->Bloss) << format->Bshift | 554 | (b >> format->Bloss) << format->Bshift |
555 | ((a >> format->Aloss) << format->Ashift & format->Amask); | 555 | ((a >> format->Aloss) << format->Ashift & format->Amask); |
556 } else { | 556 } else { |
557 return SDL_FindColor (format->palette, r, g, b); | 557 return SDL_FindColor(format->palette, r, g, b); |
558 } | 558 } |
559 } | 559 } |
560 | 560 |
561 void | 561 void |
562 SDL_GetRGBA (Uint32 pixel, SDL_PixelFormat * fmt, | 562 SDL_GetRGBA(Uint32 pixel, SDL_PixelFormat * fmt, |
563 Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a) | 563 Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a) |
564 { | 564 { |
565 if (fmt->palette == NULL) { | 565 if (fmt->palette == NULL) { |
566 /* | 566 /* |
567 * This makes sure that the result is mapped to the | 567 * This makes sure that the result is mapped to the |
568 * interval [0..255], and the maximum value for each | 568 * interval [0..255], and the maximum value for each |
592 *a = SDL_ALPHA_OPAQUE; | 592 *a = SDL_ALPHA_OPAQUE; |
593 } | 593 } |
594 } | 594 } |
595 | 595 |
596 void | 596 void |
597 SDL_GetRGB (Uint32 pixel, SDL_PixelFormat * fmt, Uint8 * r, Uint8 * g, | 597 SDL_GetRGB(Uint32 pixel, SDL_PixelFormat * fmt, Uint8 * r, Uint8 * g, |
598 Uint8 * b) | 598 Uint8 * b) |
599 { | 599 { |
600 if (fmt->palette == NULL) { | 600 if (fmt->palette == NULL) { |
601 /* the note for SDL_GetRGBA above applies here too */ | 601 /* the note for SDL_GetRGBA above applies here too */ |
602 unsigned v; | 602 unsigned v; |
603 v = (pixel & fmt->Rmask) >> fmt->Rshift; | 603 v = (pixel & fmt->Rmask) >> fmt->Rshift; |
613 } | 613 } |
614 } | 614 } |
615 | 615 |
616 /* Apply gamma to a set of colors - this is easy. :) */ | 616 /* Apply gamma to a set of colors - this is easy. :) */ |
617 void | 617 void |
618 SDL_ApplyGamma (Uint16 * gamma, SDL_Color * colors, SDL_Color * output, | 618 SDL_ApplyGamma(Uint16 * gamma, SDL_Color * colors, SDL_Color * output, |
619 int ncolors) | 619 int ncolors) |
620 { | 620 { |
621 int i; | 621 int i; |
622 | 622 |
623 for (i = 0; i < ncolors; ++i) { | 623 for (i = 0; i < ncolors; ++i) { |
624 output[i].r = gamma[0 * 256 + colors[i].r] >> 8; | 624 output[i].r = gamma[0 * 256 + colors[i].r] >> 8; |
627 } | 627 } |
628 } | 628 } |
629 | 629 |
630 /* Map from Palette to Palette */ | 630 /* Map from Palette to Palette */ |
631 static Uint8 * | 631 static Uint8 * |
632 Map1to1 (SDL_Palette * src, SDL_Palette * dst, int *identical) | 632 Map1to1(SDL_Palette * src, SDL_Palette * dst, int *identical) |
633 { | 633 { |
634 Uint8 *map; | 634 Uint8 *map; |
635 int i; | 635 int i; |
636 | 636 |
637 if (identical) { | 637 if (identical) { |
638 if (src->ncolors <= dst->ncolors) { | 638 if (src->ncolors <= dst->ncolors) { |
639 /* If an identical palette, no need to map */ | 639 /* If an identical palette, no need to map */ |
640 if (SDL_memcmp (src->colors, dst->colors, src->ncolors * | 640 if (SDL_memcmp(src->colors, dst->colors, src->ncolors * |
641 sizeof (SDL_Color)) == 0) { | 641 sizeof(SDL_Color)) == 0) { |
642 *identical = 1; | 642 *identical = 1; |
643 return (NULL); | 643 return (NULL); |
644 } | 644 } |
645 } | 645 } |
646 *identical = 0; | 646 *identical = 0; |
647 } | 647 } |
648 map = (Uint8 *) SDL_malloc (src->ncolors); | 648 map = (Uint8 *) SDL_malloc(src->ncolors); |
649 if (map == NULL) { | 649 if (map == NULL) { |
650 SDL_OutOfMemory (); | 650 SDL_OutOfMemory(); |
651 return (NULL); | 651 return (NULL); |
652 } | 652 } |
653 for (i = 0; i < src->ncolors; ++i) { | 653 for (i = 0; i < src->ncolors; ++i) { |
654 map[i] = SDL_FindColor (dst, | 654 map[i] = SDL_FindColor(dst, |
655 src->colors[i].r, src->colors[i].g, | 655 src->colors[i].r, src->colors[i].g, |
656 src->colors[i].b); | 656 src->colors[i].b); |
657 } | 657 } |
658 return (map); | 658 return (map); |
659 } | 659 } |
660 | 660 |
661 /* Map from Palette to BitField */ | 661 /* Map from Palette to BitField */ |
662 static Uint8 * | 662 static Uint8 * |
663 Map1toN (SDL_PixelFormat * src, SDL_PixelFormat * dst) | 663 Map1toN(SDL_PixelFormat * src, SDL_PixelFormat * dst) |
664 { | 664 { |
665 Uint8 *map; | 665 Uint8 *map; |
666 int i; | 666 int i; |
667 int bpp; | 667 int bpp; |
668 unsigned alpha; | 668 unsigned alpha; |
669 SDL_Palette *pal = src->palette; | 669 SDL_Palette *pal = src->palette; |
670 | 670 |
671 bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel); | 671 bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel); |
672 map = (Uint8 *) SDL_malloc (pal->ncolors * bpp); | 672 map = (Uint8 *) SDL_malloc(pal->ncolors * bpp); |
673 if (map == NULL) { | 673 if (map == NULL) { |
674 SDL_OutOfMemory (); | 674 SDL_OutOfMemory(); |
675 return (NULL); | 675 return (NULL); |
676 } | 676 } |
677 | 677 |
678 alpha = dst->Amask ? src->alpha : 0; | 678 alpha = dst->Amask ? src->alpha : 0; |
679 /* We memory copy to the pixel map so the endianness is preserved */ | 679 /* We memory copy to the pixel map so the endianness is preserved */ |
680 for (i = 0; i < pal->ncolors; ++i) { | 680 for (i = 0; i < pal->ncolors; ++i) { |
681 ASSEMBLE_RGBA (&map[i * bpp], dst->BytesPerPixel, dst, | 681 ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, |
682 pal->colors[i].r, pal->colors[i].g, | 682 pal->colors[i].r, pal->colors[i].g, |
683 pal->colors[i].b, alpha); | 683 pal->colors[i].b, alpha); |
684 } | 684 } |
685 return (map); | 685 return (map); |
686 } | 686 } |
687 | 687 |
688 /* Map from BitField to Dithered-Palette to Palette */ | 688 /* Map from BitField to Dithered-Palette to Palette */ |
689 static Uint8 * | 689 static Uint8 * |
690 MapNto1 (SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical) | 690 MapNto1(SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical) |
691 { | 691 { |
692 /* Generate a 256 color dither palette */ | 692 /* Generate a 256 color dither palette */ |
693 SDL_Palette dithered; | 693 SDL_Palette dithered; |
694 SDL_Color colors[256]; | 694 SDL_Color colors[256]; |
695 SDL_Palette *pal = dst->palette; | 695 SDL_Palette *pal = dst->palette; |
696 | 696 |
697 /* SDL_DitherColors does not initialize the 'unused' component of colors, | 697 /* SDL_DitherColors does not initialize the 'unused' component of colors, |
698 but Map1to1 compares it against pal, so we should initialize it. */ | 698 but Map1to1 compares it against pal, so we should initialize it. */ |
699 SDL_memset (colors, 0, sizeof (colors)); | 699 SDL_memset(colors, 0, sizeof(colors)); |
700 | 700 |
701 dithered.ncolors = 256; | 701 dithered.ncolors = 256; |
702 SDL_DitherColors (colors, 8); | 702 SDL_DitherColors(colors, 8); |
703 dithered.colors = colors; | 703 dithered.colors = colors; |
704 return (Map1to1 (&dithered, pal, identical)); | 704 return (Map1to1(&dithered, pal, identical)); |
705 } | 705 } |
706 | 706 |
707 SDL_BlitMap * | 707 SDL_BlitMap * |
708 SDL_AllocBlitMap (void) | 708 SDL_AllocBlitMap(void) |
709 { | 709 { |
710 SDL_BlitMap *map; | 710 SDL_BlitMap *map; |
711 | 711 |
712 /* Allocate the empty map */ | 712 /* Allocate the empty map */ |
713 map = (SDL_BlitMap *) SDL_malloc (sizeof (*map)); | 713 map = (SDL_BlitMap *) SDL_malloc(sizeof(*map)); |
714 if (map == NULL) { | 714 if (map == NULL) { |
715 SDL_OutOfMemory (); | 715 SDL_OutOfMemory(); |
716 return (NULL); | 716 return (NULL); |
717 } | 717 } |
718 SDL_memset (map, 0, sizeof (*map)); | 718 SDL_memset(map, 0, sizeof(*map)); |
719 | 719 |
720 /* Allocate the software blit data */ | 720 /* Allocate the software blit data */ |
721 map->sw_data = | 721 map->sw_data = |
722 (struct private_swaccel *) SDL_malloc (sizeof (*map->sw_data)); | 722 (struct private_swaccel *) SDL_malloc(sizeof(*map->sw_data)); |
723 if (map->sw_data == NULL) { | 723 if (map->sw_data == NULL) { |
724 SDL_FreeBlitMap (map); | 724 SDL_FreeBlitMap(map); |
725 SDL_OutOfMemory (); | 725 SDL_OutOfMemory(); |
726 return (NULL); | 726 return (NULL); |
727 } | 727 } |
728 SDL_memset (map->sw_data, 0, sizeof (*map->sw_data)); | 728 SDL_memset(map->sw_data, 0, sizeof(*map->sw_data)); |
729 | 729 |
730 /* It's ready to go */ | 730 /* It's ready to go */ |
731 return (map); | 731 return (map); |
732 } | 732 } |
733 | 733 |
734 void | 734 void |
735 SDL_InvalidateMap (SDL_BlitMap * map) | 735 SDL_InvalidateMap(SDL_BlitMap * map) |
736 { | 736 { |
737 if (!map) { | 737 if (!map) { |
738 return; | 738 return; |
739 } | 739 } |
740 map->dst = NULL; | 740 map->dst = NULL; |
741 map->format_version = (unsigned int) -1; | 741 map->format_version = (unsigned int) -1; |
742 if (map->table) { | 742 if (map->table) { |
743 SDL_free (map->table); | 743 SDL_free(map->table); |
744 map->table = NULL; | 744 map->table = NULL; |
745 } | 745 } |
746 } | 746 } |
747 int | 747 int |
748 SDL_MapSurface (SDL_Surface * src, SDL_Surface * dst) | 748 SDL_MapSurface(SDL_Surface * src, SDL_Surface * dst) |
749 { | 749 { |
750 SDL_PixelFormat *srcfmt; | 750 SDL_PixelFormat *srcfmt; |
751 SDL_PixelFormat *dstfmt; | 751 SDL_PixelFormat *dstfmt; |
752 SDL_BlitMap *map; | 752 SDL_BlitMap *map; |
753 | 753 |
754 /* Clear out any previous mapping */ | 754 /* Clear out any previous mapping */ |
755 map = src->map; | 755 map = src->map; |
756 if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) { | 756 if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) { |
757 SDL_UnRLESurface (src, 1); | 757 SDL_UnRLESurface(src, 1); |
758 } | 758 } |
759 SDL_InvalidateMap (map); | 759 SDL_InvalidateMap(map); |
760 | 760 |
761 /* Figure out what kind of mapping we're doing */ | 761 /* Figure out what kind of mapping we're doing */ |
762 map->identity = 0; | 762 map->identity = 0; |
763 srcfmt = src->format; | 763 srcfmt = src->format; |
764 dstfmt = dst->format; | 764 dstfmt = dst->format; |
770 /* If both SDL_HWSURFACE, assume have same palette */ | 770 /* If both SDL_HWSURFACE, assume have same palette */ |
771 if (((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) && | 771 if (((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) && |
772 ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE)) { | 772 ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE)) { |
773 map->identity = 1; | 773 map->identity = 1; |
774 } else { | 774 } else { |
775 map->table = Map1to1 (srcfmt->palette, | 775 map->table = Map1to1(srcfmt->palette, |
776 dstfmt->palette, &map->identity); | 776 dstfmt->palette, &map->identity); |
777 } | 777 } |
778 if (!map->identity) { | 778 if (!map->identity) { |
779 if (map->table == NULL) { | 779 if (map->table == NULL) { |
780 return (-1); | 780 return (-1); |
781 } | 781 } |
784 map->identity = 0; | 784 map->identity = 0; |
785 break; | 785 break; |
786 | 786 |
787 default: | 787 default: |
788 /* Palette --> BitField */ | 788 /* Palette --> BitField */ |
789 map->table = Map1toN (srcfmt, dstfmt); | 789 map->table = Map1toN(srcfmt, dstfmt); |
790 if (map->table == NULL) { | 790 if (map->table == NULL) { |
791 return (-1); | 791 return (-1); |
792 } | 792 } |
793 break; | 793 break; |
794 } | 794 } |
795 break; | 795 break; |
796 default: | 796 default: |
797 switch (dstfmt->BytesPerPixel) { | 797 switch (dstfmt->BytesPerPixel) { |
798 case 1: | 798 case 1: |
799 /* BitField --> Palette */ | 799 /* BitField --> Palette */ |
800 map->table = MapNto1 (srcfmt, dstfmt, &map->identity); | 800 map->table = MapNto1(srcfmt, dstfmt, &map->identity); |
801 if (!map->identity) { | 801 if (!map->identity) { |
802 if (map->table == NULL) { | 802 if (map->table == NULL) { |
803 return (-1); | 803 return (-1); |
804 } | 804 } |
805 } | 805 } |
806 map->identity = 0; /* Don't optimize to copy */ | 806 map->identity = 0; /* Don't optimize to copy */ |
807 break; | 807 break; |
808 default: | 808 default: |
809 /* BitField --> BitField */ | 809 /* BitField --> BitField */ |
810 if (FORMAT_EQUAL (srcfmt, dstfmt)) | 810 if (FORMAT_EQUAL(srcfmt, dstfmt)) |
811 map->identity = 1; | 811 map->identity = 1; |
812 break; | 812 break; |
813 } | 813 } |
814 break; | 814 break; |
815 } | 815 } |
816 | 816 |
817 map->dst = dst; | 817 map->dst = dst; |
818 map->format_version = dst->format_version; | 818 map->format_version = dst->format_version; |
819 | 819 |
820 /* Choose your blitters wisely */ | 820 /* Choose your blitters wisely */ |
821 return (SDL_CalculateBlit (src)); | 821 return (SDL_CalculateBlit(src)); |
822 } | 822 } |
823 | 823 |
824 void | 824 void |
825 SDL_FreeBlitMap (SDL_BlitMap * map) | 825 SDL_FreeBlitMap(SDL_BlitMap * map) |
826 { | 826 { |
827 if (map) { | 827 if (map) { |
828 SDL_InvalidateMap (map); | 828 SDL_InvalidateMap(map); |
829 if (map->sw_data != NULL) { | 829 if (map->sw_data != NULL) { |
830 SDL_free (map->sw_data); | 830 SDL_free(map->sw_data); |
831 } | 831 } |
832 SDL_free (map); | 832 SDL_free(map); |
833 } | 833 } |
834 } | 834 } |
835 | 835 |
836 /* vi: set ts=4 sw=4 expandtab: */ | 836 /* vi: set ts=4 sw=4 expandtab: */ |