<commit_before>#include "framework/apocresources/pck.h"
#include "framework/data.h"
#include "framework/framework.h"
#include "framework/image.h"
#include "framework/logger.h"
#include "framework/trace.h"
#include "library/sp.h"
#include <istream>
#include <vector>

namespace OpenApoc
{

static const unsigned int IMAGE_STRIDE = 640;

#pragma pack(push, 1)
struct PckHeader
{
	uint16_t compressionMode;
	uint16_t unknown;
	uint16_t leftClip;
	uint16_t rightClip;
	uint16_t topClip;
	uint16_t bottomClip;
};
#pragma pack(pop)
static_assert(sizeof(struct PckHeader) == 12, "PckHeader not 12 bytes");

#pragma pack(push, 1)
struct PckRLE1Header
{
	uint32_t pixelSkip;
	uint8_t column;
	uint16_t pixelCount;
	uint8_t padding;
};
#pragma pack(pop)
static_assert(sizeof(struct PckRLE1Header) == 8, "RLE1Header not 8 bytes");

static sp<PaletteImage> readPckCompression1(std::istream &input, Vec2<unsigned> size)
{
	auto img = mksp<PaletteImage>(size);

	struct PckRLE1Header header;

	input.read(reinterpret_cast<char *>(&header), sizeof(header));

	PaletteImageLock l(img, ImageLockUse::Write);

	while (input && header.pixelSkip != 0xffffffff)
	{
		unsigned int col = header.pixelSkip % IMAGE_STRIDE;

		if (col != header.column)
		{
			LogWarning("Header column %u doesn't match skip column %u (%u %% %u)",
			           (unsigned)header.column, col, (unsigned)header.pixelSkip, IMAGE_STRIDE);
			return nullptr;
		}

		for (unsigned int i = 0; i < header.pixelCount; i++)
		{
			unsigned int x = (i + header.pixelSkip) % IMAGE_STRIDE;
			unsigned int y = (i + header.pixelSkip) / IMAGE_STRIDE;
			uint8_t idx = 0;
			input.read(reinterpret_cast<char *>(&idx), sizeof(idx));
			if (!input)
			{
				LogWarning("Unexpected EOF reading Pck RLE data");
				return nullptr;
			}
			if (x < size.x && y < size.y)
				l.set({x, y}, idx);
		}
		input.read(reinterpret_cast<char *>(&header), sizeof(header));
	}
	return img;
}

#pragma pack(push, 1)
struct PckBlkHeader
{
	uint8_t rowRecords;
	uint16_t unknown;
	uint8_t row;
};
#pragma pack(pop)
static_assert(sizeof(struct PckBlkHeader) == 4, "BlkHeader not 4 bytes");

#pragma pack(push, 1)
struct PckBlkSubHeader
{
	uint8_t pixelSkip;
	uint8_t pixelCount;
	// MSVC doesn't like aligning bit fields, this is really a 24bit 'little endian' offset
	uint8_t blkOffset[3];
};
#pragma pack(pop)
static_assert(sizeof(struct PckBlkSubHeader) == 5, "BlkSubHeader not 5 bytes");

static sp<PaletteImage> readPckCompression3(std::istream &input, Vec2<unsigned> size)
{
	static size_t blkSize;
	static up<char[]> blkData;

	if (!blkData)
	{
		auto blkFile = fw().data->fs.open("xcom3/tacdata/xcom.blk");
		if (!blkFile)
		{
			LogWarning("Failed to open xcom.blk");
			return nullptr;
		}
		blkSize = blkFile.size();
		blkData = std::move(blkFile.readAll());
		LogInfo("Loaded %zu bytes of xcom.blk", blkSize);
	}

	auto img = mksp<PaletteImage>(size);
<commit_msg>Remove unnecessary move in PCK loading<commit_after>#include "framework/apocresources/pck.h"
#include "framework/data.h"
#include "framework/framework.h"
#include "framework/image.h"
#include "framework/logger.h"
#include "framework/trace.h"
#include "library/sp.h"
#include <istream>
#include <vector>

namespace OpenApoc
{

static const unsigned int IMAGE_STRIDE = 640;

#pragma pack(push, 1)
struct PckHeader
{
	uint16_t compressionMode;
	uint16_t unknown;
	uint16_t leftClip;
	uint16_t rightClip;
	uint16_t topClip;
	uint16_t bottomClip;
};
#pragma pack(pop)
static_assert(sizeof(struct PckHeader) == 12, "PckHeader not 12 bytes");

#pragma pack(push, 1)
struct PckRLE1Header
{
	uint32_t pixelSkip;
	uint8_t column;
	uint16_t pixelCount;
	uint8_t padding;
};
#pragma pack(pop)
static_assert(sizeof(struct PckRLE1Header) == 8, "RLE1Header not 8 bytes");

static sp<PaletteImage> readPckCompression1(std::istream &input, Vec2<unsigned> size)
{
	auto img = mksp<PaletteImage>(size);

	struct PckRLE1Header header;

	input.read(reinterpret_cast<char *>(&header), sizeof(header));

	PaletteImageLock l(img, ImageLockUse::Write);

	while (input && header.pixelSkip != 0xffffffff)
	{
		unsigned int col = header.pixelSkip % IMAGE_STRIDE;

		if (col != header.column)
		{
			LogWarning("Header column %u doesn't match skip column %u (%u %% %u)",
			           (unsigned)header.column, col, (unsigned)header.pixelSkip, IMAGE_STRIDE);
			return nullptr;
		}

		for (unsigned int i = 0; i < header.pixelCount; i++)
		{
			unsigned int x = (i + header.pixelSkip) % IMAGE_STRIDE;
			unsigned int y = (i + header.pixelSkip) / IMAGE_STRIDE;
			uint8_t idx = 0;
			input.read(reinterpret_cast<char *>(&idx), sizeof(idx));
			if (!input)
			{
				LogWarning("Unexpected EOF reading Pck RLE data");
				return nullptr;
			}
			if (x < size.x && y < size.y)
				l.set({x, y}, idx);
		}
		input.read(reinterpret_cast<char *>(&header), sizeof(header));
	}
	return img;
}

#pragma pack(push, 1)
struct PckBlkHeader
{
	uint8_t rowRecords;
	uint16_t unknown;
	uint8_t row;
};
#pragma pack(pop)
static_assert(sizeof(struct PckBlkHeader) == 4, "BlkHeader not 4 bytes");

#pragma pack(push, 1)
struct PckBlkSubHeader
{
	uint8_t pixelSkip;
	uint8_t pixelCount;
	// MSVC doesn't like aligning bit fields, this is really a 24bit 'little endian' offset
	uint8_t blkOffset[3];
};
#pragma pack(pop)
static_assert(sizeof(struct PckBlkSubHeader) == 5, "BlkSubHeader not 5 bytes");

static sp<PaletteImage> readPckCompression3(std::istream &input, Vec2<unsigned> size)
{
	static size_t blkSize;
	static up<char[]> blkData;

	if (!blkData)
	{
		auto blkFile = fw().data->fs.open("xcom3/tacdata/xcom.blk");
		if (!blkFile)
		{
			LogWarning("Failed to open xcom.blk");
			return nullptr;
		}
		blkSize = blkFile.size();
		blkData = blkFile.readAll();
		LogInfo("Loaded %zu bytes of xcom.blk", blkSize);
	}

	auto img = mksp<PaletteImage>(size);

{'fraction_non_alphanumeric': 0.1346418732782369, 'fraction_numerical': 0.01997245179063361, 'mean_word_length': 4.832329317269076, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2466752', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2147, 'n_words': 527}