src/video/SDL_pixels.c
changeset 5439 3a778c6c0269
parent 5438 b705640cb34a
child 5447 2a3cf6d7cac5
equal deleted inserted replaced
5438:b705640cb34a 5439:3a778c6c0269
    28 #include "SDL_sysvideo.h"
    28 #include "SDL_sysvideo.h"
    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 
       
    34 /* Lookup tables to expand partial bytes to the full 0..255 range */
       
    35 
       
    36 static Uint8 lookup_0[] = {
       
    37 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
       
    38 };
       
    39 
       
    40 static Uint8 lookup_1[] = {
       
    41 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
       
    42 };
       
    43 
       
    44 static Uint8 lookup_2[] = {
       
    45 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
       
    46 };
       
    47 
       
    48 static Uint8 lookup_3[] = {
       
    49 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
       
    50 };
       
    51 
       
    52 static Uint8 lookup_4[] = {
       
    53 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255
       
    54 };
       
    55 
       
    56 static Uint8 lookup_5[] = {
       
    57 0, 36, 72, 109, 145, 182, 218, 255
       
    58 };
       
    59 
       
    60 static Uint8 lookup_6[] = {
       
    61 0, 85, 170, 255
       
    62 };
       
    63 
       
    64 static Uint8 lookup_7[] = {
       
    65 0, 255
       
    66 };
       
    67 
       
    68 static Uint8 lookup_8[] = {
       
    69 255
       
    70 };
       
    71 
       
    72 Uint8* SDL_expand_byte[9] = {
       
    73     lookup_0,
       
    74     lookup_1,
       
    75     lookup_2,
       
    76     lookup_3,
       
    77     lookup_4,
       
    78     lookup_5,
       
    79     lookup_6,
       
    80     lookup_7,
       
    81     lookup_8
       
    82 };
    33 
    83 
    34 /* Helper functions */
    84 /* Helper functions */
    35 
    85 
    36 const char*
    86 const char*
    37 SDL_GetPixelFormatName(Uint32 format)
    87 SDL_GetPixelFormatName(Uint32 format)
   756 void
   806 void
   757 SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
   807 SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
   758            Uint8 * b)
   808            Uint8 * b)
   759 {
   809 {
   760     if (format->palette == NULL) {
   810     if (format->palette == NULL) {
   761         /*
       
   762          * This makes sure that the result is mapped to the
       
   763          * interval [0..255], and the maximum value for each
       
   764          * component is 255. This is important to make sure
       
   765          * that white is indeed reported as (255, 255, 255).
       
   766          * This only works for RGB bit fields at least 4 bit
       
   767          * wide, which is almost always the case.
       
   768          */
       
   769         unsigned v;
   811         unsigned v;
   770         v = (pixel & format->Rmask) >> format->Rshift;
   812         v = (pixel & format->Rmask) >> format->Rshift;
   771         *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
   813         *r = SDL_expand_byte[format->Rloss][v];
   772         v = (pixel & format->Gmask) >> format->Gshift;
   814         v = (pixel & format->Gmask) >> format->Gshift;
   773         *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
   815         *g = SDL_expand_byte[format->Gloss][v];
   774         v = (pixel & format->Bmask) >> format->Bshift;
   816         v = (pixel & format->Bmask) >> format->Bshift;
   775         *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
   817         *b = SDL_expand_byte[format->Bloss][v];
   776     } else {
   818     } else {
   777         if (pixel < format->palette->ncolors) {
   819         if (pixel < format->palette->ncolors) {
   778             *r = format->palette->colors[pixel].r;
   820             *r = format->palette->colors[pixel].r;
   779             *g = format->palette->colors[pixel].g;
   821             *g = format->palette->colors[pixel].g;
   780             *b = format->palette->colors[pixel].b;
   822             *b = format->palette->colors[pixel].b;
   787 void
   829 void
   788 SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * format,
   830 SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * format,
   789             Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
   831             Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
   790 {
   832 {
   791     if (format->palette == NULL) {
   833     if (format->palette == NULL) {
   792         /*
       
   793          * This makes sure that the result is mapped to the
       
   794          * interval [0..255], and the maximum value for each
       
   795          * component is 255. This is important to make sure
       
   796          * that white is indeed reported as (255, 255, 255),
       
   797          * and that opaque alpha is 255.
       
   798          * This only works for RGB bit fields at least 4 bit
       
   799          * wide, which is almost always the case.
       
   800          */
       
   801         unsigned v;
   834         unsigned v;
   802         v = (pixel & format->Rmask) >> format->Rshift;
   835         v = (pixel & format->Rmask) >> format->Rshift;
   803         *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
   836         *r = SDL_expand_byte[format->Rloss][v];
   804         v = (pixel & format->Gmask) >> format->Gshift;
   837         v = (pixel & format->Gmask) >> format->Gshift;
   805         *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
   838         *g = SDL_expand_byte[format->Gloss][v];
   806         v = (pixel & format->Bmask) >> format->Bshift;
   839         v = (pixel & format->Bmask) >> format->Bshift;
   807         *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
   840         *b = SDL_expand_byte[format->Bloss][v];
   808         if (format->Amask) {
   841         v = (pixel & format->Amask) >> format->Ashift;
   809             v = (pixel & format->Amask) >> format->Ashift;
   842         *a = SDL_expand_byte[format->Aloss][v];
   810             *a = (v << format->Aloss) + (v >> (8 - (format->Aloss << 1)));
       
   811         } else {
       
   812             *a = SDL_ALPHA_OPAQUE;
       
   813         }
       
   814     } else {
   843     } else {
   815         if (pixel < format->palette->ncolors) {
   844         if (pixel < format->palette->ncolors) {
   816             *r = format->palette->colors[pixel].r;
   845             *r = format->palette->colors[pixel].r;
   817             *g = format->palette->colors[pixel].g;
   846             *g = format->palette->colors[pixel].g;
   818             *b = format->palette->colors[pixel].b;
   847             *b = format->palette->colors[pixel].b;