Mercurial > sdl-ios-xcode
annotate src/video/SDL_pixels.c @ 1318:f95502c6fc72
Eliminate duplicate modes with different refresh rates
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 09:28:42 +0000 |
parents | c9b51268668f |
children | 450721ad5436 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1057
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1057
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1057
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1057
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1057
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1057
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1057
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
50
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* General (mostly internal) pixel/color manipulation routines for SDL */ | |
24 | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 #include "SDL_error.h" | |
30 #include "SDL_endian.h" | |
31 #include "SDL_video.h" | |
32 #include "SDL_sysvideo.h" | |
33 #include "SDL_blit.h" | |
34 #include "SDL_pixels_c.h" | |
35 #include "SDL_RLEaccel_c.h" | |
36 | |
37 /* Helper functions */ | |
38 /* | |
39 * Allocate a pixel format structure and fill it according to the given info. | |
40 */ | |
41 SDL_PixelFormat *SDL_AllocFormat(int bpp, | |
42 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | |
43 { | |
44 SDL_PixelFormat *format; | |
45 Uint32 mask; | |
46 | |
47 /* Allocate an empty pixel format structure */ | |
48 format = malloc(sizeof(*format)); | |
49 if ( format == NULL ) { | |
50 SDL_OutOfMemory(); | |
51 return(NULL); | |
52 } | |
53 memset(format, 0, sizeof(*format)); | |
54 format->alpha = SDL_ALPHA_OPAQUE; | |
55 | |
56 /* Set up the format */ | |
57 format->BitsPerPixel = bpp; | |
58 format->BytesPerPixel = (bpp+7)/8; | |
1027
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
59 if ( Rmask || Bmask || Gmask ) { /* Packed pixels with custom mask */ |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
60 format->palette = NULL; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
61 format->Rshift = 0; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
62 format->Rloss = 8; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
63 if ( Rmask ) { |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
64 for ( mask = Rmask; !(mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
65 ++format->Rshift; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
66 for ( ; (mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
67 --format->Rloss; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
68 } |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
69 format->Gshift = 0; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
70 format->Gloss = 8; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
71 if ( Gmask ) { |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
72 for ( mask = Gmask; !(mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
73 ++format->Gshift; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
74 for ( ; (mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
75 --format->Gloss; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
76 } |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
77 format->Bshift = 0; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
78 format->Bloss = 8; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
79 if ( Bmask ) { |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
80 for ( mask = Bmask; !(mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
81 ++format->Bshift; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
82 for ( ; (mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
83 --format->Bloss; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
84 } |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
85 format->Ashift = 0; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
86 format->Aloss = 8; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
87 if ( Amask ) { |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
88 for ( mask = Amask; !(mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
89 ++format->Ashift; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
90 for ( ; (mask&0x01); mask >>= 1 ) |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
91 --format->Aloss; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
92 } |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
93 format->Rmask = Rmask; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
94 format->Gmask = Gmask; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
95 format->Bmask = Bmask; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
96 format->Amask = Amask; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
97 } else if ( bpp > 8 ) { /* Packed pixels with standard mask */ |
0 | 98 /* R-G-B */ |
99 if ( bpp > 24 ) | |
100 bpp = 24; | |
101 format->Rloss = 8-(bpp/3); | |
102 format->Gloss = 8-(bpp/3)-(bpp%3); | |
103 format->Bloss = 8-(bpp/3); | |
104 format->Rshift = ((bpp/3)+(bpp%3))+(bpp/3); | |
105 format->Gshift = (bpp/3); | |
106 format->Bshift = 0; | |
107 format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift); | |
108 format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift); | |
109 format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift); | |
1057
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
110 } else { |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
111 /* Palettized formats have no mask info */ |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
112 format->Rloss = 8; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
113 format->Gloss = 8; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
114 format->Bloss = 8; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
115 format->Aloss = 8; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
116 format->Rshift = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
117 format->Gshift = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
118 format->Bshift = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
119 format->Ashift = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
120 format->Rmask = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
121 format->Gmask = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
122 format->Bmask = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
123 format->Amask = 0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
124 } |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
125 if ( bpp <= 8 ) { /* Palettized mode */ |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
126 int ncolors = 1<<bpp; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
127 #ifdef DEBUG_PALETTE |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
128 fprintf(stderr,"bpp=%d ncolors=%d\n",bpp,ncolors); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
129 #endif |
1027
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
130 format->palette = (SDL_Palette *)malloc(sizeof(SDL_Palette)); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
131 if ( format->palette == NULL ) { |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
132 SDL_FreeFormat(format); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
133 SDL_OutOfMemory(); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
134 return(NULL); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
135 } |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
136 (format->palette)->ncolors = ncolors; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
137 (format->palette)->colors = (SDL_Color *)malloc( |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
138 (format->palette)->ncolors*sizeof(SDL_Color)); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
139 if ( (format->palette)->colors == NULL ) { |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
140 SDL_FreeFormat(format); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
141 SDL_OutOfMemory(); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
142 return(NULL); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
143 } |
1057
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
144 if ( Rmask || Bmask || Gmask ) { |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
145 /* create palette according to masks */ |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
146 int i; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
147 int Rm=0,Gm=0,Bm=0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
148 int Rw=0,Gw=0,Bw=0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
149 #ifdef ENABLE_PALETTE_ALPHA |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
150 int Am=0,Aw=0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
151 #endif |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
152 if(Rmask) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
153 { |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
154 Rw=8-format->Rloss; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
155 for(i=format->Rloss;i>0;i-=Rw) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
156 Rm|=1<<i; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
157 } |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
158 #ifdef DEBUG_PALETTE |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
159 fprintf(stderr,"Rw=%d Rm=0x%02X\n",Rw,Rm); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
160 #endif |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
161 if(Gmask) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
162 { |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
163 Gw=8-format->Gloss; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
164 for(i=format->Gloss;i>0;i-=Gw) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
165 Gm|=1<<i; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
166 } |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
167 #ifdef DEBUG_PALETTE |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
168 fprintf(stderr,"Gw=%d Gm=0x%02X\n",Gw,Gm); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
169 #endif |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
170 if(Bmask) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
171 { |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
172 Bw=8-format->Bloss; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
173 for(i=format->Bloss;i>0;i-=Bw) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
174 Bm|=1<<i; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
175 } |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
176 #ifdef DEBUG_PALETTE |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
177 fprintf(stderr,"Bw=%d Bm=0x%02X\n",Bw,Bm); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
178 #endif |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
179 #ifdef ENABLE_PALETTE_ALPHA |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
180 if(Amask) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
181 { |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
182 Aw=8-format->Aloss; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
183 for(i=format->Aloss;i>0;i-=Aw) |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
184 Am|=1<<i; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
185 } |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
186 # ifdef DEBUG_PALETTE |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
187 fprintf(stderr,"Aw=%d Am=0x%02X\n",Aw,Am); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
188 # endif |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
189 #endif |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
190 for(i=0; i < ncolors; ++i) { |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
191 int r,g,b; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
192 r=(i&Rmask)>>format->Rshift; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
193 r=(r<<format->Rloss)|((r*Rm)>>Rw); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
194 format->palette->colors[i].r=r; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
195 |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
196 g=(i&Gmask)>>format->Gshift; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
197 g=(g<<format->Gloss)|((g*Gm)>>Gw); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
198 format->palette->colors[i].g=g; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
199 |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
200 b=(i&Bmask)>>format->Bshift; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
201 b=(b<<format->Bloss)|((b*Bm)>>Bw); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
202 format->palette->colors[i].b=b; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
203 |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
204 #ifdef ENABLE_PALETTE_ALPHA |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
205 a=(i&Amask)>>format->Ashift; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
206 a=(a<<format->Aloss)|((a*Am)>>Aw); |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
207 format->palette->colors[i].unused=a; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
208 #else |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
209 format->palette->colors[i].unused=0; |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
210 #endif |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
211 } |
e9d23bb80140
Date: Mon, 02 May 2005 04:23:16 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
1027
diff
changeset
|
212 } else if ( ncolors == 2 ) { |
1027
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
213 /* Create a black and white bitmap palette */ |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
214 format->palette->colors[0].r = 0xFF; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
215 format->palette->colors[0].g = 0xFF; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
216 format->palette->colors[0].b = 0xFF; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
217 format->palette->colors[1].r = 0x00; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
218 format->palette->colors[1].g = 0x00; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
219 format->palette->colors[1].b = 0x00; |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
220 } else { |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
221 /* Create an empty palette */ |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
222 memset((format->palette)->colors, 0, |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
223 (format->palette)->ncolors*sizeof(SDL_Color)); |
c69697a85412
Clarified the code in the pixel format allocation
Sam Lantinga <slouken@libsdl.org>
parents:
997
diff
changeset
|
224 } |
0 | 225 } |
226 return(format); | |
227 } | |
228 SDL_PixelFormat *SDL_ReallocFormat(SDL_Surface *surface, int bpp, | |
229 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | |
230 { | |
231 if ( surface->format ) { | |
232 SDL_FreeFormat(surface->format); | |
233 SDL_FormatChanged(surface); | |
234 } | |
235 surface->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask); | |
236 return surface->format; | |
237 } | |
238 | |
239 /* | |
240 * Change any previous mappings from/to the new surface format | |
241 */ | |
242 void SDL_FormatChanged(SDL_Surface *surface) | |
243 { | |
845
333db1d87876
Fixed a bug in detecting surface mapping changes
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
244 static int format_version = 0; |
333db1d87876
Fixed a bug in detecting surface mapping changes
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
245 ++format_version; |
333db1d87876
Fixed a bug in detecting surface mapping changes
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
246 if ( format_version < 0 ) { /* It wrapped... */ |
333db1d87876
Fixed a bug in detecting surface mapping changes
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
247 format_version = 1; |
333db1d87876
Fixed a bug in detecting surface mapping changes
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
248 } |
333db1d87876
Fixed a bug in detecting surface mapping changes
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
249 surface->format_version = format_version; |
0 | 250 SDL_InvalidateMap(surface->map); |
251 } | |
252 /* | |
253 * Free a previously allocated format structure | |
254 */ | |
255 void SDL_FreeFormat(SDL_PixelFormat *format) | |
256 { | |
257 if ( format ) { | |
258 if ( format->palette ) { | |
259 if ( format->palette->colors ) { | |
260 free(format->palette->colors); | |
261 } | |
262 free(format->palette); | |
263 } | |
264 free(format); | |
265 } | |
266 } | |
267 /* | |
268 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors | |
269 */ | |
270 void SDL_DitherColors(SDL_Color *colors, int bpp) | |
271 { | |
272 int i; | |
273 if(bpp != 8) | |
274 return; /* only 8bpp supported right now */ | |
275 | |
276 for(i = 0; i < 256; i++) { | |
277 int r, g, b; | |
278 /* map each bit field to the full [0, 255] interval, | |
279 so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */ | |
280 r = i & 0xe0; | |
281 r |= r >> 3 | r >> 6; | |
282 colors[i].r = r; | |
283 g = (i << 3) & 0xe0; | |
284 g |= g >> 3 | g >> 6; | |
285 colors[i].g = g; | |
286 b = i & 0x3; | |
287 b |= b << 2; | |
288 b |= b << 4; | |
289 colors[i].b = b; | |
290 } | |
291 } | |
292 /* | |
293 * Calculate the pad-aligned scanline width of a surface | |
294 */ | |
295 Uint16 SDL_CalculatePitch(SDL_Surface *surface) | |
296 { | |
297 Uint16 pitch; | |
298 | |
299 /* Surface should be 4-byte aligned for speed */ | |
300 pitch = surface->w*surface->format->BytesPerPixel; | |
301 switch (surface->format->BitsPerPixel) { | |
302 case 1: | |
303 pitch = (pitch+7)/8; | |
304 break; | |
305 case 4: | |
306 pitch = (pitch+1)/2; | |
307 break; | |
308 default: | |
309 break; | |
310 } | |
311 pitch = (pitch + 3) & ~3; /* 4-byte aligning */ | |
312 return(pitch); | |
313 } | |
314 /* | |
315 * Match an RGB value to a particular palette index | |
316 */ | |
317 Uint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b) | |
318 { | |
319 /* Do colorspace distance matching */ | |
320 unsigned int smallest; | |
321 unsigned int distance; | |
322 int rd, gd, bd; | |
323 int i; | |
324 Uint8 pixel=0; | |
325 | |
326 smallest = ~0; | |
327 for ( i=0; i<pal->ncolors; ++i ) { | |
328 rd = pal->colors[i].r - r; | |
329 gd = pal->colors[i].g - g; | |
330 bd = pal->colors[i].b - b; | |
331 distance = (rd*rd)+(gd*gd)+(bd*bd); | |
332 if ( distance < smallest ) { | |
333 pixel = i; | |
334 if ( distance == 0 ) { /* Perfect match! */ | |
335 break; | |
336 } | |
337 smallest = distance; | |
338 } | |
339 } | |
340 return(pixel); | |
341 } | |
342 | |
343 /* Find the opaque pixel value corresponding to an RGB triple */ | |
344 Uint32 SDL_MapRGB(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b) | |
345 { | |
346 if ( format->palette == NULL ) { | |
347 return (r >> format->Rloss) << format->Rshift | |
348 | (g >> format->Gloss) << format->Gshift | |
349 | (b >> format->Bloss) << format->Bshift | |
350 | format->Amask; | |
351 } else { | |
352 return SDL_FindColor(format->palette, r, g, b); | |
353 } | |
354 } | |
355 | |
356 /* Find the pixel value corresponding to an RGBA quadruple */ | |
357 Uint32 SDL_MapRGBA(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b, Uint8 a) | |
358 { | |
359 if ( format->palette == NULL ) { | |
360 return (r >> format->Rloss) << format->Rshift | |
361 | (g >> format->Gloss) << format->Gshift | |
362 | (b >> format->Bloss) << format->Bshift | |
363 | ((a >> format->Aloss) << format->Ashift & format->Amask); | |
364 } else { | |
365 return SDL_FindColor(format->palette, r, g, b); | |
366 } | |
367 } | |
368 | |
369 void SDL_GetRGBA(Uint32 pixel, SDL_PixelFormat *fmt, | |
370 Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) | |
371 { | |
372 if ( fmt->palette == NULL ) { | |
373 /* | |
374 * This makes sure that the result is mapped to the | |
375 * interval [0..255], and the maximum value for each | |
376 * component is 255. This is important to make sure | |
377 * that white is indeed reported as (255, 255, 255), | |
378 * and that opaque alpha is 255. | |
379 * This only works for RGB bit fields at least 4 bit | |
380 * wide, which is almost always the case. | |
381 */ | |
628
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
382 unsigned v; |
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
383 v = (pixel & fmt->Rmask) >> fmt->Rshift; |
688
c0522010bb6d
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
Sam Lantinga <slouken@libsdl.org>
parents:
628
diff
changeset
|
384 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1))); |
628
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
385 v = (pixel & fmt->Gmask) >> fmt->Gshift; |
688
c0522010bb6d
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
Sam Lantinga <slouken@libsdl.org>
parents:
628
diff
changeset
|
386 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1))); |
628
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
387 v = (pixel & fmt->Bmask) >> fmt->Bshift; |
688
c0522010bb6d
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
Sam Lantinga <slouken@libsdl.org>
parents:
628
diff
changeset
|
388 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1))); |
0 | 389 if(fmt->Amask) { |
628
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
390 v = (pixel & fmt->Amask) >> fmt->Ashift; |
688
c0522010bb6d
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
Sam Lantinga <slouken@libsdl.org>
parents:
628
diff
changeset
|
391 *a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1))); |
695 | 392 } else { |
0 | 393 *a = SDL_ALPHA_OPAQUE; |
695 | 394 } |
0 | 395 } else { |
396 *r = fmt->palette->colors[pixel].r; | |
397 *g = fmt->palette->colors[pixel].g; | |
398 *b = fmt->palette->colors[pixel].b; | |
399 *a = SDL_ALPHA_OPAQUE; | |
400 } | |
401 } | |
402 | |
403 void SDL_GetRGB(Uint32 pixel, SDL_PixelFormat *fmt, Uint8 *r,Uint8 *g,Uint8 *b) | |
404 { | |
405 if ( fmt->palette == NULL ) { | |
406 /* the note for SDL_GetRGBA above applies here too */ | |
628
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
407 unsigned v; |
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
408 v = (pixel & fmt->Rmask) >> fmt->Rshift; |
688
c0522010bb6d
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
Sam Lantinga <slouken@libsdl.org>
parents:
628
diff
changeset
|
409 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1))); |
628
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
410 v = (pixel & fmt->Gmask) >> fmt->Gshift; |
688
c0522010bb6d
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
Sam Lantinga <slouken@libsdl.org>
parents:
628
diff
changeset
|
411 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1))); |
628
e561e8752d33
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
412 v = (pixel & fmt->Bmask) >> fmt->Bshift; |
688
c0522010bb6d
Date: Tue, 12 Aug 2003 14:26:19 +0200 (MEST)
Sam Lantinga <slouken@libsdl.org>
parents:
628
diff
changeset
|
413 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1))); |
0 | 414 } else { |
415 *r = fmt->palette->colors[pixel].r; | |
416 *g = fmt->palette->colors[pixel].g; | |
417 *b = fmt->palette->colors[pixel].b; | |
418 } | |
419 } | |
420 | |
421 /* Apply gamma to a set of colors - this is easy. :) */ | |
422 void SDL_ApplyGamma(Uint16 *gamma, SDL_Color *colors, SDL_Color *output, | |
423 int ncolors) | |
424 { | |
425 int i; | |
426 | |
427 for ( i=0; i<ncolors; ++i ) { | |
428 output[i].r = gamma[0*256 + colors[i].r] >> 8; | |
429 output[i].g = gamma[1*256 + colors[i].g] >> 8; | |
430 output[i].b = gamma[2*256 + colors[i].b] >> 8; | |
431 } | |
432 } | |
433 | |
434 /* Map from Palette to Palette */ | |
435 static Uint8 *Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical) | |
436 { | |
437 Uint8 *map; | |
438 int i; | |
439 | |
440 if ( identical ) { | |
441 if ( src->ncolors <= dst->ncolors ) { | |
442 /* If an identical palette, no need to map */ | |
443 if ( memcmp(src->colors, dst->colors, src->ncolors* | |
444 sizeof(SDL_Color)) == 0 ) { | |
445 *identical = 1; | |
446 return(NULL); | |
447 } | |
448 } | |
449 *identical = 0; | |
450 } | |
451 map = (Uint8 *)malloc(src->ncolors); | |
452 if ( map == NULL ) { | |
453 SDL_OutOfMemory(); | |
454 return(NULL); | |
455 } | |
456 for ( i=0; i<src->ncolors; ++i ) { | |
457 map[i] = SDL_FindColor(dst, | |
458 src->colors[i].r, src->colors[i].g, src->colors[i].b); | |
459 } | |
460 return(map); | |
461 } | |
462 /* Map from Palette to BitField */ | |
463 static Uint8 *Map1toN(SDL_Palette *src, SDL_PixelFormat *dst) | |
464 { | |
465 Uint8 *map; | |
466 int i; | |
467 int bpp; | |
50 | 468 unsigned alpha; |
0 | 469 |
470 bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel); | |
471 map = (Uint8 *)malloc(src->ncolors*bpp); | |
472 if ( map == NULL ) { | |
473 SDL_OutOfMemory(); | |
474 return(NULL); | |
475 } | |
476 | |
50 | 477 alpha = dst->Amask ? SDL_ALPHA_OPAQUE : 0; |
0 | 478 /* We memory copy to the pixel map so the endianness is preserved */ |
479 for ( i=0; i<src->ncolors; ++i ) { | |
480 ASSEMBLE_RGBA(&map[i*bpp], dst->BytesPerPixel, dst, | |
481 src->colors[i].r, src->colors[i].g, | |
50 | 482 src->colors[i].b, alpha); |
0 | 483 } |
484 return(map); | |
485 } | |
486 /* Map from BitField to Dithered-Palette to Palette */ | |
487 static Uint8 *MapNto1(SDL_PixelFormat *src, SDL_Palette *dst, int *identical) | |
488 { | |
489 /* Generate a 256 color dither palette */ | |
490 SDL_Palette dithered; | |
491 SDL_Color colors[256]; | |
997
3bf4103b2b89
Date: Sat, 27 Nov 2004 13:35:43 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
845
diff
changeset
|
492 |
3bf4103b2b89
Date: Sat, 27 Nov 2004 13:35:43 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
845
diff
changeset
|
493 /* SDL_DitherColors does not initialize the 'unused' component of colors, |
3bf4103b2b89
Date: Sat, 27 Nov 2004 13:35:43 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
845
diff
changeset
|
494 but Map1to1 compares it against dst, so we should initialize it. */ |
3bf4103b2b89
Date: Sat, 27 Nov 2004 13:35:43 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
845
diff
changeset
|
495 memset(colors, 0, sizeof(colors)); |
0 | 496 |
497 dithered.ncolors = 256; | |
498 SDL_DitherColors(colors, 8); | |
499 dithered.colors = colors; | |
500 return(Map1to1(&dithered, dst, identical)); | |
501 } | |
502 | |
503 SDL_BlitMap *SDL_AllocBlitMap(void) | |
504 { | |
505 SDL_BlitMap *map; | |
506 | |
507 /* Allocate the empty map */ | |
508 map = (SDL_BlitMap *)malloc(sizeof(*map)); | |
509 if ( map == NULL ) { | |
510 SDL_OutOfMemory(); | |
511 return(NULL); | |
512 } | |
513 memset(map, 0, sizeof(*map)); | |
514 | |
515 /* Allocate the software blit data */ | |
516 map->sw_data = (struct private_swaccel *)malloc(sizeof(*map->sw_data)); | |
517 if ( map->sw_data == NULL ) { | |
518 SDL_FreeBlitMap(map); | |
519 SDL_OutOfMemory(); | |
520 return(NULL); | |
521 } | |
522 memset(map->sw_data, 0, sizeof(*map->sw_data)); | |
523 | |
524 /* It's ready to go */ | |
525 return(map); | |
526 } | |
527 void SDL_InvalidateMap(SDL_BlitMap *map) | |
528 { | |
529 if ( ! map ) { | |
530 return; | |
531 } | |
532 map->dst = NULL; | |
533 map->format_version = (unsigned int)-1; | |
534 if ( map->table ) { | |
535 free(map->table); | |
536 map->table = NULL; | |
537 } | |
538 } | |
539 int SDL_MapSurface (SDL_Surface *src, SDL_Surface *dst) | |
540 { | |
541 SDL_PixelFormat *srcfmt; | |
542 SDL_PixelFormat *dstfmt; | |
543 SDL_BlitMap *map; | |
544 | |
545 /* Clear out any previous mapping */ | |
546 map = src->map; | |
547 if ( (src->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { | |
548 SDL_UnRLESurface(src, 1); | |
549 } | |
550 SDL_InvalidateMap(map); | |
551 | |
552 /* Figure out what kind of mapping we're doing */ | |
553 map->identity = 0; | |
554 srcfmt = src->format; | |
555 dstfmt = dst->format; | |
556 switch (srcfmt->BytesPerPixel) { | |
557 case 1: | |
558 switch (dstfmt->BytesPerPixel) { | |
559 case 1: | |
560 /* Palette --> Palette */ | |
561 /* If both SDL_HWSURFACE, assume have same palette */ | |
562 if ( ((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) && | |
563 ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) ) { | |
564 map->identity = 1; | |
565 } else { | |
566 map->table = Map1to1(srcfmt->palette, | |
567 dstfmt->palette, &map->identity); | |
568 } | |
569 if ( ! map->identity ) { | |
570 if ( map->table == NULL ) { | |
571 return(-1); | |
572 } | |
573 } | |
574 if (srcfmt->BitsPerPixel!=dstfmt->BitsPerPixel) | |
575 map->identity = 0; | |
576 break; | |
577 | |
578 default: | |
579 /* Palette --> BitField */ | |
580 map->table = Map1toN(srcfmt->palette, dstfmt); | |
581 if ( map->table == NULL ) { | |
582 return(-1); | |
583 } | |
584 break; | |
585 } | |
586 break; | |
587 default: | |
588 switch (dstfmt->BytesPerPixel) { | |
589 case 1: | |
590 /* BitField --> Palette */ | |
591 map->table = MapNto1(srcfmt, | |
592 dstfmt->palette, &map->identity); | |
593 if ( ! map->identity ) { | |
594 if ( map->table == NULL ) { | |
595 return(-1); | |
596 } | |
597 } | |
598 map->identity = 0; /* Don't optimize to copy */ | |
599 break; | |
600 default: | |
601 /* BitField --> BitField */ | |
602 if ( FORMAT_EQUAL(srcfmt, dstfmt) ) | |
603 map->identity = 1; | |
604 break; | |
605 } | |
606 break; | |
607 } | |
608 | |
609 map->dst = dst; | |
610 map->format_version = dst->format_version; | |
611 | |
612 /* Choose your blitters wisely */ | |
613 return(SDL_CalculateBlit(src)); | |
614 } | |
615 void SDL_FreeBlitMap(SDL_BlitMap *map) | |
616 { | |
617 if ( map ) { | |
618 SDL_InvalidateMap(map); | |
619 if ( map->sw_data != NULL ) { | |
620 free(map->sw_data); | |
621 } | |
622 free(map); | |
623 } | |
624 } |