Debian Codegraph @

src/gfx_palette.cpp

raw
1 /**
2 * Graphics utilities: palettes...
3
4 * Copyright (C) 2003 Shawn Betts
5 * Copyright (C) 2003, 2004, 2007, 2008, 2009, 2014 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "gfx_palette.h"
29
30 #include <stdlib.h>
31 #include "SDL.h"
32 #include "SDL_image.h"
33 #include "gfx.h"
34 #include "io_util.h"
35 #include "paths.h"
36 #include "log.h"
37
38 /* Copy of the physical screen palette - SDL provides a getter for the
39 logical palette, but not for the hardware palette */
40 static SDL_Color phys_palette[256];
41
42
43 /**
44 * Get local copy of physical palette
45 */
46 void gfx_palette_get_phys(SDL_Color* palette)
47 {
48 memcpy(palette, phys_palette, sizeof(phys_palette));
49 }
50
51 /**
52 * Set physical palette
53 */
54 void gfx_palette_set_phys(SDL_Color* new_palette)
55 {
56 /* Now this one is tricky: DX/Woe has a "feature" where palette
57 indexes 0 and 255 are fixed to black and white,
58 respectively. This is the opposite of the default Dink palette
59 - which is why fill_screen(0) is black and not white as in the
60 Dink palette. This also makes "Lyna's Story"'s palette change a
61 bit ugly, because pure black and white colors are not reversed
62 when you enter "negative" color mode. This does not affect
63 other indexes. Technically this happens when you get a palette
64 from GetEntries(), and when you CreatePalette() without
65 specifying DDPCAPS_ALLOW256 (so respectively, in
66 change_screen_palette() and load_palette_from_*()). But well,
67 reproducing the bug is important for backward compatibility. */
68
69 memcpy(phys_palette, new_palette, sizeof(phys_palette));
70
71 phys_palette[0].r = 0;
72 phys_palette[0].g = 0;
73 phys_palette[0].b = 0;
74 phys_palette[255].r = 255;
75 phys_palette[255].g = 255;
76 phys_palette[255].b = 255;
77 }
78
79 /**
80 * Return physical palette as SDL_PixelFormat for easy SDL_MapRGB and
81 * SDL_ConvertSurface
82 */
83 SDL_PixelFormat* gfx_palette_get_phys_format() {
84 SDL_PixelFormat* fmt = SDL_AllocFormat(SDL_PIXELFORMAT_INDEX8);
85 if (!fmt->palette) // fmt is cached ref from SDL2
86 fmt->palette = SDL_AllocPalette(256);
87 gfx_palette_get_phys(fmt->palette->colors);
88 return fmt;
89 }
90
91 /* Generate a default/fallback 216 colors palette */
92 void gfx_palette_reset()
93 {
94 SDL_Color palette[256];
95 int i = 0;
96 int r, g, b;
97
98 /* Set a 3,3,2 color cube */
99 for (r = 0; r < 256; r += 0x33)
100 {
101 for (g = 0; g < 256; g += 0x33)
102 {
103 for (b = 0; b < 256; b += 0x33)
104 {
105 palette[i].r = r;
106 palette[i].g = g;
107 palette[i].b = b;
108 i++;
109 }
110 }
111 }
112
113 /* Set the rest of the colors to black */
114 for (; i < 256; i++)
115 palette[i].r
116 = palette[i].g
117 = palette[i].b
118 = 0;
119
120 /* Set palette with DX bug overwrite */
121 gfx_palette_set_phys(palette);
122 }
123
124 /* Get a colors palette from the specified image */
125 int
126 gfx_palette_set_from_surface(SDL_Surface *bmp)
127 {
128 SDL_Color palette[256];
129 int i;
130
131 if (bmp == NULL || bmp->format->palette == NULL)
132 return -1;
133
134 for (i = 0; i < bmp->format->palette->ncolors; i++)
135 {
136 palette[i].r = bmp->format->palette->colors[i].r;
137 palette[i].g = bmp->format->palette->colors[i].g;
138 palette[i].b = bmp->format->palette->colors[i].b;
139 // Caution: SDL_Color.a is significant in SDL2
140 palette[i].a = bmp->format->palette->colors[i].a;
141 //log_debug("palette[%03d] = #%02X%02X%02X %02X", i,
142 // palette[i].r, palette[i].g, palette[i].b, palette[i].a);
143 }
144
145 gfx_palette_set_phys(palette);
146 return 0;
147 }
148
149 /* Get a colors palette from the specified image */
150 int
151 gfx_palette_set_from_bmp(char *filename)
152 {
153 SDL_Surface *bmp;
154 int success = -1;
155 char *fullpath = NULL;
156
157 fullpath = paths_dmodfile(filename);
158 bmp = IMG_Load(fullpath);
159 free(fullpath);
160 if (bmp == NULL)
161 {
162 fullpath = paths_fallbackfile(filename);
163 bmp = IMG_Load(fullpath);
164 free(fullpath);
165 if (bmp == NULL)
166 {
167 log_error("load_palette_from_bmp: couldn't open '%s': %s",
168 filename, SDL_GetError());
169 return -1;
170 }
171 }
172
173 success = gfx_palette_set_from_surface(bmp);
174 SDL_FreeSurface(bmp);
175 return success;
176 }