__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 ResampleKeepAspectArea(CxImage &image, unsigned int area) { float fAspect = ((float)image.GetWidth()) / ((float)image.GetHeight()); unsigned int width = (unsigned int)sqrt(area * fAspect); unsigned int height = (unsigned int)sqrt(area / fAspect); if (width > MAX_WIDTH) width = MAX_WIDTH; if (height > MAX_HEIGHT) height = MAX_HEIGHT; return ResampleKeepAspect(image, width, height, true); }
__declspec(dllexport) bool CreateFolderThumbnail(const char **file, const char *thumb, int maxWidth, int maxHeight) { if (!file || !file[0] || !file[1] || !file[2] || !file[3] || !thumb) return false; CxImage folderimage(maxWidth, maxHeight, 32, CXIMAGE_FORMAT_PNG); folderimage.AlphaCreate(); int iWidth = maxWidth / 2; int iHeight = maxHeight / 2; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int width = iWidth; int height = iHeight; bool bBlank = false; if (strlen(file[i*2 + j]) == 0) bBlank = true; if (!bBlank) { CxImage image; if (image.Load(file[i*2 + j], CXIMAGE_FORMAT_JPG, width, height)) { // resize image to iWidth if (ResampleKeepAspect(image, iWidth - 2, iHeight - 2) >= 0) { int iOffX = (iWidth - 2 - image.GetWidth()) / 2; int iOffY = (iHeight - 2 - image.GetHeight()) / 2; for (int x = 0; x < iWidth; x++) { for (int y = 0; y < iHeight; y++) { RGBQUAD rgb; if (x < iOffX || x >= iOffX + (int)image.GetWidth() || y < iOffY || y >= iOffY + (int)image.GetHeight()) { rgb.rgbBlue = 0; rgb.rgbGreen = 0; rgb.rgbRed = 0; rgb.rgbReserved = 0; } else { rgb = image.GetPixelColor(x - iOffX, y - iOffY); rgb.rgbReserved = 255; } folderimage.SetPixelColor(x + j*iWidth, y + (1 - i)*iHeight, rgb, true); } } } else bBlank = true; } else bBlank = true; } if (bBlank) { // no image - just fill with black alpha for (int x = 0; x < iWidth; x++) { for (int y = 0; y < iHeight; y++) { RGBQUAD rgb; rgb.rgbBlue = 0; rgb.rgbGreen = 0; rgb.rgbRed = 0; rgb.rgbReserved = 0; folderimage.SetPixelColor(x + j*iWidth, y + (1 - i)*iHeight, rgb, true); } } } } } ::DeleteFile(thumb); if (!folderimage.Save(thumb, CXIMAGE_FORMAT_PNG)) { printf("Unable to save thumb file"); ::DeleteFile(thumb); return false; } return true; };
__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); };