-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrespicture.cpp
189 lines (149 loc) · 5.75 KB
/
respicture.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
#include "respicture.h"
ResPicture::ResPicture()
{
}
ResPicture::ResPicture(FileHeader header, const uint8_t* rawdata) :
m_header(header), m_uncompressed(), m_compressed()
{
/* Read image data from raw bytes */
const uint32_t addr = header.address;
const int width = rawdata[addr + 1] << 8 | rawdata[addr];
const int height = rawdata[addr + 3] << 8 | rawdata[addr + 2];
const uint size = static_cast<uint>((rawdata[addr + 7] << 24) |
(rawdata[addr + 6] << 16) |
(rawdata[addr + 5] << 8) |
(rawdata[addr + 4]));
m_dimension = QSize(width, height);
/* Store compressed image data */
const uint8_t *img = rawdata + addr + PIC_HEADER_SIZE;
m_compressed.resize(size);
memcpy(&m_compressed[0], img, size);
/* Decompress data and store the result */
uint size_out = static_cast<uint>(width * height);
if (IMG_HAS_ALPHA(header.type))
size_out *= 3;
else
size_out *= 2;
m_uncompressed.resize(size_out);
Zlib::inf(m_compressed, m_uncompressed);
}
bool ResPicture::isEmpty(){
return !m_parsed;
}
const QImage& ResPicture::getImage() {
if (!m_parsed) {
if (m_compressed.size() != (m_header.size - PIC_HEADER_SIZE)) {
QMessageBox::warning(nullptr, QObject::tr("Wrong image format"),
QObject::tr("The image size in the header and metadata header don't match.\n"
"The program will continue taking in account the metadata size"));
}
if (IMG_HAS_ALPHA(m_header.type))
parseAlphaImage();
else
parseOpaqueImage();
m_parsed = true;
}
return m_image;
}
const QString ResPicture::getName() {
return QString(m_header.name);
}
const QString ResPicture::getInfo() {
return QString(m_header.name) + "\tSize: " +
QString::number(m_dimension.width()) +
"x" +
QString::number(m_dimension.height());
}
void ResPicture::parseAlphaImage() {
m_image = QImage(m_dimension, QImage::Format::Format_RGBA8888);
int x = 0;
int y = 0;
for (uint32_t i = 0; i < m_uncompressed.size(); i += 3) {
uint8_t alpha = m_uncompressed[i + 2];
uint16_t bytes = static_cast<uint16_t>(m_uncompressed[i + 1] << 8 |
m_uncompressed[i]);
uint8_t blue = static_cast<uint8_t>(bytes & 0x1f);
/* We have to dump the fifth bit, so let's shift the byte by
* 6 instead of 5. */
uint8_t green = static_cast<uint8_t>((bytes >> 6) & 0x1f);
uint8_t red = static_cast<uint8_t>((bytes >> 11) & 0x1f);
/* Convert R, G and B to 8-bit values */
const float ratio = 8.2258f;
blue = static_cast<uint8_t>(blue * ratio);
green = static_cast<uint8_t>(green * ratio);
red = static_cast<uint8_t>(red * ratio);
m_image.setPixelColor(x, y,
QColor(red, green, blue, alpha));
x++;
if (x == m_dimension.width()) {
x = 0;
y++;
}
}
}
void ResPicture::parseOpaqueImage() {
m_image = QImage(m_dimension, QImage::Format::Format_RGB32);
int x = 0;
int y = 0;
for (uint32_t i = 0; i < m_uncompressed.size(); i += 2) {
uint16_t bytes = static_cast<uint16_t>(m_uncompressed[i + 1] << 8 |
m_uncompressed[i]);
uint8_t blue = static_cast<uint8_t>(bytes & 0x1f);
/* We have to dump the fifth bit, so let's shift the byte by
* 6 instead of 5. */
uint8_t green = static_cast<uint8_t>((bytes >> 6) & 0x1f);
uint8_t red = static_cast<uint8_t>((bytes >> 11) & 0x1f);
/* Convert R, G and B to 8-bit values */
const float ratio = 8.2258f;
blue = static_cast<uint8_t>(blue * ratio);
green = static_cast<uint8_t>(green * ratio);
red = static_cast<uint8_t>(red * ratio);
m_image.setPixelColor(x, y, QColor(red, green, blue));
x++;
if (x == m_dimension.width()) {
x = 0;
y++;
}
}
}
FileHeader ResPicture::getHeader() {
return m_header;
}
vector<uint8_t> ResPicture::getCompressedData() {
vector<uint8_t> finaldata;
const uint16_t width = static_cast<uint16_t>(m_dimension.width());
const uint16_t height = static_cast<uint16_t>(m_dimension.height());
const uint32_t size = static_cast<uint32_t>(m_compressed.size());
IOUtils::pushLittleEndianValue(finaldata, width);
IOUtils::pushLittleEndianValue(finaldata, height);
IOUtils::pushLittleEndianValue(finaldata, size);
IOUtils::concatVector(finaldata, m_compressed);
return finaldata;
}
const QSize& ResPicture::getSize() {
return m_dimension;
}
void ResPicture::replaceImage(const QImage& img) {
int r, g, b, a;
const float ratio = 8.2258f;
m_uncompressed.clear();
for (int i = 0; i < img.height(); i++) {
for (int j = 0; j < img.width(); j++) {
const QColor color = img.pixelColor(j, i);
color.getRgb(&r, &g, &b, &a);
uint8_t red = static_cast<uint8_t>(r/ratio);
uint8_t green = static_cast<uint8_t>(g/ratio);
uint8_t blue = static_cast<uint8_t>(b/ratio);
uint16_t bytes = ((red & 0x1f) << 11 |
(green & 0x1f) << 6 |
(blue & 0x1f) << 0);
IOUtils::pushLittleEndianValue(m_uncompressed, bytes);
if (IMG_HAS_ALPHA(m_header.type))
m_uncompressed.push_back(static_cast<uint8_t>(a));
}
}
m_compressed.clear();
m_compressed.resize(m_uncompressed.size());
Zlib::def(m_uncompressed, m_compressed, Z_DEFAULT_COMPRESSION);
m_image = img;
}