SDL  2.0
SDL_pixels.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../SDL_internal.h"
22 
23 /* General (mostly internal) pixel/color manipulation routines for SDL */
24 
25 #include "SDL_endian.h"
26 #include "SDL_video.h"
27 #include "SDL_sysvideo.h"
28 #include "SDL_blit.h"
29 #include "SDL_pixels_c.h"
30 #include "SDL_RLEaccel_c.h"
31 
32 
33 /* Lookup tables to expand partial bytes to the full 0..255 range */
34 
35 static Uint8 lookup_0[] = {
36 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
37 };
38 
39 static Uint8 lookup_1[] = {
40 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 255
41 };
42 
43 static Uint8 lookup_2[] = {
44 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 129, 133, 137, 141, 145, 149, 153, 157, 161, 165, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 214, 218, 222, 226, 230, 234, 238, 242, 246, 250, 255
45 };
46 
47 static Uint8 lookup_3[] = {
48 0, 8, 16, 24, 32, 41, 49, 57, 65, 74, 82, 90, 98, 106, 115, 123, 131, 139, 148, 156, 164, 172, 180, 189, 197, 205, 213, 222, 230, 238, 246, 255
49 };
50 
51 static Uint8 lookup_4[] = {
52 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255
53 };
54 
55 static Uint8 lookup_5[] = {
56 0, 36, 72, 109, 145, 182, 218, 255
57 };
58 
59 static Uint8 lookup_6[] = {
60 0, 85, 170, 255
61 };
62 
63 static Uint8 lookup_7[] = {
64 0, 255
65 };
66 
67 static Uint8 lookup_8[] = {
68 255
69 };
70 
72  lookup_0,
73  lookup_1,
74  lookup_2,
75  lookup_3,
76  lookup_4,
77  lookup_5,
78  lookup_6,
79  lookup_7,
80  lookup_8
81 };
82 
83 /* Helper functions */
84 
85 const char*
87 {
88  switch (format) {
89 #define CASE(X) case X: return #X;
127 #undef CASE
128  default:
129  return "SDL_PIXELFORMAT_UNKNOWN";
130  }
131 }
132 
133 SDL_bool
135  Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask)
136 {
137  Uint32 masks[4];
138 
139  /* This function doesn't work with FourCC pixel formats */
140  if (SDL_ISPIXELFORMAT_FOURCC(format)) {
141  SDL_SetError("FOURCC pixel formats are not supported");
142  return SDL_FALSE;
143  }
144 
145  /* Initialize the values here */
146  if (SDL_BYTESPERPIXEL(format) <= 2) {
147  *bpp = SDL_BITSPERPIXEL(format);
148  } else {
149  *bpp = SDL_BYTESPERPIXEL(format) * 8;
150  }
151  *Rmask = *Gmask = *Bmask = *Amask = 0;
152 
153  if (format == SDL_PIXELFORMAT_RGB24) {
154 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
155  *Rmask = 0x00FF0000;
156  *Gmask = 0x0000FF00;
157  *Bmask = 0x000000FF;
158 #else
159  *Rmask = 0x000000FF;
160  *Gmask = 0x0000FF00;
161  *Bmask = 0x00FF0000;
162 #endif
163  return SDL_TRUE;
164  }
165 
166  if (format == SDL_PIXELFORMAT_BGR24) {
167 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
168  *Rmask = 0x000000FF;
169  *Gmask = 0x0000FF00;
170  *Bmask = 0x00FF0000;
171 #else
172  *Rmask = 0x00FF0000;
173  *Gmask = 0x0000FF00;
174  *Bmask = 0x000000FF;
175 #endif
176  return SDL_TRUE;
177  }
178 
179  if (SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED8 &&
182  /* Not a format that uses masks */
183  return SDL_TRUE;
184  }
185 
186  switch (SDL_PIXELLAYOUT(format)) {
188  masks[0] = 0x00000000;
189  masks[1] = 0x000000E0;
190  masks[2] = 0x0000001C;
191  masks[3] = 0x00000003;
192  break;
194  masks[0] = 0x0000F000;
195  masks[1] = 0x00000F00;
196  masks[2] = 0x000000F0;
197  masks[3] = 0x0000000F;
198  break;
200  masks[0] = 0x00008000;
201  masks[1] = 0x00007C00;
202  masks[2] = 0x000003E0;
203  masks[3] = 0x0000001F;
204  break;
206  masks[0] = 0x0000F800;
207  masks[1] = 0x000007C0;
208  masks[2] = 0x0000003E;
209  masks[3] = 0x00000001;
210  break;
212  masks[0] = 0x00000000;
213  masks[1] = 0x0000F800;
214  masks[2] = 0x000007E0;
215  masks[3] = 0x0000001F;
216  break;
218  masks[0] = 0xFF000000;
219  masks[1] = 0x00FF0000;
220  masks[2] = 0x0000FF00;
221  masks[3] = 0x000000FF;
222  break;
224  masks[0] = 0xC0000000;
225  masks[1] = 0x3FF00000;
226  masks[2] = 0x000FFC00;
227  masks[3] = 0x000003FF;
228  break;
230  masks[0] = 0xFFC00000;
231  masks[1] = 0x003FF000;
232  masks[2] = 0x00000FFC;
233  masks[3] = 0x00000003;
234  break;
235  default:
236  SDL_SetError("Unknown pixel format");
237  return SDL_FALSE;
238  }
239 
240  switch (SDL_PIXELORDER(format)) {
242  *Rmask = masks[1];
243  *Gmask = masks[2];
244  *Bmask = masks[3];
245  break;
247  *Rmask = masks[0];
248  *Gmask = masks[1];
249  *Bmask = masks[2];
250  break;
252  *Amask = masks[0];
253  *Rmask = masks[1];
254  *Gmask = masks[2];
255  *Bmask = masks[3];
256  break;
258  *Rmask = masks[0];
259  *Gmask = masks[1];
260  *Bmask = masks[2];
261  *Amask = masks[3];
262  break;
264  *Bmask = masks[1];
265  *Gmask = masks[2];
266  *Rmask = masks[3];
267  break;
269  *Bmask = masks[0];
270  *Gmask = masks[1];
271  *Rmask = masks[2];
272  break;
274  *Bmask = masks[0];
275  *Gmask = masks[1];
276  *Rmask = masks[2];
277  *Amask = masks[3];
278  break;
280  *Amask = masks[0];
281  *Bmask = masks[1];
282  *Gmask = masks[2];
283  *Rmask = masks[3];
284  break;
285  default:
286  SDL_SetError("Unknown pixel format");
287  return SDL_FALSE;
288  }
289  return SDL_TRUE;
290 }
291 
292 Uint32
293 SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
294  Uint32 Amask)
295 {
296  switch (bpp) {
297  case 1:
298  /* SDL defaults to MSB ordering */
300  case 4:
301  /* SDL defaults to MSB ordering */
303  case 8:
304  if (Rmask == 0) {
305  return SDL_PIXELFORMAT_INDEX8;
306  }
307  if (Rmask == 0xE0 &&
308  Gmask == 0x1C &&
309  Bmask == 0x03 &&
310  Amask == 0x00) {
311  return SDL_PIXELFORMAT_RGB332;
312  }
313  break;
314  case 12:
315  if (Rmask == 0) {
316  return SDL_PIXELFORMAT_RGB444;
317  }
318  if (Rmask == 0x0F00 &&
319  Gmask == 0x00F0 &&
320  Bmask == 0x000F &&
321  Amask == 0x0000) {
322  return SDL_PIXELFORMAT_RGB444;
323  }
324  break;
325  case 15:
326  if (Rmask == 0) {
327  return SDL_PIXELFORMAT_RGB555;
328  }
329  /* fallthrough */
330  case 16:
331  if (Rmask == 0) {
332  return SDL_PIXELFORMAT_RGB565;
333  }
334  if (Rmask == 0x7C00 &&
335  Gmask == 0x03E0 &&
336  Bmask == 0x001F &&
337  Amask == 0x0000) {
338  return SDL_PIXELFORMAT_RGB555;
339  }
340  if (Rmask == 0x001F &&
341  Gmask == 0x03E0 &&
342  Bmask == 0x7C00 &&
343  Amask == 0x0000) {
344  return SDL_PIXELFORMAT_BGR555;
345  }
346  if (Rmask == 0x0F00 &&
347  Gmask == 0x00F0 &&
348  Bmask == 0x000F &&
349  Amask == 0xF000) {
351  }
352  if (Rmask == 0xF000 &&
353  Gmask == 0x0F00 &&
354  Bmask == 0x00F0 &&
355  Amask == 0x000F) {
357  }
358  if (Rmask == 0x000F &&
359  Gmask == 0x00F0 &&
360  Bmask == 0x0F00 &&
361  Amask == 0xF000) {
363  }
364  if (Rmask == 0x00F0 &&
365  Gmask == 0x0F00 &&
366  Bmask == 0xF000 &&
367  Amask == 0x000F) {
369  }
370  if (Rmask == 0x7C00 &&
371  Gmask == 0x03E0 &&
372  Bmask == 0x001F &&
373  Amask == 0x8000) {
375  }
376  if (Rmask == 0xF800 &&
377  Gmask == 0x07C0 &&
378  Bmask == 0x003E &&
379  Amask == 0x0001) {
381  }
382  if (Rmask == 0x001F &&
383  Gmask == 0x03E0 &&
384  Bmask == 0x7C00 &&
385  Amask == 0x8000) {
387  }
388  if (Rmask == 0x003E &&
389  Gmask == 0x07C0 &&
390  Bmask == 0xF800 &&
391  Amask == 0x0001) {
393  }
394  if (Rmask == 0xF800 &&
395  Gmask == 0x07E0 &&
396  Bmask == 0x001F &&
397  Amask == 0x0000) {
398  return SDL_PIXELFORMAT_RGB565;
399  }
400  if (Rmask == 0x001F &&
401  Gmask == 0x07E0 &&
402  Bmask == 0xF800 &&
403  Amask == 0x0000) {
404  return SDL_PIXELFORMAT_BGR565;
405  }
406  if (Rmask == 0x003F &&
407  Gmask == 0x07C0 &&
408  Bmask == 0xF800 &&
409  Amask == 0x0000) {
410  /* Technically this would be BGR556, but Witek says this works in bug 3158 */
411  return SDL_PIXELFORMAT_RGB565;
412  }
413  break;
414  case 24:
415  switch (Rmask) {
416  case 0:
417  case 0x00FF0000:
418 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
419  return SDL_PIXELFORMAT_RGB24;
420 #else
421  return SDL_PIXELFORMAT_BGR24;
422 #endif
423  case 0x000000FF:
424 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
425  return SDL_PIXELFORMAT_BGR24;
426 #else
427  return SDL_PIXELFORMAT_RGB24;
428 #endif
429  }
430  case 32:
431  if (Rmask == 0) {
432  return SDL_PIXELFORMAT_RGB888;
433  }
434  if (Rmask == 0x00FF0000 &&
435  Gmask == 0x0000FF00 &&
436  Bmask == 0x000000FF &&
437  Amask == 0x00000000) {
438  return SDL_PIXELFORMAT_RGB888;
439  }
440  if (Rmask == 0xFF000000 &&
441  Gmask == 0x00FF0000 &&
442  Bmask == 0x0000FF00 &&
443  Amask == 0x00000000) {
445  }
446  if (Rmask == 0x000000FF &&
447  Gmask == 0x0000FF00 &&
448  Bmask == 0x00FF0000 &&
449  Amask == 0x00000000) {
450  return SDL_PIXELFORMAT_BGR888;
451  }
452  if (Rmask == 0x0000FF00 &&
453  Gmask == 0x00FF0000 &&
454  Bmask == 0xFF000000 &&
455  Amask == 0x00000000) {
457  }
458  if (Rmask == 0x00FF0000 &&
459  Gmask == 0x0000FF00 &&
460  Bmask == 0x000000FF &&
461  Amask == 0xFF000000) {
463  }
464  if (Rmask == 0xFF000000 &&
465  Gmask == 0x00FF0000 &&
466  Bmask == 0x0000FF00 &&
467  Amask == 0x000000FF) {
469  }
470  if (Rmask == 0x000000FF &&
471  Gmask == 0x0000FF00 &&
472  Bmask == 0x00FF0000 &&
473  Amask == 0xFF000000) {
475  }
476  if (Rmask == 0x0000FF00 &&
477  Gmask == 0x00FF0000 &&
478  Bmask == 0xFF000000 &&
479  Amask == 0x000000FF) {
481  }
482  if (Rmask == 0x3FF00000 &&
483  Gmask == 0x000FFC00 &&
484  Bmask == 0x000003FF &&
485  Amask == 0xC0000000) {
487  }
488  }
490 }
491 
493 
496 {
498 
499  /* Look it up in our list of previously allocated formats */
500  for (format = formats; format; format = format->next) {
501  if (pixel_format == format->format) {
502  ++format->refcount;
503  return format;
504  }
505  }
506 
507  /* Allocate an empty pixel format structure, and initialize it */
508  format = SDL_malloc(sizeof(*format));
509  if (format == NULL) {
510  SDL_OutOfMemory();
511  return NULL;
512  }
513  if (SDL_InitFormat(format, pixel_format) < 0) {
514  SDL_free(format);
515  SDL_InvalidParamError("format");
516  return NULL;
517  }
518 
519  if (!SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
520  /* Cache the RGB formats */
521  format->next = formats;
522  formats = format;
523  }
524  return format;
525 }
526 
527 int
529 {
530  int bpp;
531  Uint32 Rmask, Gmask, Bmask, Amask;
532  Uint32 mask;
533 
534  if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
535  &Rmask, &Gmask, &Bmask, &Amask)) {
536  return -1;
537  }
538 
539  /* Set up the format */
540  SDL_zerop(format);
541  format->format = pixel_format;
542  format->BitsPerPixel = bpp;
543  format->BytesPerPixel = (bpp + 7) / 8;
544 
545  format->Rmask = Rmask;
546  format->Rshift = 0;
547  format->Rloss = 8;
548  if (Rmask) {
549  for (mask = Rmask; !(mask & 0x01); mask >>= 1)
550  ++format->Rshift;
551  for (; (mask & 0x01); mask >>= 1)
552  --format->Rloss;
553  }
554 
555  format->Gmask = Gmask;
556  format->Gshift = 0;
557  format->Gloss = 8;
558  if (Gmask) {
559  for (mask = Gmask; !(mask & 0x01); mask >>= 1)
560  ++format->Gshift;
561  for (; (mask & 0x01); mask >>= 1)
562  --format->Gloss;
563  }
564 
565  format->Bmask = Bmask;
566  format->Bshift = 0;
567  format->Bloss = 8;
568  if (Bmask) {
569  for (mask = Bmask; !(mask & 0x01); mask >>= 1)
570  ++format->Bshift;
571  for (; (mask & 0x01); mask >>= 1)
572  --format->Bloss;
573  }
574 
575  format->Amask = Amask;
576  format->Ashift = 0;
577  format->Aloss = 8;
578  if (Amask) {
579  for (mask = Amask; !(mask & 0x01); mask >>= 1)
580  ++format->Ashift;
581  for (; (mask & 0x01); mask >>= 1)
582  --format->Aloss;
583  }
584 
585  format->palette = NULL;
586  format->refcount = 1;
587  format->next = NULL;
588 
589  return 0;
590 }
591 
592 void
594 {
595  SDL_PixelFormat *prev;
596 
597  if (!format) {
598  SDL_InvalidParamError("format");
599  return;
600  }
601  if (--format->refcount > 0) {
602  return;
603  }
604 
605  /* Remove this format from our list */
606  if (format == formats) {
607  formats = format->next;
608  } else if (formats) {
609  for (prev = formats; prev->next; prev = prev->next) {
610  if (prev->next == format) {
611  prev->next = format->next;
612  break;
613  }
614  }
615  }
616 
617  if (format->palette) {
618  SDL_FreePalette(format->palette);
619  }
620  SDL_free(format);
621 }
622 
623 SDL_Palette *
624 SDL_AllocPalette(int ncolors)
625 {
626  SDL_Palette *palette;
627 
628  /* Input validation */
629  if (ncolors < 1) {
630  SDL_InvalidParamError("ncolors");
631  return NULL;
632  }
633 
634  palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
635  if (!palette) {
636  SDL_OutOfMemory();
637  return NULL;
638  }
639  palette->colors =
640  (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
641  if (!palette->colors) {
642  SDL_free(palette);
643  return NULL;
644  }
645  palette->ncolors = ncolors;
646  palette->version = 1;
647  palette->refcount = 1;
648 
649  SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
650 
651  return palette;
652 }
653 
654 int
656 {
657  if (!format) {
658  return SDL_SetError("SDL_SetPixelFormatPalette() passed NULL format");
659  }
660 
661  if (palette && palette->ncolors > (1 << format->BitsPerPixel)) {
662  return SDL_SetError("SDL_SetPixelFormatPalette() passed a palette that doesn't match the format");
663  }
664 
665  if (format->palette == palette) {
666  return 0;
667  }
668 
669  if (format->palette) {
670  SDL_FreePalette(format->palette);
671  }
672 
673  format->palette = palette;
674 
675  if (format->palette) {
676  ++format->palette->refcount;
677  }
678 
679  return 0;
680 }
681 
682 int
684  int firstcolor, int ncolors)
685 {
686  int status = 0;
687 
688  /* Verify the parameters */
689  if (!palette) {
690  return -1;
691  }
692  if (ncolors > (palette->ncolors - firstcolor)) {
693  ncolors = (palette->ncolors - firstcolor);
694  status = -1;
695  }
696 
697  if (colors != (palette->colors + firstcolor)) {
698  SDL_memcpy(palette->colors + firstcolor, colors,
699  ncolors * sizeof(*colors));
700  }
701  ++palette->version;
702  if (!palette->version) {
703  palette->version = 1;
704  }
705 
706  return status;
707 }
708 
709 void
711 {
712  if (!palette) {
713  SDL_InvalidParamError("palette");
714  return;
715  }
716  if (--palette->refcount > 0) {
717  return;
718  }
719  SDL_free(palette->colors);
720  SDL_free(palette);
721 }
722 
723 /*
724  * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
725  */
726 void
728 {
729  int i;
730  if (bpp != 8)
731  return; /* only 8bpp supported right now */
732 
733  for (i = 0; i < 256; i++) {
734  int r, g, b;
735  /* map each bit field to the full [0, 255] interval,
736  so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
737  r = i & 0xe0;
738  r |= r >> 3 | r >> 6;
739  colors[i].r = r;
740  g = (i << 3) & 0xe0;
741  g |= g >> 3 | g >> 6;
742  colors[i].g = g;
743  b = i & 0x3;
744  b |= b << 2;
745  b |= b << 4;
746  colors[i].b = b;
747  colors[i].a = SDL_ALPHA_OPAQUE;
748  }
749 }
750 
751 /*
752  * Calculate the pad-aligned scanline width of a surface
753  */
754 int
756 {
757  int pitch;
758 
759  /* Surface should be 4-byte aligned for speed */
760  pitch = surface->w * surface->format->BytesPerPixel;
761  switch (surface->format->BitsPerPixel) {
762  case 1:
763  pitch = (pitch + 7) / 8;
764  break;
765  case 4:
766  pitch = (pitch + 1) / 2;
767  break;
768  default:
769  break;
770  }
771  pitch = (pitch + 3) & ~3; /* 4-byte aligning */
772  return (pitch);
773 }
774 
775 /*
776  * Match an RGB value to a particular palette index
777  */
778 Uint8
780 {
781  /* Do colorspace distance matching */
782  unsigned int smallest;
783  unsigned int distance;
784  int rd, gd, bd, ad;
785  int i;
786  Uint8 pixel = 0;
787 
788  smallest = ~0;
789  for (i = 0; i < pal->ncolors; ++i) {
790  rd = pal->colors[i].r - r;
791  gd = pal->colors[i].g - g;
792  bd = pal->colors[i].b - b;
793  ad = pal->colors[i].a - a;
794  distance = (rd * rd) + (gd * gd) + (bd * bd) + (ad * ad);
795  if (distance < smallest) {
796  pixel = i;
797  if (distance == 0) { /* Perfect match! */
798  break;
799  }
800  smallest = distance;
801  }
802  }
803  return (pixel);
804 }
805 
806 /* Find the opaque pixel value corresponding to an RGB triple */
807 Uint32
809 {
810  if (format->palette == NULL) {
811  return (r >> format->Rloss) << format->Rshift
812  | (g >> format->Gloss) << format->Gshift
813  | (b >> format->Bloss) << format->Bshift | format->Amask;
814  } else {
815  return SDL_FindColor(format->palette, r, g, b, SDL_ALPHA_OPAQUE);
816  }
817 }
818 
819 /* Find the pixel value corresponding to an RGBA quadruple */
820 Uint32
822  Uint8 a)
823 {
824  if (format->palette == NULL) {
825  return (r >> format->Rloss) << format->Rshift
826  | (g >> format->Gloss) << format->Gshift
827  | (b >> format->Bloss) << format->Bshift
828  | ((a >> format->Aloss) << format->Ashift & format->Amask);
829  } else {
830  return SDL_FindColor(format->palette, r, g, b, a);
831  }
832 }
833 
834 void
836  Uint8 * b)
837 {
838  if (format->palette == NULL) {
839  unsigned v;
840  v = (pixel & format->Rmask) >> format->Rshift;
841  *r = SDL_expand_byte[format->Rloss][v];
842  v = (pixel & format->Gmask) >> format->Gshift;
843  *g = SDL_expand_byte[format->Gloss][v];
844  v = (pixel & format->Bmask) >> format->Bshift;
845  *b = SDL_expand_byte[format->Bloss][v];
846  } else {
847  if (pixel < (unsigned)format->palette->ncolors) {
848  *r = format->palette->colors[pixel].r;
849  *g = format->palette->colors[pixel].g;
850  *b = format->palette->colors[pixel].b;
851  } else {
852  *r = *g = *b = 0;
853  }
854  }
855 }
856 
857 void
859  Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
860 {
861  if (format->palette == NULL) {
862  unsigned v;
863  v = (pixel & format->Rmask) >> format->Rshift;
864  *r = SDL_expand_byte[format->Rloss][v];
865  v = (pixel & format->Gmask) >> format->Gshift;
866  *g = SDL_expand_byte[format->Gloss][v];
867  v = (pixel & format->Bmask) >> format->Bshift;
868  *b = SDL_expand_byte[format->Bloss][v];
869  v = (pixel & format->Amask) >> format->Ashift;
870  *a = SDL_expand_byte[format->Aloss][v];
871  } else {
872  if (pixel < (unsigned)format->palette->ncolors) {
873  *r = format->palette->colors[pixel].r;
874  *g = format->palette->colors[pixel].g;
875  *b = format->palette->colors[pixel].b;
876  *a = format->palette->colors[pixel].a;
877  } else {
878  *r = *g = *b = *a = 0;
879  }
880  }
881 }
882 
883 /* Map from Palette to Palette */
884 static Uint8 *
885 Map1to1(SDL_Palette * src, SDL_Palette * dst, int *identical)
886 {
887  Uint8 *map;
888  int i;
889 
890  if (identical) {
891  if (src->ncolors <= dst->ncolors) {
892  /* If an identical palette, no need to map */
893  if (src == dst
894  ||
895  (SDL_memcmp
896  (src->colors, dst->colors,
897  src->ncolors * sizeof(SDL_Color)) == 0)) {
898  *identical = 1;
899  return (NULL);
900  }
901  }
902  *identical = 0;
903  }
904  map = (Uint8 *) SDL_malloc(src->ncolors);
905  if (map == NULL) {
906  SDL_OutOfMemory();
907  return (NULL);
908  }
909  for (i = 0; i < src->ncolors; ++i) {
910  map[i] = SDL_FindColor(dst,
911  src->colors[i].r, src->colors[i].g,
912  src->colors[i].b, src->colors[i].a);
913  }
914  return (map);
915 }
916 
917 /* Map from Palette to BitField */
918 static Uint8 *
919 Map1toN(SDL_PixelFormat * src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod,
921 {
922  Uint8 *map;
923  int i;
924  int bpp;
925  SDL_Palette *pal = src->palette;
926 
927  bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
928  map = (Uint8 *) SDL_malloc(pal->ncolors * bpp);
929  if (map == NULL) {
930  SDL_OutOfMemory();
931  return (NULL);
932  }
933 
934  /* We memory copy to the pixel map so the endianness is preserved */
935  for (i = 0; i < pal->ncolors; ++i) {
936  Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
937  Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
938  Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
939  Uint8 A = (Uint8) ((pal->colors[i].a * Amod) / 255);
940  ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
941  }
942  return (map);
943 }
944 
945 /* Map from BitField to Dithered-Palette to Palette */
946 static Uint8 *
948 {
949  /* Generate a 256 color dither palette */
950  SDL_Palette dithered;
951  SDL_Color colors[256];
952  SDL_Palette *pal = dst->palette;
953 
954  dithered.ncolors = 256;
955  SDL_DitherColors(colors, 8);
956  dithered.colors = colors;
957  return (Map1to1(&dithered, pal, identical));
958 }
959 
960 SDL_BlitMap *
962 {
963  SDL_BlitMap *map;
964 
965  /* Allocate the empty map */
966  map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
967  if (map == NULL) {
968  SDL_OutOfMemory();
969  return (NULL);
970  }
971  map->info.r = 0xFF;
972  map->info.g = 0xFF;
973  map->info.b = 0xFF;
974  map->info.a = 0xFF;
975 
976  /* It's ready to go */
977  return (map);
978 }
979 
980 void
982 {
983  if (!map) {
984  return;
985  }
986  if (map->dst) {
987  /* Release our reference to the surface - see the note below */
988  if (--map->dst->refcount <= 0) {
989  SDL_FreeSurface(map->dst);
990  }
991  }
992  map->dst = NULL;
993  map->src_palette_version = 0;
994  map->dst_palette_version = 0;
995  SDL_free(map->info.table);
996  map->info.table = NULL;
997 }
998 
999 int
1001 {
1002  SDL_PixelFormat *srcfmt;
1003  SDL_PixelFormat *dstfmt;
1004  SDL_BlitMap *map;
1005 
1006  /* Clear out any previous mapping */
1007  map = src->map;
1008  if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
1009  SDL_UnRLESurface(src, 1);
1010  }
1011  SDL_InvalidateMap(map);
1012 
1013  /* Figure out what kind of mapping we're doing */
1014  map->identity = 0;
1015  srcfmt = src->format;
1016  dstfmt = dst->format;
1017  if (SDL_ISPIXELFORMAT_INDEXED(srcfmt->format)) {
1018  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1019  /* Palette --> Palette */
1020  map->info.table =
1021  Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
1022  if (!map->identity) {
1023  if (map->info.table == NULL) {
1024  return (-1);
1025  }
1026  }
1027  if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
1028  map->identity = 0;
1029  } else {
1030  /* Palette --> BitField */
1031  map->info.table =
1032  Map1toN(srcfmt, src->map->info.r, src->map->info.g,
1033  src->map->info.b, src->map->info.a, dstfmt);
1034  if (map->info.table == NULL) {
1035  return (-1);
1036  }
1037  }
1038  } else {
1039  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1040  /* BitField --> Palette */
1041  map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
1042  if (!map->identity) {
1043  if (map->info.table == NULL) {
1044  return (-1);
1045  }
1046  }
1047  map->identity = 0; /* Don't optimize to copy */
1048  } else {
1049  /* BitField --> BitField */
1050  if (srcfmt == dstfmt) {
1051  map->identity = 1;
1052  }
1053  }
1054  }
1055 
1056  map->dst = dst;
1057 
1058  if (map->dst) {
1059  /* Keep a reference to this surface so it doesn't get deleted
1060  while we're still pointing at it.
1061 
1062  A better method would be for the destination surface to keep
1063  track of surfaces that are mapped to it and automatically
1064  invalidate them when it is freed, but this will do for now.
1065  */
1066  ++map->dst->refcount;
1067  }
1068 
1069  if (dstfmt->palette) {
1070  map->dst_palette_version = dstfmt->palette->version;
1071  } else {
1072  map->dst_palette_version = 0;
1073  }
1074 
1075  if (srcfmt->palette) {
1076  map->src_palette_version = srcfmt->palette->version;
1077  } else {
1078  map->src_palette_version = 0;
1079  }
1080 
1081  /* Choose your blitters wisely */
1082  return (SDL_CalculateBlit(src));
1083 }
1084 
1085 void
1087 {
1088  if (map) {
1089  SDL_InvalidateMap(map);
1090  SDL_free(map);
1091  }
1092 }
1093 
1094 void
1095 SDL_CalculateGammaRamp(float gamma, Uint16 * ramp)
1096 {
1097  int i;
1098 
1099  /* Input validation */
1100  if (gamma < 0.0f ) {
1101  SDL_InvalidParamError("gamma");
1102  return;
1103  }
1104  if (ramp == NULL) {
1105  SDL_InvalidParamError("ramp");
1106  return;
1107  }
1108 
1109  /* 0.0 gamma is all black */
1110  if (gamma == 0.0f) {
1111  SDL_memset(ramp, 0, 256 * sizeof(Uint16));
1112  return;
1113  } else if (gamma == 1.0f) {
1114  /* 1.0 gamma is identity */
1115  for (i = 0; i < 256; ++i) {
1116  ramp[i] = (i << 8) | i;
1117  }
1118  return;
1119  } else {
1120  /* Calculate a real gamma ramp */
1121  int value;
1122  gamma = 1.0f / gamma;
1123  for (i = 0; i < 256; ++i) {
1124  value =
1125  (int) (SDL_pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
1126  if (value > 65535) {
1127  value = 65535;
1128  }
1129  ramp[i] = (Uint16) value;
1130  }
1131  }
1132 }
1133 
1134 /* vi: set ts=4 sw=4 expandtab: */
static Uint8 lookup_5[]
Definition: SDL_pixels.c:55
Uint8 * table
Definition: SDL_blit.h:67
Uint8 r
Definition: SDL_blit.h:70
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Uint32 version
Definition: SDL_pixels.h:306
Uint8 b
Definition: SDL_blit.h:70
void SDL_DitherColors(SDL_Color *colors, int bpp)
Definition: SDL_pixels.c:727
void SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat *format, Uint8 *r, Uint8 *g, Uint8 *b)
Get the RGB components from a pixel of the specified format.
Definition: SDL_pixels.c:835
Uint8 g
Definition: SDL_pixels.h:296
const GLdouble * v
Definition: SDL_opengl.h:2064
GLenum GLenum dst
#define SDL_PIXELLAYOUT(X)
Definition: SDL_pixels.h:126
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
static SDL_PixelFormat * formats
Definition: SDL_pixels.c:492
Uint32 SDL_MapRGBA(const SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Maps an RGBA quadruple to a pixel value for a given pixel format.
Definition: SDL_pixels.c:821
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
Uint8 g
Definition: SDL_blit.h:70
void SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat *format, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
Get the RGBA components from a pixel of the specified format.
Definition: SDL_pixels.c:858
static Uint8 lookup_0[]
Definition: SDL_pixels.c:35
SDL_bool SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 *Rmask, Uint32 *Gmask, Uint32 *Bmask, Uint32 *Amask)
Convert one of the enumerated pixel formats to a bpp and RGBA masks.
Definition: SDL_pixels.c:134
EGLSurface surface
Definition: eglext.h:248
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)
Definition: SDL_blit.h:402
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
Uint32 SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Convert a bpp and RGBA masks to an enumerated pixel format.
Definition: SDL_pixels.c:293
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
GLfloat f
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:128
Uint32 dst_palette_version
Definition: SDL_blit.h:96
Uint8 b
Definition: SDL_pixels.h:297
static Uint8 lookup_4[]
Definition: SDL_pixels.c:51
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:169
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
GLenum src
static Uint8 lookup_2[]
Definition: SDL_pixels.c:43
static Uint8 lookup_7[]
Definition: SDL_pixels.c:63
#define SDL_zerop(x)
Definition: SDL_stdinc.h:370
GLsizei GLsizei GLfloat distance
Uint32 flags
Definition: SDL_surface.h:71
Uint32 src_palette_version
Definition: SDL_blit.h:97
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
int SDL_SetPaletteColors(SDL_Palette *palette, const SDL_Color *colors, int firstcolor, int ncolors)
Set a range of colors in a palette.
Definition: SDL_pixels.c:683
static Uint8 lookup_1[]
Definition: SDL_pixels.c:39
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void SDL_FreeFormat(SDL_PixelFormat *format)
Free an SDL_PixelFormat structure.
Definition: SDL_pixels.c:593
#define SDL_memcpy
void * SDL_calloc(size_t nmemb, size_t size)
Uint8 r
Definition: SDL_pixels.h:295
static Uint8 * Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
Definition: SDL_pixels.c:885
static Uint8 lookup_8[]
Definition: SDL_pixels.c:67
int SDL_MapSurface(SDL_Surface *src, SDL_Surface *dst)
Definition: SDL_pixels.c:1000
#define SDL_FreeSurface
Uint8 a
Definition: SDL_pixels.h:298
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:153
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
void SDL_free(void *mem)
SDL_PixelFormat * SDL_AllocFormat(Uint32 pixel_format)
Create an SDL_PixelFormat structure from a pixel format enum.
Definition: SDL_pixels.c:495
int SDL_SetPixelFormatPalette(SDL_PixelFormat *format, SDL_Palette *palette)
Set the palette for a pixel format structure.
Definition: SDL_pixels.c:655
Uint32 SDL_MapRGB(const SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b)
Maps an RGB triple to an opaque pixel value for a given pixel format.
Definition: SDL_pixels.c:808
Uint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_pixels.c:779
#define SDL_pow
#define SDL_memcmp
GLenum GLint GLuint mask
void SDL_FreePalette(SDL_Palette *palette)
Free a palette created with SDL_AllocPalette().
Definition: SDL_pixels.c:710
#define SDL_PIXELORDER(X)
Definition: SDL_pixels.h:125
GLsizei const GLfloat * value
void SDL_CalculateGammaRamp(float gamma, Uint16 *ramp)
Calculate a 256 entry gamma ramp for a gamma value.
Definition: SDL_pixels.c:1095
Uint8 * SDL_expand_byte[9]
Definition: SDL_pixels.c:71
int SDL_CalculateBlit(SDL_Surface *surface)
Definition: SDL_blit.c:216
int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format)
Definition: SDL_pixels.c:528
static Uint8 * MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
Definition: SDL_pixels.c:947
#define SDL_PIXELTYPE(X)
Definition: SDL_pixels.h:124
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_Palette * SDL_AllocPalette(int ncolors)
Create a palette structure with the specified number of color entries.
Definition: SDL_pixels.c:624
SDL_Surface * dst
Definition: SDL_blit.h:88
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:139
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
static Uint8 * Map1toN(SDL_PixelFormat *src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod, SDL_PixelFormat *dst)
Definition: SDL_pixels.c:919
#define G(x, y, z)
Definition: SDL_test_md5.c:74
SDL_BlitMap * SDL_AllocBlitMap(void)
Definition: SDL_pixels.c:961
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:161
Uint32 pixel_format
Definition: testoverlay2.c:152
int SDL_CalculatePitch(SDL_Surface *surface)
Definition: SDL_pixels.c:755
#define SDL_malloc
SDL_Palette * palette
Definition: SDL_pixels.h:316
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:167
const GLubyte GLuint GLuint GLuint GLuint alpha GLboolean GLboolean GLboolean GLboolean alpha GLint GLint GLsizei GLsizei GLenum type GLenum GLint GLenum GLint GLint GLsizei GLsizei GLint border GLenum GLint GLint GLint GLint GLint GLsizei GLsizei height GLsizei GLsizei GLenum GLenum const GLvoid *pixels GLenum GLint GLint GLint GLint j2 GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLfloat *params GLenum GLint GLenum GLenum GLvoid *pixels GLenum GLint GLenum GLint *params GLenum GLenum GLint *params GLenum GLsizei const GLvoid *pointer GLenum GLenum const GLint *params GLenum GLfloat GLfloat GLint GLint const GLfloat *points GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat *points GLint GLfloat GLfloat GLint GLfloat GLfloat v2 GLenum GLenum const GLint *params GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum map
Definition: SDL_glfuncs.h:290
static Uint8 lookup_3[]
Definition: SDL_pixels.c:47
static int colors[7]
Definition: testgesture.c:39
GLboolean GLboolean GLboolean GLboolean a
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
const char * SDL_GetPixelFormatName(Uint32 format)
Get the human readable name of a pixel format.
Definition: SDL_pixels.c:86
GLboolean GLboolean g
GLboolean GLboolean GLboolean b
void SDL_FreeBlitMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1086
int identity
Definition: SDL_blit.h:89
#define SDL_BITSPERPIXEL(X)
Definition: SDL_pixels.h:127
struct SDL_PixelFormat * next
Definition: SDL_pixels.h:333
SDL_BlitInfo info
Definition: SDL_blit.h:92
static Uint8 lookup_6[]
Definition: SDL_pixels.c:59
#define CASE(X)
#define SDL_memset
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint8 a
Definition: SDL_blit.h:70