FloatImg4PythonBinding/lib/fimg-file.c

126 lines
2.3 KiB
C
Raw Normal View History

2019-03-04 02:22:55 +11:00
/*
* 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;
#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 (3 != fread(datas, sizeof(int), 3, fp)) {
fprintf(stderr, "%s: %s short read\n", __func__, fname);
fclose(fp);
return -2;
}
fclose(fp);
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_dump_to_file(FloatImg *head, char *fname, int notused)
{
FILE *fp;
int foo, nbre, dims[3];
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head,
fname, notused);
#endif
if (3 != head->type) {
fprintf(stderr, "%s : bat type %d\n", __func__, head->type);
return -8;
}
fp = fopen(fname, "w");
if (NULL==fp) {
perror(fname);
return -1;
}
dims[0] = head->width; dims[1] = head->height;
dims[2] = head->type;
foo = fwrite(dims, sizeof(int), 3, fp);
if (3 != foo) {
perror(fname);
fclose(fp);
return -2;
}
nbre = head->width*head->height*3;
#if DEBUG_LEVEL
fprintf(stderr, " %s : data at %p\n", __func__, head->R);
#endif
foo = fwrite(head->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, dims[3];
#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(dims, sizeof(int), 3, fp);
if (3 != 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__,
dims[0], dims[1], dims[2], fname);
#endif
foo = fimg_create(head, dims[0], dims[1], dims[2]);
if (foo) {
fprintf(stderr, "%s : create -> %d\n", __func__, foo);
return foo;
}
foo = fread(head->R, sizeof(float), dims[0]*dims[1]*3, fp);
fclose(fp);
return 0;
}