| 659 | } |
| 660 | |
| 661 | sp<Palette> DataImpl::loadPalette(const UString &path) |
| 662 | { |
| 663 | std::lock_guard<std::recursive_mutex> l(this->paletteCacheLock); |
| 664 | if (path == "") |
| 665 | { |
| 666 | LogWarning("Invalid palette path"); |
| 667 | return nullptr; |
| 668 | } |
| 669 | |
| 670 | auto alias = this->paletteAliases.find(path); |
| 671 | if (alias != this->paletteAliases.end()) |
| 672 | { |
| 673 | LogInfo("Using alias \"%s\" for \"%s\"", path, alias->second); |
| 674 | return this->loadPalette(alias->second); |
| 675 | } |
| 676 | |
| 677 | // Use an uppercase version of the path for the cache key |
| 678 | UString cacheKey = to_upper(path); |
| 679 | |
| 680 | auto pal = this->paletteCache[cacheKey].lock(); |
| 681 | if (pal) |
| 682 | { |
| 683 | return pal; |
| 684 | } |
| 685 | |
| 686 | pal = loadPCXPalette(*this, path); |
| 687 | if (pal) |
| 688 | { |
| 689 | LogInfo("Read \"%s\" as PCX palette", path); |
| 690 | this->paletteCache[cacheKey] = pal; |
| 691 | this->pinnedPalettes.push(pal); |
| 692 | this->pinnedPalettes.pop(); |
| 693 | return pal; |
| 694 | } |
| 695 | pal = loadPNGPalette(*this, path); |
| 696 | if (pal) |
| 697 | { |
| 698 | LogInfo("Read \"%s\" as PNG palette", path); |
| 699 | this->paletteCache[cacheKey] = pal; |
| 700 | this->pinnedPalettes.push(pal); |
| 701 | this->pinnedPalettes.pop(); |
| 702 | return pal; |
| 703 | } |
| 704 | |
| 705 | sp<RGBImage> img = std::dynamic_pointer_cast<RGBImage>(this->loadImage(path)); |
| 706 | if (img) |
| 707 | { |
| 708 | unsigned int idx = 0; |
| 709 | auto p = mksp<Palette>(img->size.x * img->size.y); |
| 710 | RGBImageLock src{img, ImageLockUse::Read}; |
| 711 | for (unsigned int y = 0; y < img->size.y; y++) |
| 712 | { |
| 713 | for (unsigned int x = 0; x < img->size.x; x++) |
| 714 | { |
| 715 | Colour c = src.get(Vec2<int>{x, y}); |
| 716 | p->setColour(idx, c); |
| 717 | idx++; |
| 718 | } |
no test coverage detected