[772] | 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 | |
---|
[821] | 34 | |
---|
| 35 | #include "nrc_os_config.h" |
---|
[772] | 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 | |
---|
[821] | 44 | |
---|
| 45 | #undef type_cube |
---|
| 46 | #define type_cube(t) \ |
---|
| 47 | t *** 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 | |
---|
| 90 | type_cube(int8_t); |
---|
| 91 | type_cube(uint8_t); |
---|
| 92 | type_cube(int16_t); |
---|
| 93 | type_cube(uint16_t); |
---|
| 94 | type_cube(int32_t); |
---|
| 95 | type_cube(uint32_t); |
---|
| 96 | type_cube(int64_t); |
---|
| 97 | type_cube(uint64_t); |
---|
| 98 | type_cube(float); |
---|
| 99 | type_cube(double); |
---|
| 100 | type_cube(rgb8); |
---|
| 101 | type_cube(rgbx8); |
---|
| 102 | |
---|
| 103 | #undef free_type_cube |
---|
| 104 | #define free_type_cube(t) \ |
---|
| 105 | void 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 | |
---|
| 112 | free_type_cube(int8_t); |
---|
| 113 | free_type_cube(uint8_t); |
---|
| 114 | free_type_cube(int16_t); |
---|
| 115 | free_type_cube(uint16_t); |
---|
| 116 | free_type_cube(int32_t); |
---|
| 117 | free_type_cube(uint32_t); |
---|
| 118 | free_type_cube(int64_t); |
---|
| 119 | free_type_cube(uint64_t); |
---|
| 120 | free_type_cube(float); |
---|
| 121 | free_type_cube(double); |
---|
| 122 | free_type_cube(rgb8); |
---|
| 123 | free_type_cube(rgbx8); |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | #if 0 |
---|
[772] | 127 | /* ----------------------------------------------------------------------- */ |
---|
| 128 | double*** 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 | } |
---|
[821] | 163 | |
---|
| 164 | |
---|
[772] | 165 | /* ------------------------------------------------------------------------------ */ |
---|
| 166 | void 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 | } |
---|
[821] | 174 | #endif |
---|
[772] | 175 | |
---|
[821] | 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 | |
---|