aboutsummaryrefslogtreecommitdiffstats
path: root/liblss16_encoder_init.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2025-03-02 22:18:45 +0100
committerMattias Andrée <m@maandree.se>2025-03-02 22:18:45 +0100
commitfd54698d12d468420d344f0ff4d5e502e04d64d4 (patch)
tree62fbc4488e3374b27dddffe00de665894e7eec41 /liblss16_encoder_init.c
parentFirst commit (diff)
downloadliblss16-fd54698d12d468420d344f0ff4d5e502e04d64d4.tar.gz
liblss16-fd54698d12d468420d344f0ff4d5e502e04d64d4.tar.bz2
liblss16-fd54698d12d468420d344f0ff4d5e502e04d64d4.tar.xz
Add some functions needed fore encoding LSS16 files
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'liblss16_encoder_init.c')
-rw-r--r--liblss16_encoder_init.c46
1 files changed, 46 insertions, 0 deletions
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 <string.h>
+
+
+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;
+}