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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ags/shared/util/compress.h"
#include "common/std/vector.h"
#include "ags/shared/ac/common.h" // quit, update_polled_stuff
#include "ags/shared/gfx/bitmap.h"
#include "ags/shared/util/file.h"
#include "ags/shared/util/lzw.h"
#include "ags/shared/util/memory_stream.h"
#include "ags/globals.h"
#if AGS_PLATFORM_ENDIAN_BIG
#include "ags/shared/util/bbop.h"
#endif
#include "common/compression/deflate.h"
namespace AGS3 {
using namespace AGS::Shared;
//-----------------------------------------------------------------------------
// RLE
//-----------------------------------------------------------------------------
static void cpackbitl(const uint8_t *line, size_t size, Stream *out) {
size_t cnt = 0; // bytes encoded
while (cnt < size) {
// IMPORTANT: the algorithm below requires signed operations
int i = static_cast<int32_t>(cnt);
int j = i + 1;
int jmax = i + 126;
if (static_cast<uint32_t>(jmax) >= size)
jmax = size - 1;
if (static_cast<uint32_t>(i) == size - 1) { //......last byte alone
out->WriteInt8(0);
out->WriteInt8(line[i]);
cnt++;
} else if (line[i] == line[j]) { //....run
while ((j < jmax) && (line[j] == line[j + 1]))
j++;
out->WriteInt8(i - j);
out->WriteInt8(line[i]);
cnt += j - i + 1;
} else { //.............................sequence
while ((j < jmax) && (line[j] != line[j + 1]))
j++;
out->WriteInt8(j - i);
out->Write(line + i, j - i + 1);
cnt += j - i + 1;
}
} // end while
}
static void cpackbitl16(const uint16_t *line, size_t size, Stream *out) {
size_t cnt = 0; // bytes encoded
while (cnt < size) {
// IMPORTANT: the algorithm below requires signed operations
int i = cnt;
int j = i + 1;
int jmax = i + 126;
if (static_cast<uint32_t>(jmax) >= size)
jmax = size - 1;
if (static_cast<uint32_t>(i) == size - 1) { //......last byte alone
out->WriteInt8(0);
out->WriteInt16(line[i]);
cnt++;
} else if (line[i] == line[j]) { //....run
while ((j < jmax) && (line[j] == line[j + 1]))
j++;
out->WriteInt8(i - j);
out->WriteInt16(line[i]);
cnt += j - i + 1;
} else { //.............................sequence
while ((j < jmax) && (line[j] != line[j + 1]))
j++;
out->WriteInt8(j - i);
out->WriteArray(line + i, j - i + 1, 2);
cnt += j - i + 1;
}
} // end while
}
static void cpackbitl32(const uint32_t *line, size_t size, Stream *out) {
size_t cnt = 0; // bytes encoded
while (cnt < size) {
// IMPORTANT: the algorithm below requires signed operations
int i = cnt;
int j = i + 1;
int jmax = i + 126;
if (static_cast<uint32_t>(jmax) >= size)
jmax = size - 1;
if (static_cast<uint32_t>(i) == size - 1) { //......last byte alone
out->WriteInt8(0);
out->WriteInt32(line[i]);
cnt++;
} else if (line[i] == line[j]) { //....run
while ((j < jmax) && (line[j] == line[j + 1]))
j++;
out->WriteInt8(i - j);
out->WriteInt32(line[i]);
cnt += j - i + 1;
} else { //.............................sequence
while ((j < jmax) && (line[j] != line[j + 1]))
j++;
out->WriteInt8(j - i);
out->WriteArray(line + i, j - i + 1, 4);
cnt += j - i + 1;
}
} // end while
}
static int cunpackbitl(uint8_t *line, size_t size, Stream *in) {
size_t n = 0; // number of bytes decoded
while (n < size) {
int ix = in->ReadByte(); // get index byte
int8 cx = ix;
if (cx == -128)
cx = 0;
if (cx < 0) { //.............run
int i = 1 - cx;
char ch = in->ReadInt8();
while (i--) {
// test for buffer overflow
if (n >= size)
return -1;
line[n++] = ch;
}
} else { //.....................seq
int i = cx + 1;
while (i--) {
// test for buffer overflow
if (n >= size)
return -1;
line[n++] = in->ReadByte();
}
}
}
return 0;
}
static int cunpackbitl16(uint16_t *line, size_t size, Stream *in) {
size_t n = 0; // number of bytes decoded
while (n < size) {
int ix = in->ReadByte(); // get index byte
int8 cx = ix;
if (cx == -128)
cx = 0;
if (cx < 0) { //.............run
int i = 1 - cx;
unsigned short ch = in->ReadInt16();
while (i--) {
// test for buffer overflow
if (n >= size)
return -1;
line[n++] = ch;
}
} else { //.....................seq
int i = cx + 1;
while (i--) {
// test for buffer overflow
if (n >= size)
return -1;
line[n++] = in->ReadInt16();
}
}
}
return 0;
}
static int cunpackbitl32(uint32_t *line, size_t size, Stream *in) {
size_t n = 0; // number of bytes decoded
while (n < size) {
int ix = in->ReadByte(); // get index byte
int8 cx = ix;
if (cx == -128)
cx = 0;
if (cx < 0) { //.............run
int i = 1 - cx;
unsigned int ch = in->ReadInt32();
while (i--) {
// test for buffer overflow
if (n >= size)
return -1;
line[n++] = ch;
}
} else { //.....................seq
int i = cx + 1;
while (i--) {
// test for buffer overflow
if (n >= size)
return -1;
line[n++] = (unsigned int)in->ReadInt32();
}
}
}
return 0;
}
bool rle_compress(const uint8_t *data, size_t data_sz, int image_bpp, Stream *out) {
switch (image_bpp) {
case 1: cpackbitl(data, data_sz, out); break;
case 2: cpackbitl16(reinterpret_cast<const uint16_t *>(data), data_sz / sizeof(uint16_t), out); break;
case 4: cpackbitl32(reinterpret_cast<const uint32_t *>(data), data_sz / sizeof(uint32_t), out); break;
default: assert(0); break;
}
return true;
}
bool rle_decompress(uint8_t *data, size_t data_sz, int image_bpp, Stream *in) {
switch (image_bpp) {
case 1: cunpackbitl(data, data_sz, in); break;
case 2: cunpackbitl16(reinterpret_cast<uint16_t *>(data), data_sz / sizeof(uint16_t), in); break;
case 4: cunpackbitl32(reinterpret_cast<uint32_t *>(data), data_sz / sizeof(uint32_t), in); break;
default: assert(0); break;
}
return true;
}
void save_rle_bitmap8(Stream *out, const Bitmap *bmp, const RGB(*pal)[256]) {
assert(bmp->GetBPP() == 1);
out->WriteInt16(static_cast<uint16_t>(bmp->GetWidth()));
out->WriteInt16(static_cast<uint16_t>(bmp->GetHeight()));
// Pack the pixels
cpackbitl(bmp->GetData(), bmp->GetWidth() * bmp->GetHeight(), out);
// Save palette
if (!pal) { // if no pal, write dummy palette, because we have to
out->WriteByteCount(0, 256 * 3);
return;
}
const RGB *ppal = *pal;
for (int i = 0; i < 256; ++i) {
out->WriteInt8(ppal[i].r);
out->WriteInt8(ppal[i].g);
out->WriteInt8(ppal[i].b);
}
}
Shared::Bitmap *load_rle_bitmap8(Stream *in, RGB(*pal)[256]) {
int w = in->ReadInt16();
int h = in->ReadInt16();
Bitmap *bmp = BitmapHelper::CreateBitmap(w, h, 8);
if (!bmp) return nullptr;
// Unpack the pixels
cunpackbitl(bmp->GetDataForWriting(), w * h, in);
// Load or skip the palette
if (!pal) {
in->Seek(3 * 256);
return bmp;
}
RGB *ppal = *pal;
for (int i = 0; i < 256; ++i) {
ppal[i].r = in->ReadInt8();
ppal[i].g = in->ReadInt8();
ppal[i].b = in->ReadInt8();
}
return bmp;
}
void skip_rle_bitmap8(Stream *in) {
int w = in->ReadInt16();
int h = in->ReadInt16();
// Unpack the pixels into temp buf
std::vector<uint8_t> buf;
buf.resize(w * h);
cunpackbitl(&buf[0], w * h, in);
// Skip RGB palette
in->Seek(3 * 256);
}
//-----------------------------------------------------------------------------
// LZW
//-----------------------------------------------------------------------------
bool lzw_compress(const uint8_t *data, size_t data_sz, int /*image_bpp*/, Shared::Stream *out) {
// LZW algorithm that we use fails on sequence less than 16 bytes.
if (data_sz < 16) {
out->Write(data, data_sz);
return true;
}
MemoryStream mem_in(data, data_sz);
return lzwcompress(&mem_in, out);
}
bool lzw_decompress(uint8_t *data, size_t data_sz, int /*image_bpp*/, Shared::Stream *in, size_t in_sz) {
// LZW algorithm that we use fails on sequence less than 16 bytes.
if (data_sz < 16) {
in->Read(data, data_sz);
return true;
}
std::vector<uint8_t> in_buf(in_sz);
in->Read(in_buf.data(), in_sz);
return lzwexpand(in_buf.data(), in_sz, data, data_sz);
}
void save_lzw(Stream *out, const Bitmap *bmpp, const RGB(*pal)[256]) {
// First write original bitmap's info and data into the memory buffer
// NOTE: we must do this purely for backward compatibility with old room formats:
// because they also included bmp width and height into compressed data!
std::vector<uint8_t> membuf;
{
VectorStream memws(membuf, kStream_Write);
int w = bmpp->GetWidth(), h = bmpp->GetHeight(), bpp = bmpp->GetBPP();
memws.WriteInt32(w * bpp); // stride
memws.WriteInt32(h);
switch (bpp) {
case 1: memws.Write(bmpp->GetData(), w * h * bpp); break;
case 2: memws.WriteArrayOfInt16(reinterpret_cast<const int16_t *>(bmpp->GetData()), w *h); break;
case 4: memws.WriteArrayOfInt32(reinterpret_cast<const int32_t *>(bmpp->GetData()), w *h); break;
default: assert(0); break;
}
}
// Open same buffer for reading, and begin writing compressed data into the output
VectorStream mem_in(membuf);
// NOTE: old format saves full RGB struct here (4 bytes, including the filler)
if (pal)
out->WriteArray(*pal, sizeof(RGB), 256);
else
out->WriteByteCount(0, sizeof(RGB) * 256);
out->WriteInt32((uint32_t)mem_in.GetLength());
// reserve space for compressed size
soff_t cmpsz_at = out->GetPosition();
out->WriteInt32(0);
lzwcompress(&mem_in, out);
soff_t toret = out->GetPosition();
out->Seek(cmpsz_at, kSeekBegin);
soff_t compressed_sz = (toret - cmpsz_at) - sizeof(uint32_t);
out->WriteInt32(compressed_sz); // write compressed size
// seek back to the end of the output stream
out->Seek(toret, kSeekBegin);
}
Bitmap *load_lzw(Stream *in, int dst_bpp, RGB(*pal)[256]) {
// NOTE: old format saves full RGB struct here (4 bytes, including the filler)
if (pal)
in->Read(*pal, sizeof(RGB) * 256);
else
in->Seek(sizeof(RGB) * 256);
const size_t uncomp_sz = in->ReadInt32();
const size_t comp_sz = in->ReadInt32();
const soff_t end_pos = in->GetPosition() + comp_sz;
// First decompress data into the memory buffer
std::vector<uint8_t> inbuf(comp_sz);
std::vector<uint8_t> membuf(uncomp_sz);
in->Read(inbuf.data(), comp_sz);
lzwexpand(inbuf.data(), comp_sz, membuf.data(), uncomp_sz);
// Open same buffer for reading and get params and pixels
VectorStream mem_in(membuf);
int stride = mem_in.ReadInt32(); // width * bpp
int height = mem_in.ReadInt32();
Bitmap *bmm = BitmapHelper::CreateBitmap((stride / dst_bpp), height, dst_bpp * 8);
if (!bmm) return nullptr; // out of mem?
size_t num_pixels = stride * height / dst_bpp;
uint8_t *bmp_data = bmm->GetDataForWriting();
switch (dst_bpp) {
case 1: mem_in.Read(bmp_data, num_pixels); break;
case 2: mem_in.ReadArrayOfInt16(reinterpret_cast<int16_t *>(bmp_data), num_pixels); break;
case 4: mem_in.ReadArrayOfInt32(reinterpret_cast<int32_t *>(bmp_data), num_pixels); break;
default: assert(0); break;
}
if (in->GetPosition() != end_pos)
in->Seek(end_pos, kSeekBegin);
return bmm;
}
bool deflate_compress(const uint8_t *data, size_t data_sz, int /*image_bpp*/, Stream *out) {
// TODO
return false;
}
bool inflate_decompress(uint8_t *data, size_t data_sz, int /*image_bpp*/, Stream *in, size_t in_sz) {
std::vector<uint8_t> in_buf(in_sz);
in->Read(in_buf.data(), in_sz);
return Common::inflateZlib(data, (unsigned long)data_sz, in_buf.data(), in_sz);
}
} // namespace AGS3
|