From fd54698d12d468420d344f0ff4d5e502e04d64d4 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 2 Mar 2025 22:18:45 +0100 Subject: Add some functions needed fore encoding LSS16 files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- liblss16_encoder_init.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 liblss16_encoder_init.c (limited to 'liblss16_encoder_init.c') diff --git a/liblss16_encoder_init.c b/liblss16_encoder_init.c new file mode 100644 index 0000000..037cd8e --- /dev/null +++ b/liblss16_encoder_init.c @@ -0,0 +1,46 @@ +/* See LICENSE file for copyright and license details. */ +#include "liblss16.h" +#include + + +int +liblss16_encoder_init(struct liblss16_encoder *encoder, const struct liblss16_header *header, + int rgb6, enum liblss16_encode_error *error_out) +{ + int i; + + memset(encoder, 0, sizeof(*encoder)); + memcpy(&encoder->header, header, sizeof(*header)); + + if (!encoder->header.width || !encoder->header.height || encoder->header.height >> 15) { + if (error_out) + *error_out = LIBLSS16_ENCODE_BAD_IMAGE_SIZE; + return -1; + } + + if (rgb6) { + uint8_t or = 0; + for (i = 0; i < 16; i++) { + or |= encoder->header.colour_map[i].r; + or |= encoder->header.colour_map[i].g; + or |= encoder->header.colour_map[i].b; + } + if (or & 0xC0) { + if (error_out) + *error_out = LIBLSS16_ENCODE_BAD_COLOUR; + return -1; + } + } else { + struct liblss16_colour *map = encoder->header.colour_map; + for (i = 0; i < 16; i++) { + map[i].r = (uint8_t)(((unsigned)map[i].r * 63U + 127U) / 255U); + map[i].g = (uint8_t)(((unsigned)map[i].g * 63U + 127U) / 255U); + map[i].b = (uint8_t)(((unsigned)map[i].b * 63U + 127U) / 255U); + } + } + + encoder->x_rem = encoder->header.width; + encoder->y_rem = encoder->header.height; + + return 0; +} -- cgit v1.2.3-70-g09d2