FloatImg4PythonBinding/lib/fimg-file.c

189 lines
3.8 KiB
C
Raw Normal View History

2019-03-03 16:22:55 +01:00
/*
* fimg-file.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#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
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;
2019-03-03 16:22:55 +01:00
return 0;
}
/* ---------------------------------------------------------------- */
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
2019-07-08 06:28:04 +02:00
if (3 != 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;
}
fp = fopen(fname, "w");
if (NULL==fp) {
perror(fname);
return -1;
}
2019-08-02 01:50:12 +02:00
memset(&filehead, 0, sizeof(filehead));
2019-07-08 06:28:04 +02:00
strcpy(filehead.magic, "FIMG");
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;
}
2019-07-08 06:28:04 +02:00
nbre = fimg->width*fimg->height*3;
2019-03-03 16:22:55 +01:00
#if DEBUG_LEVEL
2019-07-08 06:28:04 +02:00
fprintf(stderr, " %s : data at %p\n", __func__, fimg->R);
2019-03-03 16:22:55 +01:00
#endif
2019-07-08 06:28:04 +02:00
foo = fwrite(fimg->R, sizeof(float), nbre, fp);
if (nbre != foo) {
2019-03-03 16:22:55 +01:00
perror(fname);
fclose(fp);
return -3;
}
fclose(fp);
2019-12-18 14:39:47 +01:00
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_load_from_dump(char *fname, FloatImg *where)
{
FILE *fp;
int foo, nbre;
FimgFileHead filehead;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head);
#endif
if (NULL==(fp = fopen(fname, "r"))) {
perror(fname); exit(1);
}
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);
return -1;
}
/* check compatibility */
if ( (filehead.w != where->width) ||
(filehead.h != where->height) ||
(filehead.t != where->type) ) {
2019-12-21 12:35:52 +01:00
fprintf(stderr, "%s: file '%s' incompatible\n", __func__, fname);
fclose(fp);
return -17;
2019-12-18 14:39:47 +01:00
}
nbre = filehead.w*filehead.h*filehead.t; /* ugly quirk */
foo = fread(where->R, sizeof(float), nbre, fp);
if (nbre != foo) {
2019-12-21 12:35:52 +01:00
fprintf(stderr, "%s: err read '%s' : %d\n", __func__, fname, errno);
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;
int foo;
FimgFileHead filehead;
2019-03-03 16:22:55 +01:00
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head);
#endif
fp = fopen(fname, "r");
if (NULL==fp) {
perror(fname);
exit(1);
}
2019-07-08 06:28:04 +02:00
foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
if (1 != foo) {
2019-03-03 16:22:55 +01:00
fprintf(stderr, "%s : short read\n", fname);
fclose(fp);
return -1;
}
#if DEBUG_LEVEL
fprintf(stderr, "%s : got [ %d %d %d ] from '%s'\n", __func__,
2019-07-08 06:28:04 +02:00
filehead.w, filehead.h, 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;
}
2019-08-07 17:30:16 +02:00
foo = fread(head->R, sizeof(float),
filehead.w*filehead.h*filehead.t, fp);
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;
}