MCPcopy Create free account
hub / github.com/OpenApoc/OpenApoc / readPckCompression3

Function readPckCompression3

framework/apocresources/pck.cpp:100–168  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

98static_assert(sizeof(struct PckBlkSubHeader) == 5, "BlkSubHeader not 5 bytes");
99
100static sp<PaletteImage> readPckCompression3(std::istream &input, Vec2<unsigned> size)
101{
102 static size_t blkSize;
103 static up<char[]> blkData;
104
105 if (!blkData)
106 {
107 auto blkFile = fw().data->fs.open("xcom3/tacdata/xcom.blk");
108 if (!blkFile)
109 {
110 LogWarning("Failed to open xcom.blk");
111 return nullptr;
112 }
113 blkSize = blkFile.size();
114 blkData = blkFile.readAll();
115 LogInfo("Loaded %zu bytes of xcom.blk", blkSize);
116 }
117
118 auto img = mksp<PaletteImage>(size);
119
120 PaletteImageLock l(img, ImageLockUse::Write);
121
122 struct PckBlkHeader header;
123
124 input.read(reinterpret_cast<char *>(&header), sizeof(header));
125
126 while (input && header.rowRecords != 0xff && header.unknown != 0xffff && header.row != 0xff)
127 {
128 unsigned col = 0;
129 unsigned row = header.row;
130 for (unsigned record = 0; record < header.rowRecords; record++)
131 {
132 struct PckBlkSubHeader subHeader;
133 input.read(reinterpret_cast<char *>(&subHeader), sizeof(subHeader));
134 if (!input)
135 {
136 LogWarning("Unexpected EOF reading row header");
137 return nullptr;
138 }
139 unsigned blkOffset = 0;
140 blkOffset =
141 subHeader.blkOffset[0] | subHeader.blkOffset[1] << 8 | subHeader.blkOffset[2] << 16;
142 col += subHeader.pixelSkip;
143 for (unsigned i = 0; i < subHeader.pixelCount; i++)
144 {
145 if (blkOffset >= blkSize)
146 {
147 LogWarning("BLKOffset %u too large for xcom.blk size", blkOffset);
148 }
149 else
150 {
151 if (row < size.y && col < size.x)
152 {
153 l.set({col, row}, blkData[blkOffset]);
154 }
155 else
156 {
157 LogWarning("{%d,%d} out of bounds", col, row);

Callers 1

loadMethod · 0.85

Calls 5

openMethod · 0.80
readAllMethod · 0.80
readMethod · 0.80
sizeMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected