-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpngexportimport.cpp
91 lines (82 loc) · 2.56 KB
/
pngexportimport.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
#include "pngexportimport.h"
#include <QDebug>
QImage mergeTilesToImage(const QList<tile8> tiles, const SNESPalette& palette, const TilesPattern& tilesPattern)
{
QVector<QVector<tile8> > arrangedTiles = TilesPattern::transform(tilesPattern, tiles);
unsigned tHeight = arrangedTiles.size();
unsigned tWidth = arrangedTiles[0].size();
//qDebug() << height;
QImage newImage(tWidth * 8, tHeight * 8, QImage::Format_Indexed8);
newImage.setColorCount(palette.size);
for (unsigned int i = 0; i < palette.size; i++)
{
newImage.setColor(i, palette.colors.at(i).rgb);
}
QString plop = "";
for (unsigned int j = 0; j < tHeight; j++)
{
for (unsigned int i = 0; i < tWidth; i++)
{
tile8& tile = arrangedTiles[j][i];
for (unsigned int tPos = 0; tPos < 64; tPos++)
{
QPoint p(i * 8 + (tPos % 8), j * 8 + tPos / 8);
newImage.setPixel(p, tile.data[tPos]);
}
}
}
return newImage;
}
bool saveToPNG(const QImage &image, QString path)
{
return image.save(path, "PNG");
}
QList<tile8> tilesFromPNG(const QString file)
{
QList<tile8> toret;
QImage image(file);
if (image.format() != QImage::Format_Indexed8)
{
qCritical() << file << "is not an indexed PNG file";
return toret;
}
if (image.colorCount() > 16)
{
qCritical() << "Color count in" << file << "is superior to 16";
return toret;
}
unsigned int width = image.width();
unsigned int height = image.height();
qDebug() << "Image is " << width << "x" << height << "Should contain " << (width / 8) * (height / 8);
for (unsigned int posImageH = 0; posImageH < height / 8; posImageH++)
{
for (unsigned int posImageW = 0; posImageW < width / 8; posImageW++)
{
tile8 newTile;
for (unsigned int i = 0 ; i < 64; i++)
{
QPoint p(posImageW * 8 + i % 8, posImageH * 8 + i / 8);
newTile.data[i] = image.pixelIndex(p);
}
toret.append(newTile);
}
}
return toret;
}
SNESPalette paletteFromPNG(const QString file)
{
SNESPalette toret(0);
QImage image(file);
if (image.format() != QImage::Format_Indexed8)
{
qCritical() << file << "is not an indexed PNG file";
return toret;
}
if (image.colorCount() > 16)
{
qCritical() << "Color count in" << file << "is superior to 16";
return toret;
}
toret = SNESPalette(image.colorTable());
return toret;
}