// ========================================================== // Load From Handle Example // // Design and implementation by // - Herv Drolon // // Rewritten by D.Bockus July 15 05, to show how you can create your own data structure // apply a simple filter and write the info back to the free image data structure. // Below is all the original disclaimer nobody reads. // This file is part of FreeImage 3 // // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER // THIS DISCLAIMER. // // Use at own risk! // ========================================================== // This example shows how to load a bitmap from a // user allocated FILE pointer. // // Functions used in this sample : // FreeImage_GetFormatFromFIF, FreeImage_GetFileTypeFromHandle, FreeImage_LoadFromHandle, // FreeImage_GetFIFFromFilename, FreeImage_Save, FreeImage_Unload // FreeImage_GetVersion, FreeImage_GetCopyrightMessage, FreeImage_SetOutputMessage // // ========================================================== #if !defined(Linux) #include //Not Linux must be windows #include #endif #include #include #include #include #define Verbose 0 //1 == Verbose, 0 is silent typedef struct pix { //pix is a type used for holding the RGB component of the image int R; int G; int B; int RES; //REServed for the application } Pixel; //Name pix Pixel Pixel** myImage; //myImage is a pointer to a pointer of pix int myIm_Height = 0; //Stores the Image Height int myIm_Width = 0; //Stores the Image Width /** This function is an example how to allocate memory for a bitmap (user defined). */ Pixel** CreateMemory(int Width, int Height){ Pixel** myArray; //Int pointer to a pointer int x,y; myIm_Height = Height; //Save these values so we do not need to recompute myIm_Width = Width; // An array of pointers-to-an array of Pixel) // size is Width of the image // myArray=(Pixel**)malloc(Width*sizeof(Pixel*)); #if Verbose printf("In CreateMemory 1 \n"); #endif // Now, each element in the previous array is treated as a // pointer-to-Pixel, also known as an array of Pixel // //For each pointer create the array of Pixels for (x=0; x 128) Im[x][y].B = 255; else Im[x][y].B = 0; if (Im[x][y].G > 128) Im[x][y].G = 255; else Im[x][y].G = 0; if (Im[x][y].R > 128) Im[x][y].R = 255; else Im[x][y].R = 0; } } /* Copies the values in "my" image data structure back to the one provided by free Image. */ void MyWriteImage(FIBITMAP *dib, Pixel** Im){ int x,y; RGBQUAD colour; for (x=0; x < myIm_Width; x++) for (y=0; y < myIm_Height; y++){ colour.rgbRed =Im[x][y].R; colour.rgbGreen = Im[x][y].G; colour.rgbBlue = Im[x][y].B; colour.rgbReserved = Im[x][y].RES; FreeImage_SetPixelColor(dib,x,y,&colour); } } // ---------------------------------------------------------- /** FreeImage error handler @param fif Format / Plugin responsible for the error @param message Error message */ void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) { printf("\n*** "); printf("%s Format\n", FreeImage_GetFormatFromFIF(fif)); printf(message); printf(" ***\n"); } // ---------------------------------------------------------- unsigned DLL_CALLCONV myReadProc(void *buffer, unsigned size, unsigned count, fi_handle handle) { return (int) fread(buffer, size, count, (FILE *)handle); } unsigned DLL_CALLCONV myWriteProc(void *buffer, unsigned size, unsigned count, fi_handle handle) { return (int) fwrite(buffer, size, count, (FILE *)handle); } int DLL_CALLCONV mySeekProc(fi_handle handle, long offset, int origin) { return fseek((FILE *)handle, offset, origin); } long DLL_CALLCONV myTellProc(fi_handle handle) { return ftell((FILE *)handle); } // ---------------------------------------------------------- int main(int argc, char *argv[]) { FILE *file; FreeImageIO io; FREE_IMAGE_FORMAT out_fif; const char *output_filename; // call this ONLY when linking with FreeImage as a static library #ifdef FREEIMAGE_LIB printf("Using static libs\n"); FreeImage_Initialise(TRUE); #endif // FREEIMAGE_LIB // initialize your own FreeImage error handler FreeImage_SetOutputMessage(FreeImageErrorHandler); // print version & copyright infos printf(FreeImage_GetVersion()); printf("\n"); printf(FreeImage_GetCopyrightMessage()); printf("\n"); if(argc < 2) { printf("Usage : LoadFromHandle \n"); return 0; } // initialize your own IO functions io.read_proc = myReadProc; io.write_proc = myWriteProc; io.seek_proc = mySeekProc; io.tell_proc = myTellProc; file = fopen(argv[1], "rb"); #if Verbose printf("Parameter input %s %s\n",argv[1], argv[2]); #endif if (file != NULL) { // find the buffer format FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)file, 0); #if Verbose printf("Image format type is %d\n",fif); #endif if(fif != FIF_UNKNOWN) { // load from the file handle FIBITMAP *mydib = FreeImage_LoadFromHandle(fif, &io, (fi_handle)file, 0); // Demonstrates making a copy of the i/p image FIBITMAP *clone = FreeImage_Clone(mydib); // Next sequence of statements demonstrates how to create your own // data structure, Copy the image into it. Apply a filter, and then // copy it back. We will let free image deal with saving the image. PrintImageProperties(clone); myImage = CreateMemory(FreeImage_GetWidth(clone),FreeImage_GetHeight(clone)); MyLoadImage(clone,myImage); MyFilter(myImage); MyWriteImage(clone,myImage); // save the bitmap as a User defined format ... Dependent on the // file name specified in the command line. output_filename = argv[2]; // first, check the output format from the file name or file extension out_fif = FreeImage_GetFIFFromFilename(output_filename); if(out_fif != FIF_UNKNOWN) { // then save the file FreeImage_Save(out_fif, clone, output_filename, 0); } // free the loaded FIBITMAP, close any file before you unload the image fclose(file); //file for mydib FreeImage_Unload(mydib); FreeImage_Unload(clone); } } // call this ONLY when linking with FreeImage as a static library #ifdef FREEIMAGE_LIB FreeImage_DeInitialise(); #endif // FREEIMAGE_LIB printf("hit return to exit");getchar(); return 0; }