ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

pixelbuffer.h

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef GRAPHICS_PIXELBUFFER_H
00024 #define GRAPHICS_PIXELBUFFER_H
00025 
00026 #include "common/types.h"
00027 #include "common/endian.h"
00028 #include "common/textconsole.h"
00029 
00030 #include "graphics/colormasks.h"
00031 #include "graphics/pixelformat.h"
00032 
00033 namespace Graphics {
00034 
00035 class PixelBuffer {
00036 public:
00041     template<int format>
00042     inline static PixelBuffer createBuffer(int buffersize, DisposeAfterUse::Flag dispose) {
00043         return PixelBuffer(createPixelFormat<format>(), buffersize, dispose);
00044     }
00045 
00050     template<int format>
00051     inline static PixelBuffer createBuffer(byte *buffer) {
00052         return PixelBuffer(createPixelFormat<format>(), buffer);
00053     }
00054 
00058     PixelBuffer();
00066     PixelBuffer(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose);
00073     PixelBuffer(const Graphics::PixelFormat &format, byte *buffer);
00078     PixelBuffer(const PixelBuffer &buf);
00082     ~PixelBuffer();
00083 
00091     void create(const Graphics::PixelFormat &format, int buffersize, DisposeAfterUse::Flag dispose);
00099     void create(int buffersize, DisposeAfterUse::Flag dispose);
00100 
00107     void set(const Graphics::PixelFormat &format, byte *buffer);
00108 
00112     void free();
00113 
00119     void clear(int length);
00120 
00124     inline void setPixelAt(int pixel, uint32 value) {
00125         switch (_format.bytesPerPixel) {
00126         case 2:
00127             ((uint16 *) _buffer)[pixel] = TO_LE_16((uint16) value);
00128             return;
00129         case 3:
00130             pixel *= 3;
00131             value = TO_LE_32(value);
00132             _buffer[pixel + 0] = value & 0xFF;
00133             _buffer[pixel + 1] = (value >> 8) & 0xFF;
00134             _buffer[pixel + 2] = (value >> 16) & 0xFF;
00135             return;
00136         case 4:
00137             ((uint32 *) _buffer)[pixel] = TO_LE_32(value);
00138             return;
00139         }
00140         error("setPixelAt: Unhandled bytesPerPixel %i", _format.bytesPerPixel);
00141     }
00149     inline void setPixelAt(int pixel, const PixelBuffer &buf) { setPixelAt(pixel, buf, pixel); }
00157     inline void setPixelAt(int thisPix, const PixelBuffer &buf, int otherPix) {
00158         if (_format == buf._format) {
00159             memcpy(getRawBuffer(thisPix), buf.getRawBuffer(otherPix), _format.bytesPerPixel);
00160             return;
00161         }
00162         uint8 a, r, g, b;
00163         buf.getARGBAt(otherPix, a, r, g, b);
00164         setPixelAt(thisPix, a, r, g, b);
00165     }
00169     inline void setPixelAt(int pixel, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.RGBToColor(r, g, b)); }
00173     inline void setPixelAt(int pixel, uint8 a, uint8 r, uint8 g, uint8 b) { setPixelAt(pixel, _format.ARGBToColor(a, r, g, b)); }
00174 
00182     inline void copyBuffer(int from, int length, const PixelBuffer &buf) { copyBuffer(from, from, length, buf); }
00191     void copyBuffer(int thisFrom, int otherFrom, int length, const PixelBuffer &buf);
00192 
00197     inline void shiftBy(int amount) { _buffer += amount * _format.bytesPerPixel; }
00198 
00202     inline uint32 getValueAt(int i) const {
00203         switch (_format.bytesPerPixel) {
00204         case 2:
00205             return FROM_LE_16(((uint16 *) _buffer)[i]);
00206         case 3:
00207             i *= 3;
00208 #if defined(SCUMM_BIG_ENDIAN)
00209             return (_buffer[i + 0] << 16) | (_buffer[i + 1] << 8) | _buffer[i + 2];
00210 #elif defined(SCUMM_LITTLE_ENDIAN)
00211             return _buffer[i + 0] | (_buffer[i + 1] << 8) | (_buffer[i + 2] << 16);
00212 #endif
00213         case 4:
00214             return FROM_LE_32(((uint32 *) _buffer)[i]);
00215         }
00216         error("getValueAt: Unhandled bytesPerPixel %i", _format.bytesPerPixel);
00217     }
00221     inline void getRGBAt(int i, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToRGB(getValueAt(i), r, g, b); }
00225     inline void getARGBAt(int i, uint8 &a, uint8 &r, uint8 &g, uint8 &b) const { _format.colorToARGB(getValueAt(i), a, r, g, b); }
00226 
00230     inline byte *getRawBuffer() const { return _buffer; }
00234     inline byte *getRawBuffer(int pixel) const { return _buffer + _format.bytesPerPixel * pixel; }
00235 
00239     inline const PixelFormat &getFormat() const { return _format; }
00240 
00245     PixelBuffer &operator=(const PixelBuffer &buf);
00251     PixelBuffer &operator=(byte *buffer);
00252 
00258     inline operator bool() const { return (_buffer); }
00259 
00260 private:
00261     byte *_buffer;
00262     Graphics::PixelFormat _format;
00263     DisposeAfterUse::Flag _dispose;
00264 };
00265 
00266 }
00267 
00268 #endif


Generated on Sat Dec 7 2019 05:00:41 for ResidualVM by doxygen 1.7.1
curved edge   curved edge