Advertisement
Guest User

Untitled

a guest
May 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. /* This software is licensed under the terms of the GNU GPL v3
  2. * Pablo joubert, 2010
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <wand/MagickWand.h>
  8.  
  9.  
  10.  
  11.  
  12. int main(int argc,char **argv)
  13. {
  14. long y;
  15. double wb_value = 0;
  16. MagickBooleanType status;
  17. MagickPixelPacket pixel;
  18. MagickWand *autowb_wand, *image_wand;
  19. PixelIterator *wb_iterator, *iterator;
  20. PixelWand **wb_pixels, **pixels;
  21. register long x;
  22. unsigned long width;
  23.  
  24. if (argc != 3)
  25. {
  26. (void) fprintf(stdout,"Usage: %s image_prefix balanced_image_prefix\n",argv[0]);
  27. exit(0);
  28. }
  29. /*
  30. Read image.
  31. */
  32. MagickWandGenesis();
  33. image_wand=NewMagickWand();
  34. status=MagickReadImage(image_wand,argv[1]);
  35. if (status == MagickFalse) ThrowWandException(image_wand);
  36. autowb_wand=CloneMagickWand(image_wand);
  37. iterator=NewPixelIterator(image_wand);
  38. wb_iterator=NewPixelIterator(autowb_wand);
  39. if ((iterator == (PixelIterator *) NULL) || (wb_iterator == (PixelIterator *) NULL)) ThrowWandException(image_wand);
  40. double sum = 0,red=0, green=0, blue=0;
  41. double maxR = 0, maxG = 0, maxB = 0;
  42. double Rfactor,Gfactor,Bfactor;
  43. long image_pixel = 0;
  44. double tempr, tempg, tempb;
  45. double maxcolor=0,maximumR=0, maximumG=0, maximumB =0;
  46. /* first pass on the image, to find the brightest pixel on the image*/
  47. for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
  48. {
  49. pixels=PixelGetNextIteratorRow(iterator,&width);
  50. if (pixels == (PixelWand **) NULL) break;
  51. for (x=0; x < (long) width; x++)
  52. {
  53. tempr = PixelGetRed(pixels[x]);
  54. tempg = PixelGetGreen(pixels[x]);
  55. tempb = PixelGetBlue(pixels[x]);
  56. if (tempr + tempb + tempg > maxcolor)
  57. {
  58. maximumR = tempr;
  59. maximumG = tempg;
  60. maximumB = tempb;
  61. maxcolor = tempr + tempg + tempb;
  62. }
  63. }
  64. }
  65. maxcolor = maxcolor * 0.8; // thresold for brightest pixels: here, the 20% top
  66. iterator=NewPixelIterator(image_wand);
  67. if (iterator == (PixelIterator *) NULL);
  68. ThrowWandException(image_wand);
  69. /* second pass, where computing an average value of the 20% brightest pixels of the image */
  70. for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
  71. {
  72. pixels=PixelGetNextIteratorRow(iterator,&width);
  73. if (pixels == (PixelWand **) NULL) break;
  74. for (x=0; x < (long) width; x++)
  75. {
  76. tempr = PixelGetRed(pixels[x]);
  77. tempg = PixelGetGreen(pixels[x]);
  78. tempb = PixelGetBlue(pixels[x]);
  79. if (tempr + tempb + tempg > maxcolor)
  80. {
  81. image_pixel +=1;
  82. red += tempr;
  83. green += tempg;
  84. blue += tempb;
  85. }
  86. }
  87. }
  88.  
  89. red = red / image_pixel;
  90. green = green / image_pixel;
  91. blue = blue / image_pixel;
  92. sum = red + green + blue;
  93. printf("sum : %5.2f,\n medR : %5.2f,\nmedG : %5.2f,\nmedB : %5.2f\n", sum,red, green, blue);
  94. // apply to the other channel (compared to the brigth one)
  95. // the two darker channels are leveled up to avoid darkening the image
  96. if (red > green && red > blue)
  97. {
  98. printf("red rules\n");
  99. Rfactor = 1;
  100. Gfactor = red / green ;
  101. Bfactor = red / blue ;
  102. }
  103. if (green > red && green > blue)
  104. {
  105. printf("green rules\n");
  106. Rfactor = green / red ;
  107. Gfactor = 1;
  108. Bfactor = green / blue;
  109. }
  110. if (blue > red && blue > green)
  111. {
  112. printf ("blue rules\n");
  113. Rfactor = blue / red;
  114. Gfactor = blue / green ;
  115. Bfactor = 1;
  116. }
  117. /* old calculation, where average luminosity of the image was concerved
  118. Rfactor = sum / red / 3.0;
  119.  
  120. Gfactor = sum / green / 3.0;
  121.  
  122. Bfactor = sum / blue / 3.0;
  123. */
  124. printf ("R : %5.2f,\nG : %5.2f,\nB : %5.2f\n", Rfactor, Gfactor, Bfactor );
  125.  
  126.  
  127. iterator=NewPixelIterator(image_wand)
  128. /* this pass just multiply pixels value by the factor computed above*/
  129.  
  130. for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
  131. {
  132. pixels=PixelGetNextIteratorRow(iterator,&width);
  133. wb_pixels=PixelGetNextIteratorRow(wb_iterator,&width);
  134. if ((pixels == (PixelWand **) NULL) || (wb_pixels == (PixelWand **) NULL )) break;
  135. for (x=0; x < (long) width; x++)
  136. {
  137. PixelSetRed(wb_pixels[x],PixelGetRed(pixels[x]) * Rfactor);
  138. PixelSetGreen(wb_pixels[x],PixelGetGreen(pixels[x]) * Gfactor);
  139. PixelSetBlue(wb_pixels[x], PixelGetBlue(pixels[x]) * Bfactor);
  140. }
  141. (void) PixelSyncIterator(wb_iterator);
  142. }
  143. wb_iterator=DestroyPixelIterator(wb_iterator);
  144. iterator=DestroyPixelIterator(iterator);
  145. image_wand=DestroyMagickWand(image_wand);
  146. /*
  147. Write the image then destroy it.
  148. */
  149. status=MagickWriteImages(autowb_wand,argv[2],MagickTrue);
  150. if (status == MagickFalse) ThrowWandException(image_wand);
  151. autowb_wand=DestroyMagickWand(autowb_wand);
  152. MagickWandTerminus();
  153. return(0);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement