adding some magic to the file format

This commit is contained in:
phyto 2019-07-08 06:28:04 +02:00
parent b8d86da9a0
commit ecf952d881
3 changed files with 55 additions and 33 deletions

View File

@ -2,7 +2,7 @@
* floatimg.h * floatimg.h
*/ */
#define FIMG_VERSION 61 #define FIMG_VERSION 62
/* /*
* in memory descriptor * in memory descriptor
@ -23,9 +23,9 @@ typedef struct {
* fimg file header * fimg file header
*/ */
typedef struct { typedef struct {
// char magic[8]; char magic[8];
int w, h, t; int w, h, t;
} FimgFhead; } FimgFileHead;
/* /*
* core module * core module

View File

@ -7,7 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "string.h" #include <string.h>
#include "../floatimg.h" #include "../floatimg.h"
@ -17,6 +17,7 @@ extern int verbosity; /* must be declared around main() */
int fimg_fileinfos(char *fname, int *datas) int fimg_fileinfos(char *fname, int *datas)
{ {
FILE *fp; FILE *fp;
FimgFileHead filehead;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, datas); fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, datas);
@ -27,29 +28,43 @@ if (NULL==fp) {
perror(fname); perror(fname);
return -1; return -1;
} }
if (3 != fread(datas, sizeof(int), 3, fp)) {
fprintf(stderr, "%s: %s short read\n", __func__, fname); if (1 != fread(&filehead, sizeof(FimgFileHead), 1, fp)) {
fprintf(stderr, "%s: %s bad read\n", __func__, fname);
fclose(fp); fclose(fp);
return -2; return -2;
} }
fclose(fp); fclose(fp);
#if DEBUG_LEVEL
fprintf(stderr, " magic [%s]\n", filehead.magic);
#endif
if (strncmp(filehead.magic, "FIMG", 4)) {
fprintf(stderr, "'%s' is not a fimg file.\n", fname);
return -3;
}
datas[0] = filehead.w;
datas[1] = filehead.h;
datas[2] = filehead.t;
return 0; return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int fimg_dump_to_file(FloatImg *head, char *fname, int notused) int fimg_dump_to_file(FloatImg *fimg, char *fname, int notused)
{ {
FILE *fp; FILE *fp;
int foo, nbre, dims[3]; int foo, nbre;
FimgFileHead filehead;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head, fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, fimg,
fname, notused); fname, notused);
#endif #endif
if (3 != head->type) { if (3 != fimg->type) {
fprintf(stderr, "%s : bat type %d\n", __func__, head->type); fprintf(stderr, "%s : bat type %d\n", __func__, fimg->type);
return -8; return -8;
} }
@ -59,21 +74,22 @@ if (NULL==fp) {
return -1; return -1;
} }
dims[0] = head->width; dims[1] = head->height; strcpy(filehead.magic, "FIMG");
dims[2] = head->type; filehead.w = fimg->width; filehead.h = fimg->height;
filehead.t = fimg->type;
foo = fwrite(dims, sizeof(int), 3, fp); foo = fwrite(&filehead, sizeof(FimgFileHead), 1, fp);
if (3 != foo) { if (1 != foo) {
perror(fname); perror(fname);
fclose(fp); fclose(fp);
return -2; return -2;
} }
nbre = head->width*head->height*3; nbre = fimg->width*fimg->height*3;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, " %s : data at %p\n", __func__, head->R); fprintf(stderr, " %s : data at %p\n", __func__, fimg->R);
#endif #endif
foo = fwrite(head->R, sizeof(float), nbre, fp); foo = fwrite(fimg->R, sizeof(float), nbre, fp);
if (nbre != foo) { if (nbre != foo) {
perror(fname); perror(fname);
fclose(fp); fclose(fp);
@ -85,10 +101,12 @@ fclose(fp);
return 0; return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int fimg_create_from_dump(char *fname, FloatImg *head) int fimg_create_from_dump(char *fname, FloatImg *head)
{ {
FILE *fp; FILE *fp;
int foo, dims[3]; int foo;
FimgFileHead filehead;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head); fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head);
@ -100,24 +118,24 @@ if (NULL==fp) {
exit(1); exit(1);
} }
foo = fread(dims, sizeof(int), 3, fp); foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
if (3 != foo) { if (1 != foo) {
fprintf(stderr, "%s : short read\n", fname); fprintf(stderr, "%s : short read\n", fname);
fclose(fp); fclose(fp);
return -1; return -1;
} }
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s : got [ %d %d %d ] from '%s'\n", __func__, fprintf(stderr, "%s : got [ %d %d %d ] from '%s'\n", __func__,
dims[0], dims[1], dims[2], fname); filehead.w, filehead.h, filehead.t, fname);
#endif #endif
foo = fimg_create(head, dims[0], dims[1], dims[2]); foo = fimg_create(head, filehead.w, filehead.h, filehead.t);
if (foo) { if (foo) {
fprintf(stderr, "%s : create -> %d\n", __func__, foo); fprintf(stderr, "%s : create -> %d\n", __func__, foo);
return foo; return foo;
} }
foo = fread(head->R, sizeof(float), dims[0]*dims[1]*3, fp); foo = fread(head->R, sizeof(float), filehead.w*filehead.h*3, fp);
fclose(fp); fclose(fp);

10
lib/t.c
View File

@ -12,16 +12,20 @@ int main(int argc, char *argv[])
{ {
int foo; int foo;
FloatImg fimg; FloatImg fimg;
int datas[3];
verbosity = 1; verbosity = 1;
foo = fimg_load_from_pnm("/tmp/00000.ppm", &fimg, 0); foo = fimg_create(&fimg, 640, 480, 3);
printf("retour du truc %d\n", foo); printf("retour du truc ---> %d\n", foo);
fimg_printhead(&fimg); fimg_printhead(&fimg);
fimg_save_as_pnm(&fimg, "foo.pnm", 0); // fimg_save_as_pnm(&fimg, "foo.pnm", 0);
foo = fimg_dump_to_file(&fimg, "foo.fimg", 0);
foo = fimg_fileinfos("foo.fimg", datas);
return 0; return 0;
} }