rodecomp/Source/Resource/Sprites/SpriteEncoding.cpp
ragnarok-clanker 0e9cc185be Decompile fn_08062 (CSprRes::ReduceSprite)
Batch #12514 by dori: 0 byte-identical, 1 confirmed by peer review.

- fn_08062 CSprRes::ReduceSprite: logic confirmed by peer review

Peer-reviewed-by: Yommy, gordaoprogramas
Accepted without human review: peers confirmed the logic ("same" ahead by at least 3, with 70% of the votes).
2026-11-05 16:25:01 +00:00

82 lines
2.3 KiB
C++

#include "Resource/Sprites/SpriteResource.h"
#include <cstddef>
// OFFSET: 0x00761070
// FUNCTION: fn_08063
std::uint8_t* CSprRes::EncodeSpriteRuns(const std::uint8_t* pixels, int width, int height, std::uint16_t& bytes) {
std::vector<std::uint8_t> encoded;
std::uint16_t zeroCount = 0;
int zeroRun = 0;
std::uint16_t index = 0;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x, ++index) {
if (pixels[index] == 0) {
if (zeroRun) {
if (++zeroCount >= 255) {
encoded.push_back(static_cast<std::uint8_t>(zeroCount));
zeroRun = 0;
zeroCount = 0;
}
} else {
encoded.push_back(0);
zeroRun = 1;
++zeroCount;
}
} else {
if (zeroRun) {
encoded.push_back(static_cast<std::uint8_t>(zeroCount));
}
encoded.push_back(pixels[index]);
zeroRun = 0;
zeroCount = 0;
}
}
}
if (zeroCount != 0) {
encoded.push_back(static_cast<std::uint8_t>(zeroCount));
}
bytes = static_cast<std::uint16_t>(encoded.size());
auto* result = new std::uint8_t[bytes];
for (std::uint16_t index = 0; index < bytes; ++index) {
result[index] = encoded[index];
}
return result;
}
// OFFSET: 0x00760FE0
// FUNCTION: fn_08062
std::uint8_t* CSprRes::ReduceSprite(const std::uint8_t* pixels, int width, int height, int halfWidth, int halfHeight) {
const int outputWidth = halfWidth != 0 ? width / 2 : width;
const int outputHeight = halfHeight != 0 ? height / 2 : height;
auto* output = new std::uint8_t[outputWidth * outputHeight];
std::uint8_t* destination = output;
for (int y = 0; y < outputHeight; ++y) {
const auto* source = pixels + width * y * (halfHeight + 1);
for (int x = 0; x < outputWidth; ++x) {
*destination++ = *source;
source += halfWidth + 1;
}
}
return output;
}
// OFFSET: 0x0056F7B0
// FUNCTION: fn_02405
void ConvertSpritePalette(std::uint32_t* output, const std::uint8_t* input, int count) {
for (int index = 0; index < count; ++index) {
const std::uint8_t red = input[index * 4];
const std::uint8_t green = input[index * 4 + 1];
const std::uint8_t blue = input[index * 4 + 2];
output[index] = red >= 254 && green < 4 && blue >= 254 ? 1057u : ((blue | 0x40000u) >> 3) | (4 * ((green & 0xF8u) | (32 * (red & 0xF8u))));
}
}