bool SaveThumb(CxImage &image, const char *file, const char *thumb, int maxWidth, int maxHeight, bool bNeedToConvert = true, bool autoRotate = true) { // ok, now resample the image down if necessary int ret = ResampleKeepAspectArea(image, maxWidth * maxHeight); if (ret < 0) return false; if (ret) bNeedToConvert = true; // if we don't have a png but have a < 24 bit image, then convert to 24bits if ( image.GetNumColors()) { if (!image.IncreaseBpp(24) || !image.IsValid()) { printf("PICTURE::SaveThumb: Unable to convert to 24bpp: Error:%s\n", image.GetLastError()); return false; } bNeedToConvert = true; } if ( autoRotate && image.GetExifInfo()->Orientation > 1) { image.RotateExif(image.GetExifInfo()->Orientation); bNeedToConvert = true; } #ifndef _LINUX ::DeleteFile(thumb); #else unlink(thumb); #endif // only resave the image if we have to (quality of the JPG saver isn't too hot!) if (bNeedToConvert) { // May as well have decent quality thumbs image.SetJpegQuality(90); if (!image.Save(thumb, image.AlphaIsValid() ? CXIMAGE_FORMAT_PNG : CXIMAGE_FORMAT_JPG)) { printf("PICTURE::SaveThumb: Unable to save image: %s Error:%s\n", thumb, image.GetLastError()); ::DeleteFile(thumb); return false; } } else { // Don't need to convert the file - copy it instead if (!CopyFile(file, thumb)) { printf("PICTURE::SaveThumb: Unable to copy file %s\n", file); ::DeleteFile(thumb); return false; } } return true; }
int ResampleKeepAspect(CxImage &image, unsigned int width, unsigned int height) { bool bResize = false; float fAspect = ((float)image.GetWidth()) / ((float)image.GetHeight()); unsigned int newwidth = image.GetWidth(); unsigned int newheight = image.GetHeight(); if (newwidth > width) { bResize = true; newwidth = width; newheight = (DWORD)( ( (float)newwidth) / fAspect); } if (newheight > height) { bResize = true; newheight = height; newwidth = (DWORD)( fAspect * ( (float)newheight) ); } if (bResize) { if (!image.Resample(newwidth, newheight, RESAMPLE_QUALITY) || !image.IsValid()) { printf("PICTURE::SaveThumb: Unable to resample picture: Error:%s\n", image.GetLastError()); return -1; } } return bResize ? 1 : 0; }
__declspec(dllexport) bool LoadImage(const char *file, unsigned int maxwidth, unsigned int maxheight, ImageInfo *info) { if (!file || !info) return false; if (IsDir(file)) return false; // load the image DWORD dwImageType = GetImageType(file); CxImage *image = new CxImage(dwImageType); if (!image) return false; int actualwidth = maxwidth; int actualheight = maxheight; try { if (!image->Load(file, dwImageType, actualwidth, actualheight) || !image->IsValid()) { #if !defined(_LINUX) && !defined(__APPLE__) int nErr = GetLastError(); #else int nErr = errno; #endif printf("PICTURE::LoadImage: Unable to open image: %s Error:%s (%d)\n", file, image->GetLastError(),nErr); delete image; return false; } } catch (...) { printf("PICTURE::LoadImage: Unable to open image: %s\n", file); delete image; return false; } // ok, now resample the image down if necessary if (ResampleKeepAspect(*image, maxwidth, maxheight) < 0) { printf("PICTURE::LoadImage: Unable to resample picture: %s\n", file); delete image; return false; } // make sure our image is 24bit minimum image->IncreaseBpp(24); // fill in our struct info->width = image->GetWidth(); info->height = image->GetHeight(); info->originalwidth = actualwidth; info->originalheight = actualheight; memcpy(&info->exifInfo, image->GetExifInfo(), sizeof(EXIFINFO)); // create our texture info->context = image; info->texture = image->GetBits(); info->alpha = image->AlphaGetBits(); return (info->texture != NULL); };
int main(int argc, char* argv[]) { CxImage image; if (argc<3) { fprintf(stderr, image.GetVersion()); fprintf(stderr, "\nConsole demo\n"); fprintf(stderr, "usage: %s input-file output-file\n", argv[0]); return 1; } CString filein(argv[1]); CString extin(FindExtension(filein)); extin.MakeLower(); int typein = FindFormat(extin); if (typein == CXIMAGE_FORMAT_UNKNOWN) { fprintf(stderr, "unknown extension for %s\n", argv[1]); return 1; } CString fileout(argv[2]); CString extout(FindExtension(fileout)); extout.MakeLower(); int typeout = FindFormat(extout); if (typeout == CXIMAGE_FORMAT_UNKNOWN) { fprintf(stderr, "unknown extension for %s\n", argv[2]); return 1; } if (!image.Load(argv[1],typein)){ fprintf(stderr, "%s\n", image.GetLastError()); fprintf(stderr, "error loading %s\n", argv[1]); return 1; } if (!image.Save(argv[2],typeout)){ fprintf(stderr, "%s\n", image.GetLastError()); fprintf(stderr, "error saving %s\n", argv[2]); return 1; } printf("Done!\n"); return 0; }
__declspec(dllexport) bool LoadImageFromMemory(const BYTE *buffer, unsigned int size, const char *mime, unsigned int maxwidth, unsigned int maxheight, ImageInfo *info) { if (!buffer || !size || !mime || !info) return false; // load the image DWORD dwImageType = CXIMAGE_FORMAT_UNKNOWN; if (strlen(mime)) dwImageType = GetImageType(mime); if (dwImageType == CXIMAGE_FORMAT_UNKNOWN) dwImageType = DetectFileType(buffer, size); if (dwImageType == CXIMAGE_FORMAT_UNKNOWN) { printf("PICTURE::LoadImageFromMemory: Unable to determine image type."); return false; } CxImage *image = new CxImage(dwImageType); if (!image) return false; int actualwidth = maxwidth; int actualheight = maxheight; try { bool success = image->Decode((BYTE*)buffer, size, dwImageType, actualwidth, actualheight); if (!success && dwImageType != CXIMAGE_FORMAT_UNKNOWN) { // try to decode with unknown imagetype success = image->Decode((BYTE*)buffer, size, CXIMAGE_FORMAT_UNKNOWN); } if (!success || !image->IsValid()) { printf("PICTURE::LoadImageFromMemory: Unable to decode image. Error:%s\n", image->GetLastError()); delete image; return false; } } catch (...) { printf("PICTURE::LoadImageFromMemory: Unable to decode image."); delete image; return false; } // ok, now resample the image down if necessary if (ResampleKeepAspect(*image, maxwidth, maxheight) < 0) { printf("PICTURE::LoadImage: Unable to resample picture\n"); delete image; return false; } // make sure our image is 24bit minimum image->IncreaseBpp(24); // fill in our struct info->width = image->GetWidth(); info->height = image->GetHeight(); info->originalwidth = actualwidth; info->originalheight = actualheight; memcpy(&info->exifInfo, image->GetExifInfo(), sizeof(EXIFINFO)); // create our texture info->context = image; info->texture = image->GetBits(); info->alpha = image->AlphaGetBits(); return (info->texture != NULL); };