Why FreeImage loads image in BGR format?

青春壹個敷衍的年華 提交于 2019-12-13 12:16:17

问题


I write game using Android NDK and FreeImage library to load images into my game. It loads images in BGR format. It is possible to load in RGB format? Or I need to swap R and B components manualy?

Edited

My device is Samsung Galaxy S4(armv7 archtecture). Here is code`

FIMEMORY* fiStream = FreeImage_OpenMemory((BYTE*)data, size);
FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(fiStream);
if (FreeImage_FIFSupportsReading(fif))
{
    dib = FreeImage_LoadFromMemory(fif, fiStream, 0);
}

if (!dib)
    return;

data_ = FreeImage_GetBits(dib);
width_ = FreeImage_GetWidth(dib);
height_ = FreeImage_GetHeight(dib);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB, GL_UNSIGNED_BYTE, data_ );

回答1:


FreeImage actually doesn't neccessarily use BGR(A) ordering. Section "Pixel access functions" in the FreeImage documentation contains the following statement (emphasis mine):

However, the pixel layout used by this model is OS dependant. Using a byte by byte memory order to label the pixel layout, then FreeImage uses a BGR[A] pixel layout under a Little Endian processor (Windows, Linux) and uses a RGB[A] pixel layout under a Big Endian processor (Mac OS X or any Big Endian Linux / Unix). This choice was made to ease the use of FreeImage with graphics API.

This subtle difference is however transparent to the user. In order to make pixel access OS independent, FreeImage defines a set of macros used to set or get individual color components in a 24- or 32-bit DIB.

However, these macros will not help you when you want to use FreeImage's raw pixel data as source for OpenGL.

But I don't know what your issue with this is. OpenGL will accept BGR(A) as well as RGB(A) as image data. And actually, FreeImage's choice is not bad, as BGR(A) is most likely the native data format on such platforms, so that using this format represents the most efficient way to transfer image data.




回答2:


FreeImage can be built to always load in RGB order independent of the architecture. Use #define FREEIMAGE_COLORORDER=FREEIMAGE_COLORORDER_RGB. If you're using Android.mk to build FreeImage, add the following line to it:

LOCAL_CPPFLAGS += -DFREEIMAGE_COLORORDER=FREEIMAGE_COLORORDER_RGB

Documentation for this can be found in Source/FreeImage.h if you search for FREEIMAGE_COLORORDER:

Color-Order:

The specified order of color components red, green and blue affects 24- and 32-bit images of type FIT_BITMAP as well as the colors that are part of a color palette. All other images always use RGB order. By default, color order is coupled to endianness:

little-endian -> BGR

big-endian -> RGB

However, you can always define FREEIMAGE_COLORORDER to any of the known orders FREEIMAGE_COLORORDER_BGR (0) and FREEIMAGE_COLORORDER_RGB (1) to specify your preferred color order.



来源:https://stackoverflow.com/questions/31103121/why-freeimage-loads-image-in-bgr-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!