source: soft/giet_vm/applications/rosenfeld/nrc2/src/nralloc3.c

Last change on this file was 821, checked in by meunier, 8 years ago
  • Added several versions of rosenfeld: { SLOW, FAST } x { FEATURES, NO_FEATURES }
  • Added native linux compilation support
  • Added a script to check results natively
  • Started to refactor nrc code
File size: 8.4 KB
Line 
1/* ------------------ */
2/* --- nralloc3.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 <ctype.h> // isdigit
31#include <string.h> // memcpy
32#include <math.h> // fabs
33
34
35#include "nrc_os_config.h"
36#include "mypredef.h"
37#include "nrtype.h"
38#include "nrdef.h"
39#include "nrmacro.h"
40#include "nrkernel.h"
41
42#include "nralloc3.h"
43
44
45#undef type_cube
46#define type_cube(t) \
47t *** short_name(t,,cube)(int32_t ndl, int32_t ndh, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \
48{                                                                                                       \
49    int32_t ndep = ndh - ndl + 1;                                                                       \
50    int32_t nrow = nrh - nrl + 1;                                                                       \
51    int32_t nrol = nch - ncl + 1;                                                                       \
52    t *** c;                                                                                            \
53    /* allocate pointers to pointers to rows */                                                         \
54    c = malloc((ndep + NR_END) * sizeof(t **));                                                         \
55    if (c == NULL) {                                                                                    \
56        nrerror("*** Error: allocation failure in %s\n", __func__);                                     \
57    }                                                                                                   \
58    c += NR_END;                                                                                        \
59    c -= ndl;                                                                                           \
60    /* allocate pointers to rows anc set pointers to them */                                            \
61    c[ndl] = malloc((ndep * nrow + NR_END) * sizeof(t *));                                              \
62    if (c[ndl] == NULL) {                                                                               \
63        nrerror("*** Error: allocation failure in %s\n", __func__);                                     \
64    }                                                                                                   \
65    c[ndl] += NR_END;                                                                                   \
66    c[ndl] -= nrl;                                                                                      \
67    /* allocate rows anc set pointers to them */                                                        \
68    c[ndl][nrl] = malloc((ndep * nrow * nrol + NR_END) * sizeof(t));                                    \
69    if (c[ndl][nrl]) {                                                                                  \
70        nrerror("*** Error: allocation failure in %s\n", __func__);                                     \
71    }                                                                                                   \
72    c[ndl][nrl] += NR_END;                                                                              \
73    c[ndl][nrl] -= ncl;                                                                                 \
74                                                                                                        \
75    for(int32_t j = nrl + 1; j <= nrh; j++) {                                                           \
76        c[ndl][j] = c[ndl][j - 1] + nrol;                                                               \
77    }                                                                                                   \
78    for(int32_t i = ndl + 1; i <= ndh; i++) {                                                           \
79        c[i] = c[i - 1] + nrow;                                                                         \
80        c[i][nrl] = c[i - 1][nrl] + nrow * nrol;                                                        \
81        for (int32_t j = nrl + 1; j <= nrh; j++) {                                                      \
82            c[i][j] = c[i][j - 1] + nrol;                                                               \
83        }                                                                                               \
84    }                                                                                                   \
85    /* return pointer to array of pointers to rows */                                                   \
86    return t;                                                                                           \
87}
88
89
90type_cube(int8_t);
91type_cube(uint8_t);
92type_cube(int16_t);
93type_cube(uint16_t);
94type_cube(int32_t);
95type_cube(uint32_t);
96type_cube(int64_t);
97type_cube(uint64_t);
98type_cube(float);
99type_cube(double);
100type_cube(rgb8);
101type_cube(rgbx8);
102
103#undef free_type_cube
104#define free_type_cube(t) \
105void short_name(t,free_,cube)(t *** c, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t ndl, int32_t ndh) \
106{                                                                                                        \
107    free((FREE_ARG) (c[nrl][ncl] + ndl - NR_END));                                                       \
108    free((FREE_ARG) (c[nrl] + ncl - NR_END));                                                            \
109    free((FREE_ARG) (c + nrl - NR_END));                                                                 \
110}
111
112free_type_cube(int8_t);
113free_type_cube(uint8_t);
114free_type_cube(int16_t);
115free_type_cube(uint16_t);
116free_type_cube(int32_t);
117free_type_cube(uint32_t);
118free_type_cube(int64_t);
119free_type_cube(uint64_t);
120free_type_cube(float);
121free_type_cube(double);
122free_type_cube(rgb8);
123free_type_cube(rgbx8);
124
125
126#if 0
127/* ----------------------------------------------------------------------- */
128double*** d3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
129/* ----------------------------------------------------------------------- */
130/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
131{
132    long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
133    double ***t;
134   
135    /* allocate pointers to pointers to rows */
136    t=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double**)));
137    if (!t) nrerror("allocation failure 1 in d3tensor()");
138    t += NR_END;
139    t -= nrl;
140   
141    /* allocate pointers to rows and set pointers to them */
142    t[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double*)));
143    if (!t[nrl]) nrerror("allocation failure 2 in d3tensor()");
144    t[nrl] += NR_END;
145    t[nrl] -= ncl;
146   
147    /* allocate rows and set pointers to them */
148    t[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(double)));
149    if (!t[nrl][ncl]) nrerror("allocation failure 3 in d3tensor()");
150    t[nrl][ncl] += NR_END;
151    t[nrl][ncl] -= ndl;
152   
153    for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
154    for(i=nrl+1;i<=nrh;i++) {
155        t[i]=t[i-1]+ncol;
156        t[i][ncl]=t[i-1][ncl]+ncol*ndep;
157        for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
158    }
159   
160    /* return pointer to array of pointers to rows */
161    return t;
162}
163
164
165/* ------------------------------------------------------------------------------ */
166void free_d3tensor(double ***t,long nrl,long nrh,long ncl,long nch,long ndl,long ndh)
167/* ------------------------------------------------------------------------------ */
168/* free a float f3tensor allocated by d3tensor() */
169{
170    free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
171    free((FREE_ARG) (t[nrl]+ncl-NR_END));
172    free((FREE_ARG) (t+nrl-NR_END));
173}
174#endif
175
176// Local Variables:
177// tab-width: 4
178// c-basic-offset: 4
179// c-file-offsets:((innamespace . 0)(inline-open . 0))
180// indent-tabs-mode: nil
181// End:
182
183// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
184
Note: See TracBrowser for help on using the repository browser.