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 #include "graphics/transform_struct.h"
36 
37 namespace Graphics {
38 
48 struct TransformStruct;
49 
50 enum DitherMethod {
51  kDitherNaive,
52  kDitherFloyd,
53  kDitherAtkinson,
54  kDitherBurkes,
55  kDitherFalseFloyd,
56  kDitherSierra,
57  kDitherSierraTwoRow,
58  kDitherSierraLite,
59  kDitherStucki,
60  kDitherJarvis,
61 };
62 
67 struct Surface {
71  int16 w;
72 
76  int16 h;
77 
83  int32 pitch;
84 
85 protected:
89  void *pixels;
90 
91 public:
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 
272  Surface getSubArea(const Common::Rect &area);
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 
396 protected:
397  void ditherFloyd(const byte *srcPalette, int srcPaletteCount, Surface *dstSurf, const byte *dstPalette, int dstPaletteCount, DitherMethod method, const PixelFormat &dstFormat) const;
398 
399 public:
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 
519  void flipVertical(const Common::Rect &r);
520 
526  void flipHorizontal(const Common::Rect &r);
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 
631 class FloodFill {
632 public:
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 
675 private:
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
void * getPixels()
Definition: surface.h:119
int32 pitch
Definition: surface.h:83
void * pixels
Definition: surface.h:89
void * getBasePtr(int x, int y)
Definition: surface.h:152
Definition: surface.h:67
Surface * getMask()
Definition: surface.h:673
int16 h
Definition: surface.h:76
Definition: pixelformat.h:138
void convertToInPlace(const PixelFormat &dstFormat)
Definition: surface.h:361
Definition: surface.h:631
void setPixels(void *newPixels)
Definition: surface.h:130
Definition: display_client.h:58
Definition: list.h:44
Definition: rect.h:524
Surface()
Definition: surface.h:100
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: surface.h:612
const void * getBasePtr(int x, int y) const
Definition: surface.h:140
Definition: algorithm.h:29
Definition: formatinfo.h:28
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
void setPixel(int x, int y, int pixel)
Definition: surface.h:185
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
int16 w
Definition: surface.h:71
PixelFormat format
Definition: surface.h:95
Definition: transform_struct.h:75
uint32 getPixel(int x, int y) const
Definition: surface.h:164
byte bytesPerPixel
Definition: pixelformat.h:139
const void * getPixels() const
Definition: surface.h:110