src/video/SDL_pixels.c
changeset 7023 1d49dc7b5ce9
parent 6885 700f1b25f77f
child 7024 72cb3e205571
equal deleted inserted replaced
7022:d9f3d2e3bd07 7023:1d49dc7b5ce9
   760 
   760 
   761 /*
   761 /*
   762  * Match an RGB value to a particular palette index
   762  * Match an RGB value to a particular palette index
   763  */
   763  */
   764 Uint8
   764 Uint8
   765 SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b)
   765 SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
   766 {
   766 {
   767     /* Do colorspace distance matching */
   767     /* Do colorspace distance matching */
   768     unsigned int smallest;
   768     unsigned int smallest;
   769     unsigned int distance;
   769     unsigned int distance;
   770     int rd, gd, bd;
   770     int rd, gd, bd, ad;
   771     int i;
   771     int i;
   772     Uint8 pixel = 0;
   772     Uint8 pixel = 0;
   773 
   773 
   774     smallest = ~0;
   774     smallest = ~0;
   775     for (i = 0; i < pal->ncolors; ++i) {
   775     for (i = 0; i < pal->ncolors; ++i) {
   776         rd = pal->colors[i].r - r;
   776         rd = pal->colors[i].r - r;
   777         gd = pal->colors[i].g - g;
   777         gd = pal->colors[i].g - g;
   778         bd = pal->colors[i].b - b;
   778         bd = pal->colors[i].b - b;
   779         distance = (rd * rd) + (gd * gd) + (bd * bd);
   779         ad = pal->colors[i].unused - a;
       
   780         distance = (rd * rd) + (gd * gd) + (bd * bd) + (ad * ad);
   780         if (distance < smallest) {
   781         if (distance < smallest) {
   781             pixel = i;
   782             pixel = i;
   782             if (distance == 0) {        /* Perfect match! */
   783             if (distance == 0) {        /* Perfect match! */
   783                 break;
   784                 break;
   784             }
   785             }
   795     if (format->palette == NULL) {
   796     if (format->palette == NULL) {
   796         return (r >> format->Rloss) << format->Rshift
   797         return (r >> format->Rloss) << format->Rshift
   797             | (g >> format->Gloss) << format->Gshift
   798             | (g >> format->Gloss) << format->Gshift
   798             | (b >> format->Bloss) << format->Bshift | format->Amask;
   799             | (b >> format->Bloss) << format->Bshift | format->Amask;
   799     } else {
   800     } else {
   800         return SDL_FindColor(format->palette, r, g, b);
   801         return SDL_FindColor(format->palette, r, g, b, SDL_ALPHA_OPAQUE);
   801     }
   802     }
   802 }
   803 }
   803 
   804 
   804 /* Find the pixel value corresponding to an RGBA quadruple */
   805 /* Find the pixel value corresponding to an RGBA quadruple */
   805 Uint32
   806 Uint32
   810         return (r >> format->Rloss) << format->Rshift
   811         return (r >> format->Rloss) << format->Rshift
   811             | (g >> format->Gloss) << format->Gshift
   812             | (g >> format->Gloss) << format->Gshift
   812             | (b >> format->Bloss) << format->Bshift
   813             | (b >> format->Bloss) << format->Bshift
   813             | ((a >> format->Aloss) << format->Ashift & format->Amask);
   814             | ((a >> format->Aloss) << format->Ashift & format->Amask);
   814     } else {
   815     } else {
   815         return SDL_FindColor(format->palette, r, g, b);
   816         return SDL_FindColor(format->palette, r, g, b, a);
   816     }
   817     }
   817 }
   818 }
   818 
   819 
   819 void
   820 void
   820 SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
   821 SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
   856     } else {
   857     } else {
   857         if (pixel < (unsigned)format->palette->ncolors) {
   858         if (pixel < (unsigned)format->palette->ncolors) {
   858             *r = format->palette->colors[pixel].r;
   859             *r = format->palette->colors[pixel].r;
   859             *g = format->palette->colors[pixel].g;
   860             *g = format->palette->colors[pixel].g;
   860             *b = format->palette->colors[pixel].b;
   861             *b = format->palette->colors[pixel].b;
   861             *a = SDL_ALPHA_OPAQUE;
   862             *a = format->palette->colors[pixel].unused;
   862         } else {
   863         } else {
   863             *r = *g = *b = *a = 0;
   864             *r = *g = *b = *a = 0;
   864         }
   865         }
   865     }
   866     }
   866 }
   867 }
   892         return (NULL);
   893         return (NULL);
   893     }
   894     }
   894     for (i = 0; i < src->ncolors; ++i) {
   895     for (i = 0; i < src->ncolors; ++i) {
   895         map[i] = SDL_FindColor(dst,
   896         map[i] = SDL_FindColor(dst,
   896                                src->colors[i].r, src->colors[i].g,
   897                                src->colors[i].r, src->colors[i].g,
   897                                src->colors[i].b);
   898                                src->colors[i].b, src->colors[i].unused);
   898     }
   899     }
   899     return (map);
   900     return (map);
   900 }
   901 }
   901 
   902 
   902 /* Map from Palette to BitField */
   903 /* Map from Palette to BitField */
   916         return (NULL);
   917         return (NULL);
   917     }
   918     }
   918 
   919 
   919     /* We memory copy to the pixel map so the endianness is preserved */
   920     /* We memory copy to the pixel map so the endianness is preserved */
   920     for (i = 0; i < pal->ncolors; ++i) {
   921     for (i = 0; i < pal->ncolors; ++i) {
   921         Uint8 A = Amod;
       
   922         Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
   922         Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
   923         Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
   923         Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
   924         Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
   924         Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
       
   925         Uint8 A = (Uint8) ((pal->colors[i].unused * Amod) / 255);
   925         ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
   926         ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
   926     }
   927     }
   927     return (map);
   928     return (map);
   928 }
   929 }
   929 
   930