1 /* @(#) Programs for allocating and freeing matrices
2 * @(#) pages 705- of numerical recipes in C 1998
3 * @(#)
4 */
5
6 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif /*HAVE_CONFIG_H*/
9 #include <vips/intl.h>
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14
15 #include <vips/vips.h>
16 #include <vips/vips7compat.h>
17
18 #define TINY 1.0e-200
19
20 /* @(#) Allocates and returns an pointer at the beginning of
21 * @(#) an integer array array[nl,nh] or
22 * @(#) float array array[nl,nh] or
23 * @(#) double array array[nl,nh]
24 * @(#)
25 * @(#) Right call
26 * @(#) int *im_ivector(nl, nh)
27 * @(#) int nl, nh;
28 * @(#) returns a pointer to an int array or NULL on error
29 * @(#)
30 * @(#) Right call
31 * @(#) float *im_fvector(nl, nh)
32 * @(#) int nl, nh;
33 * @(#) returns a pointer to a float array or NULL on error
34 * @(#)
35 * @(#) Right call
36 * @(#) double *im_dvector(nl, nh)
37 * @(#) int nl, nh;
38 * @(#) returns a pointer to a double array or NULL on error
39 * @(#)
40 * @(#) The following functions free the array allocated by the functions above
41 * @(#)
42 * @(#) void im_free_ivector(v, nl, nh)
43 * @(#) int *v;
44 * @(#) int nl, nh;
45 * @(#)
46 * @(#) void im_free_fvector(v, nl, nh)
47 * @(#) float *v;
48 * @(#) int nl, nh;
49 * @(#)
50 * @(#) void im_free_dvector(v, nl, nh)
51 * @(#) double *v;
52 * @(#) int nl, nh;
53 * @(#)
54 */
55
56 int *
im_ivector(int nl,int nh)57 im_ivector(int nl, int nh)
58 {
59 int *v;
60
61 v = (int *)im_malloc(NULL,(unsigned)(nh - nl + 1) * sizeof(int));
62 if (v == NULL)
63 return(NULL);
64 else
65 return(v-nl);
66 }
67
im_fvector(int nl,int nh)68 float *im_fvector(int nl, int nh)
69 {
70 float *v;
71
72 v = (float *)im_malloc(NULL,(unsigned)(nh - nl + 1) * sizeof(float));
73 if (v == NULL)
74 return(NULL);
75 else
76 return(v-nl);
77 }
78
im_dvector(int nl,int nh)79 double *im_dvector(int nl, int nh)
80 {
81 double *v;
82
83 v = (double *)im_malloc(NULL,(unsigned)(nh - nl + 1) * sizeof(double));
84 if (v == NULL)
85 return(NULL);
86 else
87 return(v-nl);
88 }
89
im_free_ivector(int * v,int nl,int nh)90 void im_free_ivector(int *v, int nl, int nh)
91 {
92 im_free((char*) (v+nl));
93 }
94
im_free_fvector(float * v,int nl,int nh)95 void im_free_fvector(float *v, int nl, int nh)
96 {
97 im_free((char*) (v+nl));
98 }
99
im_free_dvector(double * v,int nl,int nh)100 void im_free_dvector(double *v, int nl, int nh)
101 {
102 im_free((char*) (v+nl));
103 }
104
105 /* @(#) Allocates and returns an pointer at the beginning of
106 * @(#) an int, float or double, two dimensional matrix[nrl,nrh][ncl,nch]
107 * @(#)
108 * @(#) Right call
109 * @(#) int **im_imat_alloc(nrl, nrh, ncl, nch)
110 * @(#) int nrl, nrh, ncl, nch;
111 * @(#) returns a pointer to an int matrix or NULL on error
112 * @(#)
113 * @(#) float **im_fmat_alloc(nrl, nrh, ncl, nch)
114 * @(#) int nrl, nrh, ncl, nch;
115 * @(#) returns a pointer to an int matrix or NULL on error
116 * @(#)
117 * @(#) double **im_dmat_alloc(nrl, nrh, ncl, nch)
118 * @(#) int nrl, nrh, ncl, nch;
119 * @(#) returns a pointer to a double matrix or NULL on error
120 * @(#)
121 * @(#) The following routines free the matrix allocated by the functions above
122 * @(#) void im_free_imat(m, nrl, nrh, ncl, nch)
123 * @(#) int **m;
124 * @(#) int nrl, nrh, ncl, nch;
125 * @(#)
126 * @(#) void im_free_fmat(m, nrl, nrh, ncl, nch)
127 * @(#) float **m;
128 * @(#) int nrl, nrh, ncl, nch;
129 * @(#)
130 * @(#) void im_free_dmat(m, nrl, nrh, ncl, nch)
131 * @(#) double **m;
132 * @(#) int nrl, nrh, ncl, nch;
133 * @(#)
134 */
im_imat_alloc(int nrl,int nrh,int ncl,int nch)135 int **im_imat_alloc(int nrl, int nrh, int ncl, int nch)
136 {
137 int i;
138 int **m;
139
140 m = (int**)im_malloc(NULL,(unsigned)(nrh-nrl+1) * sizeof(int *));
141 if (m == NULL)
142 return(NULL);
143 m -= nrl;
144
145 for (i=nrl; i<=nrh; i++)
146 {
147 m[i] = (int *)im_malloc(NULL,(unsigned) (nch-ncl+1) * sizeof(int));
148 if (m[i] == NULL)
149 return(NULL);
150 m[i] -= ncl;
151 }
152 return (m);
153 }
154
im_free_imat(int ** m,int nrl,int nrh,int ncl,int nch)155 void im_free_imat(int **m, int nrl, int nrh, int ncl, int nch)
156 {
157 int i;
158
159 for (i=nrh; i>=nrl; i--)
160 im_free((char*) (m[i]+ncl));
161 im_free((char*) (m+nrl));
162 }
163
im_fmat_alloc(int nrl,int nrh,int ncl,int nch)164 float **im_fmat_alloc(int nrl, int nrh, int ncl, int nch)
165 {
166 int i;
167 float **m;
168
169 m = (float**)im_malloc(NULL,(unsigned)(nrh-nrl+1) * sizeof(float *));
170 if (m == NULL)
171 return(NULL);
172 m -= nrl;
173
174 for (i=nrl; i<=nrh; i++)
175 {
176 m[i] = (float *)im_malloc(NULL,(unsigned) (nch-ncl+1) * sizeof(float));
177 if (m[i] == NULL)
178 return(NULL);
179 m[i] -= ncl;
180 }
181 return (m);
182 }
183
im_free_fmat(float ** m,int nrl,int nrh,int ncl,int nch)184 void im_free_fmat(float **m, int nrl, int nrh, int ncl, int nch)
185 {
186 int i;
187
188 for (i=nrh; i>=nrl; i--)
189 im_free((char*) (m[i]+ncl));
190 im_free((char*) (m+nrl));
191 }
192
im_dmat_alloc(int nrl,int nrh,int ncl,int nch)193 double **im_dmat_alloc(int nrl, int nrh, int ncl, int nch)
194 {
195 int i;
196 double **m;
197
198 m = (double**)im_malloc(NULL,(unsigned)(nrh-nrl+1) * sizeof(double *));
199 if (m == NULL)
200 return(NULL);
201 m -= nrl;
202
203 for (i=nrl; i<=nrh; i++)
204 {
205 m[i] = (double *)im_malloc(NULL,(unsigned) (nch-ncl+1) * sizeof(double));
206 if (m[i] == NULL)
207 return(NULL);
208 m[i] -= ncl;
209 }
210 return (m);
211 }
212
im_free_dmat(double ** m,int nrl,int nrh,int ncl,int nch)213 void im_free_dmat(double **m, int nrl, int nrh, int ncl, int nch)
214 {
215 int i;
216
217 for (i=nrh; i>=nrl; i--)
218 im_free((char*) (m[i]+ncl));
219 im_free((char*) (m+nrl));
220 }
221