ScummVM API documentation
psppixelformat.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef PSP_PIXEL_FORMAT_H
23#define PSP_PIXEL_FORMAT_H
24
25#include "graphics/pixelformat.h"
26#include "backends/platform/psp/trace.h"
27
35 enum Type {
36 Type_None,
37 Type_4444,
38 Type_5551,
39 Type_5650,
40 Type_8888,
41 Type_Palette_8bit,
42 Type_Palette_4bit,
43 Type_Unknown
44 };
45
46 Type format;
47 uint32 bitsPerPixel;
48
49 PSPPixelFormat() : format(Type_Unknown), bitsPerPixel(0) {}
50 void set(Type type);
51 static void convertFromScummvmPixelFormat(const Graphics::PixelFormat *pf,
52 PSPPixelFormat::Type &bufferType,
53 PSPPixelFormat::Type &paletteType,
54 bool &fakeAlpha);
55 static Graphics::PixelFormat convertToScummvmPixelFormat(PSPPixelFormat::Type type, bool fakeAlpha = false);
56
57 inline uint32 rgbaToColor(uint32 r, uint32 g, uint32 b, uint32 a) const {
58 uint32 color;
59
60 switch (format) {
61 case Type_4444:
62 color = (((b >> 4) << 8) | ((g >> 4) << 4) | ((r >> 4) << 0) | ((a >> 4) << 12));
63 break;
64 case Type_5551:
65 color = (((b >> 3) << 10) | ((g >> 3) << 5) | ((r >> 3) << 0) | ((a >> 7) << 15));
66 break;
67 case Type_5650:
68 color = (((b >> 3) << 11) | ((g >> 2) << 5) | ((r >> 3) << 0));
69 break;
70 case Type_8888:
71 color = (((b >> 0) << 16) | ((g >> 0) << 8) | ((r >> 0) << 0) | ((a >> 0) << 24));
72 break;
73 default:
74 color = 0;
75 break;
76 }
77 return color;
78 }
79
80 inline void colorToRgba(uint32 color, uint32 &r, uint32 &g, uint32 &b, uint32 &a) const {
81 switch (format) {
82 case Type_4444:
83 a = (color >> 12) & 0xF; // Interpolate to get true colors
84 b = (color >> 8) & 0xF;
85 g = (color >> 4) & 0xF;
86 r = (color >> 0) & 0xF;
87 a = a << 4 | a;
88 b = b << 4 | b;
89 g = g << 4 | g;
90 r = r << 4 | r;
91 break;
92 case Type_5551:
93 a = (color >> 15) ? 0xFF : 0;
94 b = (color >> 10) & 0x1F;
95 g = (color >> 5) & 0x1F;
96 r = (color >> 0) & 0x1F;
97 b = b << 3 | b >> 2;
98 g = g << 3 | g >> 2;
99 r = r << 3 | r >> 2;
100 break;
101 case Type_5650:
102 a = 0xFF;
103 b = (color >> 11) & 0x1F;
104 g = (color >> 5) & 0x3F;
105 r = (color >> 0) & 0x1F;
106 b = b << 3 | b >> 2;
107 g = g << 2 | g >> 4;
108 r = r << 3 | r >> 2;
109 break;
110 case Type_8888:
111 a = (color >> 24) & 0xFF;
112 b = (color >> 16) & 0xFF;
113 g = (color >> 8) & 0xFF;
114 r = (color >> 0) & 0xFF;
115 break;
116 default:
117 a = b = g = r = 0;
118 break;
119 }
120 }
121
122 inline uint32 setColorAlpha(uint32 color, byte alpha) {
123 switch (format) {
124 case Type_4444:
125 color = (color & 0x0FFF) | (((uint32)alpha >> 4) << 12);
126 break;
127 case Type_5551:
128 color = (color & 0x7FFF) | (((uint32)alpha >> 7) << 15);
129 break;
130 case Type_8888:
131 color = (color & 0x00FFFFFF) | ((uint32)alpha << 24);
132 break;
133 case Type_5650:
134 default:
135 break;
136 }
137 return color;
138 }
139
140 inline uint32 pixelsToBytes(uint32 pixels) const {
141 switch (bitsPerPixel) {
142 case 4:
143 pixels >>= 1;
144 break;
145 case 16:
146 pixels <<= 1;
147 break;
148 case 32:
149 pixels <<= 2;
150 break;
151 case 8:
152 break;
153 default:
154 PSP_ERROR("Incorrect bitsPerPixel value[%u]. pixels[%u]\n", bitsPerPixel, pixels);
155 break;
156 }
157 return pixels;
158 }
159
160 // Return whatever color we point at
161 inline uint32 getColorValueAt(byte *pointer) const {
162 uint32 result;
163
164 switch (bitsPerPixel) {
165 case 4: // We can't distinguish a 4 bit color with a pointer
166 case 8:
167 result = *pointer;
168 break;
169 case 16:
170 result = *(uint16 *)pointer;
171 break;
172 case 32:
173 result = *(uint32 *)pointer;
174 break;
175 default:
176 result = 0;
177 PSP_ERROR("Incorrect bitsPerPixel value[%u].\n", bitsPerPixel);
178 break;
179 }
180 return result;
181 }
182};
183
184#endif /* PSP_PIXEL_FORMAT_H */
Definition: pixelformat.h:138
Definition: psppixelformat.h:34
uint32 bitsPerPixel
Must match bpp of selected type.
Definition: psppixelformat.h:47