PageRenderTime 402ms CodeModel.GetById 49ms RepoModel.GetById 2ms app.codeStats 2ms

/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

Large files files are truncated, but you can click here to view the full file

  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. {

Large files files are truncated, but you can click here to view the full file