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 
108  inline const void *getPixels() const {
109  return pixels;
110  }
111 
117  inline void *getPixels() {
118  return pixels;
119  }
120 
128  void setPixels(void *newPixels) { pixels = newPixels; }
129 
138  inline const void *getBasePtr(int x, int y) const {
139  return (const byte *)(pixels) + y * pitch + x * format.bytesPerPixel;
140  }
141 
150  inline void *getBasePtr(int x, int y) {
151  return static_cast<byte *>(pixels) + y * pitch + x * format.bytesPerPixel;
152  }
153 
162  inline uint32 getPixel(int x, int y) const {
163  assert(format.bytesPerPixel > 0 && format.bytesPerPixel <= 4);
164  if (format.bytesPerPixel == 1)
165  return *((const uint8 *)getBasePtr(x, y));
166  else if (format.bytesPerPixel == 2)
167  return *((const uint16 *)getBasePtr(x, y));
168  else if (format.bytesPerPixel == 3)
169  return READ_UINT24(getBasePtr(x, y));
170  else if (format.bytesPerPixel == 4)
171  return *((const uint32 *)getBasePtr(x, y));
172  else
173  return 0;
174  }
175 
183  inline void setPixel(int x, int y, int pixel) {
184  assert(format.bytesPerPixel > 0 && format.bytesPerPixel <= 4);
185  assert(x >= 0 && x < w && y >= 0 && y < h);
186  if (format.bytesPerPixel == 1)
187  *((uint8 *)getBasePtr(x, y)) = pixel;
188  else if (format.bytesPerPixel == 2)
189  *((uint16 *)getBasePtr(x, y)) = pixel;
190  else if (format.bytesPerPixel == 3)
191  WRITE_UINT24(getBasePtr(x, y), pixel);
192  else if (format.bytesPerPixel == 4)
193  *((uint32 *)getBasePtr(x, y)) = pixel;
194  }
195 
205  void create(int16 width, int16 height, const PixelFormat &format);
206 
215  void free();
216 
229  void init(int16 width, int16 height, int16 pitch, void *pixels, const PixelFormat &format);
230 
240  void copyFrom(const Surface &surf);
241 
252  void convertFrom(const Surface &surf, const PixelFormat &format);
253 
270  Surface getSubArea(const Common::Rect &area);
271 
287  const Surface getSubArea(const Common::Rect &area) const;
288 
292  bool clip(Common::Rect &srcBounds, Common::Rect &destBounds) const;
293 
306  void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height);
317  void copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect);
318 
332  void copyRectToSurfaceWithKey(const void *buffer, int srcPitch, int destX, int destY, int width, int height, uint32 key);
333 
345  void copyRectToSurfaceWithKey(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect, uint32 key);
346 
359  inline void convertToInPlace(const PixelFormat &dstFormat) {
360  convertToInPlace(dstFormat, nullptr, 0);
361  }
362 
377  void convertToInPlace(const PixelFormat &dstFormat, const byte *palette, uint16 paletteCount);
378 
392  Graphics::Surface *convertTo(const PixelFormat &dstFormat, const byte *srcPalette = 0, int srcPaletteCount = 256, const byte *dstPalette = 0, int dstPaletteCount = 0, DitherMethod method = kDitherFloyd) const;
393 
394 protected:
395  void ditherFloyd(const byte *srcPalette, int srcPaletteCount, Surface *dstSurf, const byte *dstPalette, int dstPaletteCount, DitherMethod method, const PixelFormat &dstFormat) const;
396 
397 public:
398 
410  void drawLine(int x0, int y0, int x1, int y1, uint32 color);
411 
427  void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color);
428 
438  void hLine(int x, int y, int x2, uint32 color);
439 
449  void vLine(int x, int y, int y2, uint32 color);
450 
457  void fillRect(Common::Rect r, uint32 color);
458 
465  void frameRect(const Common::Rect &r, uint32 color);
466 
471  void move(int dx, int dy, int height);
472 
478  void flipVertical(const Common::Rect &r);
479 
485  void flipHorizontal(const Common::Rect &r);
486 
494  bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha = false);
495 
506  bool applyColorKey(uint8 rKey, uint8 gKey, uint8 bKey, bool overwriteAlpha,
507  uint8 rNew, uint8 gNew, uint8 bNew);
508 
514  bool setAlpha(uint8 alpha, bool skipTransparent = false);
515 
519  AlphaType detectAlpha() const;
520 
531  Graphics::Surface *scale(int16 newWidth, int16 newHeight, bool filtering = false) const;
532 
544  Graphics::Surface *rotoscale(const TransformStruct &transform, bool filtering = false) const;
545 
560  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;
561 };
562 
569  void operator()(Surface *ptr) {
570  if (ptr) {
571  ptr->free();
572  }
573  delete ptr;
574  }
575 };
576 
587 class FloodFill {
588 public:
596  FloodFill(Surface *surface, uint32 oldColor, uint32 fillColor, bool maskMode = false);
597  ~FloodFill();
598 
605  void addSeed(int x, int y);
606 
612  void fill();
613 
622  void fillMask();
623 
629  Surface *getMask() { return _mask; }
630 
631 private:
633  Surface *_surface;
634  Surface *_mask;
635  uint32 _oldColor, _fillColor;
636  byte *_visited;
637  int _w, _h;
638 
639  bool _maskMode;
640 };
642 } // End of namespace Graphics
643 
644 
645 #endif
void * getPixels()
Definition: surface.h:117
int32 pitch
Definition: surface.h:83
void * pixels
Definition: surface.h:89
void * getBasePtr(int x, int y)
Definition: surface.h:150
Definition: surface.h:67
Surface * getMask()
Definition: surface.h:629
int16 h
Definition: surface.h:76
Definition: pixelformat.h:138
void convertToInPlace(const PixelFormat &dstFormat)
Definition: surface.h:359
Definition: surface.h:587
void setPixels(void *newPixels)
Definition: surface.h:128
Definition: display_client.h:58
Definition: list.h:44
Definition: rect.h:144
Surface()
Definition: surface.h:100
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: surface.h:568
const void * getBasePtr(int x, int y) const
Definition: surface.h:138
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:183
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:162
byte bytesPerPixel
Definition: pixelformat.h:139
const void * getPixels() const
Definition: surface.h:108