src/video/SDL_pixels.c
author Sam Lantinga
Wed, 23 Dec 2009 06:39:18 +0000
changeset 3597 7dec3fe9b580
parent 3557 7184580ab756
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #912

SDL_MasksToPixelFormatEnum() wasn't correctly returning RGB24 formats
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* General (mostly internal) pixel/color manipulation routines for SDL */
    25 
    26 #include "SDL_endian.h"
    27 #include "SDL_video.h"
    28 #include "SDL_sysvideo.h"
    29 #include "SDL_blit.h"
    30 #include "SDL_pixels_c.h"
    31 #include "SDL_RLEaccel_c.h"
    32 
    33 struct SDL_PaletteWatch
    34 {
    35     SDL_PaletteChangedFunc callback;
    36     void *userdata;
    37     struct SDL_PaletteWatch *next;
    38 };
    39 
    40 /* Helper functions */
    41 
    42 SDL_bool
    43 SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 * Rmask,
    44                            Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask)
    45 {
    46     Uint32 masks[4];
    47 
    48     /* Initialize the values here */
    49     if (SDL_BYTESPERPIXEL(format) <= 2) {
    50         *bpp = SDL_BITSPERPIXEL(format);
    51     } else {
    52         *bpp = SDL_BYTESPERPIXEL(format) * 8;
    53     }
    54     *Rmask = *Gmask = *Bmask = *Amask = 0;
    55 
    56     if (format == SDL_PIXELFORMAT_RGB24) {
    57 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    58         *Rmask = 0x00FF0000;
    59         *Gmask = 0x0000FF00;
    60         *Bmask = 0x000000FF;
    61 #else
    62         *Rmask = 0x000000FF;
    63         *Gmask = 0x0000FF00;
    64         *Bmask = 0x00FF0000;
    65 #endif
    66         return SDL_TRUE;
    67     }
    68 
    69     if (format == SDL_PIXELFORMAT_BGR24) {
    70 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    71         *Rmask = 0x000000FF;
    72         *Gmask = 0x0000FF00;
    73         *Bmask = 0x00FF0000;
    74 #else
    75         *Rmask = 0x00FF0000;
    76         *Gmask = 0x0000FF00;
    77         *Bmask = 0x000000FF;
    78 #endif
    79         return SDL_TRUE;
    80     }
    81 
    82     if (SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED8 &&
    83         SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED16 &&
    84         SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED32) {
    85         /* Not a format that uses masks */
    86         return SDL_TRUE;
    87     }
    88 
    89     switch (SDL_PIXELLAYOUT(format)) {
    90     case SDL_PACKEDLAYOUT_332:
    91         masks[0] = 0x00000000;
    92         masks[1] = 0x000000E0;
    93         masks[2] = 0x0000001C;
    94         masks[3] = 0x00000003;
    95         break;
    96     case SDL_PACKEDLAYOUT_4444:
    97         masks[0] = 0x0000F000;
    98         masks[1] = 0x00000F00;
    99         masks[2] = 0x000000F0;
   100         masks[3] = 0x0000000F;
   101         break;
   102     case SDL_PACKEDLAYOUT_1555:
   103         masks[0] = 0x00008000;
   104         masks[1] = 0x00007C00;
   105         masks[2] = 0x000003E0;
   106         masks[3] = 0x0000001F;
   107         break;
   108     case SDL_PACKEDLAYOUT_5551:
   109         masks[0] = 0x0000F800;
   110         masks[1] = 0x000007C0;
   111         masks[2] = 0x0000003E;
   112         masks[3] = 0x00000001;
   113         break;
   114     case SDL_PACKEDLAYOUT_565:
   115         masks[0] = 0x00000000;
   116         masks[1] = 0x0000F800;
   117         masks[2] = 0x000007E0;
   118         masks[3] = 0x0000001F;
   119         break;
   120     case SDL_PACKEDLAYOUT_8888:
   121         masks[0] = 0xFF000000;
   122         masks[1] = 0x00FF0000;
   123         masks[2] = 0x0000FF00;
   124         masks[3] = 0x000000FF;
   125         break;
   126     case SDL_PACKEDLAYOUT_2101010:
   127         masks[0] = 0xC0000000;
   128         masks[1] = 0x3FF00000;
   129         masks[2] = 0x000FFC00;
   130         masks[3] = 0x000003FF;
   131         break;
   132     case SDL_PACKEDLAYOUT_1010102:
   133         masks[0] = 0xFFC00000;
   134         masks[1] = 0x003FF000;
   135         masks[2] = 0x00000FFC;
   136         masks[3] = 0x00000003;
   137         break;
   138     default:
   139         SDL_SetError("Unknown pixel format");
   140         return SDL_FALSE;
   141     }
   142 
   143     switch (SDL_PIXELORDER(format)) {
   144     case SDL_PACKEDORDER_XRGB:
   145         *Rmask = masks[1];
   146         *Gmask = masks[2];
   147         *Bmask = masks[3];
   148         break;
   149     case SDL_PACKEDORDER_RGBX:
   150         *Rmask = masks[0];
   151         *Gmask = masks[1];
   152         *Bmask = masks[2];
   153         break;
   154     case SDL_PACKEDORDER_ARGB:
   155         *Amask = masks[0];
   156         *Rmask = masks[1];
   157         *Gmask = masks[2];
   158         *Bmask = masks[3];
   159         break;
   160     case SDL_PACKEDORDER_RGBA:
   161         *Rmask = masks[0];
   162         *Gmask = masks[1];
   163         *Bmask = masks[2];
   164         *Amask = masks[3];
   165         break;
   166     case SDL_PACKEDORDER_XBGR:
   167         *Bmask = masks[1];
   168         *Gmask = masks[2];
   169         *Rmask = masks[3];
   170         break;
   171     case SDL_PACKEDORDER_BGRX:
   172         *Bmask = masks[0];
   173         *Gmask = masks[1];
   174         *Rmask = masks[2];
   175         break;
   176     case SDL_PACKEDORDER_BGRA:
   177         *Bmask = masks[0];
   178         *Gmask = masks[1];
   179         *Rmask = masks[2];
   180         *Amask = masks[3];
   181         break;
   182     case SDL_PACKEDORDER_ABGR:
   183         *Amask = masks[0];
   184         *Bmask = masks[1];
   185         *Gmask = masks[2];
   186         *Rmask = masks[3];
   187         break;
   188     default:
   189         SDL_SetError("Unknown pixel format");
   190         return SDL_FALSE;
   191     }
   192     return SDL_TRUE;
   193 }
   194 
   195 Uint32
   196 SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
   197                            Uint32 Amask)
   198 {
   199     switch (bpp) {
   200     case 8:
   201         switch (Rmask) {
   202         case 0:
   203             return SDL_PIXELFORMAT_INDEX8;
   204         case 0xE0:
   205             return SDL_PIXELFORMAT_RGB332;
   206         }
   207         break;
   208     case 12:
   209         switch (Rmask) {
   210         case 0x0F00:
   211             return SDL_PIXELFORMAT_RGB444;
   212         }
   213         break;
   214     case 15:
   215         switch (Rmask) {
   216         case 0x001F:
   217             return SDL_PIXELFORMAT_BGR555;
   218         case 0x7C00:
   219             return SDL_PIXELFORMAT_RGB555;
   220         }
   221         break;
   222     case 16:
   223         switch (Rmask) {
   224         case 0x000F:
   225             return SDL_PIXELFORMAT_ABGR4444;
   226         case 0x001F:
   227             if (Gmask == 0x07E0) {
   228                 return SDL_PIXELFORMAT_BGR565;
   229             }
   230             return SDL_PIXELFORMAT_ABGR1555;
   231         case 0x0F00:
   232             return SDL_PIXELFORMAT_ARGB4444;
   233         case 0x7C00:
   234             return SDL_PIXELFORMAT_ARGB1555;
   235         case 0xF800:
   236             return SDL_PIXELFORMAT_RGB565;
   237         }
   238         break;
   239     case 24:
   240         switch (Rmask) {
   241         case 0x00FF0000:
   242 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   243             return SDL_PIXELFORMAT_RGB24;
   244 #else
   245             return SDL_PIXELFORMAT_BGR24;
   246 #endif
   247         case 0x000000FF:
   248 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
   249             return SDL_PIXELFORMAT_BGR24;
   250 #else
   251             return SDL_PIXELFORMAT_RGB24;
   252 #endif
   253         case 0x00000000:
   254             /* FIXME: At this point we can't distinguish */
   255             /* if this format is RGB24 or BGR24          */
   256             return SDL_PIXELFORMAT_RGB24;
   257         }
   258     case 32:
   259         switch (Rmask) {
   260         case 0xFF000000:
   261             if (Amask == 0x000000FF) {
   262                 return SDL_PIXELFORMAT_RGBA8888;
   263             }
   264             break;
   265         case 0x00FF0000:
   266             if (Amask == 0xFF000000) {
   267                 return SDL_PIXELFORMAT_ARGB8888;
   268             } else {
   269                 return SDL_PIXELFORMAT_RGB888;
   270             }
   271             break;
   272         case 0x0000FF00:
   273             if (Amask == 0x000000FF) {
   274                 return SDL_PIXELFORMAT_BGRA8888;
   275             }
   276             break;
   277         case 0x000000FF:
   278             if (Amask == 0xFF000000) {
   279                 return SDL_PIXELFORMAT_ABGR8888;
   280             } else {
   281                 return SDL_PIXELFORMAT_BGR888;
   282             }
   283             break;
   284         case 0x3FF00000:
   285             return SDL_PIXELFORMAT_ARGB2101010;
   286         }
   287     }
   288     return SDL_PIXELFORMAT_UNKNOWN;
   289 }
   290 
   291 
   292 SDL_Palette *
   293 SDL_AllocPalette(int ncolors)
   294 {
   295     SDL_Palette *palette;
   296 
   297     palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
   298     if (!palette) {
   299         SDL_OutOfMemory();
   300         return NULL;
   301     }
   302     palette->colors =
   303         (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
   304     if (!palette->colors) {
   305         SDL_free(palette);
   306         return NULL;
   307     }
   308     palette->ncolors = ncolors;
   309     palette->watch = NULL;
   310     palette->refcount = 1;
   311 
   312     SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
   313 
   314     return palette;
   315 }
   316 
   317 int
   318 SDL_AddPaletteWatch(SDL_Palette * palette, SDL_PaletteChangedFunc callback,
   319                     void *userdata)
   320 {
   321     SDL_PaletteWatch *watch;
   322 
   323     if (!palette) {
   324         return -1;
   325     }
   326 
   327     watch = (SDL_PaletteWatch *) SDL_malloc(sizeof(*watch));
   328     if (!watch) {
   329         SDL_OutOfMemory();
   330         return -1;
   331     }
   332 
   333     watch->callback = callback;
   334     watch->userdata = userdata;
   335     watch->next = palette->watch;
   336     palette->watch = watch;
   337     ++palette->refcount;
   338     return 0;
   339 }
   340 
   341 void
   342 SDL_DelPaletteWatch(SDL_Palette * palette, SDL_PaletteChangedFunc callback,
   343                     void *userdata)
   344 {
   345     SDL_PaletteWatch *prev, *watch;
   346 
   347     if (!palette) {
   348         return;
   349     }
   350 
   351     for (prev = NULL, watch = palette->watch; watch;
   352          prev = watch, watch = watch->next) {
   353         if (watch->callback == callback && watch->userdata == userdata) {
   354             if (prev) {
   355                 prev->next = watch->next;
   356             } else {
   357                 palette->watch = watch->next;
   358             }
   359             SDL_free(watch);
   360             SDL_FreePalette(palette);
   361             return;
   362         }
   363     }
   364 }
   365 
   366 int
   367 SDL_SetPaletteColors(SDL_Palette * palette, const SDL_Color * colors,
   368                      int firstcolor, int ncolors)
   369 {
   370     SDL_PaletteWatch *watch;
   371     int status = 0;
   372 
   373     /* Verify the parameters */
   374     if (!palette) {
   375         return -1;
   376     }
   377     if (ncolors > (palette->ncolors - firstcolor)) {
   378         ncolors = (palette->ncolors - firstcolor);
   379         status = -1;
   380     }
   381 
   382     if (colors != (palette->colors + firstcolor)) {
   383         SDL_memcpy(palette->colors + firstcolor, colors,
   384                    ncolors * sizeof(*colors));
   385     }
   386 
   387     for (watch = palette->watch; watch; watch = watch->next) {
   388         if (watch->callback(watch->userdata, palette) < 0) {
   389             status = -1;
   390         }
   391     }
   392 
   393     return status;
   394 }
   395 
   396 void
   397 SDL_FreePalette(SDL_Palette * palette)
   398 {
   399     if (!palette) {
   400         return;
   401     }
   402     if (--palette->refcount > 0) {
   403         return;
   404     }
   405     if (palette->colors) {
   406         SDL_free(palette->colors);
   407     }
   408     SDL_free(palette);
   409 }
   410 
   411 /*
   412  * Allocate a pixel format structure and fill it according to the given info.
   413  */
   414 SDL_PixelFormat *
   415 SDL_AllocFormat(int bpp,
   416                 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
   417 {
   418     SDL_PixelFormat *format;
   419 
   420     /* Allocate an empty pixel format structure */
   421     format = SDL_malloc(sizeof(*format));
   422     if (format == NULL) {
   423         SDL_OutOfMemory();
   424         return (NULL);
   425     }
   426 
   427     /* Set up the format */
   428     return SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
   429 }
   430 
   431 SDL_PixelFormat *
   432 SDL_InitFormat(SDL_PixelFormat * format, int bpp, Uint32 Rmask, Uint32 Gmask,
   433                Uint32 Bmask, Uint32 Amask)
   434 {
   435     Uint32 mask;
   436 
   437     /* Set up the format */
   438     SDL_zerop(format);
   439     format->BitsPerPixel = bpp;
   440     format->BytesPerPixel = (bpp + 7) / 8;
   441     if (Rmask || Bmask || Gmask) {      /* Packed pixels with custom mask */
   442         format->Rshift = 0;
   443         format->Rloss = 8;
   444         if (Rmask) {
   445             for (mask = Rmask; !(mask & 0x01); mask >>= 1)
   446                 ++format->Rshift;
   447             for (; (mask & 0x01); mask >>= 1)
   448                 --format->Rloss;
   449         }
   450         format->Gshift = 0;
   451         format->Gloss = 8;
   452         if (Gmask) {
   453             for (mask = Gmask; !(mask & 0x01); mask >>= 1)
   454                 ++format->Gshift;
   455             for (; (mask & 0x01); mask >>= 1)
   456                 --format->Gloss;
   457         }
   458         format->Bshift = 0;
   459         format->Bloss = 8;
   460         if (Bmask) {
   461             for (mask = Bmask; !(mask & 0x01); mask >>= 1)
   462                 ++format->Bshift;
   463             for (; (mask & 0x01); mask >>= 1)
   464                 --format->Bloss;
   465         }
   466         format->Ashift = 0;
   467         format->Aloss = 8;
   468         if (Amask) {
   469             for (mask = Amask; !(mask & 0x01); mask >>= 1)
   470                 ++format->Ashift;
   471             for (; (mask & 0x01); mask >>= 1)
   472                 --format->Aloss;
   473         }
   474         format->Rmask = Rmask;
   475         format->Gmask = Gmask;
   476         format->Bmask = Bmask;
   477         format->Amask = Amask;
   478     } else if (bpp > 8) {       /* Packed pixels with standard mask */
   479         /* R-G-B */
   480         if (bpp > 24)
   481             bpp = 24;
   482         format->Rloss = 8 - (bpp / 3);
   483         format->Gloss = 8 - (bpp / 3) - (bpp % 3);
   484         format->Bloss = 8 - (bpp / 3);
   485         format->Rshift = ((bpp / 3) + (bpp % 3)) + (bpp / 3);
   486         format->Gshift = (bpp / 3);
   487         format->Bshift = 0;
   488         format->Rmask = ((0xFF >> format->Rloss) << format->Rshift);
   489         format->Gmask = ((0xFF >> format->Gloss) << format->Gshift);
   490         format->Bmask = ((0xFF >> format->Bloss) << format->Bshift);
   491     } else {
   492         /* Palettized formats have no mask info */
   493         format->Rloss = 8;
   494         format->Gloss = 8;
   495         format->Bloss = 8;
   496         format->Aloss = 8;
   497         format->Rshift = 0;
   498         format->Gshift = 0;
   499         format->Bshift = 0;
   500         format->Ashift = 0;
   501         format->Rmask = 0;
   502         format->Gmask = 0;
   503         format->Bmask = 0;
   504         format->Amask = 0;
   505     }
   506     format->palette = NULL;
   507 
   508     return format;
   509 }
   510 
   511 /*
   512  * Change any previous mappings from/to the new surface format
   513  */
   514 void
   515 SDL_FormatChanged(SDL_Surface * surface)
   516 {
   517     static int format_version = 0;
   518     ++format_version;
   519     if (format_version < 0) {   /* It wrapped... */
   520         format_version = 1;
   521     }
   522     surface->format_version = format_version;
   523     SDL_InvalidateMap(surface->map);
   524 }
   525 
   526 /*
   527  * Free a previously allocated format structure
   528  */
   529 void
   530 SDL_FreeFormat(SDL_PixelFormat * format)
   531 {
   532     if (!format) {
   533         return;
   534     }
   535     SDL_free(format);
   536 }
   537 
   538 /*
   539  * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
   540  */
   541 void
   542 SDL_DitherColors(SDL_Color * colors, int bpp)
   543 {
   544     int i;
   545     if (bpp != 8)
   546         return;                 /* only 8bpp supported right now */
   547 
   548     for (i = 0; i < 256; i++) {
   549         int r, g, b;
   550         /* map each bit field to the full [0, 255] interval,
   551            so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
   552         r = i & 0xe0;
   553         r |= r >> 3 | r >> 6;
   554         colors[i].r = r;
   555         g = (i << 3) & 0xe0;
   556         g |= g >> 3 | g >> 6;
   557         colors[i].g = g;
   558         b = i & 0x3;
   559         b |= b << 2;
   560         b |= b << 4;
   561         colors[i].b = b;
   562         colors[i].unused = SDL_ALPHA_OPAQUE;
   563     }
   564 }
   565 
   566 /* 
   567  * Calculate the pad-aligned scanline width of a surface
   568  */
   569 int
   570 SDL_CalculatePitch(SDL_Surface * surface)
   571 {
   572     int pitch;
   573 
   574     /* Surface should be 4-byte aligned for speed */
   575     pitch = surface->w * surface->format->BytesPerPixel;
   576     switch (surface->format->BitsPerPixel) {
   577     case 1:
   578         pitch = (pitch + 7) / 8;
   579         break;
   580     case 4:
   581         pitch = (pitch + 1) / 2;
   582         break;
   583     default:
   584         break;
   585     }
   586     pitch = (pitch + 3) & ~3;   /* 4-byte aligning */
   587     return (pitch);
   588 }
   589 
   590 /*
   591  * Match an RGB value to a particular palette index
   592  */
   593 Uint8
   594 SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b)
   595 {
   596     /* Do colorspace distance matching */
   597     unsigned int smallest;
   598     unsigned int distance;
   599     int rd, gd, bd;
   600     int i;
   601     Uint8 pixel = 0;
   602 
   603     smallest = ~0;
   604     for (i = 0; i < pal->ncolors; ++i) {
   605         rd = pal->colors[i].r - r;
   606         gd = pal->colors[i].g - g;
   607         bd = pal->colors[i].b - b;
   608         distance = (rd * rd) + (gd * gd) + (bd * bd);
   609         if (distance < smallest) {
   610             pixel = i;
   611             if (distance == 0) {        /* Perfect match! */
   612                 break;
   613             }
   614             smallest = distance;
   615         }
   616     }
   617     return (pixel);
   618 }
   619 
   620 /* Find the opaque pixel value corresponding to an RGB triple */
   621 Uint32
   622 SDL_MapRGB(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b)
   623 {
   624     if (format->palette == NULL) {
   625         return (r >> format->Rloss) << format->Rshift
   626             | (g >> format->Gloss) << format->Gshift
   627             | (b >> format->Bloss) << format->Bshift | format->Amask;
   628     } else {
   629         return SDL_FindColor(format->palette, r, g, b);
   630     }
   631 }
   632 
   633 /* Find the pixel value corresponding to an RGBA quadruple */
   634 Uint32
   635 SDL_MapRGBA(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b,
   636             Uint8 a)
   637 {
   638     if (format->palette == NULL) {
   639         return (r >> format->Rloss) << format->Rshift
   640             | (g >> format->Gloss) << format->Gshift
   641             | (b >> format->Bloss) << format->Bshift
   642             | ((a >> format->Aloss) << format->Ashift & format->Amask);
   643     } else {
   644         return SDL_FindColor(format->palette, r, g, b);
   645     }
   646 }
   647 
   648 void
   649 SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
   650            Uint8 * b)
   651 {
   652     if (format->palette == NULL) {
   653         /*
   654          * This makes sure that the result is mapped to the
   655          * interval [0..255], and the maximum value for each
   656          * component is 255. This is important to make sure
   657          * that white is indeed reported as (255, 255, 255).
   658          * This only works for RGB bit fields at least 4 bit
   659          * wide, which is almost always the case.
   660          */
   661         unsigned v;
   662         v = (pixel & format->Rmask) >> format->Rshift;
   663         *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
   664         v = (pixel & format->Gmask) >> format->Gshift;
   665         *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
   666         v = (pixel & format->Bmask) >> format->Bshift;
   667         *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
   668     } else {
   669         *r = format->palette->colors[pixel].r;
   670         *g = format->palette->colors[pixel].g;
   671         *b = format->palette->colors[pixel].b;
   672     }
   673 }
   674 
   675 void
   676 SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * format,
   677             Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
   678 {
   679     if (format->palette == NULL) {
   680         /*
   681          * This makes sure that the result is mapped to the
   682          * interval [0..255], and the maximum value for each
   683          * component is 255. This is important to make sure
   684          * that white is indeed reported as (255, 255, 255),
   685          * and that opaque alpha is 255.
   686          * This only works for RGB bit fields at least 4 bit
   687          * wide, which is almost always the case.
   688          */
   689         unsigned v;
   690         v = (pixel & format->Rmask) >> format->Rshift;
   691         *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
   692         v = (pixel & format->Gmask) >> format->Gshift;
   693         *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
   694         v = (pixel & format->Bmask) >> format->Bshift;
   695         *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
   696         if (format->Amask) {
   697             v = (pixel & format->Amask) >> format->Ashift;
   698             *a = (v << format->Aloss) + (v >> (8 - (format->Aloss << 1)));
   699         } else {
   700             *a = SDL_ALPHA_OPAQUE;
   701         }
   702     } else {
   703         *r = format->palette->colors[pixel].r;
   704         *g = format->palette->colors[pixel].g;
   705         *b = format->palette->colors[pixel].b;
   706         *a = SDL_ALPHA_OPAQUE;
   707     }
   708 }
   709 
   710 /* Apply gamma to a set of colors - this is easy. :) */
   711 void
   712 SDL_ApplyGamma(Uint16 * gamma, SDL_Color * colors, SDL_Color * output,
   713                int ncolors)
   714 {
   715     int i;
   716 
   717     for (i = 0; i < ncolors; ++i) {
   718         output[i].r = gamma[0 * 256 + colors[i].r] >> 8;
   719         output[i].g = gamma[1 * 256 + colors[i].g] >> 8;
   720         output[i].b = gamma[2 * 256 + colors[i].b] >> 8;
   721     }
   722 }
   723 
   724 /* Map from Palette to Palette */
   725 static Uint8 *
   726 Map1to1(SDL_Palette * src, SDL_Palette * dst, int *identical)
   727 {
   728     Uint8 *map;
   729     int i;
   730 
   731     if (identical) {
   732         if (src->ncolors <= dst->ncolors) {
   733             /* If an identical palette, no need to map */
   734             if (src == dst
   735                 ||
   736                 (SDL_memcmp
   737                  (src->colors, dst->colors,
   738                   src->ncolors * sizeof(SDL_Color)) == 0)) {
   739                 *identical = 1;
   740                 return (NULL);
   741             }
   742         }
   743         *identical = 0;
   744     }
   745     map = (Uint8 *) SDL_malloc(src->ncolors);
   746     if (map == NULL) {
   747         SDL_OutOfMemory();
   748         return (NULL);
   749     }
   750     for (i = 0; i < src->ncolors; ++i) {
   751         map[i] = SDL_FindColor(dst,
   752                                src->colors[i].r, src->colors[i].g,
   753                                src->colors[i].b);
   754     }
   755     return (map);
   756 }
   757 
   758 /* Map from Palette to BitField */
   759 static Uint8 *
   760 Map1toN(SDL_PixelFormat * src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod,
   761         SDL_PixelFormat * dst)
   762 {
   763     Uint8 *map;
   764     int i;
   765     int bpp;
   766     SDL_Palette *pal = src->palette;
   767 
   768     bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
   769     map = (Uint8 *) SDL_malloc(pal->ncolors * bpp);
   770     if (map == NULL) {
   771         SDL_OutOfMemory();
   772         return (NULL);
   773     }
   774 
   775     /* We memory copy to the pixel map so the endianness is preserved */
   776     for (i = 0; i < pal->ncolors; ++i) {
   777         Uint8 A = Amod;
   778         Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
   779         Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
   780         Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
   781         ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
   782     }
   783     return (map);
   784 }
   785 
   786 /* Map from BitField to Dithered-Palette to Palette */
   787 static Uint8 *
   788 MapNto1(SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical)
   789 {
   790     /* Generate a 256 color dither palette */
   791     SDL_Palette dithered;
   792     SDL_Color colors[256];
   793     SDL_Palette *pal = dst->palette;
   794 
   795     dithered.ncolors = 256;
   796     SDL_DitherColors(colors, 8);
   797     dithered.colors = colors;
   798     return (Map1to1(&dithered, pal, identical));
   799 }
   800 
   801 SDL_BlitMap *
   802 SDL_AllocBlitMap(void)
   803 {
   804     SDL_BlitMap *map;
   805 
   806     /* Allocate the empty map */
   807     map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
   808     if (map == NULL) {
   809         SDL_OutOfMemory();
   810         return (NULL);
   811     }
   812     map->info.r = 0xFF;
   813     map->info.g = 0xFF;
   814     map->info.b = 0xFF;
   815     map->info.a = 0xFF;
   816 
   817     /* It's ready to go */
   818     return (map);
   819 }
   820 
   821 void
   822 SDL_InvalidateMap(SDL_BlitMap * map)
   823 {
   824     if (!map) {
   825         return;
   826     }
   827     map->dst = NULL;
   828     map->format_version = (unsigned int) -1;
   829     if (map->info.table) {
   830         SDL_free(map->info.table);
   831         map->info.table = NULL;
   832     }
   833 }
   834 
   835 int
   836 SDL_MapSurface(SDL_Surface * src, SDL_Surface * dst)
   837 {
   838     SDL_PixelFormat *srcfmt;
   839     SDL_PixelFormat *dstfmt;
   840     SDL_BlitMap *map;
   841 
   842     /* Clear out any previous mapping */
   843     map = src->map;
   844     if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
   845         SDL_UnRLESurface(src, 1);
   846     }
   847     SDL_InvalidateMap(map);
   848 
   849     /* Figure out what kind of mapping we're doing */
   850     map->identity = 0;
   851     srcfmt = src->format;
   852     dstfmt = dst->format;
   853     switch (srcfmt->BytesPerPixel) {
   854     case 1:
   855         switch (dstfmt->BytesPerPixel) {
   856         case 1:
   857             /* Palette --> Palette */
   858             map->info.table =
   859                 Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
   860             if (!map->identity) {
   861                 if (map->info.table == NULL) {
   862                     return (-1);
   863                 }
   864             }
   865             if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
   866                 map->identity = 0;
   867             break;
   868 
   869         default:
   870             /* Palette --> BitField */
   871             map->info.table =
   872                 Map1toN(srcfmt, src->map->info.r, src->map->info.g,
   873                         src->map->info.b, src->map->info.a, dstfmt);
   874             if (map->info.table == NULL) {
   875                 return (-1);
   876             }
   877             break;
   878         }
   879         break;
   880     default:
   881         switch (dstfmt->BytesPerPixel) {
   882         case 1:
   883             /* BitField --> Palette */
   884             map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
   885             if (!map->identity) {
   886                 if (map->info.table == NULL) {
   887                     return (-1);
   888                 }
   889             }
   890             map->identity = 0;  /* Don't optimize to copy */
   891             break;
   892         default:
   893             /* BitField --> BitField */
   894             if (FORMAT_EQUAL(srcfmt, dstfmt))
   895                 map->identity = 1;
   896             break;
   897         }
   898         break;
   899     }
   900 
   901     map->dst = dst;
   902     map->format_version = dst->format_version;
   903 
   904     /* Choose your blitters wisely */
   905     return (SDL_CalculateBlit(src));
   906 }
   907 
   908 void
   909 SDL_FreeBlitMap(SDL_BlitMap * map)
   910 {
   911     if (map) {
   912         SDL_InvalidateMap(map);
   913         SDL_free(map);
   914     }
   915 }
   916 
   917 /* vi: set ts=4 sw=4 expandtab: */