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