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 
29 namespace Common {
30 struct Rect;
31 struct Point;
32 }
33 
34 #include "graphics/pixelformat.h"
35 
36 namespace Graphics {
37 
47 struct TransformStruct;
48 
49 enum DitherMethod {
50  kDitherNaive,
51  kDitherFloyd,
52  kDitherAtkinson,
53  kDitherBurkes,
54  kDitherFalseFloyd,
55  kDitherSierra,
56  kDitherSierraTwoRow,
57  kDitherSierraLite,
58  kDitherStucki,
59  kDitherJarvis,
60 };
61 
66 struct Surface {
70  int16 w;
71 
75  int16 h;
76 
82  int32 pitch;
83 
84 protected:
88  void *pixels;
89 
90 public:
95 
99  Surface() : w(0), h(0), pitch(0), pixels(0), format() {
100  }
101 
107  inline const void *getPixels() const {
108  return pixels;
109  }
110 
116  inline void *getPixels() {
117  return pixels;
118  }
119 
127  void setPixels(void *newPixels) { pixels = newPixels; }
128 
137  inline const void *getBasePtr(int x, int y) const {
138  return (const byte *)(pixels) + y * pitch + x * format.bytesPerPixel;
139  }
140 
149  inline void *getBasePtr(int x, int y) {
150  return static_cast<byte *>(pixels) + y * pitch + x * format.bytesPerPixel;
151  }
152 
161  inline uint32 getPixel(int x, int y) const {
162  assert(format.bytesPerPixel > 0 && format.bytesPerPixel <= 4);
163  if (format.bytesPerPixel == 1)
164  return *((const uint8 *)getBasePtr(x, y));
165  else if (format.bytesPerPixel == 2)
166  return *((const uint16 *)getBasePtr(x, y));
167  else if (format.bytesPerPixel == 3)
168  return READ_UINT24(getBasePtr(x, y));
169  else if (format.bytesPerPixel == 4)
170  return *((const uint32 *)getBasePtr(x, y));
171  else
172  return 0;
173  }
174 
182  inline void setPixel(int x, int y, int pixel) {
183  assert(format.bytesPerPixel > 0 && format.bytesPerPixel <= 4);
184  assert(x >= 0 && x < w && y >= 0 && y < h);
185  if (format.bytesPerPixel == 1)
186  *((uint8 *)getBasePtr(x, y)) = pixel;
187  else if (format.bytesPerPixel == 2)
188  *((uint16 *)getBasePtr(x, y)) = pixel;
189  else if (format.bytesPerPixel == 3)
190  WRITE_UINT24(getBasePtr(x, y), pixel);
191  else if (format.bytesPerPixel == 4)
192  *((uint32 *)getBasePtr(x, y)) = pixel;
193  }
194 
204  void create(int16 width, int16 height, const PixelFormat &format);
205 
214  void free();
215 
228  void init(int16 width, int16 height, int16 pitch, void *pixels, const PixelFormat &format);
229 
239  void copyFrom(const Surface &surf);
240 
251  void convertFrom(const Surface &surf, const PixelFormat &format);
252 
269  Surface getSubArea(const Common::Rect &area);
270 
286  const Surface getSubArea(const Common::Rect &area) const;
287 
291  bool clip(Common::Rect &srcBounds, Common::Rect &destBounds) const;
292 
305  void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height);
316  void copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect);
317 
331  void copyRectToSurfaceWithKey(const void *buffer, int srcPitch, int destX, int destY, int width, int height, uint32 key);
332 
344  void copyRectToSurfaceWithKey(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect, uint32 key);
345 
358  inline void convertToInPlace(const PixelFormat &dstFormat) {
359  convertToInPlace(dstFormat, nullptr, 0);
360  }
361 
376  void convertToInPlace(const PixelFormat &dstFormat, const byte *palette, uint16 paletteCount);
377 
391  Graphics::Surface *convertTo(const PixelFormat &dstFormat, const byte *srcPalette = 0, int srcPaletteCount = 0, const byte *dstPalette = 0, int dstPaletteCount = 0, DitherMethod method = kDitherFloyd) const;
392 
393 protected:
394  void ditherFloyd(const byte *srcPalette, int srcPaletteCount, Surface *dstSurf, const byte *dstPalette, int dstPaletteCount, DitherMethod method, const PixelFormat &dstFormat) const;
395 
396 public:
397 
409  void drawLine(int x0, int y0, int x1, int y1, uint32 color);
410 
426  void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color);
427 
437  void hLine(int x, int y, int x2, uint32 color);
438 
448  void vLine(int x, int y, int y2, uint32 color);
449 
456  void fillRect(Common::Rect r, uint32 color);
457 
464  void frameRect(const Common::Rect &r, uint32 color);
465 
470  void move(int dx, int dy, int height);
471 
477  void flipVertical(const Common::Rect &r);
478 
484  void flipHorizontal(const Common::Rect &r);
485 
493  bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha = false);
494 
505  bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha,
506  uint8 rNew, uint8 gNew, uint8 bNew);
507 
513  bool setAlpha(uint8 alpha, bool skipTransparent = false);
514 
525  Graphics::Surface *scale(int16 newWidth, int16 newHeight, bool filtering = false) const;
526 
538  Graphics::Surface *rotoscale(const TransformStruct &transform, bool filtering = false) const;
539 
554  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;
555 };
556 
563  void operator()(Surface *ptr) {
564  if (ptr) {
565  ptr->free();
566  }
567  delete ptr;
568  }
569 };
570 
581 class FloodFill {
582 public:
590  FloodFill(Surface *surface, uint32 oldColor, uint32 fillColor, bool maskMode = false);
591  ~FloodFill();
592 
599  void addSeed(int x, int y);
600 
606  void fill();
607 
616  void fillMask();
617 
623  Surface *getMask() { return _mask; }
624 
625 private:
627  Surface *_surface;
628  Surface *_mask;
629  uint32 _oldColor, _fillColor;
630  byte *_visited;
631  int _w, _h;
632 
633  bool _maskMode;
634 };
636 } // End of namespace Graphics
637 
638 
639 #endif
void * getPixels()
Definition: surface.h:116
int32 pitch
Definition: surface.h:82
void * pixels
Definition: surface.h:88
void * getBasePtr(int x, int y)
Definition: surface.h:149
Definition: surface.h:66
Surface * getMask()
Definition: surface.h:623
int16 h
Definition: surface.h:75
Definition: pixelformat.h:138
void convertToInPlace(const PixelFormat &dstFormat)
Definition: surface.h:358
Definition: surface.h:581
void setPixels(void *newPixels)
Definition: surface.h:127
Definition: display_client.h:58
Definition: list.h:44
Definition: rect.h:144
Surface()
Definition: surface.h:99
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: surface.h:562
const void * getBasePtr(int x, int y) const
Definition: surface.h:137
constexpr remove_reference_t< T > && move(T &&t) noexcept
Definition: util.h:209
Definition: algorithm.h:29
Definition: formatinfo.h:28
void setPixel(int x, int y, int pixel)
Definition: surface.h:182
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:111
int16 w
Definition: surface.h:70
PixelFormat format
Definition: surface.h:94
Definition: transform_struct.h:75
uint32 getPixel(int x, int y) const
Definition: surface.h:161
byte bytesPerPixel
Definition: pixelformat.h:139
const void * getPixels() const
Definition: surface.h:107