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/nt-base-private.h"
53#include "MagickCore/pixel-accessor.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];
126static void MatrixSignalHandler(
int magick_unused(status))
128 magick_unreferenced(status);
129 ThrowFatalException(CacheFatalError,
"UnableToExtendMatrixCache");
133static inline MagickOffsetType WriteMatrixElements(
134 const MatrixInfo *magick_restrict matrix_info,
const MagickOffsetType offset,
135 const MagickSizeType length,
const unsigned char *magick_restrict buffer)
143#if !defined(MAGICKCORE_HAVE_PWRITE)
144 LockSemaphoreInfo(matrix_info->semaphore);
145 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
147 UnlockSemaphoreInfo(matrix_info->semaphore);
148 return((MagickOffsetType) -1);
152 for (i=0; i < (MagickOffsetType) length; i+=count)
154#if !defined(MAGICKCORE_HAVE_PWRITE)
155 count=write(matrix_info->file,buffer+i,(
size_t) MagickMin(length-
156 (MagickSizeType) i,(MagickSizeType) MagickMaxBufferExtent));
158 count=pwrite(matrix_info->file,buffer+i,(
size_t) MagickMin(length-
159 (MagickSizeType) i,(MagickSizeType) MagickMaxBufferExtent),offset+i);
168#if !defined(MAGICKCORE_HAVE_PWRITE)
169 UnlockSemaphoreInfo(matrix_info->semaphore);
174static MagickBooleanType SetMatrixExtent(
175 MatrixInfo *magick_restrict matrix_info,MagickSizeType length)
182 if (length != (MagickSizeType) ((MagickOffsetType) length))
184 offset=(MagickOffsetType) lseek(matrix_info->file,0,SEEK_END);
187 if ((MagickSizeType) offset >= length)
189 extent=(MagickOffsetType) length-1;
190 count=WriteMatrixElements(matrix_info,extent,1,(
const unsigned char *)
"");
191#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
192 if (matrix_info->synchronize != MagickFalse)
193 (void) posix_fallocate(matrix_info->file,offset+1,extent-offset);
196 (void) signal(SIGBUS,MatrixSignalHandler);
198 return(count != (MagickOffsetType) 1 ? MagickFalse : MagickTrue);
201MagickExport
MatrixInfo *AcquireMatrixInfo(
const size_t columns,
202 const size_t rows,
const size_t stride,
ExceptionInfo *exception)
213 matrix_info=(
MatrixInfo *) AcquireMagickMemory(
sizeof(*matrix_info));
216 (void) memset(matrix_info,0,
sizeof(*matrix_info));
217 matrix_info->signature=MagickCoreSignature;
218 matrix_info->columns=columns;
219 matrix_info->rows=rows;
220 matrix_info->stride=stride;
221 matrix_info->semaphore=AcquireSemaphoreInfo();
222 synchronize=GetEnvironmentValue(
"MAGICK_SYNCHRONIZE");
223 if (synchronize != (
const char *) NULL)
225 matrix_info->synchronize=IsStringTrue(synchronize);
226 synchronize=DestroyString(synchronize);
228 matrix_info->length=(MagickSizeType) columns*rows*stride;
229 if (matrix_info->columns != (
size_t) (matrix_info->length/rows/stride))
231 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
232 "CacheResourcesExhausted",
"`%s'",
"matrix cache");
233 return(DestroyMatrixInfo(matrix_info));
235 matrix_info->type=MemoryCache;
236 status=AcquireMagickResource(AreaResource,matrix_info->length);
237 if ((status != MagickFalse) &&
238 (matrix_info->length == (MagickSizeType) ((
size_t) matrix_info->length)))
240 status=AcquireMagickResource(MemoryResource,matrix_info->length);
241 if (status != MagickFalse)
243 matrix_info->mapped=MagickFalse;
244 matrix_info->elements=AcquireMagickMemory((
size_t)
245 matrix_info->length);
246 if (matrix_info->elements == NULL)
248 matrix_info->mapped=MagickTrue;
249 matrix_info->elements=MapBlob(-1,IOMode,0,(
size_t)
250 matrix_info->length);
252 if (matrix_info->elements == (
unsigned short *) NULL)
253 RelinquishMagickResource(MemoryResource,matrix_info->length);
256 matrix_info->file=(-1);
257 if (matrix_info->elements == (
unsigned short *) NULL)
259 status=AcquireMagickResource(DiskResource,matrix_info->length);
260 if (status == MagickFalse)
262 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
263 "CacheResourcesExhausted",
"`%s'",
"matrix cache");
264 return(DestroyMatrixInfo(matrix_info));
266 matrix_info->type=DiskCache;
267 matrix_info->file=AcquireUniqueFileResource(matrix_info->path);
268 if (matrix_info->file == -1)
269 return(DestroyMatrixInfo(matrix_info));
270 status=AcquireMagickResource(MapResource,matrix_info->length);
271 if (status != MagickFalse)
273 status=SetMatrixExtent(matrix_info,matrix_info->length);
274 if (status != MagickFalse)
275 matrix_info->elements=(
void *) MapBlob(matrix_info->file,IOMode,0,
276 (
size_t) matrix_info->length);
277 if (matrix_info->elements != NULL)
278 matrix_info->type=MapCache;
280 RelinquishMagickResource(MapResource,matrix_info->length);
318MagickExport
double **AcquireMagickMatrix(
const size_t number_rows,
328 matrix=(
double **) AcquireQuantumMemory(number_rows,
sizeof(*matrix));
329 if (matrix == (
double **) NULL)
330 return((
double **) NULL);
331 for (i=0; i < (ssize_t) number_rows; i++)
333 matrix[i]=(
double *) AcquireQuantumMemory(size,
sizeof(*matrix[i]));
334 if (matrix[i] == (
double *) NULL)
336 for (j=0; j < i; j++)
337 matrix[j]=(
double *) RelinquishMagickMemory(matrix[j]);
338 matrix=(
double **) RelinquishMagickMemory(matrix);
339 return((
double **) NULL);
341 for (j=0; j < (ssize_t) size; j++)
373 assert(matrix_info->signature == MagickCoreSignature);
374 LockSemaphoreInfo(matrix_info->semaphore);
375 switch (matrix_info->type)
379 if (matrix_info->mapped == MagickFalse)
380 matrix_info->elements=RelinquishMagickMemory(matrix_info->elements);
383 (void) UnmapBlob(matrix_info->elements,(
size_t) matrix_info->length);
384 matrix_info->elements=(
unsigned short *) NULL;
386 RelinquishMagickResource(MemoryResource,matrix_info->length);
391 (void) UnmapBlob(matrix_info->elements,(
size_t) matrix_info->length);
392 matrix_info->elements=NULL;
393 RelinquishMagickResource(MapResource,matrix_info->length);
398 if (matrix_info->file != -1)
399 (void) close(matrix_info->file);
400 (void) RelinquishUniqueFileResource(matrix_info->path);
401 RelinquishMagickResource(DiskResource,matrix_info->length);
407 UnlockSemaphoreInfo(matrix_info->semaphore);
408 RelinquishSemaphoreInfo(&matrix_info->semaphore);
409 return((
MatrixInfo *) RelinquishMagickMemory(matrix_info));
482MagickPrivate MagickBooleanType GaussJordanElimination(
double **matrix,
483 double **vectors,
const size_t rank,
const size_t number_vectors)
485#define GaussJordanSwap(x,y) \
511 columns=(ssize_t *) AcquireQuantumMemory(rank,
sizeof(*columns));
512 rows=(ssize_t *) AcquireQuantumMemory(rank,
sizeof(*rows));
513 pivots=(ssize_t *) AcquireQuantumMemory(rank,
sizeof(*pivots));
514 if ((rows == (ssize_t *) NULL) || (columns == (ssize_t *) NULL) ||
515 (pivots == (ssize_t *) NULL))
517 if (pivots != (ssize_t *) NULL)
518 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
519 if (columns != (ssize_t *) NULL)
520 columns=(ssize_t *) RelinquishMagickMemory(columns);
521 if (rows != (ssize_t *) NULL)
522 rows=(ssize_t *) RelinquishMagickMemory(rows);
525 (void) memset(columns,0,rank*
sizeof(*columns));
526 (void) memset(rows,0,rank*
sizeof(*rows));
527 (void) memset(pivots,0,rank*
sizeof(*pivots));
530 for (i=0; i < (ssize_t) rank; i++)
533 for (j=0; j < (ssize_t) rank; j++)
536 for (k=0; k < (ssize_t) rank; k++)
543 if (fabs(matrix[j][k]) >= max)
545 max=fabs(matrix[j][k]);
553 for (k=0; k < (ssize_t) rank; k++)
554 GaussJordanSwap(matrix[row][k],matrix[column][k]);
555 for (k=0; k < (ssize_t) number_vectors; k++)
556 GaussJordanSwap(vectors[k][row],vectors[k][column]);
560 if (matrix[column][column] == 0.0)
562 scale=PerceptibleReciprocal(matrix[column][column]);
563 matrix[column][column]=1.0;
564 for (j=0; j < (ssize_t) rank; j++)
565 matrix[column][j]*=scale;
566 for (j=0; j < (ssize_t) number_vectors; j++)
567 vectors[j][column]*=scale;
568 for (j=0; j < (ssize_t) rank; j++)
571 scale=matrix[j][column];
572 matrix[j][column]=0.0;
573 for (k=0; k < (ssize_t) rank; k++)
574 matrix[j][k]-=scale*matrix[column][k];
575 for (k=0; k < (ssize_t) number_vectors; k++)
576 vectors[k][j]-=scale*vectors[k][column];
579 for (j=(ssize_t) rank-1; j >= 0; j--)
580 if (columns[j] != rows[j])
581 for (i=0; i < (ssize_t) rank; i++)
582 GaussJordanSwap(matrix[i][rows[j]],matrix[i][columns[j]]);
583 pivots=(ssize_t *) RelinquishMagickMemory(pivots);
584 rows=(ssize_t *) RelinquishMagickMemory(rows);
585 columns=(ssize_t *) RelinquishMagickMemory(columns);
611MagickExport
size_t GetMatrixColumns(
const MatrixInfo *matrix_info)
614 assert(matrix_info->signature == MagickCoreSignature);
615 return(matrix_info->columns);
648static inline ssize_t EdgeX(
const ssize_t x,
const size_t columns)
652 if (x >= (ssize_t) columns)
653 return((ssize_t) (columns-1));
657static inline ssize_t EdgeY(
const ssize_t y,
const size_t rows)
661 if (y >= (ssize_t) rows)
662 return((ssize_t) (rows-1));
666static inline MagickOffsetType ReadMatrixElements(
667 const MatrixInfo *magick_restrict matrix_info,
const MagickOffsetType offset,
668 const MagickSizeType length,
unsigned char *magick_restrict buffer)
676#if !defined(MAGICKCORE_HAVE_PREAD)
677 LockSemaphoreInfo(matrix_info->semaphore);
678 if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
680 UnlockSemaphoreInfo(matrix_info->semaphore);
681 return((MagickOffsetType) -1);
685 for (i=0; i < (MagickOffsetType) length; i+=count)
687#if !defined(MAGICKCORE_HAVE_PREAD)
688 count=read(matrix_info->file,buffer+i,(
size_t) MagickMin(length-i,
689 (MagickSizeType) MagickMaxBufferExtent));
691 count=pread(matrix_info->file,buffer+i,(
size_t) MagickMin(length-
692 (MagickSizeType) i,(MagickSizeType) MagickMaxBufferExtent),offset+i);
701#if !defined(MAGICKCORE_HAVE_PREAD)
702 UnlockSemaphoreInfo(matrix_info->semaphore);
707MagickExport MagickBooleanType GetMatrixElement(
const MatrixInfo *matrix_info,
708 const ssize_t x,
const ssize_t y,
void *value)
714 assert(matrix_info != (
const MatrixInfo *) NULL);
715 assert(matrix_info->signature == MagickCoreSignature);
716 i=EdgeY(y,matrix_info->rows)*(MagickOffsetType) matrix_info->columns+
717 EdgeX(x,matrix_info->columns);
718 if (matrix_info->type != DiskCache)
720 (void) memcpy(value,(
unsigned char *) matrix_info->elements+i*
721 (MagickOffsetType) matrix_info->stride,matrix_info->stride);
724 count=ReadMatrixElements(matrix_info,i*(MagickOffsetType) matrix_info->stride,
725 matrix_info->stride,(
unsigned char *) value);
726 if (count != (MagickOffsetType) matrix_info->stride)
753MagickExport
size_t GetMatrixRows(
const MatrixInfo *matrix_info)
755 assert(matrix_info != (
const MatrixInfo *) NULL);
756 assert(matrix_info->signature == MagickCoreSignature);
757 return(matrix_info->rows);
831MagickPrivate
void LeastSquaresAddTerms(
double **matrix,
double **vectors,
832 const double *terms,
const double *results,
const size_t rank,
833 const size_t number_vectors)
839 for (j=0; j < (ssize_t) rank; j++)
841 for (i=0; i < (ssize_t) rank; i++)
842 matrix[i][j]+=terms[i]*terms[j];
843 for (i=0; i < (ssize_t) number_vectors; i++)
844 vectors[i][j]+=results[i]*terms[j];
894 assert(matrix_info != (
const MatrixInfo *) NULL);
895 assert(matrix_info->signature == MagickCoreSignature);
897 assert(exception->signature == MagickCoreSignature);
898 if (matrix_info->stride <
sizeof(
double))
899 return((
Image *) NULL);
903 (void) GetMatrixElement(matrix_info,0,0,&min_value);
905 for (y=0; y < (ssize_t) matrix_info->rows; y++)
910 for (x=0; x < (ssize_t) matrix_info->columns; x++)
915 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
917 if (value < min_value)
920 if (value > max_value)
924 if ((min_value == 0.0) && (max_value == 0.0))
927 if (min_value == max_value)
929 scale_factor=(double) QuantumRange/min_value;
933 scale_factor=(double) QuantumRange/(max_value-min_value);
937 image=AcquireImage((
ImageInfo *) NULL,exception);
938 image->columns=matrix_info->columns;
939 image->rows=matrix_info->rows;
940 image->colorspace=GRAYColorspace;
942 image_view=AcquireAuthenticCacheView(image,exception);
943#if defined(MAGICKCORE_OPENMP_SUPPORT)
944 #pragma omp parallel for schedule(static) shared(status) \
945 magick_number_threads(image,image,image->rows,2)
947 for (y=0; y < (ssize_t) image->rows; y++)
958 if (status == MagickFalse)
960 q=QueueCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
961 if (q == (Quantum *) NULL)
966 for (x=0; x < (ssize_t) image->columns; x++)
968 if (GetMatrixElement(matrix_info,x,y,&value) == MagickFalse)
970 value=scale_factor*(value-min_value);
971 *q=ClampToQuantum(value);
972 q+=(ptrdiff_t) GetPixelChannels(image);
974 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
977 image_view=DestroyCacheView(image_view);
978 if (status == MagickFalse)
979 image=DestroyImage(image);
1005MagickExport MagickBooleanType NullMatrix(
MatrixInfo *matrix_info)
1017 assert(matrix_info != (
const MatrixInfo *) NULL);
1018 assert(matrix_info->signature == MagickCoreSignature);
1019 if (matrix_info->type != DiskCache)
1021 (void) memset(matrix_info->elements,0,(
size_t)
1022 matrix_info->length);
1026 (void) lseek(matrix_info->file,0,SEEK_SET);
1027 for (y=0; y < (ssize_t) matrix_info->rows; y++)
1029 for (x=0; x < (ssize_t) matrix_info->length; x++)
1031 count=write(matrix_info->file,&value,
sizeof(value));
1032 if (count != (ssize_t)
sizeof(value))
1035 if (x < (ssize_t) matrix_info->length)
1038 return(y < (ssize_t) matrix_info->rows ? MagickFalse : MagickTrue);
1068MagickExport
double **RelinquishMagickMatrix(
double **matrix,
1069 const size_t number_rows)
1074 if (matrix == (
double **) NULL )
1076 for (i=0; i < (ssize_t) number_rows; i++)
1077 matrix[i]=(
double *) RelinquishMagickMemory(matrix[i]);
1078 matrix=(
double **) RelinquishMagickMemory(matrix);
1112MagickExport MagickBooleanType SetMatrixElement(
const MatrixInfo *matrix_info,
1113 const ssize_t x,
const ssize_t y,
const void *value)
1119 assert(matrix_info != (
const MatrixInfo *) NULL);
1120 assert(matrix_info->signature == MagickCoreSignature);
1121 i=y*(MagickOffsetType) matrix_info->columns+x;
1123 (((MagickSizeType) i*matrix_info->stride) >= matrix_info->length))
1124 return(MagickFalse);
1125 if (matrix_info->type != DiskCache)
1127 (void) memcpy((
unsigned char *) matrix_info->elements+i*
1128 (MagickOffsetType) matrix_info->stride,value,matrix_info->stride);
1131 count=WriteMatrixElements(matrix_info,i*(MagickOffsetType)
1132 matrix_info->stride,matrix_info->stride,(
unsigned char *) value);
1133 if (count != (MagickOffsetType) matrix_info->stride)
1134 return(MagickFalse);