1
0
Fork 0
Univerxel/src/client/render/api/Images.cpp

68 lines
1.6 KiB
C++

#include "Images.hpp"
using namespace render;
std::unique_ptr<Texture> (*Texture::loadFunc)(const std::string&, const sampling&) = nullptr;
#include <stdio.h>
#include <string.h>
#include <string>
#define FOURCC_DXT1 0x31545844 // Equivalent to "DXT1" in ASCII
#define FOURCC_DXT3 0x33545844 // Equivalent to "DXT3" in ASCII
#define FOURCC_DXT5 0x35545844 // Equivalent to "DXT5" in ASCII
std::optional<Image::properties> Image::Read(const std::string& imagepath, std::vector<unsigned char>& data) {
unsigned char header[124];
properties info;
FILE *fp;
/* try to open the file */
fp = fopen(imagepath.c_str(), "rb");
if (fp == NULL){
printf("%s could not be opened.\n", imagepath.c_str()); getchar();
return {};
}
/* verify the type of file */
char filecode[4];
fread(filecode, 1, 4, fp);
if (strncmp(filecode, "DDS ", 4) != 0) {
fclose(fp);
return {};
}
/* get the surface desc */
fread(&header, 124, 1, fp);
info.size.height = *(unsigned int*)&(header[8 ]);
info.size.width = *(unsigned int*)&(header[12]);
unsigned int linearSize = *(unsigned int*)&(header[16]);
info.mipmapLevels = *(unsigned int*)&(header[24]);
unsigned int fourCC = *(unsigned int*)&(header[80]);
/* how big is it going to be including all mipmaps? */
unsigned int bufsize = info.mipmapLevels > 1 ? linearSize * 2 : linearSize;
data.resize(bufsize);
fread(data.data(), 1, bufsize, fp);
/* close the file pointer */
fclose(fp);
switch(fourCC)
{
case FOURCC_DXT3:
info.format = Format::BC2;
break;
case FOURCC_DXT5:
info.format = Format::BC3;
break;
//MAYBE: VK_FORMAT_BC6H_SFLOAT_BLOCK
default:
return {};
}
return info;
}