PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/MagickCore/fx.c

https://gitlab.com/ImageMagick/ImageMagick
C | 6107 lines | 4762 code | 359 blank | 986 comment | 1327 complexity | b20d1e013a3c94837285ae4f9eddfac3 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % %
  4. % %
  5. % %
  6. % FFFFF X X %
  7. % F X X %
  8. % FFF X %
  9. % F X X %
  10. % F X X %
  11. % %
  12. % %
  13. % MagickCore Image Special Effects Methods %
  14. % %
  15. % Software Design %
  16. % Cristy %
  17. % October 1996 %
  18. % %
  19. % %
  20. % %
  21. % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
  22. % dedicated to making software imaging solutions freely available. %
  23. % %
  24. % You may not use this file except in compliance with the License. You may %
  25. % obtain a copy of the License at %
  26. % %
  27. % https://imagemagick.org/script/license.php %
  28. % %
  29. % Unless required by applicable law or agreed to in writing, software %
  30. % distributed under the License is distributed on an "AS IS" BASIS, %
  31. % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
  32. % See the License for the specific language governing permissions and %
  33. % limitations under the License. %
  34. % %
  35. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  36. %
  37. %
  38. %
  39. */
  40. /*
  41. Include declarations.
  42. */
  43. #include "MagickCore/studio.h"
  44. #include "MagickCore/accelerate-private.h"
  45. #include "MagickCore/annotate.h"
  46. #include "MagickCore/artifact.h"
  47. #include "MagickCore/attribute.h"
  48. #include "MagickCore/cache.h"
  49. #include "MagickCore/cache-view.h"
  50. #include "MagickCore/channel.h"
  51. #include "MagickCore/color.h"
  52. #include "MagickCore/color-private.h"
  53. #include "MagickCore/colorspace-private.h"
  54. #include "MagickCore/composite.h"
  55. #include "MagickCore/decorate.h"
  56. #include "MagickCore/distort.h"
  57. #include "MagickCore/draw.h"
  58. #include "MagickCore/effect.h"
  59. #include "MagickCore/enhance.h"
  60. #include "MagickCore/exception.h"
  61. #include "MagickCore/exception-private.h"
  62. #include "MagickCore/fx.h"
  63. #include "MagickCore/fx-private.h"
  64. #include "MagickCore/gem.h"
  65. #include "MagickCore/gem-private.h"
  66. #include "MagickCore/geometry.h"
  67. #include "MagickCore/layer.h"
  68. #include "MagickCore/list.h"
  69. #include "MagickCore/log.h"
  70. #include "MagickCore/image.h"
  71. #include "MagickCore/image-private.h"
  72. #include "MagickCore/magick.h"
  73. #include "MagickCore/memory_.h"
  74. #include "MagickCore/memory-private.h"
  75. #include "MagickCore/monitor.h"
  76. #include "MagickCore/monitor-private.h"
  77. #include "MagickCore/option.h"
  78. #include "MagickCore/pixel.h"
  79. #include "MagickCore/pixel-accessor.h"
  80. #include "MagickCore/property.h"
  81. #include "MagickCore/quantum.h"
  82. #include "MagickCore/quantum-private.h"
  83. #include "MagickCore/random_.h"
  84. #include "MagickCore/random-private.h"
  85. #include "MagickCore/resample.h"
  86. #include "MagickCore/resample-private.h"
  87. #include "MagickCore/resize.h"
  88. #include "MagickCore/resource_.h"
  89. #include "MagickCore/splay-tree.h"
  90. #include "MagickCore/statistic.h"
  91. #include "MagickCore/string_.h"
  92. #include "MagickCore/string-private.h"
  93. #include "MagickCore/thread-private.h"
  94. #include "MagickCore/threshold.h"
  95. #include "MagickCore/transform.h"
  96. #include "MagickCore/transform-private.h"
  97. #include "MagickCore/utility.h"
  98. /*
  99. Define declarations.
  100. */
  101. #define LeftShiftOperator 0xf5U
  102. #define RightShiftOperator 0xf6U
  103. #define LessThanEqualOperator 0xf7U
  104. #define GreaterThanEqualOperator 0xf8U
  105. #define EqualOperator 0xf9U
  106. #define NotEqualOperator 0xfaU
  107. #define LogicalAndOperator 0xfbU
  108. #define LogicalOrOperator 0xfcU
  109. #define ExponentialNotation 0xfdU
  110. struct _FxInfo
  111. {
  112. const Image
  113. *images;
  114. char
  115. *expression;
  116. FILE
  117. *file;
  118. SplayTreeInfo
  119. *colors,
  120. *symbols;
  121. CacheView
  122. **view;
  123. RandomInfo
  124. *random_info;
  125. ExceptionInfo
  126. *exception;
  127. };
  128. /*
  129. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  130. % %
  131. % %
  132. % %
  133. + A c q u i r e F x I n f o %
  134. % %
  135. % %
  136. % %
  137. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  138. %
  139. % AcquireFxInfo() allocates the FxInfo structure.
  140. %
  141. % The format of the AcquireFxInfo method is:
  142. %
  143. % FxInfo *AcquireFxInfo(Image *images,const char *expression,
  144. % ExceptionInfo *exception)
  145. %
  146. % A description of each parameter follows:
  147. %
  148. % o images: the image sequence.
  149. %
  150. % o expression: the expression.
  151. %
  152. % o exception: return any errors or warnings in this structure.
  153. %
  154. */
  155. MagickPrivate FxInfo *AcquireFxInfo(const Image *images,const char *expression,
  156. ExceptionInfo *exception)
  157. {
  158. char
  159. fx_op[2];
  160. const Image
  161. *next;
  162. FxInfo
  163. *fx_info;
  164. register ssize_t
  165. i;
  166. fx_info=(FxInfo *) AcquireCriticalMemory(sizeof(*fx_info));
  167. (void) memset(fx_info,0,sizeof(*fx_info));
  168. fx_info->exception=AcquireExceptionInfo();
  169. fx_info->images=images;
  170. fx_info->colors=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
  171. RelinquishMagickMemory);
  172. fx_info->symbols=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
  173. RelinquishMagickMemory);
  174. fx_info->view=(CacheView **) AcquireQuantumMemory(GetImageListLength(
  175. fx_info->images),sizeof(*fx_info->view));
  176. if (fx_info->view == (CacheView **) NULL)
  177. ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
  178. i=0;
  179. next=GetFirstImageInList(fx_info->images);
  180. for ( ; next != (Image *) NULL; next=next->next)
  181. {
  182. fx_info->view[i]=AcquireVirtualCacheView(next,exception);
  183. i++;
  184. }
  185. fx_info->random_info=AcquireRandomInfo();
  186. fx_info->expression=ConstantString(expression);
  187. fx_info->file=stderr;
  188. (void) SubstituteString(&fx_info->expression," ",""); /* compact string */
  189. /*
  190. Force right-to-left associativity for unary negation.
  191. */
  192. (void) SubstituteString(&fx_info->expression,"-","-1.0*");
  193. (void) SubstituteString(&fx_info->expression,"^-1.0*","^-");
  194. (void) SubstituteString(&fx_info->expression,"E-1.0*","E-");
  195. (void) SubstituteString(&fx_info->expression,"e-1.0*","e-");
  196. /*
  197. Convert compound to simple operators.
  198. */
  199. fx_op[1]='\0';
  200. *fx_op=(char) LeftShiftOperator;
  201. (void) SubstituteString(&fx_info->expression,"<<",fx_op);
  202. *fx_op=(char) RightShiftOperator;
  203. (void) SubstituteString(&fx_info->expression,">>",fx_op);
  204. *fx_op=(char) LessThanEqualOperator;
  205. (void) SubstituteString(&fx_info->expression,"<=",fx_op);
  206. *fx_op=(char) GreaterThanEqualOperator;
  207. (void) SubstituteString(&fx_info->expression,">=",fx_op);
  208. *fx_op=(char) EqualOperator;
  209. (void) SubstituteString(&fx_info->expression,"==",fx_op);
  210. *fx_op=(char) NotEqualOperator;
  211. (void) SubstituteString(&fx_info->expression,"!=",fx_op);
  212. *fx_op=(char) LogicalAndOperator;
  213. (void) SubstituteString(&fx_info->expression,"&&",fx_op);
  214. *fx_op=(char) LogicalOrOperator;
  215. (void) SubstituteString(&fx_info->expression,"||",fx_op);
  216. *fx_op=(char) ExponentialNotation;
  217. (void) SubstituteString(&fx_info->expression,"**",fx_op);
  218. return(fx_info);
  219. }
  220. /*
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222. % %
  223. % %
  224. % %
  225. % A d d N o i s e I m a g e %
  226. % %
  227. % %
  228. % %
  229. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  230. %
  231. % AddNoiseImage() adds random noise to the image.
  232. %
  233. % The format of the AddNoiseImage method is:
  234. %
  235. % Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
  236. % const double attenuate,ExceptionInfo *exception)
  237. %
  238. % A description of each parameter follows:
  239. %
  240. % o image: the image.
  241. %
  242. % o channel: the channel type.
  243. %
  244. % o noise_type: The type of noise: Uniform, Gaussian, Multiplicative,
  245. % Impulse, Laplacian, or Poisson.
  246. %
  247. % o attenuate: attenuate the random distribution.
  248. %
  249. % o exception: return any errors or warnings in this structure.
  250. %
  251. */
  252. MagickExport Image *AddNoiseImage(const Image *image,const NoiseType noise_type,
  253. const double attenuate,ExceptionInfo *exception)
  254. {
  255. #define AddNoiseImageTag "AddNoise/Image"
  256. CacheView
  257. *image_view,
  258. *noise_view;
  259. Image
  260. *noise_image;
  261. MagickBooleanType
  262. status;
  263. MagickOffsetType
  264. progress;
  265. RandomInfo
  266. **magick_restrict random_info;
  267. ssize_t
  268. y;
  269. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  270. unsigned long
  271. key;
  272. #endif
  273. /*
  274. Initialize noise image attributes.
  275. */
  276. assert(image != (const Image *) NULL);
  277. assert(image->signature == MagickCoreSignature);
  278. if (image->debug != MagickFalse)
  279. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  280. assert(exception != (ExceptionInfo *) NULL);
  281. assert(exception->signature == MagickCoreSignature);
  282. #if defined(MAGICKCORE_OPENCL_SUPPORT)
  283. noise_image=AccelerateAddNoiseImage(image,noise_type,attenuate,exception);
  284. if (noise_image != (Image *) NULL)
  285. return(noise_image);
  286. #endif
  287. noise_image=CloneImage(image,0,0,MagickTrue,exception);
  288. if (noise_image == (Image *) NULL)
  289. return((Image *) NULL);
  290. if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
  291. {
  292. noise_image=DestroyImage(noise_image);
  293. return((Image *) NULL);
  294. }
  295. /*
  296. Add noise in each row.
  297. */
  298. status=MagickTrue;
  299. progress=0;
  300. random_info=AcquireRandomInfoThreadSet();
  301. image_view=AcquireVirtualCacheView(image,exception);
  302. noise_view=AcquireAuthenticCacheView(noise_image,exception);
  303. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  304. key=GetRandomSecretKey(random_info[0]);
  305. #pragma omp parallel for schedule(static) shared(progress,status) \
  306. magick_number_threads(image,noise_image,image->rows,key == ~0UL)
  307. #endif
  308. for (y=0; y < (ssize_t) image->rows; y++)
  309. {
  310. const int
  311. id = GetOpenMPThreadId();
  312. MagickBooleanType
  313. sync;
  314. register const Quantum
  315. *magick_restrict p;
  316. register ssize_t
  317. x;
  318. register Quantum
  319. *magick_restrict q;
  320. if (status == MagickFalse)
  321. continue;
  322. p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
  323. q=QueueCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
  324. exception);
  325. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  326. {
  327. status=MagickFalse;
  328. continue;
  329. }
  330. for (x=0; x < (ssize_t) image->columns; x++)
  331. {
  332. register ssize_t
  333. i;
  334. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  335. {
  336. PixelChannel channel = GetPixelChannelChannel(image,i);
  337. PixelTrait traits = GetPixelChannelTraits(image,channel);
  338. PixelTrait noise_traits=GetPixelChannelTraits(noise_image,channel);
  339. if ((traits == UndefinedPixelTrait) ||
  340. (noise_traits == UndefinedPixelTrait))
  341. continue;
  342. if ((noise_traits & CopyPixelTrait) != 0)
  343. {
  344. SetPixelChannel(noise_image,channel,p[i],q);
  345. continue;
  346. }
  347. SetPixelChannel(noise_image,channel,ClampToQuantum(
  348. GenerateDifferentialNoise(random_info[id],p[i],noise_type,attenuate)),
  349. q);
  350. }
  351. p+=GetPixelChannels(image);
  352. q+=GetPixelChannels(noise_image);
  353. }
  354. sync=SyncCacheViewAuthenticPixels(noise_view,exception);
  355. if (sync == MagickFalse)
  356. status=MagickFalse;
  357. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  358. {
  359. MagickBooleanType
  360. proceed;
  361. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  362. #pragma omp atomic
  363. #endif
  364. progress++;
  365. proceed=SetImageProgress(image,AddNoiseImageTag,progress,image->rows);
  366. if (proceed == MagickFalse)
  367. status=MagickFalse;
  368. }
  369. }
  370. noise_view=DestroyCacheView(noise_view);
  371. image_view=DestroyCacheView(image_view);
  372. random_info=DestroyRandomInfoThreadSet(random_info);
  373. if (status == MagickFalse)
  374. noise_image=DestroyImage(noise_image);
  375. return(noise_image);
  376. }
  377. /*
  378. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  379. % %
  380. % %
  381. % %
  382. % B l u e S h i f t I m a g e %
  383. % %
  384. % %
  385. % %
  386. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  387. %
  388. % BlueShiftImage() mutes the colors of the image to simulate a scene at
  389. % nighttime in the moonlight.
  390. %
  391. % The format of the BlueShiftImage method is:
  392. %
  393. % Image *BlueShiftImage(const Image *image,const double factor,
  394. % ExceptionInfo *exception)
  395. %
  396. % A description of each parameter follows:
  397. %
  398. % o image: the image.
  399. %
  400. % o factor: the shift factor.
  401. %
  402. % o exception: return any errors or warnings in this structure.
  403. %
  404. */
  405. MagickExport Image *BlueShiftImage(const Image *image,const double factor,
  406. ExceptionInfo *exception)
  407. {
  408. #define BlueShiftImageTag "BlueShift/Image"
  409. CacheView
  410. *image_view,
  411. *shift_view;
  412. Image
  413. *shift_image;
  414. MagickBooleanType
  415. status;
  416. MagickOffsetType
  417. progress;
  418. ssize_t
  419. y;
  420. /*
  421. Allocate blue shift image.
  422. */
  423. assert(image != (const Image *) NULL);
  424. assert(image->signature == MagickCoreSignature);
  425. if (image->debug != MagickFalse)
  426. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  427. assert(exception != (ExceptionInfo *) NULL);
  428. assert(exception->signature == MagickCoreSignature);
  429. shift_image=CloneImage(image,0,0,MagickTrue,exception);
  430. if (shift_image == (Image *) NULL)
  431. return((Image *) NULL);
  432. if (SetImageStorageClass(shift_image,DirectClass,exception) == MagickFalse)
  433. {
  434. shift_image=DestroyImage(shift_image);
  435. return((Image *) NULL);
  436. }
  437. /*
  438. Blue-shift DirectClass image.
  439. */
  440. status=MagickTrue;
  441. progress=0;
  442. image_view=AcquireVirtualCacheView(image,exception);
  443. shift_view=AcquireAuthenticCacheView(shift_image,exception);
  444. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  445. #pragma omp parallel for schedule(static) shared(progress,status) \
  446. magick_number_threads(image,shift_image,image->rows,1)
  447. #endif
  448. for (y=0; y < (ssize_t) image->rows; y++)
  449. {
  450. MagickBooleanType
  451. sync;
  452. PixelInfo
  453. pixel;
  454. Quantum
  455. quantum;
  456. register const Quantum
  457. *magick_restrict p;
  458. register ssize_t
  459. x;
  460. register Quantum
  461. *magick_restrict q;
  462. if (status == MagickFalse)
  463. continue;
  464. p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
  465. q=QueueCacheViewAuthenticPixels(shift_view,0,y,shift_image->columns,1,
  466. exception);
  467. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  468. {
  469. status=MagickFalse;
  470. continue;
  471. }
  472. for (x=0; x < (ssize_t) image->columns; x++)
  473. {
  474. quantum=GetPixelRed(image,p);
  475. if (GetPixelGreen(image,p) < quantum)
  476. quantum=GetPixelGreen(image,p);
  477. if (GetPixelBlue(image,p) < quantum)
  478. quantum=GetPixelBlue(image,p);
  479. pixel.red=0.5*(GetPixelRed(image,p)+factor*quantum);
  480. pixel.green=0.5*(GetPixelGreen(image,p)+factor*quantum);
  481. pixel.blue=0.5*(GetPixelBlue(image,p)+factor*quantum);
  482. quantum=GetPixelRed(image,p);
  483. if (GetPixelGreen(image,p) > quantum)
  484. quantum=GetPixelGreen(image,p);
  485. if (GetPixelBlue(image,p) > quantum)
  486. quantum=GetPixelBlue(image,p);
  487. pixel.red=0.5*(pixel.red+factor*quantum);
  488. pixel.green=0.5*(pixel.green+factor*quantum);
  489. pixel.blue=0.5*(pixel.blue+factor*quantum);
  490. SetPixelRed(shift_image,ClampToQuantum(pixel.red),q);
  491. SetPixelGreen(shift_image,ClampToQuantum(pixel.green),q);
  492. SetPixelBlue(shift_image,ClampToQuantum(pixel.blue),q);
  493. p+=GetPixelChannels(image);
  494. q+=GetPixelChannels(shift_image);
  495. }
  496. sync=SyncCacheViewAuthenticPixels(shift_view,exception);
  497. if (sync == MagickFalse)
  498. status=MagickFalse;
  499. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  500. {
  501. MagickBooleanType
  502. proceed;
  503. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  504. #pragma omp atomic
  505. #endif
  506. progress++;
  507. proceed=SetImageProgress(image,BlueShiftImageTag,progress,image->rows);
  508. if (proceed == MagickFalse)
  509. status=MagickFalse;
  510. }
  511. }
  512. image_view=DestroyCacheView(image_view);
  513. shift_view=DestroyCacheView(shift_view);
  514. if (status == MagickFalse)
  515. shift_image=DestroyImage(shift_image);
  516. return(shift_image);
  517. }
  518. /*
  519. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  520. % %
  521. % %
  522. % %
  523. % C h a r c o a l I m a g e %
  524. % %
  525. % %
  526. % %
  527. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  528. %
  529. % CharcoalImage() creates a new image that is a copy of an existing one with
  530. % the edge highlighted. It allocates the memory necessary for the new Image
  531. % structure and returns a pointer to the new image.
  532. %
  533. % The format of the CharcoalImage method is:
  534. %
  535. % Image *CharcoalImage(const Image *image,const double radius,
  536. % const double sigma,ExceptionInfo *exception)
  537. %
  538. % A description of each parameter follows:
  539. %
  540. % o image: the image.
  541. %
  542. % o radius: the radius of the pixel neighborhood.
  543. %
  544. % o sigma: the standard deviation of the Gaussian, in pixels.
  545. %
  546. % o exception: return any errors or warnings in this structure.
  547. %
  548. */
  549. MagickExport Image *CharcoalImage(const Image *image,const double radius,
  550. const double sigma,ExceptionInfo *exception)
  551. {
  552. Image
  553. *charcoal_image,
  554. *edge_image;
  555. MagickBooleanType
  556. status;
  557. assert(image != (Image *) NULL);
  558. assert(image->signature == MagickCoreSignature);
  559. if (image->debug != MagickFalse)
  560. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  561. assert(exception != (ExceptionInfo *) NULL);
  562. assert(exception->signature == MagickCoreSignature);
  563. edge_image=EdgeImage(image,radius,exception);
  564. if (edge_image == (Image *) NULL)
  565. return((Image *) NULL);
  566. charcoal_image=(Image *) NULL;
  567. status=ClampImage(edge_image,exception);
  568. if (status != MagickFalse)
  569. charcoal_image=BlurImage(edge_image,radius,sigma,exception);
  570. edge_image=DestroyImage(edge_image);
  571. if (charcoal_image == (Image *) NULL)
  572. return((Image *) NULL);
  573. status=NormalizeImage(charcoal_image,exception);
  574. if (status != MagickFalse)
  575. status=NegateImage(charcoal_image,MagickFalse,exception);
  576. if (status != MagickFalse)
  577. status=GrayscaleImage(charcoal_image,image->intensity,exception);
  578. if (status == MagickFalse)
  579. charcoal_image=DestroyImage(charcoal_image);
  580. return(charcoal_image);
  581. }
  582. /*
  583. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  584. % %
  585. % %
  586. % %
  587. % C o l o r i z e I m a g e %
  588. % %
  589. % %
  590. % %
  591. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  592. %
  593. % ColorizeImage() blends the fill color with each pixel in the image.
  594. % A percentage blend is specified with opacity. Control the application
  595. % of different color components by specifying a different percentage for
  596. % each component (e.g. 90/100/10 is 90% red, 100% green, and 10% blue).
  597. %
  598. % The format of the ColorizeImage method is:
  599. %
  600. % Image *ColorizeImage(const Image *image,const char *blend,
  601. % const PixelInfo *colorize,ExceptionInfo *exception)
  602. %
  603. % A description of each parameter follows:
  604. %
  605. % o image: the image.
  606. %
  607. % o blend: A character string indicating the level of blending as a
  608. % percentage.
  609. %
  610. % o colorize: A color value.
  611. %
  612. % o exception: return any errors or warnings in this structure.
  613. %
  614. */
  615. MagickExport Image *ColorizeImage(const Image *image,const char *blend,
  616. const PixelInfo *colorize,ExceptionInfo *exception)
  617. {
  618. #define ColorizeImageTag "Colorize/Image"
  619. #define Colorize(pixel,blend_percentage,colorize) \
  620. (((pixel)*(100.0-(blend_percentage))+(colorize)*(blend_percentage))/100.0)
  621. CacheView
  622. *image_view;
  623. GeometryInfo
  624. geometry_info;
  625. Image
  626. *colorize_image;
  627. MagickBooleanType
  628. status;
  629. MagickOffsetType
  630. progress;
  631. MagickStatusType
  632. flags;
  633. PixelInfo
  634. blend_percentage;
  635. ssize_t
  636. y;
  637. /*
  638. Allocate colorized image.
  639. */
  640. assert(image != (const Image *) NULL);
  641. assert(image->signature == MagickCoreSignature);
  642. if (image->debug != MagickFalse)
  643. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  644. assert(exception != (ExceptionInfo *) NULL);
  645. assert(exception->signature == MagickCoreSignature);
  646. colorize_image=CloneImage(image,0,0,MagickTrue,exception);
  647. if (colorize_image == (Image *) NULL)
  648. return((Image *) NULL);
  649. if (SetImageStorageClass(colorize_image,DirectClass,exception) == MagickFalse)
  650. {
  651. colorize_image=DestroyImage(colorize_image);
  652. return((Image *) NULL);
  653. }
  654. if ((IsGrayColorspace(colorize_image->colorspace) != MagickFalse) ||
  655. (IsPixelInfoGray(colorize) != MagickFalse))
  656. (void) SetImageColorspace(colorize_image,sRGBColorspace,exception);
  657. if ((colorize_image->alpha_trait == UndefinedPixelTrait) &&
  658. (colorize->alpha_trait != UndefinedPixelTrait))
  659. (void) SetImageAlpha(colorize_image,OpaqueAlpha,exception);
  660. if (blend == (const char *) NULL)
  661. return(colorize_image);
  662. GetPixelInfo(colorize_image,&blend_percentage);
  663. flags=ParseGeometry(blend,&geometry_info);
  664. blend_percentage.red=geometry_info.rho;
  665. blend_percentage.green=geometry_info.rho;
  666. blend_percentage.blue=geometry_info.rho;
  667. blend_percentage.black=geometry_info.rho;
  668. blend_percentage.alpha=(MagickRealType) TransparentAlpha;
  669. if ((flags & SigmaValue) != 0)
  670. blend_percentage.green=geometry_info.sigma;
  671. if ((flags & XiValue) != 0)
  672. blend_percentage.blue=geometry_info.xi;
  673. if ((flags & PsiValue) != 0)
  674. blend_percentage.alpha=geometry_info.psi;
  675. if (blend_percentage.colorspace == CMYKColorspace)
  676. {
  677. if ((flags & PsiValue) != 0)
  678. blend_percentage.black=geometry_info.psi;
  679. if ((flags & ChiValue) != 0)
  680. blend_percentage.alpha=geometry_info.chi;
  681. }
  682. /*
  683. Colorize DirectClass image.
  684. */
  685. status=MagickTrue;
  686. progress=0;
  687. image_view=AcquireVirtualCacheView(colorize_image,exception);
  688. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  689. #pragma omp parallel for schedule(static) shared(progress,status) \
  690. magick_number_threads(colorize_image,colorize_image,colorize_image->rows,1)
  691. #endif
  692. for (y=0; y < (ssize_t) colorize_image->rows; y++)
  693. {
  694. MagickBooleanType
  695. sync;
  696. register Quantum
  697. *magick_restrict q;
  698. register ssize_t
  699. x;
  700. if (status == MagickFalse)
  701. continue;
  702. q=GetCacheViewAuthenticPixels(image_view,0,y,colorize_image->columns,1,
  703. exception);
  704. if (q == (Quantum *) NULL)
  705. {
  706. status=MagickFalse;
  707. continue;
  708. }
  709. for (x=0; x < (ssize_t) colorize_image->columns; x++)
  710. {
  711. register ssize_t
  712. i;
  713. for (i=0; i < (ssize_t) GetPixelChannels(colorize_image); i++)
  714. {
  715. PixelTrait traits = GetPixelChannelTraits(colorize_image,
  716. (PixelChannel) i);
  717. if (traits == UndefinedPixelTrait)
  718. continue;
  719. if ((traits & CopyPixelTrait) != 0)
  720. continue;
  721. SetPixelChannel(colorize_image,(PixelChannel) i,ClampToQuantum(
  722. Colorize(q[i],GetPixelInfoChannel(&blend_percentage,(PixelChannel) i),
  723. GetPixelInfoChannel(colorize,(PixelChannel) i))),q);
  724. }
  725. q+=GetPixelChannels(colorize_image);
  726. }
  727. sync=SyncCacheViewAuthenticPixels(image_view,exception);
  728. if (sync == MagickFalse)
  729. status=MagickFalse;
  730. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  731. {
  732. MagickBooleanType
  733. proceed;
  734. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  735. #pragma omp atomic
  736. #endif
  737. progress++;
  738. proceed=SetImageProgress(image,ColorizeImageTag,progress,
  739. colorize_image->rows);
  740. if (proceed == MagickFalse)
  741. status=MagickFalse;
  742. }
  743. }
  744. image_view=DestroyCacheView(image_view);
  745. if (status == MagickFalse)
  746. colorize_image=DestroyImage(colorize_image);
  747. return(colorize_image);
  748. }
  749. /*
  750. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  751. % %
  752. % %
  753. % %
  754. % C o l o r M a t r i x I m a g e %
  755. % %
  756. % %
  757. % %
  758. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  759. %
  760. % ColorMatrixImage() applies color transformation to an image. This method
  761. % permits saturation changes, hue rotation, luminance to alpha, and various
  762. % other effects. Although variable-sized transformation matrices can be used,
  763. % typically one uses a 5x5 matrix for an RGBA image and a 6x6 for CMYKA
  764. % (or RGBA with offsets). The matrix is similar to those used by Adobe Flash
  765. % except offsets are in column 6 rather than 5 (in support of CMYKA images)
  766. % and offsets are normalized (divide Flash offset by 255).
  767. %
  768. % The format of the ColorMatrixImage method is:
  769. %
  770. % Image *ColorMatrixImage(const Image *image,
  771. % const KernelInfo *color_matrix,ExceptionInfo *exception)
  772. %
  773. % A description of each parameter follows:
  774. %
  775. % o image: the image.
  776. %
  777. % o color_matrix: the color matrix.
  778. %
  779. % o exception: return any errors or warnings in this structure.
  780. %
  781. */
  782. /* FUTURE: modify to make use of a MagickMatrix Mutliply function
  783. That should be provided in "matrix.c"
  784. (ASIDE: actually distorts should do this too but currently doesn't)
  785. */
  786. MagickExport Image *ColorMatrixImage(const Image *image,
  787. const KernelInfo *color_matrix,ExceptionInfo *exception)
  788. {
  789. #define ColorMatrixImageTag "ColorMatrix/Image"
  790. CacheView
  791. *color_view,
  792. *image_view;
  793. double
  794. ColorMatrix[6][6] =
  795. {
  796. { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
  797. { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 },
  798. { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 },
  799. { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 },
  800. { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 },
  801. { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }
  802. };
  803. Image
  804. *color_image;
  805. MagickBooleanType
  806. status;
  807. MagickOffsetType
  808. progress;
  809. register ssize_t
  810. i;
  811. ssize_t
  812. u,
  813. v,
  814. y;
  815. /*
  816. Map given color_matrix, into a 6x6 matrix RGBKA and a constant
  817. */
  818. assert(image != (Image *) NULL);
  819. assert(image->signature == MagickCoreSignature);
  820. if (image->debug != MagickFalse)
  821. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  822. assert(exception != (ExceptionInfo *) NULL);
  823. assert(exception->signature == MagickCoreSignature);
  824. i=0;
  825. for (v=0; v < (ssize_t) color_matrix->height; v++)
  826. for (u=0; u < (ssize_t) color_matrix->width; u++)
  827. {
  828. if ((v < 6) && (u < 6))
  829. ColorMatrix[v][u]=color_matrix->values[i];
  830. i++;
  831. }
  832. /*
  833. Initialize color image.
  834. */
  835. color_image=CloneImage(image,0,0,MagickTrue,exception);
  836. if (color_image == (Image *) NULL)
  837. return((Image *) NULL);
  838. if (SetImageStorageClass(color_image,DirectClass,exception) == MagickFalse)
  839. {
  840. color_image=DestroyImage(color_image);
  841. return((Image *) NULL);
  842. }
  843. if (image->debug != MagickFalse)
  844. {
  845. char
  846. format[MagickPathExtent],
  847. *message;
  848. (void) LogMagickEvent(TransformEvent,GetMagickModule(),
  849. " ColorMatrix image with color matrix:");
  850. message=AcquireString("");
  851. for (v=0; v < 6; v++)
  852. {
  853. *message='\0';
  854. (void) FormatLocaleString(format,MagickPathExtent,"%.20g: ",(double) v);
  855. (void) ConcatenateString(&message,format);
  856. for (u=0; u < 6; u++)
  857. {
  858. (void) FormatLocaleString(format,MagickPathExtent,"%+f ",
  859. ColorMatrix[v][u]);
  860. (void) ConcatenateString(&message,format);
  861. }
  862. (void) LogMagickEvent(TransformEvent,GetMagickModule(),"%s",message);
  863. }
  864. message=DestroyString(message);
  865. }
  866. /*
  867. Apply the ColorMatrix to image.
  868. */
  869. status=MagickTrue;
  870. progress=0;
  871. image_view=AcquireVirtualCacheView(image,exception);
  872. color_view=AcquireAuthenticCacheView(color_image,exception);
  873. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  874. #pragma omp parallel for schedule(static) shared(progress,status) \
  875. magick_number_threads(image,color_image,image->rows,1)
  876. #endif
  877. for (y=0; y < (ssize_t) image->rows; y++)
  878. {
  879. PixelInfo
  880. pixel;
  881. register const Quantum
  882. *magick_restrict p;
  883. register Quantum
  884. *magick_restrict q;
  885. register ssize_t
  886. x;
  887. if (status == MagickFalse)
  888. continue;
  889. p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
  890. q=GetCacheViewAuthenticPixels(color_view,0,y,color_image->columns,1,
  891. exception);
  892. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  893. {
  894. status=MagickFalse;
  895. continue;
  896. }
  897. GetPixelInfo(image,&pixel);
  898. for (x=0; x < (ssize_t) image->columns; x++)
  899. {
  900. register ssize_t
  901. v;
  902. size_t
  903. height;
  904. GetPixelInfoPixel(image,p,&pixel);
  905. height=color_matrix->height > 6 ? 6UL : color_matrix->height;
  906. for (v=0; v < (ssize_t) height; v++)
  907. {
  908. double
  909. sum;
  910. sum=ColorMatrix[v][0]*GetPixelRed(image,p)+ColorMatrix[v][1]*
  911. GetPixelGreen(image,p)+ColorMatrix[v][2]*GetPixelBlue(image,p);
  912. if (image->colorspace == CMYKColorspace)
  913. sum+=ColorMatrix[v][3]*GetPixelBlack(image,p);
  914. if (image->alpha_trait != UndefinedPixelTrait)
  915. sum+=ColorMatrix[v][4]*GetPixelAlpha(image,p);
  916. sum+=QuantumRange*ColorMatrix[v][5];
  917. switch (v)
  918. {
  919. case 0: pixel.red=sum; break;
  920. case 1: pixel.green=sum; break;
  921. case 2: pixel.blue=sum; break;
  922. case 3: pixel.black=sum; break;
  923. case 4: pixel.alpha=sum; break;
  924. default: break;
  925. }
  926. }
  927. SetPixelViaPixelInfo(color_image,&pixel,q);
  928. p+=GetPixelChannels(image);
  929. q+=GetPixelChannels(color_image);
  930. }
  931. if (SyncCacheViewAuthenticPixels(color_view,exception) == MagickFalse)
  932. status=MagickFalse;
  933. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  934. {
  935. MagickBooleanType
  936. proceed;
  937. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  938. #pragma omp atomic
  939. #endif
  940. progress++;
  941. proceed=SetImageProgress(image,ColorMatrixImageTag,progress,
  942. image->rows);
  943. if (proceed == MagickFalse)
  944. status=MagickFalse;
  945. }
  946. }
  947. color_view=DestroyCacheView(color_view);
  948. image_view=DestroyCacheView(image_view);
  949. if (status == MagickFalse)
  950. color_image=DestroyImage(color_image);
  951. return(color_image);
  952. }
  953. /*
  954. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  955. % %
  956. % %
  957. % %
  958. + D e s t r o y F x I n f o %
  959. % %
  960. % %
  961. % %
  962. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  963. %
  964. % DestroyFxInfo() deallocates memory associated with an FxInfo structure.
  965. %
  966. % The format of the DestroyFxInfo method is:
  967. %
  968. % ImageInfo *DestroyFxInfo(ImageInfo *fx_info)
  969. %
  970. % A description of each parameter follows:
  971. %
  972. % o fx_info: the fx info.
  973. %
  974. */
  975. MagickPrivate FxInfo *DestroyFxInfo(FxInfo *fx_info)
  976. {
  977. register ssize_t
  978. i;
  979. fx_info->exception=DestroyExceptionInfo(fx_info->exception);
  980. fx_info->expression=DestroyString(fx_info->expression);
  981. fx_info->symbols=DestroySplayTree(fx_info->symbols);
  982. fx_info->colors=DestroySplayTree(fx_info->colors);
  983. for (i=(ssize_t) GetImageListLength(fx_info->images)-1; i >= 0; i--)
  984. fx_info->view[i]=DestroyCacheView(fx_info->view[i]);
  985. fx_info->view=(CacheView **) RelinquishMagickMemory(fx_info->view);
  986. fx_info->random_info=DestroyRandomInfo(fx_info->random_info);
  987. fx_info=(FxInfo *) RelinquishMagickMemory(fx_info);
  988. return(fx_info);
  989. }
  990. /*
  991. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  992. % %
  993. % %
  994. % %
  995. + F x E v a l u a t e C h a n n e l E x p r e s s i o n %
  996. % %
  997. % %
  998. % %
  999. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1000. %
  1001. % FxEvaluateChannelExpression() evaluates an expression and returns the
  1002. % results.
  1003. %
  1004. % The format of the FxEvaluateExpression method is:
  1005. %
  1006. % double FxEvaluateChannelExpression(FxInfo *fx_info,
  1007. % const PixelChannel channel,const ssize_t x,const ssize_t y,
  1008. % double *alpha,Exceptioninfo *exception)
  1009. % double FxEvaluateExpression(FxInfo *fx_info,
  1010. % double *alpha,Exceptioninfo *exception)
  1011. %
  1012. % A description of each parameter follows:
  1013. %
  1014. % o fx_info: the fx info.
  1015. %
  1016. % o channel: the channel.
  1017. %
  1018. % o x,y: the pixel position.
  1019. %
  1020. % o alpha: the result.
  1021. %
  1022. % o exception: return any errors or warnings in this structure.
  1023. %
  1024. */
  1025. static double FxChannelStatistics(FxInfo *fx_info,Image *image,
  1026. PixelChannel channel,const char *symbol,ExceptionInfo *exception)
  1027. {
  1028. ChannelType
  1029. channel_mask;
  1030. char
  1031. key[MagickPathExtent],
  1032. statistic[MagickPathExtent];
  1033. const char
  1034. *value;
  1035. register const char
  1036. *p;
  1037. channel_mask=UndefinedChannel;
  1038. for (p=symbol; (*p != '.') && (*p != '\0'); p++) ;
  1039. if (*p == '.')
  1040. {
  1041. ssize_t
  1042. option;
  1043. option=ParseCommandOption(MagickPixelChannelOptions,MagickTrue,p+1);
  1044. if (option >= 0)
  1045. {
  1046. channel=(PixelChannel) option;
  1047. channel_mask=SetPixelChannelMask(image,(ChannelType)
  1048. (1UL << channel));
  1049. }
  1050. }
  1051. (void) FormatLocaleString(key,MagickPathExtent,"%p.%.20g.%s",(void *) image,
  1052. (double) channel,symbol);
  1053. value=(const char *) GetValueFromSplayTree(fx_info->symbols,key);
  1054. if (value != (const char *) NULL)
  1055. {
  1056. if (channel_mask != UndefinedChannel)
  1057. (void) SetPixelChannelMask(image,channel_mask);
  1058. return(QuantumScale*StringToDouble(value,(char **) NULL));
  1059. }
  1060. (void) DeleteNodeFromSplayTree(fx_info->symbols,key);
  1061. if (LocaleNCompare(symbol,"depth",5) == 0)
  1062. {
  1063. size_t
  1064. depth;
  1065. depth=GetImageDepth(image,exception);
  1066. (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",(double)
  1067. depth);
  1068. }
  1069. if (LocaleNCompare(symbol,"kurtosis",8) == 0)
  1070. {
  1071. double
  1072. kurtosis,
  1073. skewness;
  1074. (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
  1075. (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",kurtosis);
  1076. }
  1077. if (LocaleNCompare(symbol,"maxima",6) == 0)
  1078. {
  1079. double
  1080. maxima,
  1081. minima;
  1082. (void) GetImageRange(image,&minima,&maxima,exception);
  1083. (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",maxima);
  1084. }
  1085. if (LocaleNCompare(symbol,"mean",4) == 0)
  1086. {
  1087. double
  1088. mean,
  1089. standard_deviation;
  1090. (void) GetImageMean(image,&mean,&standard_deviation,exception);
  1091. (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",mean);
  1092. }
  1093. if (LocaleNCompare(symbol,"minima",6) == 0)
  1094. {
  1095. double
  1096. maxima,
  1097. minima;
  1098. (void) GetImageRange(image,&minima,&maxima,exception);
  1099. (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",minima);
  1100. }
  1101. if (LocaleNCompare(symbol,"skewness",8) == 0)
  1102. {
  1103. double
  1104. kurtosis,
  1105. skewness;
  1106. (void) GetImageKurtosis(image,&kurtosis,&skewness,exception);
  1107. (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",skewness);
  1108. }
  1109. if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
  1110. {
  1111. double
  1112. mean,
  1113. standard_deviation;
  1114. (void) GetImageMean(image,&mean,&standard_deviation,exception);
  1115. (void) FormatLocaleString(statistic,MagickPathExtent,"%.20g",
  1116. standard_deviation);
  1117. }
  1118. if (channel_mask != UndefinedChannel)
  1119. (void) SetPixelChannelMask(image,channel_mask);
  1120. (void) AddValueToSplayTree(fx_info->symbols,ConstantString(key),
  1121. ConstantString(statistic));
  1122. return(QuantumScale*StringToDouble(statistic,(char **) NULL));
  1123. }
  1124. static double
  1125. FxEvaluateSubexpression(FxInfo *,const PixelChannel,const ssize_t,
  1126. const ssize_t,const char *,const size_t,double *,ExceptionInfo *);
  1127. static MagickOffsetType FxGCD(MagickOffsetType alpha,MagickOffsetType beta)
  1128. {
  1129. if (beta != 0)
  1130. return(FxGCD(beta,alpha % beta));
  1131. return(alpha);
  1132. }
  1133. static inline const char *FxSubexpression(const char *expression,
  1134. ExceptionInfo *exception)
  1135. {
  1136. const char
  1137. *subexpression;
  1138. register ssize_t
  1139. level;
  1140. level=0;
  1141. subexpression=expression;
  1142. while ((*subexpression != '\0') &&
  1143. ((level != 1) || (strchr(")",(int) *subexpression) == (char *) NULL)))
  1144. {
  1145. if (strchr("(",(int) *subexpression) != (char *) NULL)
  1146. level++;
  1147. else
  1148. if (strchr(")",(int) *subexpression) != (char *) NULL)
  1149. level--;
  1150. subexpression++;
  1151. }
  1152. if (*subexpression == '\0')
  1153. (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
  1154. "UnbalancedParenthesis","`%s'",expression);
  1155. return(subexpression);
  1156. }
  1157. static double FxGetSymbol(FxInfo *fx_info,const PixelChannel channel,
  1158. const ssize_t x,const ssize_t y,const char *expression,const size_t depth,
  1159. ExceptionInfo *exception)
  1160. {
  1161. char
  1162. *q,
  1163. symbol[MagickPathExtent];
  1164. const char
  1165. *p,
  1166. *value;
  1167. Image
  1168. *image;
  1169. MagickBooleanType
  1170. status;
  1171. PixelInfo
  1172. pixel;
  1173. double
  1174. alpha,
  1175. beta;
  1176. PointInfo
  1177. point;
  1178. register ssize_t
  1179. i;
  1180. size_t
  1181. level;
  1182. p=expression;
  1183. i=GetImageIndexInList(fx_info->images);
  1184. level=0;
  1185. point.x=(double) x;
  1186. point.y=(double) y;
  1187. if (isalpha((int) ((unsigned char) *(p+1))) == 0)
  1188. {
  1189. char
  1190. *subexpression;
  1191. subexpression=AcquireString(expression);
  1192. if (strchr("suv",(int) *p) != (char *) NULL)
  1193. {
  1194. switch (*p)
  1195. {
  1196. case 's':
  1197. default:
  1198. {
  1199. i=GetImageIndexInList(fx_info->images);
  1200. break;
  1201. }
  1202. case 'u': i=0; break;
  1203. case 'v': i=1; break;
  1204. }
  1205. p++;
  1206. if (*p == '[')
  1207. {
  1208. level++;
  1209. q=subexpression;
  1210. for (p++; *p != '\0'; )
  1211. {
  1212. if (*p == '[')
  1213. level++;
  1214. else
  1215. if (*p == ']')
  1216. {
  1217. level--;
  1218. if (level == 0)
  1219. break;
  1220. }
  1221. *q++=(*p++);
  1222. }
  1223. *q='\0';
  1224. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
  1225. depth,&beta,exception);
  1226. i=(ssize_t) alpha;
  1227. if (*p != '\0')
  1228. p++;
  1229. }
  1230. if (*p == '.')
  1231. p++;
  1232. }
  1233. if ((*p == 'p') && (isalpha((int) ((unsigned char) *(p+1))) == 0))
  1234. {
  1235. p++;
  1236. if (*p == '{')
  1237. {
  1238. level++;
  1239. q=subexpression;
  1240. for (p++; *p != '\0'; )
  1241. {
  1242. if (*p == '{')
  1243. level++;
  1244. else
  1245. if (*p == '}')
  1246. {
  1247. level--;
  1248. if (level == 0)
  1249. break;
  1250. }
  1251. *q++=(*p++);
  1252. }
  1253. *q='\0';
  1254. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
  1255. depth,&beta,exception);
  1256. point.x=alpha;
  1257. point.y=beta;
  1258. if (*p != '\0')
  1259. p++;
  1260. }
  1261. else
  1262. if (*p == '[')
  1263. {
  1264. level++;
  1265. q=subexpression;
  1266. for (p++; *p != '\0'; )
  1267. {
  1268. if (*p == '[')
  1269. level++;
  1270. else
  1271. if (*p == ']')
  1272. {
  1273. level--;
  1274. if (level == 0)
  1275. break;
  1276. }
  1277. *q++=(*p++);
  1278. }
  1279. *q='\0';
  1280. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,
  1281. depth,&beta,exception);
  1282. point.x+=alpha;
  1283. point.y+=beta;
  1284. if (*p != '\0')
  1285. p++;
  1286. }
  1287. if (*p == '.')
  1288. p++;
  1289. }
  1290. subexpression=DestroyString(subexpression);
  1291. }
  1292. image=GetImageFromList(fx_info->images,i);
  1293. if (image == (Image *) NULL)
  1294. {
  1295. (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
  1296. "NoSuchImage","`%s'",expression);
  1297. return(0.0);
  1298. }
  1299. i=GetImageIndexInList(image);
  1300. GetPixelInfo(image,&pixel);
  1301. status=InterpolatePixelInfo(image,fx_info->view[i],image->interpolate,
  1302. point.x,point.y,&pixel,exception);
  1303. (void) status;
  1304. if ((strlen(p) > 2) && (LocaleCompare(p,"intensity") != 0) &&
  1305. (LocaleCompare(p,"luma") != 0) && (LocaleCompare(p,"luminance") != 0) &&
  1306. (LocaleCompare(p,"hue") != 0) && (LocaleCompare(p,"saturation") != 0) &&
  1307. (LocaleCompare(p,"lightness") != 0))
  1308. {
  1309. char
  1310. name[MagickPathExtent];
  1311. (void) CopyMagickString(name,p,MagickPathExtent);
  1312. for (q=name+(strlen(name)-1); q > name; q--)
  1313. {
  1314. if (*q == ')')
  1315. break;
  1316. if (*q == '.')
  1317. {
  1318. *q='\0';
  1319. break;
  1320. }
  1321. }
  1322. if ((strlen(name) > 2) &&
  1323. (GetValueFromSplayTree(fx_info->symbols,name) == (const char *) NULL))
  1324. {
  1325. PixelInfo
  1326. *color;
  1327. color=(PixelInfo *) GetValueFromSplayTree(fx_info->colors,name);
  1328. if (color != (PixelInfo *) NULL)
  1329. {
  1330. pixel=(*color);
  1331. p+=strlen(name);
  1332. }
  1333. else
  1334. {
  1335. MagickBooleanType
  1336. status;
  1337. status=QueryColorCompliance(name,AllCompliance,&pixel,
  1338. fx_info->exception);
  1339. if (status != MagickFalse)
  1340. {
  1341. (void) AddValueToSplayTree(fx_info->colors,ConstantString(
  1342. name),ClonePixelInfo(&pixel));
  1343. p+=strlen(name);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. (void) CopyMagickString(symbol,p,MagickPathExtent);
  1349. StripString(symbol);
  1350. if (*symbol == '\0')
  1351. {
  1352. switch (channel)
  1353. {
  1354. case RedPixelChannel: return(QuantumScale*pixel.red);
  1355. case GreenPixelChannel: return(QuantumScale*pixel.green);
  1356. case BluePixelChannel: return(QuantumScale*pixel.blue);
  1357. case BlackPixelChannel:
  1358. {
  1359. if (image->colorspace != CMYKColorspace)
  1360. {
  1361. (void) ThrowMagickException(exception,GetMagickModule(),
  1362. ImageError,"ColorSeparatedImageRequired","`%s'",
  1363. image->filename);
  1364. return(0.0);
  1365. }
  1366. return(QuantumScale*pixel.black);
  1367. }
  1368. case AlphaPixelChannel:
  1369. {
  1370. if (pixel.alpha_trait == UndefinedPixelTrait)
  1371. return(1.0);
  1372. alpha=(double) (QuantumScale*pixel.alpha);
  1373. return(alpha);
  1374. }
  1375. case CompositePixelChannel:
  1376. {
  1377. Quantum
  1378. quantum_pixel[MaxPixelChannels];
  1379. SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
  1380. return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
  1381. }
  1382. case IndexPixelChannel:
  1383. return(0.0);
  1384. default:
  1385. break;
  1386. }
  1387. (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
  1388. "UnableToParseExpression","`%s'",p);
  1389. return(0.0);
  1390. }
  1391. switch (*symbol)
  1392. {
  1393. case 'A':
  1394. case 'a':
  1395. {
  1396. if (LocaleCompare(symbol,"a") == 0)
  1397. return((QuantumScale*pixel.alpha));
  1398. break;
  1399. }
  1400. case 'B':
  1401. case 'b':
  1402. {
  1403. if (LocaleCompare(symbol,"b") == 0)
  1404. return(QuantumScale*pixel.blue);
  1405. break;
  1406. }
  1407. case 'C':
  1408. case 'c':
  1409. {
  1410. if (LocaleNCompare(symbol,"channel",7) == 0)
  1411. {
  1412. GeometryInfo
  1413. channel_info;
  1414. MagickStatusType
  1415. flags;
  1416. flags=ParseGeometry(symbol+7,&channel_info);
  1417. if (image->colorspace == CMYKColorspace)
  1418. switch (channel)
  1419. {
  1420. case CyanPixelChannel:
  1421. {
  1422. if ((flags & RhoValue) == 0)
  1423. return(0.0);
  1424. return(channel_info.rho);
  1425. }
  1426. case MagentaPixelChannel:
  1427. {
  1428. if ((flags & SigmaValue) == 0)
  1429. return(0.0);
  1430. return(channel_info.sigma);
  1431. }
  1432. case YellowPixelChannel:
  1433. {
  1434. if ((flags & XiValue) == 0)
  1435. return(0.0);
  1436. return(channel_info.xi);
  1437. }
  1438. case BlackPixelChannel:
  1439. {
  1440. if ((flags & PsiValue) == 0)
  1441. return(0.0);
  1442. return(channel_info.psi);
  1443. }
  1444. case AlphaPixelChannel:
  1445. {
  1446. if ((flags & ChiValue) == 0)
  1447. return(0.0);
  1448. return(channel_info.chi);
  1449. }
  1450. default:
  1451. return(0.0);
  1452. }
  1453. switch (channel)
  1454. {
  1455. case RedPixelChannel:
  1456. {
  1457. if ((flags & RhoValue) == 0)
  1458. return(0.0);
  1459. return(channel_info.rho);
  1460. }
  1461. case GreenPixelChannel:
  1462. {
  1463. if ((flags & SigmaValue) == 0)
  1464. return(0.0);
  1465. return(channel_info.sigma);
  1466. }
  1467. case BluePixelChannel:
  1468. {
  1469. if ((flags & XiValue) == 0)
  1470. return(0.0);
  1471. return(channel_info.xi);
  1472. }
  1473. case BlackPixelChannel:
  1474. {
  1475. if ((flags & ChiValue) == 0)
  1476. return(0.0);
  1477. return(channel_info.chi);
  1478. }
  1479. case AlphaPixelChannel:
  1480. {
  1481. if ((flags & PsiValue) == 0)
  1482. return(0.0);
  1483. return(channel_info.psi);
  1484. }
  1485. default:
  1486. return(0.0);
  1487. }
  1488. }
  1489. if (LocaleCompare(symbol,"c") == 0)
  1490. return(QuantumScale*pixel.red);
  1491. break;
  1492. }
  1493. case 'D':
  1494. case 'd':
  1495. {
  1496. if (LocaleNCompare(symbol,"depth",5) == 0)
  1497. return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
  1498. break;
  1499. }
  1500. case 'E':
  1501. case 'e':
  1502. {
  1503. if (LocaleCompare(symbol,"extent") == 0)
  1504. {
  1505. if (image->extent != 0)
  1506. return((double) image->extent);
  1507. return((double) GetBlobSize(image));
  1508. }
  1509. break;
  1510. }
  1511. case 'G':
  1512. case 'g':
  1513. {
  1514. if (LocaleCompare(symbol,"g") == 0)
  1515. return(QuantumScale*pixel.green);
  1516. break;
  1517. }
  1518. case 'K':
  1519. case 'k':
  1520. {
  1521. if (LocaleNCompare(symbol,"kurtosis",8) == 0)
  1522. return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
  1523. if (LocaleCompare(symbol,"k") == 0)
  1524. {
  1525. if (image->colorspace != CMYKColorspace)
  1526. {
  1527. (void) ThrowMagickException(exception,GetMagickModule(),
  1528. OptionError,"ColorSeparatedImageRequired","`%s'",
  1529. image->filename);
  1530. return(0.0);
  1531. }
  1532. return(QuantumScale*pixel.black);
  1533. }
  1534. break;
  1535. }
  1536. case 'H':
  1537. case 'h':
  1538. {
  1539. if (LocaleCompare(symbol,"h") == 0)
  1540. return((double) image->rows);
  1541. if (LocaleCompare(symbol,"hue") == 0)
  1542. {
  1543. double
  1544. hue,
  1545. lightness,
  1546. saturation;
  1547. ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
  1548. &lightness);
  1549. return(hue);
  1550. }
  1551. break;
  1552. }
  1553. case 'I':
  1554. case 'i':
  1555. {
  1556. if ((LocaleCompare(symbol,"image.depth") == 0) ||
  1557. (LocaleCompare(symbol,"image.minima") == 0) ||
  1558. (LocaleCompare(symbol,"image.maxima") == 0) ||
  1559. (LocaleCompare(symbol,"image.mean") == 0) ||
  1560. (LocaleCompare(symbol,"image.kurtosis") == 0) ||
  1561. (LocaleCompare(symbol,"image.skewness") == 0) ||
  1562. (LocaleCompare(symbol,"image.standard_deviation") == 0))
  1563. return(FxChannelStatistics(fx_info,image,channel,symbol+6,exception));
  1564. if (LocaleCompare(symbol,"image.resolution.x") == 0)
  1565. return(image->resolution.x);
  1566. if (LocaleCompare(symbol,"image.resolution.y") == 0)
  1567. return(image->resolution.y);
  1568. if (LocaleCompare(symbol,"intensity") == 0)
  1569. {
  1570. Quantum
  1571. quantum_pixel[MaxPixelChannels];
  1572. SetPixelViaPixelInfo(image,&pixel,quantum_pixel);
  1573. return(QuantumScale*GetPixelIntensity(image,quantum_pixel));
  1574. }
  1575. if (LocaleCompare(symbol,"i") == 0)
  1576. return((double) x);
  1577. break;
  1578. }
  1579. case 'J':
  1580. case 'j':
  1581. {
  1582. if (LocaleCompare(symbol,"j") == 0)
  1583. return((double) y);
  1584. break;
  1585. }
  1586. case 'L':
  1587. case 'l':
  1588. {
  1589. if (LocaleCompare(symbol,"lightness") == 0)
  1590. {
  1591. double
  1592. hue,
  1593. lightness,
  1594. saturation;
  1595. ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
  1596. &lightness);
  1597. return(lightness);
  1598. }
  1599. if (LocaleCompare(symbol,"luma") == 0)
  1600. {
  1601. double
  1602. luma;
  1603. luma=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
  1604. return(QuantumScale*luma);
  1605. }
  1606. if (LocaleCompare(symbol,"luminance") == 0)
  1607. {
  1608. double
  1609. luminence;
  1610. luminence=0.212656*pixel.red+0.715158*pixel.green+0.072186*pixel.blue;
  1611. return(QuantumScale*luminence);
  1612. }
  1613. break;
  1614. }
  1615. case 'M':
  1616. case 'm':
  1617. {
  1618. if (LocaleNCompare(symbol,"maxima",6) == 0)
  1619. return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
  1620. if (LocaleNCompare(symbol,"mean",4) == 0)
  1621. return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
  1622. if (LocaleNCompare(symbol,"minima",6) == 0)
  1623. return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
  1624. if (LocaleCompare(symbol,"m") == 0)
  1625. return(QuantumScale*pixel.green);
  1626. break;
  1627. }
  1628. case 'N':
  1629. case 'n':
  1630. {
  1631. if (LocaleCompare(symbol,"n") == 0)
  1632. return((double) GetImageListLength(fx_info->images));
  1633. break;
  1634. }
  1635. case 'O':
  1636. case 'o':
  1637. {
  1638. if (LocaleCompare(symbol,"o") == 0)
  1639. return(QuantumScale*pixel.alpha);
  1640. break;
  1641. }
  1642. case 'P':
  1643. case 'p':
  1644. {
  1645. if (LocaleCompare(symbol,"page.height") == 0)
  1646. return((double) image->page.height);
  1647. if (LocaleCompare(symbol,"page.width") == 0)
  1648. return((double) image->page.width);
  1649. if (LocaleCompare(symbol,"page.x") == 0)
  1650. return((double) image->page.x);
  1651. if (LocaleCompare(symbol,"page.y") == 0)
  1652. return((double) image->page.y);
  1653. if (LocaleCompare(symbol,"printsize.x") == 0)
  1654. return(PerceptibleReciprocal(image->resolution.x)*image->columns);
  1655. if (LocaleCompare(symbol,"printsize.y") == 0)
  1656. return(PerceptibleReciprocal(image->resolution.y)*image->rows);
  1657. break;
  1658. }
  1659. case 'Q':
  1660. case 'q':
  1661. {
  1662. if (LocaleCompare(symbol,"quality") == 0)
  1663. return((double) image->quality);
  1664. break;
  1665. }
  1666. case 'R':
  1667. case 'r':
  1668. {
  1669. if (LocaleCompare(symbol,"resolution.x") == 0)
  1670. return(image->resolution.x);
  1671. if (LocaleCompare(symbol,"resolution.y") == 0)
  1672. return(image->resolution.y);
  1673. if (LocaleCompare(symbol,"r") == 0)
  1674. return(QuantumScale*pixel.red);
  1675. break;
  1676. }
  1677. case 'S':
  1678. case 's':
  1679. {
  1680. if (LocaleCompare(symbol,"saturation") == 0)
  1681. {
  1682. double
  1683. hue,
  1684. lightness,
  1685. saturation;
  1686. ConvertRGBToHSL(pixel.red,pixel.green,pixel.blue,&hue,&saturation,
  1687. &lightness);
  1688. return(saturation);
  1689. }
  1690. if (LocaleNCompare(symbol,"skewness",8) == 0)
  1691. return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
  1692. if (LocaleNCompare(symbol,"standard_deviation",18) == 0)
  1693. return(FxChannelStatistics(fx_info,image,channel,symbol,exception));
  1694. break;
  1695. }
  1696. case 'T':
  1697. case 't':
  1698. {
  1699. if (LocaleCompare(symbol,"t") == 0)
  1700. return((double) GetImageIndexInList(fx_info->images));
  1701. break;
  1702. }
  1703. case 'W':
  1704. case 'w':
  1705. {
  1706. if (LocaleCompare(symbol,"w") == 0)
  1707. return((double) image->columns);
  1708. break;
  1709. }
  1710. case 'Y':
  1711. case 'y':
  1712. {
  1713. if (LocaleCompare(symbol,"y") == 0)
  1714. return(QuantumScale*pixel.blue);
  1715. break;
  1716. }
  1717. case 'Z':
  1718. case 'z':
  1719. {
  1720. if (LocaleCompare(symbol,"z") == 0)
  1721. return((double) GetImageDepth(image,fx_info->exception));
  1722. break;
  1723. }
  1724. default:
  1725. break;
  1726. }
  1727. value=(const char *) GetValueFromSplayTree(fx_info->symbols,symbol);
  1728. if (value != (const char *) NULL)
  1729. return(StringToDouble(value,(char **) NULL));
  1730. (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
  1731. "UnableToParseExpression","`%s'",symbol);
  1732. return(0.0);
  1733. }
  1734. static const char *FxOperatorPrecedence(const char *expression,
  1735. ExceptionInfo *exception)
  1736. {
  1737. typedef enum
  1738. {
  1739. UndefinedPrecedence,
  1740. NullPrecedence,
  1741. BitwiseComplementPrecedence,
  1742. ExponentPrecedence,
  1743. ExponentialNotationPrecedence,
  1744. MultiplyPrecedence,
  1745. AdditionPrecedence,
  1746. ShiftPrecedence,
  1747. RelationalPrecedence,
  1748. EquivalencyPrecedence,
  1749. BitwiseAndPrecedence,
  1750. BitwiseOrPrecedence,
  1751. LogicalAndPrecedence,
  1752. LogicalOrPrecedence,
  1753. TernaryPrecedence,
  1754. AssignmentPrecedence,
  1755. CommaPrecedence,
  1756. SeparatorPrecedence
  1757. } FxPrecedence;
  1758. FxPrecedence
  1759. precedence,
  1760. target;
  1761. register const char
  1762. *subexpression;
  1763. register int
  1764. c;
  1765. size_t
  1766. level;
  1767. c=(-1);
  1768. level=0;
  1769. subexpression=(const char *) NULL;
  1770. target=NullPrecedence;
  1771. while ((c != '\0') && (*expression != '\0'))
  1772. {
  1773. precedence=UndefinedPrecedence;
  1774. if ((isspace((int) ((unsigned char) *expression)) != 0) || (c == (int) '@'))
  1775. {
  1776. expression++;
  1777. continue;
  1778. }
  1779. switch (*expression)
  1780. {
  1781. case 'A':
  1782. case 'a':
  1783. {
  1784. #if defined(MAGICKCORE_HAVE_ACOSH)
  1785. if (LocaleNCompare(expression,"acosh",5) == 0)
  1786. {
  1787. expression+=5;
  1788. break;
  1789. }
  1790. #endif
  1791. #if defined(MAGICKCORE_HAVE_ASINH)
  1792. if (LocaleNCompare(expression,"asinh",5) == 0)
  1793. {
  1794. expression+=5;
  1795. break;
  1796. }
  1797. #endif
  1798. #if defined(MAGICKCORE_HAVE_ATANH)
  1799. if (LocaleNCompare(expression,"atanh",5) == 0)
  1800. {
  1801. expression+=5;
  1802. break;
  1803. }
  1804. #endif
  1805. if (LocaleNCompare(expression,"atan2",5) == 0)
  1806. {
  1807. expression+=5;
  1808. break;
  1809. }
  1810. break;
  1811. }
  1812. case 'E':
  1813. case 'e':
  1814. {
  1815. if ((isdigit(c) != 0) &&
  1816. ((LocaleNCompare(expression,"E+",2) == 0) ||
  1817. (LocaleNCompare(expression,"E-",2) == 0)))
  1818. {
  1819. expression+=2; /* scientific notation */
  1820. break;
  1821. }
  1822. }
  1823. case 'J':
  1824. case 'j':
  1825. {
  1826. if ((LocaleNCompare(expression,"j0",2) == 0) ||
  1827. (LocaleNCompare(expression,"j1",2) == 0))
  1828. {
  1829. expression+=2;
  1830. break;
  1831. }
  1832. break;
  1833. }
  1834. case '#':
  1835. {
  1836. while (isxdigit((int) ((unsigned char) *(expression+1))) != 0)
  1837. expression++;
  1838. break;
  1839. }
  1840. default:
  1841. break;
  1842. }
  1843. if ((c == (int) '{') || (c == (int) '['))
  1844. level++;
  1845. else
  1846. if ((c == (int) '}') || (c == (int) ']'))
  1847. level--;
  1848. if (level == 0)
  1849. switch ((unsigned char) *expression)
  1850. {
  1851. case '~':
  1852. case '!':
  1853. {
  1854. precedence=BitwiseComplementPrecedence;
  1855. break;
  1856. }
  1857. case '^':
  1858. case '@':
  1859. {
  1860. precedence=ExponentPrecedence;
  1861. break;
  1862. }
  1863. default:
  1864. {
  1865. if (((c != 0) && ((isdigit(c) != 0) ||
  1866. (strchr(")",c) != (char *) NULL))) &&
  1867. (((islower((int) ((unsigned char) *expression)) != 0) ||
  1868. (strchr("(",(int) ((unsigned char) *expression)) != (char *) NULL)) ||
  1869. ((isdigit(c) == 0) &&
  1870. (isdigit((int) ((unsigned char) *expression)) != 0))) &&
  1871. (strchr("xy",(int) ((unsigned char) *expression)) == (char *) NULL))
  1872. precedence=MultiplyPrecedence;
  1873. break;
  1874. }
  1875. case '*':
  1876. case '/':
  1877. case '%':
  1878. {
  1879. precedence=MultiplyPrecedence;
  1880. break;
  1881. }
  1882. case '+':
  1883. case '-':
  1884. {
  1885. if ((strchr("(+-/*%:&^|<>~,",c) == (char *) NULL) ||
  1886. (isalpha(c) != 0))
  1887. precedence=AdditionPrecedence;
  1888. break;
  1889. }
  1890. case LeftShiftOperator:
  1891. case RightShiftOperator:
  1892. {
  1893. precedence=ShiftPrecedence;
  1894. break;
  1895. }
  1896. case '<':
  1897. case LessThanEqualOperator:
  1898. case GreaterThanEqualOperator:
  1899. case '>':
  1900. {
  1901. precedence=RelationalPrecedence;
  1902. break;
  1903. }
  1904. case EqualOperator:
  1905. case NotEqualOperator:
  1906. {
  1907. precedence=EquivalencyPrecedence;
  1908. break;
  1909. }
  1910. case '&':
  1911. {
  1912. precedence=BitwiseAndPrecedence;
  1913. break;
  1914. }
  1915. case '|':
  1916. {
  1917. precedence=BitwiseOrPrecedence;
  1918. break;
  1919. }
  1920. case LogicalAndOperator:
  1921. {
  1922. precedence=LogicalAndPrecedence;
  1923. break;
  1924. }
  1925. case LogicalOrOperator:
  1926. {
  1927. precedence=LogicalOrPrecedence;
  1928. break;
  1929. }
  1930. case ExponentialNotation:
  1931. {
  1932. precedence=ExponentialNotationPrecedence;
  1933. break;
  1934. }
  1935. case ':':
  1936. case '?':
  1937. {
  1938. precedence=TernaryPrecedence;
  1939. break;
  1940. }
  1941. case '=':
  1942. {
  1943. precedence=AssignmentPrecedence;
  1944. break;
  1945. }
  1946. case ',':
  1947. {
  1948. precedence=CommaPrecedence;
  1949. break;
  1950. }
  1951. case ';':
  1952. {
  1953. precedence=SeparatorPrecedence;
  1954. break;
  1955. }
  1956. }
  1957. if ((precedence == BitwiseComplementPrecedence) ||
  1958. (precedence == TernaryPrecedence) ||
  1959. (precedence == AssignmentPrecedence))
  1960. {
  1961. if (precedence > target)
  1962. {
  1963. /*
  1964. Right-to-left associativity.
  1965. */
  1966. target=precedence;
  1967. subexpression=expression;
  1968. }
  1969. }
  1970. else
  1971. if (precedence >= target)
  1972. {
  1973. /*
  1974. Left-to-right associativity.
  1975. */
  1976. target=precedence;
  1977. subexpression=expression;
  1978. }
  1979. if (strchr("(",(int) *expression) != (char *) NULL)
  1980. expression=FxSubexpression(expression,exception);
  1981. c=(int) (*expression++);
  1982. }
  1983. return(subexpression);
  1984. }
  1985. static double FxEvaluateSubexpression(FxInfo *fx_info,
  1986. const PixelChannel channel,const ssize_t x,const ssize_t y,
  1987. const char *expression,const size_t depth,double *beta,
  1988. ExceptionInfo *exception)
  1989. {
  1990. #define FxMaxParenthesisDepth 58
  1991. #define FxMaxSubexpressionDepth 200
  1992. #define FxReturn(value) \
  1993. { \
  1994. subexpression=DestroyString(subexpression); \
  1995. return(value); \
  1996. }
  1997. char
  1998. *q,
  1999. *subexpression;
  2000. double
  2001. alpha,
  2002. gamma;
  2003. register const char
  2004. *p;
  2005. *beta=0.0;
  2006. subexpression=AcquireString(expression);
  2007. *subexpression='\0';
  2008. if (depth > FxMaxSubexpressionDepth)
  2009. {
  2010. (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
  2011. "UnableToParseExpression","`%s'",expression);
  2012. FxReturn(0.0);
  2013. }
  2014. if (exception->severity >= ErrorException)
  2015. FxReturn(0.0);
  2016. while (isspace((int) ((unsigned char) *expression)) != 0)
  2017. expression++;
  2018. if (*expression == '\0')
  2019. FxReturn(0.0);
  2020. p=FxOperatorPrecedence(expression,exception);
  2021. if (p != (const char *) NULL)
  2022. {
  2023. (void) CopyMagickString(subexpression,expression,(size_t)
  2024. (p-expression+1));
  2025. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,depth+1,
  2026. beta,exception);
  2027. switch ((unsigned char) *p)
  2028. {
  2029. case '~':
  2030. {
  2031. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2032. exception);
  2033. *beta=(double) (~(size_t) *beta);
  2034. FxReturn(*beta);
  2035. }
  2036. case '!':
  2037. {
  2038. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2039. exception);
  2040. FxReturn(*beta == 0.0 ? 1.0 : 0.0);
  2041. }
  2042. case '^':
  2043. {
  2044. *beta=pow(alpha,FxEvaluateSubexpression(fx_info,channel,x,y,++p,
  2045. depth+1,beta,exception));
  2046. FxReturn(*beta);
  2047. }
  2048. case '*':
  2049. case ExponentialNotation:
  2050. {
  2051. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2052. exception);
  2053. FxReturn(alpha*(*beta));
  2054. }
  2055. case '/':
  2056. {
  2057. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2058. exception);
  2059. FxReturn(PerceptibleReciprocal(*beta)*alpha);
  2060. }
  2061. case '%':
  2062. {
  2063. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2064. exception);
  2065. *beta=fabs(floor((*beta)+0.5));
  2066. FxReturn(fmod(alpha,*beta));
  2067. }
  2068. case '+':
  2069. {
  2070. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2071. exception);
  2072. FxReturn(alpha+(*beta));
  2073. }
  2074. case '-':
  2075. {
  2076. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2077. exception);
  2078. FxReturn(alpha-(*beta));
  2079. }
  2080. case LeftShiftOperator:
  2081. {
  2082. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2083. exception);
  2084. if ((size_t) (gamma+0.5) >= (8*sizeof(size_t)))
  2085. {
  2086. (void) ThrowMagickException(exception,GetMagickModule(),
  2087. OptionError,"ShiftCountOverflow","`%s'",subexpression);
  2088. FxReturn(0.0);
  2089. }
  2090. *beta=(double) ((size_t) (alpha+0.5) << (size_t) (gamma+0.5));
  2091. FxReturn(*beta);
  2092. }
  2093. case RightShiftOperator:
  2094. {
  2095. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2096. exception);
  2097. if ((size_t) (gamma+0.5) >= (8*sizeof(size_t)))
  2098. {
  2099. (void) ThrowMagickException(exception,GetMagickModule(),
  2100. OptionError,"ShiftCountOverflow","`%s'",subexpression);
  2101. FxReturn(0.0);
  2102. }
  2103. *beta=(double) ((size_t) (alpha+0.5) >> (size_t) (gamma+0.5));
  2104. FxReturn(*beta);
  2105. }
  2106. case '<':
  2107. {
  2108. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2109. exception);
  2110. FxReturn(alpha < *beta ? 1.0 : 0.0);
  2111. }
  2112. case LessThanEqualOperator:
  2113. {
  2114. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2115. exception);
  2116. FxReturn(alpha <= *beta ? 1.0 : 0.0);
  2117. }
  2118. case '>':
  2119. {
  2120. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2121. exception);
  2122. FxReturn(alpha > *beta ? 1.0 : 0.0);
  2123. }
  2124. case GreaterThanEqualOperator:
  2125. {
  2126. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2127. exception);
  2128. FxReturn(alpha >= *beta ? 1.0 : 0.0);
  2129. }
  2130. case EqualOperator:
  2131. {
  2132. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2133. exception);
  2134. FxReturn(fabs(alpha-(*beta)) < MagickEpsilon ? 1.0 : 0.0);
  2135. }
  2136. case NotEqualOperator:
  2137. {
  2138. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2139. exception);
  2140. FxReturn(fabs(alpha-(*beta)) >= MagickEpsilon ? 1.0 : 0.0);
  2141. }
  2142. case '&':
  2143. {
  2144. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2145. exception);
  2146. *beta=(double) ((size_t) (alpha+0.5) & (size_t) (gamma+0.5));
  2147. FxReturn(*beta);
  2148. }
  2149. case '|':
  2150. {
  2151. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2152. exception);
  2153. *beta=(double) ((size_t) (alpha+0.5) | (size_t) (gamma+0.5));
  2154. FxReturn(*beta);
  2155. }
  2156. case LogicalAndOperator:
  2157. {
  2158. p++;
  2159. if (alpha <= 0.0)
  2160. {
  2161. *beta=0.0;
  2162. FxReturn(*beta);
  2163. }
  2164. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,depth+1,beta,
  2165. exception);
  2166. *beta=(gamma > 0.0) ? 1.0 : 0.0;
  2167. FxReturn(*beta);
  2168. }
  2169. case LogicalOrOperator:
  2170. {
  2171. p++;
  2172. if (alpha > 0.0)
  2173. {
  2174. *beta=1.0;
  2175. FxReturn(*beta);
  2176. }
  2177. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,depth+1,beta,
  2178. exception);
  2179. *beta=(gamma > 0.0) ? 1.0 : 0.0;
  2180. FxReturn(*beta);
  2181. }
  2182. case '?':
  2183. {
  2184. (void) CopyMagickString(subexpression,++p,MagickPathExtent);
  2185. q=subexpression;
  2186. p=StringToken(":",&q);
  2187. if (q == (char *) NULL)
  2188. {
  2189. (void) ThrowMagickException(exception,GetMagickModule(),
  2190. OptionError,"UnableToParseExpression","`%s'",subexpression);
  2191. FxReturn(0.0);
  2192. }
  2193. if (fabs(alpha) >= MagickEpsilon)
  2194. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,p,depth+1,beta,
  2195. exception);
  2196. else
  2197. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,q,depth+1,beta,
  2198. exception);
  2199. FxReturn(gamma);
  2200. }
  2201. case '=':
  2202. {
  2203. char
  2204. numeric[MagickPathExtent];
  2205. q=subexpression;
  2206. while (isalpha((int) ((unsigned char) *q)) != 0)
  2207. q++;
  2208. if (*q != '\0')
  2209. {
  2210. (void) ThrowMagickException(exception,GetMagickModule(),
  2211. OptionError,"UnableToParseExpression","`%s'",subexpression);
  2212. FxReturn(0.0);
  2213. }
  2214. ClearMagickException(exception);
  2215. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2216. exception);
  2217. (void) FormatLocaleString(numeric,MagickPathExtent,"%.20g",*beta);
  2218. (void) DeleteNodeFromSplayTree(fx_info->symbols,subexpression);
  2219. (void) AddValueToSplayTree(fx_info->symbols,ConstantString(
  2220. subexpression),ConstantString(numeric));
  2221. FxReturn(*beta);
  2222. }
  2223. case ',':
  2224. {
  2225. if (fabs(alpha) > MagickEpsilon)
  2226. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2227. exception);
  2228. FxReturn(alpha);
  2229. }
  2230. case ';':
  2231. {
  2232. *beta=FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,beta,
  2233. exception);
  2234. FxReturn(*beta);
  2235. }
  2236. default:
  2237. {
  2238. gamma=alpha*FxEvaluateSubexpression(fx_info,channel,x,y,++p,depth+1,
  2239. beta,exception);
  2240. FxReturn(gamma);
  2241. }
  2242. }
  2243. }
  2244. if (strchr("(",(int) *expression) != (char *) NULL)
  2245. {
  2246. if (depth >= FxMaxParenthesisDepth)
  2247. (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
  2248. "ParenthesisNestedTooDeeply","`%s'",expression);
  2249. (void) CopyMagickString(subexpression,expression+1,MagickPathExtent);
  2250. if (strlen(subexpression) != 0)
  2251. subexpression[strlen(subexpression)-1]='\0';
  2252. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,subexpression,depth+1,
  2253. beta,exception);
  2254. FxReturn(gamma);
  2255. }
  2256. switch (*expression)
  2257. {
  2258. case '+':
  2259. {
  2260. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,depth+1,
  2261. beta,exception);
  2262. FxReturn(1.0*gamma);
  2263. }
  2264. case '-':
  2265. {
  2266. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,depth+1,
  2267. beta,exception);
  2268. FxReturn(-1.0*gamma);
  2269. }
  2270. case '~':
  2271. {
  2272. gamma=FxEvaluateSubexpression(fx_info,channel,x,y,expression+1,depth+1,
  2273. beta,exception);
  2274. FxReturn((double) (~(size_t) (gamma+0.5)));
  2275. }
  2276. case 'A':
  2277. case 'a':
  2278. {
  2279. if (LocaleNCompare(expression,"abs",3) == 0)
  2280. {
  2281. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2282. depth+1,beta,exception);
  2283. FxReturn(fabs(alpha));
  2284. }
  2285. #if defined(MAGICKCORE_HAVE_ACOSH)
  2286. if (LocaleNCompare(expression,"acosh",5) == 0)
  2287. {
  2288. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2289. depth+1,beta,exception);
  2290. FxReturn(acosh(alpha));
  2291. }
  2292. #endif
  2293. if (LocaleNCompare(expression,"acos",4) == 0)
  2294. {
  2295. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2296. depth+1,beta,exception);
  2297. FxReturn(acos(alpha));
  2298. }
  2299. #if defined(MAGICKCORE_HAVE_J1)
  2300. if (LocaleNCompare(expression,"airy",4) == 0)
  2301. {
  2302. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2303. depth+1,beta,exception);
  2304. if (alpha == 0.0)
  2305. FxReturn(1.0);
  2306. gamma=2.0*j1((MagickPI*alpha))/(MagickPI*alpha);
  2307. FxReturn(gamma*gamma);
  2308. }
  2309. #endif
  2310. #if defined(MAGICKCORE_HAVE_ASINH)
  2311. if (LocaleNCompare(expression,"asinh",5) == 0)
  2312. {
  2313. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2314. depth+1,beta,exception);
  2315. FxReturn(asinh(alpha));
  2316. }
  2317. #endif
  2318. if (LocaleNCompare(expression,"asin",4) == 0)
  2319. {
  2320. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2321. depth+1,beta,exception);
  2322. FxReturn(asin(alpha));
  2323. }
  2324. if (LocaleNCompare(expression,"alt",3) == 0)
  2325. {
  2326. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2327. depth+1,beta,exception);
  2328. FxReturn(((ssize_t) alpha) & 0x01 ? -1.0 : 1.0);
  2329. }
  2330. if (LocaleNCompare(expression,"atan2",5) == 0)
  2331. {
  2332. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2333. depth+1,beta,exception);
  2334. FxReturn(atan2(alpha,*beta));
  2335. }
  2336. #if defined(MAGICKCORE_HAVE_ATANH)
  2337. if (LocaleNCompare(expression,"atanh",5) == 0)
  2338. {
  2339. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2340. depth+1,beta,exception);
  2341. FxReturn(atanh(alpha));
  2342. }
  2343. #endif
  2344. if (LocaleNCompare(expression,"atan",4) == 0)
  2345. {
  2346. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2347. depth+1,beta,exception);
  2348. FxReturn(atan(alpha));
  2349. }
  2350. if (LocaleCompare(expression,"a") == 0)
  2351. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2352. break;
  2353. }
  2354. case 'B':
  2355. case 'b':
  2356. {
  2357. if (LocaleCompare(expression,"b") == 0)
  2358. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2359. break;
  2360. }
  2361. case 'C':
  2362. case 'c':
  2363. {
  2364. if (LocaleNCompare(expression,"ceil",4) == 0)
  2365. {
  2366. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2367. depth+1,beta,exception);
  2368. FxReturn(ceil(alpha));
  2369. }
  2370. if (LocaleNCompare(expression,"clamp",5) == 0)
  2371. {
  2372. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2373. depth+1,beta,exception);
  2374. if (alpha < 0.0)
  2375. FxReturn(0.0);
  2376. if (alpha > 1.0)
  2377. FxReturn(1.0);
  2378. FxReturn(alpha);
  2379. }
  2380. if (LocaleNCompare(expression,"cosh",4) == 0)
  2381. {
  2382. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2383. depth+1,beta,exception);
  2384. FxReturn(cosh(alpha));
  2385. }
  2386. if (LocaleNCompare(expression,"cos",3) == 0)
  2387. {
  2388. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2389. depth+1,beta,exception);
  2390. FxReturn(cos(alpha));
  2391. }
  2392. if (LocaleCompare(expression,"c") == 0)
  2393. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2394. break;
  2395. }
  2396. case 'D':
  2397. case 'd':
  2398. {
  2399. if (LocaleNCompare(expression,"debug",5) == 0)
  2400. {
  2401. const char
  2402. *type;
  2403. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2404. depth+1,beta,exception);
  2405. if (fx_info->images->colorspace == CMYKColorspace)
  2406. switch (channel)
  2407. {
  2408. case CyanPixelChannel: type="cyan"; break;
  2409. case MagentaPixelChannel: type="magenta"; break;
  2410. case YellowPixelChannel: type="yellow"; break;
  2411. case AlphaPixelChannel: type="opacity"; break;
  2412. case BlackPixelChannel: type="black"; break;
  2413. default: type="unknown"; break;
  2414. }
  2415. else
  2416. switch (channel)
  2417. {
  2418. case RedPixelChannel: type="red"; break;
  2419. case GreenPixelChannel: type="green"; break;
  2420. case BluePixelChannel: type="blue"; break;
  2421. case AlphaPixelChannel: type="opacity"; break;
  2422. default: type="unknown"; break;
  2423. }
  2424. *subexpression='\0';
  2425. if (strlen(expression) > 6)
  2426. (void) CopyMagickString(subexpression,expression+6,
  2427. MagickPathExtent);
  2428. if (strlen(subexpression) > 1)
  2429. subexpression[strlen(subexpression)-1]='\0';
  2430. if (fx_info->file != (FILE *) NULL)
  2431. (void) FormatLocaleFile(fx_info->file,"%s[%.20g,%.20g].%s: "
  2432. "%s=%.*g\n",fx_info->images->filename,(double) x,(double) y,type,
  2433. subexpression,GetMagickPrecision(),alpha);
  2434. FxReturn(0.0);
  2435. }
  2436. if (LocaleNCompare(expression,"drc",3) == 0)
  2437. {
  2438. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2439. depth+1,beta,exception);
  2440. FxReturn((alpha/(*beta*(alpha-1.0)+1.0)));
  2441. }
  2442. break;
  2443. }
  2444. case 'E':
  2445. case 'e':
  2446. {
  2447. if (LocaleCompare(expression,"epsilon") == 0)
  2448. FxReturn(MagickEpsilon);
  2449. #if defined(MAGICKCORE_HAVE_ERF)
  2450. if (LocaleNCompare(expression,"erf",3) == 0)
  2451. {
  2452. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2453. depth+1,beta,exception);
  2454. FxReturn(erf(alpha));
  2455. }
  2456. #endif
  2457. if (LocaleNCompare(expression,"exp",3) == 0)
  2458. {
  2459. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2460. depth+1,beta,exception);
  2461. FxReturn(exp(alpha));
  2462. }
  2463. if (LocaleCompare(expression,"e") == 0)
  2464. FxReturn(2.7182818284590452354);
  2465. break;
  2466. }
  2467. case 'F':
  2468. case 'f':
  2469. {
  2470. if (LocaleNCompare(expression,"floor",5) == 0)
  2471. {
  2472. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2473. depth+1,beta,exception);
  2474. FxReturn(floor(alpha));
  2475. }
  2476. break;
  2477. }
  2478. case 'G':
  2479. case 'g':
  2480. {
  2481. if (LocaleNCompare(expression,"gauss",5) == 0)
  2482. {
  2483. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2484. depth+1,beta,exception);
  2485. gamma=exp((-alpha*alpha/2.0))/sqrt(2.0*MagickPI);
  2486. FxReturn(gamma);
  2487. }
  2488. if (LocaleNCompare(expression,"gcd",3) == 0)
  2489. {
  2490. MagickOffsetType
  2491. gcd;
  2492. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2493. depth+1,beta,exception);
  2494. gcd=FxGCD((MagickOffsetType) (alpha+0.5),(MagickOffsetType) (*beta+
  2495. 0.5));
  2496. FxReturn((double) gcd);
  2497. }
  2498. if (LocaleCompare(expression,"g") == 0)
  2499. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2500. break;
  2501. }
  2502. case 'H':
  2503. case 'h':
  2504. {
  2505. if (LocaleCompare(expression,"h") == 0)
  2506. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2507. if (LocaleCompare(expression,"hue") == 0)
  2508. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2509. if (LocaleNCompare(expression,"hypot",5) == 0)
  2510. {
  2511. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2512. depth+1,beta,exception);
  2513. FxReturn(hypot(alpha,*beta));
  2514. }
  2515. break;
  2516. }
  2517. case 'K':
  2518. case 'k':
  2519. {
  2520. if (LocaleCompare(expression,"k") == 0)
  2521. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2522. break;
  2523. }
  2524. case 'I':
  2525. case 'i':
  2526. {
  2527. if (LocaleCompare(expression,"intensity") == 0)
  2528. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2529. if (LocaleNCompare(expression,"int",3) == 0)
  2530. {
  2531. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2532. depth+1,beta,exception);
  2533. FxReturn(floor(alpha));
  2534. }
  2535. if (LocaleNCompare(expression,"isnan",5) == 0)
  2536. {
  2537. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2538. depth+1,beta,exception);
  2539. FxReturn((double) !!IsNaN(alpha));
  2540. }
  2541. if (LocaleCompare(expression,"i") == 0)
  2542. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2543. break;
  2544. }
  2545. case 'J':
  2546. case 'j':
  2547. {
  2548. if (LocaleCompare(expression,"j") == 0)
  2549. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2550. #if defined(MAGICKCORE_HAVE_J0)
  2551. if (LocaleNCompare(expression,"j0",2) == 0)
  2552. {
  2553. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,
  2554. depth+1,beta,exception);
  2555. FxReturn(j0(alpha));
  2556. }
  2557. #endif
  2558. #if defined(MAGICKCORE_HAVE_J1)
  2559. if (LocaleNCompare(expression,"j1",2) == 0)
  2560. {
  2561. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,
  2562. depth+1,beta,exception);
  2563. FxReturn(j1(alpha));
  2564. }
  2565. #endif
  2566. #if defined(MAGICKCORE_HAVE_J1)
  2567. if (LocaleNCompare(expression,"jinc",4) == 0)
  2568. {
  2569. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2570. depth+1,beta,exception);
  2571. if (alpha == 0.0)
  2572. FxReturn(1.0);
  2573. gamma=(2.0*j1((MagickPI*alpha))/(MagickPI*alpha));
  2574. FxReturn(gamma);
  2575. }
  2576. #endif
  2577. break;
  2578. }
  2579. case 'L':
  2580. case 'l':
  2581. {
  2582. if (LocaleNCompare(expression,"ln",2) == 0)
  2583. {
  2584. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+2,
  2585. depth+1,beta,exception);
  2586. FxReturn(log(alpha));
  2587. }
  2588. if (LocaleNCompare(expression,"logtwo",6) == 0)
  2589. {
  2590. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,
  2591. depth+1,beta,exception);
  2592. FxReturn(log10(alpha)/log10(2.0));
  2593. }
  2594. if (LocaleNCompare(expression,"log",3) == 0)
  2595. {
  2596. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2597. depth+1,beta,exception);
  2598. FxReturn(log10(alpha));
  2599. }
  2600. if (LocaleCompare(expression,"lightness") == 0)
  2601. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2602. break;
  2603. }
  2604. case 'M':
  2605. case 'm':
  2606. {
  2607. if (LocaleCompare(expression,"MaxRGB") == 0)
  2608. FxReturn(QuantumRange);
  2609. if (LocaleNCompare(expression,"maxima",6) == 0)
  2610. break;
  2611. if (LocaleNCompare(expression,"max",3) == 0)
  2612. {
  2613. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2614. depth+1,beta,exception);
  2615. FxReturn(alpha > *beta ? alpha : *beta);
  2616. }
  2617. if (LocaleNCompare(expression,"minima",6) == 0)
  2618. break;
  2619. if (LocaleNCompare(expression,"min",3) == 0)
  2620. {
  2621. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2622. depth+1,beta,exception);
  2623. FxReturn(alpha < *beta ? alpha : *beta);
  2624. }
  2625. if (LocaleNCompare(expression,"mod",3) == 0)
  2626. {
  2627. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2628. depth+1,beta,exception);
  2629. gamma=alpha-floor((alpha*PerceptibleReciprocal(*beta)))*(*beta);
  2630. FxReturn(gamma);
  2631. }
  2632. if (LocaleCompare(expression,"m") == 0)
  2633. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2634. break;
  2635. }
  2636. case 'N':
  2637. case 'n':
  2638. {
  2639. if (LocaleNCompare(expression,"not",3) == 0)
  2640. {
  2641. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2642. depth+1,beta,exception);
  2643. FxReturn((double) (alpha < MagickEpsilon));
  2644. }
  2645. if (LocaleCompare(expression,"n") == 0)
  2646. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2647. break;
  2648. }
  2649. case 'O':
  2650. case 'o':
  2651. {
  2652. if (LocaleCompare(expression,"Opaque") == 0)
  2653. FxReturn(1.0);
  2654. if (LocaleCompare(expression,"o") == 0)
  2655. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2656. break;
  2657. }
  2658. case 'P':
  2659. case 'p':
  2660. {
  2661. if (LocaleCompare(expression,"phi") == 0)
  2662. FxReturn(MagickPHI);
  2663. if (LocaleCompare(expression,"pi") == 0)
  2664. FxReturn(MagickPI);
  2665. if (LocaleNCompare(expression,"pow",3) == 0)
  2666. {
  2667. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2668. depth+1,beta,exception);
  2669. FxReturn(pow(alpha,*beta));
  2670. }
  2671. if (LocaleCompare(expression,"p") == 0)
  2672. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2673. break;
  2674. }
  2675. case 'Q':
  2676. case 'q':
  2677. {
  2678. if (LocaleCompare(expression,"QuantumRange") == 0)
  2679. FxReturn(QuantumRange);
  2680. if (LocaleCompare(expression,"QuantumScale") == 0)
  2681. FxReturn(QuantumScale);
  2682. break;
  2683. }
  2684. case 'R':
  2685. case 'r':
  2686. {
  2687. if (LocaleNCompare(expression,"rand",4) == 0)
  2688. {
  2689. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  2690. #pragma omp critical (MagickCore_FxEvaluateSubexpression)
  2691. #endif
  2692. alpha=GetPseudoRandomValue(fx_info->random_info);
  2693. FxReturn(alpha);
  2694. }
  2695. if (LocaleNCompare(expression,"round",5) == 0)
  2696. {
  2697. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2698. depth+1,beta,exception);
  2699. FxReturn(floor(alpha+0.5));
  2700. }
  2701. if (LocaleCompare(expression,"r") == 0)
  2702. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2703. break;
  2704. }
  2705. case 'S':
  2706. case 's':
  2707. {
  2708. if (LocaleCompare(expression,"saturation") == 0)
  2709. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2710. if (LocaleNCompare(expression,"sign",4) == 0)
  2711. {
  2712. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2713. depth+1,beta,exception);
  2714. FxReturn(alpha < 0.0 ? -1.0 : 1.0);
  2715. }
  2716. if (LocaleNCompare(expression,"sinc",4) == 0)
  2717. {
  2718. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2719. depth+1,beta,exception);
  2720. if (alpha == 0)
  2721. FxReturn(1.0);
  2722. gamma=sin((MagickPI*alpha))/(MagickPI*alpha);
  2723. FxReturn(gamma);
  2724. }
  2725. if (LocaleNCompare(expression,"sinh",4) == 0)
  2726. {
  2727. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2728. depth+1,beta,exception);
  2729. FxReturn(sinh(alpha));
  2730. }
  2731. if (LocaleNCompare(expression,"sin",3) == 0)
  2732. {
  2733. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2734. depth+1,beta,exception);
  2735. FxReturn(sin(alpha));
  2736. }
  2737. if (LocaleNCompare(expression,"sqrt",4) == 0)
  2738. {
  2739. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2740. depth+1,beta,exception);
  2741. FxReturn(sqrt(alpha));
  2742. }
  2743. if (LocaleNCompare(expression,"squish",6) == 0)
  2744. {
  2745. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+6,
  2746. depth+1,beta,exception);
  2747. FxReturn((1.0/(1.0+exp(-alpha))));
  2748. }
  2749. if (LocaleCompare(expression,"s") == 0)
  2750. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2751. break;
  2752. }
  2753. case 'T':
  2754. case 't':
  2755. {
  2756. if (LocaleNCompare(expression,"tanh",4) == 0)
  2757. {
  2758. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+4,
  2759. depth+1,beta,exception);
  2760. FxReturn(tanh(alpha));
  2761. }
  2762. if (LocaleNCompare(expression,"tan",3) == 0)
  2763. {
  2764. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+3,
  2765. depth+1,beta,exception);
  2766. FxReturn(tan(alpha));
  2767. }
  2768. if (LocaleCompare(expression,"Transparent") == 0)
  2769. FxReturn(0.0);
  2770. if (LocaleNCompare(expression,"trunc",5) == 0)
  2771. {
  2772. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2773. depth+1,beta,exception);
  2774. if (alpha >= 0.0)
  2775. FxReturn(floor(alpha));
  2776. FxReturn(ceil(alpha));
  2777. }
  2778. if (LocaleCompare(expression,"t") == 0)
  2779. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2780. break;
  2781. }
  2782. case 'U':
  2783. case 'u':
  2784. {
  2785. if (LocaleCompare(expression,"u") == 0)
  2786. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2787. break;
  2788. }
  2789. case 'V':
  2790. case 'v':
  2791. {
  2792. if (LocaleCompare(expression,"v") == 0)
  2793. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2794. break;
  2795. }
  2796. case 'W':
  2797. case 'w':
  2798. {
  2799. if (LocaleNCompare(expression,"while",5) == 0)
  2800. {
  2801. do
  2802. {
  2803. alpha=FxEvaluateSubexpression(fx_info,channel,x,y,expression+5,
  2804. depth+1,beta,exception);
  2805. } while (fabs(alpha) >= MagickEpsilon);
  2806. FxReturn(*beta);
  2807. }
  2808. if (LocaleCompare(expression,"w") == 0)
  2809. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2810. break;
  2811. }
  2812. case 'Y':
  2813. case 'y':
  2814. {
  2815. if (LocaleCompare(expression,"y") == 0)
  2816. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2817. break;
  2818. }
  2819. case 'Z':
  2820. case 'z':
  2821. {
  2822. if (LocaleCompare(expression,"z") == 0)
  2823. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2824. break;
  2825. }
  2826. default:
  2827. break;
  2828. }
  2829. subexpression=DestroyString(subexpression);
  2830. q=(char *) expression;
  2831. alpha=InterpretSiPrefixValue(expression,&q);
  2832. if (q == expression)
  2833. FxReturn(FxGetSymbol(fx_info,channel,x,y,expression,depth+1,exception));
  2834. FxReturn(alpha);
  2835. }
  2836. MagickPrivate MagickBooleanType FxEvaluateExpression(FxInfo *fx_info,
  2837. double *alpha,ExceptionInfo *exception)
  2838. {
  2839. MagickBooleanType
  2840. status;
  2841. status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
  2842. exception);
  2843. return(status);
  2844. }
  2845. MagickExport MagickBooleanType FxPreprocessExpression(FxInfo *fx_info,
  2846. double *alpha,ExceptionInfo *exception)
  2847. {
  2848. FILE
  2849. *file;
  2850. MagickBooleanType
  2851. status;
  2852. file=fx_info->file;
  2853. fx_info->file=(FILE *) NULL;
  2854. status=FxEvaluateChannelExpression(fx_info,GrayPixelChannel,0,0,alpha,
  2855. exception);
  2856. fx_info->file=file;
  2857. return(status);
  2858. }
  2859. MagickPrivate MagickBooleanType FxEvaluateChannelExpression(FxInfo *fx_info,
  2860. const PixelChannel channel,const ssize_t x,const ssize_t y,
  2861. double *alpha,ExceptionInfo *exception)
  2862. {
  2863. double
  2864. beta;
  2865. beta=0.0;
  2866. *alpha=FxEvaluateSubexpression(fx_info,channel,x,y,fx_info->expression,0,
  2867. &beta,exception);
  2868. return(exception->severity == OptionError ? MagickFalse : MagickTrue);
  2869. }
  2870. /*
  2871. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2872. % %
  2873. % %
  2874. % %
  2875. % F x I m a g e %
  2876. % %
  2877. % %
  2878. % %
  2879. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2880. %
  2881. % FxImage() applies a mathematical expression to the specified image.
  2882. %
  2883. % The format of the FxImage method is:
  2884. %
  2885. % Image *FxImage(const Image *image,const char *expression,
  2886. % ExceptionInfo *exception)
  2887. %
  2888. % A description of each parameter follows:
  2889. %
  2890. % o image: the image.
  2891. %
  2892. % o expression: A mathematical expression.
  2893. %
  2894. % o exception: return any errors or warnings in this structure.
  2895. %
  2896. */
  2897. static FxInfo **DestroyFxThreadSet(FxInfo **fx_info)
  2898. {
  2899. register ssize_t
  2900. i;
  2901. assert(fx_info != (FxInfo **) NULL);
  2902. for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
  2903. if (fx_info[i] != (FxInfo *) NULL)
  2904. fx_info[i]=DestroyFxInfo(fx_info[i]);
  2905. fx_info=(FxInfo **) RelinquishMagickMemory(fx_info);
  2906. return(fx_info);
  2907. }
  2908. static FxInfo **AcquireFxThreadSet(const Image *image,const char *expression,
  2909. ExceptionInfo *exception)
  2910. {
  2911. char
  2912. *fx_expression;
  2913. FxInfo
  2914. **fx_info;
  2915. double
  2916. alpha;
  2917. register ssize_t
  2918. i;
  2919. size_t
  2920. number_threads;
  2921. number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
  2922. fx_info=(FxInfo **) AcquireQuantumMemory(number_threads,sizeof(*fx_info));
  2923. if (fx_info == (FxInfo **) NULL)
  2924. {
  2925. (void) ThrowMagickException(exception,GetMagickModule(),
  2926. ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
  2927. return((FxInfo **) NULL);
  2928. }
  2929. (void) memset(fx_info,0,number_threads*sizeof(*fx_info));
  2930. if (*expression != '@')
  2931. fx_expression=ConstantString(expression);
  2932. else
  2933. fx_expression=FileToString(expression+1,~0UL,exception);
  2934. for (i=0; i < (ssize_t) number_threads; i++)
  2935. {
  2936. MagickBooleanType
  2937. status;
  2938. fx_info[i]=AcquireFxInfo(image,fx_expression,exception);
  2939. if (fx_info[i] == (FxInfo *) NULL)
  2940. break;
  2941. status=FxPreprocessExpression(fx_info[i],&alpha,exception);
  2942. if (status == MagickFalse)
  2943. break;
  2944. }
  2945. fx_expression=DestroyString(fx_expression);
  2946. if (i < (ssize_t) number_threads)
  2947. fx_info=DestroyFxThreadSet(fx_info);
  2948. return(fx_info);
  2949. }
  2950. MagickExport Image *FxImage(const Image *image,const char *expression,
  2951. ExceptionInfo *exception)
  2952. {
  2953. #define FxImageTag "Fx/Image"
  2954. CacheView
  2955. *fx_view,
  2956. *image_view;
  2957. FxInfo
  2958. **magick_restrict fx_info;
  2959. Image
  2960. *fx_image;
  2961. MagickBooleanType
  2962. status;
  2963. MagickOffsetType
  2964. progress;
  2965. ssize_t
  2966. y;
  2967. assert(image != (Image *) NULL);
  2968. assert(image->signature == MagickCoreSignature);
  2969. if (image->debug != MagickFalse)
  2970. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  2971. if (expression == (const char *) NULL)
  2972. return(CloneImage(image,0,0,MagickTrue,exception));
  2973. fx_info=AcquireFxThreadSet(image,expression,exception);
  2974. if (fx_info == (FxInfo **) NULL)
  2975. return((Image *) NULL);
  2976. fx_image=CloneImage(image,0,0,MagickTrue,exception);
  2977. if (fx_image == (Image *) NULL)
  2978. {
  2979. fx_info=DestroyFxThreadSet(fx_info);
  2980. return((Image *) NULL);
  2981. }
  2982. if (SetImageStorageClass(fx_image,DirectClass,exception) == MagickFalse)
  2983. {
  2984. fx_info=DestroyFxThreadSet(fx_info);
  2985. fx_image=DestroyImage(fx_image);
  2986. return((Image *) NULL);
  2987. }
  2988. /*
  2989. Fx image.
  2990. */
  2991. status=MagickTrue;
  2992. progress=0;
  2993. image_view=AcquireVirtualCacheView(image,exception);
  2994. fx_view=AcquireAuthenticCacheView(fx_image,exception);
  2995. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  2996. #pragma omp parallel for schedule(static) shared(progress,status) \
  2997. magick_number_threads(image,fx_image,fx_image->rows,1)
  2998. #endif
  2999. for (y=0; y < (ssize_t) fx_image->rows; y++)
  3000. {
  3001. const int
  3002. id = GetOpenMPThreadId();
  3003. register const Quantum
  3004. *magick_restrict p;
  3005. register Quantum
  3006. *magick_restrict q;
  3007. register ssize_t
  3008. x;
  3009. if (status == MagickFalse)
  3010. continue;
  3011. p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
  3012. q=QueueCacheViewAuthenticPixels(fx_view,0,y,fx_image->columns,1,exception);
  3013. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  3014. {
  3015. status=MagickFalse;
  3016. continue;
  3017. }
  3018. for (x=0; x < (ssize_t) fx_image->columns; x++)
  3019. {
  3020. register ssize_t
  3021. i;
  3022. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  3023. {
  3024. double
  3025. alpha;
  3026. PixelChannel channel = GetPixelChannelChannel(image,i);
  3027. PixelTrait traits = GetPixelChannelTraits(image,channel);
  3028. PixelTrait fx_traits=GetPixelChannelTraits(fx_image,channel);
  3029. if ((traits == UndefinedPixelTrait) ||
  3030. (fx_traits == UndefinedPixelTrait))
  3031. continue;
  3032. if ((fx_traits & CopyPixelTrait) != 0)
  3033. {
  3034. SetPixelChannel(fx_image,channel,p[i],q);
  3035. continue;
  3036. }
  3037. alpha=0.0;
  3038. (void) FxEvaluateChannelExpression(fx_info[id],channel,x,y,&alpha,
  3039. exception);
  3040. q[i]=ClampToQuantum(QuantumRange*alpha);
  3041. }
  3042. p+=GetPixelChannels(image);
  3043. q+=GetPixelChannels(fx_image);
  3044. }
  3045. if (SyncCacheViewAuthenticPixels(fx_view,exception) == MagickFalse)
  3046. status=MagickFalse;
  3047. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  3048. {
  3049. MagickBooleanType
  3050. proceed;
  3051. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  3052. #pragma omp atomic
  3053. #endif
  3054. progress++;
  3055. proceed=SetImageProgress(image,FxImageTag,progress,image->rows);
  3056. if (proceed == MagickFalse)
  3057. status=MagickFalse;
  3058. }
  3059. }
  3060. fx_view=DestroyCacheView(fx_view);
  3061. image_view=DestroyCacheView(image_view);
  3062. fx_info=DestroyFxThreadSet(fx_info);
  3063. if (status == MagickFalse)
  3064. fx_image=DestroyImage(fx_image);
  3065. return(fx_image);
  3066. }
  3067. /*
  3068. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3069. % %
  3070. % %
  3071. % %
  3072. % I m p l o d e I m a g e %
  3073. % %
  3074. % %
  3075. % %
  3076. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3077. %
  3078. % ImplodeImage() creates a new image that is a copy of an existing
  3079. % one with the image pixels "implode" by the specified percentage. It
  3080. % allocates the memory necessary for the new Image structure and returns a
  3081. % pointer to the new image.
  3082. %
  3083. % The format of the ImplodeImage method is:
  3084. %
  3085. % Image *ImplodeImage(const Image *image,const double amount,
  3086. % const PixelInterpolateMethod method,ExceptionInfo *exception)
  3087. %
  3088. % A description of each parameter follows:
  3089. %
  3090. % o implode_image: Method ImplodeImage returns a pointer to the image
  3091. % after it is implode. A null image is returned if there is a memory
  3092. % shortage.
  3093. %
  3094. % o image: the image.
  3095. %
  3096. % o amount: Define the extent of the implosion.
  3097. %
  3098. % o method: the pixel interpolation method.
  3099. %
  3100. % o exception: return any errors or warnings in this structure.
  3101. %
  3102. */
  3103. MagickExport Image *ImplodeImage(const Image *image,const double amount,
  3104. const PixelInterpolateMethod method,ExceptionInfo *exception)
  3105. {
  3106. #define ImplodeImageTag "Implode/Image"
  3107. CacheView
  3108. *canvas_view,
  3109. *implode_view,
  3110. *interpolate_view;
  3111. double
  3112. radius;
  3113. Image
  3114. *canvas_image,
  3115. *implode_image;
  3116. MagickBooleanType
  3117. status;
  3118. MagickOffsetType
  3119. progress;
  3120. PointInfo
  3121. center,
  3122. scale;
  3123. ssize_t
  3124. y;
  3125. /*
  3126. Initialize implode image attributes.
  3127. */
  3128. assert(image != (Image *) NULL);
  3129. assert(image->signature == MagickCoreSignature);
  3130. if (image->debug != MagickFalse)
  3131. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  3132. assert(exception != (ExceptionInfo *) NULL);
  3133. assert(exception->signature == MagickCoreSignature);
  3134. canvas_image=CloneImage(image,0,0,MagickTrue,exception);
  3135. if (canvas_image == (Image *) NULL)
  3136. return((Image *) NULL);
  3137. if ((canvas_image->alpha_trait == UndefinedPixelTrait) &&
  3138. (canvas_image->background_color.alpha != OpaqueAlpha))
  3139. (void) SetImageAlphaChannel(canvas_image,OpaqueAlphaChannel,exception);
  3140. implode_image=CloneImage(canvas_image,0,0,MagickTrue,exception);
  3141. if (implode_image == (Image *) NULL)
  3142. {
  3143. canvas_image=DestroyImage(canvas_image);
  3144. return((Image *) NULL);
  3145. }
  3146. if (SetImageStorageClass(implode_image,DirectClass,exception) == MagickFalse)
  3147. {
  3148. canvas_image=DestroyImage(canvas_image);
  3149. implode_image=DestroyImage(implode_image);
  3150. return((Image *) NULL);
  3151. }
  3152. /*
  3153. Compute scaling factor.
  3154. */
  3155. scale.x=1.0;
  3156. scale.y=1.0;
  3157. center.x=0.5*canvas_image->columns;
  3158. center.y=0.5*canvas_image->rows;
  3159. radius=center.x;
  3160. if (canvas_image->columns > canvas_image->rows)
  3161. scale.y=(double) canvas_image->columns/(double) canvas_image->rows;
  3162. else
  3163. if (canvas_image->columns < canvas_image->rows)
  3164. {
  3165. scale.x=(double) canvas_image->rows/(double) canvas_image->columns;
  3166. radius=center.y;
  3167. }
  3168. /*
  3169. Implode image.
  3170. */
  3171. status=MagickTrue;
  3172. progress=0;
  3173. canvas_view=AcquireVirtualCacheView(canvas_image,exception);
  3174. interpolate_view=AcquireVirtualCacheView(canvas_image,exception);
  3175. implode_view=AcquireAuthenticCacheView(implode_image,exception);
  3176. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  3177. #pragma omp parallel for schedule(static) shared(progress,status) \
  3178. magick_number_threads(canvas_image,implode_image,canvas_image->rows,1)
  3179. #endif
  3180. for (y=0; y < (ssize_t) canvas_image->rows; y++)
  3181. {
  3182. double
  3183. distance;
  3184. PointInfo
  3185. delta;
  3186. register const Quantum
  3187. *magick_restrict p;
  3188. register ssize_t
  3189. x;
  3190. register Quantum
  3191. *magick_restrict q;
  3192. if (status == MagickFalse)
  3193. continue;
  3194. p=GetCacheViewVirtualPixels(canvas_view,0,y,canvas_image->columns,1,
  3195. exception);
  3196. q=QueueCacheViewAuthenticPixels(implode_view,0,y,implode_image->columns,1,
  3197. exception);
  3198. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  3199. {
  3200. status=MagickFalse;
  3201. continue;
  3202. }
  3203. delta.y=scale.y*(double) (y-center.y);
  3204. for (x=0; x < (ssize_t) canvas_image->columns; x++)
  3205. {
  3206. register ssize_t
  3207. i;
  3208. /*
  3209. Determine if the pixel is within an ellipse.
  3210. */
  3211. delta.x=scale.x*(double) (x-center.x);
  3212. distance=delta.x*delta.x+delta.y*delta.y;
  3213. if (distance >= (radius*radius))
  3214. for (i=0; i < (ssize_t) GetPixelChannels(canvas_image); i++)
  3215. {
  3216. PixelChannel channel = GetPixelChannelChannel(canvas_image,i);
  3217. PixelTrait traits = GetPixelChannelTraits(canvas_image,channel);
  3218. PixelTrait implode_traits = GetPixelChannelTraits(implode_image,
  3219. channel);
  3220. if ((traits == UndefinedPixelTrait) ||
  3221. (implode_traits == UndefinedPixelTrait))
  3222. continue;
  3223. SetPixelChannel(implode_image,channel,p[i],q);
  3224. }
  3225. else
  3226. {
  3227. double
  3228. factor;
  3229. /*
  3230. Implode the pixel.
  3231. */
  3232. factor=1.0;
  3233. if (distance > 0.0)
  3234. factor=pow(sin(MagickPI*sqrt((double) distance)/radius/2),-amount);
  3235. status=InterpolatePixelChannels(canvas_image,interpolate_view,
  3236. implode_image,method,(double) (factor*delta.x/scale.x+center.x),
  3237. (double) (factor*delta.y/scale.y+center.y),q,exception);
  3238. if (status == MagickFalse)
  3239. break;
  3240. }
  3241. p+=GetPixelChannels(canvas_image);
  3242. q+=GetPixelChannels(implode_image);
  3243. }
  3244. if (SyncCacheViewAuthenticPixels(implode_view,exception) == MagickFalse)
  3245. status=MagickFalse;
  3246. if (canvas_image->progress_monitor != (MagickProgressMonitor) NULL)
  3247. {
  3248. MagickBooleanType
  3249. proceed;
  3250. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  3251. #pragma omp atomic
  3252. #endif
  3253. progress++;
  3254. proceed=SetImageProgress(canvas_image,ImplodeImageTag,progress,
  3255. canvas_image->rows);
  3256. if (proceed == MagickFalse)
  3257. status=MagickFalse;
  3258. }
  3259. }
  3260. implode_view=DestroyCacheView(implode_view);
  3261. interpolate_view=DestroyCacheView(interpolate_view);
  3262. canvas_view=DestroyCacheView(canvas_view);
  3263. canvas_image=DestroyImage(canvas_image);
  3264. if (status == MagickFalse)
  3265. implode_image=DestroyImage(implode_image);
  3266. return(implode_image);
  3267. }
  3268. /*
  3269. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3270. % %
  3271. % %
  3272. % %
  3273. % M o r p h I m a g e s %
  3274. % %
  3275. % %
  3276. % %
  3277. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3278. %
  3279. % The MorphImages() method requires a minimum of two images. The first
  3280. % image is transformed into the second by a number of intervening images
  3281. % as specified by frames.
  3282. %
  3283. % The format of the MorphImage method is:
  3284. %
  3285. % Image *MorphImages(const Image *image,const size_t number_frames,
  3286. % ExceptionInfo *exception)
  3287. %
  3288. % A description of each parameter follows:
  3289. %
  3290. % o image: the image.
  3291. %
  3292. % o number_frames: Define the number of in-between image to generate.
  3293. % The more in-between frames, the smoother the morph.
  3294. %
  3295. % o exception: return any errors or warnings in this structure.
  3296. %
  3297. */
  3298. MagickExport Image *MorphImages(const Image *image,const size_t number_frames,
  3299. ExceptionInfo *exception)
  3300. {
  3301. #define MorphImageTag "Morph/Image"
  3302. double
  3303. alpha,
  3304. beta;
  3305. Image
  3306. *morph_image,
  3307. *morph_images;
  3308. MagickBooleanType
  3309. status;
  3310. MagickOffsetType
  3311. scene;
  3312. register const Image
  3313. *next;
  3314. register ssize_t
  3315. n;
  3316. ssize_t
  3317. y;
  3318. /*
  3319. Clone first frame in sequence.
  3320. */
  3321. assert(image != (Image *) NULL);
  3322. assert(image->signature == MagickCoreSignature);
  3323. if (image->debug != MagickFalse)
  3324. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  3325. assert(exception != (ExceptionInfo *) NULL);
  3326. assert(exception->signature == MagickCoreSignature);
  3327. morph_images=CloneImage(image,0,0,MagickTrue,exception);
  3328. if (morph_images == (Image *) NULL)
  3329. return((Image *) NULL);
  3330. if (GetNextImageInList(image) == (Image *) NULL)
  3331. {
  3332. /*
  3333. Morph single image.
  3334. */
  3335. for (n=1; n < (ssize_t) number_frames; n++)
  3336. {
  3337. morph_image=CloneImage(image,0,0,MagickTrue,exception);
  3338. if (morph_image == (Image *) NULL)
  3339. {
  3340. morph_images=DestroyImageList(morph_images);
  3341. return((Image *) NULL);
  3342. }
  3343. AppendImageToList(&morph_images,morph_image);
  3344. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  3345. {
  3346. MagickBooleanType
  3347. proceed;
  3348. proceed=SetImageProgress(image,MorphImageTag,(MagickOffsetType) n,
  3349. number_frames);
  3350. if (proceed == MagickFalse)
  3351. status=MagickFalse;
  3352. }
  3353. }
  3354. return(GetFirstImageInList(morph_images));
  3355. }
  3356. /*
  3357. Morph image sequence.
  3358. */
  3359. status=MagickTrue;
  3360. scene=0;
  3361. next=image;
  3362. for ( ; GetNextImageInList(next) != (Image *) NULL; next=GetNextImageInList(next))
  3363. {
  3364. for (n=0; n < (ssize_t) number_frames; n++)
  3365. {
  3366. CacheView
  3367. *image_view,
  3368. *morph_view;
  3369. beta=(double) (n+1.0)/(double) (number_frames+1.0);
  3370. alpha=1.0-beta;
  3371. morph_image=ResizeImage(next,(size_t) (alpha*next->columns+beta*
  3372. GetNextImageInList(next)->columns+0.5),(size_t) (alpha*next->rows+beta*
  3373. GetNextImageInList(next)->rows+0.5),next->filter,exception);
  3374. if (morph_image == (Image *) NULL)
  3375. {
  3376. morph_images=DestroyImageList(morph_images);
  3377. return((Image *) NULL);
  3378. }
  3379. status=SetImageStorageClass(morph_image,DirectClass,exception);
  3380. if (status == MagickFalse)
  3381. {
  3382. morph_image=DestroyImage(morph_image);
  3383. return((Image *) NULL);
  3384. }
  3385. AppendImageToList(&morph_images,morph_image);
  3386. morph_images=GetLastImageInList(morph_images);
  3387. morph_image=ResizeImage(GetNextImageInList(next),morph_images->columns,
  3388. morph_images->rows,GetNextImageInList(next)->filter,exception);
  3389. if (morph_image == (Image *) NULL)
  3390. {
  3391. morph_images=DestroyImageList(morph_images);
  3392. return((Image *) NULL);
  3393. }
  3394. image_view=AcquireVirtualCacheView(morph_image,exception);
  3395. morph_view=AcquireAuthenticCacheView(morph_images,exception);
  3396. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  3397. #pragma omp parallel for schedule(static) shared(status) \
  3398. magick_number_threads(morph_image,morph_image,morph_image->rows,1)
  3399. #endif
  3400. for (y=0; y < (ssize_t) morph_images->rows; y++)
  3401. {
  3402. MagickBooleanType
  3403. sync;
  3404. register const Quantum
  3405. *magick_restrict p;
  3406. register ssize_t
  3407. x;
  3408. register Quantum
  3409. *magick_restrict q;
  3410. if (status == MagickFalse)
  3411. continue;
  3412. p=GetCacheViewVirtualPixels(image_view,0,y,morph_image->columns,1,
  3413. exception);
  3414. q=GetCacheViewAuthenticPixels(morph_view,0,y,morph_images->columns,1,
  3415. exception);
  3416. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  3417. {
  3418. status=MagickFalse;
  3419. continue;
  3420. }
  3421. for (x=0; x < (ssize_t) morph_images->columns; x++)
  3422. {
  3423. register ssize_t
  3424. i;
  3425. for (i=0; i < (ssize_t) GetPixelChannels(morph_image); i++)
  3426. {
  3427. PixelChannel channel = GetPixelChannelChannel(morph_image,i);
  3428. PixelTrait traits = GetPixelChannelTraits(morph_image,channel);
  3429. PixelTrait morph_traits=GetPixelChannelTraits(morph_images,channel);
  3430. if ((traits == UndefinedPixelTrait) ||
  3431. (morph_traits == UndefinedPixelTrait))
  3432. continue;
  3433. if ((morph_traits & CopyPixelTrait) != 0)
  3434. {
  3435. SetPixelChannel(morph_image,channel,p[i],q);
  3436. continue;
  3437. }
  3438. SetPixelChannel(morph_image,channel,ClampToQuantum(alpha*
  3439. GetPixelChannel(morph_images,channel,q)+beta*p[i]),q);
  3440. }
  3441. p+=GetPixelChannels(morph_image);
  3442. q+=GetPixelChannels(morph_images);
  3443. }
  3444. sync=SyncCacheViewAuthenticPixels(morph_view,exception);
  3445. if (sync == MagickFalse)
  3446. status=MagickFalse;
  3447. }
  3448. morph_view=DestroyCacheView(morph_view);
  3449. image_view=DestroyCacheView(image_view);
  3450. morph_image=DestroyImage(morph_image);
  3451. }
  3452. if (n < (ssize_t) number_frames)
  3453. break;
  3454. /*
  3455. Clone last frame in sequence.
  3456. */
  3457. morph_image=CloneImage(GetNextImageInList(next),0,0,MagickTrue,exception);
  3458. if (morph_image == (Image *) NULL)
  3459. {
  3460. morph_images=DestroyImageList(morph_images);
  3461. return((Image *) NULL);
  3462. }
  3463. AppendImageToList(&morph_images,morph_image);
  3464. morph_images=GetLastImageInList(morph_images);
  3465. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  3466. {
  3467. MagickBooleanType
  3468. proceed;
  3469. proceed=SetImageProgress(image,MorphImageTag,scene,
  3470. GetImageListLength(image));
  3471. if (proceed == MagickFalse)
  3472. status=MagickFalse;
  3473. }
  3474. scene++;
  3475. }
  3476. if (GetNextImageInList(next) != (Image *) NULL)
  3477. {
  3478. morph_images=DestroyImageList(morph_images);
  3479. return((Image *) NULL);
  3480. }
  3481. return(GetFirstImageInList(morph_images));
  3482. }
  3483. /*
  3484. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3485. % %
  3486. % %
  3487. % %
  3488. % P l a s m a I m a g e %
  3489. % %
  3490. % %
  3491. % %
  3492. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3493. %
  3494. % PlasmaImage() initializes an image with plasma fractal values. The image
  3495. % must be initialized with a base color and the random number generator
  3496. % seeded before this method is called.
  3497. %
  3498. % The format of the PlasmaImage method is:
  3499. %
  3500. % MagickBooleanType PlasmaImage(Image *image,const SegmentInfo *segment,
  3501. % size_t attenuate,size_t depth,ExceptionInfo *exception)
  3502. %
  3503. % A description of each parameter follows:
  3504. %
  3505. % o image: the image.
  3506. %
  3507. % o segment: Define the region to apply plasma fractals values.
  3508. %
  3509. % o attenuate: Define the plasma attenuation factor.
  3510. %
  3511. % o depth: Limit the plasma recursion depth.
  3512. %
  3513. % o exception: return any errors or warnings in this structure.
  3514. %
  3515. */
  3516. static inline Quantum PlasmaPixel(RandomInfo *random_info,
  3517. const double pixel,const double noise)
  3518. {
  3519. Quantum
  3520. plasma;
  3521. plasma=ClampToQuantum(pixel+noise*GetPseudoRandomValue(random_info)-
  3522. noise/2.0);
  3523. if (plasma <= 0)
  3524. return((Quantum) 0);
  3525. if (plasma >= QuantumRange)
  3526. return(QuantumRange);
  3527. return(plasma);
  3528. }
  3529. static MagickBooleanType PlasmaImageProxy(Image *image,CacheView *image_view,
  3530. CacheView *u_view,CacheView *v_view,RandomInfo *random_info,
  3531. const SegmentInfo *segment,size_t attenuate,size_t depth,
  3532. ExceptionInfo *exception)
  3533. {
  3534. double
  3535. plasma;
  3536. register const Quantum
  3537. *magick_restrict u,
  3538. *magick_restrict v;
  3539. register Quantum
  3540. *magick_restrict q;
  3541. register ssize_t
  3542. i;
  3543. ssize_t
  3544. x,
  3545. x_mid,
  3546. y,
  3547. y_mid;
  3548. if ((fabs(segment->x2-segment->x1) <= MagickEpsilon) &&
  3549. (fabs(segment->y2-segment->y1) <= MagickEpsilon))
  3550. return(MagickTrue);
  3551. if (depth != 0)
  3552. {
  3553. MagickBooleanType
  3554. status;
  3555. SegmentInfo
  3556. local_info;
  3557. /*
  3558. Divide the area into quadrants and recurse.
  3559. */
  3560. depth--;
  3561. attenuate++;
  3562. x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
  3563. y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
  3564. local_info=(*segment);
  3565. local_info.x2=(double) x_mid;
  3566. local_info.y2=(double) y_mid;
  3567. (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
  3568. &local_info,attenuate,depth,exception);
  3569. local_info=(*segment);
  3570. local_info.y1=(double) y_mid;
  3571. local_info.x2=(double) x_mid;
  3572. (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
  3573. &local_info,attenuate,depth,exception);
  3574. local_info=(*segment);
  3575. local_info.x1=(double) x_mid;
  3576. local_info.y2=(double) y_mid;
  3577. (void) PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
  3578. &local_info,attenuate,depth,exception);
  3579. local_info=(*segment);
  3580. local_info.x1=(double) x_mid;
  3581. local_info.y1=(double) y_mid;
  3582. status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,
  3583. &local_info,attenuate,depth,exception);
  3584. return(status);
  3585. }
  3586. x_mid=(ssize_t) ceil((segment->x1+segment->x2)/2-0.5);
  3587. y_mid=(ssize_t) ceil((segment->y1+segment->y2)/2-0.5);
  3588. if ((fabs(segment->x1-x_mid) < MagickEpsilon) &&
  3589. (fabs(segment->x2-x_mid) < MagickEpsilon) &&
  3590. (fabs(segment->y1-y_mid) < MagickEpsilon) &&
  3591. (fabs(segment->y2-y_mid) < MagickEpsilon))
  3592. return(MagickFalse);
  3593. /*
  3594. Average pixels and apply plasma.
  3595. */
  3596. plasma=(double) QuantumRange/(2.0*attenuate);
  3597. if ((fabs(segment->x1-x_mid) > MagickEpsilon) ||
  3598. (fabs(segment->x2-x_mid) > MagickEpsilon))
  3599. {
  3600. /*
  3601. Left pixel.
  3602. */
  3603. x=(ssize_t) ceil(segment->x1-0.5);
  3604. u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),1,1,
  3605. exception);
  3606. v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),1,1,
  3607. exception);
  3608. q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
  3609. if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
  3610. (q == (Quantum *) NULL))
  3611. return(MagickTrue);
  3612. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  3613. {
  3614. PixelChannel channel = GetPixelChannelChannel(image,i);
  3615. PixelTrait traits = GetPixelChannelTraits(image,channel);
  3616. if (traits == UndefinedPixelTrait)
  3617. continue;
  3618. q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
  3619. }
  3620. (void) SyncCacheViewAuthenticPixels(image_view,exception);
  3621. if (fabs(segment->x1-segment->x2) > MagickEpsilon)
  3622. {
  3623. /*
  3624. Right pixel.
  3625. */
  3626. x=(ssize_t) ceil(segment->x2-0.5);
  3627. u=GetCacheViewVirtualPixels(u_view,x,(ssize_t) ceil(segment->y1-0.5),
  3628. 1,1,exception);
  3629. v=GetCacheViewVirtualPixels(v_view,x,(ssize_t) ceil(segment->y2-0.5),
  3630. 1,1,exception);
  3631. q=QueueCacheViewAuthenticPixels(image_view,x,y_mid,1,1,exception);
  3632. if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
  3633. (q == (Quantum *) NULL))
  3634. return(MagickTrue);
  3635. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  3636. {
  3637. PixelChannel channel = GetPixelChannelChannel(image,i);
  3638. PixelTrait traits = GetPixelChannelTraits(image,channel);
  3639. if (traits == UndefinedPixelTrait)
  3640. continue;
  3641. q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
  3642. }
  3643. (void) SyncCacheViewAuthenticPixels(image_view,exception);
  3644. }
  3645. }
  3646. if ((fabs(segment->y1-y_mid) > MagickEpsilon) ||
  3647. (fabs(segment->y2-y_mid) > MagickEpsilon))
  3648. {
  3649. if ((fabs(segment->x1-x_mid) > MagickEpsilon) ||
  3650. (fabs(segment->y2-y_mid) > MagickEpsilon))
  3651. {
  3652. /*
  3653. Bottom pixel.
  3654. */
  3655. y=(ssize_t) ceil(segment->y2-0.5);
  3656. u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
  3657. 1,1,exception);
  3658. v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
  3659. 1,1,exception);
  3660. q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
  3661. if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
  3662. (q == (Quantum *) NULL))
  3663. return(MagickTrue);
  3664. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  3665. {
  3666. PixelChannel channel = GetPixelChannelChannel(image,i);
  3667. PixelTrait traits = GetPixelChannelTraits(image,channel);
  3668. if (traits == UndefinedPixelTrait)
  3669. continue;
  3670. q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
  3671. }
  3672. (void) SyncCacheViewAuthenticPixels(image_view,exception);
  3673. }
  3674. if (fabs(segment->y1-segment->y2) > MagickEpsilon)
  3675. {
  3676. /*
  3677. Top pixel.
  3678. */
  3679. y=(ssize_t) ceil(segment->y1-0.5);
  3680. u=GetCacheViewVirtualPixels(u_view,(ssize_t) ceil(segment->x1-0.5),y,
  3681. 1,1,exception);
  3682. v=GetCacheViewVirtualPixels(v_view,(ssize_t) ceil(segment->x2-0.5),y,
  3683. 1,1,exception);
  3684. q=QueueCacheViewAuthenticPixels(image_view,x_mid,y,1,1,exception);
  3685. if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
  3686. (q == (Quantum *) NULL))
  3687. return(MagickTrue);
  3688. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  3689. {
  3690. PixelChannel channel = GetPixelChannelChannel(image,i);
  3691. PixelTrait traits = GetPixelChannelTraits(image,channel);
  3692. if (traits == UndefinedPixelTrait)
  3693. continue;
  3694. q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
  3695. }
  3696. (void) SyncCacheViewAuthenticPixels(image_view,exception);
  3697. }
  3698. }
  3699. if ((fabs(segment->x1-segment->x2) > MagickEpsilon) ||
  3700. (fabs(segment->y1-segment->y2) > MagickEpsilon))
  3701. {
  3702. /*
  3703. Middle pixel.
  3704. */
  3705. x=(ssize_t) ceil(segment->x1-0.5);
  3706. y=(ssize_t) ceil(segment->y1-0.5);
  3707. u=GetCacheViewVirtualPixels(u_view,x,y,1,1,exception);
  3708. x=(ssize_t) ceil(segment->x2-0.5);
  3709. y=(ssize_t) ceil(segment->y2-0.5);
  3710. v=GetCacheViewVirtualPixels(v_view,x,y,1,1,exception);
  3711. q=QueueCacheViewAuthenticPixels(image_view,x_mid,y_mid,1,1,exception);
  3712. if ((u == (const Quantum *) NULL) || (v == (const Quantum *) NULL) ||
  3713. (q == (Quantum *) NULL))
  3714. return(MagickTrue);
  3715. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  3716. {
  3717. PixelChannel channel = GetPixelChannelChannel(image,i);
  3718. PixelTrait traits = GetPixelChannelTraits(image,channel);
  3719. if (traits == UndefinedPixelTrait)
  3720. continue;
  3721. q[i]=PlasmaPixel(random_info,(u[i]+v[i])/2.0,plasma);
  3722. }
  3723. (void) SyncCacheViewAuthenticPixels(image_view,exception);
  3724. }
  3725. if ((fabs(segment->x2-segment->x1) < 3.0) &&
  3726. (fabs(segment->y2-segment->y1) < 3.0))
  3727. return(MagickTrue);
  3728. return(MagickFalse);
  3729. }
  3730. MagickExport MagickBooleanType PlasmaImage(Image *image,
  3731. const SegmentInfo *segment,size_t attenuate,size_t depth,
  3732. ExceptionInfo *exception)
  3733. {
  3734. CacheView
  3735. *image_view,
  3736. *u_view,
  3737. *v_view;
  3738. MagickBooleanType
  3739. status;
  3740. RandomInfo
  3741. *random_info;
  3742. assert(image != (Image *) NULL);
  3743. if (image->debug != MagickFalse)
  3744. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  3745. assert(image->signature == MagickCoreSignature);
  3746. if (image->debug != MagickFalse)
  3747. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
  3748. if (SetImageStorageClass(image,DirectClass,exception) == MagickFalse)
  3749. return(MagickFalse);
  3750. image_view=AcquireAuthenticCacheView(image,exception);
  3751. u_view=AcquireVirtualCacheView(image,exception);
  3752. v_view=AcquireVirtualCacheView(image,exception);
  3753. random_info=AcquireRandomInfo();
  3754. status=PlasmaImageProxy(image,image_view,u_view,v_view,random_info,segment,
  3755. attenuate,depth,exception);
  3756. random_info=DestroyRandomInfo(random_info);
  3757. v_view=DestroyCacheView(v_view);
  3758. u_view=DestroyCacheView(u_view);
  3759. image_view=DestroyCacheView(image_view);
  3760. return(status);
  3761. }
  3762. /*
  3763. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3764. % %
  3765. % %
  3766. % %
  3767. % P o l a r o i d I m a g e %
  3768. % %
  3769. % %
  3770. % %
  3771. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3772. %
  3773. % PolaroidImage() simulates a Polaroid picture.
  3774. %
  3775. % The format of the PolaroidImage method is:
  3776. %
  3777. % Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
  3778. % const char *caption,const double angle,
  3779. % const PixelInterpolateMethod method,ExceptionInfo exception)
  3780. %
  3781. % A description of each parameter follows:
  3782. %
  3783. % o image: the image.
  3784. %
  3785. % o draw_info: the draw info.
  3786. %
  3787. % o caption: the Polaroid caption.
  3788. %
  3789. % o angle: Apply the effect along this angle.
  3790. %
  3791. % o method: the pixel interpolation method.
  3792. %
  3793. % o exception: return any errors or warnings in this structure.
  3794. %
  3795. */
  3796. MagickExport Image *PolaroidImage(const Image *image,const DrawInfo *draw_info,
  3797. const char *caption,const double angle,const PixelInterpolateMethod method,
  3798. ExceptionInfo *exception)
  3799. {
  3800. Image
  3801. *bend_image,
  3802. *caption_image,
  3803. *flop_image,
  3804. *picture_image,
  3805. *polaroid_image,
  3806. *rotate_image,
  3807. *trim_image;
  3808. size_t
  3809. height;
  3810. ssize_t
  3811. quantum;
  3812. /*
  3813. Simulate a Polaroid picture.
  3814. */
  3815. assert(image != (Image *) NULL);
  3816. assert(image->signature == MagickCoreSignature);
  3817. if (image->debug != MagickFalse)
  3818. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  3819. assert(exception != (ExceptionInfo *) NULL);
  3820. assert(exception->signature == MagickCoreSignature);
  3821. quantum=(ssize_t) MagickMax(MagickMax((double) image->columns,(double)
  3822. image->rows)/25.0,10.0);
  3823. height=image->rows+2*quantum;
  3824. caption_image=(Image *) NULL;
  3825. if (caption != (const char *) NULL)
  3826. {
  3827. char
  3828. *text;
  3829. /*
  3830. Generate caption image.
  3831. */
  3832. caption_image=CloneImage(image,image->columns,1,MagickTrue,exception);
  3833. if (caption_image == (Image *) NULL)
  3834. return((Image *) NULL);
  3835. text=InterpretImageProperties((ImageInfo *) NULL,(Image *) image,caption,
  3836. exception);
  3837. if (text != (char *) NULL)
  3838. {
  3839. char
  3840. geometry[MagickPathExtent];
  3841. DrawInfo
  3842. *annotate_info;
  3843. MagickBooleanType
  3844. status;
  3845. ssize_t
  3846. count;
  3847. TypeMetric
  3848. metrics;
  3849. annotate_info=CloneDrawInfo((const ImageInfo *) NULL,draw_info);
  3850. (void) CloneString(&annotate_info->text,text);
  3851. count=FormatMagickCaption(caption_image,annotate_info,MagickTrue,
  3852. &metrics,&text,exception);
  3853. status=SetImageExtent(caption_image,image->columns,(size_t)
  3854. ((count+1)*(metrics.ascent-metrics.descent)+0.5),exception);
  3855. if (status == MagickFalse)
  3856. caption_image=DestroyImage(caption_image);
  3857. else
  3858. {
  3859. caption_image->background_color=image->border_color;
  3860. (void) SetImageBackgroundColor(caption_image,exception);
  3861. (void) CloneString(&annotate_info->text,text);
  3862. (void) FormatLocaleString(geometry,MagickPathExtent,"+0+%.20g",
  3863. metrics.ascent);
  3864. if (annotate_info->gravity == UndefinedGravity)
  3865. (void) CloneString(&annotate_info->geometry,AcquireString(
  3866. geometry));
  3867. (void) AnnotateImage(caption_image,annotate_info,exception);
  3868. height+=caption_image->rows;
  3869. }
  3870. annotate_info=DestroyDrawInfo(annotate_info);
  3871. text=DestroyString(text);
  3872. }
  3873. }
  3874. picture_image=CloneImage(image,image->columns+2*quantum,height,MagickTrue,
  3875. exception);
  3876. if (picture_image == (Image *) NULL)
  3877. {
  3878. if (caption_image != (Image *) NULL)
  3879. caption_image=DestroyImage(caption_image);
  3880. return((Image *) NULL);
  3881. }
  3882. picture_image->background_color=image->border_color;
  3883. (void) SetImageBackgroundColor(picture_image,exception);
  3884. (void) CompositeImage(picture_image,image,OverCompositeOp,MagickTrue,quantum,
  3885. quantum,exception);
  3886. if (caption_image != (Image *) NULL)
  3887. {
  3888. (void) CompositeImage(picture_image,caption_image,OverCompositeOp,
  3889. MagickTrue,quantum,(ssize_t) (image->rows+3*quantum/2),exception);
  3890. caption_image=DestroyImage(caption_image);
  3891. }
  3892. (void) QueryColorCompliance("none",AllCompliance,
  3893. &picture_image->background_color,exception);
  3894. (void) SetImageAlphaChannel(picture_image,OpaqueAlphaChannel,exception);
  3895. rotate_image=RotateImage(picture_image,90.0,exception);
  3896. picture_image=DestroyImage(picture_image);
  3897. if (rotate_image == (Image *) NULL)
  3898. return((Image *) NULL);
  3899. picture_image=rotate_image;
  3900. bend_image=WaveImage(picture_image,0.01*picture_image->rows,2.0*
  3901. picture_image->columns,method,exception);
  3902. picture_image=DestroyImage(picture_image);
  3903. if (bend_image == (Image *) NULL)
  3904. return((Image *) NULL);
  3905. picture_image=bend_image;
  3906. rotate_image=RotateImage(picture_image,-90.0,exception);
  3907. picture_image=DestroyImage(picture_image);
  3908. if (rotate_image == (Image *) NULL)
  3909. return((Image *) NULL);
  3910. picture_image=rotate_image;
  3911. picture_image->background_color=image->background_color;
  3912. polaroid_image=ShadowImage(picture_image,80.0,2.0,quantum/3,quantum/3,
  3913. exception);
  3914. if (polaroid_image == (Image *) NULL)
  3915. {
  3916. picture_image=DestroyImage(picture_image);
  3917. return(picture_image);
  3918. }
  3919. flop_image=FlopImage(polaroid_image,exception);
  3920. polaroid_image=DestroyImage(polaroid_image);
  3921. if (flop_image == (Image *) NULL)
  3922. {
  3923. picture_image=DestroyImage(picture_image);
  3924. return(picture_image);
  3925. }
  3926. polaroid_image=flop_image;
  3927. (void) CompositeImage(polaroid_image,picture_image,OverCompositeOp,
  3928. MagickTrue,(ssize_t) (-0.01*picture_image->columns/2.0),0L,exception);
  3929. picture_image=DestroyImage(picture_image);
  3930. (void) QueryColorCompliance("none",AllCompliance,
  3931. &polaroid_image->background_color,exception);
  3932. rotate_image=RotateImage(polaroid_image,angle,exception);
  3933. polaroid_image=DestroyImage(polaroid_image);
  3934. if (rotate_image == (Image *) NULL)
  3935. return((Image *) NULL);
  3936. polaroid_image=rotate_image;
  3937. trim_image=TrimImage(polaroid_image,exception);
  3938. polaroid_image=DestroyImage(polaroid_image);
  3939. if (trim_image == (Image *) NULL)
  3940. return((Image *) NULL);
  3941. polaroid_image=trim_image;
  3942. return(polaroid_image);
  3943. }
  3944. /*
  3945. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3946. % %
  3947. % %
  3948. % %
  3949. % S e p i a T o n e I m a g e %
  3950. % %
  3951. % %
  3952. % %
  3953. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3954. %
  3955. % MagickSepiaToneImage() applies a special effect to the image, similar to the
  3956. % effect achieved in a photo darkroom by sepia toning. Threshold ranges from
  3957. % 0 to QuantumRange and is a measure of the extent of the sepia toning. A
  3958. % threshold of 80% is a good starting point for a reasonable tone.
  3959. %
  3960. % The format of the SepiaToneImage method is:
  3961. %
  3962. % Image *SepiaToneImage(const Image *image,const double threshold,
  3963. % ExceptionInfo *exception)
  3964. %
  3965. % A description of each parameter follows:
  3966. %
  3967. % o image: the image.
  3968. %
  3969. % o threshold: the tone threshold.
  3970. %
  3971. % o exception: return any errors or warnings in this structure.
  3972. %
  3973. */
  3974. MagickExport Image *SepiaToneImage(const Image *image,const double threshold,
  3975. ExceptionInfo *exception)
  3976. {
  3977. #define SepiaToneImageTag "SepiaTone/Image"
  3978. CacheView
  3979. *image_view,
  3980. *sepia_view;
  3981. Image
  3982. *sepia_image;
  3983. MagickBooleanType
  3984. status;
  3985. MagickOffsetType
  3986. progress;
  3987. ssize_t
  3988. y;
  3989. /*
  3990. Initialize sepia-toned image attributes.
  3991. */
  3992. assert(image != (const Image *) NULL);
  3993. assert(image->signature == MagickCoreSignature);
  3994. if (image->debug != MagickFalse)
  3995. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  3996. assert(exception != (ExceptionInfo *) NULL);
  3997. assert(exception->signature == MagickCoreSignature);
  3998. sepia_image=CloneImage(image,0,0,MagickTrue,exception);
  3999. if (sepia_image == (Image *) NULL)
  4000. return((Image *) NULL);
  4001. if (SetImageStorageClass(sepia_image,DirectClass,exception) == MagickFalse)
  4002. {
  4003. sepia_image=DestroyImage(sepia_image);
  4004. return((Image *) NULL);
  4005. }
  4006. /*
  4007. Tone each row of the image.
  4008. */
  4009. status=MagickTrue;
  4010. progress=0;
  4011. image_view=AcquireVirtualCacheView(image,exception);
  4012. sepia_view=AcquireAuthenticCacheView(sepia_image,exception);
  4013. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4014. #pragma omp parallel for schedule(static) shared(progress,status) \
  4015. magick_number_threads(image,sepia_image,image->rows,1)
  4016. #endif
  4017. for (y=0; y < (ssize_t) image->rows; y++)
  4018. {
  4019. register const Quantum
  4020. *magick_restrict p;
  4021. register ssize_t
  4022. x;
  4023. register Quantum
  4024. *magick_restrict q;
  4025. if (status == MagickFalse)
  4026. continue;
  4027. p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
  4028. q=GetCacheViewAuthenticPixels(sepia_view,0,y,sepia_image->columns,1,
  4029. exception);
  4030. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  4031. {
  4032. status=MagickFalse;
  4033. continue;
  4034. }
  4035. for (x=0; x < (ssize_t) image->columns; x++)
  4036. {
  4037. double
  4038. intensity,
  4039. tone;
  4040. intensity=GetPixelIntensity(image,p);
  4041. tone=intensity > threshold ? (double) QuantumRange : intensity+
  4042. (double) QuantumRange-threshold;
  4043. SetPixelRed(sepia_image,ClampToQuantum(tone),q);
  4044. tone=intensity > (7.0*threshold/6.0) ? (double) QuantumRange :
  4045. intensity+(double) QuantumRange-7.0*threshold/6.0;
  4046. SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
  4047. tone=intensity < (threshold/6.0) ? 0 : intensity-threshold/6.0;
  4048. SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
  4049. tone=threshold/7.0;
  4050. if ((double) GetPixelGreen(image,q) < tone)
  4051. SetPixelGreen(sepia_image,ClampToQuantum(tone),q);
  4052. if ((double) GetPixelBlue(image,q) < tone)
  4053. SetPixelBlue(sepia_image,ClampToQuantum(tone),q);
  4054. SetPixelAlpha(sepia_image,GetPixelAlpha(image,p),q);
  4055. p+=GetPixelChannels(image);
  4056. q+=GetPixelChannels(sepia_image);
  4057. }
  4058. if (SyncCacheViewAuthenticPixels(sepia_view,exception) == MagickFalse)
  4059. status=MagickFalse;
  4060. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  4061. {
  4062. MagickBooleanType
  4063. proceed;
  4064. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4065. #pragma omp atomic
  4066. #endif
  4067. progress++;
  4068. proceed=SetImageProgress(image,SepiaToneImageTag,progress,image->rows);
  4069. if (proceed == MagickFalse)
  4070. status=MagickFalse;
  4071. }
  4072. }
  4073. sepia_view=DestroyCacheView(sepia_view);
  4074. image_view=DestroyCacheView(image_view);
  4075. (void) NormalizeImage(sepia_image,exception);
  4076. (void) ContrastImage(sepia_image,MagickTrue,exception);
  4077. if (status == MagickFalse)
  4078. sepia_image=DestroyImage(sepia_image);
  4079. return(sepia_image);
  4080. }
  4081. /*
  4082. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4083. % %
  4084. % %
  4085. % %
  4086. % S h a d o w I m a g e %
  4087. % %
  4088. % %
  4089. % %
  4090. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4091. %
  4092. % ShadowImage() simulates a shadow from the specified image and returns it.
  4093. %
  4094. % The format of the ShadowImage method is:
  4095. %
  4096. % Image *ShadowImage(const Image *image,const double alpha,
  4097. % const double sigma,const ssize_t x_offset,const ssize_t y_offset,
  4098. % ExceptionInfo *exception)
  4099. %
  4100. % A description of each parameter follows:
  4101. %
  4102. % o image: the image.
  4103. %
  4104. % o alpha: percentage transparency.
  4105. %
  4106. % o sigma: the standard deviation of the Gaussian, in pixels.
  4107. %
  4108. % o x_offset: the shadow x-offset.
  4109. %
  4110. % o y_offset: the shadow y-offset.
  4111. %
  4112. % o exception: return any errors or warnings in this structure.
  4113. %
  4114. */
  4115. MagickExport Image *ShadowImage(const Image *image,const double alpha,
  4116. const double sigma,const ssize_t x_offset,const ssize_t y_offset,
  4117. ExceptionInfo *exception)
  4118. {
  4119. #define ShadowImageTag "Shadow/Image"
  4120. CacheView
  4121. *image_view;
  4122. ChannelType
  4123. channel_mask;
  4124. Image
  4125. *border_image,
  4126. *clone_image,
  4127. *shadow_image;
  4128. MagickBooleanType
  4129. status;
  4130. PixelInfo
  4131. background_color;
  4132. RectangleInfo
  4133. border_info;
  4134. ssize_t
  4135. y;
  4136. assert(image != (Image *) NULL);
  4137. assert(image->signature == MagickCoreSignature);
  4138. if (image->debug != MagickFalse)
  4139. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  4140. assert(exception != (ExceptionInfo *) NULL);
  4141. assert(exception->signature == MagickCoreSignature);
  4142. clone_image=CloneImage(image,0,0,MagickTrue,exception);
  4143. if (clone_image == (Image *) NULL)
  4144. return((Image *) NULL);
  4145. if (IsGrayColorspace(image->colorspace) != MagickFalse)
  4146. (void) SetImageColorspace(clone_image,sRGBColorspace,exception);
  4147. (void) SetImageVirtualPixelMethod(clone_image,EdgeVirtualPixelMethod,
  4148. exception);
  4149. border_info.width=(size_t) floor(2.0*sigma+0.5);
  4150. border_info.height=(size_t) floor(2.0*sigma+0.5);
  4151. border_info.x=0;
  4152. border_info.y=0;
  4153. (void) QueryColorCompliance("none",AllCompliance,&clone_image->border_color,
  4154. exception);
  4155. clone_image->alpha_trait=BlendPixelTrait;
  4156. border_image=BorderImage(clone_image,&border_info,OverCompositeOp,exception);
  4157. clone_image=DestroyImage(clone_image);
  4158. if (border_image == (Image *) NULL)
  4159. return((Image *) NULL);
  4160. if (border_image->alpha_trait == UndefinedPixelTrait)
  4161. (void) SetImageAlphaChannel(border_image,OpaqueAlphaChannel,exception);
  4162. /*
  4163. Shadow image.
  4164. */
  4165. status=MagickTrue;
  4166. background_color=border_image->background_color;
  4167. background_color.alpha_trait=BlendPixelTrait;
  4168. image_view=AcquireAuthenticCacheView(border_image,exception);
  4169. for (y=0; y < (ssize_t) border_image->rows; y++)
  4170. {
  4171. register Quantum
  4172. *magick_restrict q;
  4173. register ssize_t
  4174. x;
  4175. if (status == MagickFalse)
  4176. continue;
  4177. q=QueueCacheViewAuthenticPixels(image_view,0,y,border_image->columns,1,
  4178. exception);
  4179. if (q == (Quantum *) NULL)
  4180. {
  4181. status=MagickFalse;
  4182. continue;
  4183. }
  4184. for (x=0; x < (ssize_t) border_image->columns; x++)
  4185. {
  4186. if (border_image->alpha_trait != UndefinedPixelTrait)
  4187. background_color.alpha=GetPixelAlpha(border_image,q)*alpha/100.0;
  4188. SetPixelViaPixelInfo(border_image,&background_color,q);
  4189. q+=GetPixelChannels(border_image);
  4190. }
  4191. if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
  4192. status=MagickFalse;
  4193. }
  4194. image_view=DestroyCacheView(image_view);
  4195. if (status == MagickFalse)
  4196. {
  4197. border_image=DestroyImage(border_image);
  4198. return((Image *) NULL);
  4199. }
  4200. channel_mask=SetImageChannelMask(border_image,AlphaChannel);
  4201. shadow_image=BlurImage(border_image,0.0,sigma,exception);
  4202. border_image=DestroyImage(border_image);
  4203. if (shadow_image == (Image *) NULL)
  4204. return((Image *) NULL);
  4205. (void) SetPixelChannelMask(shadow_image,channel_mask);
  4206. if (shadow_image->page.width == 0)
  4207. shadow_image->page.width=shadow_image->columns;
  4208. if (shadow_image->page.height == 0)
  4209. shadow_image->page.height=shadow_image->rows;
  4210. shadow_image->page.width+=x_offset-(ssize_t) border_info.width;
  4211. shadow_image->page.height+=y_offset-(ssize_t) border_info.height;
  4212. shadow_image->page.x+=x_offset-(ssize_t) border_info.width;
  4213. shadow_image->page.y+=y_offset-(ssize_t) border_info.height;
  4214. return(shadow_image);
  4215. }
  4216. /*
  4217. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4218. % %
  4219. % %
  4220. % %
  4221. % S k e t c h I m a g e %
  4222. % %
  4223. % %
  4224. % %
  4225. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4226. %
  4227. % SketchImage() simulates a pencil sketch. We convolve the image with a
  4228. % Gaussian operator of the given radius and standard deviation (sigma). For
  4229. % reasonable results, radius should be larger than sigma. Use a radius of 0
  4230. % and SketchImage() selects a suitable radius for you. Angle gives the angle
  4231. % of the sketch.
  4232. %
  4233. % The format of the SketchImage method is:
  4234. %
  4235. % Image *SketchImage(const Image *image,const double radius,
  4236. % const double sigma,const double angle,ExceptionInfo *exception)
  4237. %
  4238. % A description of each parameter follows:
  4239. %
  4240. % o image: the image.
  4241. %
  4242. % o radius: the radius of the Gaussian, in pixels, not counting the
  4243. % center pixel.
  4244. %
  4245. % o sigma: the standard deviation of the Gaussian, in pixels.
  4246. %
  4247. % o angle: apply the effect along this angle.
  4248. %
  4249. % o exception: return any errors or warnings in this structure.
  4250. %
  4251. */
  4252. MagickExport Image *SketchImage(const Image *image,const double radius,
  4253. const double sigma,const double angle,ExceptionInfo *exception)
  4254. {
  4255. CacheView
  4256. *random_view;
  4257. Image
  4258. *blend_image,
  4259. *blur_image,
  4260. *dodge_image,
  4261. *random_image,
  4262. *sketch_image;
  4263. MagickBooleanType
  4264. status;
  4265. RandomInfo
  4266. **magick_restrict random_info;
  4267. ssize_t
  4268. y;
  4269. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4270. unsigned long
  4271. key;
  4272. #endif
  4273. /*
  4274. Sketch image.
  4275. */
  4276. random_image=CloneImage(image,image->columns << 1,image->rows << 1,
  4277. MagickTrue,exception);
  4278. if (random_image == (Image *) NULL)
  4279. return((Image *) NULL);
  4280. status=MagickTrue;
  4281. random_info=AcquireRandomInfoThreadSet();
  4282. random_view=AcquireAuthenticCacheView(random_image,exception);
  4283. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4284. key=GetRandomSecretKey(random_info[0]);
  4285. #pragma omp parallel for schedule(static) shared(status) \
  4286. magick_number_threads(random_image,random_image,random_image->rows,key == ~0UL)
  4287. #endif
  4288. for (y=0; y < (ssize_t) random_image->rows; y++)
  4289. {
  4290. const int
  4291. id = GetOpenMPThreadId();
  4292. register Quantum
  4293. *magick_restrict q;
  4294. register ssize_t
  4295. x;
  4296. if (status == MagickFalse)
  4297. continue;
  4298. q=QueueCacheViewAuthenticPixels(random_view,0,y,random_image->columns,1,
  4299. exception);
  4300. if (q == (Quantum *) NULL)
  4301. {
  4302. status=MagickFalse;
  4303. continue;
  4304. }
  4305. for (x=0; x < (ssize_t) random_image->columns; x++)
  4306. {
  4307. double
  4308. value;
  4309. register ssize_t
  4310. i;
  4311. value=GetPseudoRandomValue(random_info[id]);
  4312. for (i=0; i < (ssize_t) GetPixelChannels(random_image); i++)
  4313. {
  4314. PixelChannel channel = GetPixelChannelChannel(image,i);
  4315. PixelTrait traits = GetPixelChannelTraits(image,channel);
  4316. if (traits == UndefinedPixelTrait)
  4317. continue;
  4318. q[i]=ClampToQuantum(QuantumRange*value);
  4319. }
  4320. q+=GetPixelChannels(random_image);
  4321. }
  4322. if (SyncCacheViewAuthenticPixels(random_view,exception) == MagickFalse)
  4323. status=MagickFalse;
  4324. }
  4325. random_view=DestroyCacheView(random_view);
  4326. random_info=DestroyRandomInfoThreadSet(random_info);
  4327. if (status == MagickFalse)
  4328. {
  4329. random_image=DestroyImage(random_image);
  4330. return(random_image);
  4331. }
  4332. blur_image=MotionBlurImage(random_image,radius,sigma,angle,exception);
  4333. random_image=DestroyImage(random_image);
  4334. if (blur_image == (Image *) NULL)
  4335. return((Image *) NULL);
  4336. dodge_image=EdgeImage(blur_image,radius,exception);
  4337. blur_image=DestroyImage(blur_image);
  4338. if (dodge_image == (Image *) NULL)
  4339. return((Image *) NULL);
  4340. status=ClampImage(dodge_image,exception);
  4341. if (status != MagickFalse)
  4342. status=NormalizeImage(dodge_image,exception);
  4343. if (status != MagickFalse)
  4344. status=NegateImage(dodge_image,MagickFalse,exception);
  4345. if (status != MagickFalse)
  4346. status=TransformImage(&dodge_image,(char *) NULL,"50%",exception);
  4347. sketch_image=CloneImage(image,0,0,MagickTrue,exception);
  4348. if (sketch_image == (Image *) NULL)
  4349. {
  4350. dodge_image=DestroyImage(dodge_image);
  4351. return((Image *) NULL);
  4352. }
  4353. (void) CompositeImage(sketch_image,dodge_image,ColorDodgeCompositeOp,
  4354. MagickTrue,0,0,exception);
  4355. dodge_image=DestroyImage(dodge_image);
  4356. blend_image=CloneImage(image,0,0,MagickTrue,exception);
  4357. if (blend_image == (Image *) NULL)
  4358. {
  4359. sketch_image=DestroyImage(sketch_image);
  4360. return((Image *) NULL);
  4361. }
  4362. if (blend_image->alpha_trait != BlendPixelTrait)
  4363. (void) SetImageAlpha(blend_image,TransparentAlpha,exception);
  4364. (void) SetImageArtifact(blend_image,"compose:args","20x80");
  4365. (void) CompositeImage(sketch_image,blend_image,BlendCompositeOp,MagickTrue,
  4366. 0,0,exception);
  4367. blend_image=DestroyImage(blend_image);
  4368. return(sketch_image);
  4369. }
  4370. /*
  4371. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4372. % %
  4373. % %
  4374. % %
  4375. % S o l a r i z e I m a g e %
  4376. % %
  4377. % %
  4378. % %
  4379. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4380. %
  4381. % SolarizeImage() applies a special effect to the image, similar to the effect
  4382. % achieved in a photo darkroom by selectively exposing areas of photo
  4383. % sensitive paper to light. Threshold ranges from 0 to QuantumRange and is a
  4384. % measure of the extent of the solarization.
  4385. %
  4386. % The format of the SolarizeImage method is:
  4387. %
  4388. % MagickBooleanType SolarizeImage(Image *image,const double threshold,
  4389. % ExceptionInfo *exception)
  4390. %
  4391. % A description of each parameter follows:
  4392. %
  4393. % o image: the image.
  4394. %
  4395. % o threshold: Define the extent of the solarization.
  4396. %
  4397. % o exception: return any errors or warnings in this structure.
  4398. %
  4399. */
  4400. MagickExport MagickBooleanType SolarizeImage(Image *image,
  4401. const double threshold,ExceptionInfo *exception)
  4402. {
  4403. #define SolarizeImageTag "Solarize/Image"
  4404. CacheView
  4405. *image_view;
  4406. MagickBooleanType
  4407. status;
  4408. MagickOffsetType
  4409. progress;
  4410. ssize_t
  4411. y;
  4412. assert(image != (Image *) NULL);
  4413. assert(image->signature == MagickCoreSignature);
  4414. if (image->debug != MagickFalse)
  4415. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  4416. if (IsGrayColorspace(image->colorspace) != MagickFalse)
  4417. (void) SetImageColorspace(image,sRGBColorspace,exception);
  4418. if (image->storage_class == PseudoClass)
  4419. {
  4420. register ssize_t
  4421. i;
  4422. /*
  4423. Solarize colormap.
  4424. */
  4425. for (i=0; i < (ssize_t) image->colors; i++)
  4426. {
  4427. if ((double) image->colormap[i].red > threshold)
  4428. image->colormap[i].red=QuantumRange-image->colormap[i].red;
  4429. if ((double) image->colormap[i].green > threshold)
  4430. image->colormap[i].green=QuantumRange-image->colormap[i].green;
  4431. if ((double) image->colormap[i].blue > threshold)
  4432. image->colormap[i].blue=QuantumRange-image->colormap[i].blue;
  4433. }
  4434. }
  4435. /*
  4436. Solarize image.
  4437. */
  4438. status=MagickTrue;
  4439. progress=0;
  4440. image_view=AcquireAuthenticCacheView(image,exception);
  4441. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4442. #pragma omp parallel for schedule(static) shared(progress,status) \
  4443. magick_number_threads(image,image,image->rows,1)
  4444. #endif
  4445. for (y=0; y < (ssize_t) image->rows; y++)
  4446. {
  4447. register ssize_t
  4448. x;
  4449. register Quantum
  4450. *magick_restrict q;
  4451. if (status == MagickFalse)
  4452. continue;
  4453. q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
  4454. if (q == (Quantum *) NULL)
  4455. {
  4456. status=MagickFalse;
  4457. continue;
  4458. }
  4459. for (x=0; x < (ssize_t) image->columns; x++)
  4460. {
  4461. register ssize_t
  4462. i;
  4463. for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
  4464. {
  4465. PixelChannel channel = GetPixelChannelChannel(image,i);
  4466. PixelTrait traits = GetPixelChannelTraits(image,channel);
  4467. if ((traits & UpdatePixelTrait) == 0)
  4468. continue;
  4469. if ((double) q[i] > threshold)
  4470. q[i]=QuantumRange-q[i];
  4471. }
  4472. q+=GetPixelChannels(image);
  4473. }
  4474. if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
  4475. status=MagickFalse;
  4476. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  4477. {
  4478. MagickBooleanType
  4479. proceed;
  4480. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4481. #pragma omp atomic
  4482. #endif
  4483. progress++;
  4484. proceed=SetImageProgress(image,SolarizeImageTag,progress,image->rows);
  4485. if (proceed == MagickFalse)
  4486. status=MagickFalse;
  4487. }
  4488. }
  4489. image_view=DestroyCacheView(image_view);
  4490. return(status);
  4491. }
  4492. /*
  4493. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4494. % %
  4495. % %
  4496. % %
  4497. % S t e g a n o I m a g e %
  4498. % %
  4499. % %
  4500. % %
  4501. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4502. %
  4503. % SteganoImage() hides a digital watermark within the image. Recover
  4504. % the hidden watermark later to prove that the authenticity of an image.
  4505. % Offset defines the start position within the image to hide the watermark.
  4506. %
  4507. % The format of the SteganoImage method is:
  4508. %
  4509. % Image *SteganoImage(const Image *image,Image *watermark,
  4510. % ExceptionInfo *exception)
  4511. %
  4512. % A description of each parameter follows:
  4513. %
  4514. % o image: the image.
  4515. %
  4516. % o watermark: the watermark image.
  4517. %
  4518. % o exception: return any errors or warnings in this structure.
  4519. %
  4520. */
  4521. MagickExport Image *SteganoImage(const Image *image,const Image *watermark,
  4522. ExceptionInfo *exception)
  4523. {
  4524. #define GetBit(alpha,i) ((((size_t) (alpha) >> (size_t) (i)) & 0x01) != 0)
  4525. #define SetBit(alpha,i,set) (Quantum) ((set) != 0 ? (size_t) (alpha) \
  4526. | (one << (size_t) (i)) : (size_t) (alpha) & ~(one << (size_t) (i)))
  4527. #define SteganoImageTag "Stegano/Image"
  4528. CacheView
  4529. *stegano_view,
  4530. *watermark_view;
  4531. Image
  4532. *stegano_image;
  4533. int
  4534. c;
  4535. MagickBooleanType
  4536. status;
  4537. PixelInfo
  4538. pixel;
  4539. register Quantum
  4540. *q;
  4541. register ssize_t
  4542. x;
  4543. size_t
  4544. depth,
  4545. one;
  4546. ssize_t
  4547. i,
  4548. j,
  4549. k,
  4550. y;
  4551. /*
  4552. Initialize steganographic image attributes.
  4553. */
  4554. assert(image != (const Image *) NULL);
  4555. assert(image->signature == MagickCoreSignature);
  4556. if (image->debug != MagickFalse)
  4557. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  4558. assert(watermark != (const Image *) NULL);
  4559. assert(watermark->signature == MagickCoreSignature);
  4560. assert(exception != (ExceptionInfo *) NULL);
  4561. assert(exception->signature == MagickCoreSignature);
  4562. one=1UL;
  4563. stegano_image=CloneImage(image,0,0,MagickTrue,exception);
  4564. if (stegano_image == (Image *) NULL)
  4565. return((Image *) NULL);
  4566. stegano_image->depth=MAGICKCORE_QUANTUM_DEPTH;
  4567. if (SetImageStorageClass(stegano_image,DirectClass,exception) == MagickFalse)
  4568. {
  4569. stegano_image=DestroyImage(stegano_image);
  4570. return((Image *) NULL);
  4571. }
  4572. /*
  4573. Hide watermark in low-order bits of image.
  4574. */
  4575. c=0;
  4576. i=0;
  4577. j=0;
  4578. depth=stegano_image->depth;
  4579. k=stegano_image->offset;
  4580. status=MagickTrue;
  4581. watermark_view=AcquireVirtualCacheView(watermark,exception);
  4582. stegano_view=AcquireAuthenticCacheView(stegano_image,exception);
  4583. for (i=(ssize_t) depth-1; (i >= 0) && (j < (ssize_t) depth); i--)
  4584. {
  4585. for (y=0; (y < (ssize_t) watermark->rows) && (j < (ssize_t) depth); y++)
  4586. {
  4587. for (x=0; (x < (ssize_t) watermark->columns) && (j < (ssize_t) depth); x++)
  4588. {
  4589. ssize_t
  4590. offset;
  4591. (void) GetOneCacheViewVirtualPixelInfo(watermark_view,x,y,&pixel,
  4592. exception);
  4593. offset=k/(ssize_t) stegano_image->columns;
  4594. if (offset >= (ssize_t) stegano_image->rows)
  4595. break;
  4596. q=GetCacheViewAuthenticPixels(stegano_view,k % (ssize_t)
  4597. stegano_image->columns,k/(ssize_t) stegano_image->columns,1,1,
  4598. exception);
  4599. if (q == (Quantum *) NULL)
  4600. break;
  4601. switch (c)
  4602. {
  4603. case 0:
  4604. {
  4605. SetPixelRed(stegano_image,SetBit(GetPixelRed(stegano_image,q),j,
  4606. GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
  4607. break;
  4608. }
  4609. case 1:
  4610. {
  4611. SetPixelGreen(stegano_image,SetBit(GetPixelGreen(stegano_image,q),j,
  4612. GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
  4613. break;
  4614. }
  4615. case 2:
  4616. {
  4617. SetPixelBlue(stegano_image,SetBit(GetPixelBlue(stegano_image,q),j,
  4618. GetBit(GetPixelInfoIntensity(stegano_image,&pixel),i)),q);
  4619. break;
  4620. }
  4621. }
  4622. if (SyncCacheViewAuthenticPixels(stegano_view,exception) == MagickFalse)
  4623. break;
  4624. c++;
  4625. if (c == 3)
  4626. c=0;
  4627. k++;
  4628. if (k == (ssize_t) (stegano_image->columns*stegano_image->columns))
  4629. k=0;
  4630. if (k == stegano_image->offset)
  4631. j++;
  4632. }
  4633. }
  4634. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  4635. {
  4636. MagickBooleanType
  4637. proceed;
  4638. proceed=SetImageProgress(image,SteganoImageTag,(MagickOffsetType)
  4639. (depth-i),depth);
  4640. if (proceed == MagickFalse)
  4641. status=MagickFalse;
  4642. }
  4643. }
  4644. stegano_view=DestroyCacheView(stegano_view);
  4645. watermark_view=DestroyCacheView(watermark_view);
  4646. if (status == MagickFalse)
  4647. stegano_image=DestroyImage(stegano_image);
  4648. return(stegano_image);
  4649. }
  4650. /*
  4651. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4652. % %
  4653. % %
  4654. % %
  4655. % S t e r e o A n a g l y p h I m a g e %
  4656. % %
  4657. % %
  4658. % %
  4659. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4660. %
  4661. % StereoAnaglyphImage() combines two images and produces a single image that
  4662. % is the composite of a left and right image of a stereo pair. Special
  4663. % red-green stereo glasses are required to view this effect.
  4664. %
  4665. % The format of the StereoAnaglyphImage method is:
  4666. %
  4667. % Image *StereoImage(const Image *left_image,const Image *right_image,
  4668. % ExceptionInfo *exception)
  4669. % Image *StereoAnaglyphImage(const Image *left_image,
  4670. % const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
  4671. % ExceptionInfo *exception)
  4672. %
  4673. % A description of each parameter follows:
  4674. %
  4675. % o left_image: the left image.
  4676. %
  4677. % o right_image: the right image.
  4678. %
  4679. % o exception: return any errors or warnings in this structure.
  4680. %
  4681. % o x_offset: amount, in pixels, by which the left image is offset to the
  4682. % right of the right image.
  4683. %
  4684. % o y_offset: amount, in pixels, by which the left image is offset to the
  4685. % bottom of the right image.
  4686. %
  4687. %
  4688. */
  4689. MagickExport Image *StereoImage(const Image *left_image,
  4690. const Image *right_image,ExceptionInfo *exception)
  4691. {
  4692. return(StereoAnaglyphImage(left_image,right_image,0,0,exception));
  4693. }
  4694. MagickExport Image *StereoAnaglyphImage(const Image *left_image,
  4695. const Image *right_image,const ssize_t x_offset,const ssize_t y_offset,
  4696. ExceptionInfo *exception)
  4697. {
  4698. #define StereoImageTag "Stereo/Image"
  4699. const Image
  4700. *image;
  4701. Image
  4702. *stereo_image;
  4703. MagickBooleanType
  4704. status;
  4705. ssize_t
  4706. y;
  4707. assert(left_image != (const Image *) NULL);
  4708. assert(left_image->signature == MagickCoreSignature);
  4709. if (left_image->debug != MagickFalse)
  4710. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
  4711. left_image->filename);
  4712. assert(right_image != (const Image *) NULL);
  4713. assert(right_image->signature == MagickCoreSignature);
  4714. assert(exception != (ExceptionInfo *) NULL);
  4715. assert(exception->signature == MagickCoreSignature);
  4716. image=left_image;
  4717. if ((left_image->columns != right_image->columns) ||
  4718. (left_image->rows != right_image->rows))
  4719. ThrowImageException(ImageError,"LeftAndRightImageSizesDiffer");
  4720. /*
  4721. Initialize stereo image attributes.
  4722. */
  4723. stereo_image=CloneImage(left_image,left_image->columns,left_image->rows,
  4724. MagickTrue,exception);
  4725. if (stereo_image == (Image *) NULL)
  4726. return((Image *) NULL);
  4727. if (SetImageStorageClass(stereo_image,DirectClass,exception) == MagickFalse)
  4728. {
  4729. stereo_image=DestroyImage(stereo_image);
  4730. return((Image *) NULL);
  4731. }
  4732. (void) SetImageColorspace(stereo_image,sRGBColorspace,exception);
  4733. /*
  4734. Copy left image to red channel and right image to blue channel.
  4735. */
  4736. status=MagickTrue;
  4737. for (y=0; y < (ssize_t) stereo_image->rows; y++)
  4738. {
  4739. register const Quantum
  4740. *magick_restrict p,
  4741. *magick_restrict q;
  4742. register ssize_t
  4743. x;
  4744. register Quantum
  4745. *magick_restrict r;
  4746. p=GetVirtualPixels(left_image,-x_offset,y-y_offset,image->columns,1,
  4747. exception);
  4748. q=GetVirtualPixels(right_image,0,y,right_image->columns,1,exception);
  4749. r=QueueAuthenticPixels(stereo_image,0,y,stereo_image->columns,1,exception);
  4750. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL) ||
  4751. (r == (Quantum *) NULL))
  4752. break;
  4753. for (x=0; x < (ssize_t) stereo_image->columns; x++)
  4754. {
  4755. SetPixelRed(stereo_image,GetPixelRed(left_image,p),r);
  4756. SetPixelGreen(stereo_image,GetPixelGreen(right_image,q),r);
  4757. SetPixelBlue(stereo_image,GetPixelBlue(right_image,q),r);
  4758. if ((GetPixelAlphaTraits(stereo_image) & CopyPixelTrait) != 0)
  4759. SetPixelAlpha(stereo_image,(GetPixelAlpha(left_image,p)+
  4760. GetPixelAlpha(right_image,q))/2,r);
  4761. p+=GetPixelChannels(left_image);
  4762. q+=GetPixelChannels(right_image);
  4763. r+=GetPixelChannels(stereo_image);
  4764. }
  4765. if (SyncAuthenticPixels(stereo_image,exception) == MagickFalse)
  4766. break;
  4767. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  4768. {
  4769. MagickBooleanType
  4770. proceed;
  4771. proceed=SetImageProgress(image,StereoImageTag,(MagickOffsetType) y,
  4772. stereo_image->rows);
  4773. if (proceed == MagickFalse)
  4774. status=MagickFalse;
  4775. }
  4776. }
  4777. if (status == MagickFalse)
  4778. stereo_image=DestroyImage(stereo_image);
  4779. return(stereo_image);
  4780. }
  4781. /*
  4782. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4783. % %
  4784. % %
  4785. % %
  4786. % S w i r l I m a g e %
  4787. % %
  4788. % %
  4789. % %
  4790. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4791. %
  4792. % SwirlImage() swirls the pixels about the center of the image, where
  4793. % degrees indicates the sweep of the arc through which each pixel is moved.
  4794. % You get a more dramatic effect as the degrees move from 1 to 360.
  4795. %
  4796. % The format of the SwirlImage method is:
  4797. %
  4798. % Image *SwirlImage(const Image *image,double degrees,
  4799. % const PixelInterpolateMethod method,ExceptionInfo *exception)
  4800. %
  4801. % A description of each parameter follows:
  4802. %
  4803. % o image: the image.
  4804. %
  4805. % o degrees: Define the tightness of the swirling effect.
  4806. %
  4807. % o method: the pixel interpolation method.
  4808. %
  4809. % o exception: return any errors or warnings in this structure.
  4810. %
  4811. */
  4812. MagickExport Image *SwirlImage(const Image *image,double degrees,
  4813. const PixelInterpolateMethod method,ExceptionInfo *exception)
  4814. {
  4815. #define SwirlImageTag "Swirl/Image"
  4816. CacheView
  4817. *canvas_view,
  4818. *interpolate_view,
  4819. *swirl_view;
  4820. double
  4821. radius;
  4822. Image
  4823. *canvas_image,
  4824. *swirl_image;
  4825. MagickBooleanType
  4826. status;
  4827. MagickOffsetType
  4828. progress;
  4829. PointInfo
  4830. center,
  4831. scale;
  4832. ssize_t
  4833. y;
  4834. /*
  4835. Initialize swirl image attributes.
  4836. */
  4837. assert(image != (const Image *) NULL);
  4838. assert(image->signature == MagickCoreSignature);
  4839. if (image->debug != MagickFalse)
  4840. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  4841. assert(exception != (ExceptionInfo *) NULL);
  4842. assert(exception->signature == MagickCoreSignature);
  4843. canvas_image=CloneImage(image,0,0,MagickTrue,exception);
  4844. if (canvas_image == (Image *) NULL)
  4845. return((Image *) NULL);
  4846. if ((canvas_image->alpha_trait == UndefinedPixelTrait) &&
  4847. (canvas_image->background_color.alpha != OpaqueAlpha))
  4848. (void) SetImageAlphaChannel(canvas_image,OpaqueAlphaChannel,exception);
  4849. swirl_image=CloneImage(canvas_image,0,0,MagickTrue,exception);
  4850. if (swirl_image == (Image *) NULL)
  4851. {
  4852. canvas_image=DestroyImage(canvas_image);
  4853. return((Image *) NULL);
  4854. }
  4855. if (SetImageStorageClass(swirl_image,DirectClass,exception) == MagickFalse)
  4856. {
  4857. canvas_image=DestroyImage(canvas_image);
  4858. swirl_image=DestroyImage(swirl_image);
  4859. return((Image *) NULL);
  4860. }
  4861. /*
  4862. Compute scaling factor.
  4863. */
  4864. center.x=(double) canvas_image->columns/2.0;
  4865. center.y=(double) canvas_image->rows/2.0;
  4866. radius=MagickMax(center.x,center.y);
  4867. scale.x=1.0;
  4868. scale.y=1.0;
  4869. if (canvas_image->columns > canvas_image->rows)
  4870. scale.y=(double) canvas_image->columns/(double) canvas_image->rows;
  4871. else
  4872. if (canvas_image->columns < canvas_image->rows)
  4873. scale.x=(double) canvas_image->rows/(double) canvas_image->columns;
  4874. degrees=(double) DegreesToRadians(degrees);
  4875. /*
  4876. Swirl image.
  4877. */
  4878. status=MagickTrue;
  4879. progress=0;
  4880. canvas_view=AcquireVirtualCacheView(canvas_image,exception);
  4881. interpolate_view=AcquireVirtualCacheView(image,exception);
  4882. swirl_view=AcquireAuthenticCacheView(swirl_image,exception);
  4883. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4884. #pragma omp parallel for schedule(static) shared(progress,status) \
  4885. magick_number_threads(canvas_image,swirl_image,canvas_image->rows,1)
  4886. #endif
  4887. for (y=0; y < (ssize_t) canvas_image->rows; y++)
  4888. {
  4889. double
  4890. distance;
  4891. PointInfo
  4892. delta;
  4893. register const Quantum
  4894. *magick_restrict p;
  4895. register ssize_t
  4896. x;
  4897. register Quantum
  4898. *magick_restrict q;
  4899. if (status == MagickFalse)
  4900. continue;
  4901. p=GetCacheViewVirtualPixels(canvas_view,0,y,canvas_image->columns,1,
  4902. exception);
  4903. q=QueueCacheViewAuthenticPixels(swirl_view,0,y,swirl_image->columns,1,
  4904. exception);
  4905. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  4906. {
  4907. status=MagickFalse;
  4908. continue;
  4909. }
  4910. delta.y=scale.y*(double) (y-center.y);
  4911. for (x=0; x < (ssize_t) canvas_image->columns; x++)
  4912. {
  4913. /*
  4914. Determine if the pixel is within an ellipse.
  4915. */
  4916. delta.x=scale.x*(double) (x-center.x);
  4917. distance=delta.x*delta.x+delta.y*delta.y;
  4918. if (distance >= (radius*radius))
  4919. {
  4920. register ssize_t
  4921. i;
  4922. for (i=0; i < (ssize_t) GetPixelChannels(canvas_image); i++)
  4923. {
  4924. PixelChannel channel = GetPixelChannelChannel(canvas_image,i);
  4925. PixelTrait traits = GetPixelChannelTraits(canvas_image,channel);
  4926. PixelTrait swirl_traits = GetPixelChannelTraits(swirl_image,
  4927. channel);
  4928. if ((traits == UndefinedPixelTrait) ||
  4929. (swirl_traits == UndefinedPixelTrait))
  4930. continue;
  4931. SetPixelChannel(swirl_image,channel,p[i],q);
  4932. }
  4933. }
  4934. else
  4935. {
  4936. double
  4937. cosine,
  4938. factor,
  4939. sine;
  4940. /*
  4941. Swirl the pixel.
  4942. */
  4943. factor=1.0-sqrt((double) distance)/radius;
  4944. sine=sin((double) (degrees*factor*factor));
  4945. cosine=cos((double) (degrees*factor*factor));
  4946. status=InterpolatePixelChannels(canvas_image,interpolate_view,
  4947. swirl_image,method,((cosine*delta.x-sine*delta.y)/scale.x+center.x),
  4948. (double) ((sine*delta.x+cosine*delta.y)/scale.y+center.y),q,
  4949. exception);
  4950. if (status == MagickFalse)
  4951. break;
  4952. }
  4953. p+=GetPixelChannels(canvas_image);
  4954. q+=GetPixelChannels(swirl_image);
  4955. }
  4956. if (SyncCacheViewAuthenticPixels(swirl_view,exception) == MagickFalse)
  4957. status=MagickFalse;
  4958. if (canvas_image->progress_monitor != (MagickProgressMonitor) NULL)
  4959. {
  4960. MagickBooleanType
  4961. proceed;
  4962. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  4963. #pragma omp atomic
  4964. #endif
  4965. progress++;
  4966. proceed=SetImageProgress(canvas_image,SwirlImageTag,progress,
  4967. canvas_image->rows);
  4968. if (proceed == MagickFalse)
  4969. status=MagickFalse;
  4970. }
  4971. }
  4972. swirl_view=DestroyCacheView(swirl_view);
  4973. interpolate_view=DestroyCacheView(interpolate_view);
  4974. canvas_view=DestroyCacheView(canvas_view);
  4975. canvas_image=DestroyImage(canvas_image);
  4976. if (status == MagickFalse)
  4977. swirl_image=DestroyImage(swirl_image);
  4978. return(swirl_image);
  4979. }
  4980. /*
  4981. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4982. % %
  4983. % %
  4984. % %
  4985. % T i n t I m a g e %
  4986. % %
  4987. % %
  4988. % %
  4989. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4990. %
  4991. % TintImage() applies a color vector to each pixel in the image. The length
  4992. % of the vector is 0 for black and white and at its maximum for the midtones.
  4993. % The vector weighting function is f(x)=(1-(4.0*((x-0.5)*(x-0.5))))
  4994. %
  4995. % The format of the TintImage method is:
  4996. %
  4997. % Image *TintImage(const Image *image,const char *blend,
  4998. % const PixelInfo *tint,ExceptionInfo *exception)
  4999. %
  5000. % A description of each parameter follows:
  5001. %
  5002. % o image: the image.
  5003. %
  5004. % o blend: A color value used for tinting.
  5005. %
  5006. % o tint: A color value used for tinting.
  5007. %
  5008. % o exception: return any errors or warnings in this structure.
  5009. %
  5010. */
  5011. MagickExport Image *TintImage(const Image *image,const char *blend,
  5012. const PixelInfo *tint,ExceptionInfo *exception)
  5013. {
  5014. #define TintImageTag "Tint/Image"
  5015. CacheView
  5016. *image_view,
  5017. *tint_view;
  5018. double
  5019. intensity;
  5020. GeometryInfo
  5021. geometry_info;
  5022. Image
  5023. *tint_image;
  5024. MagickBooleanType
  5025. status;
  5026. MagickOffsetType
  5027. progress;
  5028. PixelInfo
  5029. color_vector;
  5030. MagickStatusType
  5031. flags;
  5032. ssize_t
  5033. y;
  5034. /*
  5035. Allocate tint image.
  5036. */
  5037. assert(image != (const Image *) NULL);
  5038. assert(image->signature == MagickCoreSignature);
  5039. if (image->debug != MagickFalse)
  5040. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  5041. assert(exception != (ExceptionInfo *) NULL);
  5042. assert(exception->signature == MagickCoreSignature);
  5043. tint_image=CloneImage(image,0,0,MagickTrue,exception);
  5044. if (tint_image == (Image *) NULL)
  5045. return((Image *) NULL);
  5046. if (SetImageStorageClass(tint_image,DirectClass,exception) == MagickFalse)
  5047. {
  5048. tint_image=DestroyImage(tint_image);
  5049. return((Image *) NULL);
  5050. }
  5051. if ((IsGrayColorspace(image->colorspace) != MagickFalse) &&
  5052. (IsPixelInfoGray(tint) == MagickFalse))
  5053. (void) SetImageColorspace(tint_image,sRGBColorspace,exception);
  5054. if (blend == (const char *) NULL)
  5055. return(tint_image);
  5056. /*
  5057. Determine RGB values of the color.
  5058. */
  5059. GetPixelInfo(image,&color_vector);
  5060. flags=ParseGeometry(blend,&geometry_info);
  5061. color_vector.red=geometry_info.rho;
  5062. color_vector.green=geometry_info.rho;
  5063. color_vector.blue=geometry_info.rho;
  5064. color_vector.alpha=(MagickRealType) OpaqueAlpha;
  5065. if ((flags & SigmaValue) != 0)
  5066. color_vector.green=geometry_info.sigma;
  5067. if ((flags & XiValue) != 0)
  5068. color_vector.blue=geometry_info.xi;
  5069. if ((flags & PsiValue) != 0)
  5070. color_vector.alpha=geometry_info.psi;
  5071. if (image->colorspace == CMYKColorspace)
  5072. {
  5073. color_vector.black=geometry_info.rho;
  5074. if ((flags & PsiValue) != 0)
  5075. color_vector.black=geometry_info.psi;
  5076. if ((flags & ChiValue) != 0)
  5077. color_vector.alpha=geometry_info.chi;
  5078. }
  5079. intensity=(double) GetPixelInfoIntensity((const Image *) NULL,tint);
  5080. color_vector.red=(double) (color_vector.red*tint->red/100.0-intensity);
  5081. color_vector.green=(double) (color_vector.green*tint->green/100.0-intensity);
  5082. color_vector.blue=(double) (color_vector.blue*tint->blue/100.0-intensity);
  5083. color_vector.black=(double) (color_vector.black*tint->black/100.0-intensity);
  5084. color_vector.alpha=(double) (color_vector.alpha*tint->alpha/100.0-intensity);
  5085. /*
  5086. Tint image.
  5087. */
  5088. status=MagickTrue;
  5089. progress=0;
  5090. image_view=AcquireVirtualCacheView(image,exception);
  5091. tint_view=AcquireAuthenticCacheView(tint_image,exception);
  5092. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  5093. #pragma omp parallel for schedule(static) shared(progress,status) \
  5094. magick_number_threads(image,tint_image,image->rows,1)
  5095. #endif
  5096. for (y=0; y < (ssize_t) image->rows; y++)
  5097. {
  5098. register const Quantum
  5099. *magick_restrict p;
  5100. register Quantum
  5101. *magick_restrict q;
  5102. register ssize_t
  5103. x;
  5104. if (status == MagickFalse)
  5105. continue;
  5106. p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
  5107. q=QueueCacheViewAuthenticPixels(tint_view,0,y,tint_image->columns,1,
  5108. exception);
  5109. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  5110. {
  5111. status=MagickFalse;
  5112. continue;
  5113. }
  5114. for (x=0; x < (ssize_t) image->columns; x++)
  5115. {
  5116. PixelInfo
  5117. pixel;
  5118. double
  5119. weight;
  5120. GetPixelInfo(image,&pixel);
  5121. weight=QuantumScale*GetPixelRed(image,p)-0.5;
  5122. pixel.red=(MagickRealType) GetPixelRed(image,p)+color_vector.red*
  5123. (1.0-(4.0*(weight*weight)));
  5124. weight=QuantumScale*GetPixelGreen(image,p)-0.5;
  5125. pixel.green=(MagickRealType) GetPixelGreen(image,p)+color_vector.green*
  5126. (1.0-(4.0*(weight*weight)));
  5127. weight=QuantumScale*GetPixelBlue(image,p)-0.5;
  5128. pixel.blue=(MagickRealType) GetPixelBlue(image,p)+color_vector.blue*
  5129. (1.0-(4.0*(weight*weight)));
  5130. weight=QuantumScale*GetPixelBlack(image,p)-0.5;
  5131. pixel.black=(MagickRealType) GetPixelBlack(image,p)+color_vector.black*
  5132. (1.0-(4.0*(weight*weight)));
  5133. pixel.alpha=(MagickRealType) GetPixelAlpha(image,p);
  5134. SetPixelViaPixelInfo(tint_image,&pixel,q);
  5135. p+=GetPixelChannels(image);
  5136. q+=GetPixelChannels(tint_image);
  5137. }
  5138. if (SyncCacheViewAuthenticPixels(tint_view,exception) == MagickFalse)
  5139. status=MagickFalse;
  5140. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  5141. {
  5142. MagickBooleanType
  5143. proceed;
  5144. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  5145. #pragma omp atomic
  5146. #endif
  5147. progress++;
  5148. proceed=SetImageProgress(image,TintImageTag,progress,image->rows);
  5149. if (proceed == MagickFalse)
  5150. status=MagickFalse;
  5151. }
  5152. }
  5153. tint_view=DestroyCacheView(tint_view);
  5154. image_view=DestroyCacheView(image_view);
  5155. if (status == MagickFalse)
  5156. tint_image=DestroyImage(tint_image);
  5157. return(tint_image);
  5158. }
  5159. /*
  5160. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5161. % %
  5162. % %
  5163. % %
  5164. % V i g n e t t e I m a g e %
  5165. % %
  5166. % %
  5167. % %
  5168. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5169. %
  5170. % VignetteImage() softens the edges of the image in vignette style.
  5171. %
  5172. % The format of the VignetteImage method is:
  5173. %
  5174. % Image *VignetteImage(const Image *image,const double radius,
  5175. % const double sigma,const ssize_t x,const ssize_t y,
  5176. % ExceptionInfo *exception)
  5177. %
  5178. % A description of each parameter follows:
  5179. %
  5180. % o image: the image.
  5181. %
  5182. % o radius: the radius of the pixel neighborhood.
  5183. %
  5184. % o sigma: the standard deviation of the Gaussian, in pixels.
  5185. %
  5186. % o x, y: Define the x and y ellipse offset.
  5187. %
  5188. % o exception: return any errors or warnings in this structure.
  5189. %
  5190. */
  5191. MagickExport Image *VignetteImage(const Image *image,const double radius,
  5192. const double sigma,const ssize_t x,const ssize_t y,ExceptionInfo *exception)
  5193. {
  5194. char
  5195. ellipse[MagickPathExtent];
  5196. DrawInfo
  5197. *draw_info;
  5198. Image
  5199. *canvas,
  5200. *blur_image,
  5201. *oval_image,
  5202. *vignette_image;
  5203. assert(image != (Image *) NULL);
  5204. assert(image->signature == MagickCoreSignature);
  5205. if (image->debug != MagickFalse)
  5206. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  5207. assert(exception != (ExceptionInfo *) NULL);
  5208. assert(exception->signature == MagickCoreSignature);
  5209. canvas=CloneImage(image,0,0,MagickTrue,exception);
  5210. if (canvas == (Image *) NULL)
  5211. return((Image *) NULL);
  5212. if (SetImageStorageClass(canvas,DirectClass,exception) == MagickFalse)
  5213. {
  5214. canvas=DestroyImage(canvas);
  5215. return((Image *) NULL);
  5216. }
  5217. canvas->alpha_trait=BlendPixelTrait;
  5218. oval_image=CloneImage(canvas,canvas->columns,canvas->rows,MagickTrue,
  5219. exception);
  5220. if (oval_image == (Image *) NULL)
  5221. {
  5222. canvas=DestroyImage(canvas);
  5223. return((Image *) NULL);
  5224. }
  5225. (void) QueryColorCompliance("#000000",AllCompliance,
  5226. &oval_image->background_color,exception);
  5227. (void) SetImageBackgroundColor(oval_image,exception);
  5228. draw_info=CloneDrawInfo((const ImageInfo *) NULL,(const DrawInfo *) NULL);
  5229. (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->fill,
  5230. exception);
  5231. (void) QueryColorCompliance("#ffffff",AllCompliance,&draw_info->stroke,
  5232. exception);
  5233. (void) FormatLocaleString(ellipse,MagickPathExtent,"ellipse %g,%g,%g,%g,"
  5234. "0.0,360.0",image->columns/2.0,image->rows/2.0,image->columns/2.0-x,
  5235. image->rows/2.0-y);
  5236. draw_info->primitive=AcquireString(ellipse);
  5237. (void) DrawImage(oval_image,draw_info,exception);
  5238. draw_info=DestroyDrawInfo(draw_info);
  5239. blur_image=BlurImage(oval_image,radius,sigma,exception);
  5240. oval_image=DestroyImage(oval_image);
  5241. if (blur_image == (Image *) NULL)
  5242. {
  5243. canvas=DestroyImage(canvas);
  5244. return((Image *) NULL);
  5245. }
  5246. blur_image->alpha_trait=UndefinedPixelTrait;
  5247. (void) CompositeImage(canvas,blur_image,IntensityCompositeOp,MagickTrue,
  5248. 0,0,exception);
  5249. blur_image=DestroyImage(blur_image);
  5250. vignette_image=MergeImageLayers(canvas,FlattenLayer,exception);
  5251. canvas=DestroyImage(canvas);
  5252. if (vignette_image != (Image *) NULL)
  5253. (void) TransformImageColorspace(vignette_image,image->colorspace,exception);
  5254. return(vignette_image);
  5255. }
  5256. /*
  5257. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5258. % %
  5259. % %
  5260. % %
  5261. % W a v e I m a g e %
  5262. % %
  5263. % %
  5264. % %
  5265. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5266. %
  5267. % WaveImage() creates a "ripple" effect in the image by shifting the pixels
  5268. % vertically along a sine wave whose amplitude and wavelength is specified
  5269. % by the given parameters.
  5270. %
  5271. % The format of the WaveImage method is:
  5272. %
  5273. % Image *WaveImage(const Image *image,const double amplitude,
  5274. % const double wave_length,const PixelInterpolateMethod method,
  5275. % ExceptionInfo *exception)
  5276. %
  5277. % A description of each parameter follows:
  5278. %
  5279. % o image: the image.
  5280. %
  5281. % o amplitude, wave_length: Define the amplitude and wave length of the
  5282. % sine wave.
  5283. %
  5284. % o interpolate: the pixel interpolation method.
  5285. %
  5286. % o exception: return any errors or warnings in this structure.
  5287. %
  5288. */
  5289. MagickExport Image *WaveImage(const Image *image,const double amplitude,
  5290. const double wave_length,const PixelInterpolateMethod method,
  5291. ExceptionInfo *exception)
  5292. {
  5293. #define WaveImageTag "Wave/Image"
  5294. CacheView
  5295. *canvas_image_view,
  5296. *wave_view;
  5297. float
  5298. *sine_map;
  5299. Image
  5300. *canvas_image,
  5301. *wave_image;
  5302. MagickBooleanType
  5303. status;
  5304. MagickOffsetType
  5305. progress;
  5306. register ssize_t
  5307. i;
  5308. ssize_t
  5309. y;
  5310. /*
  5311. Initialize wave image attributes.
  5312. */
  5313. assert(image != (Image *) NULL);
  5314. assert(image->signature == MagickCoreSignature);
  5315. if (image->debug != MagickFalse)
  5316. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  5317. assert(exception != (ExceptionInfo *) NULL);
  5318. assert(exception->signature == MagickCoreSignature);
  5319. canvas_image=CloneImage(image,0,0,MagickTrue,exception);
  5320. if (canvas_image == (Image *) NULL)
  5321. return((Image *) NULL);
  5322. if ((canvas_image->alpha_trait == UndefinedPixelTrait) &&
  5323. (canvas_image->background_color.alpha != OpaqueAlpha))
  5324. (void) SetImageAlpha(canvas_image,OpaqueAlpha,exception);
  5325. wave_image=CloneImage(canvas_image,canvas_image->columns,(size_t)
  5326. (canvas_image->rows+2.0*fabs(amplitude)),MagickTrue,exception);
  5327. if (wave_image == (Image *) NULL)
  5328. {
  5329. canvas_image=DestroyImage(canvas_image);
  5330. return((Image *) NULL);
  5331. }
  5332. if (SetImageStorageClass(wave_image,DirectClass,exception) == MagickFalse)
  5333. {
  5334. canvas_image=DestroyImage(canvas_image);
  5335. wave_image=DestroyImage(wave_image);
  5336. return((Image *) NULL);
  5337. }
  5338. /*
  5339. Allocate sine map.
  5340. */
  5341. sine_map=(float *) AcquireQuantumMemory((size_t) wave_image->columns,
  5342. sizeof(*sine_map));
  5343. if (sine_map == (float *) NULL)
  5344. {
  5345. canvas_image=DestroyImage(canvas_image);
  5346. wave_image=DestroyImage(wave_image);
  5347. ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
  5348. }
  5349. for (i=0; i < (ssize_t) wave_image->columns; i++)
  5350. sine_map[i]=(float) fabs(amplitude)+amplitude*sin((double)
  5351. ((2.0*MagickPI*i)/wave_length));
  5352. /*
  5353. Wave image.
  5354. */
  5355. status=MagickTrue;
  5356. progress=0;
  5357. canvas_image_view=AcquireVirtualCacheView(canvas_image,exception);
  5358. wave_view=AcquireAuthenticCacheView(wave_image,exception);
  5359. (void) SetCacheViewVirtualPixelMethod(canvas_image_view,
  5360. BackgroundVirtualPixelMethod);
  5361. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  5362. #pragma omp parallel for schedule(static) shared(progress,status) \
  5363. magick_number_threads(canvas_image,wave_image,wave_image->rows,1)
  5364. #endif
  5365. for (y=0; y < (ssize_t) wave_image->rows; y++)
  5366. {
  5367. register const Quantum
  5368. *magick_restrict p;
  5369. register Quantum
  5370. *magick_restrict q;
  5371. register ssize_t
  5372. x;
  5373. if (status == MagickFalse)
  5374. continue;
  5375. p=GetCacheViewVirtualPixels(canvas_image_view,0,y,canvas_image->columns,1,
  5376. exception);
  5377. q=QueueCacheViewAuthenticPixels(wave_view,0,y,wave_image->columns,1,
  5378. exception);
  5379. if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
  5380. {
  5381. status=MagickFalse;
  5382. continue;
  5383. }
  5384. for (x=0; x < (ssize_t) wave_image->columns; x++)
  5385. {
  5386. status=InterpolatePixelChannels(canvas_image,canvas_image_view,
  5387. wave_image,method,(double) x,(double) (y-sine_map[x]),q,exception);
  5388. if (status == MagickFalse)
  5389. break;
  5390. p+=GetPixelChannels(canvas_image);
  5391. q+=GetPixelChannels(wave_image);
  5392. }
  5393. if (SyncCacheViewAuthenticPixels(wave_view,exception) == MagickFalse)
  5394. status=MagickFalse;
  5395. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  5396. {
  5397. MagickBooleanType
  5398. proceed;
  5399. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  5400. #pragma omp atomic
  5401. #endif
  5402. progress++;
  5403. proceed=SetImageProgress(canvas_image,WaveImageTag,progress,
  5404. canvas_image->rows);
  5405. if (proceed == MagickFalse)
  5406. status=MagickFalse;
  5407. }
  5408. }
  5409. wave_view=DestroyCacheView(wave_view);
  5410. canvas_image_view=DestroyCacheView(canvas_image_view);
  5411. canvas_image=DestroyImage(canvas_image);
  5412. sine_map=(float *) RelinquishMagickMemory(sine_map);
  5413. if (status == MagickFalse)
  5414. wave_image=DestroyImage(wave_image);
  5415. return(wave_image);
  5416. }
  5417. /*
  5418. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5419. % %
  5420. % %
  5421. % %
  5422. % W a v e l e t D e n o i s e I m a g e %
  5423. % %
  5424. % %
  5425. % %
  5426. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5427. %
  5428. % WaveletDenoiseImage() removes noise from the image using a wavelet
  5429. % transform. The wavelet transform is a fast hierarchical scheme for
  5430. % processing an image using a set of consecutive lowpass and high_pass filters,
  5431. % followed by a decimation. This results in a decomposition into different
  5432. % scales which can be regarded as different “frequency bands”, determined by
  5433. % the mother wavelet. Adapted from dcraw.c by David Coffin.
  5434. %
  5435. % The format of the WaveletDenoiseImage method is:
  5436. %
  5437. % Image *WaveletDenoiseImage(const Image *image,const double threshold,
  5438. % const double softness,ExceptionInfo *exception)
  5439. %
  5440. % A description of each parameter follows:
  5441. %
  5442. % o image: the image.
  5443. %
  5444. % o threshold: set the threshold for smoothing.
  5445. %
  5446. % o softness: attenuate the smoothing threshold.
  5447. %
  5448. % o exception: return any errors or warnings in this structure.
  5449. %
  5450. */
  5451. static inline void HatTransform(const float *magick_restrict pixels,
  5452. const size_t stride,const size_t extent,const size_t scale,float *kernel)
  5453. {
  5454. const float
  5455. *magick_restrict p,
  5456. *magick_restrict q,
  5457. *magick_restrict r;
  5458. register ssize_t
  5459. i;
  5460. p=pixels;
  5461. q=pixels+scale*stride;
  5462. r=pixels+scale*stride;
  5463. for (i=0; i < (ssize_t) scale; i++)
  5464. {
  5465. kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
  5466. p+=stride;
  5467. q-=stride;
  5468. r+=stride;
  5469. }
  5470. for ( ; i < (ssize_t) (extent-scale); i++)
  5471. {
  5472. kernel[i]=0.25f*(2.0f*(*p)+*(p-scale*stride)+*(p+scale*stride));
  5473. p+=stride;
  5474. }
  5475. q=p-scale*stride;
  5476. r=pixels+stride*(extent-2);
  5477. for ( ; i < (ssize_t) extent; i++)
  5478. {
  5479. kernel[i]=0.25f*(*p+(*p)+(*q)+(*r));
  5480. p+=stride;
  5481. q+=stride;
  5482. r-=stride;
  5483. }
  5484. }
  5485. MagickExport Image *WaveletDenoiseImage(const Image *image,
  5486. const double threshold,const double softness,ExceptionInfo *exception)
  5487. {
  5488. CacheView
  5489. *image_view,
  5490. *noise_view;
  5491. float
  5492. *kernel,
  5493. *pixels;
  5494. Image
  5495. *noise_image;
  5496. MagickBooleanType
  5497. status;
  5498. MagickSizeType
  5499. number_pixels;
  5500. MemoryInfo
  5501. *pixels_info;
  5502. ssize_t
  5503. channel;
  5504. static const float
  5505. noise_levels[] = { 0.8002f, 0.2735f, 0.1202f, 0.0585f, 0.0291f, 0.0152f,
  5506. 0.0080f, 0.0044f };
  5507. /*
  5508. Initialize noise image attributes.
  5509. */
  5510. assert(image != (const Image *) NULL);
  5511. assert(image->signature == MagickCoreSignature);
  5512. if (image->debug != MagickFalse)
  5513. (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
  5514. assert(exception != (ExceptionInfo *) NULL);
  5515. assert(exception->signature == MagickCoreSignature);
  5516. #if defined(MAGICKCORE_OPENCL_SUPPORT)
  5517. noise_image=AccelerateWaveletDenoiseImage(image,threshold,exception);
  5518. if (noise_image != (Image *) NULL)
  5519. return(noise_image);
  5520. #endif
  5521. noise_image=CloneImage(image,0,0,MagickTrue,exception);
  5522. if (noise_image == (Image *) NULL)
  5523. return((Image *) NULL);
  5524. if (SetImageStorageClass(noise_image,DirectClass,exception) == MagickFalse)
  5525. {
  5526. noise_image=DestroyImage(noise_image);
  5527. return((Image *) NULL);
  5528. }
  5529. if (AcquireMagickResource(WidthResource,4*image->columns) == MagickFalse)
  5530. ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
  5531. pixels_info=AcquireVirtualMemory(3*image->columns,image->rows*
  5532. sizeof(*pixels));
  5533. kernel=(float *) AcquireQuantumMemory(MagickMax(image->rows,image->columns)+1,
  5534. GetOpenMPMaximumThreads()*sizeof(*kernel));
  5535. if ((pixels_info == (MemoryInfo *) NULL) || (kernel == (float *) NULL))
  5536. {
  5537. if (kernel != (float *) NULL)
  5538. kernel=(float *) RelinquishMagickMemory(kernel);
  5539. if (pixels_info != (MemoryInfo *) NULL)
  5540. pixels_info=RelinquishVirtualMemory(pixels_info);
  5541. ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
  5542. }
  5543. pixels=(float *) GetVirtualMemoryBlob(pixels_info);
  5544. status=MagickTrue;
  5545. number_pixels=(MagickSizeType) image->columns*image->rows;
  5546. image_view=AcquireAuthenticCacheView(image,exception);
  5547. noise_view=AcquireAuthenticCacheView(noise_image,exception);
  5548. for (channel=0; channel < (ssize_t) GetPixelChannels(image); channel++)
  5549. {
  5550. register ssize_t
  5551. i;
  5552. size_t
  5553. high_pass,
  5554. low_pass;
  5555. ssize_t
  5556. level,
  5557. y;
  5558. PixelChannel
  5559. pixel_channel;
  5560. PixelTrait
  5561. traits;
  5562. if (status == MagickFalse)
  5563. continue;
  5564. traits=GetPixelChannelTraits(image,(PixelChannel) channel);
  5565. if (traits == UndefinedPixelTrait)
  5566. continue;
  5567. pixel_channel=GetPixelChannelChannel(image,channel);
  5568. if ((pixel_channel != RedPixelChannel) &&
  5569. (pixel_channel != GreenPixelChannel) &&
  5570. (pixel_channel != BluePixelChannel))
  5571. continue;
  5572. /*
  5573. Copy channel from image to wavelet pixel array.
  5574. */
  5575. i=0;
  5576. for (y=0; y < (ssize_t) image->rows; y++)
  5577. {
  5578. register const Quantum
  5579. *magick_restrict p;
  5580. ssize_t
  5581. x;
  5582. p=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
  5583. if (p == (const Quantum *) NULL)
  5584. {
  5585. status=MagickFalse;
  5586. break;
  5587. }
  5588. for (x=0; x < (ssize_t) image->columns; x++)
  5589. {
  5590. pixels[i++]=(float) p[channel];
  5591. p+=GetPixelChannels(image);
  5592. }
  5593. }
  5594. /*
  5595. Low pass filter outputs are called approximation kernel & high pass
  5596. filters are referred to as detail kernel. The detail kernel
  5597. have high values in the noisy parts of the signal.
  5598. */
  5599. high_pass=0;
  5600. for (level=0; level < 5; level++)
  5601. {
  5602. double
  5603. magnitude;
  5604. ssize_t
  5605. x,
  5606. y;
  5607. low_pass=(size_t) (number_pixels*((level & 0x01)+1));
  5608. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  5609. #pragma omp parallel for schedule(static,1) \
  5610. magick_number_threads(image,image,image->rows,1)
  5611. #endif
  5612. for (y=0; y < (ssize_t) image->rows; y++)
  5613. {
  5614. const int
  5615. id = GetOpenMPThreadId();
  5616. register float
  5617. *magick_restrict p,
  5618. *magick_restrict q;
  5619. register ssize_t
  5620. x;
  5621. p=kernel+id*image->columns;
  5622. q=pixels+y*image->columns;
  5623. HatTransform(q+high_pass,1,image->columns,(size_t) (1UL << level),p);
  5624. q+=low_pass;
  5625. for (x=0; x < (ssize_t) image->columns; x++)
  5626. *q++=(*p++);
  5627. }
  5628. #if defined(MAGICKCORE_OPENMP_SUPPORT)
  5629. #pragma omp parallel for schedule(static,1) \
  5630. magick_number_threads(image,image,image->columns,1)
  5631. #endif
  5632. for (x=0; x < (ssize_t) image->columns; x++)
  5633. {
  5634. const int
  5635. id = GetOpenMPThreadId();
  5636. register float
  5637. *magick_restrict p,
  5638. *magick_restrict q;
  5639. register ssize_t
  5640. y;
  5641. p=kernel+id*image->rows;
  5642. q=pixels+x+low_pass;
  5643. HatTransform(q,image->columns,image->rows,(size_t) (1UL << level),p);
  5644. for (y=0; y < (ssize_t) image->rows; y++)
  5645. {
  5646. *q=(*p++);
  5647. q+=image->columns;
  5648. }
  5649. }
  5650. /*
  5651. To threshold, each coefficient is compared to a threshold value and
  5652. attenuated / shrunk by some factor.
  5653. */
  5654. magnitude=threshold*noise_levels[level];
  5655. for (i=0; i < (ssize_t) number_pixels; ++i)
  5656. {
  5657. pixels[high_pass+i]-=pixels[low_pass+i];
  5658. if (pixels[high_pass+i] < -magnitude)
  5659. pixels[high_pass+i]+=magnitude-softness*magnitude;
  5660. else
  5661. if (pixels[high_pass+i] > magnitude)
  5662. pixels[high_pass+i]-=magnitude-softness*magnitude;
  5663. else
  5664. pixels[high_pass+i]*=softness;
  5665. if (high_pass != 0)
  5666. pixels[i]+=pixels[high_pass+i];
  5667. }
  5668. high_pass=low_pass;
  5669. }
  5670. /*
  5671. Reconstruct image from the thresholded wavelet kernel.
  5672. */
  5673. i=0;
  5674. for (y=0; y < (ssize_t) image->rows; y++)
  5675. {
  5676. MagickBooleanType
  5677. sync;
  5678. register Quantum
  5679. *magick_restrict q;
  5680. register ssize_t
  5681. x;
  5682. ssize_t
  5683. offset;
  5684. q=GetCacheViewAuthenticPixels(noise_view,0,y,noise_image->columns,1,
  5685. exception);
  5686. if (q == (Quantum *) NULL)
  5687. {
  5688. status=MagickFalse;
  5689. break;
  5690. }
  5691. offset=GetPixelChannelOffset(noise_image,pixel_channel);
  5692. for (x=0; x < (ssize_t) image->columns; x++)
  5693. {
  5694. MagickRealType
  5695. pixel;
  5696. pixel=(MagickRealType) pixels[i]+pixels[low_pass+i];
  5697. q[offset]=ClampToQuantum(pixel);
  5698. i++;
  5699. q+=GetPixelChannels(noise_image);
  5700. }
  5701. sync=SyncCacheViewAuthenticPixels(noise_view,exception);
  5702. if (sync == MagickFalse)
  5703. status=MagickFalse;
  5704. }
  5705. if (image->progress_monitor != (MagickProgressMonitor) NULL)
  5706. {
  5707. MagickBooleanType
  5708. proceed;
  5709. proceed=SetImageProgress(image,AddNoiseImageTag,(MagickOffsetType)
  5710. channel,GetPixelChannels(image));
  5711. if (proceed == MagickFalse)
  5712. status=MagickFalse;
  5713. }
  5714. }
  5715. noise_view=DestroyCacheView(noise_view);
  5716. image_view=DestroyCacheView(image_view);
  5717. kernel=(float *) RelinquishMagickMemory(kernel);
  5718. pixels_info=RelinquishVirtualMemory(pixels_info);
  5719. if (status == MagickFalse)
  5720. noise_image=DestroyImage(noise_image);
  5721. return(noise_image);
  5722. }