-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibany2col.cpp
197 lines (172 loc) · 6.26 KB
/
libany2col.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright 2017-2022 - Geoffrey Brun <[email protected]>
*
* This file is part of any2coloring.
*
* any2coloring is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* any2coloring is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* any2coloring. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QFile>
#include <QPainter>
#include <QPdfWriter>
#include <QVector>
#include <CImg.h>
#include <gmic.h>
#include "any2col.hpp"
inline double mm_to_pt(double mm)
{
return mm / 25.4 * 72.0;
}
using namespace cimg_library;
bool read_palette(const char *filename, QVector<struct color> &palette)
{
QFile qfile(filename);
QString str_colrgb;
QString str_colname;
if (!qfile.open(QIODevice::ReadOnly)) {
qDebug() << Q_FUNC_INFO << "unable to open" << filename << qfile.errorString();
return false;
}
QTextStream textStream(&qfile);
while (!textStream.atEnd()) {
color Color;
bool ok;
uint32_t rawcolor;
QString str = textStream.readLine();
QRegExp regExp("\\s+");
QStringList strList = str.split(regExp);
if (strList.size() < 2)
continue;
rawcolor = strList.at(0).toUInt(&ok, 16);
if (!ok) {
qDebug() << Q_FUNC_INFO << "invalid RGB color:" << strList.at(0) << "skipping...";
continue;
}
Color.rgb.R = rawcolor >> 2*8;
Color.rgb.G = (uint32_t)(rawcolor >> 8) & (uint32_t)(0xFF);
Color.rgb.B = rawcolor & (uint32_t)0xFF;
Color.name = strList.at(1);
palette.push_back(Color);
}
return true;
}
void palette2CImg(QVector<struct color> const &palette, CImg<float> &cimg_palette)
{
cimg_palette.assign(palette.size(), 1, 1, 3);
for (int i = 0; i < palette.size(); i += 1) {
cimg_palette(i, 0, 0, 0) = palette[i].rgb.R;
cimg_palette(i, 0, 0, 1) = palette[i].rgb.G;
cimg_palette(i, 0, 0, 2) = palette[i].rgb.B;
}
}
void make_coloring(const char *palette_csv_file, const char *original_picture, struct col_opt const &opts, struct Coloring &coloring)
{
CImgList<float> cimgList(2);
CImgList<char> cimgNames(2);
QVector<struct color> palette;
QString gmic_cmdline;
QByteArray byteArray;
gmic gmic_obj;
bool result;
double pic_width = opts.page.width - opts.margin.right - opts.margin.left;
double pic_height = opts.page.height - opts.margin.top - opts.margin.bottom;
// Read palette from file
result = read_palette(palette_csv_file, palette);
if (!result) {
qDebug() << Q_FUNC_INFO << "Palette file read error, aborting";
exit(EXIT_FAILURE);
}
palette2CImg(palette, cimgList[1]);
// Read original picture
cimgList[0].load(original_picture);
cimgList[0].resize(-100, -100, -100, 3); // prevent abort() if picture is gray
// build cmdline
gmic_cmdline = QString::asprintf(
"-local[0] "
"-if {w>h} -rotate 90 -endif "
"-if {h/w>%g} "
"-r2dy {int(%g)} "
"-else "
"-r2dx {int(%g)} "
"-endif "
"-endlocal "
"-index.. .,1 +map[0] [1] -rm..",
(double)pic_height/(double)pic_width,
(double)pic_height/(double)opts.px_size,
(double)pic_width/(double)opts.px_size
);
// Set names (useless?)
cimgNames[1] = "palette";
cimgNames[0] = "picture";
byteArray = gmic_cmdline.toUtf8();
gmic_obj.run(byteArray.constData(), cimgList, cimgNames);
coloring.picture = cimgList[0];
coloring.palette = palette;
}
static inline double mm2pdf(int dpi, double mm)
{
return mm/25.4*(double)dpi;
}
void coloring2pdf(const char *filename, struct Coloring const &coloring, struct col_opt const &opts, bool soluce)
{
double offset_x, offset_y;
QPdfWriter pdfWriter(filename);
QPainter qPainter;
offset_x = (opts.page.width + opts.margin.left - opts.margin.right)/2.0 - coloring.picture.width()*opts.px_size / 2.0;
offset_y = (opts.page.height + opts.margin.top - opts.margin.bottom)/2.0 - coloring.picture.height()*opts.px_size / 2.0;
QPageSize pageSize(QSizeF(opts.page.width, opts.page.height), QPageSize::Millimeter);
QPageLayout pageLayout(pageSize, QPageLayout::Portrait, QMarginsF(0, 0, 0, 0), QPageLayout::Millimeter);
pdfWriter.setPageLayout(pageLayout);
pdfWriter.setTitle(QObject::tr("Coloriage !"));
pdfWriter.setCreator(QString("any2coloring"));
qPainter.begin(&pdfWriter);
if (soluce) {
for (int y = 0; y < coloring.picture.height(); y += 1) {
for (int x = 0; x < coloring.picture.width(); x += 1) {
int R, G, B;
double rectx, recty, rectw, recth;
R = coloring.palette.at(coloring.picture(x, y, 0)).rgb.R;
G = coloring.palette.at(coloring.picture(x, y, 0)).rgb.G;
B = coloring.palette.at(coloring.picture(x, y, 0)).rgb.B;
rectx = mm2pdf(pdfWriter.resolution(), offset_x + x*opts.px_size);
recty = mm2pdf(pdfWriter.resolution(), offset_y + y*opts.px_size);
rectw = recth = mm2pdf(pdfWriter.resolution(), opts.px_size);
qPainter.fillRect(QRectF(rectx, recty, rectw, recth), QColor(R, G, B));
}
}
} else {
QPen penLines;
QPen penFonts;
QFont font;
penLines.setColor(QColor(opts.lineColor, opts.lineColor, opts.lineColor));
penLines.setStyle(Qt::SolidLine);
penLines.setWidthF(mm2pdf(pdfWriter.resolution(), opts.px_size/20.0));
penFonts.setColor(QColor(opts.textColor, opts.textColor, opts.textColor));
font.setFamily("Sans");
font.setPointSizeF(mm2pdf(pdfWriter.resolution(), opts.px_size)*0.035);
qPainter.setFont(font);
for (int y = 0; y < coloring.picture.height(); y += 1) {
for (int x = 0; x < coloring.picture.width(); x += 1) {
double rectx, recty, rectw, recth;
rectx = mm2pdf(pdfWriter.resolution(), offset_x + x*opts.px_size);
recty = mm2pdf(pdfWriter.resolution(), offset_y + y*opts.px_size);
rectw = recth = mm2pdf(pdfWriter.resolution(), opts.px_size);
qPainter.setPen(penLines);
qPainter.drawRect(QRectF(rectx, recty, rectw, recth));
qPainter.setPen(penFonts);
qPainter.drawText(QRectF(rectx, recty, rectw, recth), Qt::AlignCenter | Qt::AlignHCenter, coloring.palette.at(coloring.picture(x, y, 0)).name);
}
}
}
}