| 191 | //========================================================================== |
| 192 | |
| 193 | FBitmap FImageSource::GetCachedBitmap(const PalEntry *remap, int conversion, int *ptrans, int frame) |
| 194 | { |
| 195 | FBitmap ret; |
| 196 | |
| 197 | int trans = -1; |
| 198 | auto imageID = ImageID; |
| 199 | |
| 200 | if (NumOfFrames == 1 && frame == 1) |
| 201 | { |
| 202 | frame = 0; |
| 203 | } |
| 204 | |
| 205 | if (remap != nullptr) |
| 206 | { |
| 207 | // Remapped images are never run through the cache because they would complicate matters too much for very little gain. |
| 208 | // Translated images are normally sprites which normally just consist of a single image and use no composition. |
| 209 | // Additionally, since translation requires the base palette, the really time consuming stuff will never be subjected to it. |
| 210 | ret.Create(Width, Height); |
| 211 | trans = CopyTranslatedPixels(&ret, remap, frame); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | if (conversion == luminance) conversion = normal; // luminance has no meaning for true color. |
| 216 | // Do we have this image in the cache? |
| 217 | unsigned index = conversion != normal? UINT_MAX : precacheDataRgba.FindEx([=](PrecacheDataRgba &entry) { return entry.ImageID == imageID && entry.Frame == frame; }); |
| 218 | if (index < precacheDataRgba.Size()) |
| 219 | { |
| 220 | auto cache = &precacheDataRgba[index]; |
| 221 | |
| 222 | trans = cache->TransInfo; |
| 223 | if (cache->RefCount > 1) |
| 224 | { |
| 225 | //Printf("returning reference to %s, refcount = %d\n", name.GetChars(), cache->RefCount); |
| 226 | ret.Copy(cache->Pixels, false); |
| 227 | cache->RefCount--; |
| 228 | } |
| 229 | else if (cache->Pixels.GetPixels()) |
| 230 | { |
| 231 | //Printf("returning contents of %s, refcount = %d\n", name.GetChars(), cache->RefCount); |
| 232 | ret = std::move(cache->Pixels); |
| 233 | precacheDataRgba.Delete(index); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | // This should never happen if the function is implemented correctly |
| 238 | //Printf("something bad happened for %s, refcount = %d\n", name.GetChars(), cache->RefCount); |
| 239 | ret.Create(Width, Height); |
| 240 | trans = CopyPixels(&ret, normal, frame); |
| 241 | } |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | // The image wasn't cached. Now there's two possibilities: |
| 246 | auto info = precacheInfo.CheckKey(ImageID); |
| 247 | if (!info || info->first <= 1 || conversion != normal) |
| 248 | { |
| 249 | // This is either the only copy needed or some access outside the caching block. In these cases create a new one and directly return it. |
| 250 | //Printf("returning fresh copy of %s\n", name.GetChars()); |
no test coverage detected