Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This software is licensed under the terms of the GNU GPL v3
- * Pablo joubert, 2010
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <wand/MagickWand.h>
- int main(int argc,char **argv)
- {
- long y;
- double wb_value = 0;
- MagickBooleanType status;
- MagickPixelPacket pixel;
- MagickWand *autowb_wand, *image_wand;
- PixelIterator *wb_iterator, *iterator;
- PixelWand **wb_pixels, **pixels;
- register long x;
- unsigned long width;
- if (argc != 3)
- {
- (void) fprintf(stdout,"Usage: %s image_prefix balanced_image_prefix\n",argv[0]);
- exit(0);
- }
- /*
- Read image.
- */
- MagickWandGenesis();
- image_wand=NewMagickWand();
- status=MagickReadImage(image_wand,argv[1]);
- if (status == MagickFalse) ThrowWandException(image_wand);
- autowb_wand=CloneMagickWand(image_wand);
- iterator=NewPixelIterator(image_wand);
- wb_iterator=NewPixelIterator(autowb_wand);
- if ((iterator == (PixelIterator *) NULL) || (wb_iterator == (PixelIterator *) NULL)) ThrowWandException(image_wand);
- double sum = 0,red=0, green=0, blue=0;
- double maxR = 0, maxG = 0, maxB = 0;
- double Rfactor,Gfactor,Bfactor;
- long image_pixel = 0;
- double tempr, tempg, tempb;
- double maxcolor=0,maximumR=0, maximumG=0, maximumB =0;
- /* first pass on the image, to find the brightest pixel on the image*/
- for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
- {
- pixels=PixelGetNextIteratorRow(iterator,&width);
- if (pixels == (PixelWand **) NULL) break;
- for (x=0; x < (long) width; x++)
- {
- tempr = PixelGetRed(pixels[x]);
- tempg = PixelGetGreen(pixels[x]);
- tempb = PixelGetBlue(pixels[x]);
- if (tempr + tempb + tempg > maxcolor)
- {
- maximumR = tempr;
- maximumG = tempg;
- maximumB = tempb;
- maxcolor = tempr + tempg + tempb;
- }
- }
- }
- maxcolor = maxcolor * 0.8; // thresold for brightest pixels: here, the 20% top
- iterator=NewPixelIterator(image_wand);
- if (iterator == (PixelIterator *) NULL);
- ThrowWandException(image_wand);
- /* second pass, where computing an average value of the 20% brightest pixels of the image */
- for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
- {
- pixels=PixelGetNextIteratorRow(iterator,&width);
- if (pixels == (PixelWand **) NULL) break;
- for (x=0; x < (long) width; x++)
- {
- tempr = PixelGetRed(pixels[x]);
- tempg = PixelGetGreen(pixels[x]);
- tempb = PixelGetBlue(pixels[x]);
- if (tempr + tempb + tempg > maxcolor)
- {
- image_pixel +=1;
- red += tempr;
- green += tempg;
- blue += tempb;
- }
- }
- }
- red = red / image_pixel;
- green = green / image_pixel;
- blue = blue / image_pixel;
- sum = red + green + blue;
- printf("sum : %5.2f,\n medR : %5.2f,\nmedG : %5.2f,\nmedB : %5.2f\n", sum,red, green, blue);
- // apply to the other channel (compared to the brigth one)
- // the two darker channels are leveled up to avoid darkening the image
- if (red > green && red > blue)
- {
- printf("red rules\n");
- Rfactor = 1;
- Gfactor = red / green ;
- Bfactor = red / blue ;
- }
- if (green > red && green > blue)
- {
- printf("green rules\n");
- Rfactor = green / red ;
- Gfactor = 1;
- Bfactor = green / blue;
- }
- if (blue > red && blue > green)
- {
- printf ("blue rules\n");
- Rfactor = blue / red;
- Gfactor = blue / green ;
- Bfactor = 1;
- }
- /* old calculation, where average luminosity of the image was concerved
- Rfactor = sum / red / 3.0;
- Gfactor = sum / green / 3.0;
- Bfactor = sum / blue / 3.0;
- */
- printf ("R : %5.2f,\nG : %5.2f,\nB : %5.2f\n", Rfactor, Gfactor, Bfactor );
- iterator=NewPixelIterator(image_wand)
- /* this pass just multiply pixels value by the factor computed above*/
- for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
- {
- pixels=PixelGetNextIteratorRow(iterator,&width);
- wb_pixels=PixelGetNextIteratorRow(wb_iterator,&width);
- if ((pixels == (PixelWand **) NULL) || (wb_pixels == (PixelWand **) NULL )) break;
- for (x=0; x < (long) width; x++)
- {
- PixelSetRed(wb_pixels[x],PixelGetRed(pixels[x]) * Rfactor);
- PixelSetGreen(wb_pixels[x],PixelGetGreen(pixels[x]) * Gfactor);
- PixelSetBlue(wb_pixels[x], PixelGetBlue(pixels[x]) * Bfactor);
- }
- (void) PixelSyncIterator(wb_iterator);
- }
- wb_iterator=DestroyPixelIterator(wb_iterator);
- iterator=DestroyPixelIterator(iterator);
- image_wand=DestroyMagickWand(image_wand);
- /*
- Write the image then destroy it.
- */
- status=MagickWriteImages(autowb_wand,argv[2],MagickTrue);
- if (status == MagickFalse) ThrowWandException(image_wand);
- autowb_wand=DestroyMagickWand(autowb_wand);
- MagickWandTerminus();
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement