From 7386dd9cf283f0846a34bf8efc95d059953cad60 Mon Sep 17 00:00:00 2001 From: Krow Savcik Date: Sun, 14 Jul 2024 13:47:28 +0300 Subject: feature: new cdr format --- src/canvas.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 7 deletions(-) (limited to 'src/canvas.c') diff --git a/src/canvas.c b/src/canvas.c index 927bde1..9807c26 100644 --- a/src/canvas.c +++ b/src/canvas.c @@ -27,6 +27,8 @@ static void canvas_set_proj_path(Canvas *, const char *); static void action_do(Canvas *); static void action_remove(Action *); static void action_pixcols_redo(Canvas *, Action *); +static Canvas *canvas_open1(FILE *, void *); +static Canvas *canvas_open2(FILE *, void *); static uint8 canvas_coord_get(Canvas *c, long int tx, long int ty, long int *x, long int *y) @@ -135,21 +137,62 @@ canvas_init(uint w, uint h, void *ren) Canvas * canvas_open(const char *path, void *ren) { - int ver, w, h; - Canvas *c; + unsigned int ver; + Canvas *c = NULL; FILE *fp = fopen(path, "r"); if (fp == NULL) return NULL; - fscanf(fp, "%d;%d;%d;", &ver, &w, &h); - c = canvas_init(w, h, ren); - fread(c->layers[0]->pix, sizeof *(c->layers[0]->pix), c->w*c->h, fp); + fscanf(fp, "%u;", &ver); + switch (ver) { + case 1: c = canvas_open1(fp, ren); break; + case 2: c = canvas_open2(fp, ren); break; + default: + fprintf(stderr, "Error opening file. Unkown version %u.", ver); + break; + } + fclose(fp); + if (c == NULL) return NULL; canvas_refresh(c); canvas_set_proj_path(c, path); return c; } +static Canvas * +canvas_open1(FILE *fp, void *ren) +{ + unsigned int w, h; + Canvas *c; + fscanf(fp, "%u;%u;", &w, &h); + c = canvas_init(w, h, ren); + fread(c->layers[0]->pix, sizeof *(c->layers[0]->pix), c->w*c->h, fp); + + return c; +} + +static Canvas * +canvas_open2(FILE *fp, void *ren) +{ + unsigned int w, h, lc, i; + Canvas *c; + fscanf(fp, "%u;%u;%u;", &w, &h, &lc); + puts("READING DATA"); + c = canvas_init(w, h, ren); + for (i = 1; i < lc; i++) + canvas_add_layer(c, lc); + for (i = 0; i < lc; i++) { + fread(c->layers[i]->name, sizeof *(c->layers[i]->name), LAYERNAMELEN, fp); + c->layers[i]->name[LAYERNAMELEN] = '\0'; + fscanf(fp, ";"); + } + + for (i = 0; i < lc; i++) + fread(c->layers[i]->pix, sizeof *(c->layers[i]->pix), c->w*c->h, fp); + + return c; +} + Canvas * canvas_import_png(const char *path, void *ren) { @@ -378,14 +421,20 @@ canvas_move_y(Canvas *c, long int delta) uint8 canvas_save(Canvas *c, const char *path, short int s) { + unsigned int i; FILE *fp = fopen(path, "w"); if (fp == NULL) { /* TODO: Error handling */ return 1; } - fprintf(fp, "1;%d;%d;", c->w, c->h); - int u = fwrite(c->layers[0]->pix, sizeof *(c->layers[0]->pix), c->w*c->h, fp); + fprintf(fp, "2;%u;%u;%u;", c->w, c->h, c->layer_arr_cnt); + for (i = 0; i < c->layer_arr_cnt; i++) { + fwrite(c->layers[i]->name, sizeof *(c->layers[i]->name), LAYERNAMELEN, fp); + fprintf(fp, ";"); + } + for (i = 0; i < c->layer_arr_cnt; i++) + fwrite(c->layers[i]->pix, sizeof *(c->layers[i]->pix), c->w*c->h, fp); fclose(fp); if (s) canvas_set_proj_path(c, path); -- cgit v1.2.3