blob: 1bee454611e2b0df6a0f8781f818d4faca21225e [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% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 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% %
26% http://www.imagemagick.org/script/license.php %
27% %
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"
50#include "MagickCore/memory_.h"
cristy0f09a8c2014-04-23 00:20:40 +000051#include "MagickCore/pixel-accessor.h"
cristy106efa12014-03-09 01:06:05 +000052#include "MagickCore/pixel-private.h"
53#include "MagickCore/resource_.h"
cristycba9e952014-03-30 23:17:57 +000054#include "MagickCore/semaphore.h"
cristyf3b58c52014-04-22 01:27:24 +000055#include "MagickCore/thread-private.h"
cristy106efa12014-03-09 01:06:05 +000056#include "MagickCore/utility.h"
57
58/*
59 Typedef declaration.
60*/
61struct _MatrixInfo
62{
63 CacheType
64 type;
65
66 size_t
67 columns,
68 rows,
69 stride;
70
71 MagickSizeType
72 length;
73
74 MagickBooleanType
cristyc1712b22014-03-09 14:54:05 +000075 mapped,
76 synchronize;
cristy106efa12014-03-09 01:06:05 +000077
78 char
cristy151b66d2015-04-15 10:50:31 +000079 path[MagickPathExtent];
cristy106efa12014-03-09 01:06:05 +000080
81 int
82 file;
83
84 void
85 *elements;
86
cristycba9e952014-03-30 23:17:57 +000087 SemaphoreInfo
88 *semaphore;
89
cristy106efa12014-03-09 01:06:05 +000090 size_t
91 signature;
92};
93
94/*
95%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96% %
97% %
98% %
99% A c q u i r e M a t r i x I n f o %
100% %
101% %
102% %
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104%
cristyc1712b22014-03-09 14:54:05 +0000105% AcquireMatrixInfo() allocates the ImageInfo structure.
cristy106efa12014-03-09 01:06:05 +0000106%
107% The format of the AcquireMatrixInfo method is:
108%
109% MatrixInfo *AcquireMatrixInfo(const size_t columns,const size_t rows,
110% const size_t stride,ExceptionInfo *exception)
111%
112% A description of each parameter follows:
113%
114% o columns: the matrix columns.
115%
116% o rows: the matrix rows.
117%
118% o stride: the matrix stride.
119%
120% o exception: return any errors or warnings in this structure.
121%
122*/
cristyc1712b22014-03-09 14:54:05 +0000123
cristyc1712b22014-03-09 14:54:05 +0000124#if defined(SIGBUS)
125static void MatrixSignalHandler(int status)
126{
127 ThrowFatalException(CacheFatalError,"UnableToExtendMatrixCache");
128}
129#endif
130
131static inline MagickOffsetType WriteMatrixElements(
132 const MatrixInfo *restrict matrix_info,const MagickOffsetType offset,
133 const MagickSizeType length,const unsigned char *restrict buffer)
134{
135 register MagickOffsetType
136 i;
137
138 ssize_t
139 count;
140
141#if !defined(MAGICKCORE_HAVE_PWRITE)
cristy10c210e2014-03-30 23:27:24 +0000142 LockSemaphoreInfo(matrix_info->semaphore);
cristyc1712b22014-03-09 14:54:05 +0000143 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
cristy10c210e2014-03-30 23:27:24 +0000144 {
145 UnlockSemaphoreInfo(matrix_info->semaphore);
146 return((MagickOffsetType) -1);
147 }
cristyc1712b22014-03-09 14:54:05 +0000148#endif
149 count=0;
150 for (i=0; i < (MagickOffsetType) length; i+=count)
151 {
152#if !defined(MAGICKCORE_HAVE_PWRITE)
153 count=write(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
154 (MagickSizeType) SSIZE_MAX));
155#else
156 count=pwrite(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
157 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
158#endif
159 if (count <= 0)
160 {
161 count=0;
162 if (errno != EINTR)
163 break;
164 }
165 }
cristy10c210e2014-03-30 23:27:24 +0000166#if !defined(MAGICKCORE_HAVE_PWRITE)
167 UnlockSemaphoreInfo(matrix_info->semaphore);
168#endif
cristyc1712b22014-03-09 14:54:05 +0000169 return(i);
170}
171
172static MagickBooleanType SetMatrixExtent(MatrixInfo *restrict matrix_info,
173 MagickSizeType length)
174{
175 MagickOffsetType
176 count,
177 extent,
178 offset;
179
180 if (length != (MagickSizeType) ((MagickOffsetType) length))
181 return(MagickFalse);
182 offset=(MagickOffsetType) lseek(matrix_info->file,0,SEEK_END);
183 if (offset < 0)
184 return(MagickFalse);
185 if ((MagickSizeType) offset >= length)
186 return(MagickTrue);
187 extent=(MagickOffsetType) length-1;
188 count=WriteMatrixElements(matrix_info,extent,1,(const unsigned char *) "");
189#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
190 if (matrix_info->synchronize != MagickFalse)
cristybb380e72014-10-31 12:56:36 +0000191 (void) posix_fallocate(matrix_info->file,offset+1,extent-offset);
cristyc1712b22014-03-09 14:54:05 +0000192#endif
193#if defined(SIGBUS)
194 (void) signal(SIGBUS,MatrixSignalHandler);
195#endif
196 return(count != (MagickOffsetType) 1 ? MagickFalse : MagickTrue);
197}
198
cristy106efa12014-03-09 01:06:05 +0000199MagickExport MatrixInfo *AcquireMatrixInfo(const size_t columns,
200 const size_t rows,const size_t stride,ExceptionInfo *exception)
201{
cristyc1712b22014-03-09 14:54:05 +0000202 char
203 *synchronize;
204
cristy106efa12014-03-09 01:06:05 +0000205 MagickBooleanType
206 status;
207
208 MatrixInfo
209 *matrix_info;
210
211 matrix_info=(MatrixInfo *) AcquireMagickMemory(sizeof(*matrix_info));
212 if (matrix_info == (MatrixInfo *) NULL)
213 return((MatrixInfo *) NULL);
214 (void) ResetMagickMemory(matrix_info,0,sizeof(*matrix_info));
cristye1c94d92015-06-28 12:16:33 +0000215 matrix_info->signature=MagickCoreSignature;
cristy106efa12014-03-09 01:06:05 +0000216 matrix_info->columns=columns;
217 matrix_info->rows=rows;
218 matrix_info->stride=stride;
cristycba9e952014-03-30 23:17:57 +0000219 matrix_info->semaphore=AcquireSemaphoreInfo();
cristyc1712b22014-03-09 14:54:05 +0000220 synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
221 if (synchronize != (const char *) NULL)
222 {
223 matrix_info->synchronize=IsStringTrue(synchronize);
224 synchronize=DestroyString(synchronize);
225 }
cristy106efa12014-03-09 01:06:05 +0000226 matrix_info->length=(MagickSizeType) columns*rows*stride;
227 if (matrix_info->columns != (size_t) (matrix_info->length/rows/stride))
228 {
229 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
230 "CacheResourcesExhausted","`%s'","matrix cache");
231 return(DestroyMatrixInfo(matrix_info));
232 }
233 matrix_info->type=MemoryCache;
234 status=AcquireMagickResource(AreaResource,matrix_info->length);
235 if ((status != MagickFalse) &&
236 (matrix_info->length == (MagickSizeType) ((size_t) matrix_info->length)))
237 {
238 status=AcquireMagickResource(MemoryResource,matrix_info->length);
239 if (status != MagickFalse)
240 {
cristyc1712b22014-03-09 14:54:05 +0000241 matrix_info->mapped=MagickFalse;
cristy106efa12014-03-09 01:06:05 +0000242 matrix_info->elements=AcquireMagickMemory((size_t)
243 matrix_info->length);
244 if (matrix_info->elements == NULL)
245 {
246 matrix_info->mapped=MagickTrue;
247 matrix_info->elements=MapBlob(-1,IOMode,0,(size_t)
248 matrix_info->length);
249 }
250 if (matrix_info->elements == (unsigned short *) NULL)
251 RelinquishMagickResource(MemoryResource,matrix_info->length);
252 }
253 }
254 matrix_info->file=(-1);
255 if (matrix_info->elements == (unsigned short *) NULL)
256 {
257 status=AcquireMagickResource(DiskResource,matrix_info->length);
258 if (status == MagickFalse)
259 {
260 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
261 "CacheResourcesExhausted","`%s'","matrix cache");
262 return(DestroyMatrixInfo(matrix_info));
263 }
264 matrix_info->type=DiskCache;
265 (void) AcquireMagickResource(MemoryResource,matrix_info->length);
266 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)
274 {
275 matrix_info->elements=(void *) MapBlob(matrix_info->file,IOMode,0,
276 (size_t) matrix_info->length);
277 if (matrix_info->elements != NULL)
278 matrix_info->type=MapCache;
279 else
280 RelinquishMagickResource(MapResource,matrix_info->length);
281 }
282 }
283 }
284 return(matrix_info);
285}
cristy3ed852e2009-09-05 21:47:34 +0000286
287/*
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289% %
290% %
291% %
292% A c q u i r e M a g i c k M a t r i x %
293% %
294% %
295% %
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297%
298% AcquireMagickMatrix() allocates and returns a matrix in the form of an
299% array of pointers to an array of doubles, with all values pre-set to zero.
300%
cristy106efa12014-03-09 01:06:05 +0000301% This used to generate the two dimensional matrix, and vectors required
302% for the GaussJordanElimination() method below, solving some system of
303% simultanious equations.
cristy3ed852e2009-09-05 21:47:34 +0000304%
305% The format of the AcquireMagickMatrix method is:
306%
cristybb503372010-05-27 20:51:26 +0000307% double **AcquireMagickMatrix(const size_t number_rows,
308% const size_t size)
cristy3ed852e2009-09-05 21:47:34 +0000309%
310% A description of each parameter follows:
311%
cristy74908cf2010-05-17 19:43:54 +0000312% o number_rows: the number pointers for the array of pointers
cristy1ad491d2010-05-17 19:45:27 +0000313% (first dimension).
cristy3ed852e2009-09-05 21:47:34 +0000314%
cristy1ad491d2010-05-17 19:45:27 +0000315% o size: the size of the array of doubles each pointer points to
316% (second dimension).
cristy3ed852e2009-09-05 21:47:34 +0000317%
318*/
cristybb503372010-05-27 20:51:26 +0000319MagickExport double **AcquireMagickMatrix(const size_t number_rows,
320 const size_t size)
cristy3ed852e2009-09-05 21:47:34 +0000321{
322 double
cristy74908cf2010-05-17 19:43:54 +0000323 **matrix;
cristy3ed852e2009-09-05 21:47:34 +0000324
cristybb503372010-05-27 20:51:26 +0000325 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000326 i,
327 j;
328
cristy74908cf2010-05-17 19:43:54 +0000329 matrix=(double **) AcquireQuantumMemory(number_rows,sizeof(*matrix));
cristy3ed852e2009-09-05 21:47:34 +0000330 if (matrix == (double **) NULL)
cristyf432c632014-12-07 15:11:28 +0000331 return((double **) NULL);
cristybb503372010-05-27 20:51:26 +0000332 for (i=0; i < (ssize_t) number_rows; i++)
cristy3ed852e2009-09-05 21:47:34 +0000333 {
334 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
335 if (matrix[i] == (double *) NULL)
336 {
337 for (j=0; j < i; j++)
338 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
339 matrix=(double **) RelinquishMagickMemory(matrix);
340 return((double **) NULL);
341 }
cristybb503372010-05-27 20:51:26 +0000342 for (j=0; j < (ssize_t) size; j++)
cristy74908cf2010-05-17 19:43:54 +0000343 matrix[i][j]=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000344 }
345 return(matrix);
346}
347
348/*
349%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350% %
351% %
352% %
cristy106efa12014-03-09 01:06:05 +0000353% D e s t r o y M a t r i x I n f o %
354% %
355% %
356% %
357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358%
359% DestroyMatrixInfo() dereferences a matrix, deallocating memory associated
360% with the matrix.
361%
362% The format of the DestroyImage method is:
363%
364% MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
365%
366% A description of each parameter follows:
367%
368% o matrix_info: the matrix.
369%
370*/
371MagickExport MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
372{
373 assert(matrix_info != (MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000374 assert(matrix_info->signature == MagickCoreSignature);
cristycba9e952014-03-30 23:17:57 +0000375 LockSemaphoreInfo(matrix_info->semaphore);
cristy106efa12014-03-09 01:06:05 +0000376 switch (matrix_info->type)
377 {
378 case MemoryCache:
379 {
380 if (matrix_info->mapped == MagickFalse)
381 matrix_info->elements=RelinquishMagickMemory(matrix_info->elements);
382 else
383 {
384 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
385 matrix_info->elements=(unsigned short *) NULL;
386 }
387 RelinquishMagickResource(MemoryResource,matrix_info->length);
388 break;
389 }
390 case MapCache:
391 {
392 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
393 matrix_info->elements=NULL;
394 RelinquishMagickResource(MapResource,matrix_info->length);
395 }
396 case DiskCache:
397 {
398 if (matrix_info->file != -1)
399 (void) close(matrix_info->file);
400 (void) RelinquishUniqueFileResource(matrix_info->path);
401 RelinquishMagickResource(DiskResource,matrix_info->length);
402 break;
403 }
404 default:
405 break;
406 }
cristycba9e952014-03-30 23:17:57 +0000407 UnlockSemaphoreInfo(matrix_info->semaphore);
408 RelinquishSemaphoreInfo(&matrix_info->semaphore);
cristy106efa12014-03-09 01:06:05 +0000409 return((MatrixInfo *) RelinquishMagickMemory(matrix_info));
410}
411
412/*
413%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414% %
415% %
416% %
cristy3ed852e2009-09-05 21:47:34 +0000417% G a u s s J o r d a n E l i m i n a t i o n %
418% %
419% %
420% %
421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422%
423% GaussJordanElimination() returns a matrix in reduced row echelon form,
424% while simultaneously reducing and thus solving the augumented results
425% matrix.
426%
427% See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
428%
429% The format of the GaussJordanElimination method is:
430%
cristy7dbde212014-10-11 06:59:40 +0000431% MagickBooleanType GaussJordanElimination(double **matrix,
432% double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000433%
434% A description of each parameter follows:
435%
436% o matrix: the matrix to be reduced, as an 'array of row pointers'.
437%
438% o vectors: the additional matrix argumenting the matrix for row reduction.
439% Producing an 'array of column vectors'.
440%
cristy106efa12014-03-09 01:06:05 +0000441% o rank: The size of the matrix (both rows and columns).
cristy3ed852e2009-09-05 21:47:34 +0000442% Also represents the number terms that need to be solved.
443%
cristy74908cf2010-05-17 19:43:54 +0000444% o number_vectors: Number of vectors columns, argumenting the above matrix.
cristy3ed852e2009-09-05 21:47:34 +0000445% Usally 1, but can be more for more complex equation solving.
446%
447% Note that the 'matrix' is given as a 'array of row pointers' of rank size.
448% That is values can be assigned as matrix[row][column] where 'row' is
449% typically the equation, and 'column' is the term of the equation.
450% That is the matrix is in the form of a 'row first array'.
451%
452% However 'vectors' is a 'array of column pointers' which can have any number
453% of columns, with each column array the same 'rank' size as 'matrix'.
454%
455% This allows for simpler handling of the results, especially is only one
cristy106efa12014-03-09 01:06:05 +0000456% column 'vector' is all that is required to produce the desired solution.
cristy3ed852e2009-09-05 21:47:34 +0000457%
458% For example, the 'vectors' can consist of a pointer to a simple array of
459% doubles. when only one set of simultanious equations is to be solved from
460% the given set of coefficient weighted terms.
461%
462% double **matrix = AcquireMagickMatrix(8UL,8UL);
463% double coefficents[8];
464% ...
465% GaussJordanElimination(matrix, &coefficents, 8UL, 1UL);
anthony34364f42011-10-21 05:31:53 +0000466%
cristy106efa12014-03-09 01:06:05 +0000467% However by specifing more 'columns' (as an 'array of vector columns',
468% you can use this function to solve a set of 'separable' equations.
cristy3ed852e2009-09-05 21:47:34 +0000469%
470% For example a distortion function where u = U(x,y) v = V(x,y)
471% And the functions U() and V() have separate coefficents, but are being
472% generated from a common x,y->u,v data set.
473%
cristy7dbde212014-10-11 06:59:40 +0000474% Another example is generation of a color gradient from a set of colors at
475% specific coordients, such as a list x,y -> r,g,b,a.
cristy3ed852e2009-09-05 21:47:34 +0000476%
477% You can also use the 'vectors' to generate an inverse of the given 'matrix'
cristy106efa12014-03-09 01:06:05 +0000478% though as a 'column first array' rather than a 'row first array'. For
cristy7dbde212014-10-11 06:59:40 +0000479% details see http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
cristy3ed852e2009-09-05 21:47:34 +0000480%
481*/
cristy106efa12014-03-09 01:06:05 +0000482MagickExport MagickBooleanType GaussJordanElimination(double **matrix,
cristybb503372010-05-27 20:51:26 +0000483 double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000484{
485#define GaussJordanSwap(x,y) \
486{ \
487 if ((x) != (y)) \
488 { \
489 (x)+=(y); \
490 (y)=(x)-(y); \
491 (x)=(x)-(y); \
492 } \
493}
494
495 double
496 max,
497 scale;
498
cristy9d314ff2011-03-09 01:30:28 +0000499 register ssize_t
500 i,
501 j,
502 k;
503
cristybb503372010-05-27 20:51:26 +0000504 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000505 column,
506 *columns,
507 *pivots,
508 row,
509 *rows;
510
cristybb503372010-05-27 20:51:26 +0000511 columns=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*columns));
512 rows=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*rows));
513 pivots=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*pivots));
514 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
515 (pivots == (ssize_t *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000516 {
cristybb503372010-05-27 20:51:26 +0000517 if (pivots != (ssize_t *) NULL)
518 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
519 if (columns != (ssize_t *) NULL)
520 columns=(ssize_t *) RelinquishMagickMemory(columns);
521 if (rows != (ssize_t *) NULL)
522 rows=(ssize_t *) RelinquishMagickMemory(rows);
cristy3ed852e2009-09-05 21:47:34 +0000523 return(MagickFalse);
524 }
525 (void) ResetMagickMemory(columns,0,rank*sizeof(*columns));
526 (void) ResetMagickMemory(rows,0,rank*sizeof(*rows));
527 (void) ResetMagickMemory(pivots,0,rank*sizeof(*pivots));
528 column=0;
529 row=0;
cristybb503372010-05-27 20:51:26 +0000530 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000531 {
532 max=0.0;
cristybb503372010-05-27 20:51:26 +0000533 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000534 if (pivots[j] != 1)
535 {
cristybb503372010-05-27 20:51:26 +0000536 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000537 if (pivots[k] != 0)
538 {
539 if (pivots[k] > 1)
540 return(MagickFalse);
541 }
542 else
543 if (fabs(matrix[j][k]) >= max)
544 {
545 max=fabs(matrix[j][k]);
546 row=j;
547 column=k;
548 }
549 }
550 pivots[column]++;
551 if (row != column)
552 {
cristybb503372010-05-27 20:51:26 +0000553 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000554 GaussJordanSwap(matrix[row][k],matrix[column][k]);
cristybb503372010-05-27 20:51:26 +0000555 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000556 GaussJordanSwap(vectors[k][row],vectors[k][column]);
557 }
558 rows[i]=row;
559 columns[i]=column;
560 if (matrix[column][column] == 0.0)
cristy106efa12014-03-09 01:06:05 +0000561 return(MagickFalse); /* sigularity */
cristy3e3ec3a2012-11-03 23:11:06 +0000562 scale=PerceptibleReciprocal(matrix[column][column]);
cristy3ed852e2009-09-05 21:47:34 +0000563 matrix[column][column]=1.0;
cristybb503372010-05-27 20:51:26 +0000564 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000565 matrix[column][j]*=scale;
cristybb503372010-05-27 20:51:26 +0000566 for (j=0; j < (ssize_t) number_vectors; j++)
cristy3ed852e2009-09-05 21:47:34 +0000567 vectors[j][column]*=scale;
cristybb503372010-05-27 20:51:26 +0000568 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000569 if (j != column)
570 {
571 scale=matrix[j][column];
572 matrix[j][column]=0.0;
cristybb503372010-05-27 20:51:26 +0000573 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000574 matrix[j][k]-=scale*matrix[column][k];
cristybb503372010-05-27 20:51:26 +0000575 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000576 vectors[k][j]-=scale*vectors[k][column];
577 }
578 }
cristybb503372010-05-27 20:51:26 +0000579 for (j=(ssize_t) rank-1; j >= 0; j--)
cristy3ed852e2009-09-05 21:47:34 +0000580 if (columns[j] != rows[j])
cristybb503372010-05-27 20:51:26 +0000581 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000582 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
cristybb503372010-05-27 20:51:26 +0000583 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
584 rows=(ssize_t *) RelinquishMagickMemory(rows);
585 columns=(ssize_t *) RelinquishMagickMemory(columns);
cristy3ed852e2009-09-05 21:47:34 +0000586 return(MagickTrue);
587}
588
589/*
590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591% %
592% %
593% %
cristy106efa12014-03-09 01:06:05 +0000594% G e t M a t r i x C o l u m n s %
595% %
596% %
597% %
598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599%
600% GetMatrixColumns() returns the number of columns in the matrix.
601%
602% The format of the GetMatrixColumns method is:
603%
604% size_t GetMatrixColumns(const MatrixInfo *matrix_info)
605%
606% A description of each parameter follows:
607%
608% o matrix_info: the matrix.
609%
610*/
611MagickExport size_t GetMatrixColumns(const MatrixInfo *matrix_info)
612{
613 assert(matrix_info != (MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000614 assert(matrix_info->signature == MagickCoreSignature);
cristy106efa12014-03-09 01:06:05 +0000615 return(matrix_info->columns);
616}
617
618/*
619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
620% %
621% %
622% %
623% G e t M a t r i x E l e m e n t %
624% %
625% %
626% %
627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628%
629% GetMatrixElement() returns the specifed element in the matrix.
630%
631% The format of the GetMatrixElement method is:
632%
633% MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
634% const ssize_t x,const ssize_t y,void *value)
635%
636% A description of each parameter follows:
637%
638% o matrix_info: the matrix columns.
639%
640% o x: the matrix x-offset.
641%
642% o y: the matrix y-offset.
643%
644% o value: return the matrix element in this buffer.
645%
646*/
647
cristy6cccee42014-03-23 00:25:22 +0000648static inline ssize_t EdgeX(const ssize_t x,const size_t columns)
649{
650 if (x < 0L)
651 return(0L);
652 if (x >= (ssize_t) columns)
653 return((ssize_t) (columns-1));
654 return(x);
655}
656
657static inline ssize_t EdgeY(const ssize_t y,const size_t rows)
658{
659 if (y < 0L)
660 return(0L);
661 if (y >= (ssize_t) rows)
662 return((ssize_t) (rows-1));
663 return(y);
664}
665
cristyc1712b22014-03-09 14:54:05 +0000666static inline MagickOffsetType ReadMatrixElements(
667 const MatrixInfo *restrict matrix_info,const MagickOffsetType offset,
668 const MagickSizeType length,unsigned char *restrict buffer)
cristy106efa12014-03-09 01:06:05 +0000669{
cristyc1712b22014-03-09 14:54:05 +0000670 register MagickOffsetType
cristy106efa12014-03-09 01:06:05 +0000671 i;
672
673 ssize_t
674 count;
675
cristyc1712b22014-03-09 14:54:05 +0000676#if !defined(MAGICKCORE_HAVE_PREAD)
cristy10c210e2014-03-30 23:27:24 +0000677 LockSemaphoreInfo(matrix_info->semaphore);
cristyc1712b22014-03-09 14:54:05 +0000678 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
cristy10c210e2014-03-30 23:27:24 +0000679 {
680 UnlockSemaphoreInfo(matrix_info->semaphore);
681 return((MagickOffsetType) -1);
682 }
cristy106efa12014-03-09 01:06:05 +0000683#endif
cristyc1712b22014-03-09 14:54:05 +0000684 count=0;
685 for (i=0; i < (MagickOffsetType) length; i+=count)
cristy106efa12014-03-09 01:06:05 +0000686 {
cristyc1712b22014-03-09 14:54:05 +0000687#if !defined(MAGICKCORE_HAVE_PREAD)
688 count=read(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
689 (MagickSizeType) SSIZE_MAX));
cristy106efa12014-03-09 01:06:05 +0000690#else
cristyc1712b22014-03-09 14:54:05 +0000691 count=pread(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
692 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
cristy106efa12014-03-09 01:06:05 +0000693#endif
cristyc1712b22014-03-09 14:54:05 +0000694 if (count <= 0)
695 {
696 count=0;
697 if (errno != EINTR)
698 break;
cristy106efa12014-03-09 01:06:05 +0000699 }
700 }
cristy10c210e2014-03-30 23:27:24 +0000701#if !defined(MAGICKCORE_HAVE_PREAD)
702 UnlockSemaphoreInfo(matrix_info->semaphore);
703#endif
cristy106efa12014-03-09 01:06:05 +0000704 return(i);
705}
706
707MagickExport MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
708 const ssize_t x,const ssize_t y,void *value)
709{
710 MagickOffsetType
cristyc1712b22014-03-09 14:54:05 +0000711 count,
cristy106efa12014-03-09 01:06:05 +0000712 i;
713
cristy106efa12014-03-09 01:06:05 +0000714 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000715 assert(matrix_info->signature == MagickCoreSignature);
cristy6cccee42014-03-23 00:25:22 +0000716 i=(MagickOffsetType) EdgeY(y,matrix_info->rows)*matrix_info->columns+
717 EdgeX(x,matrix_info->columns);
cristy106efa12014-03-09 01:06:05 +0000718 if (matrix_info->type != DiskCache)
719 {
cristy7a729512014-03-09 16:43:07 +0000720 (void) memcpy(value,(unsigned char *) matrix_info->elements+i*
721 matrix_info->stride,matrix_info->stride);
cristy106efa12014-03-09 01:06:05 +0000722 return(MagickTrue);
723 }
cristyc1712b22014-03-09 14:54:05 +0000724 count=ReadMatrixElements(matrix_info,i*matrix_info->stride,
cristy759ba912014-06-26 11:59:43 +0000725 matrix_info->stride,(unsigned char *) value);
cristyc1712b22014-03-09 14:54:05 +0000726 if (count != (MagickOffsetType) matrix_info->stride)
cristy106efa12014-03-09 01:06:05 +0000727 return(MagickFalse);
728 return(MagickTrue);
729}
730
731/*
732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733% %
734% %
735% %
736% G e t M a t r i x R o w s %
737% %
738% %
739% %
740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
741%
742% GetMatrixRows() returns the number of rows in the matrix.
743%
744% The format of the GetMatrixRows method is:
745%
746% size_t GetMatrixRows(const MatrixInfo *matrix_info)
747%
748% A description of each parameter follows:
749%
750% o matrix_info: the matrix.
751%
752*/
753MagickExport size_t GetMatrixRows(const MatrixInfo *matrix_info)
754{
755 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000756 assert(matrix_info->signature == MagickCoreSignature);
cristy106efa12014-03-09 01:06:05 +0000757 return(matrix_info->rows);
758}
759
760/*
761%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762% %
763% %
764% %
cristy3ed852e2009-09-05 21:47:34 +0000765% L e a s t S q u a r e s A d d T e r m s %
766% %
767% %
768% %
769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770%
771% LeastSquaresAddTerms() adds one set of terms and associate results to the
772% given matrix and vectors for solving using least-squares function fitting.
773%
774% The format of the AcquireMagickMatrix method is:
775%
776% void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000777% const double *terms,const double *results,const size_t rank,
778% const size_t number_vectors);
cristy3ed852e2009-09-05 21:47:34 +0000779%
780% A description of each parameter follows:
781%
782% o matrix: the square matrix to add given terms/results to.
783%
784% o vectors: the result vectors to add terms/results to.
785%
786% o terms: the pre-calculated terms (without the unknown coefficent
cristy106efa12014-03-09 01:06:05 +0000787% weights) that forms the equation being added.
cristy3ed852e2009-09-05 21:47:34 +0000788%
789% o results: the result(s) that should be generated from the given terms
cristy106efa12014-03-09 01:06:05 +0000790% weighted by the yet-to-be-solved coefficents.
cristy3ed852e2009-09-05 21:47:34 +0000791%
cristy106efa12014-03-09 01:06:05 +0000792% o rank: the rank or size of the dimensions of the square matrix.
793% Also the length of vectors, and number of terms being added.
cristy3ed852e2009-09-05 21:47:34 +0000794%
cristy1ad491d2010-05-17 19:45:27 +0000795% o number_vectors: Number of result vectors, and number or results being
796% added. Also represents the number of separable systems of equations
797% that is being solved.
cristy3ed852e2009-09-05 21:47:34 +0000798%
799% Example of use...
800%
glennrp2489f532011-06-25 03:02:43 +0000801% 2 dimensional Affine Equations (which are separable)
cristy3ed852e2009-09-05 21:47:34 +0000802% c0*x + c2*y + c4*1 => u
803% c1*x + c3*y + c5*1 => v
804%
805% double **matrix = AcquireMagickMatrix(3UL,3UL);
806% double **vectors = AcquireMagickMatrix(2UL,3UL);
807% double terms[3], results[2];
808% ...
809% for each given x,y -> u,v
810% terms[0] = x;
811% terms[1] = y;
812% terms[2] = 1;
813% results[0] = u;
814% results[1] = v;
815% LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
816% ...
817% if ( GaussJordanElimination(matrix,vectors,3UL,2UL) ) {
818% c0 = vectors[0][0];
cristy106efa12014-03-09 01:06:05 +0000819% c2 = vectors[0][1];
cristy3ed852e2009-09-05 21:47:34 +0000820% c4 = vectors[0][2];
821% c1 = vectors[1][0];
cristy106efa12014-03-09 01:06:05 +0000822% c3 = vectors[1][1];
cristy3ed852e2009-09-05 21:47:34 +0000823% c5 = vectors[1][2];
824% }
825% else
826% printf("Matrix unsolvable\n);
827% RelinquishMagickMatrix(matrix,3UL);
828% RelinquishMagickMatrix(vectors,2UL);
829%
830*/
cristy106efa12014-03-09 01:06:05 +0000831MagickExport void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000832 const double *terms,const double *results,const size_t rank,
833 const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000834{
cristybb503372010-05-27 20:51:26 +0000835 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000836 i,
837 j;
838
cristybb503372010-05-27 20:51:26 +0000839 for (j=0; j < (ssize_t) rank; j++)
cristy74908cf2010-05-17 19:43:54 +0000840 {
cristybb503372010-05-27 20:51:26 +0000841 for (i=0; i < (ssize_t) rank; i++)
cristy74908cf2010-05-17 19:43:54 +0000842 matrix[i][j]+=terms[i]*terms[j];
cristybb503372010-05-27 20:51:26 +0000843 for (i=0; i < (ssize_t) number_vectors; i++)
cristy74908cf2010-05-17 19:43:54 +0000844 vectors[i][j]+=results[i]*terms[j];
cristy3ed852e2009-09-05 21:47:34 +0000845 }
cristy3ed852e2009-09-05 21:47:34 +0000846}
847
848/*
849%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850% %
851% %
852% %
cristyf3b58c52014-04-22 01:27:24 +0000853% M a t r i x T o I m a g e %
854% %
855% %
856% %
857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858%
cristy0f09a8c2014-04-23 00:20:40 +0000859% MatrixToImage() returns a matrix as an image. The matrix elements must be
860% of type double otherwise nonsense is returned.
cristyf3b58c52014-04-22 01:27:24 +0000861%
862% The format of the MatrixToImage method is:
863%
864% Image *MatrixToImage(const MatrixInfo *matrix_info,
865% ExceptionInfo *exception)
866%
867% A description of each parameter follows:
868%
869% o matrix_info: the matrix.
870%
871% o exception: return any errors or warnings in this structure.
872%
873*/
874MagickExport Image *MatrixToImage(const MatrixInfo *matrix_info,
875 ExceptionInfo *exception)
876{
cristy0f09a8c2014-04-23 00:20:40 +0000877 CacheView
878 *image_view;
879
880 double
881 max_value,
882 min_value,
883 scale_factor,
884 value;
885
886 Image
887 *image;
888
889 MagickBooleanType
890 status;
891
892 ssize_t
893 y;
894
895 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000896 assert(matrix_info->signature == MagickCoreSignature);
cristy0f09a8c2014-04-23 00:20:40 +0000897 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000898 assert(exception->signature == MagickCoreSignature);
cristy0f09a8c2014-04-23 00:20:40 +0000899 if (matrix_info->stride < sizeof(double))
900 return((Image *) NULL);
901 /*
902 Determine range of matrix.
903 */
904 (void) GetMatrixElement(matrix_info,0,0,&value);
905 min_value=value;
906 max_value=value;
907 for (y=0; y < (ssize_t) matrix_info->rows; y++)
908 {
909 register ssize_t
910 x;
911
912 for (x=0; x < (ssize_t) matrix_info->columns; x++)
913 {
914 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
915 continue;
916 if (value < min_value)
917 min_value=value;
918 else
919 if (value > max_value)
920 max_value=value;
921 }
922 }
923 if ((min_value == 0.0) && (max_value == 0.0))
924 scale_factor=0;
925 else
926 if (min_value == max_value)
927 {
928 scale_factor=(double) QuantumRange/min_value;
929 min_value=0;
930 }
931 else
932 scale_factor=(double) QuantumRange/(max_value-min_value);
933 /*
934 Convert matrix to image.
935 */
936 image=AcquireImage((ImageInfo *) NULL,exception);
937 image->columns=matrix_info->columns;
938 image->rows=matrix_info->rows;
939 image->colorspace=GRAYColorspace;
940 status=MagickTrue;
941 image_view=AcquireAuthenticCacheView(image,exception);
942#if defined(MAGICKCORE_OPENMP_SUPPORT)
943 #pragma omp parallel for schedule(static,4) shared(status) \
944 magick_threads(image,image,image->rows,1)
945#endif
946 for (y=0; y < (ssize_t) image->rows; y++)
947 {
948 double
949 value;
950
951 register Quantum
952 *q;
953
954 register ssize_t
955 x;
956
957 if (status == MagickFalse)
958 continue;
959 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
960 if (q == (Quantum *) NULL)
961 {
962 status=MagickFalse;
963 continue;
964 }
965 for (x=0; x < (ssize_t) image->columns; x++)
966 {
967 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
968 continue;
969 value=scale_factor*(value-min_value);
970 *q=ClampToQuantum(value);
971 q+=GetPixelChannels(image);
972 }
973 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
974 status=MagickFalse;
975 }
976 image_view=DestroyCacheView(image_view);
977 if (status == MagickFalse)
978 image=DestroyImage(image);
979 return(image);
cristyf3b58c52014-04-22 01:27:24 +0000980}
981
982/*
983%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984% %
985% %
986% %
cristyc1712b22014-03-09 14:54:05 +0000987% N u l l M a t r i x %
988% %
989% %
990% %
991%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992%
993% NullMatrix() sets all elements of the matrix to zero.
994%
995% The format of the ResetMagickMemory method is:
996%
997% MagickBooleanType *NullMatrix(MatrixInfo *matrix_info)
998%
999% A description of each parameter follows:
1000%
1001% o matrix_info: the matrix.
1002%
1003*/
1004MagickExport MagickBooleanType NullMatrix(MatrixInfo *matrix_info)
1005{
1006 register ssize_t
1007 x;
1008
1009 ssize_t
1010 count,
1011 y;
1012
1013 unsigned char
1014 value;
1015
1016 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001017 assert(matrix_info->signature == MagickCoreSignature);
cristyc1712b22014-03-09 14:54:05 +00001018 if (matrix_info->type != DiskCache)
1019 {
1020 (void) ResetMagickMemory(matrix_info->elements,0,(size_t)
1021 matrix_info->length);
1022 return(MagickTrue);
1023 }
1024 value=0;
1025 (void) lseek(matrix_info->file,0,SEEK_SET);
1026 for (y=0; y < (ssize_t) matrix_info->rows; y++)
1027 {
1028 for (x=0; x < (ssize_t) matrix_info->length; x++)
1029 {
1030 count=write(matrix_info->file,&value,sizeof(value));
1031 if (count != (ssize_t) sizeof(value))
1032 break;
1033 }
1034 if (x < (ssize_t) matrix_info->length)
1035 break;
1036 }
1037 return(y < (ssize_t) matrix_info->rows ? MagickFalse : MagickTrue);
1038}
1039
1040/*
1041%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1042% %
1043% %
1044% %
cristy3ed852e2009-09-05 21:47:34 +00001045% R e l i n q u i s h M a g i c k M a t r i x %
1046% %
1047% %
1048% %
1049%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050%
1051% RelinquishMagickMatrix() frees the previously acquired matrix (array of
1052% pointers to arrays of doubles).
1053%
1054% The format of the RelinquishMagickMatrix method is:
1055%
1056% double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +00001057% const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +00001058%
1059% A description of each parameter follows:
1060%
1061% o matrix: the matrix to relinquish
1062%
cristy1ad491d2010-05-17 19:45:27 +00001063% o number_rows: the first dimension of the acquired matrix (number of
1064% pointers)
cristy3ed852e2009-09-05 21:47:34 +00001065%
1066*/
1067MagickExport double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +00001068 const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +00001069{
cristybb503372010-05-27 20:51:26 +00001070 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001071 i;
1072
1073 if (matrix == (double **) NULL )
1074 return(matrix);
cristybb503372010-05-27 20:51:26 +00001075 for (i=0; i < (ssize_t) number_rows; i++)
cristy3ed852e2009-09-05 21:47:34 +00001076 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
1077 matrix=(double **) RelinquishMagickMemory(matrix);
cristy3ed852e2009-09-05 21:47:34 +00001078 return(matrix);
1079}
cristy106efa12014-03-09 01:06:05 +00001080
1081/*
1082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1083% %
1084% %
1085% %
cristy106efa12014-03-09 01:06:05 +00001086% S e t M a t r i x E l e m e n t %
1087% %
1088% %
1089% %
1090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091%
1092% SetMatrixElement() sets the specifed element in the matrix.
1093%
1094% The format of the SetMatrixElement method is:
1095%
1096% MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1097% const ssize_t x,const ssize_t y,void *value)
1098%
1099% A description of each parameter follows:
1100%
1101% o matrix_info: the matrix columns.
1102%
1103% o x: the matrix x-offset.
1104%
1105% o y: the matrix y-offset.
1106%
1107% o value: set the matrix element to this value.
1108%
1109*/
1110
cristy106efa12014-03-09 01:06:05 +00001111MagickExport MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1112 const ssize_t x,const ssize_t y,const void *value)
1113{
1114 MagickOffsetType
cristyc1712b22014-03-09 14:54:05 +00001115 count,
cristy106efa12014-03-09 01:06:05 +00001116 i;
1117
cristy106efa12014-03-09 01:06:05 +00001118 assert(matrix_info != (const MatrixInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001119 assert(matrix_info->signature == MagickCoreSignature);
cristy6cccee42014-03-23 00:25:22 +00001120 i=(MagickOffsetType) y*matrix_info->columns+x;
cristy106efa12014-03-09 01:06:05 +00001121 if ((i < 0) ||
1122 ((MagickSizeType) (i*matrix_info->stride) >= matrix_info->length))
1123 return(MagickFalse);
1124 if (matrix_info->type != DiskCache)
1125 {
cristy7a729512014-03-09 16:43:07 +00001126 (void) memcpy((unsigned char *) matrix_info->elements+i*
1127 matrix_info->stride,value,matrix_info->stride);
cristy106efa12014-03-09 01:06:05 +00001128 return(MagickTrue);
1129 }
cristyc1712b22014-03-09 14:54:05 +00001130 count=WriteMatrixElements(matrix_info,i*matrix_info->stride,
cristy759ba912014-06-26 11:59:43 +00001131 matrix_info->stride,(unsigned char *) value);
cristyc1712b22014-03-09 14:54:05 +00001132 if (count != (MagickOffsetType) matrix_info->stride)
cristy106efa12014-03-09 01:06:05 +00001133 return(MagickFalse);
1134 return(MagickTrue);
1135}