1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include "gd.h"
#include<iostream>
int main() {
FILE *in;
gdImagePtr im;
in = fopen("point.jpg", "rb");
im = gdImageCreateFromJpeg(in);
fclose(in);
gdImagePtr imResult;
FILE *jpegout;
int width = gdImageSX(im);
int height = gdImageSY(im);
imResult = gdImageCreate(width, height);
// int noir = gdImageColorAllocate(imResult, 0, 0, 0);
// int blanc = gdImageColorAllocate(imResult, 255, 255, 255);
int c;
int pixelColor;
int red, green, blue;
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
c = gdImageGetPixel(im, i, j);
int red = gdImageRed(im, c);
int green = gdImageGreen(im, c);
int blue = gdImageBlue(im, c);
// METHODE 1 -----
/* // Try to allocate red directly
pixelColor = gdImageColorExact(imResult, red, green, blue);
// If red isn't already present...
if (pixelColor == (-1)) {
// Second best: try to allocate it directly.
pixelColor = gdImageColorAllocate(imResult, red, green, blue);
// Out of colors, so find the closest color instead.
pixelColor = gdImageColorClosest(imResult, red, green, blue);
}
*/
// METHODE 2 -----
pixelColor = gdImageColorResolve(imResult, red, green, blue);
gdImageSetPixel(imResult, i, j, pixelColor);
}
}
jpegout = fopen("imageResult.jpg", "wb");
gdImageJpeg(imResult, jpegout, -1);
fclose(jpegout);
gdImageDestroy(imResult);
gdImageDestroy(im);
} |
Partager