Add option for color quantization in the ColorBar options popup

This commit is contained in:
David Capello 2015-05-08 17:34:51 -03:00
parent 2995190d00
commit 1ef1f9e3e7
5 changed files with 117 additions and 2 deletions

View File

@ -745,6 +745,8 @@
<item command="SavePalette" text="Save as Default Palette">
<param name="preset" value="default" />
</item>
<separator />
<item command="ColorQuantization" text="Create Palette from RGB Image" />
</menu>
</menus>

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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
}