mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-11 10:02:27 +01:00
Refactor RLE minify
This commit is contained in:
@@ -7,8 +7,6 @@
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma warning(disable : 4127) // conditional expression is constant
|
||||
|
||||
#include "Drawing.h"
|
||||
|
||||
#include <cstring>
|
||||
@@ -156,127 +154,95 @@ template<DrawBlendOp TBlendOp, size_t TZoom> static void FASTCALL DrawRLESpriteM
|
||||
}
|
||||
}
|
||||
|
||||
template<DrawBlendOp TBlendOp, int32_t zoom_level> static void FASTCALL DrawRLESpriteMinify(DrawSpriteArgs& args)
|
||||
template<DrawBlendOp TBlendOp, size_t TZoom> static void FASTCALL DrawRLESpriteMinify(DrawSpriteArgs& args)
|
||||
{
|
||||
auto dpi = args.DPI;
|
||||
auto source_bits_pointer = args.SourceImage.offset;
|
||||
auto dest_bits_pointer = args.DestinationBits;
|
||||
auto source_x_start = args.SrcX;
|
||||
auto source_y_start = args.SrcY;
|
||||
auto src0 = args.SourceImage.offset;
|
||||
auto dst0 = args.DestinationBits;
|
||||
auto srcX = args.SrcX;
|
||||
auto srcY = args.SrcY;
|
||||
auto width = args.Width;
|
||||
auto height = args.Height;
|
||||
[[maybe_unused]] auto& paletteMap = args.PalMap;
|
||||
|
||||
// The distance between two samples in the source image.
|
||||
// We draw the image at 1 / (2^zoom_level) scale.
|
||||
int32_t zoom_amount = 1 << zoom_level;
|
||||
|
||||
// Width of one screen line in the dest buffer
|
||||
int32_t line_width = (dpi->width >> zoom_level) + dpi->pitch;
|
||||
auto& paletteMap = args.PalMap;
|
||||
auto zoom = 1 << TZoom;
|
||||
auto dstLineWidth = (static_cast<size_t>(dpi->width) >> TZoom) + dpi->pitch;
|
||||
|
||||
// Move up to the first line of the image if source_y_start is negative. Why does this even occur?
|
||||
if (source_y_start < 0)
|
||||
if (srcY < 0)
|
||||
{
|
||||
source_y_start += zoom_amount;
|
||||
height -= zoom_amount;
|
||||
dest_bits_pointer += line_width;
|
||||
srcY += zoom;
|
||||
height -= zoom;
|
||||
dst0 += dstLineWidth;
|
||||
}
|
||||
|
||||
// For every line in the image
|
||||
for (int32_t i = 0; i < height; i += zoom_amount)
|
||||
for (int32_t i = 0; i < height; i += zoom)
|
||||
{
|
||||
int32_t y = source_y_start + i;
|
||||
int32_t y = srcY + i;
|
||||
|
||||
// The first part of the source pointer is a list of offsets to different lines
|
||||
// This will move the pointer to the correct source line.
|
||||
const uint16_t lineOffset = source_bits_pointer[y * 2] | (source_bits_pointer[y * 2 + 1] << 8);
|
||||
const uint8_t* lineData = source_bits_pointer + lineOffset;
|
||||
uint8_t* loop_dest_pointer = dest_bits_pointer + line_width * (i >> zoom_level);
|
||||
|
||||
uint8_t isEndOfLine = 0;
|
||||
uint16_t lineOffset = src0[y * 2] | (src0[y * 2 + 1] << 8);
|
||||
auto nextRun = src0 + lineOffset;
|
||||
auto dstLineStart = dst0 + dstLineWidth * (i >> TZoom);
|
||||
|
||||
// For every data chunk in the line
|
||||
auto isEndOfLine = false;
|
||||
while (!isEndOfLine)
|
||||
{
|
||||
const uint8_t* copySrc = lineData;
|
||||
|
||||
// Read chunk metadata
|
||||
uint8_t dataSize = *copySrc++;
|
||||
uint8_t firstPixelX = *copySrc++;
|
||||
|
||||
isEndOfLine = dataSize & 0x80; // If the last bit in dataSize is set, then this is the last line
|
||||
dataSize &= 0x7F; // The rest of the bits are the actual size
|
||||
auto src = nextRun;
|
||||
auto dataSize = *src++;
|
||||
auto firstPixelX = *src++;
|
||||
isEndOfLine = (dataSize & 0x80) != 0;
|
||||
dataSize &= 0x7F;
|
||||
|
||||
// Have our next source pointer point to the next data section
|
||||
lineData = copySrc + dataSize;
|
||||
nextRun = src + dataSize;
|
||||
|
||||
int32_t x_start = firstPixelX - source_x_start;
|
||||
int32_t x = firstPixelX - srcX;
|
||||
int32_t numPixels = dataSize;
|
||||
|
||||
if (x_start > 0)
|
||||
if (x > 0)
|
||||
{
|
||||
int mod = x_start & (zoom_amount - 1); // x_start modulo zoom_amount
|
||||
|
||||
// If x_start is not a multiple of zoom_amount, round it up to a multiple
|
||||
// If x is not a multiple of zoom, round it up to a multiple
|
||||
auto mod = x & (zoom - 1);
|
||||
if (mod != 0)
|
||||
{
|
||||
int offset = zoom_amount - mod;
|
||||
x_start += offset;
|
||||
copySrc += offset;
|
||||
auto offset = zoom - mod;
|
||||
x += offset;
|
||||
src += offset;
|
||||
numPixels -= offset;
|
||||
}
|
||||
}
|
||||
else if (x_start < 0)
|
||||
else if (x < 0)
|
||||
{
|
||||
// Clamp x_start to zero if negative
|
||||
int offset = 0 - x_start;
|
||||
x_start = 0;
|
||||
copySrc += offset;
|
||||
numPixels -= offset;
|
||||
// Clamp x to zero if negative
|
||||
src += -x;
|
||||
numPixels += x;
|
||||
x = 0;
|
||||
}
|
||||
|
||||
// If the end position is further out than the whole image
|
||||
// end position then we need to shorten the line again
|
||||
if (x_start + numPixels > width)
|
||||
numPixels = width - x_start;
|
||||
numPixels = std::min(numPixels, width - x);
|
||||
|
||||
uint8_t* copyDest = loop_dest_pointer + (x_start >> zoom_level);
|
||||
|
||||
// Finally after all those checks, copy the image onto the drawing surface
|
||||
// If the image type is not a basic one we require to mix the pixels
|
||||
if constexpr ((TBlendOp & BLEND_SRC) != 0) // palette controlled images
|
||||
auto dst = dstLineStart + (x >> TZoom);
|
||||
if constexpr ((TBlendOp & BLEND_SRC) == 0 && (TBlendOp & BLEND_DST) == 0 && TZoom == 0)
|
||||
{
|
||||
for (int j = 0; j < numPixels; j += zoom_amount, copySrc += zoom_amount, copyDest++)
|
||||
// Since we're sampling each pixel at this zoom level, just do a straight std::memcpy
|
||||
if (numPixels > 0)
|
||||
{
|
||||
if ((TBlendOp & BLEND_DST) != 0)
|
||||
{
|
||||
*copyDest = paletteMap.Blend(*copySrc, *copyDest);
|
||||
}
|
||||
else
|
||||
{
|
||||
*copyDest = paletteMap[*copySrc];
|
||||
}
|
||||
std::memcpy(dst, src, numPixels);
|
||||
}
|
||||
}
|
||||
else if constexpr ((TBlendOp & BLEND_DST) != 0) // single alpha blended color (used for glass)
|
||||
else
|
||||
{
|
||||
for (int j = 0; j < numPixels; j += zoom_amount, copyDest++)
|
||||
while (numPixels > 0)
|
||||
{
|
||||
*copyDest = paletteMap[*copyDest];
|
||||
}
|
||||
}
|
||||
else // standard opaque image
|
||||
{
|
||||
if (zoom_level == 0)
|
||||
{
|
||||
// Since we're sampling each pixel at this zoom level, just do a straight std::memcpy
|
||||
if (numPixels > 0)
|
||||
std::memcpy(copyDest, copySrc, numPixels);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = 0; j < numPixels; j += zoom_amount, copySrc += zoom_amount, copyDest++)
|
||||
*copyDest = *copySrc;
|
||||
BlitPixel<TBlendOp>(src, dst, paletteMap);
|
||||
numPixels -= zoom;
|
||||
src += zoom;
|
||||
dst++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user