Add combobox to select RGB5A5/Octree algorithms in ColorQuantization dialog

This commit is contained in:
Gaspar Capello 2020-04-15 11:05:41 -03:00 committed by David Capello
parent 696bb9a537
commit 80cbb2caf5
4 changed files with 77 additions and 9 deletions

View File

@ -258,6 +258,7 @@
<option id="with_alpha" type="bool" default="true" /> <option id="with_alpha" type="bool" default="true" />
<option id="dithering_algorithm" type="std::string" /> <option id="dithering_algorithm" type="std::string" />
<option id="dithering_factor" type="int" default="100" /> <option id="dithering_factor" type="int" default="100" />
<option id="advanced" type="bool" default="false" />
</section> </section>
<section id="eyedropper" text="Editor"> <section id="eyedropper" text="Editor">
<option id="channel" type="EyedropperChannel" default="EyedropperChannel::COLOR_ALPHA" /> <option id="channel" type="EyedropperChannel" default="EyedropperChannel::COLOR_ALPHA" />

View File

@ -1258,10 +1258,11 @@ bg_color = Background Color:
[palette_from_sprite] [palette_from_sprite]
title = Palette from Sprite title = Palette from Sprite
new_palette = Create a new palette with a specific number of colors (or less): new_palette = Create new palette, color count limit:
replace_palette = Replace current palette replace_palette = Replace current palette
replace_range = Replace current range replace_range = Replace current range
alpha_channel = Create entries with alpha component alpha_channel = Create entries with alpha component
advanced_options = Advanced Options
[palette_popup] [palette_popup]
load = &Load load = &Load

View File

@ -1,5 +1,6 @@
<!-- Aseprite --> <!-- Aseprite -->
<!-- Copyright (C) 2015-2018 by David Capello --> <!-- Copyright (c) 2020 Igara Studio S.A. -->
<!-- Copyright (c) 2015-2018 David Capello -->
<gui> <gui>
<window id="palette_from_sprite" text="@.title"> <window id="palette_from_sprite" text="@.title">
<grid columns="2"> <grid columns="2">
@ -7,7 +8,15 @@
<expr expansive="true" id="ncolors" magnet="true" /> <expr expansive="true" id="ncolors" magnet="true" />
<radio id="current_palette" text="@.replace_palette" group="1" cell_hspan="2" /> <radio id="current_palette" text="@.replace_palette" group="1" cell_hspan="2" />
<radio id="current_range" text="@.replace_range" group="1" cell_hspan="2" /> <radio id="current_range" text="@.replace_range" group="1" cell_hspan="2" />
<separator horizontal="true" cell_hspan="2" />
<check id="alpha_channel" text="@.alpha_channel" cell_hspan="2" /> <check id="alpha_channel" text="@.alpha_channel" cell_hspan="2" />
<check id="advanced_check" text="@.advanced_options" cell_hspan="2" />
<hbox id="advanced" cell_hspan="2">
<label text="@rgbmap_algorithm_selector.label" />
<hbox id="rgbmap_algorithm_placeholder" />
</hbox>
<separator horizontal="true" cell_hspan="2" /> <separator horizontal="true" cell_hspan="2" />

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -20,10 +20,12 @@
#include "app/sprite_job.h" #include "app/sprite_job.h"
#include "app/transaction.h" #include "app/transaction.h"
#include "app/ui/color_bar.h" #include "app/ui/color_bar.h"
#include "app/ui/rgbmap_algorithm_selector.h"
#include "app/ui_context.h" #include "app/ui_context.h"
#include "doc/palette.h" #include "doc/palette.h"
#include "doc/sprite.h" #include "doc/sprite.h"
#include "render/quantization.h" #include "render/quantization.h"
#include "ui/manager.h"
#include "palette_from_sprite.xml.h" #include "palette_from_sprite.xml.h"
@ -36,8 +38,53 @@ struct ColorQuantizationParams : public NewParams {
Param<bool> withAlpha { this, true, "withAlpha" }; Param<bool> withAlpha { this, true, "withAlpha" };
Param<int> maxColors { this, 256, "maxColors" }; Param<int> maxColors { this, 256, "maxColors" };
Param<bool> useRange { this, false, "useRange" }; Param<bool> useRange { this, false, "useRange" };
Param<RgbMapAlgorithm> algorithm { this, RgbMapAlgorithm::DEFAULT, "algorithm" };
}; };
#if ENABLE_UI
class PaletteFromSpriteWindow : public app::gen::PaletteFromSprite {
public:
PaletteFromSpriteWindow() {
rgbmapAlgorithmPlaceholder()->addChild(&m_algoSelector);
advancedCheck()->Click.connect(
[this](ui::Event&){
advanced()->setVisible(advancedCheck()->isSelected());
const gfx::Rect origBounds = bounds();
setBounds(gfx::Rect(bounds().origin(), sizeHint()));
manager()->invalidateRect(origBounds);
});
m_algoSelector.Change.connect(
[this](){
switch (algorithm()) {
case RgbMapAlgorithm::RGB5A3:
alphaChannel()->setEnabled(true);
break;
case RgbMapAlgorithm::OCTREE:
alphaChannel()->setSelected(false);
alphaChannel()->setEnabled(false);
break;
}
});
}
doc::RgbMapAlgorithm algorithm() {
return m_algoSelector.algorithm();
}
void algorithm(const doc::RgbMapAlgorithm mapAlgo) {
m_algoSelector.algorithm(mapAlgo);
}
private:
RgbMapAlgorithmSelector m_algoSelector;
};
#endif
class ColorQuantizationCommand : public CommandWithNewParams<ColorQuantizationParams> { class ColorQuantizationCommand : public CommandWithNewParams<ColorQuantizationParams> {
public: public:
ColorQuantizationCommand(); ColorQuantizationCommand();
@ -65,8 +112,10 @@ void ColorQuantizationCommand::onExecute(Context* ctx)
const bool ui = (params().ui() && ctx->isUIAvailable()); const bool ui = (params().ui() && ctx->isUIAvailable());
#endif #endif
auto& pref = Preferences::instance();
bool withAlpha = params().withAlpha(); bool withAlpha = params().withAlpha();
int maxColors = params().maxColors(); int maxColors = params().maxColors();
RgbMapAlgorithm algorithm = params().algorithm();
bool createPal; bool createPal;
Site site = ctx->activeSite(); Site site = ctx->activeSite();
@ -74,16 +123,22 @@ void ColorQuantizationCommand::onExecute(Context* ctx)
#ifdef ENABLE_UI #ifdef ENABLE_UI
if (ui) { if (ui) {
app::gen::PaletteFromSprite window; PaletteFromSpriteWindow window;
{ {
ContextReader reader(ctx); ContextReader reader(ctx);
const Palette* curPalette = site.sprite()->palette(site.frame()); const Palette* curPalette = site.sprite()->palette(site.frame());
if (!params().algorithm.isSet())
algorithm = pref.experimental.rgbmapAlgorithm();
if (!params().withAlpha.isSet()) if (!params().withAlpha.isSet())
withAlpha = App::instance()->preferences().quantization.withAlpha(); withAlpha = pref.quantization.withAlpha();
const bool advanced = pref.quantization.advanced();
window.advancedCheck()->setSelected(advanced);
window.advanced()->setVisible(advanced);
window.algorithm(algorithm);
window.newPalette()->setSelected(true); window.newPalette()->setSelected(true);
window.alphaChannel()->setSelected(withAlpha);
window.ncolors()->setTextf("%d", maxColors); window.ncolors()->setTextf("%d", maxColors);
if (entries.picks() > 1) { if (entries.picks() > 1) {
@ -107,7 +162,10 @@ void ColorQuantizationCommand::onExecute(Context* ctx)
maxColors = window.ncolors()->textInt(); maxColors = window.ncolors()->textInt();
withAlpha = window.alphaChannel()->isSelected(); withAlpha = window.alphaChannel()->isSelected();
App::instance()->preferences().quantization.withAlpha(withAlpha); algorithm = window.algorithm();
pref.quantization.withAlpha(withAlpha);
pref.quantization.advanced(window.advancedCheck()->isSelected());
if (window.newPalette()->isSelected()) { if (window.newPalette()->isSelected()) {
createPal = true; createPal = true;
@ -139,8 +197,7 @@ void ColorQuantizationCommand::onExecute(Context* ctx)
Palette tmpPalette(frame, entries.picks()); Palette tmpPalette(frame, entries.picks());
SpriteJob job(reader, "Color Quantization"); SpriteJob job(reader, "Color Quantization");
const bool newBlend = Preferences::instance().experimental.newBlend(); const bool newBlend = pref.experimental.newBlend();
const RgbMapAlgorithm algorithm = Preferences::instance().experimental.rgbmapAlgorithm();
job.startJobWithCallback( job.startJobWithCallback(
[sprite, withAlpha, &tmpPalette, &job, newBlend, algorithm]{ [sprite, withAlpha, &tmpPalette, &job, newBlend, algorithm]{
render::create_palette_from_sprite( render::create_palette_from_sprite(