Mercurial > sdl-ios-xcode
comparison src/video/SDL_pixels.c @ 1027:c69697a85412
Clarified the code in the pixel format allocation
Added support for direct color 8-bpp modes.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 12 Jan 2005 19:38:24 +0000 |
parents | 3bf4103b2b89 |
children | e9d23bb80140 |
comparison
equal
deleted
inserted
replaced
1026:0f3aa6ab3341 | 1027:c69697a85412 |
---|---|
59 format->alpha = SDL_ALPHA_OPAQUE; | 59 format->alpha = SDL_ALPHA_OPAQUE; |
60 | 60 |
61 /* Set up the format */ | 61 /* Set up the format */ |
62 format->BitsPerPixel = bpp; | 62 format->BitsPerPixel = bpp; |
63 format->BytesPerPixel = (bpp+7)/8; | 63 format->BytesPerPixel = (bpp+7)/8; |
64 switch (bpp) { | 64 if ( Rmask || Bmask || Gmask ) { /* Packed pixels with custom mask */ |
65 case 1: | 65 format->palette = NULL; |
66 /* Create the 2 color black-white palette */ | 66 format->Rshift = 0; |
67 format->palette = (SDL_Palette *)malloc( | 67 format->Rloss = 8; |
68 sizeof(SDL_Palette)); | 68 if ( Rmask ) { |
69 if ( format->palette == NULL ) { | 69 for ( mask = Rmask; !(mask&0x01); mask >>= 1 ) |
70 SDL_FreeFormat(format); | 70 ++format->Rshift; |
71 SDL_OutOfMemory(); | 71 for ( ; (mask&0x01); mask >>= 1 ) |
72 return(NULL); | 72 --format->Rloss; |
73 } | 73 } |
74 (format->palette)->ncolors = 2; | 74 format->Gshift = 0; |
75 (format->palette)->colors = (SDL_Color *)malloc( | 75 format->Gloss = 8; |
76 (format->palette)->ncolors*sizeof(SDL_Color)); | 76 if ( Gmask ) { |
77 if ( (format->palette)->colors == NULL ) { | 77 for ( mask = Gmask; !(mask&0x01); mask >>= 1 ) |
78 SDL_FreeFormat(format); | 78 ++format->Gshift; |
79 SDL_OutOfMemory(); | 79 for ( ; (mask&0x01); mask >>= 1 ) |
80 return(NULL); | 80 --format->Gloss; |
81 } | 81 } |
82 format->palette->colors[0].r = 0xFF; | 82 format->Bshift = 0; |
83 format->palette->colors[0].g = 0xFF; | 83 format->Bloss = 8; |
84 format->palette->colors[0].b = 0xFF; | 84 if ( Bmask ) { |
85 format->palette->colors[1].r = 0x00; | 85 for ( mask = Bmask; !(mask&0x01); mask >>= 1 ) |
86 format->palette->colors[1].g = 0x00; | 86 ++format->Bshift; |
87 format->palette->colors[1].b = 0x00; | 87 for ( ; (mask&0x01); mask >>= 1 ) |
88 format->Rloss = 8; | 88 --format->Bloss; |
89 format->Gloss = 8; | 89 } |
90 format->Bloss = 8; | 90 format->Ashift = 0; |
91 format->Aloss = 8; | 91 format->Aloss = 8; |
92 format->Rshift = 0; | 92 if ( Amask ) { |
93 format->Gshift = 0; | 93 for ( mask = Amask; !(mask&0x01); mask >>= 1 ) |
94 format->Bshift = 0; | 94 ++format->Ashift; |
95 format->Ashift = 0; | 95 for ( ; (mask&0x01); mask >>= 1 ) |
96 format->Rmask = 0; | 96 --format->Aloss; |
97 format->Gmask = 0; | 97 } |
98 format->Bmask = 0; | 98 format->Rmask = Rmask; |
99 format->Amask = 0; | 99 format->Gmask = Gmask; |
100 break; | 100 format->Bmask = Bmask; |
101 | 101 format->Amask = Amask; |
102 case 4: | 102 } else if ( bpp > 8 ) { /* Packed pixels with standard mask */ |
103 /* Create the 16 color VGA palette */ | |
104 format->palette = (SDL_Palette *)malloc( | |
105 sizeof(SDL_Palette)); | |
106 if ( format->palette == NULL ) { | |
107 SDL_FreeFormat(format); | |
108 SDL_OutOfMemory(); | |
109 return(NULL); | |
110 } | |
111 (format->palette)->ncolors = 16; | |
112 (format->palette)->colors = (SDL_Color *)malloc( | |
113 (format->palette)->ncolors*sizeof(SDL_Color)); | |
114 if ( (format->palette)->colors == NULL ) { | |
115 SDL_FreeFormat(format); | |
116 SDL_OutOfMemory(); | |
117 return(NULL); | |
118 } | |
119 /* Punt for now, will this ever be used? */ | |
120 memset((format->palette)->colors, 0, | |
121 (format->palette)->ncolors*sizeof(SDL_Color)); | |
122 | |
123 /* Palettized formats have no mask info */ | |
124 format->Rloss = 8; | |
125 format->Gloss = 8; | |
126 format->Bloss = 8; | |
127 format->Aloss = 8; | |
128 format->Rshift = 0; | |
129 format->Gshift = 0; | |
130 format->Bshift = 0; | |
131 format->Ashift = 0; | |
132 format->Rmask = 0; | |
133 format->Gmask = 0; | |
134 format->Bmask = 0; | |
135 format->Amask = 0; | |
136 break; | |
137 | |
138 case 8: | |
139 /* Create an empty 256 color palette */ | |
140 format->palette = (SDL_Palette *)malloc( | |
141 sizeof(SDL_Palette)); | |
142 if ( format->palette == NULL ) { | |
143 SDL_FreeFormat(format); | |
144 SDL_OutOfMemory(); | |
145 return(NULL); | |
146 } | |
147 (format->palette)->ncolors = 256; | |
148 (format->palette)->colors = (SDL_Color *)malloc( | |
149 (format->palette)->ncolors*sizeof(SDL_Color)); | |
150 if ( (format->palette)->colors == NULL ) { | |
151 SDL_FreeFormat(format); | |
152 SDL_OutOfMemory(); | |
153 return(NULL); | |
154 } | |
155 memset((format->palette)->colors, 0, | |
156 (format->palette)->ncolors*sizeof(SDL_Color)); | |
157 | |
158 /* Palettized formats have no mask info */ | |
159 format->Rloss = 8; | |
160 format->Gloss = 8; | |
161 format->Bloss = 8; | |
162 format->Aloss = 8; | |
163 format->Rshift = 0; | |
164 format->Gshift = 0; | |
165 format->Bshift = 0; | |
166 format->Ashift = 0; | |
167 format->Rmask = 0; | |
168 format->Gmask = 0; | |
169 format->Bmask = 0; | |
170 format->Amask = 0; | |
171 break; | |
172 | |
173 default: | |
174 /* No palette, just packed pixel info */ | |
175 format->palette = NULL; | |
176 format->Rshift = 0; | |
177 format->Rloss = 8; | |
178 if ( Rmask ) { | |
179 for ( mask = Rmask; !(mask&0x01); mask >>= 1 ) | |
180 ++format->Rshift; | |
181 for ( ; (mask&0x01); mask >>= 1 ) | |
182 --format->Rloss; | |
183 } | |
184 format->Gshift = 0; | |
185 format->Gloss = 8; | |
186 if ( Gmask ) { | |
187 for ( mask = Gmask; !(mask&0x01); mask >>= 1 ) | |
188 ++format->Gshift; | |
189 for ( ; (mask&0x01); mask >>= 1 ) | |
190 --format->Gloss; | |
191 } | |
192 format->Bshift = 0; | |
193 format->Bloss = 8; | |
194 if ( Bmask ) { | |
195 for ( mask = Bmask; !(mask&0x01); mask >>= 1 ) | |
196 ++format->Bshift; | |
197 for ( ; (mask&0x01); mask >>= 1 ) | |
198 --format->Bloss; | |
199 } | |
200 format->Ashift = 0; | |
201 format->Aloss = 8; | |
202 if ( Amask ) { | |
203 for ( mask = Amask; !(mask&0x01); mask >>= 1 ) | |
204 ++format->Ashift; | |
205 for ( ; (mask&0x01); mask >>= 1 ) | |
206 --format->Aloss; | |
207 } | |
208 format->Rmask = Rmask; | |
209 format->Gmask = Gmask; | |
210 format->Bmask = Bmask; | |
211 format->Amask = Amask; | |
212 break; | |
213 } | |
214 /* Calculate some standard bitmasks, if necessary | |
215 * Note: This could conflict with an alpha mask, if given. | |
216 */ | |
217 if ( (bpp > 8) && !format->Rmask && !format->Gmask && !format->Bmask ) { | |
218 /* R-G-B */ | 103 /* R-G-B */ |
219 if ( bpp > 24 ) | 104 if ( bpp > 24 ) |
220 bpp = 24; | 105 bpp = 24; |
221 format->Rloss = 8-(bpp/3); | 106 format->Rloss = 8-(bpp/3); |
222 format->Gloss = 8-(bpp/3)-(bpp%3); | 107 format->Gloss = 8-(bpp/3)-(bpp%3); |
225 format->Gshift = (bpp/3); | 110 format->Gshift = (bpp/3); |
226 format->Bshift = 0; | 111 format->Bshift = 0; |
227 format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift); | 112 format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift); |
228 format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift); | 113 format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift); |
229 format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift); | 114 format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift); |
115 } else { /* Palettized mode */ | |
116 int i, ncolors = 1; | |
117 for ( i = 0; i < bpp; ++i ) { | |
118 ncolors *= 2; | |
119 } | |
120 format->palette = (SDL_Palette *)malloc(sizeof(SDL_Palette)); | |
121 if ( format->palette == NULL ) { | |
122 SDL_FreeFormat(format); | |
123 SDL_OutOfMemory(); | |
124 return(NULL); | |
125 } | |
126 (format->palette)->ncolors = ncolors; | |
127 (format->palette)->colors = (SDL_Color *)malloc( | |
128 (format->palette)->ncolors*sizeof(SDL_Color)); | |
129 if ( (format->palette)->colors == NULL ) { | |
130 SDL_FreeFormat(format); | |
131 SDL_OutOfMemory(); | |
132 return(NULL); | |
133 } | |
134 if ( ncolors == 2 ) { | |
135 /* Create a black and white bitmap palette */ | |
136 format->palette->colors[0].r = 0xFF; | |
137 format->palette->colors[0].g = 0xFF; | |
138 format->palette->colors[0].b = 0xFF; | |
139 format->palette->colors[1].r = 0x00; | |
140 format->palette->colors[1].g = 0x00; | |
141 format->palette->colors[1].b = 0x00; | |
142 } else { | |
143 /* Create an empty palette */ | |
144 memset((format->palette)->colors, 0, | |
145 (format->palette)->ncolors*sizeof(SDL_Color)); | |
146 } | |
147 | |
148 /* Palettized formats have no mask info */ | |
149 format->Rloss = 8; | |
150 format->Gloss = 8; | |
151 format->Bloss = 8; | |
152 format->Aloss = 8; | |
153 format->Rshift = 0; | |
154 format->Gshift = 0; | |
155 format->Bshift = 0; | |
156 format->Ashift = 0; | |
157 format->Rmask = 0; | |
158 format->Gmask = 0; | |
159 format->Bmask = 0; | |
160 format->Amask = 0; | |
230 } | 161 } |
231 return(format); | 162 return(format); |
232 } | 163 } |
233 SDL_PixelFormat *SDL_ReallocFormat(SDL_Surface *surface, int bpp, | 164 SDL_PixelFormat *SDL_ReallocFormat(SDL_Surface *surface, int bpp, |
234 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) | 165 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) |