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