33 std::vector<unsigned char> getRawData(
const freeimage::ImagePtr& image,
const bool flip =
true)
35 #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
39 const auto width = FreeImage_GetWidth(image.get());
40 const auto height = FreeImage_GetHeight(image.get());
41 const auto bpp = FreeImage_GetBPP(image.get());
42 const auto pitch = width * bpp / 8;
44 std::vector<unsigned char> rawData(height * pitch);
45 FreeImage_ConvertToRawBits(rawData.data(), image.get(), pitch, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK,
46 FI_RGBA_BLUE_MASK, flip);
52 auto width = texture->width;
53 auto height = texture->height;
54 for (uint8_t i = 0; i < mip; ++i)
59 const bool flipFace = !texture->isCubeMap();
60 for (uint8_t face = 0; face < texture->getNumFaces(); ++face)
62 const auto offset = face * width;
63 freeimage::ImagePtr faceImg(FreeImage_CreateView(image.get(), offset, 0, offset + width, height));
64 texture->setRawData(getRawData(faceImg, flipFace), face, mip);
71 auto format = FreeImage_GetFileType(filename.c_str());
72 if (format == FIF_UNKNOWN)
73 format = FreeImage_GetFIFFromFilename(filename.c_str());
74 if (format == FIF_UNKNOWN)
82 switch (FreeImage_GetImageType(image.get()))
108 auto width = FreeImage_GetWidth(image.get());
109 const auto height = FreeImage_GetHeight(image.get());
110 const auto bytesPerPixel = FreeImage_GetBPP(image.get()) / 8;
111 const auto channels = bytesPerPixel / depth;
112 FreeImage_FlipVertical(image.get());
126 auto texture = std::make_shared<Texture2D>(textureType, filename, channels, depth, width, height);
130 setRawData(texture, image);
132 const auto path = fs::path(filename).parent_path().string();
133 const auto basename = path +
"/" + fs::path(filename).stem().string();
134 const auto ext = fs::path(filename).extension().string();
136 uint8_t mipLevels = 1;
137 while (fs::exists(basename + std::to_string((
int)mipLevels) + ext))
140 texture->setMipLevels(mipLevels);
142 for (uint8_t mip = 1; mip < mipLevels; ++mip)
144 freeimage::ImagePtr mipImage(FreeImage_Load(format, (basename + std::to_string((
int)mip) + ext).c_str()));
145 FreeImage_FlipVertical(mipImage.get());
147 setRawData(texture, mipImage, mip);
static Texture2DPtr importTextureFromFile(const std::string &filename, const TextureType type)
Import a Texture from file.
std::unique_ptr< FIBITMAP, ImageDeleter > ImagePtr
bool SwapRedBlue32(FIBITMAP *freeImage)
std::shared_ptr< Texture2D > Texture2DPtr