blob: 6a502c75cb2eb2e32868c8d271859ba447b80d22 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M AAA TTTTT RRRR IIIII X X %
7% MM MM A A T R R I X X %
8% M M M AAAAA T RRRR I X %
9% M M A A T R R I X X %
10% M M A A T R R IIIII X X %
11% %
12% %
13% MagickCore Matrix Methods %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% August 2007 %
18% %
19% %
Cristy93b707b2017-12-06 07:05:51 -050020% Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
Cristyf19d4142017-04-24 11:34:30 -040026% https://www.imagemagick.org/script/license.php %
cristy3ed852e2009-09-05 21:47:34 +000027% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
cristy106efa12014-03-09 01:06:05 +000043#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
dirk007e9252015-01-15 21:02:57 +000048#include "MagickCore/image-private.h"
cristy4c08aed2011-07-01 19:47:50 +000049#include "MagickCore/matrix.h"
Cristy044b9332017-06-02 08:32:43 -040050#include "MagickCore/matrix-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/memory_.h"
cristy0f09a8c2014-04-23 00:20:40 +000052#include "MagickCore/pixel-accessor.h"
cristy106efa12014-03-09 01:06:05 +000053#include "MagickCore/pixel-private.h"
54#include "MagickCore/resource_.h"
cristycba9e952014-03-30 23:17:57 +000055#include "MagickCore/semaphore.h"
cristyf3b58c52014-04-22 01:27:24 +000056#include "MagickCore/thread-private.h"
cristy106efa12014-03-09 01:06:05 +000057#include "MagickCore/utility.h"
58
59/*
60 Typedef declaration.
61*/
62struct _MatrixInfo
63{
64 CacheType
65 type;
66
67 size_t
68 columns,
69 rows,
70 stride;
71
72 MagickSizeType
73 length;
74
75 MagickBooleanType
cristyc1712b22014-03-09 14:54:05 +000076 mapped,
77 synchronize;
cristy106efa12014-03-09 01:06:05 +000078
79 char
cristy151b66d2015-04-15 10:50:31 +000080 path[MagickPathExtent];
cristy106efa12014-03-09 01:06:05 +000081
82 int
83 file;
84
85 void
86 *elements;
87
cristycba9e952014-03-30 23:17:57 +000088 SemaphoreInfo
89 *semaphore;
90
cristy106efa12014-03-09 01:06:05 +000091 size_t
92 signature;
93};
94
95/*
96%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97% %
98% %
99% %
100% A c q u i r e M a t r i x I n f o %
101% %
102% %
103% %
104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105%
cristyc1712b22014-03-09 14:54:05 +0000106% AcquireMatrixInfo() allocates the ImageInfo structure.
cristy106efa12014-03-09 01:06:05 +0000107%
108% The format of the AcquireMatrixInfo method is:
109%
110% MatrixInfo *AcquireMatrixInfo(const size_t columns,const size_t rows,
111% const size_t stride,ExceptionInfo *exception)
112%
113% A description of each parameter follows:
114%
115% o columns: the matrix columns.
116%
117% o rows: the matrix rows.
118%
119% o stride: the matrix stride.
120%
121% o exception: return any errors or warnings in this structure.
122%
123*/
cristyc1712b22014-03-09 14:54:05 +0000124
cristyc1712b22014-03-09 14:54:05 +0000125#if defined(SIGBUS)
126static void MatrixSignalHandler(int status)
127{
128 ThrowFatalException(CacheFatalError,"UnableToExtendMatrixCache");
129}
130#endif
131
132static inline MagickOffsetType WriteMatrixElements(
dirk05d2ff72015-11-18 23:13:43 +0100133 const MatrixInfo *magick_restrict matrix_info,const MagickOffsetType offset,
134 const MagickSizeType length,const unsigned char *magick_restrict buffer)
cristyc1712b22014-03-09 14:54:05 +0000135{
136 register MagickOffsetType
137 i;
138
139 ssize_t
140 count;
141
142#if !defined(MAGICKCORE_HAVE_PWRITE)
cristy10c210e2014-03-30 23:27:24 +0000143 LockSemaphoreInfo(matrix_info->semaphore);
cristyc1712b22014-03-09 14:54:05 +0000144 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
cristy10c210e2014-03-30 23:27:24 +0000145 {
146 UnlockSemaphoreInfo(matrix_info->semaphore);
147 return((MagickOffsetType) -1);
148 }
cristyc1712b22014-03-09 14:54:05 +0000149#endif
150 count=0;
151 for (i=0; i < (MagickOffsetType) length; i+=count)
152 {
153#if !defined(MAGICKCORE_HAVE_PWRITE)
154 count=write(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
155 (MagickSizeType) SSIZE_MAX));
156#else
157 count=pwrite(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
158 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
159#endif
160 if (count <= 0)
161 {
162 count=0;
163 if (errno != EINTR)
164 break;
165 }
166 }
cristy10c210e2014-03-30 23:27:24 +0000167#if !defined(MAGICKCORE_HAVE_PWRITE)
168 UnlockSemaphoreInfo(matrix_info->semaphore);
169#endif
cristyc1712b22014-03-09 14:54:05 +0000170 return(i);
171}
172
dirk05d2ff72015-11-18 23:13:43 +0100173static MagickBooleanType SetMatrixExtent(
Cristyc2744aa2017-10-31 15:45:44 -0400174 MatrixInfo *magick_restrict matrix_info,MagickSizeType length)
cristyc1712b22014-03-09 14:54:05 +0000175{
176 MagickOffsetType
177 count,
178 extent,
179 offset;
180
181 if (length != (MagickSizeType) ((MagickOffsetType) length))
182 return(MagickFalse);
183 offset=(MagickOffsetType) lseek(matrix_info->file,0,SEEK_END);
184 if (offset < 0)
185 return(MagickFalse);
186 if ((MagickSizeType) offset >= length)
187 return(MagickTrue);
188 extent=(MagickOffsetType) length-1;
189 count=WriteMatrixElements(matrix_info,extent,1,(const unsigned char *) "");
190#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
191 if (matrix_info->synchronize != MagickFalse)
cristybb380e72014-10-31 12:56:36 +0000192 (void) posix_fallocate(matrix_info->file,offset+1,extent-offset);
cristyc1712b22014-03-09 14:54:05 +0000193#endif
194#if defined(SIGBUS)
195 (void) signal(SIGBUS,MatrixSignalHandler);
196#endif
197 return(count != (MagickOffsetType) 1 ? MagickFalse : MagickTrue);
198}
199
cristy106efa12014-03-09 01:06:05 +0000200MagickExport MatrixInfo *AcquireMatrixInfo(const size_t columns,
201 const size_t rows,const size_t stride,ExceptionInfo *exception)
202{
cristyc1712b22014-03-09 14:54:05 +0000203 char
204 *synchronize;
205
cristy106efa12014-03-09 01:06:05 +0000206 MagickBooleanType
207 status;
208
209 MatrixInfo
210 *matrix_info;
211
212 matrix_info=(MatrixInfo *) AcquireMagickMemory(sizeof(*matrix_info));
213 if (matrix_info == (MatrixInfo *) NULL)
214 return((MatrixInfo *) NULL);
Cristy81bfff22018-03-10 07:58:31 -0500215 (void) memset(matrix_info,0,sizeof(*matrix_info));
cristye1c94d92015-06-28 12:16:33 +0000216 matrix_info->signature=MagickCoreSignature;
cristy106efa12014-03-09 01:06:05 +0000217 matrix_info->columns=columns;
218 matrix_info->rows=rows;
219 matrix_info->stride=stride;
cristycba9e952014-03-30 23:17:57 +0000220 matrix_info->semaphore=AcquireSemaphoreInfo();
cristyc1712b22014-03-09 14:54:05 +0000221 synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
222 if (synchronize != (const char *) NULL)
223 {
224 matrix_info->synchronize=IsStringTrue(synchronize);
225 synchronize=DestroyString(synchronize);
226 }
cristy106efa12014-03-09 01:06:05 +0000227 matrix_info->length=(MagickSizeType) columns*rows*stride;
228 if (matrix_info->columns != (size_t) (matrix_info->length/rows/stride))
229 {
230 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
231 "CacheResourcesExhausted","`%s'","matrix cache");
232 return(DestroyMatrixInfo(matrix_info));
233 }
234 matrix_info->type=MemoryCache;
235 status=AcquireMagickResource(AreaResource,matrix_info->length);
236 if ((status != MagickFalse) &&
237 (matrix_info->length == (MagickSizeType) ((size_t) matrix_info->length)))
238 {
239 status=AcquireMagickResource(MemoryResource,matrix_info->length);
240 if (status != MagickFalse)
241 {
cristyc1712b22014-03-09 14:54:05 +0000242 matrix_info->mapped=MagickFalse;
cristy106efa12014-03-09 01:06:05 +0000243 matrix_info->elements=AcquireMagickMemory((size_t)
244 matrix_info->length);
245 if (matrix_info->elements == NULL)
246 {
247 matrix_info->mapped=MagickTrue;
248 matrix_info->elements=MapBlob(-1,IOMode,0,(size_t)
249 matrix_info->length);
250 }
251 if (matrix_info->elements == (unsigned short *) NULL)
252 RelinquishMagickResource(MemoryResource,matrix_info->length);
253 }
254 }
255 matrix_info->file=(-1);
256 if (matrix_info->elements == (unsigned short *) NULL)
257 {
258 status=AcquireMagickResource(DiskResource,matrix_info->length);
259 if (status == MagickFalse)
260 {
261 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
262 "CacheResourcesExhausted","`%s'","matrix cache");
263 return(DestroyMatrixInfo(matrix_info));
264 }
265 matrix_info->type=DiskCache;
cristy106efa12014-03-09 01:06:05 +0000266 matrix_info->file=AcquireUniqueFileResource(matrix_info->path);
267 if (matrix_info->file == -1)
268 return(DestroyMatrixInfo(matrix_info));
269 status=AcquireMagickResource(MapResource,matrix_info->length);
270 if (status != MagickFalse)
271 {
cristyc1712b22014-03-09 14:54:05 +0000272 status=SetMatrixExtent(matrix_info,matrix_info->length);
cristy106efa12014-03-09 01:06:05 +0000273 if (status != MagickFalse)
Dirk Lemstra3cd3aee2017-11-03 07:51:04 +0100274 matrix_info->elements=(void *) MapBlob(matrix_info->file,IOMode,0,
275 (size_t) matrix_info->length);
276 if (matrix_info->elements != NULL)
Cristy6f240882017-11-04 20:17:29 -0400277 matrix_info->type=MapCache;
Dirk Lemstra3cd3aee2017-11-03 07:51:04 +0100278 else
279 RelinquishMagickResource(MapResource,matrix_info->length);
cristy106efa12014-03-09 01:06:05 +0000280 }
281 }
282 return(matrix_info);
283}
cristy3ed852e2009-09-05 21:47:34 +0000284
285/*
286%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287% %
288% %
289% %
290% A c q u i r e M a g i c k M a t r i x %
291% %
292% %
293% %
294%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295%
296% AcquireMagickMatrix() allocates and returns a matrix in the form of an
297% array of pointers to an array of doubles, with all values pre-set to zero.
298%
cristy106efa12014-03-09 01:06:05 +0000299% This used to generate the two dimensional matrix, and vectors required
300% for the GaussJordanElimination() method below, solving some system of
301% simultanious equations.
cristy3ed852e2009-09-05 21:47:34 +0000302%
303% The format of the AcquireMagickMatrix method is:
304%
cristybb503372010-05-27 20:51:26 +0000305% double **AcquireMagickMatrix(const size_t number_rows,
306% const size_t size)
cristy3ed852e2009-09-05 21:47:34 +0000307%
308% A description of each parameter follows:
309%
cristy74908cf2010-05-17 19:43:54 +0000310% o number_rows: the number pointers for the array of pointers
cristy1ad491d2010-05-17 19:45:27 +0000311% (first dimension).
cristy3ed852e2009-09-05 21:47:34 +0000312%
cristy1ad491d2010-05-17 19:45:27 +0000313% o size: the size of the array of doubles each pointer points to
314% (second dimension).
cristy3ed852e2009-09-05 21:47:34 +0000315%
316*/
cristybb503372010-05-27 20:51:26 +0000317MagickExport double **AcquireMagickMatrix(const size_t number_rows,
318 const size_t size)
cristy3ed852e2009-09-05 21:47:34 +0000319{
320 double
cristy74908cf2010-05-17 19:43:54 +0000321 **matrix;
cristy3ed852e2009-09-05 21:47:34 +0000322
cristybb503372010-05-27 20:51:26 +0000323 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000324 i,
325 j;
326
cristy74908cf2010-05-17 19:43:54 +0000327 matrix=(double **) AcquireQuantumMemory(number_rows,sizeof(*matrix));
cristy3ed852e2009-09-05 21:47:34 +0000328 if (matrix == (double **) NULL)
cristyf432c632014-12-07 15:11:28 +0000329 return((double **) NULL);
cristybb503372010-05-27 20:51:26 +0000330 for (i=0; i < (ssize_t) number_rows; i++)
cristy3ed852e2009-09-05 21:47:34 +0000331 {
332 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
333 if (matrix[i] == (double *) NULL)
Cristy301e46e2017-11-24 18:37:54 -0500334 {
335 for (j=0; j < i; j++)
336 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
337 matrix=(double **) RelinquishMagickMemory(matrix);
338 return((double **) NULL);
339 }
cristybb503372010-05-27 20:51:26 +0000340 for (j=0; j < (ssize_t) size; j++)
cristy74908cf2010-05-17 19:43:54 +0000341 matrix[i][j]=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000342 }
343 return(matrix);
344}
345
346/*
347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348% %
349% %
350% %
cristy106efa12014-03-09 01:06:05 +0000351% D e s t r o y M a t r i x I n f o %
352% %
353% %
354% %
355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356%
357% DestroyMatrixInfo() dereferences a matrix, deallocating memory associated
358% with the matrix.
359%
360% The format of the DestroyImage method is:
361%
362% MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
363%
364% A description of each parameter follows:
365%
366% o matrix_info: the matrix.
367%
368*/
369MagickExport MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
370{
371 assert(matrix_info != (MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000372 assert(matrix_info->signature == MagickCoreSignature);
cristycba9e952014-03-30 23:17:57 +0000373 LockSemaphoreInfo(matrix_info->semaphore);
cristy106efa12014-03-09 01:06:05 +0000374 switch (matrix_info->type)
375 {
376 case MemoryCache:
377 {
378 if (matrix_info->mapped == MagickFalse)
379 matrix_info->elements=RelinquishMagickMemory(matrix_info->elements);
380 else
381 {
382 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
383 matrix_info->elements=(unsigned short *) NULL;
384 }
385 RelinquishMagickResource(MemoryResource,matrix_info->length);
386 break;
387 }
388 case MapCache:
389 {
390 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
391 matrix_info->elements=NULL;
392 RelinquishMagickResource(MapResource,matrix_info->length);
393 }
394 case DiskCache:
395 {
396 if (matrix_info->file != -1)
397 (void) close(matrix_info->file);
398 (void) RelinquishUniqueFileResource(matrix_info->path);
399 RelinquishMagickResource(DiskResource,matrix_info->length);
400 break;
401 }
402 default:
403 break;
404 }
cristycba9e952014-03-30 23:17:57 +0000405 UnlockSemaphoreInfo(matrix_info->semaphore);
406 RelinquishSemaphoreInfo(&matrix_info->semaphore);
cristy106efa12014-03-09 01:06:05 +0000407 return((MatrixInfo *) RelinquishMagickMemory(matrix_info));
408}
409
410/*
411%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412% %
413% %
414% %
Cristy88932ef2015-12-20 17:35:31 -0500415+ G a u s s J o r d a n E l i m i n a t i o n %
cristy3ed852e2009-09-05 21:47:34 +0000416% %
417% %
418% %
419%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420%
421% GaussJordanElimination() returns a matrix in reduced row echelon form,
422% while simultaneously reducing and thus solving the augumented results
423% matrix.
424%
425% See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
426%
427% The format of the GaussJordanElimination method is:
428%
cristy7dbde212014-10-11 06:59:40 +0000429% MagickBooleanType GaussJordanElimination(double **matrix,
430% double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000431%
432% A description of each parameter follows:
433%
434% o matrix: the matrix to be reduced, as an 'array of row pointers'.
435%
436% o vectors: the additional matrix argumenting the matrix for row reduction.
437% Producing an 'array of column vectors'.
438%
cristy106efa12014-03-09 01:06:05 +0000439% o rank: The size of the matrix (both rows and columns).
cristy3ed852e2009-09-05 21:47:34 +0000440% Also represents the number terms that need to be solved.
441%
cristy74908cf2010-05-17 19:43:54 +0000442% o number_vectors: Number of vectors columns, argumenting the above matrix.
cristy3ed852e2009-09-05 21:47:34 +0000443% Usally 1, but can be more for more complex equation solving.
444%
445% Note that the 'matrix' is given as a 'array of row pointers' of rank size.
446% That is values can be assigned as matrix[row][column] where 'row' is
447% typically the equation, and 'column' is the term of the equation.
448% That is the matrix is in the form of a 'row first array'.
449%
450% However 'vectors' is a 'array of column pointers' which can have any number
451% of columns, with each column array the same 'rank' size as 'matrix'.
452%
453% This allows for simpler handling of the results, especially is only one
cristy106efa12014-03-09 01:06:05 +0000454% column 'vector' is all that is required to produce the desired solution.
cristy3ed852e2009-09-05 21:47:34 +0000455%
456% For example, the 'vectors' can consist of a pointer to a simple array of
457% doubles. when only one set of simultanious equations is to be solved from
458% the given set of coefficient weighted terms.
459%
460% double **matrix = AcquireMagickMatrix(8UL,8UL);
461% double coefficents[8];
462% ...
463% GaussJordanElimination(matrix, &coefficents, 8UL, 1UL);
anthony34364f42011-10-21 05:31:53 +0000464%
cristy106efa12014-03-09 01:06:05 +0000465% However by specifing more 'columns' (as an 'array of vector columns',
466% you can use this function to solve a set of 'separable' equations.
cristy3ed852e2009-09-05 21:47:34 +0000467%
468% For example a distortion function where u = U(x,y) v = V(x,y)
469% And the functions U() and V() have separate coefficents, but are being
470% generated from a common x,y->u,v data set.
471%
cristy7dbde212014-10-11 06:59:40 +0000472% Another example is generation of a color gradient from a set of colors at
473% specific coordients, such as a list x,y -> r,g,b,a.
cristy3ed852e2009-09-05 21:47:34 +0000474%
475% You can also use the 'vectors' to generate an inverse of the given 'matrix'
cristy106efa12014-03-09 01:06:05 +0000476% though as a 'column first array' rather than a 'row first array'. For
cristy7dbde212014-10-11 06:59:40 +0000477% details see http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
cristy3ed852e2009-09-05 21:47:34 +0000478%
479*/
Cristy88932ef2015-12-20 17:35:31 -0500480MagickPrivate MagickBooleanType GaussJordanElimination(double **matrix,
cristybb503372010-05-27 20:51:26 +0000481 double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000482{
483#define GaussJordanSwap(x,y) \
484{ \
485 if ((x) != (y)) \
486 { \
487 (x)+=(y); \
488 (y)=(x)-(y); \
489 (x)=(x)-(y); \
490 } \
491}
492
493 double
494 max,
495 scale;
496
cristy9d314ff2011-03-09 01:30:28 +0000497 register ssize_t
498 i,
499 j,
500 k;
501
cristybb503372010-05-27 20:51:26 +0000502 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000503 column,
504 *columns,
505 *pivots,
506 row,
507 *rows;
508
cristybb503372010-05-27 20:51:26 +0000509 columns=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*columns));
510 rows=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*rows));
511 pivots=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*pivots));
512 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
513 (pivots == (ssize_t *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000514 {
cristybb503372010-05-27 20:51:26 +0000515 if (pivots != (ssize_t *) NULL)
516 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
517 if (columns != (ssize_t *) NULL)
518 columns=(ssize_t *) RelinquishMagickMemory(columns);
519 if (rows != (ssize_t *) NULL)
520 rows=(ssize_t *) RelinquishMagickMemory(rows);
cristy3ed852e2009-09-05 21:47:34 +0000521 return(MagickFalse);
522 }
Cristy81bfff22018-03-10 07:58:31 -0500523 (void) memset(columns,0,rank*sizeof(*columns));
524 (void) memset(rows,0,rank*sizeof(*rows));
525 (void) memset(pivots,0,rank*sizeof(*pivots));
cristy3ed852e2009-09-05 21:47:34 +0000526 column=0;
527 row=0;
cristybb503372010-05-27 20:51:26 +0000528 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000529 {
530 max=0.0;
cristybb503372010-05-27 20:51:26 +0000531 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000532 if (pivots[j] != 1)
533 {
cristybb503372010-05-27 20:51:26 +0000534 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000535 if (pivots[k] != 0)
536 {
537 if (pivots[k] > 1)
538 return(MagickFalse);
539 }
540 else
541 if (fabs(matrix[j][k]) >= max)
542 {
543 max=fabs(matrix[j][k]);
544 row=j;
545 column=k;
546 }
547 }
548 pivots[column]++;
549 if (row != column)
550 {
cristybb503372010-05-27 20:51:26 +0000551 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000552 GaussJordanSwap(matrix[row][k],matrix[column][k]);
cristybb503372010-05-27 20:51:26 +0000553 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000554 GaussJordanSwap(vectors[k][row],vectors[k][column]);
555 }
556 rows[i]=row;
557 columns[i]=column;
558 if (matrix[column][column] == 0.0)
cristy106efa12014-03-09 01:06:05 +0000559 return(MagickFalse); /* sigularity */
cristy3e3ec3a2012-11-03 23:11:06 +0000560 scale=PerceptibleReciprocal(matrix[column][column]);
cristy3ed852e2009-09-05 21:47:34 +0000561 matrix[column][column]=1.0;
cristybb503372010-05-27 20:51:26 +0000562 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000563 matrix[column][j]*=scale;
cristybb503372010-05-27 20:51:26 +0000564 for (j=0; j < (ssize_t) number_vectors; j++)
cristy3ed852e2009-09-05 21:47:34 +0000565 vectors[j][column]*=scale;
cristybb503372010-05-27 20:51:26 +0000566 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000567 if (j != column)
568 {
569 scale=matrix[j][column];
570 matrix[j][column]=0.0;
cristybb503372010-05-27 20:51:26 +0000571 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000572 matrix[j][k]-=scale*matrix[column][k];
cristybb503372010-05-27 20:51:26 +0000573 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000574 vectors[k][j]-=scale*vectors[k][column];
575 }
576 }
cristybb503372010-05-27 20:51:26 +0000577 for (j=(ssize_t) rank-1; j >= 0; j--)
cristy3ed852e2009-09-05 21:47:34 +0000578 if (columns[j] != rows[j])
cristybb503372010-05-27 20:51:26 +0000579 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000580 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
cristybb503372010-05-27 20:51:26 +0000581 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
582 rows=(ssize_t *) RelinquishMagickMemory(rows);
583 columns=(ssize_t *) RelinquishMagickMemory(columns);
cristy3ed852e2009-09-05 21:47:34 +0000584 return(MagickTrue);
585}
586
587/*
588%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
589% %
590% %
591% %
cristy106efa12014-03-09 01:06:05 +0000592% G e t M a t r i x C o l u m n s %
593% %
594% %
595% %
596%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597%
598% GetMatrixColumns() returns the number of columns in the matrix.
599%
600% The format of the GetMatrixColumns method is:
601%
602% size_t GetMatrixColumns(const MatrixInfo *matrix_info)
603%
604% A description of each parameter follows:
605%
606% o matrix_info: the matrix.
607%
608*/
609MagickExport size_t GetMatrixColumns(const MatrixInfo *matrix_info)
610{
611 assert(matrix_info != (MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000612 assert(matrix_info->signature == MagickCoreSignature);
cristy106efa12014-03-09 01:06:05 +0000613 return(matrix_info->columns);
614}
615
616/*
617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618% %
619% %
620% %
621% G e t M a t r i x E l e m e n t %
622% %
623% %
624% %
625%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
626%
627% GetMatrixElement() returns the specifed element in the matrix.
628%
629% The format of the GetMatrixElement method is:
630%
631% MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
632% const ssize_t x,const ssize_t y,void *value)
633%
634% A description of each parameter follows:
635%
636% o matrix_info: the matrix columns.
637%
638% o x: the matrix x-offset.
639%
640% o y: the matrix y-offset.
641%
642% o value: return the matrix element in this buffer.
643%
644*/
645
cristy6cccee42014-03-23 00:25:22 +0000646static inline ssize_t EdgeX(const ssize_t x,const size_t columns)
647{
648 if (x < 0L)
649 return(0L);
650 if (x >= (ssize_t) columns)
651 return((ssize_t) (columns-1));
652 return(x);
653}
654
655static inline ssize_t EdgeY(const ssize_t y,const size_t rows)
656{
657 if (y < 0L)
658 return(0L);
659 if (y >= (ssize_t) rows)
660 return((ssize_t) (rows-1));
661 return(y);
662}
663
cristyc1712b22014-03-09 14:54:05 +0000664static inline MagickOffsetType ReadMatrixElements(
dirk05d2ff72015-11-18 23:13:43 +0100665 const MatrixInfo *magick_restrict matrix_info,const MagickOffsetType offset,
666 const MagickSizeType length,unsigned char *magick_restrict buffer)
cristy106efa12014-03-09 01:06:05 +0000667{
cristyc1712b22014-03-09 14:54:05 +0000668 register MagickOffsetType
cristy106efa12014-03-09 01:06:05 +0000669 i;
670
671 ssize_t
672 count;
673
cristyc1712b22014-03-09 14:54:05 +0000674#if !defined(MAGICKCORE_HAVE_PREAD)
cristy10c210e2014-03-30 23:27:24 +0000675 LockSemaphoreInfo(matrix_info->semaphore);
cristyc1712b22014-03-09 14:54:05 +0000676 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
cristy10c210e2014-03-30 23:27:24 +0000677 {
678 UnlockSemaphoreInfo(matrix_info->semaphore);
679 return((MagickOffsetType) -1);
680 }
cristy106efa12014-03-09 01:06:05 +0000681#endif
cristyc1712b22014-03-09 14:54:05 +0000682 count=0;
683 for (i=0; i < (MagickOffsetType) length; i+=count)
cristy106efa12014-03-09 01:06:05 +0000684 {
cristyc1712b22014-03-09 14:54:05 +0000685#if !defined(MAGICKCORE_HAVE_PREAD)
686 count=read(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
687 (MagickSizeType) SSIZE_MAX));
cristy106efa12014-03-09 01:06:05 +0000688#else
cristyc1712b22014-03-09 14:54:05 +0000689 count=pread(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
690 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
cristy106efa12014-03-09 01:06:05 +0000691#endif
cristyc1712b22014-03-09 14:54:05 +0000692 if (count <= 0)
693 {
694 count=0;
695 if (errno != EINTR)
696 break;
cristy106efa12014-03-09 01:06:05 +0000697 }
698 }
cristy10c210e2014-03-30 23:27:24 +0000699#if !defined(MAGICKCORE_HAVE_PREAD)
700 UnlockSemaphoreInfo(matrix_info->semaphore);
701#endif
cristy106efa12014-03-09 01:06:05 +0000702 return(i);
703}
704
705MagickExport MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
706 const ssize_t x,const ssize_t y,void *value)
707{
708 MagickOffsetType
cristyc1712b22014-03-09 14:54:05 +0000709 count,
cristy106efa12014-03-09 01:06:05 +0000710 i;
711
cristy106efa12014-03-09 01:06:05 +0000712 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000713 assert(matrix_info->signature == MagickCoreSignature);
cristy6cccee42014-03-23 00:25:22 +0000714 i=(MagickOffsetType) EdgeY(y,matrix_info->rows)*matrix_info->columns+
715 EdgeX(x,matrix_info->columns);
cristy106efa12014-03-09 01:06:05 +0000716 if (matrix_info->type != DiskCache)
717 {
cristy7a729512014-03-09 16:43:07 +0000718 (void) memcpy(value,(unsigned char *) matrix_info->elements+i*
719 matrix_info->stride,matrix_info->stride);
cristy106efa12014-03-09 01:06:05 +0000720 return(MagickTrue);
721 }
cristyc1712b22014-03-09 14:54:05 +0000722 count=ReadMatrixElements(matrix_info,i*matrix_info->stride,
cristy759ba912014-06-26 11:59:43 +0000723 matrix_info->stride,(unsigned char *) value);
cristyc1712b22014-03-09 14:54:05 +0000724 if (count != (MagickOffsetType) matrix_info->stride)
cristy106efa12014-03-09 01:06:05 +0000725 return(MagickFalse);
726 return(MagickTrue);
727}
728
729/*
730%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731% %
732% %
733% %
734% G e t M a t r i x R o w s %
735% %
736% %
737% %
738%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
739%
740% GetMatrixRows() returns the number of rows in the matrix.
741%
742% The format of the GetMatrixRows method is:
743%
744% size_t GetMatrixRows(const MatrixInfo *matrix_info)
745%
746% A description of each parameter follows:
747%
748% o matrix_info: the matrix.
749%
750*/
751MagickExport size_t GetMatrixRows(const MatrixInfo *matrix_info)
752{
753 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000754 assert(matrix_info->signature == MagickCoreSignature);
cristy106efa12014-03-09 01:06:05 +0000755 return(matrix_info->rows);
756}
757
758/*
759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760% %
761% %
762% %
Cristy88932ef2015-12-20 17:35:31 -0500763+ L e a s t S q u a r e s A d d T e r m s %
cristy3ed852e2009-09-05 21:47:34 +0000764% %
765% %
766% %
767%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768%
769% LeastSquaresAddTerms() adds one set of terms and associate results to the
770% given matrix and vectors for solving using least-squares function fitting.
771%
772% The format of the AcquireMagickMatrix method is:
773%
774% void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000775% const double *terms,const double *results,const size_t rank,
776% const size_t number_vectors);
cristy3ed852e2009-09-05 21:47:34 +0000777%
778% A description of each parameter follows:
779%
780% o matrix: the square matrix to add given terms/results to.
781%
782% o vectors: the result vectors to add terms/results to.
783%
784% o terms: the pre-calculated terms (without the unknown coefficent
cristy106efa12014-03-09 01:06:05 +0000785% weights) that forms the equation being added.
cristy3ed852e2009-09-05 21:47:34 +0000786%
787% o results: the result(s) that should be generated from the given terms
cristy106efa12014-03-09 01:06:05 +0000788% weighted by the yet-to-be-solved coefficents.
cristy3ed852e2009-09-05 21:47:34 +0000789%
cristy106efa12014-03-09 01:06:05 +0000790% o rank: the rank or size of the dimensions of the square matrix.
791% Also the length of vectors, and number of terms being added.
cristy3ed852e2009-09-05 21:47:34 +0000792%
cristy1ad491d2010-05-17 19:45:27 +0000793% o number_vectors: Number of result vectors, and number or results being
794% added. Also represents the number of separable systems of equations
795% that is being solved.
cristy3ed852e2009-09-05 21:47:34 +0000796%
797% Example of use...
798%
glennrp2489f532011-06-25 03:02:43 +0000799% 2 dimensional Affine Equations (which are separable)
cristy3ed852e2009-09-05 21:47:34 +0000800% c0*x + c2*y + c4*1 => u
801% c1*x + c3*y + c5*1 => v
802%
803% double **matrix = AcquireMagickMatrix(3UL,3UL);
804% double **vectors = AcquireMagickMatrix(2UL,3UL);
805% double terms[3], results[2];
806% ...
807% for each given x,y -> u,v
808% terms[0] = x;
809% terms[1] = y;
810% terms[2] = 1;
811% results[0] = u;
812% results[1] = v;
813% LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
814% ...
815% if ( GaussJordanElimination(matrix,vectors,3UL,2UL) ) {
816% c0 = vectors[0][0];
cristy106efa12014-03-09 01:06:05 +0000817% c2 = vectors[0][1];
cristy3ed852e2009-09-05 21:47:34 +0000818% c4 = vectors[0][2];
819% c1 = vectors[1][0];
cristy106efa12014-03-09 01:06:05 +0000820% c3 = vectors[1][1];
cristy3ed852e2009-09-05 21:47:34 +0000821% c5 = vectors[1][2];
822% }
823% else
824% printf("Matrix unsolvable\n);
825% RelinquishMagickMatrix(matrix,3UL);
826% RelinquishMagickMatrix(vectors,2UL);
827%
828*/
Cristy88932ef2015-12-20 17:35:31 -0500829MagickPrivate void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000830 const double *terms,const double *results,const size_t rank,
831 const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000832{
cristybb503372010-05-27 20:51:26 +0000833 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000834 i,
835 j;
836
cristybb503372010-05-27 20:51:26 +0000837 for (j=0; j < (ssize_t) rank; j++)
cristy74908cf2010-05-17 19:43:54 +0000838 {
cristybb503372010-05-27 20:51:26 +0000839 for (i=0; i < (ssize_t) rank; i++)
cristy74908cf2010-05-17 19:43:54 +0000840 matrix[i][j]+=terms[i]*terms[j];
cristybb503372010-05-27 20:51:26 +0000841 for (i=0; i < (ssize_t) number_vectors; i++)
cristy74908cf2010-05-17 19:43:54 +0000842 vectors[i][j]+=results[i]*terms[j];
cristy3ed852e2009-09-05 21:47:34 +0000843 }
cristy3ed852e2009-09-05 21:47:34 +0000844}
845
846/*
847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848% %
849% %
850% %
cristyf3b58c52014-04-22 01:27:24 +0000851% M a t r i x T o I m a g e %
852% %
853% %
854% %
855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856%
cristy0f09a8c2014-04-23 00:20:40 +0000857% MatrixToImage() returns a matrix as an image. The matrix elements must be
858% of type double otherwise nonsense is returned.
cristyf3b58c52014-04-22 01:27:24 +0000859%
860% The format of the MatrixToImage method is:
861%
862% Image *MatrixToImage(const MatrixInfo *matrix_info,
863% ExceptionInfo *exception)
864%
865% A description of each parameter follows:
866%
867% o matrix_info: the matrix.
868%
869% o exception: return any errors or warnings in this structure.
870%
871*/
872MagickExport Image *MatrixToImage(const MatrixInfo *matrix_info,
873 ExceptionInfo *exception)
874{
cristy0f09a8c2014-04-23 00:20:40 +0000875 CacheView
876 *image_view;
877
878 double
879 max_value,
880 min_value,
881 scale_factor,
882 value;
883
884 Image
885 *image;
886
887 MagickBooleanType
888 status;
889
890 ssize_t
891 y;
892
893 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000894 assert(matrix_info->signature == MagickCoreSignature);
cristy0f09a8c2014-04-23 00:20:40 +0000895 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000896 assert(exception->signature == MagickCoreSignature);
cristy0f09a8c2014-04-23 00:20:40 +0000897 if (matrix_info->stride < sizeof(double))
898 return((Image *) NULL);
899 /*
900 Determine range of matrix.
901 */
902 (void) GetMatrixElement(matrix_info,0,0,&value);
903 min_value=value;
904 max_value=value;
905 for (y=0; y < (ssize_t) matrix_info->rows; y++)
906 {
907 register ssize_t
908 x;
909
910 for (x=0; x < (ssize_t) matrix_info->columns; x++)
911 {
912 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
913 continue;
914 if (value < min_value)
915 min_value=value;
916 else
917 if (value > max_value)
918 max_value=value;
919 }
920 }
921 if ((min_value == 0.0) && (max_value == 0.0))
922 scale_factor=0;
923 else
924 if (min_value == max_value)
925 {
926 scale_factor=(double) QuantumRange/min_value;
927 min_value=0;
928 }
929 else
930 scale_factor=(double) QuantumRange/(max_value-min_value);
931 /*
932 Convert matrix to image.
933 */
934 image=AcquireImage((ImageInfo *) NULL,exception);
935 image->columns=matrix_info->columns;
936 image->rows=matrix_info->rows;
Cristybeb4c2b2017-12-26 19:43:17 -0500937 image->colorspace=GRAYColorspace;
cristy0f09a8c2014-04-23 00:20:40 +0000938 status=MagickTrue;
939 image_view=AcquireAuthenticCacheView(image,exception);
940#if defined(MAGICKCORE_OPENMP_SUPPORT)
Cristy38b42e12018-03-18 10:13:01 -0400941 #pragma omp parallel for schedule(static) shared(status) \
Cristy8255ca92017-10-09 08:37:35 -0400942 magick_number_threads(image,image,image->rows,1)
cristy0f09a8c2014-04-23 00:20:40 +0000943#endif
944 for (y=0; y < (ssize_t) image->rows; y++)
945 {
946 double
947 value;
948
949 register Quantum
950 *q;
951
952 register ssize_t
953 x;
954
955 if (status == MagickFalse)
956 continue;
957 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
958 if (q == (Quantum *) NULL)
959 {
960 status=MagickFalse;
961 continue;
962 }
963 for (x=0; x < (ssize_t) image->columns; x++)
964 {
965 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
966 continue;
967 value=scale_factor*(value-min_value);
968 *q=ClampToQuantum(value);
969 q+=GetPixelChannels(image);
970 }
971 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
972 status=MagickFalse;
973 }
974 image_view=DestroyCacheView(image_view);
975 if (status == MagickFalse)
976 image=DestroyImage(image);
977 return(image);
cristyf3b58c52014-04-22 01:27:24 +0000978}
979
980/*
981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982% %
983% %
984% %
cristyc1712b22014-03-09 14:54:05 +0000985% N u l l M a t r i x %
986% %
987% %
988% %
989%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
990%
991% NullMatrix() sets all elements of the matrix to zero.
992%
Cristy81bfff22018-03-10 07:58:31 -0500993% The format of the memset method is:
cristyc1712b22014-03-09 14:54:05 +0000994%
995% MagickBooleanType *NullMatrix(MatrixInfo *matrix_info)
996%
997% A description of each parameter follows:
998%
999% o matrix_info: the matrix.
1000%
1001*/
1002MagickExport MagickBooleanType NullMatrix(MatrixInfo *matrix_info)
1003{
1004 register ssize_t
1005 x;
1006
1007 ssize_t
1008 count,
1009 y;
1010
1011 unsigned char
1012 value;
1013
1014 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001015 assert(matrix_info->signature == MagickCoreSignature);
cristyc1712b22014-03-09 14:54:05 +00001016 if (matrix_info->type != DiskCache)
1017 {
Cristy81bfff22018-03-10 07:58:31 -05001018 (void) memset(matrix_info->elements,0,(size_t)
cristyc1712b22014-03-09 14:54:05 +00001019 matrix_info->length);
1020 return(MagickTrue);
1021 }
1022 value=0;
1023 (void) lseek(matrix_info->file,0,SEEK_SET);
1024 for (y=0; y < (ssize_t) matrix_info->rows; y++)
1025 {
1026 for (x=0; x < (ssize_t) matrix_info->length; x++)
1027 {
1028 count=write(matrix_info->file,&value,sizeof(value));
1029 if (count != (ssize_t) sizeof(value))
1030 break;
1031 }
1032 if (x < (ssize_t) matrix_info->length)
1033 break;
1034 }
1035 return(y < (ssize_t) matrix_info->rows ? MagickFalse : MagickTrue);
1036}
1037
1038/*
1039%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1040% %
1041% %
1042% %
cristy3ed852e2009-09-05 21:47:34 +00001043% R e l i n q u i s h M a g i c k M a t r i x %
1044% %
1045% %
1046% %
1047%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1048%
1049% RelinquishMagickMatrix() frees the previously acquired matrix (array of
1050% pointers to arrays of doubles).
1051%
1052% The format of the RelinquishMagickMatrix method is:
1053%
1054% double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +00001055% const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +00001056%
1057% A description of each parameter follows:
1058%
1059% o matrix: the matrix to relinquish
1060%
cristy1ad491d2010-05-17 19:45:27 +00001061% o number_rows: the first dimension of the acquired matrix (number of
1062% pointers)
cristy3ed852e2009-09-05 21:47:34 +00001063%
1064*/
1065MagickExport double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +00001066 const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +00001067{
cristybb503372010-05-27 20:51:26 +00001068 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001069 i;
1070
1071 if (matrix == (double **) NULL )
1072 return(matrix);
cristybb503372010-05-27 20:51:26 +00001073 for (i=0; i < (ssize_t) number_rows; i++)
Cristy301e46e2017-11-24 18:37:54 -05001074 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
cristy3ed852e2009-09-05 21:47:34 +00001075 matrix=(double **) RelinquishMagickMemory(matrix);
cristy3ed852e2009-09-05 21:47:34 +00001076 return(matrix);
1077}
cristy106efa12014-03-09 01:06:05 +00001078
1079/*
1080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081% %
1082% %
1083% %
cristy106efa12014-03-09 01:06:05 +00001084% S e t M a t r i x E l e m e n t %
1085% %
1086% %
1087% %
1088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089%
1090% SetMatrixElement() sets the specifed element in the matrix.
1091%
1092% The format of the SetMatrixElement method is:
1093%
1094% MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1095% const ssize_t x,const ssize_t y,void *value)
1096%
1097% A description of each parameter follows:
1098%
1099% o matrix_info: the matrix columns.
1100%
1101% o x: the matrix x-offset.
1102%
1103% o y: the matrix y-offset.
1104%
1105% o value: set the matrix element to this value.
1106%
1107*/
1108
cristy106efa12014-03-09 01:06:05 +00001109MagickExport MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1110 const ssize_t x,const ssize_t y,const void *value)
1111{
1112 MagickOffsetType
cristyc1712b22014-03-09 14:54:05 +00001113 count,
cristy106efa12014-03-09 01:06:05 +00001114 i;
1115
cristy106efa12014-03-09 01:06:05 +00001116 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001117 assert(matrix_info->signature == MagickCoreSignature);
cristy6cccee42014-03-23 00:25:22 +00001118 i=(MagickOffsetType) y*matrix_info->columns+x;
cristy106efa12014-03-09 01:06:05 +00001119 if ((i < 0) ||
1120 ((MagickSizeType) (i*matrix_info->stride) >= matrix_info->length))
1121 return(MagickFalse);
1122 if (matrix_info->type != DiskCache)
1123 {
cristy7a729512014-03-09 16:43:07 +00001124 (void) memcpy((unsigned char *) matrix_info->elements+i*
1125 matrix_info->stride,value,matrix_info->stride);
cristy106efa12014-03-09 01:06:05 +00001126 return(MagickTrue);
1127 }
cristyc1712b22014-03-09 14:54:05 +00001128 count=WriteMatrixElements(matrix_info,i*matrix_info->stride,
cristy759ba912014-06-26 11:59:43 +00001129 matrix_info->stride,(unsigned char *) value);
cristyc1712b22014-03-09 14:54:05 +00001130 if (count != (MagickOffsetType) matrix_info->stride)
cristy106efa12014-03-09 01:06:05 +00001131 return(MagickFalse);
1132 return(MagickTrue);
1133}