blob: defcd2beb2a1f6781c8868c1ca293c7f7f02977c [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"
cristy4c08aed2011-07-01 19:47:50 +000048#include "MagickCore/matrix.h"
49#include "MagickCore/memory_.h"
cristy0f09a8c2014-04-23 00:20:40 +000050#include "MagickCore/pixel-accessor.h"
cristy106efa12014-03-09 01:06:05 +000051#include "MagickCore/pixel-private.h"
52#include "MagickCore/resource_.h"
cristycba9e952014-03-30 23:17:57 +000053#include "MagickCore/semaphore.h"
cristyf3b58c52014-04-22 01:27:24 +000054#include "MagickCore/thread-private.h"
cristy106efa12014-03-09 01:06:05 +000055#include "MagickCore/utility.h"
56
57/*
58 Typedef declaration.
59*/
60struct _MatrixInfo
61{
62 CacheType
63 type;
64
65 size_t
66 columns,
67 rows,
68 stride;
69
70 MagickSizeType
71 length;
72
73 MagickBooleanType
cristyc1712b22014-03-09 14:54:05 +000074 mapped,
75 synchronize;
cristy106efa12014-03-09 01:06:05 +000076
77 char
78 path[MaxTextExtent];
79
80 int
81 file;
82
83 void
84 *elements;
85
cristycba9e952014-03-30 23:17:57 +000086 SemaphoreInfo
87 *semaphore;
88
cristy106efa12014-03-09 01:06:05 +000089 size_t
90 signature;
91};
92
93/*
94%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95% %
96% %
97% %
98% A c q u i r e M a t r i x I n f o %
99% %
100% %
101% %
102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103%
cristyc1712b22014-03-09 14:54:05 +0000104% AcquireMatrixInfo() allocates the ImageInfo structure.
cristy106efa12014-03-09 01:06:05 +0000105%
106% The format of the AcquireMatrixInfo method is:
107%
108% MatrixInfo *AcquireMatrixInfo(const size_t columns,const size_t rows,
109% const size_t stride,ExceptionInfo *exception)
110%
111% A description of each parameter follows:
112%
113% o columns: the matrix columns.
114%
115% o rows: the matrix rows.
116%
117% o stride: the matrix stride.
118%
119% o exception: return any errors or warnings in this structure.
120%
121*/
cristyc1712b22014-03-09 14:54:05 +0000122
123static inline MagickSizeType MagickMin(const MagickSizeType x,
124 const MagickSizeType y)
125{
126 if (x < y)
127 return(x);
128 return(y);
129}
130
131#if defined(SIGBUS)
132static void MatrixSignalHandler(int status)
133{
134 ThrowFatalException(CacheFatalError,"UnableToExtendMatrixCache");
135}
136#endif
137
138static inline MagickOffsetType WriteMatrixElements(
139 const MatrixInfo *restrict matrix_info,const MagickOffsetType offset,
140 const MagickSizeType length,const unsigned char *restrict buffer)
141{
142 register MagickOffsetType
143 i;
144
145 ssize_t
146 count;
147
148#if !defined(MAGICKCORE_HAVE_PWRITE)
cristy10c210e2014-03-30 23:27:24 +0000149 LockSemaphoreInfo(matrix_info->semaphore);
cristyc1712b22014-03-09 14:54:05 +0000150 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
cristy10c210e2014-03-30 23:27:24 +0000151 {
152 UnlockSemaphoreInfo(matrix_info->semaphore);
153 return((MagickOffsetType) -1);
154 }
cristyc1712b22014-03-09 14:54:05 +0000155#endif
156 count=0;
157 for (i=0; i < (MagickOffsetType) length; i+=count)
158 {
159#if !defined(MAGICKCORE_HAVE_PWRITE)
160 count=write(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
161 (MagickSizeType) SSIZE_MAX));
162#else
163 count=pwrite(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
164 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
165#endif
166 if (count <= 0)
167 {
168 count=0;
169 if (errno != EINTR)
170 break;
171 }
172 }
cristy10c210e2014-03-30 23:27:24 +0000173#if !defined(MAGICKCORE_HAVE_PWRITE)
174 UnlockSemaphoreInfo(matrix_info->semaphore);
175#endif
cristyc1712b22014-03-09 14:54:05 +0000176 return(i);
177}
178
179static MagickBooleanType SetMatrixExtent(MatrixInfo *restrict matrix_info,
180 MagickSizeType length)
181{
182 MagickOffsetType
183 count,
184 extent,
185 offset;
186
187 if (length != (MagickSizeType) ((MagickOffsetType) length))
188 return(MagickFalse);
189 offset=(MagickOffsetType) lseek(matrix_info->file,0,SEEK_END);
190 if (offset < 0)
191 return(MagickFalse);
192 if ((MagickSizeType) offset >= length)
193 return(MagickTrue);
194 extent=(MagickOffsetType) length-1;
195 count=WriteMatrixElements(matrix_info,extent,1,(const unsigned char *) "");
196#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
197 if (matrix_info->synchronize != MagickFalse)
cristybb380e72014-10-31 12:56:36 +0000198 (void) posix_fallocate(matrix_info->file,offset+1,extent-offset);
cristyc1712b22014-03-09 14:54:05 +0000199#endif
200#if defined(SIGBUS)
201 (void) signal(SIGBUS,MatrixSignalHandler);
202#endif
203 return(count != (MagickOffsetType) 1 ? MagickFalse : MagickTrue);
204}
205
cristy106efa12014-03-09 01:06:05 +0000206MagickExport MatrixInfo *AcquireMatrixInfo(const size_t columns,
207 const size_t rows,const size_t stride,ExceptionInfo *exception)
208{
cristyc1712b22014-03-09 14:54:05 +0000209 char
210 *synchronize;
211
cristy106efa12014-03-09 01:06:05 +0000212 MagickBooleanType
213 status;
214
215 MatrixInfo
216 *matrix_info;
217
218 matrix_info=(MatrixInfo *) AcquireMagickMemory(sizeof(*matrix_info));
219 if (matrix_info == (MatrixInfo *) NULL)
220 return((MatrixInfo *) NULL);
221 (void) ResetMagickMemory(matrix_info,0,sizeof(*matrix_info));
222 matrix_info->signature=MagickSignature;
223 matrix_info->columns=columns;
224 matrix_info->rows=rows;
225 matrix_info->stride=stride;
cristycba9e952014-03-30 23:17:57 +0000226 matrix_info->semaphore=AcquireSemaphoreInfo();
cristyc1712b22014-03-09 14:54:05 +0000227 synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
228 if (synchronize != (const char *) NULL)
229 {
230 matrix_info->synchronize=IsStringTrue(synchronize);
231 synchronize=DestroyString(synchronize);
232 }
cristy106efa12014-03-09 01:06:05 +0000233 matrix_info->length=(MagickSizeType) columns*rows*stride;
234 if (matrix_info->columns != (size_t) (matrix_info->length/rows/stride))
235 {
236 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
237 "CacheResourcesExhausted","`%s'","matrix cache");
238 return(DestroyMatrixInfo(matrix_info));
239 }
240 matrix_info->type=MemoryCache;
241 status=AcquireMagickResource(AreaResource,matrix_info->length);
242 if ((status != MagickFalse) &&
243 (matrix_info->length == (MagickSizeType) ((size_t) matrix_info->length)))
244 {
245 status=AcquireMagickResource(MemoryResource,matrix_info->length);
246 if (status != MagickFalse)
247 {
cristyc1712b22014-03-09 14:54:05 +0000248 matrix_info->mapped=MagickFalse;
cristy106efa12014-03-09 01:06:05 +0000249 matrix_info->elements=AcquireMagickMemory((size_t)
250 matrix_info->length);
251 if (matrix_info->elements == NULL)
252 {
253 matrix_info->mapped=MagickTrue;
254 matrix_info->elements=MapBlob(-1,IOMode,0,(size_t)
255 matrix_info->length);
256 }
257 if (matrix_info->elements == (unsigned short *) NULL)
258 RelinquishMagickResource(MemoryResource,matrix_info->length);
259 }
260 }
261 matrix_info->file=(-1);
262 if (matrix_info->elements == (unsigned short *) NULL)
263 {
264 status=AcquireMagickResource(DiskResource,matrix_info->length);
265 if (status == MagickFalse)
266 {
267 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
268 "CacheResourcesExhausted","`%s'","matrix cache");
269 return(DestroyMatrixInfo(matrix_info));
270 }
271 matrix_info->type=DiskCache;
272 (void) AcquireMagickResource(MemoryResource,matrix_info->length);
273 matrix_info->file=AcquireUniqueFileResource(matrix_info->path);
274 if (matrix_info->file == -1)
275 return(DestroyMatrixInfo(matrix_info));
276 status=AcquireMagickResource(MapResource,matrix_info->length);
277 if (status != MagickFalse)
278 {
cristyc1712b22014-03-09 14:54:05 +0000279 status=SetMatrixExtent(matrix_info,matrix_info->length);
cristy106efa12014-03-09 01:06:05 +0000280 if (status != MagickFalse)
281 {
282 matrix_info->elements=(void *) MapBlob(matrix_info->file,IOMode,0,
283 (size_t) matrix_info->length);
284 if (matrix_info->elements != NULL)
285 matrix_info->type=MapCache;
286 else
287 RelinquishMagickResource(MapResource,matrix_info->length);
288 }
289 }
290 }
291 return(matrix_info);
292}
cristy3ed852e2009-09-05 21:47:34 +0000293
294/*
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296% %
297% %
298% %
299% A c q u i r e M a g i c k M a t r i x %
300% %
301% %
302% %
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304%
305% AcquireMagickMatrix() allocates and returns a matrix in the form of an
306% array of pointers to an array of doubles, with all values pre-set to zero.
307%
cristy106efa12014-03-09 01:06:05 +0000308% This used to generate the two dimensional matrix, and vectors required
309% for the GaussJordanElimination() method below, solving some system of
310% simultanious equations.
cristy3ed852e2009-09-05 21:47:34 +0000311%
312% The format of the AcquireMagickMatrix method is:
313%
cristybb503372010-05-27 20:51:26 +0000314% double **AcquireMagickMatrix(const size_t number_rows,
315% const size_t size)
cristy3ed852e2009-09-05 21:47:34 +0000316%
317% A description of each parameter follows:
318%
cristy74908cf2010-05-17 19:43:54 +0000319% o number_rows: the number pointers for the array of pointers
cristy1ad491d2010-05-17 19:45:27 +0000320% (first dimension).
cristy3ed852e2009-09-05 21:47:34 +0000321%
cristy1ad491d2010-05-17 19:45:27 +0000322% o size: the size of the array of doubles each pointer points to
323% (second dimension).
cristy3ed852e2009-09-05 21:47:34 +0000324%
325*/
cristybb503372010-05-27 20:51:26 +0000326MagickExport double **AcquireMagickMatrix(const size_t number_rows,
327 const size_t size)
cristy3ed852e2009-09-05 21:47:34 +0000328{
329 double
cristy74908cf2010-05-17 19:43:54 +0000330 **matrix;
cristy3ed852e2009-09-05 21:47:34 +0000331
cristybb503372010-05-27 20:51:26 +0000332 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000333 i,
334 j;
335
cristy74908cf2010-05-17 19:43:54 +0000336 matrix=(double **) AcquireQuantumMemory(number_rows,sizeof(*matrix));
cristy3ed852e2009-09-05 21:47:34 +0000337 if (matrix == (double **) NULL)
cristy106efa12014-03-09 01:06:05 +0000338 return((double **)NULL);
cristybb503372010-05-27 20:51:26 +0000339 for (i=0; i < (ssize_t) number_rows; i++)
cristy3ed852e2009-09-05 21:47:34 +0000340 {
341 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
342 if (matrix[i] == (double *) NULL)
343 {
344 for (j=0; j < i; j++)
345 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
346 matrix=(double **) RelinquishMagickMemory(matrix);
347 return((double **) NULL);
348 }
cristybb503372010-05-27 20:51:26 +0000349 for (j=0; j < (ssize_t) size; j++)
cristy74908cf2010-05-17 19:43:54 +0000350 matrix[i][j]=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000351 }
352 return(matrix);
353}
354
355/*
356%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357% %
358% %
359% %
cristy106efa12014-03-09 01:06:05 +0000360% D e s t r o y M a t r i x I n f o %
361% %
362% %
363% %
364%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365%
366% DestroyMatrixInfo() dereferences a matrix, deallocating memory associated
367% with the matrix.
368%
369% The format of the DestroyImage method is:
370%
371% MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
372%
373% A description of each parameter follows:
374%
375% o matrix_info: the matrix.
376%
377*/
378MagickExport MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
379{
380 assert(matrix_info != (MatrixInfo *) NULL);
381 assert(matrix_info->signature == MagickSignature);
cristycba9e952014-03-30 23:17:57 +0000382 LockSemaphoreInfo(matrix_info->semaphore);
cristy106efa12014-03-09 01:06:05 +0000383 switch (matrix_info->type)
384 {
385 case MemoryCache:
386 {
387 if (matrix_info->mapped == MagickFalse)
388 matrix_info->elements=RelinquishMagickMemory(matrix_info->elements);
389 else
390 {
391 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
392 matrix_info->elements=(unsigned short *) NULL;
393 }
394 RelinquishMagickResource(MemoryResource,matrix_info->length);
395 break;
396 }
397 case MapCache:
398 {
399 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
400 matrix_info->elements=NULL;
401 RelinquishMagickResource(MapResource,matrix_info->length);
402 }
403 case DiskCache:
404 {
405 if (matrix_info->file != -1)
406 (void) close(matrix_info->file);
407 (void) RelinquishUniqueFileResource(matrix_info->path);
408 RelinquishMagickResource(DiskResource,matrix_info->length);
409 break;
410 }
411 default:
412 break;
413 }
cristycba9e952014-03-30 23:17:57 +0000414 UnlockSemaphoreInfo(matrix_info->semaphore);
415 RelinquishSemaphoreInfo(&matrix_info->semaphore);
cristy106efa12014-03-09 01:06:05 +0000416 return((MatrixInfo *) RelinquishMagickMemory(matrix_info));
417}
418
419/*
420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421% %
422% %
423% %
cristy3ed852e2009-09-05 21:47:34 +0000424% G a u s s J o r d a n E l i m i n a t i o n %
425% %
426% %
427% %
428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429%
430% GaussJordanElimination() returns a matrix in reduced row echelon form,
431% while simultaneously reducing and thus solving the augumented results
432% matrix.
433%
434% See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
435%
436% The format of the GaussJordanElimination method is:
437%
cristy7dbde212014-10-11 06:59:40 +0000438% MagickBooleanType GaussJordanElimination(double **matrix,
439% double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000440%
441% A description of each parameter follows:
442%
443% o matrix: the matrix to be reduced, as an 'array of row pointers'.
444%
445% o vectors: the additional matrix argumenting the matrix for row reduction.
446% Producing an 'array of column vectors'.
447%
cristy106efa12014-03-09 01:06:05 +0000448% o rank: The size of the matrix (both rows and columns).
cristy3ed852e2009-09-05 21:47:34 +0000449% Also represents the number terms that need to be solved.
450%
cristy74908cf2010-05-17 19:43:54 +0000451% o number_vectors: Number of vectors columns, argumenting the above matrix.
cristy3ed852e2009-09-05 21:47:34 +0000452% Usally 1, but can be more for more complex equation solving.
453%
454% Note that the 'matrix' is given as a 'array of row pointers' of rank size.
455% That is values can be assigned as matrix[row][column] where 'row' is
456% typically the equation, and 'column' is the term of the equation.
457% That is the matrix is in the form of a 'row first array'.
458%
459% However 'vectors' is a 'array of column pointers' which can have any number
460% of columns, with each column array the same 'rank' size as 'matrix'.
461%
462% This allows for simpler handling of the results, especially is only one
cristy106efa12014-03-09 01:06:05 +0000463% column 'vector' is all that is required to produce the desired solution.
cristy3ed852e2009-09-05 21:47:34 +0000464%
465% For example, the 'vectors' can consist of a pointer to a simple array of
466% doubles. when only one set of simultanious equations is to be solved from
467% the given set of coefficient weighted terms.
468%
469% double **matrix = AcquireMagickMatrix(8UL,8UL);
470% double coefficents[8];
471% ...
472% GaussJordanElimination(matrix, &coefficents, 8UL, 1UL);
anthony34364f42011-10-21 05:31:53 +0000473%
cristy106efa12014-03-09 01:06:05 +0000474% However by specifing more 'columns' (as an 'array of vector columns',
475% you can use this function to solve a set of 'separable' equations.
cristy3ed852e2009-09-05 21:47:34 +0000476%
477% For example a distortion function where u = U(x,y) v = V(x,y)
478% And the functions U() and V() have separate coefficents, but are being
479% generated from a common x,y->u,v data set.
480%
cristy7dbde212014-10-11 06:59:40 +0000481% Another example is generation of a color gradient from a set of colors at
482% specific coordients, such as a list x,y -> r,g,b,a.
cristy3ed852e2009-09-05 21:47:34 +0000483%
484% You can also use the 'vectors' to generate an inverse of the given 'matrix'
cristy106efa12014-03-09 01:06:05 +0000485% though as a 'column first array' rather than a 'row first array'. For
cristy7dbde212014-10-11 06:59:40 +0000486% details see http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
cristy3ed852e2009-09-05 21:47:34 +0000487%
488*/
cristy106efa12014-03-09 01:06:05 +0000489MagickExport MagickBooleanType GaussJordanElimination(double **matrix,
cristybb503372010-05-27 20:51:26 +0000490 double **vectors,const size_t rank,const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000491{
492#define GaussJordanSwap(x,y) \
493{ \
494 if ((x) != (y)) \
495 { \
496 (x)+=(y); \
497 (y)=(x)-(y); \
498 (x)=(x)-(y); \
499 } \
500}
501
502 double
503 max,
504 scale;
505
cristy9d314ff2011-03-09 01:30:28 +0000506 register ssize_t
507 i,
508 j,
509 k;
510
cristybb503372010-05-27 20:51:26 +0000511 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000512 column,
513 *columns,
514 *pivots,
515 row,
516 *rows;
517
cristybb503372010-05-27 20:51:26 +0000518 columns=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*columns));
519 rows=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*rows));
520 pivots=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*pivots));
521 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
522 (pivots == (ssize_t *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000523 {
cristybb503372010-05-27 20:51:26 +0000524 if (pivots != (ssize_t *) NULL)
525 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
526 if (columns != (ssize_t *) NULL)
527 columns=(ssize_t *) RelinquishMagickMemory(columns);
528 if (rows != (ssize_t *) NULL)
529 rows=(ssize_t *) RelinquishMagickMemory(rows);
cristy3ed852e2009-09-05 21:47:34 +0000530 return(MagickFalse);
531 }
532 (void) ResetMagickMemory(columns,0,rank*sizeof(*columns));
533 (void) ResetMagickMemory(rows,0,rank*sizeof(*rows));
534 (void) ResetMagickMemory(pivots,0,rank*sizeof(*pivots));
535 column=0;
536 row=0;
cristybb503372010-05-27 20:51:26 +0000537 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000538 {
539 max=0.0;
cristybb503372010-05-27 20:51:26 +0000540 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000541 if (pivots[j] != 1)
542 {
cristybb503372010-05-27 20:51:26 +0000543 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000544 if (pivots[k] != 0)
545 {
546 if (pivots[k] > 1)
547 return(MagickFalse);
548 }
549 else
550 if (fabs(matrix[j][k]) >= max)
551 {
552 max=fabs(matrix[j][k]);
553 row=j;
554 column=k;
555 }
556 }
557 pivots[column]++;
558 if (row != column)
559 {
cristybb503372010-05-27 20:51:26 +0000560 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000561 GaussJordanSwap(matrix[row][k],matrix[column][k]);
cristybb503372010-05-27 20:51:26 +0000562 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000563 GaussJordanSwap(vectors[k][row],vectors[k][column]);
564 }
565 rows[i]=row;
566 columns[i]=column;
567 if (matrix[column][column] == 0.0)
cristy106efa12014-03-09 01:06:05 +0000568 return(MagickFalse); /* sigularity */
cristy3e3ec3a2012-11-03 23:11:06 +0000569 scale=PerceptibleReciprocal(matrix[column][column]);
cristy3ed852e2009-09-05 21:47:34 +0000570 matrix[column][column]=1.0;
cristybb503372010-05-27 20:51:26 +0000571 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000572 matrix[column][j]*=scale;
cristybb503372010-05-27 20:51:26 +0000573 for (j=0; j < (ssize_t) number_vectors; j++)
cristy3ed852e2009-09-05 21:47:34 +0000574 vectors[j][column]*=scale;
cristybb503372010-05-27 20:51:26 +0000575 for (j=0; j < (ssize_t) rank; j++)
cristy3ed852e2009-09-05 21:47:34 +0000576 if (j != column)
577 {
578 scale=matrix[j][column];
579 matrix[j][column]=0.0;
cristybb503372010-05-27 20:51:26 +0000580 for (k=0; k < (ssize_t) rank; k++)
cristy3ed852e2009-09-05 21:47:34 +0000581 matrix[j][k]-=scale*matrix[column][k];
cristybb503372010-05-27 20:51:26 +0000582 for (k=0; k < (ssize_t) number_vectors; k++)
cristy3ed852e2009-09-05 21:47:34 +0000583 vectors[k][j]-=scale*vectors[k][column];
584 }
585 }
cristybb503372010-05-27 20:51:26 +0000586 for (j=(ssize_t) rank-1; j >= 0; j--)
cristy3ed852e2009-09-05 21:47:34 +0000587 if (columns[j] != rows[j])
cristybb503372010-05-27 20:51:26 +0000588 for (i=0; i < (ssize_t) rank; i++)
cristy3ed852e2009-09-05 21:47:34 +0000589 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
cristybb503372010-05-27 20:51:26 +0000590 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
591 rows=(ssize_t *) RelinquishMagickMemory(rows);
592 columns=(ssize_t *) RelinquishMagickMemory(columns);
cristy3ed852e2009-09-05 21:47:34 +0000593 return(MagickTrue);
594}
595
596/*
597%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598% %
599% %
600% %
cristy106efa12014-03-09 01:06:05 +0000601% G e t M a t r i x C o l u m n s %
602% %
603% %
604% %
605%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606%
607% GetMatrixColumns() returns the number of columns in the matrix.
608%
609% The format of the GetMatrixColumns method is:
610%
611% size_t GetMatrixColumns(const MatrixInfo *matrix_info)
612%
613% A description of each parameter follows:
614%
615% o matrix_info: the matrix.
616%
617*/
618MagickExport size_t GetMatrixColumns(const MatrixInfo *matrix_info)
619{
620 assert(matrix_info != (MatrixInfo *) NULL);
621 assert(matrix_info->signature == MagickSignature);
622 return(matrix_info->columns);
623}
624
625/*
626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627% %
628% %
629% %
630% G e t M a t r i x E l e m e n t %
631% %
632% %
633% %
634%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635%
636% GetMatrixElement() returns the specifed element in the matrix.
637%
638% The format of the GetMatrixElement method is:
639%
640% MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
641% const ssize_t x,const ssize_t y,void *value)
642%
643% A description of each parameter follows:
644%
645% o matrix_info: the matrix columns.
646%
647% o x: the matrix x-offset.
648%
649% o y: the matrix y-offset.
650%
651% o value: return the matrix element in this buffer.
652%
653*/
654
cristy6cccee42014-03-23 00:25:22 +0000655static inline ssize_t EdgeX(const ssize_t x,const size_t columns)
656{
657 if (x < 0L)
658 return(0L);
659 if (x >= (ssize_t) columns)
660 return((ssize_t) (columns-1));
661 return(x);
662}
663
664static inline ssize_t EdgeY(const ssize_t y,const size_t rows)
665{
666 if (y < 0L)
667 return(0L);
668 if (y >= (ssize_t) rows)
669 return((ssize_t) (rows-1));
670 return(y);
671}
672
cristyc1712b22014-03-09 14:54:05 +0000673static inline MagickOffsetType ReadMatrixElements(
674 const MatrixInfo *restrict matrix_info,const MagickOffsetType offset,
675 const MagickSizeType length,unsigned char *restrict buffer)
cristy106efa12014-03-09 01:06:05 +0000676{
cristyc1712b22014-03-09 14:54:05 +0000677 register MagickOffsetType
cristy106efa12014-03-09 01:06:05 +0000678 i;
679
680 ssize_t
681 count;
682
cristyc1712b22014-03-09 14:54:05 +0000683#if !defined(MAGICKCORE_HAVE_PREAD)
cristy10c210e2014-03-30 23:27:24 +0000684 LockSemaphoreInfo(matrix_info->semaphore);
cristyc1712b22014-03-09 14:54:05 +0000685 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
cristy10c210e2014-03-30 23:27:24 +0000686 {
687 UnlockSemaphoreInfo(matrix_info->semaphore);
688 return((MagickOffsetType) -1);
689 }
cristy106efa12014-03-09 01:06:05 +0000690#endif
cristyc1712b22014-03-09 14:54:05 +0000691 count=0;
692 for (i=0; i < (MagickOffsetType) length; i+=count)
cristy106efa12014-03-09 01:06:05 +0000693 {
cristyc1712b22014-03-09 14:54:05 +0000694#if !defined(MAGICKCORE_HAVE_PREAD)
695 count=read(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
696 (MagickSizeType) SSIZE_MAX));
cristy106efa12014-03-09 01:06:05 +0000697#else
cristyc1712b22014-03-09 14:54:05 +0000698 count=pread(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
699 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
cristy106efa12014-03-09 01:06:05 +0000700#endif
cristyc1712b22014-03-09 14:54:05 +0000701 if (count <= 0)
702 {
703 count=0;
704 if (errno != EINTR)
705 break;
cristy106efa12014-03-09 01:06:05 +0000706 }
707 }
cristy10c210e2014-03-30 23:27:24 +0000708#if !defined(MAGICKCORE_HAVE_PREAD)
709 UnlockSemaphoreInfo(matrix_info->semaphore);
710#endif
cristy106efa12014-03-09 01:06:05 +0000711 return(i);
712}
713
714MagickExport MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
715 const ssize_t x,const ssize_t y,void *value)
716{
717 MagickOffsetType
cristyc1712b22014-03-09 14:54:05 +0000718 count,
cristy106efa12014-03-09 01:06:05 +0000719 i;
720
cristy106efa12014-03-09 01:06:05 +0000721 assert(matrix_info != (const MatrixInfo *) NULL);
722 assert(matrix_info->signature == MagickSignature);
cristy6cccee42014-03-23 00:25:22 +0000723 i=(MagickOffsetType) EdgeY(y,matrix_info->rows)*matrix_info->columns+
724 EdgeX(x,matrix_info->columns);
cristy106efa12014-03-09 01:06:05 +0000725 if (matrix_info->type != DiskCache)
726 {
cristy7a729512014-03-09 16:43:07 +0000727 (void) memcpy(value,(unsigned char *) matrix_info->elements+i*
728 matrix_info->stride,matrix_info->stride);
cristy106efa12014-03-09 01:06:05 +0000729 return(MagickTrue);
730 }
cristyc1712b22014-03-09 14:54:05 +0000731 count=ReadMatrixElements(matrix_info,i*matrix_info->stride,
cristy759ba912014-06-26 11:59:43 +0000732 matrix_info->stride,(unsigned char *) value);
cristyc1712b22014-03-09 14:54:05 +0000733 if (count != (MagickOffsetType) matrix_info->stride)
cristy106efa12014-03-09 01:06:05 +0000734 return(MagickFalse);
735 return(MagickTrue);
736}
737
738/*
739%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740% %
741% %
742% %
743% G e t M a t r i x R o w s %
744% %
745% %
746% %
747%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
748%
749% GetMatrixRows() returns the number of rows in the matrix.
750%
751% The format of the GetMatrixRows method is:
752%
753% size_t GetMatrixRows(const MatrixInfo *matrix_info)
754%
755% A description of each parameter follows:
756%
757% o matrix_info: the matrix.
758%
759*/
760MagickExport size_t GetMatrixRows(const MatrixInfo *matrix_info)
761{
762 assert(matrix_info != (const MatrixInfo *) NULL);
763 assert(matrix_info->signature == MagickSignature);
764 return(matrix_info->rows);
765}
766
767/*
768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769% %
770% %
771% %
cristy3ed852e2009-09-05 21:47:34 +0000772% L e a s t S q u a r e s A d d T e r m s %
773% %
774% %
775% %
776%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777%
778% LeastSquaresAddTerms() adds one set of terms and associate results to the
779% given matrix and vectors for solving using least-squares function fitting.
780%
781% The format of the AcquireMagickMatrix method is:
782%
783% void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000784% const double *terms,const double *results,const size_t rank,
785% const size_t number_vectors);
cristy3ed852e2009-09-05 21:47:34 +0000786%
787% A description of each parameter follows:
788%
789% o matrix: the square matrix to add given terms/results to.
790%
791% o vectors: the result vectors to add terms/results to.
792%
793% o terms: the pre-calculated terms (without the unknown coefficent
cristy106efa12014-03-09 01:06:05 +0000794% weights) that forms the equation being added.
cristy3ed852e2009-09-05 21:47:34 +0000795%
796% o results: the result(s) that should be generated from the given terms
cristy106efa12014-03-09 01:06:05 +0000797% weighted by the yet-to-be-solved coefficents.
cristy3ed852e2009-09-05 21:47:34 +0000798%
cristy106efa12014-03-09 01:06:05 +0000799% o rank: the rank or size of the dimensions of the square matrix.
800% Also the length of vectors, and number of terms being added.
cristy3ed852e2009-09-05 21:47:34 +0000801%
cristy1ad491d2010-05-17 19:45:27 +0000802% o number_vectors: Number of result vectors, and number or results being
803% added. Also represents the number of separable systems of equations
804% that is being solved.
cristy3ed852e2009-09-05 21:47:34 +0000805%
806% Example of use...
807%
glennrp2489f532011-06-25 03:02:43 +0000808% 2 dimensional Affine Equations (which are separable)
cristy3ed852e2009-09-05 21:47:34 +0000809% c0*x + c2*y + c4*1 => u
810% c1*x + c3*y + c5*1 => v
811%
812% double **matrix = AcquireMagickMatrix(3UL,3UL);
813% double **vectors = AcquireMagickMatrix(2UL,3UL);
814% double terms[3], results[2];
815% ...
816% for each given x,y -> u,v
817% terms[0] = x;
818% terms[1] = y;
819% terms[2] = 1;
820% results[0] = u;
821% results[1] = v;
822% LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
823% ...
824% if ( GaussJordanElimination(matrix,vectors,3UL,2UL) ) {
825% c0 = vectors[0][0];
cristy106efa12014-03-09 01:06:05 +0000826% c2 = vectors[0][1];
cristy3ed852e2009-09-05 21:47:34 +0000827% c4 = vectors[0][2];
828% c1 = vectors[1][0];
cristy106efa12014-03-09 01:06:05 +0000829% c3 = vectors[1][1];
cristy3ed852e2009-09-05 21:47:34 +0000830% c5 = vectors[1][2];
831% }
832% else
833% printf("Matrix unsolvable\n);
834% RelinquishMagickMatrix(matrix,3UL);
835% RelinquishMagickMatrix(vectors,2UL);
836%
837*/
cristy106efa12014-03-09 01:06:05 +0000838MagickExport void LeastSquaresAddTerms(double **matrix,double **vectors,
cristybb503372010-05-27 20:51:26 +0000839 const double *terms,const double *results,const size_t rank,
840 const size_t number_vectors)
cristy3ed852e2009-09-05 21:47:34 +0000841{
cristybb503372010-05-27 20:51:26 +0000842 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000843 i,
844 j;
845
cristybb503372010-05-27 20:51:26 +0000846 for (j=0; j < (ssize_t) rank; j++)
cristy74908cf2010-05-17 19:43:54 +0000847 {
cristybb503372010-05-27 20:51:26 +0000848 for (i=0; i < (ssize_t) rank; i++)
cristy74908cf2010-05-17 19:43:54 +0000849 matrix[i][j]+=terms[i]*terms[j];
cristybb503372010-05-27 20:51:26 +0000850 for (i=0; i < (ssize_t) number_vectors; i++)
cristy74908cf2010-05-17 19:43:54 +0000851 vectors[i][j]+=results[i]*terms[j];
cristy3ed852e2009-09-05 21:47:34 +0000852 }
cristy106efa12014-03-09 01:06:05 +0000853 return;
cristy3ed852e2009-09-05 21:47:34 +0000854}
855
856/*
857%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858% %
859% %
860% %
cristyf3b58c52014-04-22 01:27:24 +0000861% M a t r i x T o I m a g e %
862% %
863% %
864% %
865%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866%
cristy0f09a8c2014-04-23 00:20:40 +0000867% MatrixToImage() returns a matrix as an image. The matrix elements must be
868% of type double otherwise nonsense is returned.
cristyf3b58c52014-04-22 01:27:24 +0000869%
870% The format of the MatrixToImage method is:
871%
872% Image *MatrixToImage(const MatrixInfo *matrix_info,
873% ExceptionInfo *exception)
874%
875% A description of each parameter follows:
876%
877% o matrix_info: the matrix.
878%
879% o exception: return any errors or warnings in this structure.
880%
881*/
882MagickExport Image *MatrixToImage(const MatrixInfo *matrix_info,
883 ExceptionInfo *exception)
884{
cristy0f09a8c2014-04-23 00:20:40 +0000885 CacheView
886 *image_view;
887
888 double
889 max_value,
890 min_value,
891 scale_factor,
892 value;
893
894 Image
895 *image;
896
897 MagickBooleanType
898 status;
899
900 ssize_t
901 y;
902
903 assert(matrix_info != (const MatrixInfo *) NULL);
904 assert(matrix_info->signature == MagickSignature);
905 assert(exception != (ExceptionInfo *) NULL);
906 assert(exception->signature == MagickSignature);
907 if (matrix_info->stride < sizeof(double))
908 return((Image *) NULL);
909 /*
910 Determine range of matrix.
911 */
912 (void) GetMatrixElement(matrix_info,0,0,&value);
913 min_value=value;
914 max_value=value;
915 for (y=0; y < (ssize_t) matrix_info->rows; y++)
916 {
917 register ssize_t
918 x;
919
920 for (x=0; x < (ssize_t) matrix_info->columns; x++)
921 {
922 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
923 continue;
924 if (value < min_value)
925 min_value=value;
926 else
927 if (value > max_value)
928 max_value=value;
929 }
930 }
931 if ((min_value == 0.0) && (max_value == 0.0))
932 scale_factor=0;
933 else
934 if (min_value == max_value)
935 {
936 scale_factor=(double) QuantumRange/min_value;
937 min_value=0;
938 }
939 else
940 scale_factor=(double) QuantumRange/(max_value-min_value);
941 /*
942 Convert matrix to image.
943 */
944 image=AcquireImage((ImageInfo *) NULL,exception);
945 image->columns=matrix_info->columns;
946 image->rows=matrix_info->rows;
947 image->colorspace=GRAYColorspace;
948 status=MagickTrue;
949 image_view=AcquireAuthenticCacheView(image,exception);
950#if defined(MAGICKCORE_OPENMP_SUPPORT)
951 #pragma omp parallel for schedule(static,4) shared(status) \
952 magick_threads(image,image,image->rows,1)
953#endif
954 for (y=0; y < (ssize_t) image->rows; y++)
955 {
956 double
957 value;
958
959 register Quantum
960 *q;
961
962 register ssize_t
963 x;
964
965 if (status == MagickFalse)
966 continue;
967 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
968 if (q == (Quantum *) NULL)
969 {
970 status=MagickFalse;
971 continue;
972 }
973 for (x=0; x < (ssize_t) image->columns; x++)
974 {
975 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
976 continue;
977 value=scale_factor*(value-min_value);
978 *q=ClampToQuantum(value);
979 q+=GetPixelChannels(image);
980 }
981 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
982 status=MagickFalse;
983 }
984 image_view=DestroyCacheView(image_view);
985 if (status == MagickFalse)
986 image=DestroyImage(image);
987 return(image);
cristyf3b58c52014-04-22 01:27:24 +0000988}
989
990/*
991%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992% %
993% %
994% %
cristyc1712b22014-03-09 14:54:05 +0000995% N u l l M a t r i x %
996% %
997% %
998% %
999%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000%
1001% NullMatrix() sets all elements of the matrix to zero.
1002%
1003% The format of the ResetMagickMemory method is:
1004%
1005% MagickBooleanType *NullMatrix(MatrixInfo *matrix_info)
1006%
1007% A description of each parameter follows:
1008%
1009% o matrix_info: the matrix.
1010%
1011*/
1012MagickExport MagickBooleanType NullMatrix(MatrixInfo *matrix_info)
1013{
1014 register ssize_t
1015 x;
1016
1017 ssize_t
1018 count,
1019 y;
1020
1021 unsigned char
1022 value;
1023
1024 assert(matrix_info != (const MatrixInfo *) NULL);
1025 assert(matrix_info->signature == MagickSignature);
1026 if (matrix_info->type != DiskCache)
1027 {
1028 (void) ResetMagickMemory(matrix_info->elements,0,(size_t)
1029 matrix_info->length);
1030 return(MagickTrue);
1031 }
1032 value=0;
1033 (void) lseek(matrix_info->file,0,SEEK_SET);
1034 for (y=0; y < (ssize_t) matrix_info->rows; y++)
1035 {
1036 for (x=0; x < (ssize_t) matrix_info->length; x++)
1037 {
1038 count=write(matrix_info->file,&value,sizeof(value));
1039 if (count != (ssize_t) sizeof(value))
1040 break;
1041 }
1042 if (x < (ssize_t) matrix_info->length)
1043 break;
1044 }
1045 return(y < (ssize_t) matrix_info->rows ? MagickFalse : MagickTrue);
1046}
1047
1048/*
1049%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050% %
1051% %
1052% %
cristy3ed852e2009-09-05 21:47:34 +00001053% R e l i n q u i s h M a g i c k M a t r i x %
1054% %
1055% %
1056% %
1057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1058%
1059% RelinquishMagickMatrix() frees the previously acquired matrix (array of
1060% pointers to arrays of doubles).
1061%
1062% The format of the RelinquishMagickMatrix method is:
1063%
1064% double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +00001065% const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +00001066%
1067% A description of each parameter follows:
1068%
1069% o matrix: the matrix to relinquish
1070%
cristy1ad491d2010-05-17 19:45:27 +00001071% o number_rows: the first dimension of the acquired matrix (number of
1072% pointers)
cristy3ed852e2009-09-05 21:47:34 +00001073%
1074*/
1075MagickExport double **RelinquishMagickMatrix(double **matrix,
cristybb503372010-05-27 20:51:26 +00001076 const size_t number_rows)
cristy3ed852e2009-09-05 21:47:34 +00001077{
cristybb503372010-05-27 20:51:26 +00001078 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001079 i;
1080
1081 if (matrix == (double **) NULL )
1082 return(matrix);
cristybb503372010-05-27 20:51:26 +00001083 for (i=0; i < (ssize_t) number_rows; i++)
cristy3ed852e2009-09-05 21:47:34 +00001084 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
1085 matrix=(double **) RelinquishMagickMemory(matrix);
cristy3ed852e2009-09-05 21:47:34 +00001086 return(matrix);
1087}
cristy106efa12014-03-09 01:06:05 +00001088
1089/*
1090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091% %
1092% %
1093% %
cristy106efa12014-03-09 01:06:05 +00001094% S e t M a t r i x E l e m e n t %
1095% %
1096% %
1097% %
1098%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1099%
1100% SetMatrixElement() sets the specifed element in the matrix.
1101%
1102% The format of the SetMatrixElement method is:
1103%
1104% MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1105% const ssize_t x,const ssize_t y,void *value)
1106%
1107% A description of each parameter follows:
1108%
1109% o matrix_info: the matrix columns.
1110%
1111% o x: the matrix x-offset.
1112%
1113% o y: the matrix y-offset.
1114%
1115% o value: set the matrix element to this value.
1116%
1117*/
1118
cristy106efa12014-03-09 01:06:05 +00001119MagickExport MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1120 const ssize_t x,const ssize_t y,const void *value)
1121{
1122 MagickOffsetType
cristyc1712b22014-03-09 14:54:05 +00001123 count,
cristy106efa12014-03-09 01:06:05 +00001124 i;
1125
cristy106efa12014-03-09 01:06:05 +00001126 assert(matrix_info != (const MatrixInfo *) NULL);
1127 assert(matrix_info->signature == MagickSignature);
cristy6cccee42014-03-23 00:25:22 +00001128 i=(MagickOffsetType) y*matrix_info->columns+x;
cristy106efa12014-03-09 01:06:05 +00001129 if ((i < 0) ||
1130 ((MagickSizeType) (i*matrix_info->stride) >= matrix_info->length))
1131 return(MagickFalse);
1132 if (matrix_info->type != DiskCache)
1133 {
cristy7a729512014-03-09 16:43:07 +00001134 (void) memcpy((unsigned char *) matrix_info->elements+i*
1135 matrix_info->stride,value,matrix_info->stride);
cristy106efa12014-03-09 01:06:05 +00001136 return(MagickTrue);
1137 }
cristyc1712b22014-03-09 14:54:05 +00001138 count=WriteMatrixElements(matrix_info,i*matrix_info->stride,
cristy759ba912014-06-26 11:59:43 +00001139 matrix_info->stride,(unsigned char *) value);
cristyc1712b22014-03-09 14:54:05 +00001140 if (count != (MagickOffsetType) matrix_info->stride)
cristy106efa12014-03-09 01:06:05 +00001141 return(MagickFalse);
1142 return(MagickTrue);
1143}