1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef GRAPHICS_PIXELBUFFER_H
#define GRAPHICS_PIXELBUFFER_H
#include "common/types.h"
#include "common/endian.h"
#include "common/textconsole.h"
#include "graphics/colormasks.h"
#include "graphics/pixelformat.h"
namespace Graphics {
class PixelBuffer {
public:
/**
* Create a PixelBuffer.
* Convenience syntax for PixelBuffer(createPixelFormat<format>(), buffersize, dispose).
*/
template<int format>
inline static PixelBuffer createBuffer(int buffersize, DisposeAfterUse::Flag dispose) {
return PixelBuffer(createPixelFormat<format>(), buffersize, dispose);
}
/**
* Create a PixelBuffer.
* Convenience syntax for PixelBuffer(createPixelFormat<format>(), buffer).
*/
template<int format>
inline static PixelBuffer createBuffer(byte *buffer) {
return PixelBuffer(createPixelFormat<format>(), buffer);
}
/**
* Construct an empty PixelBuffer.
*/
PixelBuffer();
/**
* Construct a PixelBuffer, allocating the internal buffer.
*
* @param format The format of the pixels in this buffer.
* @param buffersize The number of pixels the buffer will store.
* @param dispose If YES the internal buffer will be deleted when this object is destroyed,
*/
PixelBuffer(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose);
/**
* Construct a PixelBuffer, using an already allocated buffer.
*
* @param format The format of the pixels in this buffer.
* @param buffer The raw buffer containing the pixels.
*/
PixelBuffer(const Graphics::PixelFormat &format, byte *buffer);
/**
* Copy constructor.
* The internal buffer will NOT be duplicated, it will be shared between the instances.
*/
PixelBuffer(const PixelBuffer &buf);
/**
* Destroy the object.
*/
~PixelBuffer();
/**
* Initialize the buffer.
*
* @param format The format of the pixels.
* @param buffersize The number of pixels the buffer will store.
* @param dispose If YES the internal buffer will be deleted when this object is destroyed,
*/
void create(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose);
/**
* Initialize the buffer, using the already set pixel format.
* @note If the pixel format was not set before the results are undefined.
*
* @param buffersize The number of pixels the buffer will store.
* @param dispose If YES the internal buffer will be deleted when this object is destroyed,
*/
void create(int buffersize, DisposeAfterUse::Flag dispose);
/**
* Initialize the buffer.
*
* @param format The format of the pixels in this buffer.
* @param buffer The raw buffer containing the pixels.
*/
void set(const Graphics::PixelFormat &format, byte *buffer);
/**
* Delete the internal pixel buffer.
*/
void free();
/**
* Reset the value of the pixels.
*
* @param length The length of the buffer, in pixels.
*/
void clear(int length);
/**
* Set the value of the pixel at index 'pixel' to 'value',
*/
inline void setPixelAt(int pixel, uint32 value) {
switch (_format.bytesPerPixel) {
case 2:
((uint16 *) _buffer)[pixel] = TO_LE_16((uint16) value);
return;
case 3:
pixel *= 3;
value = TO_LE_32(value);
_buffer[pixel + 0] = value & 0xFF;
_buffer[pixel + 1] = (value >> 8) & 0xFF;
_buffer[pixel + 2] = (value >> 16) & 0xFF;
return;
case 4:
((uint32 *) _buffer)[pixel] = TO_LE_32(value);
return;
}
error("setPixelAt: Unhandled bytesPerPixel %i", _format.bytesPerPixel);
}
/**
* Set the value of a pixel. The pixel will be converted from a pixel in another PixelBuffer,
* at the same index.
*
* @param pixel The index of the pixel to set.
* @param buf The buffer storing the source pixel.
*/
inline void setPixelAt(int pixel, const PixelBuffer &buf) { setPixelAt(pixel, buf, pixel); }
/**
* Set the value of a pixel. The pixel will be converted from a pixel in another PixelBuffer.
*
* @param thisPix The index of the pixel to set.
* @param buf The buffer storing the source pixel.
* @param otherPix The index of the source pixel in 'buf'.
*/
inline void setPixelAt(int thisPix, const PixelBuffer &buf, int otherPix) {
if (_format == buf._format) {
memcpy(getRawBuffer(thisPix), buf.getRawBuffer(otherPix), _format.bytesPerPixel);
return;
}
uint8 a, r, g, b;
buf.getARGBAt(otherPix, a, r, g, b);
setPixelAt(thisPix, a, r, g, b);
}
/**
* Set a pixel, from RGB values.
*/
inline void setPixelAt(int pixel, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.RGBToColor(r, g, b)); }
/**
* Set a pixel, from ARGB values.
*/
inline void setPixelAt(int pixel, uint8 a, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.ARGBToColor(a, r, g, b)); }
/**
* Copy some pixels from a buffer. The pixels will be converted, storing the same ARGB value.
*
* @param from The starting index.
* @param length The number of pixels to copy.
* @param buf The source buffer.
*/
inline void copyBuffer(int from, int length, const PixelBuffer &buf) { copyBuffer(from, from, length, buf); }
/**
* Copy some pixels from a buffer. The pixels will be converted, storing the same ARGB value.
*
* @param thisFrom The starting index.
* @param otherFrom The starting index in the source buffer.
* @param length The number of pixels to copy.
* @param buf The source buffer.
*/
void copyBuffer(int thisFrom, int otherFrom, int length, const PixelBuffer &buf);
/**
* Shift the internal buffer of some pixels, losing some pixels at the start of the buffer.
* The pixels lost will NOT be deleted.
*/
inline void shiftBy(int amount) { _buffer += amount * _format.bytesPerPixel; }
/**
* Return the encoded value of the pixel at the given index.
*/
inline uint32 getValueAt(int i) const {
switch (_format.bytesPerPixel) {
case 2:
return FROM_LE_16(((uint16 *) _buffer)[i]);
case 3:
i *= 3;
#if defined(SCUMM_BIG_ENDIAN)
return (_buffer[i + 0] << 16) | (_buffer[i + 1] << 8) | _buffer[i + 2];
#elif defined(SCUMM_LITTLE_ENDIAN)
return _buffer[i + 0] | (_buffer[i + 1] << 8) | (_buffer[i + 2] << 16);
#endif
case 4:
return FROM_LE_32(((uint32 *) _buffer)[i]);
}
error("getValueAt: Unhandled bytesPerPixel %i", _format.bytesPerPixel);
}
/**
* Return the RGB value of the pixel at the given index.
*/
inline void getRGBAt(int i, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToRGB(getValueAt(i), r, g, b); }
/**
* Return the ARGB value of the pixel at the given index.
*/
inline void getARGBAt(int i, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToARGB(getValueAt(i), a, r, g, b); }
/**
* Return the internal buffer.
*/
inline byte *getRawBuffer() const { return _buffer; }
/**
* Return the internal buffer, pointing at the wanted pixel.
*/
inline byte *getRawBuffer(int pixel) const { return _buffer + _format.bytesPerPixel * pixel; }
/**
* Return the pixel format used.
*/
inline const PixelFormat &getFormat() const { return _format; }
/**
* Copy a PixelBuffer object.
* The internal buffer will NOT be duplicated, it will be shared between the instances.
*/
PixelBuffer &operator=(const PixelBuffer &buf);
/**
* Set the internal buffer to an already allocated array.
*
* @param buffer The pointer to the array.
*/
PixelBuffer &operator=(byte *buffer);
/**
* Check if the interal buffer is allocated.
*
* @returns true if allocated.
*/
inline operator bool() const { return (_buffer); }
private:
byte *_buffer;
Graphics::PixelFormat _format;
DisposeAfterUse::Flag _dispose;
};
}
#endif
|