forked from tTh/FloatImg
146 lines
2.8 KiB
C
146 lines
2.8 KiB
C
/*
|
|
* fimg-file.c
|
|
*
|
|
*
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#include "../floatimg.h"
|
|
|
|
extern int verbosity; /* must be declared around main() */
|
|
|
|
/* ---------------------------------------------------------------- */
|
|
int fimg_fileinfos(char *fname, int *datas)
|
|
{
|
|
FILE *fp;
|
|
FimgFileHead filehead;
|
|
|
|
#if DEBUG_LEVEL
|
|
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, datas);
|
|
#endif
|
|
|
|
fp = fopen(fname, "r");
|
|
if (NULL==fp) {
|
|
perror(fname);
|
|
return -1;
|
|
}
|
|
|
|
if (1 != fread(&filehead, sizeof(FimgFileHead), 1, fp)) {
|
|
fprintf(stderr, "%s: %s bad read\n", __func__, fname);
|
|
fclose(fp);
|
|
return -2;
|
|
}
|
|
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;
|
|
}
|
|
/* ---------------------------------------------------------------- */
|
|
int fimg_dump_to_file(FloatImg *fimg, char *fname, int notused)
|
|
{
|
|
FILE *fp;
|
|
int foo, nbre;
|
|
FimgFileHead filehead;
|
|
|
|
#if DEBUG_LEVEL
|
|
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, fimg,
|
|
fname, notused);
|
|
#endif
|
|
|
|
if (3 != fimg->type) {
|
|
fprintf(stderr, "%s : bad type %d\n", __func__, fimg->type);
|
|
return -8;
|
|
}
|
|
|
|
fp = fopen(fname, "w");
|
|
if (NULL==fp) {
|
|
perror(fname);
|
|
return -1;
|
|
}
|
|
|
|
memset(&filehead, 0, sizeof(filehead));
|
|
strcpy(filehead.magic, "FIMG");
|
|
filehead.w = fimg->width; filehead.h = fimg->height;
|
|
filehead.t = fimg->type;
|
|
|
|
foo = fwrite(&filehead, sizeof(FimgFileHead), 1, fp);
|
|
if (1 != foo) {
|
|
perror(fname);
|
|
fclose(fp);
|
|
return -2;
|
|
}
|
|
nbre = fimg->width*fimg->height*3;
|
|
#if DEBUG_LEVEL
|
|
fprintf(stderr, " %s : data at %p\n", __func__, fimg->R);
|
|
#endif
|
|
|
|
foo = fwrite(fimg->R, sizeof(float), nbre, fp);
|
|
if (nbre != foo) {
|
|
perror(fname);
|
|
fclose(fp);
|
|
return -3;
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
}
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
int fimg_create_from_dump(char *fname, FloatImg *head)
|
|
{
|
|
FILE *fp;
|
|
int foo;
|
|
FimgFileHead filehead;
|
|
|
|
#if DEBUG_LEVEL
|
|
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head);
|
|
#endif
|
|
|
|
fp = fopen(fname, "r");
|
|
if (NULL==fp) {
|
|
perror(fname);
|
|
exit(1);
|
|
}
|
|
|
|
foo = fread(&filehead, sizeof(FimgFileHead), 1, fp);
|
|
if (1 != foo) {
|
|
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__,
|
|
filehead.w, filehead.h, filehead.t, fname);
|
|
#endif
|
|
|
|
foo = fimg_create(head, filehead.w, filehead.h, filehead.t);
|
|
if (foo) {
|
|
fprintf(stderr, "%s : create -> %d\n", __func__, foo);
|
|
return foo;
|
|
}
|
|
|
|
foo = fread(head->R, sizeof(float),
|
|
filehead.w*filehead.h*filehead.t, fp);
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
}
|