contrast.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <wand/MagickWand.h>
  5. int main(int argc,char **argv)
  6. {
  7. #define QuantumScale ((MagickRealType) 1.0/(MagickRealType) QuantumRange)
  8. #define SigmoidalContrast(x) \
  9. (QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
  10. #define ThrowWandException(wand) \
  11. { \
  12. char \
  13. *description; \
  14. \
  15. ExceptionType \
  16. severity; \
  17. \
  18. description=MagickGetException(wand,&severity); \
  19. (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
  20. description=(char *) MagickRelinquishMemory(description); \
  21. exit(-1); \
  22. }
  23. MagickBooleanType
  24. status;
  25. MagickPixelPacket
  26. pixel;
  27. MagickWand
  28. *contrast_wand,
  29. *image_wand;
  30. PixelIterator
  31. *contrast_iterator,
  32. *iterator;
  33. PixelWand
  34. **contrast_pixels,
  35. **pixels;
  36. register ssize_t
  37. x;
  38. size_t
  39. width;
  40. ssize_t
  41. y;
  42. if (argc != 3)
  43. {
  44. (void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
  45. exit(0);
  46. }
  47. /*
  48. Read an image.
  49. */
  50. MagickWandGenesis();
  51. image_wand=NewMagickWand();
  52. status=MagickReadImage(image_wand,argv[1]);
  53. if (status == MagickFalse)
  54. ThrowWandException(image_wand);
  55. contrast_wand=CloneMagickWand(image_wand);
  56. /*
  57. Sigmoidal non-linearity contrast control.
  58. */
  59. iterator=NewPixelIterator(image_wand);
  60. contrast_iterator=NewPixelIterator(contrast_wand);
  61. if ((iterator == (PixelIterator *) NULL) ||
  62. (contrast_iterator == (PixelIterator *) NULL))
  63. ThrowWandException(image_wand);
  64. for (y=0; y < (ssize_t) MagickGetImageHeight(image_wand); y++)
  65. {
  66. pixels=PixelGetNextIteratorRow(iterator,&width);
  67. contrast_pixels=PixelGetNextIteratorRow(contrast_iterator,&width);
  68. if ((pixels == (PixelWand **) NULL) ||
  69. (contrast_pixels == (PixelWand **) NULL))
  70. break;
  71. for (x=0; x < (ssize_t) width; x++)
  72. {
  73. PixelGetMagickColor(pixels[x],&pixel);
  74. pixel.red=SigmoidalContrast(pixel.red);
  75. pixel.green=SigmoidalContrast(pixel.green);
  76. pixel.blue=SigmoidalContrast(pixel.blue);
  77. pixel.index=SigmoidalContrast(pixel.index);
  78. PixelSetMagickColor(contrast_pixels[x],&pixel);
  79. }
  80. (void) PixelSyncIterator(contrast_iterator);
  81. }
  82. if (y < (ssize_t) MagickGetImageHeight(image_wand))
  83. ThrowWandException(image_wand);
  84. contrast_iterator=DestroyPixelIterator(contrast_iterator);
  85. iterator=DestroyPixelIterator(iterator);
  86. image_wand=DestroyMagickWand(image_wand);
  87. /*
  88. Write the image then destroy it.
  89. */
  90. status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
  91. if (status == MagickFalse)
  92. ThrowWandException(image_wand);
  93. contrast_wand=DestroyMagickWand(contrast_wand);
  94. MagickWandTerminus();
  95. return(0);
  96. }