bool Texture::LoadFromSTDIO(FILE* a_pFile) { if(!a_pFile) return false; png_image image; memset(&image, 0, (sizeof image)); image.version = PNG_IMAGE_VERSION; png_image_begin_read_from_stdio(&image, a_pFile); if(image.format == PNG_FORMAT_RGBA) m_Format = GL_RGBA; else m_Format = GL_RGB; m_pData = new unsigned char[PNG_IMAGE_SIZE(image)]; png_image_finish_read(&image, NULL, m_pData, 0, NULL); m_Width = image.width; m_Height = image.height; m_Type = GL_UNSIGNED_BYTE; return true; }
void ReplacedTexture::Load(int level, void *out, int rowPitch) { _assert_msg_(G3D, (size_t)level < levels_.size(), "Invalid miplevel"); _assert_msg_(G3D, out != nullptr && rowPitch > 0, "Invalid out/pitch"); const ReplacedTextureLevel &info = levels_[level]; #ifdef USING_QT_UI ERROR_LOG(G3D, "Replacement texture loading not implemented for Qt"); #else png_image png = {}; png.version = PNG_IMAGE_VERSION; FILE *fp = File::OpenCFile(info.file, "rb"); if (!png_image_begin_read_from_stdio(&png, fp)) { ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", info.file.c_str(), png.message); return; } bool checkedAlpha = false; if ((png.format & PNG_FORMAT_FLAG_ALPHA) == 0) { // Well, we know for sure it doesn't have alpha. if (level == 0) { alphaStatus_ = ReplacedTextureAlpha::FULL; } checkedAlpha = true; } png.format = PNG_FORMAT_RGBA; if (!png_image_finish_read(&png, nullptr, out, rowPitch, nullptr)) { ERROR_LOG(G3D, "Could not load texture replacement: %s - %s", info.file.c_str(), png.message); return; } if (!checkedAlpha) { // This will only check the hashed bits. CheckAlphaResult res = CheckAlphaRGBA8888Basic((u32 *)out, rowPitch / sizeof(u32), png.width, png.height); if (res == CHECKALPHA_ANY || level == 0) { alphaStatus_ = ReplacedTextureAlpha(res); } else if (res == CHECKALPHA_ZERO && alphaStatus_ == ReplacedTextureAlpha::FULL) { alphaStatus_ = ReplacedTextureAlpha(res); } } fclose(fp); png_image_free(&png); #endif }
void TextureReplacer::PopulateReplacement(ReplacedTexture *result, u64 cachekey, u32 hash, int w, int h) { int newW = w; int newH = h; LookupHashRange(cachekey >> 32, newW, newH); for (int i = 0; i < MAX_MIP_LEVELS; ++i) { const std::string hashfile = LookupHashFile(cachekey, hash, i); const std::string filename = basePath_ + hashfile; if (hashfile.empty() || !File::Exists(filename)) { // Out of valid mip levels. Bail out. break; } ReplacedTextureLevel level; level.fmt = ReplacedTextureFormat::F_8888; level.file = filename; #ifdef USING_QT_UI ERROR_LOG(G3D, "Replacement texture loading not implemented for Qt"); #else png_image png = {}; png.version = PNG_IMAGE_VERSION; FILE *fp = File::OpenCFile(filename, "rb"); if (png_image_begin_read_from_stdio(&png, fp)) { // We pad files that have been hashrange'd so they are the same texture size. level.w = (png.width * w) / newW; level.h = (png.height * h) / newH; result->levels_.push_back(level); } else { ERROR_LOG(G3D, "Could not load texture replacement info: %s - %s", filename.c_str(), png.message); } fclose(fp); png_image_free(&png); #endif } result->alphaStatus_ = ReplacedTextureAlpha::UNKNOWN; }
bool Texture::LoadFromCache() { if(m_Source.empty()) return false; __ResourceLocation& t_Location = __ResourceLocations[m_Source]; FILE* t_pFile; t_pFile = fopen(t_Location.File.c_str(), "rb"); if(!t_pFile) return false; fseek(t_pFile, t_Location.Position, SEEK_SET); png_image image; memset(&image, 0, (sizeof image)); image.version = PNG_IMAGE_VERSION; png_image_begin_read_from_stdio(&image, t_pFile); if(image.format == PNG_FORMAT_RGBA) m_Format = GL_RGBA; else m_Format = GL_RGB; m_pData = new unsigned char[PNG_IMAGE_SIZE(image)]; png_image_finish_read(&image, NULL, m_pData, 0, NULL); m_Width = image.width; m_Height = image.height; m_Type = GL_UNSIGNED_BYTE; fclose(t_pFile); return true; }