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