Otclient  14/8/2020
image.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef IMAGE_H
24 #define IMAGE_H
25 
26 #include "declarations.h"
28 
30 {
31 public:
32  Image(const Size& size, int bpp = 4, uint8 *pixels = nullptr);
33 
34  static ImagePtr load(std::string file);
35  static ImagePtr loadPNG(const std::string& file);
36 
37  void savePNG(const std::string& fileName);
38 
39  void overwriteMask(const Color& maskedColor, const Color& insideColor = Color::white, const Color& outsideColor = Color::alpha);
40  void blit(const Point& dest, const ImagePtr& other);
41  void paste(const ImagePtr& other);
42  void resize(const Size& size) { m_size = size; m_pixels.resize(size.area() * m_bpp, 0); }
43  bool nextMipmap();
44 
45  void setPixel(int x, int y, uint8 *pixel) { memcpy(&m_pixels[(y * m_size.width() + x) * m_bpp], pixel, m_bpp);}
46  void setPixel(int x, int y, const Color& color) { uint32 tmp = color.rgba(); setPixel(x,y,(uint8*)&tmp); }
47 
48  std::vector<uint8>& getPixels() { return m_pixels; }
49  uint8* getPixelData() { return &m_pixels[0]; }
50  int getPixelCount() { return m_size.area(); }
51  const Size& getSize() { return m_size; }
52  int getWidth() { return m_size.width(); }
53  int getHeight() { return m_size.height(); }
54  int getBpp() { return m_bpp; }
55  uint8* getPixel(int x, int y) { return &m_pixels[(y * m_size.width() + x) * m_bpp]; }
56 
57 private:
58  std::vector<uint8> m_pixels;
59  Size m_size;
60  int m_bpp;
61 };
62 
63 #endif
databuffer.h
Color
Definition: color.h:32
Image::load
static ImagePtr load(std::string file)
Definition: image.cpp:39
uint32
uint32_t uint32
Definition: types.h:35
TSize::area
T area() const
Definition: size.h:101
Image::getPixelData
uint8 * getPixelData()
Definition: image.h:49
TSize::width
int width() const
Definition: size.h:43
Image::nextMipmap
bool nextMipmap()
Definition: image.cpp:142
Image::blit
void blit(const Point &dest, const ImagePtr &other)
Definition: image.cpp:100
Image::getSize
const Size & getSize()
Definition: image.h:51
Image::getPixels
std::vector< uint8 > & getPixels()
Definition: image.h:48
Image
Definition: image.h:29
Image::paste
void paste(const ImagePtr &other)
Definition: image.cpp:122
Image::setPixel
void setPixel(int x, int y, uint8 *pixel)
Definition: image.h:45
Image::resize
void resize(const Size &size)
Definition: image.h:42
Image::getWidth
int getWidth()
Definition: image.h:52
Image::getBpp
int getBpp()
Definition: image.h:54
Color::white
static const Color white
Definition: color.h:101
Image::getPixel
uint8 * getPixel(int x, int y)
Definition: image.h:55
Image::overwriteMask
void overwriteMask(const Color &maskedColor, const Color &insideColor=Color::white, const Color &outsideColor=Color::alpha)
Definition: image.cpp:80
Image::loadPNG
static ImagePtr loadPNG(const std::string &file)
Definition: image.cpp:53
Image::getHeight
int getHeight()
Definition: image.h:53
Color::alpha
static const Color alpha
Definition: color.h:100
TSize::height
int height() const
Definition: size.h:44
Image::setPixel
void setPixel(int x, int y, const Color &color)
Definition: image.h:46
declarations.h
stdext::shared_object_ptr
Definition: shared_object.h:39
Image::Image
Image(const Size &size, int bpp=4, uint8 *pixels=nullptr)
Definition: image.cpp:30
Image::getPixelCount
int getPixelCount()
Definition: image.h:50
TPoint< int >
Color::rgba
uint32 rgba() const
Definition: color.h:54
TSize< int >
Image::savePNG
void savePNG(const std::string &fileName)
Definition: image.cpp:66
stdext::shared_object
Definition: shared_object.h:41
uint8
uint8_t uint8
Definition: types.h:37