MagickCore 7.1.1-43
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
matrix.c
1/*
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 %
16% Cristy %
17% August 2007 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% 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% https://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*/
42#include "MagickCore/studio.h"
43#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"
48#include "MagickCore/image-private.h"
49#include "MagickCore/matrix.h"
50#include "MagickCore/matrix-private.h"
51#include "MagickCore/memory_.h"
52#include "MagickCore/memory-private.h"
53#include "MagickCore/nt-base-private.h"
54#include "MagickCore/pixel-accessor.h"
55#include "MagickCore/resource_.h"
56#include "MagickCore/semaphore.h"
57#include "MagickCore/thread-private.h"
58#include "MagickCore/utility.h"
59#include "MagickCore/utility-private.h"
60
61/*
62 Typedef declaration.
63*/
65{
66 CacheType
67 type;
68
69 size_t
70 columns,
71 rows,
72 stride;
73
74 MagickSizeType
75 length;
76
77 MagickBooleanType
78 mapped,
79 synchronize;
80
81 char
82 path[MagickPathExtent];
83
84 int
85 file;
86
87 void
88 *elements;
89
91 *semaphore;
92
93 size_t
94 signature;
95};
96
97/*
98%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99% %
100% %
101% %
102% A c q u i r e M a t r i x I n f o %
103% %
104% %
105% %
106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107%
108% AcquireMatrixInfo() allocates the ImageInfo structure.
109%
110% The format of the AcquireMatrixInfo method is:
111%
112% MatrixInfo *AcquireMatrixInfo(const size_t columns,const size_t rows,
113% const size_t stride,ExceptionInfo *exception)
114%
115% A description of each parameter follows:
116%
117% o columns: the matrix columns.
118%
119% o rows: the matrix rows.
120%
121% o stride: the matrix stride.
122%
123% o exception: return any errors or warnings in this structure.
124%
125*/
126
127#if defined(SIGBUS)
128static void MatrixSignalHandler(int magick_unused(status))
129{
130 magick_unreferenced(status);
131 ThrowFatalException(CacheFatalError,"UnableToExtendMatrixCache");
132}
133#endif
134
135static inline MagickOffsetType WriteMatrixElements(
136 const MatrixInfo *magick_restrict matrix_info,const MagickOffsetType offset,
137 const MagickSizeType length,const unsigned char *magick_restrict buffer)
138{
139 MagickOffsetType
140 i;
141
142 ssize_t
143 count;
144
145#if !defined(MAGICKCORE_HAVE_PWRITE)
146 LockSemaphoreInfo(matrix_info->semaphore);
147 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
148 {
149 UnlockSemaphoreInfo(matrix_info->semaphore);
150 return((MagickOffsetType) -1);
151 }
152#endif
153 count=0;
154 for (i=0; i < (MagickOffsetType) length; i+=count)
155 {
156#if !defined(MAGICKCORE_HAVE_PWRITE)
157 count=write(matrix_info->file,buffer+i,(size_t) MagickMin(length-
158 (MagickSizeType) i,(MagickSizeType) MagickMaxBufferExtent));
159#else
160 count=pwrite(matrix_info->file,buffer+i,(size_t) MagickMin(length-
161 (MagickSizeType) i,(MagickSizeType) MagickMaxBufferExtent),offset+i);
162#endif
163 if (count <= 0)
164 {
165 count=0;
166 if (errno != EINTR)
167 break;
168 }
169 }
170#if !defined(MAGICKCORE_HAVE_PWRITE)
171 UnlockSemaphoreInfo(matrix_info->semaphore);
172#endif
173 return(i);
174}
175
176static MagickBooleanType SetMatrixExtent(
177 MatrixInfo *magick_restrict matrix_info,MagickSizeType length)
178{
179 MagickOffsetType
180 count,
181 extent,
182 offset;
183
184 if (length != (MagickSizeType) ((MagickOffsetType) length))
185 return(MagickFalse);
186 offset=(MagickOffsetType) lseek(matrix_info->file,0,SEEK_END);
187 if (offset < 0)
188 return(MagickFalse);
189 if ((MagickSizeType) offset >= length)
190 return(MagickTrue);
191 extent=(MagickOffsetType) length-1;
192 count=WriteMatrixElements(matrix_info,extent,1,(const unsigned char *) "");
193#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
194 if (matrix_info->synchronize != MagickFalse)
195 (void) posix_fallocate(matrix_info->file,offset+1,extent-offset);
196#endif
197#if defined(SIGBUS)
198 (void) signal(SIGBUS,MatrixSignalHandler);
199#endif
200 return(count != (MagickOffsetType) 1 ? MagickFalse : MagickTrue);
201}
202
203MagickExport MatrixInfo *AcquireMatrixInfo(const size_t columns,
204 const size_t rows,const size_t stride,ExceptionInfo *exception)
205{
206 char
207 *synchronize;
208
209 MagickBooleanType
210 status;
211
213 *matrix_info;
214
215 matrix_info=(MatrixInfo *) AcquireMagickMemory(sizeof(*matrix_info));
216 if (matrix_info == (MatrixInfo *) NULL)
217 return((MatrixInfo *) NULL);
218 (void) memset(matrix_info,0,sizeof(*matrix_info));
219 matrix_info->signature=MagickCoreSignature;
220 matrix_info->columns=columns;
221 matrix_info->rows=rows;
222 matrix_info->stride=stride;
223 matrix_info->semaphore=AcquireSemaphoreInfo();
224 synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
225 if (synchronize != (const char *) NULL)
226 {
227 matrix_info->synchronize=IsStringTrue(synchronize);
228 synchronize=DestroyString(synchronize);
229 }
230 matrix_info->length=(MagickSizeType) columns*rows*stride;
231 if (matrix_info->columns != (size_t) (matrix_info->length/rows/stride))
232 {
233 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
234 "CacheResourcesExhausted","`%s'","matrix cache");
235 return(DestroyMatrixInfo(matrix_info));
236 }
237 matrix_info->type=MemoryCache;
238 status=AcquireMagickResource(AreaResource,matrix_info->length);
239 if ((status != MagickFalse) &&
240 (matrix_info->length == (MagickSizeType) ((size_t) matrix_info->length)) &&
241 ((size_t) matrix_info->length <= GetMaxMemoryRequest()))
242 {
243 status=AcquireMagickResource(MemoryResource,matrix_info->length);
244 if (status != MagickFalse)
245 {
246 matrix_info->mapped=MagickFalse;
247 matrix_info->elements=MagickAssumeAligned(AcquireAlignedMemory(1,
248 (size_t) matrix_info->length));
249 if (matrix_info->elements == NULL)
250 {
251 matrix_info->mapped=MagickTrue;
252 matrix_info->elements=MapBlob(-1,IOMode,0,(size_t)
253 matrix_info->length);
254 }
255 if (matrix_info->elements == (unsigned short *) NULL)
256 RelinquishMagickResource(MemoryResource,matrix_info->length);
257 }
258 }
259 matrix_info->file=(-1);
260 if (matrix_info->elements == (unsigned short *) NULL)
261 {
262 status=AcquireMagickResource(DiskResource,matrix_info->length);
263 if (status == MagickFalse)
264 {
265 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
266 "CacheResourcesExhausted","`%s'","matrix cache");
267 return(DestroyMatrixInfo(matrix_info));
268 }
269 matrix_info->type=DiskCache;
270 matrix_info->file=AcquireUniqueFileResource(matrix_info->path);
271 if (matrix_info->file == -1)
272 return(DestroyMatrixInfo(matrix_info));
273 status=AcquireMagickResource(MapResource,matrix_info->length);
274 if (status != MagickFalse)
275 {
276 status=SetMatrixExtent(matrix_info,matrix_info->length);
277 if (status != MagickFalse)
278 matrix_info->elements=(void *) MapBlob(matrix_info->file,IOMode,0,
279 (size_t) matrix_info->length);
280 if (matrix_info->elements != NULL)
281 matrix_info->type=MapCache;
282 else
283 RelinquishMagickResource(MapResource,matrix_info->length);
284 }
285 }
286 return(matrix_info);
287}
288
289/*
290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291% %
292% %
293% %
294% A c q u i r e M a g i c k M a t r i x %
295% %
296% %
297% %
298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299%
300% AcquireMagickMatrix() allocates and returns a matrix in the form of an
301% array of pointers to an array of doubles, with all values pre-set to zero.
302%
303% This used to generate the two dimensional matrix, and vectors required
304% for the GaussJordanElimination() method below, solving some system of
305% simultaneous equations.
306%
307% The format of the AcquireMagickMatrix method is:
308%
309% double **AcquireMagickMatrix(const size_t number_rows,
310% const size_t size)
311%
312% A description of each parameter follows:
313%
314% o number_rows: the number pointers for the array of pointers
315% (first dimension).
316%
317% o size: the size of the array of doubles each pointer points to
318% (second dimension).
319%
320*/
321MagickExport double **AcquireMagickMatrix(const size_t number_rows,
322 const size_t size)
323{
324 double
325 **matrix;
326
327 ssize_t
328 i,
329 j;
330
331 matrix=(double **) AcquireQuantumMemory(number_rows,sizeof(*matrix));
332 if (matrix == (double **) NULL)
333 return((double **) NULL);
334 for (i=0; i < (ssize_t) number_rows; i++)
335 {
336 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
337 if (matrix[i] == (double *) NULL)
338 {
339 for (j=0; j < i; j++)
340 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
341 matrix=(double **) RelinquishMagickMemory(matrix);
342 return((double **) NULL);
343 }
344 for (j=0; j < (ssize_t) size; j++)
345 matrix[i][j]=0.0;
346 }
347 return(matrix);
348}
349
350/*
351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352% %
353% %
354% %
355% D e s t r o y M a t r i x I n f o %
356% %
357% %
358% %
359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360%
361% DestroyMatrixInfo() dereferences a matrix, deallocating memory associated
362% with the matrix.
363%
364% The format of the DestroyImage method is:
365%
366% MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
367%
368% A description of each parameter follows:
369%
370% o matrix_info: the matrix.
371%
372*/
373MagickExport MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
374{
375 assert(matrix_info != (MatrixInfo *) NULL);
376 assert(matrix_info->signature == MagickCoreSignature);
377 LockSemaphoreInfo(matrix_info->semaphore);
378 switch (matrix_info->type)
379 {
380 case MemoryCache:
381 {
382 if (matrix_info->mapped == MagickFalse)
383 matrix_info->elements=RelinquishMagickMemory(matrix_info->elements);
384 else
385 {
386 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
387 matrix_info->elements=(unsigned short *) NULL;
388 }
389 RelinquishMagickResource(MemoryResource,matrix_info->length);
390 break;
391 }
392 case MapCache:
393 {
394 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
395 matrix_info->elements=NULL;
396 RelinquishMagickResource(MapResource,matrix_info->length);
397 magick_fallthrough;
398 }
399 case DiskCache:
400 {
401 if (matrix_info->file != -1)
402 (void) close_utf8(matrix_info->file);
403 (void) RelinquishUniqueFileResource(matrix_info->path);
404 RelinquishMagickResource(DiskResource,matrix_info->length);
405 break;
406 }
407 default:
408 break;
409 }
410 UnlockSemaphoreInfo(matrix_info->semaphore);
411 RelinquishSemaphoreInfo(&matrix_info->semaphore);
412 return((MatrixInfo *) RelinquishMagickMemory(matrix_info));
413}
414
415/*
416%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417% %
418% %
419% %
420+ G a u s s J o r d a n E l i m i n a t i o n %
421% %
422% %
423% %
424%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425%
426% GaussJordanElimination() returns a matrix in reduced row echelon form,
427% while simultaneously reducing and thus solving the augmented results
428% matrix.
429%
430% See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
431%
432% The format of the GaussJordanElimination method is:
433%
434% MagickBooleanType GaussJordanElimination(double **matrix,
435% double **vectors,const size_t rank,const size_t number_vectors)
436%
437% A description of each parameter follows:
438%
439% o matrix: the matrix to be reduced, as an 'array of row pointers'.
440%
441% o vectors: the additional matrix argumenting the matrix for row reduction.
442% Producing an 'array of column vectors'.
443%
444% o rank: The size of the matrix (both rows and columns).
445% Also represents the number terms that need to be solved.
446%
447% o number_vectors: Number of vectors columns, argumenting the above matrix.
448% Usually 1, but can be more for more complex equation solving.
449%
450% Note that the 'matrix' is given as a 'array of row pointers' of rank size.
451% That is values can be assigned as matrix[row][column] where 'row' is
452% typically the equation, and 'column' is the term of the equation.
453% That is the matrix is in the form of a 'row first array'.
454%
455% However 'vectors' is a 'array of column pointers' which can have any number
456% of columns, with each column array the same 'rank' size as 'matrix'.
457%
458% This allows for simpler handling of the results, especially is only one
459% column 'vector' is all that is required to produce the desired solution.
460%
461% For example, the 'vectors' can consist of a pointer to a simple array of
462% doubles. when only one set of simultaneous equations is to be solved from
463% the given set of coefficient weighted terms.
464%
465% double **matrix = AcquireMagickMatrix(8UL,8UL);
466% double coefficients[8];
467% ...
468% GaussJordanElimination(matrix, &coefficients, 8UL, 1UL);
469%
470% However by specifying more 'columns' (as an 'array of vector columns',
471% you can use this function to solve a set of 'separable' equations.
472%
473% For example a distortion function where u = U(x,y) v = V(x,y)
474% And the functions U() and V() have separate coefficients, but are being
475% generated from a common x,y->u,v data set.
476%
477% Another example is generation of a color gradient from a set of colors at
478% specific coordinates, such as a list x,y -> r,g,b,a.
479%
480% You can also use the 'vectors' to generate an inverse of the given 'matrix'
481% though as a 'column first array' rather than a 'row first array'. For
482% details see http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
483%
484*/
485MagickPrivate MagickBooleanType GaussJordanElimination(double **matrix,
486 double **vectors,const size_t rank,const size_t number_vectors)
487{
488#define GaussJordanSwap(x,y) \
489{ \
490 if ((x) != (y)) \
491 { \
492 (x)+=(y); \
493 (y)=(x)-(y); \
494 (x)=(x)-(y); \
495 } \
496}
497
498 double
499 max,
500 scale;
501
502 ssize_t
503 i,
504 j,
505 k;
506
507 ssize_t
508 column,
509 *columns,
510 *pivots,
511 row,
512 *rows;
513
514 columns=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*columns));
515 rows=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*rows));
516 pivots=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*pivots));
517 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
518 (pivots == (ssize_t *) NULL))
519 {
520 if (pivots != (ssize_t *) NULL)
521 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
522 if (columns != (ssize_t *) NULL)
523 columns=(ssize_t *) RelinquishMagickMemory(columns);
524 if (rows != (ssize_t *) NULL)
525 rows=(ssize_t *) RelinquishMagickMemory(rows);
526 return(MagickFalse);
527 }
528 (void) memset(columns,0,rank*sizeof(*columns));
529 (void) memset(rows,0,rank*sizeof(*rows));
530 (void) memset(pivots,0,rank*sizeof(*pivots));
531 column=0;
532 row=0;
533 for (i=0; i < (ssize_t) rank; i++)
534 {
535 max=0.0;
536 for (j=0; j < (ssize_t) rank; j++)
537 if (pivots[j] != 1)
538 {
539 for (k=0; k < (ssize_t) rank; k++)
540 if (pivots[k] != 0)
541 {
542 if (pivots[k] > 1)
543 return(MagickFalse);
544 }
545 else
546 if (fabs(matrix[j][k]) >= max)
547 {
548 max=fabs(matrix[j][k]);
549 row=j;
550 column=k;
551 }
552 }
553 pivots[column]++;
554 if (row != column)
555 {
556 for (k=0; k < (ssize_t) rank; k++)
557 GaussJordanSwap(matrix[row][k],matrix[column][k]);
558 for (k=0; k < (ssize_t) number_vectors; k++)
559 GaussJordanSwap(vectors[k][row],vectors[k][column]);
560 }
561 rows[i]=row;
562 columns[i]=column;
563 if (matrix[column][column] == 0.0)
564 return(MagickFalse); /* singularity */
565 scale=PerceptibleReciprocal(matrix[column][column]);
566 matrix[column][column]=1.0;
567 for (j=0; j < (ssize_t) rank; j++)
568 matrix[column][j]*=scale;
569 for (j=0; j < (ssize_t) number_vectors; j++)
570 vectors[j][column]*=scale;
571 for (j=0; j < (ssize_t) rank; j++)
572 if (j != column)
573 {
574 scale=matrix[j][column];
575 matrix[j][column]=0.0;
576 for (k=0; k < (ssize_t) rank; k++)
577 matrix[j][k]-=scale*matrix[column][k];
578 for (k=0; k < (ssize_t) number_vectors; k++)
579 vectors[k][j]-=scale*vectors[k][column];
580 }
581 }
582 for (j=(ssize_t) rank-1; j >= 0; j--)
583 if (columns[j] != rows[j])
584 for (i=0; i < (ssize_t) rank; i++)
585 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
586 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
587 rows=(ssize_t *) RelinquishMagickMemory(rows);
588 columns=(ssize_t *) RelinquishMagickMemory(columns);
589 return(MagickTrue);
590}
591
592/*
593%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594% %
595% %
596% %
597% G e t M a t r i x C o l u m n s %
598% %
599% %
600% %
601%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602%
603% GetMatrixColumns() returns the number of columns in the matrix.
604%
605% The format of the GetMatrixColumns method is:
606%
607% size_t GetMatrixColumns(const MatrixInfo *matrix_info)
608%
609% A description of each parameter follows:
610%
611% o matrix_info: the matrix.
612%
613*/
614MagickExport size_t GetMatrixColumns(const MatrixInfo *matrix_info)
615{
616 assert(matrix_info != (MatrixInfo *) NULL);
617 assert(matrix_info->signature == MagickCoreSignature);
618 return(matrix_info->columns);
619}
620
621/*
622%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623% %
624% %
625% %
626% G e t M a t r i x E l e m e n t %
627% %
628% %
629% %
630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631%
632% GetMatrixElement() returns the specified element in the matrix.
633%
634% The format of the GetMatrixElement method is:
635%
636% MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
637% const ssize_t x,const ssize_t y,void *value)
638%
639% A description of each parameter follows:
640%
641% o matrix_info: the matrix columns.
642%
643% o x: the matrix x-offset.
644%
645% o y: the matrix y-offset.
646%
647% o value: return the matrix element in this buffer.
648%
649*/
650
651static inline ssize_t EdgeX(const ssize_t x,const size_t columns)
652{
653 if (x < 0L)
654 return(0L);
655 if (x >= (ssize_t) columns)
656 return((ssize_t) (columns-1));
657 return(x);
658}
659
660static inline ssize_t EdgeY(const ssize_t y,const size_t rows)
661{
662 if (y < 0L)
663 return(0L);
664 if (y >= (ssize_t) rows)
665 return((ssize_t) (rows-1));
666 return(y);
667}
668
669static inline MagickOffsetType ReadMatrixElements(
670 const MatrixInfo *magick_restrict matrix_info,const MagickOffsetType offset,
671 const MagickSizeType length,unsigned char *magick_restrict buffer)
672{
673 MagickOffsetType
674 i;
675
676 ssize_t
677 count;
678
679#if !defined(MAGICKCORE_HAVE_PREAD)
680 LockSemaphoreInfo(matrix_info->semaphore);
681 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
682 {
683 UnlockSemaphoreInfo(matrix_info->semaphore);
684 return((MagickOffsetType) -1);
685 }
686#endif
687 count=0;
688 for (i=0; i < (MagickOffsetType) length; i+=count)
689 {
690#if !defined(MAGICKCORE_HAVE_PREAD)
691 count=read(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
692 (MagickSizeType) MagickMaxBufferExtent));
693#else
694 count=pread(matrix_info->file,buffer+i,(size_t) MagickMin(length-
695 (MagickSizeType) i,(MagickSizeType) MagickMaxBufferExtent),offset+i);
696#endif
697 if (count <= 0)
698 {
699 count=0;
700 if (errno != EINTR)
701 break;
702 }
703 }
704#if !defined(MAGICKCORE_HAVE_PREAD)
705 UnlockSemaphoreInfo(matrix_info->semaphore);
706#endif
707 return(i);
708}
709
710MagickExport MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
711 const ssize_t x,const ssize_t y,void *value)
712{
713 MagickOffsetType
714 count,
715 i;
716
717 assert(matrix_info != (const MatrixInfo *) NULL);
718 assert(matrix_info->signature == MagickCoreSignature);
719 i=EdgeY(y,matrix_info->rows)*(MagickOffsetType) matrix_info->columns+
720 EdgeX(x,matrix_info->columns);
721 if (matrix_info->type != DiskCache)
722 {
723 (void) memcpy(value,(unsigned char *) matrix_info->elements+i*
724 (MagickOffsetType) matrix_info->stride,matrix_info->stride);
725 return(MagickTrue);
726 }
727 count=ReadMatrixElements(matrix_info,i*(MagickOffsetType) matrix_info->stride,
728 matrix_info->stride,(unsigned char *) value);
729 if (count != (MagickOffsetType) matrix_info->stride)
730 return(MagickFalse);
731 return(MagickTrue);
732}
733
734/*
735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736% %
737% %
738% %
739% G e t M a t r i x R o w s %
740% %
741% %
742% %
743%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744%
745% GetMatrixRows() returns the number of rows in the matrix.
746%
747% The format of the GetMatrixRows method is:
748%
749% size_t GetMatrixRows(const MatrixInfo *matrix_info)
750%
751% A description of each parameter follows:
752%
753% o matrix_info: the matrix.
754%
755*/
756MagickExport size_t GetMatrixRows(const MatrixInfo *matrix_info)
757{
758 assert(matrix_info != (const MatrixInfo *) NULL);
759 assert(matrix_info->signature == MagickCoreSignature);
760 return(matrix_info->rows);
761}
762
763/*
764%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765% %
766% %
767% %
768+ L e a s t S q u a r e s A d d T e r m s %
769% %
770% %
771% %
772%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773%
774% LeastSquaresAddTerms() adds one set of terms and associate results to the
775% given matrix and vectors for solving using least-squares function fitting.
776%
777% The format of the AcquireMagickMatrix method is:
778%
779% void LeastSquaresAddTerms(double **matrix,double **vectors,
780% const double *terms,const double *results,const size_t rank,
781% const size_t number_vectors);
782%
783% A description of each parameter follows:
784%
785% o matrix: the square matrix to add given terms/results to.
786%
787% o vectors: the result vectors to add terms/results to.
788%
789% o terms: the pre-calculated terms (without the unknown coefficient
790% weights) that forms the equation being added.
791%
792% o results: the result(s) that should be generated from the given terms
793% weighted by the yet-to-be-solved coefficients.
794%
795% o rank: the rank or size of the dimensions of the square matrix.
796% Also the length of vectors, and number of terms being added.
797%
798% o number_vectors: Number of result vectors, and number or results being
799% added. Also represents the number of separable systems of equations
800% that is being solved.
801%
802% Example of use...
803%
804% 2 dimensional Affine Equations (which are separable)
805% c0*x + c2*y + c4*1 => u
806% c1*x + c3*y + c5*1 => v
807%
808% double **matrix = AcquireMagickMatrix(3UL,3UL);
809% double **vectors = AcquireMagickMatrix(2UL,3UL);
810% double terms[3], results[2];
811% ...
812% for each given x,y -> u,v
813% terms[0] = x;
814% terms[1] = y;
815% terms[2] = 1;
816% results[0] = u;
817% results[1] = v;
818% LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
819% ...
820% if ( GaussJordanElimination(matrix,vectors,3UL,2UL) ) {
821% c0 = vectors[0][0];
822% c2 = vectors[0][1];
823% c4 = vectors[0][2];
824% c1 = vectors[1][0];
825% c3 = vectors[1][1];
826% c5 = vectors[1][2];
827% }
828% else
829% printf("Matrix unsolvable\n");
830% RelinquishMagickMatrix(matrix,3UL);
831% RelinquishMagickMatrix(vectors,2UL);
832%
833*/
834MagickPrivate void LeastSquaresAddTerms(double **matrix,double **vectors,
835 const double *terms,const double *results,const size_t rank,
836 const size_t number_vectors)
837{
838 ssize_t
839 i,
840 j;
841
842 for (j=0; j < (ssize_t) rank; j++)
843 {
844 for (i=0; i < (ssize_t) rank; i++)
845 matrix[i][j]+=terms[i]*terms[j];
846 for (i=0; i < (ssize_t) number_vectors; i++)
847 vectors[i][j]+=results[i]*terms[j];
848 }
849}
850
851/*
852%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853% %
854% %
855% %
856% M a t r i x T o I m a g e %
857% %
858% %
859% %
860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861%
862% MatrixToImage() returns a matrix as an image. The matrix elements must be
863% of type double otherwise nonsense is returned.
864%
865% The format of the MatrixToImage method is:
866%
867% Image *MatrixToImage(const MatrixInfo *matrix_info,
868% ExceptionInfo *exception)
869%
870% A description of each parameter follows:
871%
872% o matrix_info: the matrix.
873%
874% o exception: return any errors or warnings in this structure.
875%
876*/
877MagickExport Image *MatrixToImage(const MatrixInfo *matrix_info,
878 ExceptionInfo *exception)
879{
881 *image_view;
882
883 double
884 max_value,
885 min_value,
886 scale_factor;
887
888 Image
889 *image;
890
891 MagickBooleanType
892 status;
893
894 ssize_t
895 y;
896
897 assert(matrix_info != (const MatrixInfo *) NULL);
898 assert(matrix_info->signature == MagickCoreSignature);
899 assert(exception != (ExceptionInfo *) NULL);
900 assert(exception->signature == MagickCoreSignature);
901 if (matrix_info->stride < sizeof(double))
902 return((Image *) NULL);
903 /*
904 Determine range of matrix.
905 */
906 (void) GetMatrixElement(matrix_info,0,0,&min_value);
907 max_value=min_value;
908 for (y=0; y < (ssize_t) matrix_info->rows; y++)
909 {
910 ssize_t
911 x;
912
913 for (x=0; x < (ssize_t) matrix_info->columns; x++)
914 {
915 double
916 value;
917
918 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
919 continue;
920 if (value < min_value)
921 min_value=value;
922 else
923 if (value > max_value)
924 max_value=value;
925 }
926 }
927 if ((min_value == 0.0) && (max_value == 0.0))
928 scale_factor=0;
929 else
930 if (min_value == max_value)
931 {
932 scale_factor=(double) QuantumRange/min_value;
933 min_value=0;
934 }
935 else
936 scale_factor=(double) QuantumRange/(max_value-min_value);
937 /*
938 Convert matrix to image.
939 */
940 image=AcquireImage((ImageInfo *) NULL,exception);
941 image->columns=matrix_info->columns;
942 image->rows=matrix_info->rows;
943 image->colorspace=GRAYColorspace;
944 status=MagickTrue;
945 image_view=AcquireAuthenticCacheView(image,exception);
946#if defined(MAGICKCORE_OPENMP_SUPPORT)
947 #pragma omp parallel for schedule(static) shared(status) \
948 magick_number_threads(image,image,image->rows,2)
949#endif
950 for (y=0; y < (ssize_t) image->rows; y++)
951 {
952 double
953 value;
954
955 Quantum
956 *q;
957
958 ssize_t
959 x;
960
961 if (status == MagickFalse)
962 continue;
963 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
964 if (q == (Quantum *) NULL)
965 {
966 status=MagickFalse;
967 continue;
968 }
969 for (x=0; x < (ssize_t) image->columns; x++)
970 {
971 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
972 continue;
973 value=scale_factor*(value-min_value);
974 *q=ClampToQuantum(value);
975 q+=(ptrdiff_t) GetPixelChannels(image);
976 }
977 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
978 status=MagickFalse;
979 }
980 image_view=DestroyCacheView(image_view);
981 if (status == MagickFalse)
982 image=DestroyImage(image);
983 return(image);
984}
985
986/*
987%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
988% %
989% %
990% %
991% N u l l M a t r i x %
992% %
993% %
994% %
995%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
996%
997% NullMatrix() sets all elements of the matrix to zero.
998%
999% The format of the memset method is:
1000%
1001% MagickBooleanType *NullMatrix(MatrixInfo *matrix_info)
1002%
1003% A description of each parameter follows:
1004%
1005% o matrix_info: the matrix.
1006%
1007*/
1008MagickExport MagickBooleanType NullMatrix(MatrixInfo *matrix_info)
1009{
1010 ssize_t
1011 x;
1012
1013 ssize_t
1014 count,
1015 y;
1016
1017 unsigned char
1018 value;
1019
1020 assert(matrix_info != (const MatrixInfo *) NULL);
1021 assert(matrix_info->signature == MagickCoreSignature);
1022 if (matrix_info->type != DiskCache)
1023 {
1024 (void) memset(matrix_info->elements,0,(size_t)
1025 matrix_info->length);
1026 return(MagickTrue);
1027 }
1028 value=0;
1029 (void) lseek(matrix_info->file,0,SEEK_SET);
1030 for (y=0; y < (ssize_t) matrix_info->rows; y++)
1031 {
1032 for (x=0; x < (ssize_t) matrix_info->length; x++)
1033 {
1034 count=write(matrix_info->file,&value,sizeof(value));
1035 if (count != (ssize_t) sizeof(value))
1036 break;
1037 }
1038 if (x < (ssize_t) matrix_info->length)
1039 break;
1040 }
1041 return(y < (ssize_t) matrix_info->rows ? MagickFalse : MagickTrue);
1042}
1043
1044/*
1045%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1046% %
1047% %
1048% %
1049% R e l i n q u i s h M a g i c k M a t r i x %
1050% %
1051% %
1052% %
1053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054%
1055% RelinquishMagickMatrix() frees the previously acquired matrix (array of
1056% pointers to arrays of doubles).
1057%
1058% The format of the RelinquishMagickMatrix method is:
1059%
1060% double **RelinquishMagickMatrix(double **matrix,
1061% const size_t number_rows)
1062%
1063% A description of each parameter follows:
1064%
1065% o matrix: the matrix to relinquish
1066%
1067% o number_rows: the first dimension of the acquired matrix (number of
1068% pointers)
1069%
1070*/
1071MagickExport double **RelinquishMagickMatrix(double **matrix,
1072 const size_t number_rows)
1073{
1074 ssize_t
1075 i;
1076
1077 if (matrix == (double **) NULL )
1078 return(matrix);
1079 for (i=0; i < (ssize_t) number_rows; i++)
1080 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
1081 matrix=(double **) RelinquishMagickMemory(matrix);
1082 return(matrix);
1083}
1084
1085/*
1086%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087% %
1088% %
1089% %
1090% S e t M a t r i x E l e m e n t %
1091% %
1092% %
1093% %
1094%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1095%
1096% SetMatrixElement() sets the specified element in the matrix.
1097%
1098% The format of the SetMatrixElement method is:
1099%
1100% MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1101% const ssize_t x,const ssize_t y,void *value)
1102%
1103% A description of each parameter follows:
1104%
1105% o matrix_info: the matrix columns.
1106%
1107% o x: the matrix x-offset.
1108%
1109% o y: the matrix y-offset.
1110%
1111% o value: set the matrix element to this value.
1112%
1113*/
1114
1115MagickExport MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1116 const ssize_t x,const ssize_t y,const void *value)
1117{
1118 MagickOffsetType
1119 count,
1120 i;
1121
1122 assert(matrix_info != (const MatrixInfo *) NULL);
1123 assert(matrix_info->signature == MagickCoreSignature);
1124 i=y*(MagickOffsetType) matrix_info->columns+x;
1125 if ((i < 0) ||
1126 (((MagickSizeType) i*matrix_info->stride) >= matrix_info->length))
1127 return(MagickFalse);
1128 if (matrix_info->type != DiskCache)
1129 {
1130 (void) memcpy((unsigned char *) matrix_info->elements+i*
1131 (MagickOffsetType) matrix_info->stride,value,matrix_info->stride);
1132 return(MagickTrue);
1133 }
1134 count=WriteMatrixElements(matrix_info,i*(MagickOffsetType)
1135 matrix_info->stride,matrix_info->stride,(unsigned char *) value);
1136 if (count != (MagickOffsetType) matrix_info->stride)
1137 return(MagickFalse);
1138 return(MagickTrue);
1139}