Root/
Source at commit 79dcc8a created 9 years 8 months ago. By Maarten ter Huurne, Implemented stream output for RGBAColor | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
3 | * massimiliano.torromeo@gmail.com * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program 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 * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include "surface.h" |
22 | |
23 | #include "debug.h" |
24 | #include "imageio.h" |
25 | #include "surfacecollection.h" |
26 | #include "utilities.h" |
27 | |
28 | #include <algorithm> |
29 | #include <cassert> |
30 | #include <iomanip> |
31 | |
32 | using namespace std; |
33 | |
34 | RGBAColor RGBAColor::fromString(const string &strColor) { |
35 | return { |
36 | uint8_t(constrain(strtol(strColor.substr(0, 2).c_str(), nullptr, 16), |
37 | 0, 255)), |
38 | uint8_t(constrain(strtol(strColor.substr(2, 2).c_str(), nullptr, 16), |
39 | 0, 255)), |
40 | uint8_t(constrain(strtol(strColor.substr(4, 2).c_str(), nullptr, 16), |
41 | 0, 255)), |
42 | uint8_t(constrain(strtol(strColor.substr(6, 2).c_str(), nullptr, 16), |
43 | 0, 255)), |
44 | }; |
45 | } |
46 | |
47 | ostream& operator<<(ostream& os, RGBAColor const& color) { |
48 | auto oldfill = os.fill('0'); |
49 | auto oldflags = os.setf(ios::hex | ios::right, |
50 | ios::basefield | ios::adjustfield); |
51 | os << setw(2) << uint32_t(color.r) |
52 | << setw(2) << uint32_t(color.g) |
53 | << setw(2) << uint32_t(color.b) |
54 | << setw(2) << uint32_t(color.a); |
55 | os.fill(oldfill); |
56 | os.setf(oldflags); |
57 | return os; |
58 | } |
59 | |
60 | Surface *Surface::openOutputSurface(int width, int height, int bitsperpixel) { |
61 | SDL_ShowCursor(SDL_DISABLE); |
62 | SDL_Surface *raw = SDL_SetVideoMode( |
63 | width, height, bitsperpixel, SDL_HWSURFACE | SDL_DOUBLEBUF); |
64 | return raw ? new Surface(raw, false) : NULL; |
65 | } |
66 | |
67 | Surface *Surface::emptySurface(int width, int height) { |
68 | SDL_Surface *raw = SDL_CreateRGBSurface( |
69 | SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0); |
70 | if (!raw) return NULL; |
71 | SDL_FillRect(raw, NULL, SDL_MapRGB(raw->format, 0, 0, 0)); |
72 | return new Surface(raw, true); |
73 | } |
74 | |
75 | Surface *Surface::loadImage(const string &img, const string &skin, bool loadAlpha) { |
76 | string skinpath; |
77 | if (!skin.empty() && !img.empty() && img[0]!='/') |
78 | skinpath = SurfaceCollection::getSkinFilePath(skin, img); |
79 | else |
80 | skinpath = img; |
81 | |
82 | SDL_Surface *raw = loadPNG(skinpath, loadAlpha); |
83 | if (!raw) { |
84 | ERROR("Couldn't load surface '%s'\n", img.c_str()); |
85 | return NULL; |
86 | } |
87 | |
88 | return new Surface(raw, true); |
89 | } |
90 | |
91 | Surface::Surface(SDL_Surface *raw_, bool freeWhenDone_) |
92 | : raw(raw_) |
93 | , freeWhenDone(freeWhenDone_) |
94 | { |
95 | halfW = raw->w/2; |
96 | halfH = raw->h/2; |
97 | } |
98 | |
99 | Surface::Surface(Surface *s) { |
100 | raw = SDL_ConvertSurface(s->raw, s->raw->format, SDL_SWSURFACE); |
101 | // Note: A bug in SDL_ConvertSurface() leaves the per-surface alpha |
102 | // undefined when converting from RGBA to RGBA. This can cause |
103 | // problems if the surface is later converted to a format without |
104 | // an alpha channel, such as the display format. |
105 | raw->format->alpha = s->raw->format->alpha; |
106 | freeWhenDone = true; |
107 | halfW = raw->w/2; |
108 | halfH = raw->h/2; |
109 | } |
110 | |
111 | Surface::~Surface() { |
112 | if (freeWhenDone) { |
113 | SDL_FreeSurface(raw); |
114 | } |
115 | } |
116 | |
117 | void Surface::convertToDisplayFormat() { |
118 | SDL_Surface *newSurface = SDL_DisplayFormat(raw); |
119 | if (newSurface) { |
120 | if (freeWhenDone) { |
121 | SDL_FreeSurface(raw); |
122 | } |
123 | raw = newSurface; |
124 | freeWhenDone = true; |
125 | } |
126 | } |
127 | |
128 | void Surface::flip() { |
129 | SDL_Flip(raw); |
130 | } |
131 | |
132 | void Surface::blit(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
133 | if (destination == NULL || a==0) return; |
134 | |
135 | SDL_Rect src = { 0, 0, static_cast<Uint16>(w), static_cast<Uint16>(h) }; |
136 | SDL_Rect dest; |
137 | dest.x = x; |
138 | dest.y = y; |
139 | if (a>0 && a!=raw->format->alpha) |
140 | SDL_SetAlpha(raw, SDL_SRCALPHA|SDL_RLEACCEL, a); |
141 | SDL_BlitSurface(raw, (w==0 || h==0) ? NULL : &src, destination, &dest); |
142 | } |
143 | void Surface::blit(Surface *destination, int x, int y, int w, int h, int a) const { |
144 | blit(destination->raw,x,y,w,h,a); |
145 | } |
146 | |
147 | void Surface::blitCenter(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
148 | int oh, ow; |
149 | if (w==0) ow = halfW; else ow = min(halfW,w/2); |
150 | if (h==0) oh = halfH; else oh = min(halfH,h/2); |
151 | blit(destination,x-ow,y-oh,w,h,a); |
152 | } |
153 | void Surface::blitCenter(Surface *destination, int x, int y, int w, int h, int a) const { |
154 | blitCenter(destination->raw,x,y,w,h,a); |
155 | } |
156 | |
157 | void Surface::blitRight(SDL_Surface *destination, int x, int y, int w, int h, int a) const { |
158 | if (!w) w = raw->w; |
159 | blit(destination,x-min(raw->w,w),y,w,h,a); |
160 | } |
161 | void Surface::blitRight(Surface *destination, int x, int y, int w, int h, int a) const { |
162 | if (!w) w = raw->w; |
163 | blitRight(destination->raw,x,y,w,h,a); |
164 | } |
165 | |
166 | void Surface::box(SDL_Rect re, RGBAColor c) { |
167 | if (c.a == 255) { |
168 | SDL_FillRect(raw, &re, c.pixelValue(raw->format)); |
169 | } else if (c.a != 0) { |
170 | fillRectAlpha(re, c); |
171 | } |
172 | } |
173 | |
174 | void Surface::rectangle(SDL_Rect re, RGBAColor c) { |
175 | if (re.h >= 1) { |
176 | // Top. |
177 | box(SDL_Rect { re.x, re.y, re.w, 1 }, c); |
178 | } |
179 | if (re.h >= 2) { |
180 | Sint16 ey = re.y + re.h - 1; |
181 | // Bottom. |
182 | box(SDL_Rect { re.x, ey, re.w, 1 }, c); |
183 | |
184 | Sint16 ex = re.x + re.w - 1; |
185 | Sint16 sy = re.y + 1; |
186 | Uint16 sh = re.h - 2; |
187 | // Left. |
188 | if (re.w >= 1) { |
189 | box(SDL_Rect { re.x, sy, 1, sh }, c); |
190 | } |
191 | // Right. |
192 | if (re.w >= 2) { |
193 | box(SDL_Rect { ex, sy, 1, sh }, c); |
194 | } |
195 | } |
196 | } |
197 | |
198 | void Surface::clearClipRect() { |
199 | SDL_SetClipRect(raw,NULL); |
200 | } |
201 | |
202 | void Surface::setClipRect(int x, int y, int w, int h) { |
203 | SDL_Rect rect = { |
204 | static_cast<Sint16>(x), static_cast<Sint16>(y), |
205 | static_cast<Uint16>(w), static_cast<Uint16>(h) |
206 | }; |
207 | setClipRect(rect); |
208 | } |
209 | |
210 | void Surface::setClipRect(SDL_Rect rect) { |
211 | SDL_SetClipRect(raw,&rect); |
212 | } |
213 | |
214 | void Surface::applyClipRect(SDL_Rect& rect) { |
215 | SDL_Rect clip; |
216 | SDL_GetClipRect(raw, &clip); |
217 | |
218 | // Clip along X-axis. |
219 | if (rect.x < clip.x) { |
220 | rect.w = max(rect.x + rect.w - clip.x, 0); |
221 | rect.x = clip.x; |
222 | } |
223 | if (rect.x + rect.w > clip.x + clip.w) { |
224 | rect.w = max(clip.x + clip.w - rect.x, 0); |
225 | } |
226 | |
227 | // Clip along Y-axis. |
228 | if (rect.y < clip.y) { |
229 | rect.h = max(rect.y + rect.h - clip.y, 0); |
230 | rect.y = clip.y; |
231 | } |
232 | if (rect.y + rect.h > clip.y + clip.h) { |
233 | rect.h = max(clip.y + clip.h - rect.y, 0); |
234 | } |
235 | } |
236 | |
237 | void Surface::blit(Surface *destination, SDL_Rect container, Font::HAlign halign, Font::VAlign valign) const { |
238 | switch (halign) { |
239 | case Font::HAlignLeft: |
240 | break; |
241 | case Font::HAlignCenter: |
242 | container.x += container.w/2-halfW; |
243 | break; |
244 | case Font::HAlignRight: |
245 | container.x += container.w-raw->w; |
246 | break; |
247 | } |
248 | |
249 | switch (valign) { |
250 | case Font::VAlignTop: |
251 | break; |
252 | case Font::VAlignMiddle: |
253 | container.y += container.h/2-halfH; |
254 | break; |
255 | case Font::VAlignBottom: |
256 | container.y += container.h-raw->h; |
257 | break; |
258 | } |
259 | |
260 | blit(destination,container.x,container.y); |
261 | } |
262 | |
263 | static inline uint32_t mult8x4(uint32_t c, uint8_t a) { |
264 | return ((((c >> 8) & 0x00FF00FF) * a) & 0xFF00FF00) |
265 | | ((((c & 0x00FF00FF) * a) & 0xFF00FF00) >> 8); |
266 | } |
267 | |
268 | void Surface::fillRectAlpha(SDL_Rect rect, RGBAColor c) { |
269 | applyClipRect(rect); |
270 | if (rect.w == 0 || rect.h == 0) { |
271 | // Entire rectangle is outside clipping area. |
272 | return; |
273 | } |
274 | |
275 | if (SDL_MUSTLOCK(raw)) { |
276 | if (SDL_LockSurface(raw) < 0) { |
277 | return; |
278 | } |
279 | } |
280 | |
281 | SDL_PixelFormat *format = raw->format; |
282 | uint32_t color = c.pixelValue(format); |
283 | uint8_t alpha = c.a; |
284 | |
285 | uint8_t* edge = static_cast<uint8_t*>(raw->pixels) |
286 | + rect.y * raw->pitch |
287 | + rect.x * format->BytesPerPixel; |
288 | |
289 | // Blending: surf' = surf * (1 - alpha) + fill * alpha |
290 | |
291 | if (format->BytesPerPixel == 2) { |
292 | uint32_t Rmask = format->Rmask; |
293 | uint32_t Gmask = format->Gmask; |
294 | uint32_t Bmask = format->Bmask; |
295 | |
296 | // Pre-multiply the fill color. We're hardcoding alpha to 1: 15/16bpp |
297 | // modes are unlikely to have an alpha channel and even if they do, |
298 | // the written alpha isn't used by gmenu2x. |
299 | uint16_t f = (((color & Rmask) * alpha >> 8) & Rmask) |
300 | | (((color & Gmask) * alpha >> 8) & Gmask) |
301 | | (((color & Bmask) * alpha >> 8) & Bmask) |
302 | | format->Amask; |
303 | alpha = 255 - alpha; |
304 | |
305 | for (auto y = 0; y < rect.h; y++) { |
306 | for (auto x = 0; x < rect.w; x++) { |
307 | uint16_t& pixel = reinterpret_cast<uint16_t*>(edge)[x]; |
308 | uint32_t R = ((pixel & Rmask) * alpha >> 8) & Rmask; |
309 | uint32_t G = ((pixel & Gmask) * alpha >> 8) & Gmask; |
310 | uint32_t B = ((pixel & Bmask) * alpha >> 8) & Bmask; |
311 | pixel = uint16_t(R | G | B) + f; |
312 | } |
313 | edge += raw->pitch; |
314 | } |
315 | } else if (format->BytesPerPixel == 4) { |
316 | // Assume the pixel format uses 8 bits per component; we don't care |
317 | // which component is where since they all blend the same. |
318 | uint32_t f = mult8x4(color, alpha); // pre-multiply the fill color |
319 | alpha = 255 - alpha; |
320 | |
321 | for (auto y = 0; y < rect.h; y++) { |
322 | for (auto x = 0; x < rect.w; x++) { |
323 | uint32_t& pixel = reinterpret_cast<uint32_t*>(edge)[x]; |
324 | pixel = mult8x4(pixel, alpha) + f; |
325 | } |
326 | edge += raw->pitch; |
327 | } |
328 | } else { |
329 | assert(false); |
330 | } |
331 | |
332 | if (SDL_MUSTLOCK(raw)) { |
333 | SDL_UnlockSurface(raw); |
334 | } |
335 | } |
336 |
Branches:
install_locations
master
opkrun
packages