168 lines
5.3 KiB
C
168 lines
5.3 KiB
C
/* *****************************************************************************
|
|
* decode/std/picture.c -- decode a picture file.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libg1m.
|
|
* libg1m is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libg1m 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 Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
#include <zlib.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Obfuscation-related functions */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g3p_deobfuscate:
|
|
* De-obfuscate the image data.
|
|
*
|
|
* @arg buf the buffer.
|
|
* @arg n the buffer size.
|
|
*/
|
|
|
|
static void g3p_deobfuscate(uint8_t *buf, size_t n)
|
|
{
|
|
while (n--) {
|
|
uint8_t byte = ~*buf;
|
|
*buf++ = (byte << 5) | (byte >> 3);
|
|
}
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Prizm Picture decoding function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_std_g3p:
|
|
* Decode a G3P file.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg std the standard header.
|
|
* @arg pic the standard picture header.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_std_g3p(g1m_t **h, g1m_buffer_t *buffer,
|
|
struct standard_header *std, struct standard_picheader *pic)
|
|
{
|
|
int err; *h = NULL;
|
|
uint8_t *defbuf = NULL, *infbuf = NULL;
|
|
|
|
/* read the image header */
|
|
DREAD(ihd, g3p_subheader)
|
|
unsigned int width = be16toh(ihd.width), height = be16toh(ihd.height);
|
|
log_info("Width: %upx, height: %upx", width, height);
|
|
g1m_pictureformat_t picfmt; uint16_t rawcol = be16toh(ihd.color_depth);
|
|
const char *coldesc; switch (rawcol) {
|
|
case g3p_color_4bit: picfmt = g1m_pictureformat_4bit_code;
|
|
coldesc = "4-bit"; break;
|
|
case g3p_color_16bit: picfmt = g1m_pictureformat_16bit;
|
|
coldesc = "16-bit"; break;
|
|
default: log_error("Unknown picture format: 0x%04" PRIX16, rawcol);
|
|
err = g1m_error_magic; goto fail;
|
|
}
|
|
log_info("Picture format: %s", coldesc);
|
|
int gen = be16toh(ihd.generator_id);
|
|
log_info("Generator ID: 0x%04X", gen);
|
|
log_info("-");
|
|
|
|
/* read deflated image */
|
|
size_t deflated_size = be32toh(ihd.data_size) - 6; /* FIXME: dangerous */
|
|
log_info("Reading %" PRIuSIZE "B of deflated data", deflated_size);
|
|
err = g1m_error_alloc;
|
|
if (!(defbuf = malloc(deflated_size))) goto fail;
|
|
READ(defbuf, deflated_size)
|
|
|
|
/* unobfuscate if required */
|
|
if (std->obfuscated0) {
|
|
log_info("Is obfuscated, let's deobfuscate!");
|
|
g3p_deobfuscate(defbuf, deflated_size);
|
|
}
|
|
|
|
/* make the destination buffer */
|
|
size_t rawsize = picfmt == g1m_pictureformat_4bit_code
|
|
? g1m_picturesize_4bit_code(width, height)
|
|
: g1m_picturesize_16bit(width, height);
|
|
err = g1m_error_alloc;
|
|
if (!(infbuf = malloc(rawsize))) goto fail;
|
|
|
|
/* uncompress */
|
|
uLong inflated_size = (uLong)rawsize;
|
|
int z_err = uncompress(infbuf, &inflated_size,
|
|
defbuf, (uLong)deflated_size);
|
|
if (z_err) {
|
|
log_fatal("Zlib error: error #%d", z_err);
|
|
err = g1m_error_magic;
|
|
goto fail;
|
|
}
|
|
free(defbuf); defbuf = NULL;
|
|
|
|
/* check the checksum */
|
|
uint32_t checksum; READ(&checksum, sizeof(uint32_t))
|
|
if (std->obfuscated0) g3p_deobfuscate((void*)&checksum, sizeof(uint32_t));
|
|
checksum = be32toh(checksum);
|
|
uLong adl = adler32(0, infbuf, inflated_size);
|
|
if (adl != (uLong)checksum) {
|
|
log_fatal("Incorrect Adler32 checksum!");
|
|
log_fatal("Expected 0x%08" PRIX32 ", got 0x%08lX",
|
|
checksum, adl);
|
|
err = g1m_error_checksum;
|
|
goto fail;
|
|
}
|
|
|
|
/* make the handle */
|
|
err = g1m_make_picture(h, ihd.width, ihd.height);
|
|
if (err) goto fail;
|
|
g1m_t *handle = *h;
|
|
|
|
/* then store it */
|
|
g1m_decode_picture(handle->pixels, picfmt,
|
|
infbuf, handle->width, handle->height);
|
|
free(infbuf); infbuf = NULL;
|
|
|
|
/* TODO: footers? */
|
|
|
|
/* no error */
|
|
return (0);
|
|
fail:
|
|
g1m_free(*h); *h = NULL;
|
|
free(infbuf); free(defbuf);
|
|
return (err);
|
|
}
|
|
|
|
/* ************************************************************************** */
|
|
/* Classpad Picture decoding function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* g1m_decode_std_c2p:
|
|
* Decode Classpad images.
|
|
*
|
|
* @arg handle the handle.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg std the standard header.
|
|
* @arg pic the standard picture header.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_std_c2p(g1m_t **h, g1m_buffer_t *buffer,
|
|
struct standard_header *std, struct standard_picheader *pic)
|
|
{
|
|
(void)h; (void)buffer;
|
|
(void)std; (void)pic;
|
|
/* TODO */
|
|
log_info("C2P files are not managed yet.");
|
|
|
|
/* no error */
|
|
return (g1m_error_magic);
|
|
}
|