#include "FreeImage.h" typedef struct { int r, g, b; } pixel; pixel **read_img(char *name, int *width, int *height) { FIBITMAP *image; int i,j; RGBQUAD aPixel; pixel **data; if((image = FreeImage_Load(FIF_TIFF, name, 0)) == NULL) { perror("FreeImage_Load"); return NULL; } *width = FreeImage_GetWidth(image); *height = FreeImage_GetHeight(image); data = (pixel **)malloc((*height)*sizeof(pixel *)); for(i = 0 ; i < (*height) ; i++) { data[i] = (pixel *)malloc((*width)*sizeof(pixel)); for(j = 0 ; j < (*width) ; j++) { FreeImage_GetPixelColor(image, j, i, &aPixel); data[i][j].r = (aPixel.rgbRed); data[i][j].g = (aPixel.rgbGreen); data[i][j].b = (aPixel.rgbBlue); } } FreeImage_Unload(image); return data; } /* A simple thresholding filter. */ void MyFilter(Pixel** data){ int x,y; for (x=0; x < myIm_Width; x++) for (y=0; y < myIm_Height; y++){ if (data[x][y].B > 128) data[x][y].B = 255; else data[x][y].B = 0; if (data[x][y].G > 128) data[x][y].G = 255; else data[x][y].G = 0; if (data[x][y].R > 128) data[x][y].R = 255; else data[x][y].R = 0; } } void write_img(char *name, pixel **data, int width, int height) { FIBITMAP *image; RGBQUAD aPixel; int i,j; image = FreeImage_Allocate(width, height, 24, 0, 0, 0); if(!image) { perror("FreeImage_Allocate"); return; } for(i = 0 ; i < height ; i++) { for(j = 0 ; j < width ; j++) { aPixel.rgbRed = data[i][j].r; aPixel.rgbGreen = data[i][j].g; aPixel.rgbBlue = data[i][j].b; FreeImage_SetPixelColor(image, j, i, &aPixel); } } if(!FreeImage_Save(FIF_TIFF, image, name, 0)) { perror("FreeImage_Save"); } FreeImage_Unload(image); } int main() { pixel **data; int w, h; data = read_img("spit06.tif", &w, &h); write_img("backup.tif", data, w, h); return 0; }