2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
13 % MagickCore Matrix Methods %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/pixel-accessor.h"
53 #include "MagickCore/pixel-private.h"
54 #include "MagickCore/resource_.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/thread-private.h"
57 #include "MagickCore/utility.h"
80 path[MagickPathExtent];
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % A c q u i r e M a t r i x I n f o %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % AcquireMatrixInfo() allocates the ImageInfo structure.
108 % The format of the AcquireMatrixInfo method is:
110 % MatrixInfo *AcquireMatrixInfo(const size_t columns,const size_t rows,
111 % const size_t stride,ExceptionInfo *exception)
113 % A description of each parameter follows:
115 % o columns: the matrix columns.
117 % o rows: the matrix rows.
119 % o stride: the matrix stride.
121 % o exception: return any errors or warnings in this structure.
126 static void MatrixSignalHandler(int status)
128 ThrowFatalException(CacheFatalError,"UnableToExtendMatrixCache");
132 static inline MagickOffsetType WriteMatrixElements(
133 const MatrixInfo *magick_restrict matrix_info,const MagickOffsetType offset,
134 const MagickSizeType length,const unsigned char *magick_restrict buffer)
136 register MagickOffsetType
142 #if !defined(MAGICKCORE_HAVE_PWRITE)
143 LockSemaphoreInfo(matrix_info->semaphore);
144 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
146 UnlockSemaphoreInfo(matrix_info->semaphore);
147 return((MagickOffsetType) -1);
151 for (i=0; i < (MagickOffsetType) length; i+=count)
153 #if !defined(MAGICKCORE_HAVE_PWRITE)
154 count=write(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
155 (MagickSizeType) SSIZE_MAX));
157 count=pwrite(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
158 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
167 #if !defined(MAGICKCORE_HAVE_PWRITE)
168 UnlockSemaphoreInfo(matrix_info->semaphore);
173 static MagickBooleanType SetMatrixExtent(
174 MatrixInfo *magick_restrict matrix_info,MagickSizeType length)
181 if (length != (MagickSizeType) ((MagickOffsetType) length))
183 offset=(MagickOffsetType) lseek(matrix_info->file,0,SEEK_END);
186 if ((MagickSizeType) offset >= length)
188 extent=(MagickOffsetType) length-1;
189 count=WriteMatrixElements(matrix_info,extent,1,(const unsigned char *) "");
190 #if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
191 if (matrix_info->synchronize != MagickFalse)
192 (void) posix_fallocate(matrix_info->file,offset+1,extent-offset);
195 (void) signal(SIGBUS,MatrixSignalHandler);
197 return(count != (MagickOffsetType) 1 ? MagickFalse : MagickTrue);
200 MagickExport MatrixInfo *AcquireMatrixInfo(const size_t columns,
201 const size_t rows,const size_t stride,ExceptionInfo *exception)
212 matrix_info=(MatrixInfo *) AcquireMagickMemory(sizeof(*matrix_info));
213 if (matrix_info == (MatrixInfo *) NULL)
214 return((MatrixInfo *) NULL);
215 (void) ResetMagickMemory(matrix_info,0,sizeof(*matrix_info));
216 matrix_info->signature=MagickCoreSignature;
217 matrix_info->columns=columns;
218 matrix_info->rows=rows;
219 matrix_info->stride=stride;
220 matrix_info->semaphore=AcquireSemaphoreInfo();
221 synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
222 if (synchronize != (const char *) NULL)
224 matrix_info->synchronize=IsStringTrue(synchronize);
225 synchronize=DestroyString(synchronize);
227 matrix_info->length=(MagickSizeType) columns*rows*stride;
228 if (matrix_info->columns != (size_t) (matrix_info->length/rows/stride))
230 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
231 "CacheResourcesExhausted","`%s'","matrix cache");
232 return(DestroyMatrixInfo(matrix_info));
234 matrix_info->type=MemoryCache;
235 status=AcquireMagickResource(AreaResource,matrix_info->length);
236 if ((status != MagickFalse) &&
237 (matrix_info->length == (MagickSizeType) ((size_t) matrix_info->length)))
239 status=AcquireMagickResource(MemoryResource,matrix_info->length);
240 if (status != MagickFalse)
242 matrix_info->mapped=MagickFalse;
243 matrix_info->elements=AcquireMagickMemory((size_t)
244 matrix_info->length);
245 if (matrix_info->elements == NULL)
247 matrix_info->mapped=MagickTrue;
248 matrix_info->elements=MapBlob(-1,IOMode,0,(size_t)
249 matrix_info->length);
251 if (matrix_info->elements == (unsigned short *) NULL)
252 RelinquishMagickResource(MemoryResource,matrix_info->length);
255 matrix_info->file=(-1);
256 if (matrix_info->elements == (unsigned short *) NULL)
258 status=AcquireMagickResource(DiskResource,matrix_info->length);
259 if (status == MagickFalse)
261 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
262 "CacheResourcesExhausted","`%s'","matrix cache");
263 return(DestroyMatrixInfo(matrix_info));
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)
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;
279 RelinquishMagickResource(MapResource,matrix_info->length);
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % A c q u i r e M a g i c k M a t r i x %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
299 % This used to generate the two dimensional matrix, and vectors required
300 % for the GaussJordanElimination() method below, solving some system of
301 % simultanious equations.
303 % The format of the AcquireMagickMatrix method is:
305 % double **AcquireMagickMatrix(const size_t number_rows,
308 % A description of each parameter follows:
310 % o number_rows: the number pointers for the array of pointers
313 % o size: the size of the array of doubles each pointer points to
314 % (second dimension).
317 MagickExport double **AcquireMagickMatrix(const size_t number_rows,
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++)
332 matrix[i]=(double *) AcquireQuantumMemory(size,sizeof(*matrix[i]));
333 if (matrix[i] == (double *) NULL)
335 for (j=0; j < i; j++)
336 matrix[j]=(double *) RelinquishMagickMemory(matrix[j]);
337 matrix=(double **) RelinquishMagickMemory(matrix);
338 return((double **) NULL);
340 for (j=0; j < (ssize_t) size; j++)
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 % D e s t r o y M a t r i x I n f o %
355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 % DestroyMatrixInfo() dereferences a matrix, deallocating memory associated
360 % The format of the DestroyImage method is:
362 % MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
364 % A description of each parameter follows:
366 % o matrix_info: the matrix.
369 MagickExport MatrixInfo *DestroyMatrixInfo(MatrixInfo *matrix_info)
371 assert(matrix_info != (MatrixInfo *) NULL);
372 assert(matrix_info->signature == MagickCoreSignature);
373 LockSemaphoreInfo(matrix_info->semaphore);
374 switch (matrix_info->type)
378 if (matrix_info->mapped == MagickFalse)
379 matrix_info->elements=RelinquishMagickMemory(matrix_info->elements);
382 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
383 matrix_info->elements=(unsigned short *) NULL;
385 RelinquishMagickResource(MemoryResource,matrix_info->length);
390 (void) UnmapBlob(matrix_info->elements,(size_t) matrix_info->length);
391 matrix_info->elements=NULL;
392 RelinquishMagickResource(MapResource,matrix_info->length);
396 if (matrix_info->file != -1)
397 (void) close(matrix_info->file);
398 (void) RelinquishUniqueFileResource(matrix_info->path);
399 RelinquishMagickResource(DiskResource,matrix_info->length);
405 UnlockSemaphoreInfo(matrix_info->semaphore);
406 RelinquishSemaphoreInfo(&matrix_info->semaphore);
407 return((MatrixInfo *) RelinquishMagickMemory(matrix_info));
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415 + G a u s s J o r d a n E l i m i n a t i o n %
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 % GaussJordanElimination() returns a matrix in reduced row echelon form,
422 % while simultaneously reducing and thus solving the augumented results
425 % See also http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
427 % The format of the GaussJordanElimination method is:
429 % MagickBooleanType GaussJordanElimination(double **matrix,
430 % double **vectors,const size_t rank,const size_t number_vectors)
432 % A description of each parameter follows:
434 % o matrix: the matrix to be reduced, as an 'array of row pointers'.
436 % o vectors: the additional matrix argumenting the matrix for row reduction.
437 % Producing an 'array of column vectors'.
439 % o rank: The size of the matrix (both rows and columns).
440 % Also represents the number terms that need to be solved.
442 % o number_vectors: Number of vectors columns, argumenting the above matrix.
443 % Usally 1, but can be more for more complex equation solving.
445 % Note that the 'matrix' is given as a 'array of row pointers' of rank size.
446 % That is values can be assigned as matrix[row][column] where 'row' is
447 % typically the equation, and 'column' is the term of the equation.
448 % That is the matrix is in the form of a 'row first array'.
450 % However 'vectors' is a 'array of column pointers' which can have any number
451 % of columns, with each column array the same 'rank' size as 'matrix'.
453 % This allows for simpler handling of the results, especially is only one
454 % column 'vector' is all that is required to produce the desired solution.
456 % For example, the 'vectors' can consist of a pointer to a simple array of
457 % doubles. when only one set of simultanious equations is to be solved from
458 % the given set of coefficient weighted terms.
460 % double **matrix = AcquireMagickMatrix(8UL,8UL);
461 % double coefficents[8];
463 % GaussJordanElimination(matrix, &coefficents, 8UL, 1UL);
465 % However by specifing more 'columns' (as an 'array of vector columns',
466 % you can use this function to solve a set of 'separable' equations.
468 % For example a distortion function where u = U(x,y) v = V(x,y)
469 % And the functions U() and V() have separate coefficents, but are being
470 % generated from a common x,y->u,v data set.
472 % Another example is generation of a color gradient from a set of colors at
473 % specific coordients, such as a list x,y -> r,g,b,a.
475 % You can also use the 'vectors' to generate an inverse of the given 'matrix'
476 % though as a 'column first array' rather than a 'row first array'. For
477 % details see http://en.wikipedia.org/wiki/Gauss-Jordan_elimination
480 MagickPrivate MagickBooleanType GaussJordanElimination(double **matrix,
481 double **vectors,const size_t rank,const size_t number_vectors)
483 #define GaussJordanSwap(x,y) \
509 columns=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*columns));
510 rows=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*rows));
511 pivots=(ssize_t *) AcquireQuantumMemory(rank,sizeof(*pivots));
512 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
513 (pivots == (ssize_t *) NULL))
515 if (pivots != (ssize_t *) NULL)
516 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
517 if (columns != (ssize_t *) NULL)
518 columns=(ssize_t *) RelinquishMagickMemory(columns);
519 if (rows != (ssize_t *) NULL)
520 rows=(ssize_t *) RelinquishMagickMemory(rows);
523 (void) ResetMagickMemory(columns,0,rank*sizeof(*columns));
524 (void) ResetMagickMemory(rows,0,rank*sizeof(*rows));
525 (void) ResetMagickMemory(pivots,0,rank*sizeof(*pivots));
528 for (i=0; i < (ssize_t) rank; i++)
531 for (j=0; j < (ssize_t) rank; j++)
534 for (k=0; k < (ssize_t) rank; k++)
541 if (fabs(matrix[j][k]) >= max)
543 max=fabs(matrix[j][k]);
551 for (k=0; k < (ssize_t) rank; k++)
552 GaussJordanSwap(matrix[row][k],matrix[column][k]);
553 for (k=0; k < (ssize_t) number_vectors; k++)
554 GaussJordanSwap(vectors[k][row],vectors[k][column]);
558 if (matrix[column][column] == 0.0)
559 return(MagickFalse); /* sigularity */
560 scale=PerceptibleReciprocal(matrix[column][column]);
561 matrix[column][column]=1.0;
562 for (j=0; j < (ssize_t) rank; j++)
563 matrix[column][j]*=scale;
564 for (j=0; j < (ssize_t) number_vectors; j++)
565 vectors[j][column]*=scale;
566 for (j=0; j < (ssize_t) rank; j++)
569 scale=matrix[j][column];
570 matrix[j][column]=0.0;
571 for (k=0; k < (ssize_t) rank; k++)
572 matrix[j][k]-=scale*matrix[column][k];
573 for (k=0; k < (ssize_t) number_vectors; k++)
574 vectors[k][j]-=scale*vectors[k][column];
577 for (j=(ssize_t) rank-1; j >= 0; j--)
578 if (columns[j] != rows[j])
579 for (i=0; i < (ssize_t) rank; i++)
580 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
581 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
582 rows=(ssize_t *) RelinquishMagickMemory(rows);
583 columns=(ssize_t *) RelinquishMagickMemory(columns);
588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
592 % G e t M a t r i x C o l u m n s %
596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 % GetMatrixColumns() returns the number of columns in the matrix.
600 % The format of the GetMatrixColumns method is:
602 % size_t GetMatrixColumns(const MatrixInfo *matrix_info)
604 % A description of each parameter follows:
606 % o matrix_info: the matrix.
609 MagickExport size_t GetMatrixColumns(const MatrixInfo *matrix_info)
611 assert(matrix_info != (MatrixInfo *) NULL);
612 assert(matrix_info->signature == MagickCoreSignature);
613 return(matrix_info->columns);
617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 % G e t M a t r i x E l e m e n t %
625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627 % GetMatrixElement() returns the specifed element in the matrix.
629 % The format of the GetMatrixElement method is:
631 % MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
632 % const ssize_t x,const ssize_t y,void *value)
634 % A description of each parameter follows:
636 % o matrix_info: the matrix columns.
638 % o x: the matrix x-offset.
640 % o y: the matrix y-offset.
642 % o value: return the matrix element in this buffer.
646 static inline ssize_t EdgeX(const ssize_t x,const size_t columns)
650 if (x >= (ssize_t) columns)
651 return((ssize_t) (columns-1));
655 static inline ssize_t EdgeY(const ssize_t y,const size_t rows)
659 if (y >= (ssize_t) rows)
660 return((ssize_t) (rows-1));
664 static inline MagickOffsetType ReadMatrixElements(
665 const MatrixInfo *magick_restrict matrix_info,const MagickOffsetType offset,
666 const MagickSizeType length,unsigned char *magick_restrict buffer)
668 register MagickOffsetType
674 #if !defined(MAGICKCORE_HAVE_PREAD)
675 LockSemaphoreInfo(matrix_info->semaphore);
676 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
678 UnlockSemaphoreInfo(matrix_info->semaphore);
679 return((MagickOffsetType) -1);
683 for (i=0; i < (MagickOffsetType) length; i+=count)
685 #if !defined(MAGICKCORE_HAVE_PREAD)
686 count=read(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
687 (MagickSizeType) SSIZE_MAX));
689 count=pread(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
690 (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
699 #if !defined(MAGICKCORE_HAVE_PREAD)
700 UnlockSemaphoreInfo(matrix_info->semaphore);
705 MagickExport MagickBooleanType GetMatrixElement(const MatrixInfo *matrix_info,
706 const ssize_t x,const ssize_t y,void *value)
712 assert(matrix_info != (const MatrixInfo *) NULL);
713 assert(matrix_info->signature == MagickCoreSignature);
714 i=(MagickOffsetType) EdgeY(y,matrix_info->rows)*matrix_info->columns+
715 EdgeX(x,matrix_info->columns);
716 if (matrix_info->type != DiskCache)
718 (void) memcpy(value,(unsigned char *) matrix_info->elements+i*
719 matrix_info->stride,matrix_info->stride);
722 count=ReadMatrixElements(matrix_info,i*matrix_info->stride,
723 matrix_info->stride,(unsigned char *) value);
724 if (count != (MagickOffsetType) matrix_info->stride)
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % G e t M a t r i x R o w s %
738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740 % GetMatrixRows() returns the number of rows in the matrix.
742 % The format of the GetMatrixRows method is:
744 % size_t GetMatrixRows(const MatrixInfo *matrix_info)
746 % A description of each parameter follows:
748 % o matrix_info: the matrix.
751 MagickExport size_t GetMatrixRows(const MatrixInfo *matrix_info)
753 assert(matrix_info != (const MatrixInfo *) NULL);
754 assert(matrix_info->signature == MagickCoreSignature);
755 return(matrix_info->rows);
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 + L e a s t S q u a r e s A d d T e r m s %
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 % LeastSquaresAddTerms() adds one set of terms and associate results to the
770 % given matrix and vectors for solving using least-squares function fitting.
772 % The format of the AcquireMagickMatrix method is:
774 % void LeastSquaresAddTerms(double **matrix,double **vectors,
775 % const double *terms,const double *results,const size_t rank,
776 % const size_t number_vectors);
778 % A description of each parameter follows:
780 % o matrix: the square matrix to add given terms/results to.
782 % o vectors: the result vectors to add terms/results to.
784 % o terms: the pre-calculated terms (without the unknown coefficent
785 % weights) that forms the equation being added.
787 % o results: the result(s) that should be generated from the given terms
788 % weighted by the yet-to-be-solved coefficents.
790 % o rank: the rank or size of the dimensions of the square matrix.
791 % Also the length of vectors, and number of terms being added.
793 % o number_vectors: Number of result vectors, and number or results being
794 % added. Also represents the number of separable systems of equations
795 % that is being solved.
799 % 2 dimensional Affine Equations (which are separable)
800 % c0*x + c2*y + c4*1 => u
801 % c1*x + c3*y + c5*1 => v
803 % double **matrix = AcquireMagickMatrix(3UL,3UL);
804 % double **vectors = AcquireMagickMatrix(2UL,3UL);
805 % double terms[3], results[2];
807 % for each given x,y -> u,v
813 % LeastSquaresAddTerms(matrix,vectors,terms,results,3UL,2UL);
815 % if ( GaussJordanElimination(matrix,vectors,3UL,2UL) ) {
816 % c0 = vectors[0][0];
817 % c2 = vectors[0][1];
818 % c4 = vectors[0][2];
819 % c1 = vectors[1][0];
820 % c3 = vectors[1][1];
821 % c5 = vectors[1][2];
824 % printf("Matrix unsolvable\n);
825 % RelinquishMagickMatrix(matrix,3UL);
826 % RelinquishMagickMatrix(vectors,2UL);
829 MagickPrivate void LeastSquaresAddTerms(double **matrix,double **vectors,
830 const double *terms,const double *results,const size_t rank,
831 const size_t number_vectors)
837 for (j=0; j < (ssize_t) rank; j++)
839 for (i=0; i < (ssize_t) rank; i++)
840 matrix[i][j]+=terms[i]*terms[j];
841 for (i=0; i < (ssize_t) number_vectors; i++)
842 vectors[i][j]+=results[i]*terms[j];
847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851 % M a t r i x T o I m a g e %
855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857 % MatrixToImage() returns a matrix as an image. The matrix elements must be
858 % of type double otherwise nonsense is returned.
860 % The format of the MatrixToImage method is:
862 % Image *MatrixToImage(const MatrixInfo *matrix_info,
863 % ExceptionInfo *exception)
865 % A description of each parameter follows:
867 % o matrix_info: the matrix.
869 % o exception: return any errors or warnings in this structure.
872 MagickExport Image *MatrixToImage(const MatrixInfo *matrix_info,
873 ExceptionInfo *exception)
893 assert(matrix_info != (const MatrixInfo *) NULL);
894 assert(matrix_info->signature == MagickCoreSignature);
895 assert(exception != (ExceptionInfo *) NULL);
896 assert(exception->signature == MagickCoreSignature);
897 if (matrix_info->stride < sizeof(double))
898 return((Image *) NULL);
900 Determine range of matrix.
902 (void) GetMatrixElement(matrix_info,0,0,&value);
905 for (y=0; y < (ssize_t) matrix_info->rows; y++)
910 for (x=0; x < (ssize_t) matrix_info->columns; x++)
912 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
914 if (value < min_value)
917 if (value > max_value)
921 if ((min_value == 0.0) && (max_value == 0.0))
924 if (min_value == max_value)
926 scale_factor=(double) QuantumRange/min_value;
930 scale_factor=(double) QuantumRange/(max_value-min_value);
932 Convert matrix to image.
934 image=AcquireImage((ImageInfo *) NULL,exception);
935 image->columns=matrix_info->columns;
936 image->rows=matrix_info->rows;
937 image->colorspace=GRAYColorspace;
939 image_view=AcquireAuthenticCacheView(image,exception);
940 #if defined(MAGICKCORE_OPENMP_SUPPORT)
941 #pragma omp parallel for schedule(static,4) shared(status) \
942 magick_number_threads(image,image,image->rows,1)
944 for (y=0; y < (ssize_t) image->rows; y++)
955 if (status == MagickFalse)
957 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
958 if (q == (Quantum *) NULL)
963 for (x=0; x < (ssize_t) image->columns; x++)
965 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
967 value=scale_factor*(value-min_value);
968 *q=ClampToQuantum(value);
969 q+=GetPixelChannels(image);
971 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
974 image_view=DestroyCacheView(image_view);
975 if (status == MagickFalse)
976 image=DestroyImage(image);
981 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 % N u l l M a t r i x %
989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
991 % NullMatrix() sets all elements of the matrix to zero.
993 % The format of the ResetMagickMemory method is:
995 % MagickBooleanType *NullMatrix(MatrixInfo *matrix_info)
997 % A description of each parameter follows:
999 % o matrix_info: the matrix.
1002 MagickExport MagickBooleanType NullMatrix(MatrixInfo *matrix_info)
1014 assert(matrix_info != (const MatrixInfo *) NULL);
1015 assert(matrix_info->signature == MagickCoreSignature);
1016 if (matrix_info->type != DiskCache)
1018 (void) ResetMagickMemory(matrix_info->elements,0,(size_t)
1019 matrix_info->length);
1023 (void) lseek(matrix_info->file,0,SEEK_SET);
1024 for (y=0; y < (ssize_t) matrix_info->rows; y++)
1026 for (x=0; x < (ssize_t) matrix_info->length; x++)
1028 count=write(matrix_info->file,&value,sizeof(value));
1029 if (count != (ssize_t) sizeof(value))
1032 if (x < (ssize_t) matrix_info->length)
1035 return(y < (ssize_t) matrix_info->rows ? MagickFalse : MagickTrue);
1039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1043 % R e l i n q u i s h M a g i c k M a t r i x %
1047 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1049 % RelinquishMagickMatrix() frees the previously acquired matrix (array of
1050 % pointers to arrays of doubles).
1052 % The format of the RelinquishMagickMatrix method is:
1054 % double **RelinquishMagickMatrix(double **matrix,
1055 % const size_t number_rows)
1057 % A description of each parameter follows:
1059 % o matrix: the matrix to relinquish
1061 % o number_rows: the first dimension of the acquired matrix (number of
1065 MagickExport double **RelinquishMagickMatrix(double **matrix,
1066 const size_t number_rows)
1071 if (matrix == (double **) NULL )
1073 for (i=0; i < (ssize_t) number_rows; i++)
1074 matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
1075 matrix=(double **) RelinquishMagickMemory(matrix);
1080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084 % S e t M a t r i x E l e m e n t %
1088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090 % SetMatrixElement() sets the specifed element in the matrix.
1092 % The format of the SetMatrixElement method is:
1094 % MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1095 % const ssize_t x,const ssize_t y,void *value)
1097 % A description of each parameter follows:
1099 % o matrix_info: the matrix columns.
1101 % o x: the matrix x-offset.
1103 % o y: the matrix y-offset.
1105 % o value: set the matrix element to this value.
1109 MagickExport MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
1110 const ssize_t x,const ssize_t y,const void *value)
1116 assert(matrix_info != (const MatrixInfo *) NULL);
1117 assert(matrix_info->signature == MagickCoreSignature);
1118 i=(MagickOffsetType) y*matrix_info->columns+x;
1120 ((MagickSizeType) (i*matrix_info->stride) >= matrix_info->length))
1121 return(MagickFalse);
1122 if (matrix_info->type != DiskCache)
1124 (void) memcpy((unsigned char *) matrix_info->elements+i*
1125 matrix_info->stride,value,matrix_info->stride);
1128 count=WriteMatrixElements(matrix_info,i*matrix_info->stride,
1129 matrix_info->stride,(unsigned char *) value);
1130 if (count != (MagickOffsetType) matrix_info->stride)
1131 return(MagickFalse);