1 | /* ---------------- */ |
---|
2 | /* --- nriof1.c --- */ |
---|
3 | /* ---------------- */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (c) 2000-2014, Lionel Lacassagne, All rights reserved |
---|
7 | * Univ Paris Sud XI, CNRS |
---|
8 | * |
---|
9 | * Distributed under the Boost Software License, Version 1.0 |
---|
10 | * see accompanying file LICENSE.txt or copy it at |
---|
11 | * http://www.boost.org/LICENSE_1_0.txt |
---|
12 | */ |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <stddef.h> |
---|
16 | #include <stdlib.h> |
---|
17 | |
---|
18 | #include "nrc_os_config.h" |
---|
19 | #include "mypredef.h" |
---|
20 | #include "nrtype.h" |
---|
21 | #include "nrdef.h" |
---|
22 | #include "nrmacro.h" |
---|
23 | #include "nrkernel.h" |
---|
24 | |
---|
25 | #include "nralloc1.h" |
---|
26 | #include "nrio0.h" |
---|
27 | #include "nrio1.h" |
---|
28 | |
---|
29 | /* |
---|
30 | * -------------------- |
---|
31 | * --- write_vector --- |
---|
32 | * -------------------- |
---|
33 | */ |
---|
34 | |
---|
35 | #undef write_type_vector |
---|
36 | #define write_type_vector(t) \ |
---|
37 | void short_name(t,write_,vector)(t * v, int32_t nl, int32_t nh, char * format, char * filename) \ |
---|
38 | { \ |
---|
39 | FILE * f = fopen(filename, "wt"); \ |
---|
40 | if (f == NULL) { \ |
---|
41 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
42 | } \ |
---|
43 | for (int32_t k = nl; k <= nh; k++) { \ |
---|
44 | fprintf(f, format, v[k]); \ |
---|
45 | } \ |
---|
46 | fprintf(f, "\n"); \ |
---|
47 | fclose(f); \ |
---|
48 | } |
---|
49 | |
---|
50 | write_type_vector(int8_t); |
---|
51 | write_type_vector(uint8_t); |
---|
52 | write_type_vector(int16_t); |
---|
53 | write_type_vector(uint16_t); |
---|
54 | write_type_vector(int32_t); |
---|
55 | write_type_vector(uint32_t); |
---|
56 | write_type_vector(int64_t); |
---|
57 | write_type_vector(uint64_t); |
---|
58 | write_type_vector(float); |
---|
59 | write_type_vector(double); |
---|
60 | |
---|
61 | |
---|
62 | /* ---------------------------------------------------------------------------------- */ |
---|
63 | void write_rgb8vector(rgb8 * v,int32_t nl, int32_t nh, char * format, char * filename) |
---|
64 | /* ---------------------------------------------------------------------------------- */ |
---|
65 | { |
---|
66 | FILE * f = fopen(filename, "wt"); |
---|
67 | if (f == NULL) { |
---|
68 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
69 | } |
---|
70 | for (int32_t k = nl; k <= nh; k++) { |
---|
71 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
72 | } |
---|
73 | fprintf(f, "\n"); |
---|
74 | fclose(f); |
---|
75 | } |
---|
76 | |
---|
77 | /* -------------------------------------------------------------------------------------- */ |
---|
78 | void write_rgbx8vector(rgbx8 * v, int32_t nl, int32_t nh, char * format, char * filename) |
---|
79 | /* -------------------------------------------------------------------------------------- */ |
---|
80 | { |
---|
81 | FILE * f = fopen(filename, "wt"); |
---|
82 | if (f == NULL) { |
---|
83 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
84 | } |
---|
85 | for (int32_t k = nl; k <= nh; k++) { |
---|
86 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
87 | } |
---|
88 | fprintf(f, "\n"); |
---|
89 | fclose(f); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | /* |
---|
94 | * ---------------------- |
---|
95 | * --- write_vector_T --- |
---|
96 | * ---------------------- |
---|
97 | */ |
---|
98 | |
---|
99 | #undef write_type_vector_T |
---|
100 | #define write_type_vector_T(t) \ |
---|
101 | void short_name(t,write_,vector_T)(t * v, int32_t nl, int32_t nh, char * format, char * filename) \ |
---|
102 | { \ |
---|
103 | FILE * f = fopen(filename, "wt"); \ |
---|
104 | if (f == NULL) { \ |
---|
105 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
106 | } \ |
---|
107 | for (int32_t k = nl; k <= nh; k++) { \ |
---|
108 | fprintf(f, format, v[k]); \ |
---|
109 | fprintf(f, "\n"); \ |
---|
110 | } \ |
---|
111 | fclose(f); \ |
---|
112 | } |
---|
113 | |
---|
114 | write_type_vector_T(int8_t); |
---|
115 | write_type_vector_T(uint8_t); |
---|
116 | write_type_vector_T(int16_t); |
---|
117 | write_type_vector_T(uint16_t); |
---|
118 | write_type_vector_T(int32_t); |
---|
119 | write_type_vector_T(uint32_t); |
---|
120 | write_type_vector_T(int64_t); |
---|
121 | write_type_vector_T(uint64_t); |
---|
122 | write_type_vector_T(float); |
---|
123 | write_type_vector_T(double); |
---|
124 | |
---|
125 | |
---|
126 | /* ---------------------------------------------------------------------------------- */ |
---|
127 | void write_rgb8vector_T(rgb8 * v,int32_t nl, int32_t nh, char * format, char * filename) |
---|
128 | /* ---------------------------------------------------------------------------------- */ |
---|
129 | { |
---|
130 | FILE * f = fopen(filename, "wt"); |
---|
131 | if (f == NULL) { |
---|
132 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
133 | } |
---|
134 | for (int32_t k = nl; k <= nh; k++) { |
---|
135 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
136 | fprintf(f, "\n"); |
---|
137 | } |
---|
138 | fclose(f); |
---|
139 | } |
---|
140 | |
---|
141 | /* -------------------------------------------------------------------------------------- */ |
---|
142 | void write_rgbx8vector_T(rgbx8 * v, int32_t nl, int32_t nh, char * format, char * filename) |
---|
143 | /* -------------------------------------------------------------------------------------- */ |
---|
144 | { |
---|
145 | FILE * f = fopen(filename, "wt"); |
---|
146 | if (f == NULL) { |
---|
147 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
148 | } |
---|
149 | for (int32_t k = nl; k <= nh; k++) { |
---|
150 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
151 | fprintf(f, "\n"); |
---|
152 | } |
---|
153 | fclose(f); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /* |
---|
158 | * --------------------------- |
---|
159 | * --- write_vector_number --- |
---|
160 | * --------------------------- |
---|
161 | */ |
---|
162 | |
---|
163 | // @QM : fprintf(f, format, k, k, k); ? |
---|
164 | #undef write_type_vector_number |
---|
165 | #define write_type_vector_number(t) \ |
---|
166 | void short_name(t,write_,vector_number)(t * v, int32_t nl, int32_t nh, char * format, char * filename) \ |
---|
167 | { \ |
---|
168 | FILE * f = fopen(filename, "wt"); \ |
---|
169 | if (f == NULL) { \ |
---|
170 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
171 | } \ |
---|
172 | for (int32_t k = nl; k <= nh; k++) { \ |
---|
173 | fprintf(f, format, k); \ |
---|
174 | } \ |
---|
175 | fprintf(f, "\n"); \ |
---|
176 | for (int32_t k = nl; k <= nh; k++) { \ |
---|
177 | fprintf(f, format, v[k]); \ |
---|
178 | } \ |
---|
179 | fprintf(f, "\n"); \ |
---|
180 | fclose(f); \ |
---|
181 | } |
---|
182 | |
---|
183 | write_type_vector_number(int8_t); |
---|
184 | write_type_vector_number(uint8_t); |
---|
185 | write_type_vector_number(int16_t); |
---|
186 | write_type_vector_number(uint16_t); |
---|
187 | write_type_vector_number(int32_t); |
---|
188 | write_type_vector_number(uint32_t); |
---|
189 | write_type_vector_number(int64_t); |
---|
190 | write_type_vector_number(uint64_t); |
---|
191 | write_type_vector_number(float); |
---|
192 | write_type_vector_number(double); |
---|
193 | |
---|
194 | |
---|
195 | /* ------------------------------------------------------------------------------------------- */ |
---|
196 | void write_rgb8vector_number(rgb8 * v, int32_t nl, int32_t nh, char * format, char * filename) |
---|
197 | /* ------------------------------------------------------------------------------------------- */ |
---|
198 | { |
---|
199 | FILE * f = fopen(filename, "wt"); |
---|
200 | if (f == NULL) { |
---|
201 | nrerror("Can't open file %s in %s\n", filename, __func__); |
---|
202 | } |
---|
203 | for (int32_t k = nl; k <= nh; k++) { |
---|
204 | fprintf(f, "%3d", k); |
---|
205 | } |
---|
206 | fprintf(f, "\n"); |
---|
207 | for (int32_t k = nl; k <= nh; k++) { |
---|
208 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
209 | } |
---|
210 | fprintf(f, "\n"); |
---|
211 | fclose(f); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /* --------------------------------------------------------------------------------------------- */ |
---|
216 | void write_rgbx8vector_number(rgbx8 * v, int32_t nl, int32_t nh, char * format, char * filename) |
---|
217 | /* --------------------------------------------------------------------------------------------- */ |
---|
218 | { |
---|
219 | FILE * f = fopen(filename, "wt"); |
---|
220 | if (f == NULL) { |
---|
221 | nrerror("Can't open file %s in %s\n", filename, __func__); |
---|
222 | } |
---|
223 | for (int32_t k = nl; k <= nh; k++) { |
---|
224 | fprintf(f, "%3d", k); |
---|
225 | } |
---|
226 | fprintf(f, "\n"); |
---|
227 | for (int32_t k = nl; k <= nh; k++) { |
---|
228 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
229 | } |
---|
230 | fprintf(f, "\n"); |
---|
231 | fclose(f); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | /* |
---|
236 | * ----------------------------- |
---|
237 | * --- write_vector_T_number --- |
---|
238 | * ----------------------------- |
---|
239 | */ |
---|
240 | |
---|
241 | #undef write_type_vector_T_number |
---|
242 | #define write_type_vector_T_number(t) \ |
---|
243 | void short_name(t,write_,vector_T_number)(t * v, int32_t nl, int32_t nh, char * format, char * filename) \ |
---|
244 | { \ |
---|
245 | FILE * f = fopen(filename, "wt"); \ |
---|
246 | if (f == NULL) { \ |
---|
247 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
248 | } \ |
---|
249 | for (int32_t k = nl; k <= nh; k++) { \ |
---|
250 | fprintf(f, "%3d", k); \ |
---|
251 | fprintf(f, format, v[k]); \ |
---|
252 | fprintf(f, "\n"); \ |
---|
253 | } \ |
---|
254 | fclose(f); \ |
---|
255 | } |
---|
256 | |
---|
257 | write_type_vector_T_number(int8_t); |
---|
258 | write_type_vector_T_number(uint8_t); |
---|
259 | write_type_vector_T_number(int16_t); |
---|
260 | write_type_vector_T_number(uint16_t); |
---|
261 | write_type_vector_T_number(int32_t); |
---|
262 | write_type_vector_T_number(uint32_t); |
---|
263 | write_type_vector_T_number(int64_t); |
---|
264 | write_type_vector_T_number(uint64_t); |
---|
265 | write_type_vector_T_number(float); |
---|
266 | write_type_vector_T_number(double); |
---|
267 | |
---|
268 | |
---|
269 | /* ------------------------------------------------------------------------------------------- */ |
---|
270 | void write_rgb8vector_T_number(rgb8 * v, int32_t nl, int32_t nh, char * format, char * filename) |
---|
271 | /* ------------------------------------------------------------------------------------------- */ |
---|
272 | { |
---|
273 | FILE * f = fopen(filename, "wt"); |
---|
274 | if (f == NULL) { |
---|
275 | nrerror("Can't open file %s in %s\n", filename, __func__); |
---|
276 | } |
---|
277 | for (int32_t k = nl; k <= nh; k++) { |
---|
278 | fprintf(f, "%3d", k); |
---|
279 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
280 | fprintf(f, "\n"); |
---|
281 | } |
---|
282 | fclose(f); |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | /* --------------------------------------------------------------------------------------------- */ |
---|
287 | void write_rgbx8vector_T_number(rgbx8 * v, int32_t nl, int32_t nh, char * format, char * filename) |
---|
288 | /* --------------------------------------------------------------------------------------------- */ |
---|
289 | { |
---|
290 | FILE * f = fopen(filename, "wt"); |
---|
291 | if (f == NULL) { |
---|
292 | nrerror("Can't open file %s in %s\n", filename, __func__); |
---|
293 | } |
---|
294 | for (int32_t k = nl; k <= nh; k++) { |
---|
295 | fprintf(f, "%3d", k); |
---|
296 | fprintf(f, format, v[k].r, v[k].g, v[k].b); |
---|
297 | fprintf(f, "\n"); |
---|
298 | } |
---|
299 | fclose(f); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | /* |
---|
305 | * -------------------- |
---|
306 | * --- fread_vector --- |
---|
307 | * -------------------- |
---|
308 | */ |
---|
309 | |
---|
310 | #undef fread_type_vector |
---|
311 | #define fread_type_vector(t) \ |
---|
312 | void short_name(t,fread_,vector)(char * filename, t * v, int32_t nl, int32_t nh) \ |
---|
313 | { \ |
---|
314 | int32_t ncol = nh - nl + 1; \ |
---|
315 | int32_t nread; \ |
---|
316 | FILE * f = fopen(filename, "rb"); \ |
---|
317 | if (f == NULL) { \ |
---|
318 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
319 | } \ |
---|
320 | \ |
---|
321 | nread = fread(v + nl, sizeof(t), ncol, f); \ |
---|
322 | if (nread != ncol) { \ |
---|
323 | nrerror("%s can't read the whole vector", __func__); \ |
---|
324 | } \ |
---|
325 | fclose(f); \ |
---|
326 | } |
---|
327 | |
---|
328 | fread_type_vector(int8_t); |
---|
329 | fread_type_vector(uint8_t); |
---|
330 | fread_type_vector(int16_t); |
---|
331 | fread_type_vector(uint16_t); |
---|
332 | fread_type_vector(int32_t); |
---|
333 | fread_type_vector(uint32_t); |
---|
334 | fread_type_vector(int64_t); |
---|
335 | fread_type_vector(uint64_t); |
---|
336 | fread_type_vector(float); |
---|
337 | fread_type_vector(double); |
---|
338 | fread_type_vector(rgb8); |
---|
339 | fread_type_vector(rgbx8); |
---|
340 | |
---|
341 | |
---|
342 | /* ------------------- */ |
---|
343 | /* -- fwrite_vector -- */ |
---|
344 | /* ------------------- */ |
---|
345 | |
---|
346 | #undef fwrite_type_vector |
---|
347 | #define fwrite_type_vector(t) \ |
---|
348 | void short_name(t,fwrite_,vector)(t * v, int32_t nl, int32_t nh, char * filename) \ |
---|
349 | { \ |
---|
350 | int32_t ncol = nh - nl + 1; \ |
---|
351 | FILE * f = fopen(filename, "rb"); \ |
---|
352 | if (f == NULL) { \ |
---|
353 | nrerror("Can't open file %s in %s\n", filename, __func__); \ |
---|
354 | } \ |
---|
355 | fwrite(v + nl, sizeof(t), ncol, f); \ |
---|
356 | fclose(f); \ |
---|
357 | } |
---|
358 | |
---|
359 | fwrite_type_vector(int8_t); |
---|
360 | fwrite_type_vector(uint8_t); |
---|
361 | fwrite_type_vector(int16_t); |
---|
362 | fwrite_type_vector(uint16_t); |
---|
363 | fwrite_type_vector(int32_t); |
---|
364 | fwrite_type_vector(uint32_t); |
---|
365 | fwrite_type_vector(int64_t); |
---|
366 | fwrite_type_vector(uint64_t); |
---|
367 | fwrite_type_vector(float); |
---|
368 | fwrite_type_vector(double); |
---|
369 | fwrite_type_vector(rgb8); |
---|
370 | fwrite_type_vector(rgbx8); |
---|
371 | |
---|
372 | |
---|
373 | |
---|
374 | // Local Variables: |
---|
375 | // tab-width: 4 |
---|
376 | // c-basic-offset: 4 |
---|
377 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
378 | // indent-tabs-mode: nil |
---|
379 | // End: |
---|
380 | |
---|
381 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
382 | |
---|