-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.c
182 lines (155 loc) · 5.17 KB
/
fileio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* fileio.c
*
* HISTORY:
* quick and dirty hack Linas Vepstas October 1989
* more hacks ever since -- Linas
*/
#include <endian.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fileio.h"
extern FILE *Fopen();
extern FILE *Fopenr();
/*-------------------------------------------------------------------*/
// My version of fgets, because fgets is sometimes broken on other machines.
// I don't remember what the problem was.
static char *my_fgets(char *s, int size, FILE *stream)
{
for (int i=0; i< size; i++)
{
int ch = fgetc(stream);
s[i] = ch;
if ('\n' == ch) break;
if (0 == ch) break;
}
return s;
}
/*-------------------------------------------------------------------*/
// Read a *.flo greyscale floating-point pixmap.
// The file format is width, height in ascii, then newline
// then floating point data in machine-native byte order.
// One float per pixel, a total of width*height floats.
float* read_flo_file(const char *fname,
unsigned int* data_width, unsigned int* data_height)
{
/* open input file */
FILE *fh = Fopenr (fname, ".flo");
if (NULL == fh) return NULL;
#define CLEN 80
char str[CLEN];
my_fgets(str, CLEN, fh);
sscanf(str, "%d %d", data_width, data_height);
fprintf(stderr, "Input file %s has width %d height %d \n",
fname, *data_width, *data_height);
size_t datalen = (*data_width) * (*data_height);
float* data_in = (float *) malloc (datalen * sizeof(float));
/* read floating point data */
fread(data_in, sizeof(float), datalen, fh);
fclose(fh);
return data_in;
}
/*-------------------------------------------------------------------*/
// Read a *.pfm greyscale floating-point pixmap.
// This reads the PFM file format as described in various places.
float* read_pfm_file(const char *fname,
unsigned int* data_width, unsigned int* data_height)
{
/* open input file */
FILE *fh = Fopenr (fname, ".pfm");
if (NULL == fh) return NULL;
#define PFLEN 80
char str[PFLEN];
my_fgets(str, PFLEN, fh);
// Handle only greyscale
if ('P' != str[0] || 'f' != str[1]) return NULL;
// Read the dimensions
my_fgets(str, PFLEN, fh);
sscanf(str, "%d %d", data_width, data_height);
fprintf(stderr, "Input file %s has width %d height %d \n",
fname, *data_width, *data_height);
// Read the "scale factor" and ignore it.
my_fgets(str, PFLEN, fh);
// double scale = atof(str);
size_t datalen = (*data_width) * (*data_height);
float* data_in = (float *) malloc (datalen * sizeof(float));
/* read floating point data */
fread(data_in, sizeof(float), datalen, fh);
fclose(fh);
return data_in;
}
/*-------------------------------------------------------------------*/
float* read_floats(const char *fname, const char** suff,
unsigned int* data_width, unsigned int* data_height)
{
/* open input file */
*suff = ".flo";
float* data_in = read_flo_file(fname, data_width, data_height);
if (NULL == data_in)
{
*suff = ".pfm";
data_in = read_pfm_file(fname, data_width, data_height);
}
if (NULL == data_in)
{
*suff = "";
fprintf (stderr, "Can't open input file %s\n", fname);
return NULL;
}
return data_in;
}
/*-------------------------------------------------------------------*/
// Write a *.flo greyscale floating-point pixmap.
// The file format is width, height in ascii, then newline
// then floating point data in machine-native byte order.
// One float per pixel, a total of width*height floats.
void write_flo_file(const char *fname, float* data,
unsigned int data_width, unsigned int data_height)
{
FILE *fh = Fopen(fname, ".flo");
if (NULL == fh)
{
fprintf (stderr, "Can't open output file %s.flo\n", fname);
return;
}
/* dump the size to output */
fprintf (fh, "%d %d\n", data_width, data_height);
fwrite (data, sizeof(float), data_width*data_height, fh);
fclose (fh);
}
/*-------------------------------------------------------------------*/
// Write a *.pfm greyscale floating-point pixmap.
// The file format is width, height in ascii, then newline
// then floating point data in machine-native byte order.
// One float per pixel, a total of width*height floats.
void write_pfm_file(const char *fname, float* data,
unsigned int data_width, unsigned int data_height)
{
FILE *fh = Fopen(fname, ".pfm");
if (NULL == fh)
{
fprintf (stderr, "Can't open output file %s.pfm\n", fname);
return;
}
/* dump the size to output */
fprintf (fh, "Pf\n");
fprintf (fh, "%d %d\n", data_width, data_height);
double scale = 1.0;
// if (htonl(47) != 47) scale = -1.0; // Little-endian
if (htobe16(47) != 47) scale = -1.0; // Little-endian
fprintf (fh, "%f\n", scale);
/* And now the data. */
fwrite (data, sizeof(float), data_width*data_height, fh);
fclose (fh);
}
/*-------------------------------------------------------------------*/
void write_floats(const char *fname, const char* suff, float* data,
unsigned int data_width, unsigned int data_height)
{
if (0 == strcmp(suff, ".flo"))
write_flo_file(fname, data, data_width, data_height);
if (0 == strcmp(suff, ".pfm"))
write_pfm_file(fname, data, data_width, data_height);
}
/* --------------- END OF FILE ------------------------- */