| 6 | |
| 7 | |
| 8 | std::vector<unsigned char> ImageIO::loadFromMemoryRGBA32(const unsigned char * data, const size_t size, size_t & width, size_t & height) |
| 9 | { |
| 10 | std::vector<unsigned char> rawData; |
| 11 | width = 0; |
| 12 | height = 0; |
| 13 | FIMEMORY * fiMemory = FreeImage_OpenMemory((BYTE *)data, size); |
| 14 | if (fiMemory != nullptr) { |
| 15 | //detect the filetype from data |
| 16 | FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeFromMemory(fiMemory); |
| 17 | if (format != FIF_UNKNOWN && FreeImage_FIFSupportsReading(format)) |
| 18 | { |
| 19 | //file type is supported. load image |
| 20 | FIBITMAP * fiBitmap = FreeImage_LoadFromMemory(format, fiMemory); |
| 21 | if (fiBitmap != nullptr) |
| 22 | { |
| 23 | //loaded. convert to 32bit if necessary |
| 24 | FIBITMAP * fiConverted = nullptr; |
| 25 | if (FreeImage_GetBPP(fiBitmap) != 32) |
| 26 | { |
| 27 | FIBITMAP * fiConverted = FreeImage_ConvertTo32Bits(fiBitmap); |
| 28 | if (fiConverted != nullptr) |
| 29 | { |
| 30 | //free original bitmap data |
| 31 | FreeImage_Unload(fiBitmap); |
| 32 | fiBitmap = fiConverted; |
| 33 | } |
| 34 | } |
| 35 | if (fiBitmap != nullptr) |
| 36 | { |
| 37 | width = FreeImage_GetWidth(fiBitmap); |
| 38 | height = FreeImage_GetHeight(fiBitmap); |
| 39 | unsigned int pitch = FreeImage_GetPitch(fiBitmap); |
| 40 | //loop through scanlines and add all pixel data to the return vector |
| 41 | //this is necessary, because width*height*bpp might not be == pitch |
| 42 | unsigned char * tempData = new unsigned char[width * height * 4]; |
| 43 | for (size_t i = 0; i < height; i++) |
| 44 | { |
| 45 | const BYTE * scanLine = FreeImage_GetScanLine(fiBitmap, i); |
| 46 | memcpy(tempData + (i * width * 4), scanLine, width * 4); |
| 47 | } |
| 48 | //convert from BGRA to RGBA |
| 49 | for(size_t i = 0; i < width*height; i++) |
| 50 | { |
| 51 | RGBQUAD bgra = ((RGBQUAD *)tempData)[i]; |
| 52 | RGBQUAD rgba; |
| 53 | rgba.rgbBlue = bgra.rgbRed; |
| 54 | rgba.rgbGreen = bgra.rgbGreen; |
| 55 | rgba.rgbRed = bgra.rgbBlue; |
| 56 | rgba.rgbReserved = bgra.rgbReserved; |
| 57 | ((RGBQUAD *)tempData)[i] = rgba; |
| 58 | } |
| 59 | rawData = std::vector<unsigned char>(tempData, tempData + width * height * 4); |
| 60 | //free bitmap data |
| 61 | FreeImage_Unload(fiBitmap); |
| 62 | delete[] tempData; |
| 63 | } |
| 64 | } |
| 65 | else |
nothing calls this directly
no outgoing calls
no test coverage detected