ScummVM API documentation
pixelbuffer.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 GRAPHICS_PIXELBUFFER_H
23 #define GRAPHICS_PIXELBUFFER_H
24 
25 #include "common/types.h"
26 #include "common/endian.h"
27 #include "common/textconsole.h"
28 
29 #include "graphics/pixelformat.h"
30 
31 namespace Graphics {
32 
42 class PixelBuffer {
43 public:
47  PixelBuffer();
55  PixelBuffer(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose);
62  PixelBuffer(const Graphics::PixelFormat &format, byte *buffer);
67  PixelBuffer(const PixelBuffer &buf);
71  ~PixelBuffer();
72 
80  void create(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose);
88  void create(int buffersize, DisposeAfterUse::Flag dispose);
89 
96  void set(const Graphics::PixelFormat &format, byte *buffer);
97 
101  void free();
102 
108  void clear(uint length);
109 
113  inline void setPixelAt(int pixel, uint32 value) {
114  switch (_format.bytesPerPixel) {
115  case 2:
116  ((uint16 *) _buffer)[pixel] = value;
117  return;
118  case 3:
119  pixel *= 3;
120 #if defined(SCUMM_BIG_ENDIAN)
121  _buffer[pixel + 0] = (value >> 16) & 0xFF;
122  _buffer[pixel + 1] = (value >> 8) & 0xFF;
123  _buffer[pixel + 2] = value & 0xFF;
124 #elif defined(SCUMM_LITTLE_ENDIAN)
125  _buffer[pixel + 0] = value & 0xFF;
126  _buffer[pixel + 1] = (value >> 8) & 0xFF;
127  _buffer[pixel + 2] = (value >> 16) & 0xFF;
128 #endif
129  return;
130  case 4:
131  ((uint32 *) _buffer)[pixel] = value;
132  return;
133  }
134  error("setPixelAt: Unhandled bytesPerPixel %d", int(_format.bytesPerPixel));
135  }
143  inline void setPixelAt(int pixel, const PixelBuffer &buf) { setPixelAt(pixel, buf, pixel); }
151  inline void setPixelAt(int thisPix, const PixelBuffer &buf, int otherPix) {
152  if (_format == buf._format) {
153  memcpy(getRawBuffer(thisPix), buf.getRawBuffer(otherPix), _format.bytesPerPixel);
154  return;
155  }
156  uint8 a, r, g, b;
157  buf.getARGBAt(otherPix, a, r, g, b);
158  setPixelAt(thisPix, a, r, g, b);
159  }
163  inline void setPixelAt(int pixel, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.RGBToColor(r, g, b)); }
167  inline void setPixelAt(int pixel, uint8 a, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.ARGBToColor(a, r, g, b)); }
168 
176  inline void copyBuffer(int from, int length, const PixelBuffer &buf) { copyBuffer(from, from, length, buf); }
185  void copyBuffer(int thisFrom, int otherFrom, int length, const PixelBuffer &buf);
186 
191  inline void shiftBy(int amount) { _buffer += amount * _format.bytesPerPixel; }
192 
196  inline uint32 getValueAt(int i) const {
197  switch (_format.bytesPerPixel) {
198  case 2:
199  return ((uint16 *) _buffer)[i];
200  case 3:
201  i *= 3;
202 #if defined(SCUMM_BIG_ENDIAN)
203  return (_buffer[i + 0] << 16) | (_buffer[i + 1] << 8) | _buffer[i + 2];
204 #elif defined(SCUMM_LITTLE_ENDIAN)
205  return _buffer[i + 0] | (_buffer[i + 1] << 8) | (_buffer[i + 2] << 16);
206 #endif
207  case 4:
208  return ((uint32 *) _buffer)[i];
209  }
210  error("getValueAt: Unhandled bytesPerPixel %d", int(_format.bytesPerPixel));
211  }
215  inline void getRGBAt(int i, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToRGB(getValueAt(i), r, g, b); }
219  inline void getARGBAt(int i, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToARGB(getValueAt(i), a, r, g, b); }
220 
224  inline byte *getRawBuffer() const { return _buffer; }
228  inline byte *getRawBuffer(int pixel) const { return _buffer + _format.bytesPerPixel * pixel; }
229 
233  inline const PixelFormat &getFormat() const { return _format; }
234 
239  PixelBuffer &operator=(const PixelBuffer &buf);
245  PixelBuffer &operator=(byte *buffer);
246 
252  inline operator bool() const { return (_buffer); }
253 
254 private:
255  byte *_buffer;
256  Graphics::PixelFormat _format;
257  DisposeAfterUse::Flag _dispose;
258 };
259 
260 }
262 #endif
PixelBuffer & operator=(const PixelBuffer &buf)
void getARGBAt(int i, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const
Definition: pixelbuffer.h:219
Definition: pixelformat.h:138
void setPixelAt(int pixel, const PixelBuffer &buf)
Definition: pixelbuffer.h:143
void shiftBy(int amount)
Definition: pixelbuffer.h:191
byte * getRawBuffer(int pixel) const
Definition: pixelbuffer.h:228
const PixelFormat & getFormat() const
Definition: pixelbuffer.h:233
void setPixelAt(int pixel, uint8 r, uint8 g, uint8 b)
Definition: pixelbuffer.h:163
void setPixelAt(int pixel, uint32 value)
Definition: pixelbuffer.h:113
void clear(uint length)
void setPixelAt(int pixel, uint8 a, uint8 r, uint8 g, uint8 b)
Definition: pixelbuffer.h:167
Definition: pixelbuffer.h:42
void colorToRGB(uint32 color, uint8 &r, uint8 &g, uint8 &b) const
Definition: pixelformat.h:241
void copyBuffer(int from, int length, const PixelBuffer &buf)
Definition: pixelbuffer.h:176
void setPixelAt(int thisPix, const PixelBuffer &buf, int otherPix)
Definition: pixelbuffer.h:151
Definition: formatinfo.h:28
void create(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose)
void getRGBAt(int i, uint8 &r, uint8 &g, uint8 &b) const
Definition: pixelbuffer.h:215
uint32 getValueAt(int i) const
Definition: pixelbuffer.h:196
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
byte * getRawBuffer() const
Definition: pixelbuffer.h:224
uint32 ARGBToColor(uint8 a, uint8 r, uint8 g, uint8 b) const
Definition: pixelformat.h:224
byte bytesPerPixel
Definition: pixelformat.h:139
uint32 RGBToColor(uint8 r, uint8 g, uint8 b) const
Definition: pixelformat.h:207
void colorToARGB(uint32 color, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const
Definition: pixelformat.h:255