emote2ss

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

fileops.c (1262B)


      1 #include "webp/decode.h"
      2 #include "webp/encode.h"
      3 #include "webp/demux.h"
      4 #include "fileops.h"
      5 #include "spritesheet.h"
      6 
      7 int FileRead(const char *filename, const uint8_t **data, size_t *size) {
      8 	assert(data != NULL);
      9 	assert(size != NULL);
     10 
     11 	*data = NULL;
     12 	*size = 0;
     13 	FILE *infile = fopen(filename, "rb");
     14 	assert(infile != NULL);
     15 	fseek(infile, 0, SEEK_END);
     16 	size_t fsize = ftell(infile);
     17 	// printf("%s: %zu bytes\n", filename, fsize);
     18 	fseek(infile, 0, SEEK_SET);
     19 
     20 	uint8_t *fdata = malloc(fsize+1);
     21 	assert(fdata != NULL);
     22 	fdata[fsize] = '\0';
     23 	int ok = (fread(fdata, fsize, 1, infile)==1);
     24 	fclose(infile);
     25 
     26 	if (!ok) {
     27 		fprintf(stderr, "cannot read file %s (%d)\n", filename, ok);
     28 		free(fdata);
     29 		return -1;
     30 	}
     31 	*data = fdata;
     32 	*size = fsize;
     33 	return 0;
     34 }
     35 
     36 void WebpWrite(const char *path, Animation *img, int cols) {
     37 	Spritesheet ss = SpritesheetGen(img, cols);
     38 	uint8_t *out;
     39 	FILE *fp = fopen(path, "wb");
     40 	assert(fp!=NULL);
     41 	size_t encoded = WebPEncodeLosslessRGBA(ss.data, ss.width, ss.height, ss.stride, &out);
     42 	// printf("size: %zu, encoded: %zu\n", img->width*img->height*sizeof(uint32_t), encoded);
     43 	assert(encoded!=0);
     44 	size_t written = fwrite(out, sizeof(uint8_t), encoded, fp);
     45 	assert(written==encoded);
     46 	WebPFree(out);
     47 	free(ss.data);
     48 }