| 88 | } |
| 89 | |
| 90 | int ImageSampler::Load(const std::string& path, uint32_t load_flags) { |
| 91 | std::string load_path; |
| 92 | ModID cache_modsource; |
| 93 | bool loaded_cache = false; |
| 94 | |
| 95 | if (CacheFile::CheckForCache(path_, suffix, &load_path, &cache_modsource, &checksum_)) { |
| 96 | if (ReadCacheFile(load_path, checksum_)) { |
| 97 | loaded_cache = true; |
| 98 | LOGI << "Loaded cache file " << load_path << std::endl; |
| 99 | modsource_ = cache_modsource; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (loaded_cache == false) { |
| 104 | char abs_path[kPathSize]; |
| 105 | ModID modsource; |
| 106 | if (FindImagePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kWriteDir | kModWriteDirs, true, NULL, false, false, &modsource) == -1) { |
| 107 | return kLoadErrorMissingFile; |
| 108 | } |
| 109 | LOGI << "Loading " << abs_path << std::endl; |
| 110 | /* |
| 111 | if(FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kWriteDir) == -1){ |
| 112 | FatalError("Error", "Could not find image sampler file \"%s\"", path_.c_str()); |
| 113 | } |
| 114 | */ |
| 115 | |
| 116 | int n = 0; |
| 117 | // Tell stb_image that we want 4 components (RGBA) |
| 118 | stbi_uc* data = stbi_load(abs_path, &width_, &height_, &n, 4); |
| 119 | |
| 120 | // stbi will always ensure that the total output channels will match the specified (4); 'n' will contain the source images' channels |
| 121 | // So we only need to throw an error if the data doesn't exist, it's format is guaranteed |
| 122 | if (data == nullptr) { // || n != 4 |
| 123 | return kLoadErrorGeneralFileError; |
| 124 | } |
| 125 | |
| 126 | pixels_.resize(width_ * height_); |
| 127 | std::memcpy(pixels_.data(), data, width_ * height_ * 4); |
| 128 | |
| 129 | stbi_image_free(data); |
| 130 | |
| 131 | char write_path[kPathSize]; |
| 132 | FormatString(write_path, kPathSize, "%s%s%s", GetWritePath(modsource).c_str(), path.c_str(), suffix); |
| 133 | modsource_ = modsource; |
| 134 | WriteCacheFile(write_path); |
| 135 | } |
| 136 | return kLoadOk; |
| 137 | } |
| 138 | |
| 139 | const char* ImageSampler::GetLoadErrorString() { |
| 140 | switch (sub_error) { |
nothing calls this directly
no test coverage detected