ScummVM API documentation
surface.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_SURFACE_H
23#define GRAPHICS_SURFACE_H
24
25#include "common/scummsys.h"
26#include "common/endian.h"
27#include "common/list.h"
28
29namespace Common {
30struct Rect;
31struct Point;
32}
33
34#include "graphics/pixelformat.h"
35#include "graphics/transform_struct.h"
36
37namespace Graphics {
38
48struct TransformStruct;
49
50enum DitherMethod {
51 kDitherNaive,
52 kDitherFloyd,
53 kDitherAtkinson,
54 kDitherBurkes,
55 kDitherFalseFloyd,
56 kDitherSierra,
57 kDitherSierraTwoRow,
58 kDitherSierraLite,
59 kDitherStucki,
60 kDitherJarvis,
61};
62
67struct Surface {
71 int16 w;
72
76 int16 h;
77
83 int32 pitch;
84
85protected:
89 void *pixels;
90
91public:
96
100 Surface() : w(0), h(0), pitch(0), pixels(0), format() {
101 }
102
103 Common::Rect getRect() const;
104
110 inline const void *getPixels() const {
111 return pixels;
112 }
113
119 inline void *getPixels() {
120 return pixels;
121 }
122
130 void setPixels(void *newPixels) { pixels = newPixels; }
131
140 inline const void *getBasePtr(int x, int y) const {
141 return (const byte *)(pixels) + y * pitch + x * format.bytesPerPixel;
142 }
143
152 inline void *getBasePtr(int x, int y) {
153 return static_cast<byte *>(pixels) + y * pitch + x * format.bytesPerPixel;
154 }
155
164 inline uint32 getPixel(int x, int y) const {
165 assert(format.bytesPerPixel > 0 && format.bytesPerPixel <= 4);
166 if (format.bytesPerPixel == 1)
167 return *((const uint8 *)getBasePtr(x, y));
168 else if (format.bytesPerPixel == 2)
169 return *((const uint16 *)getBasePtr(x, y));
170 else if (format.bytesPerPixel == 3)
171 return READ_UINT24(getBasePtr(x, y));
172 else if (format.bytesPerPixel == 4)
173 return *((const uint32 *)getBasePtr(x, y));
174 else
175 return 0;
176 }
177
185 inline void setPixel(int x, int y, int pixel) {
186 assert(format.bytesPerPixel > 0 && format.bytesPerPixel <= 4);
187 assert(x >= 0 && x < w && y >= 0 && y < h);
188 if (format.bytesPerPixel == 1)
189 *((uint8 *)getBasePtr(x, y)) = pixel;
190 else if (format.bytesPerPixel == 2)
191 *((uint16 *)getBasePtr(x, y)) = pixel;
192 else if (format.bytesPerPixel == 3)
193 WRITE_UINT24(getBasePtr(x, y), pixel);
194 else if (format.bytesPerPixel == 4)
195 *((uint32 *)getBasePtr(x, y)) = pixel;
196 }
197
207 void create(int16 width, int16 height, const PixelFormat &format);
208
217 void free();
218
231 void init(int16 width, int16 height, int16 pitch, void *pixels, const PixelFormat &format);
232
242 void copyFrom(const Surface &surf);
243
254 void convertFrom(const Surface &surf, const PixelFormat &format);
255
273
289 const Surface getSubArea(const Common::Rect &area) const;
290
294 bool clip(Common::Rect &srcBounds, Common::Rect &destBounds, uint src_w = 0, uint src_h = 0, byte flip = FLIP_NONE) const;
295
308 void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height);
319 void copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect &subRect);
320
334 void copyRectToSurfaceWithKey(const void *buffer, int srcPitch, int destX, int destY, int width, int height, uint32 key);
335
347 void copyRectToSurfaceWithKey(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect &subRect, uint32 key);
348
361 inline void convertToInPlace(const PixelFormat &dstFormat) {
362 convertToInPlace(dstFormat, nullptr, 0);
363 }
364
379 void convertToInPlace(const PixelFormat &dstFormat, const byte *palette, uint16 paletteCount);
380
394 Graphics::Surface *convertTo(const PixelFormat &dstFormat, const byte *srcPalette = 0, int srcPaletteCount = 256, const byte *dstPalette = 0, int dstPaletteCount = 0, DitherMethod method = kDitherFloyd) const;
395
396protected:
397 void ditherFloyd(const byte *srcPalette, int srcPaletteCount, Surface *dstSurf, const byte *dstPalette, int dstPaletteCount, DitherMethod method, const PixelFormat &dstFormat) const;
398
399public:
400
412 void drawLine(int x0, int y0, int x1, int y1, uint32 color);
413
429 void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color);
430
441 void drawRoundRect(const Common::Rect &rect, int arc, uint32 color, bool filled);
442
454 void drawPolygonScan(const int *polyX, const int *polyY, int npoints, const Common::Rect &bbox, uint32 color);
455
468 void drawEllipse(int x0, int y0, int x1, int y1, uint32 color, bool filled);
469
479 void hLine(int x, int y, int x2, uint32 color);
480
490 void vLine(int x, int y, int y2, uint32 color);
491
498 void fillRect(Common::Rect r, uint32 color);
499
506 void frameRect(const Common::Rect &r, uint32 color);
507
512 void move(int dx, int dy, int height);
513
520
527
536 bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha = false);
537
549 bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha,
550 uint8 rNew, uint8 gNew, uint8 bNew);
551
557 bool setAlpha(uint8 alpha, bool skipTransparent = false);
558
562 AlphaType detectAlpha() const;
563
575 Graphics::Surface *scale(int16 newWidth, int16 newHeight, bool filtering = false, byte flip = 0) const;
576
588 Graphics::Surface *rotoscale(const TransformStruct &transform, bool filtering = false) const;
589
604 void debugPrint(int debuglevel = 0, int width = 0, int height = 0, int x = 0, int y = 0, int scale = -1, int maxwidth = 160, const byte *palette = NULL) const;
605};
606
613 void operator()(Surface *ptr) {
614 if (ptr) {
615 ptr->free();
616 }
617 delete ptr;
618 }
619};
620
632public:
640 FloodFill(Surface *surface, uint32 oldColor, uint32 fillColor, bool maskMode = false);
641 ~FloodFill();
642
649 void addSeed(int x, int y);
650
656 void fill();
657
666 void fillMask();
667
673 Surface *getMask() { return _mask; }
674
675private:
677 Surface *_surface;
678 Surface *_mask;
679 uint32 _oldColor, _fillColor;
680 byte *_visited;
681 int _w, _h;
682
683 bool _maskMode;
684};
686} // End of namespace Graphics
687
688
689#endif
Definition: surface.h:631
FloodFill(Surface *surface, uint32 oldColor, uint32 fillColor, bool maskMode=false)
void addSeed(int x, int y)
Surface * getMask()
Definition: surface.h:673
Definition: algorithm.h:29
Definition: formatinfo.h:28
@ FLIP_NONE
The image will not be flipped.
Definition: transform_struct.h:49
Definition: rect.h:536
Definition: pixelformat.h:138
byte bytesPerPixel
Definition: pixelformat.h:139
Definition: surface.h:612
Definition: surface.h:67
uint32 getPixel(int x, int y) const
Definition: surface.h:164
int16 h
Definition: surface.h:76
void init(int16 width, int16 height, int16 pitch, void *pixels, const PixelFormat &format)
void copyRectToSurfaceWithKey(const void *buffer, int srcPitch, int destX, int destY, int width, int height, uint32 key)
void flipHorizontal(const Common::Rect &r)
AlphaType detectAlpha() const
Graphics::Surface * rotoscale(const TransformStruct &transform, bool filtering=false) const
Rotoscale function; this returns a transformed version of this surface after rotation and scaling....
Surface()
Definition: surface.h:100
void fillRect(Common::Rect r, uint32 color)
const Surface getSubArea(const Common::Rect &area) const
Graphics::Surface * convertTo(const PixelFormat &dstFormat, const byte *srcPalette=0, int srcPaletteCount=256, const byte *dstPalette=0, int dstPaletteCount=0, DitherMethod method=kDitherFloyd) const
void convertFrom(const Surface &surf, const PixelFormat &format)
const void * getBasePtr(int x, int y) const
Definition: surface.h:140
void debugPrint(int debuglevel=0, int width=0, int height=0, int x=0, int y=0, int scale=-1, int maxwidth=160, const byte *palette=NULL) const
void convertToInPlace(const PixelFormat &dstFormat)
Definition: surface.h:361
bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha=false)
void frameRect(const Common::Rect &r, uint32 color)
void convertToInPlace(const PixelFormat &dstFormat, const byte *palette, uint16 paletteCount)
void hLine(int x, int y, int x2, uint32 color)
void move(int dx, int dy, int height)
bool setAlpha(uint8 alpha, bool skipTransparent=false)
void copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect &subRect)
void copyRectToSurfaceWithKey(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect &subRect, uint32 key)
void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color)
Surface getSubArea(const Common::Rect &area)
void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height)
void * getPixels()
Definition: surface.h:119
void drawRoundRect(const Common::Rect &rect, int arc, uint32 color, bool filled)
void setPixel(int x, int y, int pixel)
Definition: surface.h:185
void flipVertical(const Common::Rect &r)
bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha, uint8 rNew, uint8 gNew, uint8 bNew)
void * getBasePtr(int x, int y)
Definition: surface.h:152
bool clip(Common::Rect &srcBounds, Common::Rect &destBounds, uint src_w=0, uint src_h=0, byte flip=FLIP_NONE) const
Graphics::Surface * scale(int16 newWidth, int16 newHeight, bool filtering=false, byte flip=0) const
void setPixels(void *newPixels)
Definition: surface.h:130
void * pixels
Definition: surface.h:89
int32 pitch
Definition: surface.h:83
void drawLine(int x0, int y0, int x1, int y1, uint32 color)
void drawEllipse(int x0, int y0, int x1, int y1, uint32 color, bool filled)
int16 w
Definition: surface.h:71
void vLine(int x, int y, int y2, uint32 color)
const void * getPixels() const
Definition: surface.h:110
void drawPolygonScan(const int *polyX, const int *polyY, int npoints, const Common::Rect &bbox, uint32 color)
void create(int16 width, int16 height, const PixelFormat &format)
PixelFormat format
Definition: surface.h:95
void copyFrom(const Surface &surf)
Definition: transform_struct.h:75
Definition: display_client.h:58