emote2ss

Animated webp to spritesheets converting tool
git clone git://bsandro.tech/emote2ss
Log | Files | Refs

commit e12a80e2dae2f4908a5bece6133a14041e9d3a77
parent c5cfc4dafcd131df9c4bbd70bcb134a70ce43b89
Author: bsandro <email@bsandro.tech>
Date:   Sat,  8 Jul 2023 23:23:15 +0300

merge transitional -> master

Diffstat:
MMakefile | 2+-
Msrc/main.c | 40++++++++++++++++++++++++----------------
2 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,7 +2,7 @@ NAME=$(shell basename ${PWD}) SRC=$(wildcard *.c src/*.c) DEPS:=$(wildcard *.h src/*.h) OBJ:=$(SRC:.c=.o) -LIBS:=libwebp libwebpdecoder libwebpdemux +LIBS:=libwebp libwebpdemux CFLAGS=-Og -g -std=c99 -Werror -Wall -Wextra -I. -I./src/ ${shell pkg-config --cflags $(LIBS)} LDFLAGS=-lc ${shell pkg-config --libs $(LIBS)} diff --git a/src/main.c b/src/main.c @@ -45,7 +45,7 @@ void alloc_image(AnimatedImage *img, uint32_t frame_count) { assert(img_size == (size_t)img_size); assert(frames_size == (size_t)frames_size); - printf("img mem: %lu\nframes mem: %lu\n", img_size, frames_size); + printf("img mem: %" PRIu64 "\nframes mem: %" PRIu64 "\n", img_size, frames_size); mem = malloc(img_size); frames = malloc(frames_size); @@ -149,30 +149,40 @@ int read_webp(const char *fname, AnimatedImage *anim) { return 0; } -void write_webp(const char *fname, AnimatedImage *img) { +void write_webp(const char *fname, AnimatedImage *img, int cols, int rows) { + // validate that we have enough space in resulting picture for all the frames + assert(rows*cols >= (int)img->frame_count); FILE *fp = fopen(fname, "wb"); assert(fp!=NULL); uint8_t *out; size_t frame_size = img->width * img->height * sizeof(uint32_t); - size_t frames_size = frame_size * img->frame_count; + size_t img_size = frame_size * img->frame_count; size_t line_size = img->width * sizeof(uint32_t); - size_t full_line = line_size * img->frame_count; - uint8_t *merged = malloc(frames_size); + size_t full_line = line_size * cols; + uint8_t *merged = malloc(frame_size * rows * cols); assert(merged!=NULL); - bzero(merged, frames_size); - for (int y = 0; y < img->height; ++y) { - for (uint32_t i = 0; i < img->frame_count; ++i) { - memcpy(merged+y*full_line+i*line_size, img->frames[i].rgba+line_size*y, line_size); + bzero(merged, img_size); + uint8_t *merged_orig = merged; + for (int row = 0; row < rows; ++row) { + for (int y = 0; y < img->height; ++y) { + for (int col = 0; col < cols; ++col) { + uint32_t offset = row*cols+col; + if (offset < img->frame_count) { + memcpy(merged, img->frames[offset].rgba+y*line_size, line_size); + } + merged += line_size; + } } } - int stride = img->width * sizeof(uint32_t) * img->frame_count; - size_t encoded = WebPEncodeLosslessRGBA(merged, img->width * img->frame_count, img->height, stride, &out); - printf("size: %lu, encoded: %lu\n", img->width*img->height*sizeof(uint32_t), encoded); + int stride = full_line; + printf("stride: %d\n", stride); + size_t encoded = WebPEncodeLosslessRGBA(merged_orig, img->width * cols, img->height * rows, stride, &out); + printf("size: %" PRIu32 ", encoded: %zu\n", img->width*img->height*sizeof(uint32_t), encoded); assert(encoded!=0); size_t written = fwrite(out, sizeof(uint8_t), encoded, fp); assert(written==encoded); WebPFree(out); - free(merged); + free(merged_orig); fclose(fp); } @@ -184,9 +194,7 @@ int main(int argc, const char **argv) { AnimatedImage img = {0}; assert(read_webp(argv[i], &img) == 0); printf("dimensions: %dx%d\nframes: %d\n", img.width, img.height, img.frame_count); - - // dump 1st frame to test stuff - write_webp("test01.webp", &img); + write_webp("test01.webp", &img, 5, 4); } }