bitmap_ptr finalize(bool showProgress = true) { if (chunks.empty()) { return nullptr; } if (showProgress) { std::cout << "Adding all accepted chunks to the final image\n"; } const auto it = chunks.begin(); bitmap_ptr firstChunk = GenericLoader(*it); auto currentHeight = 0; const auto type = FreeImage_GetImageType(firstChunk.get()); const auto bpp = FreeImage_GetBPP(firstChunk.get()); bitmap_ptr finalImage(FreeImage_AllocateT(type, width, height, bpp)); auto RGBChunkWorker = [=, &finalImage, ¤tHeight](const std::string& el) { bitmap_ptr chunk = GenericLoader(el); auto chunkHeight = FreeImage_GetHeight(chunk.get()); auto chunk_img = FreeImage_Copy(chunk.get(), 0, 0, this->width, chunkHeight); if (chunk_img) { FreeImage_Paste(finalImage.get(), chunk_img, 0, currentHeight, 256); } currentHeight += chunkHeight; }; std::for_each(chunks.begin(), chunks.end(), RGBChunkWorker); return finalImage; }
bool addImgFile(const char* pathName, int flag = 0) { FIBITMAP *img = GenericLoader(pathName, flag); if (img == NULL) return false; chunks.push_back(img); return true; };
int main(int argc, char *argv[]) { // call this ONLY when linking with FreeImage as a static library #ifdef FREEIMAGE_LIB FreeImage_Initialise(); #endif // FREEIMAGE_LIB // initialize your own FreeImage error handler FreeImage_SetOutputMessage(FreeImageErrorHandler); // print version & copyright infos printf("FreeImage version : %s", FreeImage_GetVersion()); printf("\n"); printf(FreeImage_GetCopyrightMessage()); printf("\n"); if(argc != 3) { printf("Usage : CreateAlpha <input file name> <output file name>\n"); return 0; } // Load the source image FIBITMAP *src = GenericLoader(argv[1], 0); if(src) { // Create a transparent image from the lightness image of src FIBITMAP *dst = CreateAlphaFromLightness(src); if(dst) { // Save the destination image bool bSuccess = GenericWriter(dst, argv[2], 0); if(!bSuccess) { printf("\nUnable to save %s file", argv[2]); printf("\nThis format does not support 32-bit images"); } // Free dst FreeImage_Unload(dst); } // Free src FreeImage_Unload(src); } // call this ONLY when linking with FreeImage as a static library #ifdef FREEIMAGE_LIB FreeImage_DeInitialise(); #endif // FREEIMAGE_LIB return 0; }
int main(int argc, char *argv[]) { const char *input_dir = "d:\\images\\"; FIBITMAP *dib = NULL; int id = 1; // call this ONLY when linking with FreeImage as a static library #ifdef FREEIMAGE_LIB FreeImage_Initialise(); #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"); // open the log file FILE *log_file = fopen("log_file.txt", "w"); // batch convert all supported bitmaps _finddata_t finddata; long handle; char image_path[MAX_PATH]; // scan all files strcpy(image_path, input_dir); strcat(image_path, "*.*"); if ((handle = _findfirst(image_path, &finddata)) != -1) { do { // make a path to a directory char *directory = new char[MAX_PATH]; strcpy(directory, input_dir); strcat(directory, finddata.name); // make a unique filename char *unique = new char[128]; itoa(id, unique, 10); strcat(unique, ".png"); // open and load the file using the default load option dib = GenericLoader(directory, 0); if (dib != NULL) { // save the file as PNG bool bSuccess = GenericWriter(dib, unique, PNG_DEFAULT); // free the dib FreeImage_Unload(dib); if(bSuccess) { fwrite(unique, strlen(unique), 1, log_file); } else { strcpy(unique, "FAILED"); fwrite(unique, strlen(unique), 1, log_file); } fwrite(" >> ", 4, 1, log_file); fwrite(directory, strlen(directory), 1, log_file); fwrite("\n", 1, 1, log_file); id++; } delete [] unique; delete [] directory; } while (_findnext(handle, &finddata) == 0); _findclose(handle); } fclose(log_file); // call this ONLY when linking with FreeImage as a static library #ifdef FREEIMAGE_LIB FreeImage_DeInitialise(); #endif // FREEIMAGE_LIB return 0; }
bitmap_ptr finalize(bool showProgress = false) { if (chunks.empty()) { return nullptr; } if (showProgress) { std::cout << "Adding all accepted chunks to the final image\n"; } const auto it = chunks.begin(); bitmap_ptr firstChunk = GenericLoader(*it); const auto type = FreeImage_GetImageType(firstChunk.get()); bitmap_ptr finalImage(FreeImage_Copy(firstChunk.get(), 0, height, width, 0)); auto RGBChunkWorker = [=, &finalImage](const std::string& el) { bitmap_ptr chunk = GenericLoader(el); auto chunkHeight = FreeImage_GetHeight(chunk.get()); for (unsigned int y = 0; y < chunkHeight; ++y) { auto srcbits = reinterpret_cast<FIRGBF *>(FreeImage_GetScanLine(chunk.get(), y)); auto dstbits = reinterpret_cast<FIRGBF *>(FreeImage_GetScanLine(finalImage.get(), y)); for (unsigned int x = 0; x < this->width; ++x) { dstbits[x].red += srcbits[x].red; dstbits[x].blue += srcbits[x].blue; dstbits[x].green += srcbits[x].green; } } }; auto RGBAChunkWorker = [=, &finalImage](const std::string& el) { bitmap_ptr chunk = GenericLoader(el); auto chunkHeight = FreeImage_GetHeight(chunk.get()); for (unsigned int y = 0; y < chunkHeight; ++y) { const auto srcbits = reinterpret_cast<FIRGBAF *>(FreeImage_GetScanLine(chunk.get(), y)); auto dstbits = reinterpret_cast<FIRGBAF *>(FreeImage_GetScanLine(finalImage.get(), y)); for (unsigned int x = 0; x < this->width; ++x) { dstbits[x].red += srcbits[x].red; dstbits[x].blue += srcbits[x].blue; dstbits[x].green += srcbits[x].green; dstbits[x].alpha += srcbits[x].alpha; } } }; auto alphaChunksWorker = [this, &finalImage](const std::string& el) { bitmap_ptr chunk = GenericLoader(el); auto chunkHeight = FreeImage_GetHeight(chunk.get()); for (unsigned int y = 0; y < chunkHeight; ++y) { const auto srcbits = reinterpret_cast<FIRGBAF *>(FreeImage_GetScanLine(chunk.get(), y)); auto dstbits = reinterpret_cast<FIRGBAF *>(FreeImage_GetScanLine(finalImage.get(), y)); for (unsigned int x = 0; x < this->width; ++x) { dstbits[x].alpha += srcbits[x].red + srcbits[x].blue + srcbits[x].green; } } }; if (type == FIT_RGBF) std::for_each(std::next(chunks.begin()), chunks.end(), RGBChunkWorker); else if (type == FIT_RGBAF) std::for_each(std::next(chunks.begin()), chunks.end(), RGBAChunkWorker); std::for_each(alphaChunks.begin(), alphaChunks.end(), alphaChunksWorker); return finalImage; }