Created
October 21, 2011 21:31
-
-
Save ciembor/1305038 to your computer and use it in GitHub Desktop.
Check if images are bright or dark (university homework - libgd)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// author: Maciej Ciemborowicz | |
// Jagiellonian University | |
// ZGK 2011/2012 | |
#include <stdio.h> | |
#include <math.h> | |
#include <gd.h> | |
#include "gd_wewy.h" | |
#define DARK 0 | |
#define BRIGHT 1 | |
int main(int argc, char * argv[]) | |
{ | |
// check if RGB pixel is BRIGHT or DARK | |
int is_pixel_bright(int r, int g, int b) { | |
// http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx | |
float brightness = sqrt(.241*r*r + .691*g*g + .068*b*b); | |
return (127.5 <= brightness) ? BRIGHT : DARK; | |
} | |
////////////////////////////////////////////////////////////////////// | |
// index 0 - dark pixels | |
// index 1 - bright pixels | |
int pixels_number[2]; | |
int x, y; | |
int i, j, k; | |
gdImagePtr img; | |
int color; | |
////////////////////////////////////////////////////////////////////// | |
if (argc < 2) { | |
fprintf(stderr, "Error: No argument!\n"); | |
return 1; | |
} | |
////////////////////////////////////////////////////////////////////// | |
for (k=1; k<argc; ++k) { | |
img = wczytajPng(argv[k]); | |
if (img == NULL) { | |
fprintf(stderr, "%s: Can't read this file!\n", argv[k]); | |
} | |
else { | |
pixels_number[DARK] = 0; | |
pixels_number[BRIGHT] = 0; | |
x = gdImageSX(img); | |
y = gdImageSY(img); | |
for (i=0; i<x; ++i) { | |
for (j=0; j<y; ++j) { | |
color = gdImageGetPixel(img, i, j); | |
// like a true geek:) | |
++pixels_number[is_pixel_bright(gdImageRed(img, color), gdImageGreen(img, color), gdImageBlue(img, color))]; | |
} | |
} | |
printf("%s: ", argv[k]); | |
if (pixels_number[DARK] > pixels_number[BRIGHT]) { | |
printf("This image is dark.\n"); | |
} | |
else if (pixels_number[DARK] < pixels_number[BRIGHT]) { | |
printf("This image is bright.\n"); | |
} | |
else { | |
printf("This image is neutral.\n"); | |
} | |
gdImageDestroy(img); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment