1/*
2 * Copyright (c) 2010-2017 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"
27#include <framework/util/databuffer.h>
28
29class Image : public stdext::shared_object
30{
31public:
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 static ImagePtr loadPNG(const void* data, uint32_t size);
37
38 void savePNG(const std::string& fileName);
39
40 void blit(const Point& dest, const ImagePtr& other);
41 void paste(const ImagePtr& other);
42 ImagePtr upscale();
43 void resize(const Size& size) { m_size = size; m_pixels.resize(size.area() * m_bpp, 0); }
44 bool nextMipmap();
45
46 void setPixel(int x, int y, uint8 *pixel) { memcpy(&m_pixels[(y * m_size.width() + x) * m_bpp], pixel, m_bpp);}
47 void setPixel(int x, int y, uint32_t argb) { setPixel(x, y, (uint8*)&argb); }
48 void setPixel(int x, int y, const Color& color) {
49 m_pixels[(y * m_size.width() + x) * m_bpp] = color.r();
50 m_pixels[(y * m_size.width() + x) * m_bpp + 1] = color.g();
51 m_pixels[(y * m_size.width() + x) * m_bpp + 2] = color.b();
52 m_pixels[(y * m_size.width() + x) * m_bpp + 3] = color.a();
53 }
54
55 std::vector<uint8>& getPixels() { return m_pixels; }
56 uint8* getPixelData() { return &m_pixels[0]; }
57 int getPixelCount() { return m_size.area(); }
58 const Size& getSize() { return m_size; }
59 int getWidth() { return m_size.width(); }
60 int getHeight() { return m_size.height(); }
61 int getBpp() { return m_bpp; }
62 uint8* getPixel(int x, int y) { return &m_pixels[(y * m_size.width() + x) * m_bpp]; }
63
64 static ImagePtr fromQRCode(const std::string& code, int border);
65
66private:
67 std::vector<uint8> m_pixels;
68 Size m_size;
69 int m_bpp;
70};
71
72#endif
73