1 /*
2  * scriptimage.h
3  * Copyright 2020, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include <QImage>
24 #include <QJSValue>
25 #include <QObject>
26 
27 namespace Tiled {
28 
29 class ScriptImage : public QObject
30 {
31     Q_OBJECT
32 
33     Q_PROPERTY(int width READ width)
34     Q_PROPERTY(int height READ height)
35     Q_PROPERTY(int depth READ depth)
36     Q_PROPERTY(QSize size READ size)
37     Q_PROPERTY(Format format READ format)
38 
39 public:
40     // Copied from QImage
41     enum Format {
42         Format_Invalid,
43         Format_Mono,
44         Format_MonoLSB,
45         Format_Indexed8,
46         Format_RGB32,
47         Format_ARGB32,
48         Format_ARGB32_Premultiplied,
49         Format_RGB16,
50         Format_ARGB8565_Premultiplied,
51         Format_RGB666,
52         Format_ARGB6666_Premultiplied,
53         Format_RGB555,
54         Format_ARGB8555_Premultiplied,
55         Format_RGB888,
56         Format_RGB444,
57         Format_ARGB4444_Premultiplied,
58         Format_RGBX8888,
59         Format_RGBA8888,
60         Format_RGBA8888_Premultiplied,
61         Format_BGR30,
62         Format_A2BGR30_Premultiplied,
63         Format_RGB30,
64         Format_A2RGB30_Premultiplied,
65         Format_Alpha8,
66         Format_Grayscale8,
67         Format_RGBX64,
68         Format_RGBA64,
69         Format_RGBA64_Premultiplied,
70         Format_Grayscale16,
71         Format_BGR888,
72 #ifndef Q_QDOC
73         NImageFormats
74 #endif
75     };
76     Q_ENUM(Format)
77 
78     enum AspectRatioMode {
79         IgnoreAspectRatio           = Qt::IgnoreAspectRatio,
80         KeepAspectRatio             = Qt::KeepAspectRatio,
81         KeepAspectRatioByExpanding  = Qt::KeepAspectRatioByExpanding
82     };
83     Q_ENUM(AspectRatioMode)
84 
85     enum TransformationMode {
86         FastTransformation          = Qt::FastTransformation,
87         SmoothTransformation        = Qt::SmoothTransformation
88     };
89     Q_ENUM(TransformationMode)
90 
91     Q_INVOKABLE explicit ScriptImage(QObject *parent = nullptr);
92     Q_INVOKABLE ScriptImage(int width, int height, Format format = Format_ARGB32_Premultiplied, QObject *parent = nullptr);
93     Q_INVOKABLE ScriptImage(const QByteArray &data, int width, int height, Format format, QObject *parent = nullptr);
94     Q_INVOKABLE ScriptImage(const QByteArray &data, int width, int height, int bytesPerLine, Format format, QObject *parent = nullptr);
95     Q_INVOKABLE ScriptImage(const QString &fileName, const QByteArray &format = QByteArray(), QObject *parent = nullptr);
96 
ScriptImage(QImage image)97     ScriptImage(QImage image)
98         : mImage(std::move(image))
99     {}
100 
format()101     Format format() const { return static_cast<Format>(mImage.format()); }
width()102     int width() const { return mImage.width(); }
height()103     int height() const { return mImage.height(); }
depth()104     int depth() const { return mImage.depth(); }
size()105     QSize size() const { return mImage.size(); }
106 
pixel(int x,int y)107     Q_INVOKABLE uint pixel(int x, int y) const
108     { return mImage.pixel(x, y); }
109 
pixelColor(int x,int y)110     Q_INVOKABLE QColor pixelColor(int x, int y) const
111     { return mImage.pixelColor(x, y); }
112 
setPixel(int x,int y,uint index_or_rgb)113     Q_INVOKABLE void setPixel(int x, int y, uint index_or_rgb)
114     { mImage.setPixel(x, y, index_or_rgb); }
115 
setPixelColor(int x,int y,const QColor & color)116     Q_INVOKABLE void setPixelColor(int x, int y, const QColor &color)
117     { mImage.setPixelColor(x, y, color); }
118 
fill(uint index_or_rgb)119     Q_INVOKABLE void fill(uint index_or_rgb)
120     { mImage.fill(index_or_rgb); }
121 
fill(const QColor & color)122     Q_INVOKABLE void fill(const QColor &color)
123     { mImage.fill(color); }
124 
125     Q_INVOKABLE bool load(const QString &fileName, const QByteArray &format = QByteArray())
126     { return mImage.load(fileName, format); }
127 
128     Q_INVOKABLE bool loadFromData(const QByteArray &data, const QByteArray &format = QByteArray())
129     { return mImage.loadFromData(data, format); }
130 
131     Q_INVOKABLE bool save(const QString &fileName, const QByteArray &format = QByteArray(), int quality = -1)
132     { return mImage.save(fileName, format, quality); }
133 
134     Q_INVOKABLE QByteArray saveToData(const QByteArray &format = "PNG", int quality = -1);
135 
color(int i)136     Q_INVOKABLE uint color(int i) const
137     { return mImage.color(i); }
138 
139     Q_INVOKABLE QJSValue colorTable() const;
140 
setColor(int i,uint rgb)141     Q_INVOKABLE void setColor(int i, uint rgb)
142     { mImage.setColor(i, rgb); }
143 
setColor(int i,const QColor & color)144     Q_INVOKABLE void setColor(int i, const QColor &color)
145     { mImage.setColor(i, color.rgba()); }
146 
147     Q_INVOKABLE void setColorTable(QJSValue colors);
148 
149     Q_INVOKABLE Tiled::ScriptImage *copy(int x, int y, int w, int h) const;
150     Q_INVOKABLE Tiled::ScriptImage *scaled(int w, int h,
151                                            AspectRatioMode aspectMode = IgnoreAspectRatio,
152                                            TransformationMode mode = FastTransformation) const;
153     Q_INVOKABLE Tiled::ScriptImage *mirrored(bool horiz, bool vert) const;
154 
image()155     const QImage &image() const { return mImage; }
156 
157 private:
158     QByteArray mImageData;
159     QImage mImage;
160 };
161 
162 } // namespace Tiled
163 
164 Q_DECLARE_METATYPE(Tiled::ScriptImage*)
165