| 1 | // Aseprite | 
|---|---|
| 2 | // Copyright (C) 2019 Igara Studio S.A. | 
| 3 | // | 
| 4 | // This program is distributed under the terms of | 
| 5 | // the End-User License Agreement for Aseprite. | 
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H | 
| 8 | #include "config.h" | 
| 9 | #endif | 
| 10 | |
| 11 | #include "filters/filter.h" | 
| 12 | |
| 13 | #include "doc/palette.h" | 
| 14 | #include "doc/palette_picks.h" | 
| 15 | #include "filters/filter_indexed_data.h" | 
| 16 | #include "filters/filter_manager.h" | 
| 17 | |
| 18 | namespace filters { | 
| 19 | |
| 20 | FilterWithPalette::FilterWithPalette() | 
| 21 | : m_usePaletteOnRGB(false) | 
| 22 | { | 
| 23 | } | 
| 24 | |
| 25 | void FilterWithPalette::applyToPalette(FilterManager* filterMgr) | 
| 26 | { | 
| 27 | FilterIndexedData* fid = filterMgr->getIndexedData(); | 
| 28 | doc::PalettePicks picks = fid->getPalettePicks(); | 
| 29 | |
| 30 | switch (filterMgr->pixelFormat()) { | 
| 31 | |
| 32 | case doc::IMAGE_RGB: | 
| 33 | m_usePaletteOnRGB = (picks.picks() > 0); | 
| 34 | if (!m_usePaletteOnRGB) | 
| 35 | return; | 
| 36 | break; | 
| 37 | |
| 38 | case doc::IMAGE_INDEXED: | 
| 39 | // If there is a selection, we don't apply the filter to color | 
| 40 | // palette, instead we apply the filter to the pixels as an RGB | 
| 41 | // image (using closest colors from the palette) | 
| 42 | if (filterMgr->isMaskActive()) | 
| 43 | return; | 
| 44 | |
| 45 | // If there are no picks, we apply the filter to the whole palette. | 
| 46 | if (picks.picks() == 0) { | 
| 47 | picks.resize(fid->getPalette()->size()); | 
| 48 | picks.all(); | 
| 49 | } | 
| 50 | break; | 
| 51 | |
| 52 | default: | 
| 53 | // We cannot change the palette of a grayscale image | 
| 54 | return; | 
| 55 | } | 
| 56 | |
| 57 | onApplyToPalette(filterMgr, picks); | 
| 58 | } | 
| 59 | |
| 60 | } // namespace filters | 
| 61 | 
