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_8888_RGBA,
42  Type_Palette_8bit,
43  Type_Palette_4bit,
44  Type_Unknown
45  };
46 
47  Type format;
48  uint32 bitsPerPixel;
49  bool swapRB;
50 
51  PSPPixelFormat() : format(Type_Unknown), bitsPerPixel(0), swapRB(false) {}
52  void set(Type type, bool swap = false);
53  static void convertFromScummvmPixelFormat(const Graphics::PixelFormat *pf,
54  PSPPixelFormat::Type &bufferType,
55  PSPPixelFormat::Type &paletteType,
56  bool &swapRedBlue);
57  static Graphics::PixelFormat convertToScummvmPixelFormat(PSPPixelFormat::Type type);
58  uint32 convertTo32BitColor(uint32 color) const;
59 
60  inline uint32 rgbaToColor(uint32 r, uint32 g, uint32 b, uint32 a) const {
61  uint32 color;
62 
63  switch (format) {
64  case Type_4444:
65  color = (((b >> 4) << 8) | ((g >> 4) << 4) | ((r >> 4) << 0) | ((a >> 4) << 12));
66  break;
67  case Type_5551:
68  color = (((b >> 3) << 10) | ((g >> 3) << 5) | ((r >> 3) << 0) | ((a >> 7) << 15));
69  break;
70  case Type_5650:
71  color = (((b >> 3) << 11) | ((g >> 2) << 5) | ((r >> 3) << 0));
72  break;
73  case Type_8888:
74  case Type_8888_RGBA:
75  color = (((b >> 0) << 16) | ((g >> 0) << 8) | ((r >> 0) << 0) | ((a >> 0) << 24));
76  break;
77  default:
78  color = 0;
79  break;
80  }
81  return color;
82  }
83 
84  inline void colorToRgba(uint32 color, uint32 &r, uint32 &g, uint32 &b, uint32 &a) const {
85  switch (format) {
86  case Type_4444:
87  a = (color >> 12) & 0xF; // Interpolate to get true colors
88  b = (color >> 8) & 0xF;
89  g = (color >> 4) & 0xF;
90  r = (color >> 0) & 0xF;
91  a = a << 4 | a;
92  b = b << 4 | b;
93  g = g << 4 | g;
94  r = r << 4 | r;
95  break;
96  case Type_5551:
97  a = (color >> 15) ? 0xFF : 0;
98  b = (color >> 10) & 0x1F;
99  g = (color >> 5) & 0x1F;
100  r = (color >> 0) & 0x1F;
101  b = b << 3 | b >> 2;
102  g = g << 3 | g >> 2;
103  r = r << 3 | r >> 2;
104  break;
105  case Type_5650:
106  a = 0xFF;
107  b = (color >> 11) & 0x1F;
108  g = (color >> 5) & 0x3F;
109  r = (color >> 0) & 0x1F;
110  b = b << 3 | b >> 2;
111  g = g << 2 | g >> 4;
112  r = r << 3 | r >> 2;
113  break;
114  case Type_8888:
115  case Type_8888_RGBA:
116  a = (color >> 24) & 0xFF;
117  b = (color >> 16) & 0xFF;
118  g = (color >> 8) & 0xFF;
119  r = (color >> 0) & 0xFF;
120  break;
121  default:
122  a = b = g = r = 0;
123  break;
124  }
125  }
126 
127  inline uint32 setColorAlpha(uint32 color, byte alpha) {
128  switch (format) {
129  case Type_4444:
130  color = (color & 0x0FFF) | (((uint32)alpha >> 4) << 12);
131  break;
132  case Type_5551:
133  color = (color & 0x7FFF) | (((uint32)alpha >> 7) << 15);
134  break;
135  case Type_8888:
136  case Type_8888_RGBA:
137  color = (color & 0x00FFFFFF) | ((uint32)alpha << 24);
138  break;
139  case Type_5650:
140  default:
141  break;
142  }
143  return color;
144  }
145 
146  inline uint32 pixelsToBytes(uint32 pixels) const {
147  switch (bitsPerPixel) {
148  case 4:
149  pixels >>= 1;
150  break;
151  case 16:
152  pixels <<= 1;
153  break;
154  case 32:
155  pixels <<= 2;
156  break;
157  case 8:
158  break;
159  default:
160  PSP_ERROR("Incorrect bitsPerPixel value[%u]. pixels[%u]\n", bitsPerPixel, pixels);
161  break;
162  }
163  return pixels;
164  }
165 
166  inline uint16 swapRedBlue16(uint16 color) const {
167  uint16 output;
168 
169  switch (format) {
170  case Type_4444:
171  output = (color & 0xf0f0) | ((color & 0x000f) << 8) | ((color & 0x0f00) >> 8);
172  break;
173  case Type_5551:
174  output = (color & 0x83e0) | ((color & 0x001f) << 10) | ((color & 0x7c00) >> 10);
175  break;
176  case Type_5650:
177  output = (color & 0x07e0) | ((color & 0x001f) << 11) | ((color & 0xf800) >> 11);
178  break;
179  default:
180  PSP_ERROR("invalid format[%u] for swapping\n", format);
181  output = 0;
182  break;
183  }
184  return output;
185  }
186 
187  inline uint32 swapRedBlue32(uint32 color) const {
188  uint32 output;
189 
190  switch (format) {
191  case Type_4444:
192  output = (color & 0xf0f0f0f0) |
193  ((color & 0x000f000f) << 8) | ((color & 0x0f000f00) >> 8);
194  break;
195  case Type_5551:
196  output = (color & 0x83e083e0) |
197  ((color & 0x001f001f) << 10) | ((color & 0x7c007c00) >> 10);
198  break;
199  case Type_5650:
200  output = (color & 0x07e007e0) |
201  ((color & 0x001f001f) << 11) | ((color & 0xf800f800) >> 11);
202  break;
203  case Type_8888:
204  output = (color & 0xff00ff00) |
205  ((color & 0x000000ff) << 16) | ((color & 0x00ff0000) >> 16);
206  break;
207  case Type_8888_RGBA:
208  output = ((color & 0x000000ff) << 24) | ((color & 0x0000ff00) << 8) |
209  ((color & 0x00ff0000) >> 8) | ((color & 0xff000000) >> 24);
210  break;
211  default:
212  PSP_ERROR("invalid format[%u] for swapping\n", format);
213  output = 0;
214  break;
215  }
216 
217  return output;
218  }
219 
220  // Return whatever color we point at
221  inline uint32 getColorValueAt(byte *pointer) const {
222  uint32 result;
223 
224  switch (bitsPerPixel) {
225  case 4: // We can't distinguish a 4 bit color with a pointer
226  case 8:
227  result = *pointer;
228  break;
229  case 16:
230  result = *(uint16 *)pointer;
231  break;
232  case 32:
233  result = *(uint32 *)pointer;
234  break;
235  default:
236  result = 0;
237  PSP_ERROR("Incorrect bitsPerPixel value[%u].\n", bitsPerPixel);
238  break;
239  }
240  return result;
241  }
242 };
243 
244 #endif /* PSP_PIXEL_FORMAT_H */
Definition: pixelformat.h:138
uint32 bitsPerPixel
Must match bpp of selected type.
Definition: psppixelformat.h:48
bool swapRB
Swap red and blue values when reading and writing.
Definition: psppixelformat.h:49
Definition: psppixelformat.h:34