70 unsigned int depth = 32;
71 if (cd == VuoImageColorDepth_16) depth = 64;
72 if (cd == VuoImageColorDepth_32) depth = 128;
75 unsigned int bufferFormat = GL_BGRA;
76 if (image->glInternalFormat == GL_DEPTH_COMPONENT)
77 bufferFormat = GL_DEPTH_COMPONENT16;
78 else if (image->glInternalFormat == GL_LUMINANCE
79 || image->glInternalFormat == GL_LUMINANCE16F_ARB
80 || image->glInternalFormat == GL_LUMINANCE32F_ARB)
81 bufferFormat = GL_R16;
83 || image->glInternalFormat == GL_LUMINANCE_ALPHA16F_ARB)
84 bufferFormat = GL_RGBA16I_EXT;
85 else if (depth == 128)
88 bufferFormat = GL_RGBA32F_ARB;
90 bufferFormat = GL_RGBA16I_EXT;
95 VUserLog(
"Error: Couldn't get pixels.");
99 int width = image->pixelsWide;
100 int height = image->pixelsHigh;
103 if (bufferFormat == GL_BGRA)
105 VDebugLog(
"Output format: RGBA 8bpc integer%s", unpremultiply ?
" (unpremultiplied)" :
"");
106 const unsigned char *b = (
unsigned char *)buffer;
107 fibmp = FreeImage_AllocateT(FIT_BITMAP, width, height, depth, FI_RGBA_BLUE_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_RED_MASK);
109 for (
long int y = 0; y < height; ++y)
111 unsigned char *d = (
unsigned char *)FreeImage_GetScanLine(fibmp, y);
112 for (
long int x = 0; x < width; ++x)
114 float alpha = (float)b[3] / 255.;
126 memcpy(FreeImage_GetBits(fibmp), buffer, width*height*4);
128 else if (bufferFormat == GL_RGBA16I_EXT)
130 bool mono = (image->glInternalFormat == GL_LUMINANCE_ALPHA16F_ARB);
131 VDebugLog(
"Output format: RGBA 16bpc integer%s%s", mono?
" (mono)":
"", unpremultiply ?
" (unpremultiplied)" :
"");
132 const unsigned short *b = (
const unsigned short *)buffer;
133 fibmp = FreeImage_AllocateT(FIT_RGBA16, width, height, depth, FI_RGBA_BLUE_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_RED_MASK);
135 for (
long int y = 0; y < height; ++y)
137 unsigned short *d = (
unsigned short *)FreeImage_GetScanLine(fibmp, y);
138 for (
long int x = 0; x < width; ++x)
140 float alpha = (float)b[3] / 65535.;
152 for (
long int y = 0; y < height; ++y)
154 unsigned short *d = (
unsigned short *)FreeImage_GetScanLine(fibmp, y);
155 for (
long int x = 0; x < width; ++x)
166 else if (bufferFormat == GL_RGBA32F_ARB)
168 bool mono = (image->glInternalFormat == GL_LUMINANCE_ALPHA32F_ARB);
169 VDebugLog(
"Output format: RGBA 32bpc float%s%s", mono?
" (mono)":
"", unpremultiply ?
" (unpremultiplied)" :
"");
170 const float *b = (
const float *)buffer;
171 fibmp = FreeImage_AllocateT(FIT_RGBAF, width, height, depth, FI_RGBA_BLUE_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_RED_MASK);
173 for (
long int y = 0; y < height; ++y)
175 float *d = (
float *)FreeImage_GetScanLine(fibmp, y);
176 for (
long int x = 0; x < width; ++x)
179 d[0] = b[ 2] / alpha;
180 d[1] = b[mono?2:1] / alpha;
181 d[2] = b[mono?2:0] / alpha;
188 for (
long int y = 0; y < height; ++y)
190 float *d = (
float *)FreeImage_GetScanLine(fibmp, y);
191 for (
long int x = 0; x < width; ++x)
202 else if (bufferFormat == GL_DEPTH_COMPONENT16
203 || bufferFormat == GL_R16)
205 VDebugLog(
"Output format: mono 16bpc integer");
206 const unsigned short *b = (
const unsigned short *)buffer;
207 fibmp = FreeImage_AllocateT(FIT_UINT16, width, height, 16, 0, 0, 0);
208 for (
long int y = 0; y < height; ++y)
210 unsigned short *d = (
unsigned short *)FreeImage_GetScanLine(fibmp, y);
211 memcpy(d, b + y*width, width*2);
216 VUserLog(
"Error: Unknown bufferFormat.");
241 unsigned char *pixels;
242 unsigned long pixelsWide;
243 unsigned long pixelsHigh;
245 pixelsWide = FreeImage_GetWidth(fi);
246 pixelsHigh = FreeImage_GetHeight(fi);
248 const FREE_IMAGE_TYPE type = FreeImage_GetImageType(fi);
249 const unsigned int bpp = FreeImage_GetBPP(fi);
250 const FREE_IMAGE_COLOR_TYPE colorType = FreeImage_GetColorType(fi);
251 const FIICCPROFILE *colorProfile = FreeImage_GetICCProfile(fi);
252 FITAG *exifColorSpace = NULL;
253 FreeImage_GetMetadata(FIMD_EXIF_EXIF, fi,
"ColorSpace", &exifColorSpace);
254 VDebugLog(
"ImageType=%d BPP=%d colorType=%s Profile=%s(%d) EXIF_ColorSpace=%s",
257 colorType == FIC_MINISWHITE ?
"minIsWhite" :
258 (colorType == FIC_MINISBLACK ?
"minIsBlack" :
259 (colorType == FIC_RGB ?
"RGB" :
260 (colorType == FIC_PALETTE ?
"indexed" :
261 (colorType == FIC_RGBALPHA ?
"RGBA" :
262 (colorType == FIC_CMYK ?
"CMYK" :
"unknown"))))),
263 colorProfile->flags & FIICC_COLOR_IS_CMYK ?
"CMYK" :
"RGB",
265 FreeImage_TagToString(FIMD_EXIF_EXIF, exifColorSpace, NULL));
267 if (type == FIT_FLOAT
268 || type == FIT_DOUBLE
269 || type == FIT_UINT16
271 || type == FIT_UINT32
272 || type == FIT_INT32)
275 colorDepth = VuoImageColorDepth_32;
276 format = GL_LUMINANCE;
278 FIBITMAP *fiFloat = FreeImage_ConvertToFloat(fi);
279 FreeImage_Unload(fi);
282 else if (type == FIT_RGB16
286 colorDepth = VuoImageColorDepth_32;
289 FIBITMAP *fiFloat = FreeImage_ConvertToRGBF(fi);
290 FreeImage_Unload(fi);
293 else if (type == FIT_RGBA16
294 || type == FIT_RGBAF)
297 colorDepth = VuoImageColorDepth_32;
300 FIBITMAP *fiFloat = FreeImage_ConvertToRGBAF(fi);
301 FreeImage_Unload(fi);
305 float *pixels = (
float *)FreeImage_GetBits(fi);
308 VUserLog(
"Error: '%s': FreeImage couldn't get this image's pixel data. The file might be corrupted.", imageURL);
309 FreeImage_Unload(fi);
313 for (
int y = 0; y < pixelsHigh; ++y)
314 for (
int x = 0; x < pixelsWide; ++x)
316 float alpha = pixels[(y * pixelsWide + x) * 4 + 3];
317 pixels[(y * pixelsWide + x) * 4 + 0] *= alpha;
318 pixels[(y * pixelsWide + x) * 4 + 1] *= alpha;
319 pixels[(y * pixelsWide + x) * 4 + 2] *= alpha;
325 colorDepth = VuoImageColorDepth_8;
327 if (colorType == FIC_MINISWHITE
328 || colorType == FIC_MINISBLACK)
330 format = GL_LUMINANCE;
332 FIBITMAP *fiConverted = FreeImage_ConvertTo8Bits(fi);
333 FreeImage_Unload(fi);
336 else if (colorType == FIC_RGB
337 || (colorType == FIC_PALETTE && !FreeImage_IsTransparent(fi)))
341 FIBITMAP *fiConverted = FreeImage_ConvertTo24Bits(fi);
342 FreeImage_Unload(fi);
345 else if (colorType == FIC_RGBALPHA
346 || (colorType == FIC_PALETTE && FreeImage_IsTransparent(fi)))
350 FIBITMAP *fiConverted = FreeImage_ConvertTo32Bits(fi);
351 FreeImage_Unload(fi);
354 if (!FreeImage_PreMultiplyWithAlpha(fi))
355 VUserLog(
"Warning: Premultiplication failed.");
359 VUserLog(
"Error: '%s': Unknown colorType %d.", imageURL, colorType);
360 FreeImage_Unload(fi);
366 pixels = FreeImage_GetBits(fi);
369 VUserLog(
"Error: '%s': FreeImage couldn't get this image's pixel data. The file might be corrupted.", imageURL);
370 FreeImage_Unload(fi);
380 bytesPerRow = (bytesPerRow + 3) & ~0x3;
383 void (^freeCallback)(
void *buffer) = ^(
void *buffer) {
384 FreeImage_Unload(fi);