aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrow Savcik <krow@savcik.xyz>2024-07-14 13:47:28 +0300
committerKrow Savcik <krow@savcik.xyz>2024-07-14 13:47:28 +0300
commit7386dd9cf283f0846a34bf8efc95d059953cad60 (patch)
tree1f2834a2a0c164a975850294cc52773d9c7a30f8
parent6e8119be3bdd4ac54cdb08667c39f4a1d3047270 (diff)
feature: new cdr format
-rw-r--r--other/format_v25
-rw-r--r--src/canvas.c63
2 files changed, 61 insertions, 7 deletions
diff --git a/other/format_v2 b/other/format_v2
new file mode 100644
index 0000000..6b129bd
--- /dev/null
+++ b/other/format_v2
@@ -0,0 +1,5 @@
+VERSION NUMBER;
+WIDTH;HEIGHT;
+LAYER_CNT;
+LAYER_NAME;...
+CANVAS DATA(4*H*W*LC bytes)
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);