source: soft/giet_vm/applications/rosenfeld/nrc2/src/nralloc1.c @ 792

Last change on this file since 792 was 772, checked in by meunier, 9 years ago
  • Ajout de l'application rosenfeld
  • Changement du nom du flag O_CREATE en O_CREAT
File size: 17.9 KB
Line 
1/* ------------------ */
2/* --- nralloc1.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/*
15 * this code is based on the "Numerical Recipes in C 2nd edition" nrutil.c nrutil.h files from
16 * William H. Press, Saul A. Teukolsky, William T. Vetterling, Brian P. Flannery
17 *
18 * The original code is not-copyrighted.
19 * The original routines are placed into the public domain
20 * (see Appendix B: utility routines, pp 964)
21 * for more information, visit http://www.nr.com
22*/
23
24/*
25* 2002/06/11 ajout des fonctions endline
26*/
27#include <stdio.h>
28#include <stddef.h>
29#include <stdlib.h>
30#include <malloc.h>
31#include <math.h> // fabs
32
33#include "mypredef.h"
34#include "nrtype.h"
35#include "nrdef.h"
36#include "nrmacro.h"
37#include "nrkernel.h"
38
39#include "nralloc1.h"
40#include "nrset1.h" // set 1
41
42
43long nr_end = NR_END;
44
45// ------------------------------------------------------------------
46// -- deprecated type (original NRC type, not enough typed vector) --
47// ------------------------------------------------------------------
48
49/* ---------------------------------------- */
50NRC_EXPORT(sint8*) si8vector(long nl, long nh)
51/* ---------------------------------------- */
52{
53    sint8 *v;
54   
55    v=(sint8 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(sint8)));
56    if (!v) nrerror("allocation failure in si8vector()");
57    return v-nl+NR_END;
58}
59/* ---------------------------------------- */
60NRC_EXPORT(uint8*) ui8vector(long nl, long nh)
61/* ---------------------------------------- */
62{
63    uint8 *v;
64   
65    v=(uint8 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(uint8)));
66    if (!v) nrerror("allocation failure in ui8vector()");
67    return v-nl+NR_END;
68}
69/* -------------------------------------------- */
70NRC_EXPORT(sint16*) si16vector(long nl, long nh)
71/* -------------------------------------------- */
72{
73    sint16 *v;
74   
75    v=(sint16 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(sint16)));
76    if (!v) nrerror("allocation failure in si16vector()");
77    return v-nl+NR_END;
78}
79/* -------------------------------------------- */
80NRC_EXPORT(uint16*) ui16vector(long nl, long nh)
81/* -------------------------------------------- */
82{
83    uint16 *v;
84   
85    v=(uint16 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(uint16)));
86    if (!v) nrerror("allocation failure in ui16vector()");
87    return v-nl+NR_END;
88}
89/* -------------------------------------------- */
90IMAGE_EXPORT(sint32*) si32vector(long nl, long nh)
91/* -------------------------------------------- */
92{
93    sint32 *v;
94   
95    v=(sint32 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(sint32)));
96    if (!v) nrerror("allocation failure in si32vector()");
97    if(!v) return NULL;
98    return v-nl+NR_END;
99}
100/* -------------------------------------------- */
101IMAGE_EXPORT(uint32*) ui32vector(long nl, long nh)
102/* -------------------------------------------- */
103{
104    uint32 *v;
105   
106    v=(uint32 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(uint32)));
107    if (!v) nrerror("allocation failure in ui32vector()");
108    if(!v) return NULL;
109    return v-nl+NR_END;
110}
111/* -------------------------------------------- */
112IMAGE_EXPORT(sint64*) si64vector(long nl, long nh)
113/* -------------------------------------------- */
114{
115    sint64 *v;
116   
117    v=(sint64 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(sint64)));
118    if (!v) nrerror("allocation failure in si64vector()");
119    return v-nl+NR_END;
120}
121/* -------------------------------------------- */
122IMAGE_EXPORT(uint64*) ui64vector(long nl, long nh)
123/* -------------------------------------------- */
124{
125    uint64 *v;
126   
127    v=(uint64 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(uint64)));
128    if (!v) nrerror("allocation failure in ui64vector()");
129    return v-nl+NR_END;
130}
131/* ------------------------------------------ */
132NRC_EXPORT(float32*) f32vector(long nl, long nh)
133/* ------------------------------------------ */
134{
135    float32 *v;
136   
137    v=(float32 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float32)));
138    if (!v) nrerror("allocation failure in f32vector()");
139    if(!v) return NULL;
140    return v-nl+NR_END;
141}
142/* ------------------------------------------ */
143NRC_EXPORT(float64*) f64vector(long nl, long nh)
144/* ------------------------------------------ */
145{
146    float64 *v;
147   
148    v=(float64 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float64)));
149    if (!v) nrerror("allocation failure in f64vector()");
150    if(!v) return NULL;
151    return v-nl+NR_END;
152}
153/* ------------------------------------------ */
154IMAGE_EXPORT(rgb8*) rgb8vector(long nl, long nh)
155/* ------------------------------------------ */
156{
157    rgb8 *v;
158   
159    v=(rgb8 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(rgb8)));
160    if (!v) nrerror("allocation failure in rgb8vector()");
161    return v-nl+NR_END;
162}
163/* -------------------------------------------- */
164IMAGE_EXPORT(rgbx8*) rgbx8vector(long nl, long nh)
165/* -------------------------------------------- */
166{
167    rgbx8 *v;
168   
169    v=(rgbx8 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(rgbx8)));
170    if (!v) nrerror("allocation failure in rgbx8vector()");
171    return v-nl+NR_END;
172}
173/* -------------------------------------------- */
174IMAGE_EXPORT(rgb32*) rgb32vector(long nl, long nh)
175/* -------------------------------------------- */
176{
177    rgb32 *v;
178   
179    v=(rgb32 *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(rgb32)));
180    if (!v) nrerror("allocation failure in rgb32vector()");
181    return v-nl+NR_END;
182}
183
184/* ---------------------------------------- */
185IMAGE_EXPORT(void**) vvector(long nl, long nh)
186/* ---------------------------------------- */
187{
188    void **v;
189   
190    v=(void**)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(void*)));
191    if (!v) nrerror("allocation failure in vvector()");
192    return v-nl+NR_END;
193}
194/*
195 * ---------------
196 * --- vector0 ---
197 * ---------------
198 */
199
200// do: allocate a vector and set it to 0
201
202// ----------------------
203// --- not deprecated ---
204// ----------------------
205/* ------------------------------------------- */
206IMAGE_EXPORT(sint8*) si8vector0(long nl, long nh)
207/* ------------------------------------------- */
208{
209    sint8 *v;
210   
211    v=(sint8 *)calloc((size_t) ((nh-nl+1+NR_END)), sizeof(sint8));
212    if (!v) nrerror("allocation failure in si8vector0()");
213    return v-nl+NR_END;
214}
215/* ------------------------------------------- */
216IMAGE_EXPORT(uint8*) ui8vector0(long nl, long nh)
217/* ------------------------------------------- */
218{
219    uint8 *v;
220   
221    v=(uint8 *)calloc((size_t) ((nh-nl+1+NR_END)), sizeof(uint8));
222    if (!v) nrerror("allocation failure in ui8vector0()");
223    return v-nl+NR_END;
224}
225/* -------------------------------------------- */
226IMAGE_EXPORT(int16*) si16vector0(long nl, long nh)
227/* -------------------------------------------- */
228{
229    sint16 *v;
230   
231    v=(sint16 *)calloc((size_t) ((nh-nl+1+NR_END)), sizeof(sint16));
232    if (!v) nrerror("allocation failure in si16vector0()");
233    return v-nl+NR_END;
234}
235/* --------------------------------------------- */
236IMAGE_EXPORT(uint16*) ui16vector0(long nl, long nh)
237/* --------------------------------------------- */
238{
239    uint16 *v;
240   
241    v=(uint16 *)calloc((size_t) ((nh-nl+1+NR_END)), sizeof(uint16));
242    if (!v) nrerror("allocation failure in ui16vector0()");
243    return v-nl+NR_END;
244}
245/* --------------------------------------------- */
246IMAGE_EXPORT(sint32*) si32vector0(long nl, long nh)
247/* --------------------------------------------- */
248/* allocate a sint32 vector with subscript range v[nl..nh] */
249{
250    sint32 *v;
251   
252    v=(sint32 *) calloc((size_t) (nh-nl+1+NR_END),sizeof(sint32));
253    if (!v) nrerror("allocation failure in si32vector0()");
254    return v-nl+NR_END;
255}
256/* --------------------------------------------- */
257IMAGE_EXPORT(uint32*) ui32vector0(long nl, long nh)
258/* --------------------------------------------- */
259/* allocate a uint32 vector with subscript range v[nl..nh] */
260{
261    uint32 *v;
262   
263    v=(uint32 *) calloc((size_t) (nh-nl+1+NR_END),sizeof(uint32));
264    if (!v) nrerror("allocation failure in ui32vector0()");
265    return v-nl+NR_END;
266}
267/* --------------------------------------------- */
268IMAGE_EXPORT(float32*) f32vector0(long nl, long nh)
269/* --------------------------------------------- */
270/* allocate a float32 vector with subscript range v[nl..nh] */
271{
272    float32 *v;
273   
274    v=(float32 *) calloc ( (size_t) (nh-nl+1+NR_END), sizeof(float32) );
275    if (!v) nrerror("allocation failure in f32vector0()");
276    if(!v) return NULL;
277    return v-nl+NR_END;
278}
279/* --------------------------------------------- */
280IMAGE_EXPORT(float64*) f64vector0(long nl, long nh)
281/* --------------------------------------------- */
282/* allocate a float vector with subscript range v[nl..nh] */
283{
284    float64 *v;
285   
286    v=(float64 *) calloc ( (size_t) (nh-nl+1+NR_END), sizeof(float64) );
287    if (!v) nrerror("allocation failure in f64vector0()");
288    if(!v) return NULL;
289    return v-nl+NR_END;
290}
291/* ------------------------------------------- */
292IMAGE_EXPORT(rgb8*) rgb8vector0(long nl, long nh)
293/* ------------------------------------------- */
294{
295    rgb8 *v;
296   
297    v=(rgb8 *)calloc((size_t) ((nh-nl+1+NR_END)), sizeof(rgb8));
298    if (!v) nrerror("allocation failure in rgb8vector0()");
299    return v-nl+NR_END;
300}
301/* --------------------------------------------- */
302IMAGE_EXPORT(rgbx8*) rgbx8vector0(long nl, long nh)
303/* --------------------------------------------- */
304{
305    rgbx8 *v;
306   
307    v=(rgbx8 *)calloc((size_t) ((nh-nl+1+NR_END)), sizeof(rgbx8));
308    if (!v) nrerror("allocation failure in rgbx8vector0()");
309    return v-nl+NR_END;
310}
311/* --------------------------------------------- */
312IMAGE_EXPORT(rgb32*) rgb32vector0(long nl, long nh)
313/* --------------------------------------------- */
314{
315    rgb32 *v;
316   
317    v=(rgb32 *)calloc((size_t) ((nh-nl+1+NR_END)), sizeof(rgb32));
318    if (!v) nrerror("allocation failure in rgb32vector0()");
319    return v-nl+NR_END;
320}
321/*
322 * ----------------------
323 * --- realloc_vector ---
324 * ----------------------
325 */
326
327/* ------------------------------------------------------------ */
328IMAGE_EXPORT(sint8*) realloc_si8vector(sint8 *v, long nl, long nh)
329/* ------------------------------------------------------------ */
330{
331    v += nl;
332    v -= NR_END;
333    v=(sint8 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(sint8)));
334    if (!v) nrerror("allocation failure in realloc_si8vector()");
335   
336    return v-nl+NR_END;
337}
338/* ------------------------------------------------------------ */
339IMAGE_EXPORT(uint8*) realloc_ui8vector(uint8 *v, long nl, long nh)
340/* ------------------------------------------------------------ */
341{
342    v += nl;
343    v -= NR_END;
344    v=(uint8 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(uint8)));
345    if (!v) nrerror("allocation failure in realloc_ui8vector()");
346   
347    return v-nl+NR_END;
348}
349/* --------------------------------------------------------------- */
350IMAGE_EXPORT(sint16*) realloc_si16vector(sint16 *v, long nl, long nh)
351/* --------------------------------------------------------------- */
352{
353    v += nl;
354    v -= NR_END;
355    v=(sint16 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(sint16)));
356    if (!v) nrerror("allocation failure in realloc_si16vector()");
357   
358    return v-nl+NR_END;
359}
360/* --------------------------------------------------------------- */
361IMAGE_EXPORT(uint16*) realloc_ui16vector(uint16 *v, long nl, long nh)
362/* --------------------------------------------------------------- */
363{
364    v += nl;
365    v -= NR_END;
366    v=(uint16 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(uint16)));
367    if (!v) nrerror("allocation failure in realloc_ui16vector()");
368   
369    return v-nl+NR_END;
370}
371/* --------------------------------------------------------------- */
372IMAGE_EXPORT(sint32*) realloc_si32vector(sint32 *v, long nl, long nh)
373/* --------------------------------------------------------------- */
374{
375    v += nl;
376    v -= NR_END;
377    v=(sint32 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(sint32)));
378    if (!v) nrerror("allocation failure in realloc_si32vector()");
379   
380    return v-nl+NR_END;
381}
382/* --------------------------------------------------------------- */
383IMAGE_EXPORT(uint32*) realloc_ui32vector(uint32 *v, long nl, long nh)
384/* --------------------------------------------------------------- */
385{
386    v += nl;
387    v -= NR_END;
388    v=(uint32 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(uint32)));
389    if (!v) nrerror("allocation failure in realloc_ui32vector()");
390   
391    return v-nl+NR_END;
392}
393/* --------------------------------------------------------------- */
394IMAGE_EXPORT(sint64*) realloc_si64vector(sint64 *v, long nl, long nh)
395/* --------------------------------------------------------------- */
396{
397    v += nl;
398    v -= NR_END;
399    v=(sint64 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(sint64)));
400    if (!v) nrerror("allocation failure in realloc_si64vector()");
401   
402    return v-nl+NR_END;
403}
404/* --------------------------------------------------------------- */
405IMAGE_EXPORT(uint64*) realloc_ui64vector(uint64 *v, long nl, long nh)
406/* --------------------------------------------------------------- */
407{
408    v += nl;
409    v -= NR_END;
410    v=(uint64 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(uint64)));
411    if (!v) nrerror("allocation failure in realloc_ui64vector()");
412   
413    return v-nl+NR_END;
414}
415/* ---------------------------------------------------------- */
416IMAGE_EXPORT(rgb8*) realloc_rgb8vector(rgb8 *v, long nl, long nh)
417/* ---------------------------------------------------------- */
418{
419    v += nl;
420    v -= NR_END;
421    v=(rgb8 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(rgb8)));
422    if (!v) nrerror("allocation failure in realloc_rgb8vector()");
423   
424    return v-nl+NR_END;
425}
426/* -------------------------------------------------------------- */
427IMAGE_EXPORT(rgbx8*) realloc_rgbx8vector(rgbx8 *v, long nl, long nh)
428/* -------------------------------------------------------------- */
429{
430    v += nl;
431    v -= NR_END;
432    v=(rgbx8 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(rgbx8)));
433    if (!v) nrerror("allocation failure in realloc_rgbx8vector()");
434   
435    return v-nl+NR_END;
436}
437/* -------------------------------------------------------------- */
438IMAGE_EXPORT(rgb32*) realloc_rgb32vector(rgb32 *v, long nl, long nh)
439/* -------------------------------------------------------------- */
440{
441    v += nl;
442    v -= NR_END;
443    v=(rgb32 *)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(rgb32)));
444    if (!v) nrerror("allocation failure in realloc_rgb32vector()");
445   
446    return v-nl+NR_END;
447}
448/* ---------------------------------------------------------- */
449IMAGE_EXPORT(void**) realloc_vvector(void **v, long nl, long nh)
450/* ---------------------------------------------------------- */
451/*
452* add n item to an void* vector with subscript range
453* fromv[nl..nh] to [nl..nh+n] */
454{
455    v += nl;
456    v -= NR_END;
457    v=(void**)realloc(v, (size_t) ((nh-nl+1+NR_END)*sizeof(void*)));
458    if (!v) nrerror("allocation failure in realloc vvector()");
459   
460    return v-nl+NR_END;
461}
462/*
463 * -------------------
464 * --- free_vector ---
465 * -------------------
466 */
467
468/* free a byte vector allocated with bvector() */
469
470/* ------------------------------------------------------- */
471IMAGE_EXPORT(void) free_si8vector(sint8 *v, long nl, long nh)
472/* ------------------------------------------------------- */
473{
474    free((FREE_ARG) (v+nl-NR_END));
475}
476/* ------------------------------------------------------- */
477IMAGE_EXPORT(void) free_ui8vector(uint8 *v, long nl, long nh)
478/* ------------------------------------------------------- */
479{
480    free((FREE_ARG) (v+nl-NR_END));
481}
482/* --------------------------------------------------------- */
483IMAGE_EXPORT(void) free_si16vector(sint16 *v, long nl, long nh)
484/* --------------------------------------------------------- */
485{
486    free((FREE_ARG) (v+nl-NR_END));
487}
488/* ------------------------------------------------------- */
489IMAGE_EXPORT(void) free_ui16vector(uint16 *v, long nl, long nh)
490/* ------------------------------------------------------- */
491{
492    free((FREE_ARG) (v+nl-NR_END));
493}
494/* --------------------------------------------------------- */
495IMAGE_EXPORT(void) free_si32vector(sint32 *v, long nl, long nh)
496/* --------------------------------------------------------- */
497{
498    free((FREE_ARG) (v+nl-NR_END));
499}
500/* --------------------------------------------------------- */
501IMAGE_EXPORT(void) free_ui32vector(uint32 *v, long nl, long nh)
502/* --------------------------------------------------------- */
503{
504    free((FREE_ARG) (v+nl-NR_END));
505}
506/* --------------------------------------------------------- */
507IMAGE_EXPORT(void) free_si64vector(sint64 *v, long nl, long nh)
508/* --------------------------------------------------------- */
509{
510    free((FREE_ARG) (v+nl-NR_END));
511}
512/* --------------------------------------------------------- */
513IMAGE_EXPORT(void) free_ui64vector(uint64 *v, long nl, long nh)
514/* --------------------------------------------------------- */
515{
516    free((FREE_ARG) (v+nl-NR_END));
517}
518/* ---------------------------------------------------- */
519IMAGE_EXPORT(void) free_f32vector(float32 *v, long nl, long nh)
520/* ---------------------------------------------------- */
521{
522    free((FREE_ARG) (v+nl-NR_END));
523}
524/* ---------------------------------------------------- */
525IMAGE_EXPORT(void) free_f64vector(float64 *v, long nl, long nh)
526/* ---------------------------------------------------- */
527{
528    free((FREE_ARG) (v+nl-NR_END));
529}
530/* -------------------------------------------------------- */
531IMAGE_EXPORT(void) free_rgb8vector(rgb8 *v, long nl, long nh)
532/* -------------------------------------------------------- */
533{
534    free((FREE_ARG) (v+nl-NR_END));
535}
536/* --------------------------------------------------------- */
537IMAGE_EXPORT(void) free_rgbx8vector(rgbx8 *v, long nl, long nh)
538/* --------------------------------------------------------- */
539{
540    free((FREE_ARG) (v+nl-NR_END));
541}
542/* ------------------------------------------------------- */
543IMAGE_EXPORT(void) free_rgb32vector(rgb32 *v, long nl, long nh)
544/* ------------------------------------------------------- */
545{
546    free((FREE_ARG) (v+nl-NR_END));
547}
548/* ----------------------------------------------------- */
549IMAGE_EXPORT(void) free_vvector(void **v, long nl, long nh)
550/* ----------------------------------------------------- */
551{
552    free((FREE_ARG) (v+nl-NR_END));
553}
Note: See TracBrowser for help on using the repository browser.