From 1ef1f9e3e74561879f6a35f788fb8ba0d75389e3 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 8 May 2015 17:34:51 -0300 Subject: [PATCH] Add option for color quantization in the ColorBar options popup --- data/gui.xml | 2 + src/app/CMakeLists.txt | 1 + src/app/commands/cmd_color_quantization.cpp | 111 ++++++++++++++++++++ src/app/commands/commands_list.h | 1 + src/render/quantization.cpp | 4 +- 5 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/app/commands/cmd_color_quantization.cpp diff --git a/data/gui.xml b/data/gui.xml index 3c2c4a17f..d958acb20 100644 --- a/data/gui.xml +++ b/data/gui.xml @@ -745,6 +745,8 @@ + + diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 82dcd47fe..c349c7d48 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -136,6 +136,7 @@ add_library(app-lib commands/cmd_clear.cpp commands/cmd_clear_cel.cpp commands/cmd_close_file.cpp + commands/cmd_color_quantization.cpp commands/cmd_configure_tools.cpp commands/cmd_copy.cpp commands/cmd_copy_cel.cpp diff --git a/src/app/commands/cmd_color_quantization.cpp b/src/app/commands/cmd_color_quantization.cpp new file mode 100644 index 000000000..9e6279957 --- /dev/null +++ b/src/app/commands/cmd_color_quantization.cpp @@ -0,0 +1,111 @@ +// Aseprite +// Copyright (C) 2001-2015 David Capello +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "app/cmd/set_palette.h" +#include "app/commands/command.h" +#include "app/console.h" +#include "app/context.h" +#include "app/context_access.h" +#include "app/modules/palettes.h" +#include "app/transaction.h" +#include "app/ui/color_bar.h" +#include "app/ui_context.h" +#include "base/unique_ptr.h" +#include "doc/palette.h" +#include "doc/sprite.h" +#include "render/quantization.h" +#include "ui/manager.h" + +namespace app { + +class ColorQuantizationCommand : public Command { +public: + ColorQuantizationCommand(); + Command* clone() const override { return new ColorQuantizationCommand(*this); } + +protected: + bool onEnabled(Context* context) override; + void onExecute(Context* context) override; +}; + +ColorQuantizationCommand::ColorQuantizationCommand() + : Command("ColorQuantization", + "Color Quantization", + CmdRecordableFlag) +{ +} + +bool ColorQuantizationCommand::onEnabled(Context* context) +{ + ContextWriter writer(context); + Sprite* sprite(writer.sprite()); + return (sprite && sprite->pixelFormat() == IMAGE_RGB); +} + +void ColorQuantizationCommand::onExecute(Context* context) +{ + try { + ContextWriter writer(UIContext::instance(), 500); + Sprite* sprite = writer.sprite(); + frame_t frame = writer.frame(); + if (sprite) { + PaletteView::SelectedEntries entries; + ColorBar::instance()->getPaletteView()->getSelectedEntries(entries); + + // TODO convert SelectedEntries into a class, and put the + // following lines in that class it is used several times (here + // and in ColorBar) + + int n = 0; + for (bool state : entries) { + if (state) + ++n; + } + + if (n < 2) { + n = 0; + for (auto& state : entries) { + state = true; + ++n; + } + } + + Palette palette(frame, n); + render::create_palette_from_rgb(sprite, frame, &palette); + + Palette newPalette(*get_current_palette()); + + int i = 0, j = 0; + for (bool state : entries) { + if (state) + newPalette.setEntry(i, palette.getEntry(j++)); + ++i; + } + + Transaction transaction(writer.context(), "Color Quantization", ModifyDocument); + transaction.execute(new cmd::SetPalette(sprite, frame, &newPalette)); + transaction.commit(); + + set_current_palette(&newPalette, false); + ui::Manager::getDefault()->invalidate(); + } + } + catch (base::Exception& e) { + Console::showException(e); + } +} + +Command* CommandFactory::createColorQuantizationCommand() +{ + return new ColorQuantizationCommand; +} + +} // namespace app diff --git a/src/app/commands/commands_list.h b/src/app/commands/commands_list.h index 80bb23b6e..6894a2066 100644 --- a/src/app/commands/commands_list.h +++ b/src/app/commands/commands_list.h @@ -20,6 +20,7 @@ FOR_EACH_COMMAND(ClearCel) FOR_EACH_COMMAND(CloseAllFiles) FOR_EACH_COMMAND(CloseFile) FOR_EACH_COMMAND(ColorCurve) +FOR_EACH_COMMAND(ColorQuantization) FOR_EACH_COMMAND(ConfigureTools) FOR_EACH_COMMAND(ConvolutionMatrix) FOR_EACH_COMMAND(Copy) diff --git a/src/render/quantization.cpp b/src/render/quantization.cpp index 5d803f3be..4f8511581 100644 --- a/src/render/quantization.cpp +++ b/src/render/quantization.cpp @@ -47,7 +47,7 @@ Palette* create_palette_from_rgb( Palette* palette) { if (!palette) - palette = new Palette(frame_t(0), 256); + palette = new Palette(frame_t(0), Palette::MaxColors); bool has_background_layer = (sprite->backgroundLayer() != NULL); @@ -481,7 +481,7 @@ void PaletteOptimizer::calculate(Palette* palette, bool has_background_layer) // Indexed). int first_usable_entry = (has_background_layer ? 0: 1); //int used_colors = - m_histogram.createOptimizedPalette(palette, first_usable_entry, 255); + m_histogram.createOptimizedPalette(palette, first_usable_entry, palette->size()-1); //palette->resize(first_usable_entry+used_colors); // TODO }