2019-03-03 16:22:55 +01:00
|
|
|
/*
|
|
|
|
* fimg-file.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-05-20 09:31:28 +02:00
|
|
|
#include <stdint.h>
|
2019-03-03 16:22:55 +01:00
|
|
|
#include <unistd.h>
|
2019-07-08 06:28:04 +02:00
|
|
|
#include <string.h>
|
2019-12-18 14:39:47 +01:00
|
|
|
#include <errno.h>
|
2019-03-03 16:22:55 +01:00
|
|
|
#include "../floatimg.h"
|
|
|
|
|
|
|
|
extern int verbosity; /* must be declared around main() */
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
int fimg_fileinfos(char *fname, int *datas)
|
|
|
|
{
|
2019-07-08 06:28:04 +02:00
|
|
|
FILE *fp;
|
|
|
|
FimgFileHead filehead;
|
2019-03-03 16:22:55 +01:00
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, datas);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fp = fopen(fname, "r");
|
|
|
|
if (NULL==fp) {
|
|
|
|
perror(fname);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-07-08 06:28:04 +02:00
|
|
|
|
|
|
|
if (1 != fread(&filehead, sizeof(FimgFileHead), 1, fp)) {
|
|
|
|
fprintf(stderr, "%s: %s bad read\n", __func__, fname);
|
2019-03-03 16:22:55 +01:00
|
|
|
fclose(fp);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
2019-07-08 06:28:04 +02:00
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, " magic [%s]\n", filehead.magic);
|
|
|
|
#endif
|
|
|
|
|
2021-03-17 18:32:51 +01:00
|
|
|
if (memcmp(filehead.magic, "FIMG", 4)) {
|
2019-07-08 06:28:04 +02:00
|
|
|
fprintf(stderr, "'%s' is not a fimg file.\n", fname);
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2022-03-19 01:21:58 +01:00
|
|
|
/* XXX preparer la gestion des metadata */
|
|
|
|
if ('a' == filehead.magic[4]) {
|
2022-05-14 20:49:59 +02:00
|
|
|
fprintf(stderr,"\n\t****** %s have metadata\n\n", fname);
|
2022-03-19 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 06:28:04 +02:00
|
|
|
datas[0] = filehead.w;
|
|
|
|
datas[1] = filehead.h;
|
|
|
|
datas[2] = filehead.t;
|
|
|
|
|
2019-03-03 16:22:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|
2021-03-09 11:55:48 +01:00
|
|
|
/*
|
2022-04-09 23:18:14 +02:00
|
|
|
* new avril 2022 : trying to save metadatas...
|
|
|
|
*/
|
|
|
|
int fimg_dumpmd_to_file(FloatImg *fimg, char *fname, \
|
|
|
|
FimgMetaData *pmd, int notused)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int foo, nbre;
|
|
|
|
FimgFileHead filehead;
|
2022-05-14 20:49:59 +02:00
|
|
|
FimgMetaData fakemd;
|
2022-04-09 23:18:14 +02:00
|
|
|
|
2022-04-17 04:19:06 +02:00
|
|
|
#if DEBUG_LEVEL
|
2022-04-09 23:18:14 +02:00
|
|
|
fprintf(stderr, ">>> %s ( %p '%s' %p %d )\n", __func__, fimg,
|
|
|
|
fname, pmd, notused);
|
|
|
|
#endif
|
|
|
|
|
2022-04-17 04:19:06 +02:00
|
|
|
if (notused) {
|
|
|
|
fprintf(stderr, "%s: notused must be 0, was %d\n", \
|
|
|
|
__func__, notused);
|
|
|
|
}
|
|
|
|
|
2022-04-09 23:18:14 +02:00
|
|
|
if (FIMG_TYPE_RGB != fimg->type) {
|
|
|
|
fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type);
|
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
|
2022-05-14 20:49:59 +02:00
|
|
|
/* OK, we have to make a fake metadata chunk, if the caller
|
|
|
|
* don't have one. Ugly, and nice */
|
|
|
|
if (NULL == pmd) {
|
|
|
|
fprintf(stderr, ">>> %s making faked metadata\n", __func__);
|
|
|
|
foo = fimg_default_metadata(&fakemd, 3);
|
|
|
|
fprintf(stderr, "fakemd is at %p\n", &fakemd);
|
|
|
|
pmd = &fakemd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* OK, get some funky metadatas */
|
2022-07-06 10:27:55 +02:00
|
|
|
// fprintf(stderr, "%s get metadatas\n", __func__);
|
|
|
|
fprintf(stderr, "acqu fval=%f count=%d\n", pmd->fval, pmd->count);
|
2022-05-14 20:49:59 +02:00
|
|
|
}
|
|
|
|
|
2022-04-09 23:18:14 +02:00
|
|
|
fp = fopen(fname, "w");
|
|
|
|
if (NULL==fp) {
|
|
|
|
perror(fname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&filehead, 0, sizeof(filehead));
|
|
|
|
memcpy(filehead.magic, "FIMG", 4);
|
|
|
|
filehead.w = fimg->width; filehead.h = fimg->height;
|
|
|
|
filehead.t = fimg->type;
|
|
|
|
|
|
|
|
/* XXX metadata */
|
|
|
|
if (NULL != pmd) {
|
2022-07-06 10:27:55 +02:00
|
|
|
// fprintf(stderr, "ok, %s give an 'a' flag\n", __func__);
|
2022-04-09 23:18:14 +02:00
|
|
|
filehead.magic[4] = 'a';
|
|
|
|
}
|
|
|
|
|
|
|
|
foo = fwrite(&filehead, sizeof(FimgFileHead), 1, fp);
|
|
|
|
if (1 != foo) {
|
|
|
|
perror(fname);
|
|
|
|
fclose(fp);
|
|
|
|
/* may be here, we can remove the broken file ? */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2022-05-14 20:49:59 +02:00
|
|
|
/* we have metadata, put them on tape */
|
2022-04-09 23:18:14 +02:00
|
|
|
if (NULL != pmd) {
|
2022-07-06 10:27:55 +02:00
|
|
|
// fprintf(stderr, ">>> %s write metadata %p\n", __func__, pmd);
|
2022-04-09 23:18:14 +02:00
|
|
|
foo = fwrite(pmd, sizeof(FimgMetaData), 1, fp);
|
|
|
|
if (1 != foo) {
|
|
|
|
perror(fname);
|
|
|
|
fclose(fp);
|
|
|
|
/* may be here, we can remove the broken file ? */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nbre = fimg->width * fimg->height; /* pixels per frame */
|
|
|
|
foo = fwrite(fimg->R, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
|
|
|
perror(fname); fclose(fp); return -3;
|
|
|
|
}
|
|
|
|
foo = fwrite(fimg->G, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
|
|
|
perror(fname); fclose(fp); return -3;
|
|
|
|
}
|
|
|
|
foo = fwrite(fimg->B, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
|
|
|
perror(fname); fclose(fp); return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
* /!\ this func work ONLY on RGB image
|
2021-03-09 11:55:48 +01:00
|
|
|
*/
|
2019-07-08 06:28:04 +02:00
|
|
|
int fimg_dump_to_file(FloatImg *fimg, char *fname, int notused)
|
2019-03-03 16:22:55 +01:00
|
|
|
{
|
2019-07-08 06:28:04 +02:00
|
|
|
FILE *fp;
|
|
|
|
int foo, nbre;
|
|
|
|
FimgFileHead filehead;
|
2019-03-03 16:22:55 +01:00
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
2019-07-08 06:28:04 +02:00
|
|
|
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, fimg,
|
2019-03-03 16:22:55 +01:00
|
|
|
fname, notused);
|
|
|
|
#endif
|
|
|
|
|
2022-04-17 04:19:06 +02:00
|
|
|
if (notused) {
|
|
|
|
fprintf(stderr, "%s: notused must be 0, was %d\n", \
|
|
|
|
__func__, notused);
|
|
|
|
}
|
|
|
|
|
2022-04-09 23:18:14 +02:00
|
|
|
if (FIMG_TYPE_RGB != fimg->type) {
|
2019-08-08 17:16:20 +02:00
|
|
|
fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type);
|
2019-03-03 16:22:55 +01:00
|
|
|
return -8;
|
|
|
|
}
|
|
|
|
|
2023-06-29 14:29:14 +02:00
|
|
|
// fprintf(stderr, "'%s' try to open '%s'\n", __func__, fname);
|
2019-03-03 16:22:55 +01:00
|
|
|
fp = fopen(fname, "w");
|
2023-06-29 14:29:14 +02:00
|
|
|
// fprintf(stderr, " fp at %p\n", fp);
|
|
|
|
|
|
|
|
if (NULL == fp) {
|
2019-03-03 16:22:55 +01:00
|
|
|
perror(fname);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-06-29 14:29:14 +02:00
|
|
|
else {
|
|
|
|
// fprintf(stderr, "file '%s' opened\n", fname);
|
|
|
|
}
|
2019-03-03 16:22:55 +01:00
|
|
|
|
2019-08-02 01:50:12 +02:00
|
|
|
memset(&filehead, 0, sizeof(filehead));
|
2021-03-09 11:55:48 +01:00
|
|
|
memcpy(filehead.magic, "FIMG", 4);
|
2019-07-08 06:28:04 +02:00
|
|
|
filehead.w = fimg->width; filehead.h = fimg->height;
|
|
|
|
filehead.t = fimg->type;
|
2019-03-03 16:22:55 +01:00
|
|
|
|
2019-07-08 06:28:04 +02:00
|
|
|
foo = fwrite(&filehead, sizeof(FimgFileHead), 1, fp);
|
|
|
|
if (1 != foo) {
|
2019-03-03 16:22:55 +01:00
|
|
|
perror(fname);
|
|
|
|
fclose(fp);
|
|
|
|
return -2;
|
|
|
|
}
|
2023-06-29 14:29:14 +02:00
|
|
|
// fprintf(stderr, "'%s' write header --> %d\n", __func__, foo);
|
|
|
|
|
|
|
|
|
2021-03-09 11:55:48 +01:00
|
|
|
nbre = fimg->width * fimg->height; /* pixels per frame */
|
2019-03-03 16:22:55 +01:00
|
|
|
|
2019-07-08 06:28:04 +02:00
|
|
|
foo = fwrite(fimg->R, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
2021-03-09 11:55:48 +01:00
|
|
|
perror(fname); fclose(fp); return -3;
|
|
|
|
}
|
|
|
|
foo = fwrite(fimg->G, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
|
|
|
perror(fname); fclose(fp); return -3;
|
|
|
|
}
|
|
|
|
foo = fwrite(fimg->B, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
|
|
|
perror(fname); fclose(fp); return -3;
|
2019-03-03 16:22:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2019-12-18 14:39:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|
2019-12-21 14:42:55 +01:00
|
|
|
/*
|
|
|
|
* load a dump in a pre-allocated FloatImg
|
|
|
|
*/
|
2019-12-18 14:39:47 +01:00
|
|
|
int fimg_load_from_dump(char *fname, FloatImg *where)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int foo, nbre;
|
|
|
|
FimgFileHead filehead;
|
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
2020-02-16 23:50:44 +01:00
|
|
|
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, where);
|
2019-12-18 14:39:47 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NULL==(fp = fopen(fname, "r"))) {
|
2019-12-21 14:42:55 +01:00
|
|
|
perror(fname);
|
|
|
|
return -15;
|
2019-12-18 14:39:47 +01:00
|
|
|
}
|
|
|
|
foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
|
|
|
|
if (1 != foo) {
|
2019-12-21 12:35:52 +01:00
|
|
|
fprintf(stderr, "%s: short read on '%s'\n", __func__, fname);
|
2019-12-18 14:39:47 +01:00
|
|
|
fclose(fp);
|
2019-12-21 14:42:55 +01:00
|
|
|
return -16;
|
2019-12-18 14:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check compatibility */
|
|
|
|
if ( (filehead.w != where->width) ||
|
|
|
|
(filehead.h != where->height) ||
|
|
|
|
(filehead.t != where->type) ) {
|
2021-03-09 11:55:48 +01:00
|
|
|
fprintf(stderr, "%s: file '%s' incompatible\n",
|
|
|
|
__func__, fname);
|
|
|
|
fclose(fp);
|
|
|
|
return -17;
|
|
|
|
}
|
2019-12-18 14:39:47 +01:00
|
|
|
|
2022-03-19 01:21:58 +01:00
|
|
|
/* XXX preparer la gestion des metadata */
|
|
|
|
if ('a' == filehead.magic[4]) {
|
2023-02-11 18:04:38 +01:00
|
|
|
if (verbosity > 1) fprintf(stderr,"****** %s have metadata\n", fname);
|
2022-05-14 20:49:59 +02:00
|
|
|
foo = fseek(fp, (long)sizeof(FimgMetaData), SEEK_CUR);
|
|
|
|
if (foo) {
|
|
|
|
fprintf(stderr, "%s: seek error\n", __func__);
|
|
|
|
perror(fname);
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-03-19 01:21:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 11:55:48 +01:00
|
|
|
nbre = filehead.w * filehead.h; /* number of pixels per frame */
|
2019-12-18 14:39:47 +01:00
|
|
|
foo = fread(where->R, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
2019-12-21 14:42:55 +01:00
|
|
|
fprintf(stderr, "%s: err read '%s' : %d\n", __func__, fname, foo);
|
2021-03-09 11:55:48 +01:00
|
|
|
fclose(fp); return -18;
|
|
|
|
}
|
|
|
|
foo = fread(where->G, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
|
|
|
fprintf(stderr, "%s: err read '%s' : %d\n", __func__, fname, foo);
|
|
|
|
fclose(fp); return -18;
|
|
|
|
}
|
|
|
|
foo = fread(where->B, sizeof(float), nbre, fp);
|
|
|
|
if (nbre != foo) {
|
|
|
|
fprintf(stderr, "%s: err read '%s' : %d\n", __func__, fname, foo);
|
|
|
|
fclose(fp); return -18;
|
2019-12-18 14:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2019-03-03 16:22:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- */
|
2019-07-08 06:28:04 +02:00
|
|
|
|
2019-03-03 16:22:55 +01:00
|
|
|
int fimg_create_from_dump(char *fname, FloatImg *head)
|
|
|
|
{
|
2019-07-08 06:28:04 +02:00
|
|
|
FILE *fp;
|
2021-03-09 11:55:48 +01:00
|
|
|
int foo, size;
|
2021-03-17 18:32:51 +01:00
|
|
|
long nbread;
|
2019-07-08 06:28:04 +02:00
|
|
|
FimgFileHead filehead;
|
2019-03-03 16:22:55 +01:00
|
|
|
|
|
|
|
#if DEBUG_LEVEL
|
|
|
|
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head);
|
|
|
|
#endif
|
|
|
|
|
2020-02-20 02:31:06 +01:00
|
|
|
/*
|
|
|
|
* may be we can crash coredump here if the head
|
2022-07-06 10:27:55 +02:00
|
|
|
* descriptor from caller is not blank ?
|
2020-02-20 02:31:06 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-03 16:22:55 +01:00
|
|
|
fp = fopen(fname, "r");
|
|
|
|
if (NULL==fp) {
|
|
|
|
perror(fname);
|
2020-01-10 12:01:12 +01:00
|
|
|
return -15;
|
2019-03-03 16:22:55 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 06:28:04 +02:00
|
|
|
foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
|
|
|
|
if (1 != foo) {
|
2021-03-09 11:55:48 +01:00
|
|
|
fprintf(stderr, "%s: short read on '%s'\n", __func__, fname);
|
2019-03-03 16:22:55 +01:00
|
|
|
fclose(fp);
|
2020-01-10 12:01:12 +01:00
|
|
|
return -16;
|
2019-03-03 16:22:55 +01:00
|
|
|
}
|
|
|
|
#if DEBUG_LEVEL
|
2021-03-09 11:55:48 +01:00
|
|
|
fprintf(stderr, "%s : got [ %dx%d %s ] from '%s'\n", __func__,
|
|
|
|
filehead.w, filehead.h, fimg_str_type(filehead.t),
|
|
|
|
fname);
|
2019-03-03 16:22:55 +01:00
|
|
|
#endif
|
|
|
|
|
2019-07-08 06:28:04 +02:00
|
|
|
foo = fimg_create(head, filehead.w, filehead.h, filehead.t);
|
2019-03-03 16:22:55 +01:00
|
|
|
if (foo) {
|
|
|
|
fprintf(stderr, "%s : create -> %d\n", __func__, foo);
|
|
|
|
return foo;
|
|
|
|
}
|
|
|
|
|
2022-05-14 20:49:59 +02:00
|
|
|
/* bugfix Sat 14 May 2022 07:02:53 PM CEST */
|
|
|
|
/*
|
|
|
|
* Oh boy, we have to skeep the metadata chunck
|
|
|
|
*/
|
2023-02-11 18:04:38 +01:00
|
|
|
if ('a' == filehead.magic[4]) {
|
|
|
|
if (verbosity > 1)
|
2022-07-06 10:27:55 +02:00
|
|
|
{ fprintf(stderr, "%s: %s has metadata\n", __func__, fname); }
|
2022-05-14 20:49:59 +02:00
|
|
|
foo = fseek(fp, (long)sizeof(FimgMetaData), SEEK_CUR);
|
|
|
|
if (foo) {
|
|
|
|
fprintf(stderr, "%s : shit hit the fan\n", __func__);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 11:55:48 +01:00
|
|
|
size = filehead.w * filehead.h;
|
2021-03-17 18:32:51 +01:00
|
|
|
nbread = 0;
|
|
|
|
nbread += fread(head->R, sizeof(float), size, fp);
|
|
|
|
nbread += fread(head->G, sizeof(float), size, fp);
|
|
|
|
nbread += fread(head->B, sizeof(float), size, fp);
|
2021-02-03 19:33:38 +01:00
|
|
|
|
2019-03-03 16:22:55 +01:00
|
|
|
fclose(fp);
|
2019-08-02 01:50:12 +02:00
|
|
|
|
2019-03-03 16:22:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|