source: soft/giet_vm/applications/rosenfeld/nrc2/src/nralloc2.c @ 772

Last change on this file since 772 was 772, checked in by meunier, 8 years ago
  • Ajout de l'application rosenfeld
  • Changement du nom du flag O_CREATE en O_CREAT
File size: 37.6 KB
Line 
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// #include <memory.h> // memcpy
33
34#include "mypredef.h"
35#include "nrtype.h"
36#include "nrdef.h"
37#include "nrmacro.h"
38#include "nrkernel.h"
39
40#include "nralloc1.h"
41#include "nralloc2.h"
42//#include "nrarith.h"
43
44/*
45 * --------------
46 * --- matrix ---
47 * --------------
48 */
49/* ------------------------------------------------ */
50float** matrix(long nrl, long nrh, long ncl, long nch)
51/* ------------------------------------------------ */
52{
53    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
54    float **m;
55   
56    /* allocate pointers to rows */
57    m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
58    if (!m) nrerror("allocation failure 1 in matrix()");
59    m += NR_END;
60    m -= nrl;
61   
62   
63    /* allocate rows and set pointers to them */
64    m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
65    if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
66    m[nrl] += NR_END;
67    m[nrl] -= ncl;
68   
69    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
70   
71    /* return pointer to array of pointers to rows */
72    return m;
73}
74/* -------------------------------------------------------------- */
75IMAGE_EXPORT(byte**) bmatrix(long nrl, long nrh, long ncl, long nch)
76/* -------------------------------------------------------------- */
77{
78    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
79    byte **m;
80   
81    /* allocate pointers to rows */
82    m=(byte **) malloc((size_t)((nrow+NR_END)*sizeof(byte*)));
83    if (!m) nrerror("allocation failure 1 in bmatrix()");
84    m += NR_END;
85    m -= nrl;
86   
87   
88    /* allocate rows and set pointers to them */
89    m[nrl]=(byte *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(byte)));
90    if (!m[nrl]) nrerror("allocation failure 2 in bmatrix()");
91    m[nrl] += NR_END;
92    m[nrl] -= ncl;
93   
94    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
95   
96    /* return pointer to array of pointers to rows */
97    return m;
98}/* ---------------------------------------------------------------- */
99IMAGE_EXPORT(sint8**) si8matrix(long nrl, long nrh, long ncl, long nch)
100/* ----------------------------------------------------------------- */
101{
102    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
103    sint8 **m;
104   
105    /* allocate pointers to rows */
106    m=(sint8 **) malloc((size_t)((nrow+NR_END)*sizeof(sint8*)));
107    if (!m) nrerror("allocation failure 1 in i8matrix()");
108    m += NR_END;
109    m -= nrl;
110   
111    /* allocate rows and set pointers to them */
112    m[nrl]=(sint8 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(sint8)));
113    if (!m[nrl]) nrerror("allocation failure 2 in i8matrix()");
114    m[nrl] += NR_END;
115    m[nrl] -= ncl;
116   
117    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
118   
119    /* return pointer to array of pointers to rows */
120    return m;
121}
122/* ----------------------------------------------------------------- */
123IMAGE_EXPORT(uint8**) ui8matrix(long nrl, long nrh, long ncl, long nch)
124/* ----------------------------------------------------------------- */
125{
126    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
127    uint8 **m;
128   
129    /* allocate pointers to rows */
130    m=(uint8 **) malloc((size_t)((nrow+NR_END)*sizeof(uint8*)));
131    if (!m) nrerror("allocation failure 1 in ui8matrix()");
132    m += NR_END;
133    m -= nrl;
134   
135    /* allocate rows and set pointers to them */
136    m[nrl]=(uint8 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(uint8)));
137    if (!m[nrl]) nrerror("allocation failure 2 in ui8matrix()");
138    m[nrl] += NR_END;
139    m[nrl] -= ncl;
140   
141    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
142   
143    /* return pointer to array of pointers to rows */
144    return m;
145}
146/* ------------------------------------------------------------------- */
147IMAGE_EXPORT(sint16**) si16matrix(long nrl, long nrh, long ncl, long nch)
148/* ------------------------------------------------------------------- */
149{
150    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
151    sint16 **m;
152   
153    /* allocate pointers to rows */
154    m=(sint16 **) malloc((size_t)((nrow+NR_END)*sizeof(sint16*)));
155    if (!m) nrerror("allocation failure 1 in si16matrix()");
156    m += NR_END;
157    m -= nrl;
158   
159    /* allocate rows and set pointers to them */
160    m[nrl]=(sint16 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(sint16)));
161    if (!m[nrl]) nrerror("allocation failure 2 in si16matrix()");
162    m[nrl] += NR_END;
163    m[nrl] -= ncl;
164   
165    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
166   
167    /* return pointer to array of pointers to rows */
168    return m;
169}
170/* ------------------------------------------------------------------- */
171IMAGE_EXPORT(uint16**) ui16matrix(long nrl, long nrh, long ncl, long nch)
172/* ------------------------------------------------------------------- */
173{
174    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
175    uint16 **m;
176   
177    /* allocate pointers to rows */
178    m=(uint16 **) malloc((size_t)((nrow+NR_END)*sizeof(uint16*)));
179    if (!m) nrerror("allocation failure 1 in ui16matrix()");
180    m += NR_END;
181    m -= nrl;
182   
183    /* allocate rows and set pointers to them */
184    m[nrl]=(uint16 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(uint16)));
185    if (!m[nrl]) nrerror("allocation failure 2 in ui16matrix()");
186    m[nrl] += NR_END;
187    m[nrl] -= ncl;
188   
189    for(i=nrl+1;i<=nrh;i++)
190        m[i]=m[i-1]+ncol;
191   
192    /* return pointer to array of pointers to rows */
193    return m;
194}
195/* ------------------------------------------------------------------- */
196IMAGE_EXPORT(sint32**) si32matrix(long nrl, long nrh, long ncl, long nch)
197/* ------------------------------------------------------------------- */
198/* allocate a int32 matrix with subscript range m[nrl..nrh][ncl..nch] */
199{
200    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
201    sint32 **m;
202   
203    /* allocate pointers to rows */
204    m=(sint32 **) malloc((size_t)((nrow+NR_END)*sizeof(sint32*)));
205    if (!m) nrerror("allocation failure 1 in si32matrix()");
206    m += NR_END;
207    m -= nrl;
208   
209    /* allocate rows and set pointers to them */
210    m[nrl]=(sint32 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(sint32)));
211    if (!m[nrl]) nrerror("allocation failure 2 in si32matrix()");
212    m[nrl] += NR_END;
213    m[nrl] -= ncl;
214   
215    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
216   
217    /* return pointer to array of pointers to rows */
218    return m;
219}
220/* ------------------------------------------------------------------- */
221IMAGE_EXPORT(uint32**) ui32matrix(long nrl, long nrh, long ncl, long nch)
222/* ------------------------------------------------------------------- */
223/* allocate a uint32 matrix with subscript range m[nrl..nrh][ncl..nch] */
224{
225    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
226    uint32 **m;
227   
228    /* allocate pointers to rows */
229    m=(uint32 **) malloc((size_t)((nrow+NR_END)*sizeof(uint32*)));
230    if (!m) nrerror("allocation failure 1 in ui32matrix()");
231    m += NR_END;
232    m -= nrl;
233   
234    /* allocate rows and set pointers to them */
235    m[nrl]=(uint32 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(uint32)));
236    if (!m[nrl]) nrerror("allocation failure 2 in ui32matrix()");
237    m[nrl] += NR_END;
238    m[nrl] -= ncl;
239   
240    for(i=nrl+1;i<=nrh;i++)
241        m[i]=m[i-1]+ncol;
242   
243    /* return pointer to array of pointers to rows */
244    return m;
245}
246/* ------------------------------------------------------------------- */
247IMAGE_EXPORT(sint64**) si64matrix(long nrl, long nrh, long ncl, long nch)
248/* ------------------------------------------------------------------- */
249/* allocate a int64 matrix with subscript range m[nrl..nrh][ncl..nch] */
250{
251    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
252    sint64 **m;
253   
254    /* allocate pointers to rows */
255    m=(sint64 **) malloc((size_t)((nrow+NR_END)*sizeof(sint64*)));
256    if (!m) nrerror("allocation failure 1 in si64matrix()");
257    m += NR_END;
258    m -= nrl;
259   
260    /* allocate rows and set pointers to them */
261    m[nrl]=(sint64 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(sint64)));
262    if (!m[nrl]) nrerror("allocation failure 2 in si64matrix()");
263    m[nrl] += NR_END;
264    m[nrl] -= ncl;
265   
266    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
267   
268    /* return pointer to array of pointers to rows */
269    return m;
270}
271/* ------------------------------------------------------------------- */
272IMAGE_EXPORT(uint64**) ui64matrix(long nrl, long nrh, long ncl, long nch)
273/* ------------------------------------------------------------------- */
274/* allocate a int64 matrix with subscript range m[nrl..nrh][ncl..nch] */
275{
276    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
277    uint64 **m;
278   
279    /* allocate pointers to rows */
280    m=(uint64 **) malloc((size_t)((nrow+NR_END)*sizeof(uint64*)));
281    if (!m) nrerror("allocation failure 1 in ui64matrix()");
282    m += NR_END;
283    m -= nrl;
284   
285    /* allocate rows and set pointers to them */
286    m[nrl]=(uint64 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(uint64)));
287    if (!m[nrl]) nrerror("allocation failure 2 in ui64matrix()");
288    m[nrl] += NR_END;
289    m[nrl] -= ncl;
290   
291    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
292   
293    /* return pointer to array of pointers to rows */
294    return m;
295}
296/* ------------------------------------------------------------------- */
297IMAGE_EXPORT(float32**) f32matrix(long nrl, long nrh, long ncl, long nch)
298/* ------------------------------------------------------------------- */
299/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
300{
301    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
302    float32 **m;
303   
304    /* allocate pointers to rows */
305    m=(float32 **) malloc((size_t)((nrow+NR_END)*sizeof(float32*)));
306    if (!m) nrerror("allocation failure 1 in f32matrix()");
307    m += NR_END;
308    m -= nrl;
309   
310    /* allocate rows and set pointers to them */
311    m[nrl]=(float32 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float32)));
312    if (!m[nrl]) nrerror("allocation failure 2 in f32matrix()");
313    m[nrl] += NR_END;
314    m[nrl] -= ncl;
315   
316    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
317   
318    /* return pointer to array of pointers to rows */
319    return m;
320}/* ------------------------------------------------------------------- */
321IMAGE_EXPORT(float64**) f64matrix(long nrl, long nrh, long ncl, long nch)
322/* ------------------------------------------------------------------- */
323
324/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
325{
326    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
327    float64 **m;
328   
329    /* allocate pointers to rows */
330    m=(float64 **) malloc((size_t)((nrow+NR_END)*sizeof(float64*)));
331    if (!m) nrerror("allocation failure 1 in f64matrix()");
332    m += NR_END;
333    m -= nrl;
334   
335    /* allocate rows and set pointers to them */
336    m[nrl]=(float64 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float64)));
337    if (!m[nrl]) nrerror("allocation failure 2 in f64matrix()");
338    m[nrl] += NR_END;
339    m[nrl] -= ncl;
340   
341    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
342    /* return pointer to array of pointers to rows */
343    return m;
344}
345/* --------------------------------------------------------------------- */
346IMAGE_EXPORT(complex32**) c32matrix(long nrl, long nrh, long ncl, long nch)
347/* --------------------------------------------------------------------- */
348/* allocate a complex32 matrix with subscript range m[nrl..nrh][ncl..nch] */
349{
350    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
351    complex32 **m;
352   
353    /* allocate pointers to rows */
354    m=(complex32 **) malloc((size_t)((nrow+NR_END)*sizeof(complex32*)));
355    if (!m) nrerror("allocation failure 1 in c32matrix()");
356    m += NR_END;
357    m -= nrl;
358   
359    /* allocate rows and set pointers to them */
360    m[nrl]=(complex32 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(complex32)));
361    if (!m[nrl]) nrerror("allocation failure 2 in c32matrix()");
362    m[nrl] += NR_END;
363    m[nrl] -= ncl;
364   
365    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
366   
367    /* return pointer to array of pointers to rows */
368    return m;
369}
370/* --------------------------------------------------------------------- */
371IMAGE_EXPORT(complex64**) c64matrix(long nrl, long nrh, long ncl, long nch)
372/* --------------------------------------------------------------------- */
373/* allocate a complex32 matrix with subscript range m[nrl..nrh][ncl..nch] */
374{
375    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
376    complex64 **m;
377   
378    /* allocate pointers to rows */
379    m=(complex64 **) malloc((size_t)((nrow+NR_END)*sizeof(complex64*)));
380    if (!m) nrerror("allocation failure 1 in c64matrix()");
381    m += NR_END;
382    m -= nrl;
383   
384    /* allocate rows and set pointers to them */
385    m[nrl]=(complex64 *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(complex64)));
386    if (!m[nrl]) nrerror("allocation failure 2 in c64matrix()");
387    m[nrl] += NR_END;
388    m[nrl] -= ncl;
389   
390    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
391   
392    /* return pointer to array of pointers to rows */
393    return m;
394}
395/* ----------------------------------------------------------------- */
396IMAGE_EXPORT(rgb8**) rgb8matrix(long nrl, long nrh, long ncl, long nch)
397/* ----------------------------------------------------------------- */
398/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
399{
400    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
401    rgb8 **m;
402   
403    /* allocate pointers to rows */
404    m=(rgb8**) malloc((size_t)((nrow+NR_END)*sizeof(rgb8*)));
405    if (!m) nrerror("allocation failure 1 in rgb8matrix()");
406    m += NR_END;
407    m -= nrl;
408   
409    /* allocate rows and set pointers to them */
410    m[nrl]=(rgb8*) malloc((size_t)((nrow*ncol+NR_END)*sizeof(rgb8)));
411    if (!m[nrl]) nrerror("allocation failure 2 in rgb8matrix()");
412    m[nrl] += NR_END;
413    m[nrl] -= ncl;
414   
415    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
416   
417    /* return pointer to array of pointers to rows */
418    return m;
419}
420/* ------------------------------------------------------------------- */
421IMAGE_EXPORT(rgbx8**) rgbx8matrix(long nrl, long nrh, long ncl, long nch)
422/* ------------------------------------------------------------------- */
423/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
424{
425    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
426    rgbx8 **m;
427   
428    /* allocate pointers to rows */
429    m=(rgbx8**) malloc((size_t)((nrow+NR_END)*sizeof(rgbx8*)));
430    if (!m) nrerror("allocation failure 1 in rgbx8matrix()");
431    m += NR_END;
432    m -= nrl;
433   
434    /* allocate rows and set pointers to them */
435    m[nrl]=(rgbx8*) malloc((size_t)((nrow*ncol+NR_END)*sizeof(rgbx8)));
436    if (!m[nrl]) nrerror("allocation failure 2 in rgbx8matrix()");
437    m[nrl] += NR_END;
438    m[nrl] -= ncl;
439   
440    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
441   
442    /* return pointer to array of pointers to rows */
443    return m;
444}
445/* ------------------------------------------------------------------- */
446IMAGE_EXPORT(rgb32**) rgb32matrix(long nrl, long nrh, long ncl, long nch)
447/* ------------------------------------------------------------------- */
448/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
449{
450    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
451    rgb32 **m;
452   
453    /* allocate pointers to rows */
454    m=(rgb32**) malloc((size_t)((nrow+NR_END)*sizeof(rgb32*)));
455    if (!m) nrerror("allocation failure 1 in rgb32matrix()");
456    m += NR_END;
457    m -= nrl;
458   
459    /* allocate rows and set pointers to them */
460    m[nrl]=(rgb32*) malloc((size_t)((nrow*ncol+NR_END)*sizeof(rgb32)));
461    if (!m[nrl]) nrerror("allocation failure 2 in rgb32matrix()");
462    m[nrl] += NR_END;
463    m[nrl] -= ncl;
464   
465    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
466   
467    /* return pointer to array of pointers to rows */
468    return m;
469}
470/* --------------------------------------------------------------------- */
471IMAGE_EXPORT(rgbx32**) rgbx32matrix(long nrl, long nrh, long ncl, long nch)
472/* --------------------------------------------------------------------- */
473/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
474{
475    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
476    rgbx32 **m;
477   
478    /* allocate pointers to rows */
479    m=(rgbx32**) malloc((size_t)((nrow+NR_END)*sizeof(rgbx32*)));
480    if (!m) nrerror("allocation failure 1 in rgbx32matrix()");
481    m += NR_END;
482    m -= nrl;
483   
484    /* allocate rows and set pointers to them */
485    m[nrl]=(rgbx32*) malloc((size_t)((nrow*ncol+NR_END)*sizeof(rgbx32)));
486    if (!m[nrl]) nrerror("allocation failure 2 in rgbx32matrix()");
487    m[nrl] += NR_END;
488    m[nrl] -= ncl;
489   
490    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
491   
492    /* return pointer to array of pointers to rows */
493    return m;
494}
495
496/*
497 * ---------------
498 * --- matrix0 ---
499 * ---------------
500 */
501
502/* --------------------------------------------------------------- */
503IMAGE_EXPORT(byte**) bmatrix0(long nrl, long nrh, long ncl, long nch)
504/* --------------------------------------------------------------- */
505/* allocate a byte matrix with subscript range m[nrl..nrh][ncl..nch] */
506{
507    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
508    byte **m;
509   
510    /* allocate pointers to rows */
511    m=(byte **) malloc((size_t)((nrow+NR_END)*sizeof(byte*)));
512    if (!m) nrerror("allocation failure 1 in bmatrix0()");
513    m += NR_END;
514    m -= nrl;
515   
516    /* allocate rows and set pointers to them */
517    m[nrl]=(byte *) calloc((size_t)(nrow*ncol+NR_END),sizeof(byte));
518    if (!m[nrl]) nrerror("allocation failure 2 in bmatrix0()");
519    m[nrl] += NR_END;
520    m[nrl] -= ncl;
521   
522    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
523   
524    /* return pointer to array of pointers to rows */
525    return m;
526}
527/* ------------------------------------------------------------------ */
528IMAGE_EXPORT(sint8**) si8matrix0(long nrl, long nrh, long ncl, long nch)
529/* ------------------------------------------------------------------ */
530{
531    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
532    sint8 **m;
533   
534    /* allocate pointers to rows */
535    m=(sint8 **) malloc((size_t)((nrow+NR_END)*sizeof(sint8*)));
536    if (!m) nrerror("allocation failure 1 in i8matrix0()");
537    m += NR_END;
538    m -= nrl;
539   
540    /* allocate rows and set pointers to them */
541    m[nrl]=(sint8 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(sint8));
542    if (!m[nrl]) nrerror("allocation failure 2 in imatrix0()");
543    m[nrl] += NR_END;
544    m[nrl] -= ncl;
545   
546    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
547   
548    /* return pointer to array of pointers to rows */
549    return m;
550}
551/* ------------------------------------------------------------------ */
552IMAGE_EXPORT(uint8**) ui8matrix0(long nrl, long nrh, long ncl, long nch)
553/* ------------------------------------------------------------------ */
554{
555    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
556    uint8 **m;
557   
558    /* allocate pointers to rows */
559    m=(uint8 **) malloc((size_t)((nrow+NR_END)*sizeof(uint8*)));
560    if (!m) nrerror("allocation failure 1 in ui8matrix0()");
561    m += NR_END;
562    m -= nrl;
563   
564    /* allocate rows and set pointers to them */
565    m[nrl]=(uint8 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(uint8));
566    if (!m[nrl]) nrerror("allocation failure 2 in ui8matrix0()");
567    m[nrl] += NR_END;
568    m[nrl] -= ncl;
569   
570    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
571   
572    /* return pointer to array of pointers to rows */
573    return m;
574}
575/* -------------------------------------------------------------------- */
576IMAGE_EXPORT(sint16**) si16matrix0(long nrl, long nrh, long ncl, long nch)
577/* -------------------------------------------------------------------- */
578/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
579{
580    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
581    sint16 **m;
582   
583    /* allocate pointers to rows */
584    m=(sint16 **) malloc((size_t)((nrow+NR_END)*sizeof(sint16*)));
585    if (!m) nrerror("allocation failure 1 in si16matrix0()");
586    m += NR_END;
587    m -= nrl;
588   
589    /* allocate rows and set pointers to them */
590    m[nrl]=(sint16 *) calloc(nrow*ncol, sizeof(int16));
591    if (!m[nrl]) nrerror("allocation failure 2 in si16matrix0()");
592    m[nrl] += NR_END;
593    m[nrl] -= ncl;
594   
595    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
596   
597    /* return pointer to array of pointers to rows */
598    return m;
599}
600/* -------------------------------------------------------------------- */
601IMAGE_EXPORT(uint16**) ui16matrix0(long nrl, long nrh, long ncl, long nch)
602/* -------------------------------------------------------------------- */
603/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
604{
605    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
606    uint16 **m;
607   
608    /* allocate pointers to rows */
609    m=(uint16 **) malloc((size_t)((nrow+NR_END)*sizeof(uint16*)));
610    if (!m) nrerror("allocation failure 1 in ui16matrix0()");
611    m += NR_END;
612    m -= nrl;
613   
614    /* allocate rows and set pointers to them */
615    m[nrl]=(uint16 *) calloc(nrow*ncol, sizeof(uint16));
616    if (!m[nrl]) nrerror("allocation failure 2 in ui16matrix0()");
617    m[nrl] += NR_END;
618    m[nrl] -= ncl;
619   
620    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
621   
622    /* return pointer to array of pointers to rows */
623    return m;
624}
625/* -------------------------------------------------------------------- */
626IMAGE_EXPORT(sint32**) si32matrix0(long nrl, long nrh, long ncl, long nch)
627/* -------------------------------------------------------------------- */
628/* allocate a int32 matrix with subscript range m[nrl..nrh][ncl..nch] */
629{
630    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
631    sint32 **m;
632   
633    /* allocate pointers to rows */
634    m=(sint32 **) malloc((size_t)((nrow+NR_END)*sizeof(sint32*)));
635    if (!m) nrerror("allocation failure 1 in si32matrix0()");
636    m += NR_END;
637    m -= nrl;
638   
639    /* allocate rows and set pointers to them */
640    m[nrl]=(sint32 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(sint32));
641    if (!m[nrl]) nrerror("allocation failure 2 in si32matrix0()");
642    m[nrl] += NR_END;
643    m[nrl] -= ncl;
644   
645    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
646   
647    /* return pointer to array of pointers to rows */
648    return m;
649}
650/* -------------------------------------------------------------------- */
651IMAGE_EXPORT(uint32**) ui32matrix0(long nrl, long nrh, long ncl, long nch)
652/* -------------------------------------------------------------------- */
653/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
654{
655    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; 
656    uint32 **m;
657    /* allocate pointers to rows */
658    m=(uint32 **) malloc((size_t)((nrow+NR_END)*sizeof(uint32*)));
659    if (!m) nrerror("allocation failure 1 in ui32matrix0()");
660    m += NR_END;
661    m -= nrl;
662
663    /* allocate rows and set pointers to them */
664    m[nrl]=(uint32 *) calloc((size_t)(nrow*ncol+NR_END), sizeof(uint32));
665    if (!m[nrl]) nrerror("allocation failure 2 in ui32matrix0()");
666    m[nrl] += NR_END;
667    m[nrl] -= ncl;
668   
669    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
670    /* return pointer to array of pointers to rows */
671    return m;   
672}
673/* -------------------------------------------------------------------- */
674IMAGE_EXPORT(sint64**) si64matrix0(long nrl, long nrh, long ncl, long nch)
675/* -------------------------------------------------------------------- */
676{
677    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
678    sint64 **m;
679   
680    /* allocate pointers to rows */
681    m=(sint64 **) malloc((size_t)((nrow+NR_END)*sizeof(sint64*)));
682    if (!m) nrerror("allocation failure 1 in si64matrix0()");
683    m += NR_END;
684    m -= nrl;
685   
686    /* allocate rows and set pointers to them */
687    m[nrl]=(sint64 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(sint64));
688    if (!m[nrl]) nrerror("allocation failure 2 in si64matrix0()");
689    m[nrl] += NR_END;
690    m[nrl] -= ncl;
691   
692    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
693   
694    /* return pointer to array of pointers to rows */
695    return m;
696}
697/* -------------------------------------------------------------------- */
698IMAGE_EXPORT(uint64**) ui64matrix0(long nrl, long nrh, long ncl, long nch)
699/* -------------------------------------------------------------------- */
700{
701    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
702    uint64 **m;
703   
704    /* allocate pointers to rows */
705    m=(uint64 **) malloc((size_t)((nrow+NR_END)*sizeof(uint64*)));
706    if (!m) nrerror("allocation failure 1 in i64matrix0()");
707    m += NR_END;
708    m -= nrl;
709   
710    /* allocate rows and set pointers to them */
711    m[nrl]=(uint64 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(uint64));
712    if (!m[nrl]) nrerror("allocation failure 2 in i64matrix0()");
713    m[nrl] += NR_END;
714    m[nrl] -= ncl;
715   
716    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
717   
718    /* return pointer to array of pointers to rows */
719    return m;
720}
721/* -------------------------------------------------------------------- */
722IMAGE_EXPORT(float32**) f32matrix0(long nrl, long nrh, long ncl, long nch)
723/* -------------------------------------------------------------------- */
724{
725    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
726    float32 **m;
727   
728    /* allocate pointers to rows */
729    m=(float32 **) malloc((size_t)((nrow+NR_END)*sizeof(float32*)));
730    if (!m) nrerror("allocation failure 1 in f32matrix0()");
731    m += NR_END;
732    m -= nrl;
733   
734    /* allocate rows and set pointers to them */
735    m[nrl]=(float32 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(float32));
736    if (!m[nrl]) nrerror("allocation failure 2 in f32matrix0()");
737    m[nrl] += NR_END;
738    m[nrl] -= ncl;
739   
740    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
741   
742    /* return pointer to array of pointers to rows */
743    return m;
744}
745/* -------------------------------------------------------------------- */
746IMAGE_EXPORT(float64**) f64matrix0(long nrl, long nrh, long ncl, long nch)
747/* -------------------------------------------------------------------- */
748{
749    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
750    float64 **m;
751   
752    /* allocate pointers to rows */
753    m=(float64 **) malloc((size_t)((nrow+NR_END)*sizeof(float64*)));
754    if (!m) nrerror("allocation failure 1 in f64matrix0()");
755    m += NR_END;
756    m -= nrl;
757   
758    /* allocate rows and set pointers to them */
759    m[nrl]=(float64 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(float64));
760    if (!m[nrl]) nrerror("allocation failure 2 in f64matrix0()");
761    m[nrl] += NR_END;
762    m[nrl] -= ncl;
763   
764    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
765    /* return pointer to array of pointers to rows */
766    return m;
767}
768/* ---------------------------------------------------------------------- */
769IMAGE_EXPORT(complex32**) c32matrix0(long nrl, long nrh, long ncl, long nch)
770/* ---------------------------------------------------------------------- */
771{
772    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
773    complex32 **m;
774   
775    /* allocate pointers to rows */
776    m=(complex32 **) malloc((size_t)((nrow+NR_END)*sizeof(complex32*)));
777    if (!m) nrerror("allocation failure 1 in c32matrix0()");
778    m += NR_END;
779    m -= nrl;
780   
781    /* allocate rows and set pointers to them */
782    m[nrl]=(complex32 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(complex32));
783    if (!m[nrl]) nrerror("allocation failure 2 in c32matrix0()");
784    m[nrl] += NR_END;
785    m[nrl] -= ncl;
786   
787    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
788   
789    /* return pointer to array of pointers to rows */
790    return m;
791}
792/* ---------------------------------------------------------------------- */
793IMAGE_EXPORT(complex64**) c64matrix0(long nrl, long nrh, long ncl, long nch)
794/* ---------------------------------------------------------------------- */
795{
796    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
797    complex64 **m;
798   
799    /* allocate pointers to rows */
800    m=(complex64 **) malloc((size_t)((nrow+NR_END)*sizeof(complex64*)));
801    if (!m) nrerror("allocation failure 1 in c64matrix0()");
802    m += NR_END;
803    m -= nrl;
804   
805    /* allocate rows and set pointers to them */
806    m[nrl]=(complex64 *) calloc((size_t)(nrow*ncol+NR_END),sizeof(complex64));
807    if (!m[nrl]) nrerror("allocation failure 2 in c64matrix0()");
808    m[nrl] += NR_END;
809    m[nrl] -= ncl;
810   
811    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
812   
813    /* return pointer to array of pointers to rows */
814    return m;
815}
816/* ------------------------------------------------------------------ */
817IMAGE_EXPORT(rgb8**) rgb8matrix0(long nrl, long nrh, long ncl, long nch)
818/* ------------------------------------------------------------------ */
819/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
820{
821    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
822    rgb8 **m;
823   
824    /* allocate pointers to rows */
825    m=(rgb8**) malloc((size_t)((nrow+NR_END)*sizeof(rgb8*)));
826    if (!m) nrerror("allocation failure 1 in rgb8matrix0()");
827    m += NR_END;
828    m -= nrl;
829   
830    /* allocate rows and set pointers to them */
831    m[nrl]=(rgb8*) calloc((size_t)(nrow*ncol+NR_END), sizeof(rgb8));
832    if (!m[nrl]) nrerror("allocation failure 2 in rgb8matrix0()");
833    m[nrl] += NR_END;
834    m[nrl] -= ncl;
835   
836    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
837   
838    /* return pointer to array of pointers to rows */
839    return m;
840}
841/* -------------------------------------------------------------------- */
842IMAGE_EXPORT(rgbx8**) rgbx8matrix0(long nrl, long nrh, long ncl, long nch)
843/* -------------------------------------------------------------------- */
844/* allocate a sint16  matrix with subscript range m[nrl..nrh][ncl..nch] */
845{
846    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
847    rgbx8 **m;
848   
849    /* allocate pointers to rows */
850    m=(rgbx8**) malloc((size_t)((nrow+NR_END)*sizeof(rgbx8*)));
851    if (!m) nrerror("allocation failure 1 in rgbx8matrix()");
852    m += NR_END;
853    m -= nrl;
854   
855    /* allocate rows and set pointers to them */
856    m[nrl]=(rgbx8*) calloc((size_t)(nrow*ncol+NR_END), sizeof(rgbx8));
857    if (!m[nrl]) nrerror("allocation failure 2 in rgbx8matrix()");
858    m[nrl] += NR_END;
859    m[nrl] -= ncl;
860   
861    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
862   
863    /* return pointer to array of pointers to rows */
864    return m;
865}
866/* -------------------------------------------------------------------- */
867IMAGE_EXPORT(rgb32**) rgb32matrix0(long nrl, long nrh, long ncl, long nch)
868/* -------------------------------------------------------------------- */
869/* allocate a rgb32 matrix with subscript range m[nrl..nrh][ncl..nch] */
870{
871    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
872    rgb32 **m;
873   
874    /* allocate pointers to rows */
875    m=(rgb32**) malloc((size_t)((nrow+NR_END)*sizeof(rgb32*)));
876    if (!m) nrerror("allocation failure 1 in rgb32matrix0()");
877    m += NR_END;
878    m -= nrl;
879   
880    /* allocate rows and set pointers to them */
881    m[nrl]=(rgb32*) calloc((nrow*ncol+NR_END), sizeof(rgb32));
882    if (!m[nrl]) nrerror("allocation failure 2 in rgb32matrix0()");
883    m[nrl] += NR_END;
884    m[nrl] -= ncl;
885   
886    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
887   
888    /* return pointer to array of pointers to rows */
889    return m;
890}
891/* ---------------------------------------------------------------------- */
892IMAGE_EXPORT(rgbx32**) rgbx32matrix0(long nrl, long nrh, long ncl, long nch)
893/* ---------------------------------------------------------------------- */
894/* allocate a rgb32 matrix with subscript range m[nrl..nrh][ncl..nch] */
895{
896    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
897    rgbx32 **m;
898   
899    /* allocate pointers to rows */
900    m=(rgbx32**) malloc((size_t)((nrow+NR_END)*sizeof(rgbx32*)));
901    if (!m) nrerror("allocation failure 1 in rgbx32matrix()");
902    m += NR_END;
903    m -= nrl;
904   
905    /* allocate rows and set pointers to them */
906    m[nrl]=(rgbx32*) calloc((size_t)(nrow*ncol+NR_END), sizeof(rgbx32));
907    if (!m[nrl]) nrerror("allocation failure 2 in rgbx32matrix()");
908    m[nrl] += NR_END;
909    m[nrl] -= ncl;
910   
911    for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
912   
913    /* return pointer to array of pointers to rows */
914    return m;
915}
916/*
917 * -------------------
918 * --- free_matrix ---
919 * -------------------
920 */ 
921
922/* ------------------------------------------------------------- */
923void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
924/* ------------------------------------------------------------- */
925/* free a float matrix allocated by matrix() */
926{
927    free((FREE_ARG) (m[nrl]+ncl-NR_END));
928    free((FREE_ARG) (m+nrl-NR_END));
929}
930/* --------------------------------------------------------------------------- */
931IMAGE_EXPORT(void) free_bmatrix(byte **m, long nrl, long nrh, long ncl, long nch)
932/* --------------------------------------------------------------------------- */
933{
934    free((FREE_ARG) (m[nrl]+ncl-NR_END));
935    free((FREE_ARG) (m+nrl-NR_END));
936}
937/* ------------------------------------------------------------------------------ */
938IMAGE_EXPORT(void) free_si8matrix(sint8 **m, long nrl, long nrh, long ncl, long nch)
939/* ------------------------------------------------------------------------------ */
940{
941    free((FREE_ARG) (m[nrl]+ncl-NR_END));
942    free((FREE_ARG) (m+nrl-NR_END));
943}
944/* ------------------------------------------------------------------------------ */
945IMAGE_EXPORT(void) free_ui8matrix(uint8 **m, long nrl, long nrh, long ncl, long nch)
946/* ------------------------------------------------------------------------------ */
947{
948    free((FREE_ARG) (m[nrl]+ncl-NR_END));
949    free((FREE_ARG) (m+nrl-NR_END));
950}/* -------------------------------------------------------------------------------- */
951IMAGE_EXPORT(void) free_si16matrix(sint16 **m, long nrl, long nrh, long ncl, long nch)
952/* -------------------------------------------------------------------------------- */
953{ 
954    free((FREE_ARG) (m[nrl]+ncl-NR_END));
955    free((FREE_ARG) (m+nrl-NR_END));
956}
957/* -------------------------------------------------------------------------------- */
958IMAGE_EXPORT(void) free_ui16matrix(uint16 **m, long nrl, long nrh, long ncl, long nch)
959/* -------------------------------------------------------------------------------- */
960{ 
961    free((FREE_ARG) (m[nrl]+ncl-NR_END));
962    free((FREE_ARG) (m+nrl-NR_END));
963}
964/* -------------------------------------------------------------------------------- */
965IMAGE_EXPORT(void) free_si32matrix(sint32 **m, long nrl, long nrh, long ncl, long nch)
966/* -------------------------------------------------------------------------------- */
967{ 
968    free((FREE_ARG) (m[nrl]+ncl-NR_END));
969    free((FREE_ARG) (m+nrl-NR_END));
970}
971/* -------------------------------------------------------------------------------- */
972IMAGE_EXPORT(void) free_ui32matrix(uint32 **m, long nrl, long nrh, long ncl, long nch)
973/* -------------------------------------------------------------------------------- */
974{ 
975    free((FREE_ARG) (m[nrl]+ncl-NR_END));
976    free((FREE_ARG) (m+nrl-NR_END));
977}
978/* -------------------------------------------------------------------------------- */
979IMAGE_EXPORT(void) free_si64matrix(sint64 **m, long nrl, long nrh, long ncl, long nch)
980/* -------------------------------------------------------------------------------- */
981{ 
982    free((FREE_ARG) (m[nrl]+ncl-NR_END));
983    free((FREE_ARG) (m+nrl-NR_END));
984}
985/* -------------------------------------------------------------------------------- */
986IMAGE_EXPORT(void) free_ui64matrix(uint64 **m, long nrl, long nrh, long ncl, long nch)
987/* -------------------------------------------------------------------------------- */
988{ 
989    free((FREE_ARG) (m[nrl]+ncl-NR_END));
990    free((FREE_ARG) (m+nrl-NR_END));
991}
992/* -------------------------------------------------------------------------------- */
993IMAGE_EXPORT(void) free_f32matrix(float32 **m, long nrl, long nrh, long ncl, long nch)
994/* -------------------------------------------------------------------------------- */
995{
996    free((FREE_ARG) (m[nrl]+ncl-NR_END));
997    free((FREE_ARG) (m+nrl-NR_END));
998}
999/* -------------------------------------------------------------------------------- */
1000IMAGE_EXPORT(void) free_f64matrix(float64 **m, long nrl, long nrh, long ncl, long nch)
1001/* -------------------------------------------------------------------------------- */
1002{
1003    free((FREE_ARG) (m[nrl]+ncl-NR_END));
1004    free((FREE_ARG) (m+nrl-NR_END));
1005}
1006/* ---------------------------------------------------------------------------------- */
1007IMAGE_EXPORT(void) free_c32matrix(complex32 **m, long nrl, long nrh, long ncl, long nch)
1008/* ---------------------------------------------------------------------------------- */
1009{
1010    free((FREE_ARG) (m[nrl]+ncl-NR_END));
1011    free((FREE_ARG) (m+nrl-NR_END));
1012}
1013/* ---------------------------------------------------------------------------------- */
1014IMAGE_EXPORT(void) free_c64matrix(complex64 **m, long nrl, long nrh, long ncl, long nch)
1015/* ---------------------------------------------------------------------------------- */
1016{
1017    free((FREE_ARG) (m[nrl]+ncl-NR_END));
1018    free((FREE_ARG) (m+nrl-NR_END));
1019}
1020/* ------------------------------------------------------------------------------ */
1021IMAGE_EXPORT(void) free_rgb8matrix(rgb8 **m, long nrl, long nrh, long ncl, long nch)
1022/* ------------------------------------------------------------------------------ */
1023{
1024    free((FREE_ARG) (m[nrl]+ncl-NR_END));
1025    free((FREE_ARG) (m+nrl-NR_END));
1026}
1027/* -------------------------------------------------------------------------------- */
1028IMAGE_EXPORT(void) free_rgbx8matrix(rgbx8 **m, long nrl, long nrh, long ncl, long nch)
1029/* -------------------------------------------------------------------------------- */
1030{
1031    free((FREE_ARG) (m[nrl]+ncl-NR_END));
1032    free((FREE_ARG) (m+nrl-NR_END));
1033}
1034/* ------------------------------------------------------------------------------ */
1035IMAGE_EXPORT(void) free_rgb32matrix(rgb32 **m, long nrl, long nrh, long ncl, long nch)
1036/* ------------------------------------------------------------------------------ */
1037{
1038    free((FREE_ARG) (m[nrl]+ncl-NR_END));
1039    free((FREE_ARG) (m+nrl-NR_END));
1040}
1041/* ---------------------------------------------------------------------------------- */
1042IMAGE_EXPORT(void) free_rgbx32matrix(rgbx32 **m, long nrl, long nrh, long ncl, long nch)
1043/* ---------------------------------------------------------------------------------- */
1044{
1045    free((FREE_ARG) (m[nrl]+ncl-NR_END));
1046    free((FREE_ARG) (m+nrl-NR_END));
1047}
Note: See TracBrowser for help on using the repository browser.