[772] | 1 | /* ------------------ */ |
---|
| 2 | /* --- nralloc2.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 | |
---|
[821] | 33 | #include "nrc_os_config.h" |
---|
[772] | 34 | #include "mypredef.h" |
---|
| 35 | #include "nrtype.h" |
---|
[821] | 36 | #include "nrtypex.h" |
---|
[772] | 37 | #include "nrdef.h" |
---|
| 38 | #include "nrmacro.h" |
---|
| 39 | #include "nrkernel.h" |
---|
| 40 | |
---|
| 41 | #include "nralloc2.h" |
---|
| 42 | |
---|
| 43 | /* |
---|
| 44 | * -------------- |
---|
| 45 | * --- matrix --- |
---|
| 46 | * -------------- |
---|
| 47 | */ |
---|
[821] | 48 | |
---|
| 49 | |
---|
| 50 | #undef type_matrix |
---|
| 51 | #define type_matrix(t) \ |
---|
| 52 | t ** short_name(t,,matrix)(int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \ |
---|
| 53 | { \ |
---|
| 54 | int32_t nrow = nrh - nrl + 1; \ |
---|
| 55 | int32_t ncol = nch - ncl + 1; \ |
---|
| 56 | t ** m; \ |
---|
| 57 | /* allocate pointers to row */ \ |
---|
| 58 | m = malloc((nrow + NR_END) * sizeof(t *)); \ |
---|
| 59 | if (m == NULL) { \ |
---|
| 60 | nrerror("*** Error: allocation failure in %s\n", __func__); \ |
---|
| 61 | } \ |
---|
| 62 | m += NR_END; \ |
---|
| 63 | m -= nrl; \ |
---|
| 64 | /* allocate rows and set pointers to them */ \ |
---|
| 65 | m[nrl] = malloc((nrow * ncol + NR_END) * sizeof(t)); \ |
---|
| 66 | if (m[nrl] == NULL) { \ |
---|
| 67 | nrerror("*** Error: allocation failure in %s\n", __func__); \ |
---|
| 68 | } \ |
---|
| 69 | m[nrl] += NR_END; \ |
---|
| 70 | m[nrl] -= ncl; \ |
---|
| 71 | for (int32_t i = nrl + 1; i <= nrh; i++) { \ |
---|
| 72 | m[i] = m[i - 1] + ncol; \ |
---|
| 73 | } \ |
---|
| 74 | /* return pointer to array of pointers to rows */ \ |
---|
| 75 | return m; \ |
---|
[772] | 76 | } |
---|
| 77 | |
---|
[821] | 78 | |
---|
| 79 | type_matrix(int8_t); |
---|
| 80 | type_matrix(uint8_t); |
---|
| 81 | type_matrix(int16_t); |
---|
| 82 | type_matrix(uint16_t); |
---|
| 83 | type_matrix(int32_t); |
---|
| 84 | type_matrix(uint32_t); |
---|
| 85 | type_matrix(int64_t); |
---|
| 86 | type_matrix(uint64_t); |
---|
| 87 | type_matrix(float); |
---|
| 88 | type_matrix(double); |
---|
| 89 | type_matrix(void_p); |
---|
| 90 | type_matrix(rgb8); |
---|
| 91 | type_matrix(rgbx8); |
---|
| 92 | type_matrix(rgb32); |
---|
| 93 | type_matrix(rgbx32); |
---|
| 94 | type_matrix(complex32); |
---|
| 95 | type_matrix(complex64); |
---|
| 96 | type_matrix(si16Point); |
---|
| 97 | type_matrix(ui16Point); |
---|
| 98 | type_matrix(si32Point); |
---|
| 99 | type_matrix(ui32Point); |
---|
| 100 | type_matrix(f32Point); |
---|
| 101 | type_matrix(si16Triplet); |
---|
| 102 | type_matrix(ui16Triplet); |
---|
| 103 | type_matrix(si32Triplet); |
---|
| 104 | type_matrix(ui32Triplet); |
---|
| 105 | type_matrix(f32Triplet); |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | #undef type_matrix0 |
---|
| 111 | #define type_matrix0(t) \ |
---|
| 112 | t ** short_name(t,,matrix0)(int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \ |
---|
| 113 | { \ |
---|
| 114 | int32_t nrow = nrh - nrl + 1; \ |
---|
| 115 | int32_t ncol = nch - ncl + 1; \ |
---|
| 116 | t ** m; \ |
---|
| 117 | /* allocate pointers to row */ \ |
---|
| 118 | m = malloc((nrow + NR_END) * sizeof(t *)); \ |
---|
| 119 | if (m == NULL) { \ |
---|
| 120 | nrerror("*** Error: allocation failure in %s\n", __func__); \ |
---|
| 121 | } \ |
---|
| 122 | m += NR_END; \ |
---|
| 123 | m -= nrl; \ |
---|
| 124 | /* allocate rows and set pointers to them */ \ |
---|
| 125 | m[nrl] = calloc((nrow * ncol + NR_END), sizeof(t)); \ |
---|
| 126 | if (m[nrl] == NULL) { \ |
---|
| 127 | nrerror("*** Error: allocation failure in %s\n", __func__); \ |
---|
| 128 | } \ |
---|
| 129 | m[nrl] += NR_END; \ |
---|
| 130 | m[nrl] -= ncl; \ |
---|
| 131 | for (int32_t i = nrl + 1; i <= nrh; i++) { \ |
---|
| 132 | m[i] = m[i - 1] + ncol; \ |
---|
| 133 | } \ |
---|
| 134 | /* return pointer to array of pointers to rows */ \ |
---|
| 135 | return m; \ |
---|
[772] | 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
[821] | 139 | type_matrix0(int8_t); |
---|
| 140 | type_matrix0(uint8_t); |
---|
| 141 | type_matrix0(int16_t); |
---|
| 142 | type_matrix0(uint16_t); |
---|
| 143 | type_matrix0(int32_t); |
---|
| 144 | type_matrix0(uint32_t); |
---|
| 145 | type_matrix0(int64_t); |
---|
| 146 | type_matrix0(uint64_t); |
---|
| 147 | type_matrix0(float); |
---|
| 148 | type_matrix0(double); |
---|
| 149 | type_matrix0(void_p); |
---|
| 150 | type_matrix0(rgb8); |
---|
| 151 | type_matrix0(rgbx8); |
---|
| 152 | type_matrix0(rgb32); |
---|
| 153 | type_matrix0(rgbx32); |
---|
| 154 | type_matrix0(complex32); |
---|
| 155 | type_matrix0(complex64); |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | #if TARGET_OS == GIETVM |
---|
| 159 | |
---|
| 160 | #undef remote_type_matrix |
---|
| 161 | #define remote_type_matrix(t) \ |
---|
| 162 | t ** short_name(t,remote_,matrix)(int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \ |
---|
| 163 | { \ |
---|
| 164 | int32_t nrow = nrh - nrl + 1; \ |
---|
| 165 | int32_t ncol = nch - ncl + 1; \ |
---|
| 166 | t ** m; \ |
---|
| 167 | /* allocate pointers to row */ \ |
---|
| 168 | m = malloc((nrow + NR_END) * sizeof(t *)); \ |
---|
| 169 | if (m == NULL) { \ |
---|
| 170 | nrerror("*** Error: allocation failure in %s\n", __func__); \ |
---|
| 171 | } \ |
---|
| 172 | m += NR_END; \ |
---|
| 173 | m -= nrl; \ |
---|
| 174 | /* allocate rows and set pointers to them */ \ |
---|
| 175 | m[nrl] = malloc((nrow * ncol + NR_END) * sizeof(t)); \ |
---|
| 176 | if (m[nrl] == NULL) { \ |
---|
| 177 | nrerror("*** Error: allocation failure in %s\n", __func__); \ |
---|
| 178 | } \ |
---|
| 179 | m[nrl] += NR_END; \ |
---|
| 180 | m[nrl] -= ncl; \ |
---|
| 181 | for (int32_t i = nrl + 1; i <= nrh; i++) { \ |
---|
| 182 | m[i] = m[i - 1] + ncol; \ |
---|
| 183 | } \ |
---|
| 184 | /* return pointer to array of pointers to rows */ \ |
---|
| 185 | return m; \ |
---|
[772] | 186 | } |
---|
| 187 | |
---|
[821] | 188 | |
---|
| 189 | remote_type_matrix(int8_t); |
---|
| 190 | remote_type_matrix(uint8_t); |
---|
| 191 | remote_type_matrix(int16_t); |
---|
| 192 | remote_type_matrix(uint16_t); |
---|
| 193 | remote_type_matrix(int32_t); |
---|
| 194 | remote_type_matrix(uint32_t); |
---|
| 195 | remote_type_matrix(int64_t); |
---|
| 196 | remote_type_matrix(uint64_t); |
---|
| 197 | remote_type_matrix(float); |
---|
| 198 | remote_type_matrix(double); |
---|
| 199 | remote_type_matrix(void_p); |
---|
| 200 | remote_type_matrix(rgb8); |
---|
| 201 | remote_type_matrix(rgbx8); |
---|
| 202 | remote_type_matrix(rgb32); |
---|
| 203 | remote_type_matrix(rgbx32); |
---|
| 204 | remote_type_matrix(complex32); |
---|
| 205 | remote_type_matrix(complex64); |
---|
| 206 | |
---|
| 207 | #endif |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | #undef free_type_matrix |
---|
| 211 | #define free_type_matrix(t) \ |
---|
| 212 | void short_name(t,free_,matrix)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch) \ |
---|
| 213 | { \ |
---|
| 214 | free((FREE_ARG) (m[nrl] + ncl - NR_END)); \ |
---|
| 215 | free((FREE_ARG) (m + nrl - NR_END)); \ |
---|
[772] | 216 | } |
---|
| 217 | |
---|
[821] | 218 | free_type_matrix(int8_t); |
---|
| 219 | free_type_matrix(uint8_t); |
---|
| 220 | free_type_matrix(int16_t); |
---|
| 221 | free_type_matrix(uint16_t); |
---|
| 222 | free_type_matrix(int32_t); |
---|
| 223 | free_type_matrix(uint32_t); |
---|
| 224 | free_type_matrix(int64_t); |
---|
| 225 | free_type_matrix(uint64_t); |
---|
| 226 | free_type_matrix(float); |
---|
| 227 | free_type_matrix(double); |
---|
| 228 | free_type_matrix(void_p); |
---|
| 229 | free_type_matrix(rgb8); |
---|
| 230 | free_type_matrix(rgbx8); |
---|
| 231 | free_type_matrix(rgb32); |
---|
| 232 | free_type_matrix(rgbx32); |
---|
| 233 | free_type_matrix(complex32); |
---|
| 234 | free_type_matrix(complex64); |
---|
| 235 | free_type_matrix(si16Point); |
---|
| 236 | free_type_matrix(ui16Point); |
---|
| 237 | free_type_matrix(si32Point); |
---|
| 238 | free_type_matrix(ui32Point); |
---|
| 239 | free_type_matrix(f32Point); |
---|
| 240 | free_type_matrix(si16Triplet); |
---|
| 241 | free_type_matrix(ui16Triplet); |
---|
| 242 | free_type_matrix(si32Triplet); |
---|
| 243 | free_type_matrix(ui32Triplet); |
---|
| 244 | free_type_matrix(f32Triplet); |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | // Local Variables: |
---|
| 249 | // tab-width: 4 |
---|
| 250 | // c-basic-offset: 4 |
---|
| 251 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 252 | // indent-tabs-mode: nil |
---|
| 253 | // End: |
---|
| 254 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 255 | |
---|