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; }
__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); };
__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); };