-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgbrfile.cpp
More file actions
145 lines (125 loc) · 3.65 KB
/
gbrfile.cpp
File metadata and controls
145 lines (125 loc) · 3.65 KB
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
#include "gbrfile.h"
#include <QDataStream>
#include <QString>
#include <QDebug>
#include <QBuffer>
QImage readGBR(QIODevice *file)
{
if (!file->isOpen())
file->open(QIODevice::ReadOnly);
auto startPosition = file->pos();
uint32_t headerLength;
uint32_t version;
uint32_t width;
uint32_t height;
uint32_t format;
{
QDataStream stream(file);
stream >> headerLength >> version >> width >> height >> format;
if (stream.status() != QDataStream::Ok)
{
qWarning() << "Failed to read .gbr file, header too short";
return {};
}
}
if ((file->size() - (startPosition + headerLength)) < (format * width * height))
{
qWarning() << "Failed to read .gbr file, data too short";
return {};
}
file->seek(startPosition + headerLength);
QImage image;
if (format == 1)
{
image = QImage(width, height, QImage::Format_Indexed8);
image.setColorCount(0xFF);
for (int i = 0; i <= 0xFF; i++)
image.setColor(i, qRgba(i, i, i, 0xFF));
}
else if (format == 4)
{
image = QImage(width, height, QImage::Format_RGBA8888);
}
else
{
qWarning() << "Failed to read .gbr file, unknown format";
return {};
}
for (uint32_t i = 0; i < height; ++i)
file->read((char *)image.scanLine(i), format * width);
return image;
}
QList<QImage> readGIH(QIODevice *file)
{
if (!file->isOpen())
file->open(QIODevice::ReadOnly);
/* Discard the description */
file->readLine();
QByteArray layout = file->readLine();
/* If there was an error reading numImages will zero */
int numImages = QString::fromUtf8(layout).section(' ', 0, 0).toInt();
QList<QImage> result;
while (numImages--){
QImage image = readGBR(file);
if (!image.isNull())
result.append(image);
}
return result;
}
namespace {
void writeGBR_internal(QBuffer &buffer, QImage const image)
{
QDataStream stream(&buffer);
uint32_t headerLength = 28+1;
uint32_t version = 2;
uint32_t width = image.width();
uint32_t height = image.height();
uint32_t format = image.depth() <= 8 ? 1 : 4;
uint32_t magic = ('G' << 24) + ('I' << 16) + ('M' << 8) + 'P';
uint32_t spacing = 100;
uint8_t name = 0;
stream << headerLength << version << width << height << format << magic << spacing << name;
if (format == 1)
{
for (int y = 0; y < image.height(); ++y)
for (int x = 0; x < image.width(); ++x)
{
QRgb p = image.pixel(x, y);
stream << (uint8_t)qRed(p);
}
}
else
{
for (int y = 0; y < image.height(); ++y)
for (int x = 0; x < image.width(); ++x)
{
QRgb p = image.pixel(x, y);
stream << (uint8_t)qRed(p) << (uint8_t)qGreen(p) << (uint8_t)qBlue(p) << (uint8_t)qAlpha(p);
}
}
}
}
QByteArray writeGBR(QImage const image)
{
QByteArray result;
QBuffer buffer(&result);
buffer.open(QIODevice::WriteOnly);
writeGBR_internal(buffer, image);
buffer.close();
return result;
}
QByteArray writeGIH(QList<QImage> const images)
{
if (images.empty())
return {};
QByteArray result;
QBuffer buffer(&result);
buffer.open(QIODevice::WriteOnly);
buffer.write("Brush\n", 6);
// We need to include the number of images but the rest of the parasite string is optional
buffer.write(QString("%1\n").arg(images.size()).toUtf8());
for (auto const &image: images)
writeGBR_internal(buffer, image);
buffer.close();
return result;
}