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