forked from czajowaty/ad_resources_dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualPsxVRam.cpp
353 lines (318 loc) · 10.8 KB
/
VirtualPsxVRam.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "VirtualPsxVRam.hpp"
#include <cstring>
constexpr VirtualPsxVRam::ColorMapping VirtualPsxVRam::COLOR_MAPPING;
VirtualPsxVRam::VirtualPsxVRam()
{ clear(); }
void VirtualPsxVRam::clear()
{
vram_.fill(0);
initializedRects_.clear();
}
QVector<QRect> const& VirtualPsxVRam::initializedRects() const
{ return initializedRects_; }
QRect const& VirtualPsxVRam::initializedBoundingRect() const
{ return initializedBoundingRect_; }
QImage VirtualPsxVRam::asImage() const
{
QImage image(
PsxVRamConst::PIXELS_PER_LINE,
PsxVRamConst::HEIGHT,
QImage::Format_ARGB32);
int x = 0;
int y = 0;
QRgb* imagePixelAddress = reinterpret_cast<QRgb*>(image.scanLine(y));
for (
auto it = vram_.cbegin();
it != vram_.cend();
it += PsxVRamConst::PIXEL_SIZE)
{
*imagePixelAddress = read16BppPixel(it).toRgb();
++x;
if (x < image.width())
{ ++imagePixelAddress; }
else
{
x = 0;
++y;
imagePixelAddress = reinterpret_cast<QRgb*>(image.scanLine(y));
}
}
return image;
}
QImage VirtualPsxVRam::read16BppTexture(Graphic const& graphic) const
{
return readTexture(
graphic,
TexpageBpp::BPP_15,
[this](Graphic const& graphic) {
return read16BppTexture(calculateVRamRect(graphic));
});
}
QImage VirtualPsxVRam::read16BppTexture(QRect const& rect) const
{
if (!isRectInitialized(rect))
{ throwUninitializedRectError(rect, "16 Bpp texture"); }
QImage image(rect.width(), rect.height(), QImage::Format_ARGB32);
for (int y = 0; y < image.height(); ++y)
{
QRgb* imagePixelAddress = reinterpret_cast<QRgb*>(image.scanLine(y));
uint8_t const* vramPixelAddress = pixelAddress(rect.x(), rect.y() + y);
for (int x = 0; x < image.width(); ++x)
{
*imagePixelAddress = read16BppPixel(vramPixelAddress).toRgba();
++imagePixelAddress;
vramPixelAddress += PsxVRamConst::PIXEL_SIZE;
}
}
return image;
}
QImage VirtualPsxVRam::read8BppTexture(
Graphic const& graphic,
Palette8Bpp const* palette) const
{
return readTexture(
graphic,
TexpageBpp::BPP_8,
[this, palette](Graphic const& graphic) {
return read8BppTexture(calculateVRamRect(graphic), palette);
});
}
QImage VirtualPsxVRam::read8BppTexture(
QRect const& rect,
Palette8Bpp const* palette) const
{
if (!isRectInitialized(rect))
{ throwUninitializedRectError(rect, "8 Bpp texture"); }
QImage image(rect.width() << 1, rect.height(), QImage::Format_Indexed8);
image.setColorTable(prepareColorTable(palette));
for (int y = 0; y < image.height(); ++y)
{
std::memcpy(
image.scanLine(y),
pixelAddress(rect.x(), rect.y() + y),
image.width());
}
return image;
}
QImage VirtualPsxVRam::read4BppTexture(
Graphic const& graphic,
Palette4Bpp const* palette) const
{
return readTexture(
graphic,
TexpageBpp::BPP_4,
[this, palette](Graphic const& graphic) {
return read4BppTexture(calculateVRamRect(graphic), palette);
});
}
QImage VirtualPsxVRam::read4BppTexture(
QRect const& rect,
Palette4Bpp const* palette) const
{
if (!isRectInitialized(rect))
{ throwUninitializedRectError(rect, "4 Bpp texture"); }
QImage image(rect.width() << 2, rect.height(), QImage::Format_Indexed8);
image.setColorTable(prepareColorTable(palette));
for (int y = 0; y < rect.height(); ++y)
{
uint8_t* imagePixelAddress = image.scanLine(y);
uint8_t const* vramPixelAddress = pixelAddress(rect.x(), rect.y() + y);
for (int x = 0; x < rect.width(); ++x)
{
uint8_t vramPixel1 = *vramPixelAddress;
uint8_t vramPixel2 = *(vramPixelAddress + 1);
*imagePixelAddress = vramPixel1 & 0xf;
*(imagePixelAddress + 1) = (vramPixel1 & 0xf0) >> 4;
*(imagePixelAddress + 2) = vramPixel2 & 0xf;
*(imagePixelAddress + 3) = (vramPixel2 & 0xf0) >> 4;
imagePixelAddress += 4;
vramPixelAddress += PsxVRamConst::PIXEL_SIZE;
}
}
return image;
}
QImage VirtualPsxVRam::readTexture(
Graphic const& graphic,
TexpageBpp expectedBpp,
std::function<QImage(Graphic const&)> textureReader) const
{
assertTextureDepth(graphic.texpage, expectedBpp);
auto texture = textureReader(graphic);
QSize expectedTextureSize(graphic.width, graphic.height);
return (texture.size() == expectedTextureSize) ?
texture :
texture.copy(QRect(QPoint(0, 0), expectedTextureSize));
}
void VirtualPsxVRam::assertTextureDepth(
Texpage const& texpage,
TexpageBpp expected) const
{
if (texpage.texpageBpp() != expected)
{
throw QString("Expected Bpp %1 != bpp %2.")
.arg(static_cast<uint16_t>(expected))
.arg(static_cast<uint16_t>(texpage.texpageBpp()));
}
}
void VirtualPsxVRam::read4BppPalette(
Clut const& clut,
Palette4Bpp& palette) const
{ return read4BppPalette(clutToVRamPoint(clut), palette); }
void VirtualPsxVRam::read4BppPalette(
QPoint const& point,
Palette4Bpp& palette) const
{ return readPalette(point, palette); }
void VirtualPsxVRam::read8BppPalette(
Clut const& clut,
Palette8Bpp& palette) const
{ return read8BppPalette(clutToVRamPoint(clut), palette); }
void VirtualPsxVRam::read8BppPalette(
QPoint const& point,
Palette8Bpp& palette) const
{ return readPalette(point, palette); }
QPoint VirtualPsxVRam::clutToVRamPoint(Clut const& clut) const
{ return QPoint(clut.x << PsxVRamConst::CLUT_X_SHIFT, clut.y); }
uint8_t const* VirtualPsxVRam::pixelAddress(int x, int y) const
{ return scanLine(y) + (x << PsxVRamConst::PIXEL_DEPTH); }
uint8_t const* VirtualPsxVRam::scanLine(int y) const
{ return vram_.cbegin() + (y << PsxVRamConst::WIDTH_DEPTH); }
uint8_t* VirtualPsxVRam::pixelAddress(int x, int y)
{ return scanLine(y) + (x << PsxVRamConst::PIXEL_DEPTH); }
uint8_t* VirtualPsxVRam::scanLine(int y)
{ return vram_.begin() + (y << PsxVRamConst::WIDTH_DEPTH); }
VirtualPsxVRam::Pixel16Bpp VirtualPsxVRam::read16BppPixel(
uint8_t const* pixelAddress) const
{
Pixel16Bpp pixel;
uint8_t byte1 = *pixelAddress;
uint8_t byte2 = *(pixelAddress + 1);
pixel.r = COLOR_MAPPING[(byte1 & 0x1f)];
pixel.g = COLOR_MAPPING[(byte1 >> 5) | ((byte2 & 0x03) << 3)];
pixel.b = COLOR_MAPPING[(byte2 >> 2) & 0x1f];
pixel.semiTransparent = (byte2 & 0x80) == 0x80;
return pixel;
}
void VirtualPsxVRam::load(QByteArray const& data, QRect const& rect)
{ load(reinterpret_cast<uint8_t const*>(data.constData()), data.size(), rect); }
void VirtualPsxVRam::load(
uint8_t const* data,
uint32_t dataSize,
QRect const& rect)
{
if (rect.isEmpty())
{
throw QString("Invalid rect (%1, %2)(%3, %4).")
.arg(rect.left())
.arg(rect.top())
.arg(rect.right())
.arg(rect.bottom());
}
if (
rect.x() < 0 || rect.right() >= PsxVRamConst::PIXELS_PER_LINE ||
rect.y() < 0 || rect.bottom() >= PsxVRamConst::HEIGHT)
{
throw QString("Rect (%1, %2)(%3, %4) is out of VRAM bounds.")
.arg(rect.left())
.arg(rect.top())
.arg(rect.right())
.arg(rect.bottom());
}
auto scanLineDataSize = rect.width() * PsxVRamConst::PIXEL_SIZE;
uint32_t expectedDataSize = scanLineDataSize * rect.height();
if (dataSize != expectedDataSize)
{
throw QString(
"With rect (%1, %2)(%3, %4) expected data size is 0x%5. "
"Provided data size 0x%6.")
.arg(rect.left())
.arg(rect.top())
.arg(rect.right())
.arg(rect.bottom())
.arg(expectedDataSize, 0, 16)
.arg(dataSize, 0, 16);
}
auto* dataStartIt = data;
for (int y = rect.y(); y <= rect.bottom(); ++y)
{
auto* dataEndIt = dataStartIt + scanLineDataSize;
auto* vramPixel = pixelAddress(rect.x(), y);
std::copy(dataStartIt, dataEndIt, vramPixel);
dataStartIt = dataEndIt;
}
appendInitializedRect(rect);
}
void VirtualPsxVRam::appendInitializedRect(QRect const& rect)
{
initializedBoundingRect_ = initializedBoundingRect_.united(rect);
for (int i = 0; i < initializedRects_.size(); ++i)
{
auto& initializedRect = initializedRects_[i];
if (initializedRect.contains(rect))
{ return; }
// TODO: add combining rects when adjacent
}
initializedRects_.append(rect);
}
bool VirtualPsxVRam::isRectInitialized(QRect const& rect) const
{
// This function is not fully correct. It will all only tell wheter
// all rectangle corner points are initialized.
if (!initializedBoundingRect_.contains(rect))
{ return false; }
if (!isPointInitialized(rect.topLeft()))
{ return false; }
if (!isPointInitialized(rect.topRight()))
{ return false; }
if (!isPointInitialized(rect.bottomLeft()))
{ return false; }
if (!isPointInitialized(rect.bottomLeft()))
{ return false; }
return true;
}
bool VirtualPsxVRam::isPointInitialized(QPoint const& point) const
{
for (auto const& initializedRect : initializedRects_)
{
if (initializedRect.contains(point))
{ return true; }
}
return false;
}
void VirtualPsxVRam::throwUninitializedRectError(
QRect const& rect,
QString const& readType) const
{
throw QString("Trying to read %1 from uninitialized rect (%2, %3)(%4, %5).")
.arg(readType)
.arg(rect.left())
.arg(rect.top())
.arg(rect.right())
.arg(rect.bottom());
}
QRect VirtualPsxVRam::calculateVRamRect(Graphic const& graphic)
{
auto xShift = inTextureXShift(graphic.texpage.texpageBpp());
return QRect(
calculateTextureVRamPoint(graphic),
QSize(graphic.width >> xShift, graphic.height));
}
uint8_t VirtualPsxVRam::inTextureXShift(TexpageBpp bpp)
{
if (bpp == TexpageBpp::BPP_4)
{ return 2; }
else if (bpp == TexpageBpp::BPP_8)
{ return 1; }
else
{ return 0; }
}
QPoint VirtualPsxVRam::calculateTextureVRamPoint(Graphic const& graphic)
{
auto const& texpage = graphic.texpage;
QPoint point(
texpage.x << PsxVRamConst::TEXTURE_PAGE_X_SHIFT,
texpage.y << PsxVRamConst::TEXTURE_PAGE_Y_SHIFT);
point.rx() +=
graphic.xOffsetInTexpage >> inTextureXShift(texpage.texpageBpp());
point.ry() += graphic.yOffsetInTexpage;
return point;
}