Skip to content

Commit

Permalink
Now DitheringSelector is used to select just a DitheringMatrix for gr…
Browse files Browse the repository at this point in the history
…adients
  • Loading branch information
dacap committed Jun 9, 2017
1 parent 317b493 commit 2a15c58
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/app/commands/cmd_change_pixel_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class ColorModeWindow : public app::gen::ColorMode {
if (from != IMAGE_INDEXED) {
colorMode()->addChild(new ConversionItem(IMAGE_INDEXED));

m_ditheringSelector = new DitheringSelector;
m_ditheringSelector = new DitheringSelector(DitheringSelector::SelectBoth);
m_ditheringSelector->setExpansive(true);
m_ditheringSelector->Change.connect(
base::Bind<void>(&ColorModeWindow::onDithering, this));
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/context_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ ContextBar::ContextBar()
addChild(m_tolerance = new ToleranceField());
addChild(m_contiguous = new ContiguousField());
addChild(m_paintBucketSettings = new PaintBucketSettingsField());
addChild(m_ditheringSelector = new DitheringSelector());
addChild(m_ditheringSelector = new DitheringSelector(DitheringSelector::SelectMatrix));
m_ditheringSelector->setUseCustomWidget(false); // Disable custom widget because the context bar is too small

addChild(m_inkType = new InkTypeField(this));
Expand Down
88 changes: 61 additions & 27 deletions src/app/ui/dithering_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "doc/image.h"
#include "doc/image_ref.h"
#include "doc/primitives.h"
#include "render/gradient.h"
#include "render/quantization.h"
#include "she/surface.h"
#include "she/system.h"
Expand All @@ -33,10 +34,12 @@ namespace {

class DitherItem : public ListItem {
public:

DitherItem(render::DitheringAlgorithm algo,
const render::DitheringMatrix& matrix,
const std::string& text)
: ListItem(text)
, m_matrixOnly(false)
, m_algo(algo)
, m_matrix(matrix)
, m_preview(nullptr)
Expand All @@ -45,6 +48,18 @@ class DitherItem : public ListItem {
{
}

DitherItem(const render::DitheringMatrix& matrix,
const std::string& text)
: ListItem(text)
, m_matrixOnly(true)
, m_algo(render::DitheringAlgorithm::None)
, m_matrix(matrix)
, m_preview(nullptr)
, m_palId(0)
, m_palMods(0)
{
}

render::DitheringAlgorithm algo() const { return m_algo; }
render::DitheringMatrix matrix() const { return m_matrix; }

Expand All @@ -66,18 +81,25 @@ class DitherItem : public ListItem {

const int w = 128, h = 16;
doc::ImageRef image1(doc::Image::create(doc::IMAGE_RGB, w, h));
doc::clear_image(image1.get(), 0);
for (int y=0; y<h; ++y)
for (int x=0; x<w; ++x) {
int v = 255 * x / (w-1);
image1->putPixel(x, y, doc::rgba(v, v, v, 255));
}

doc::ImageRef image2(doc::Image::create(doc::IMAGE_INDEXED, w, h));
doc::clear_image(image2.get(), 0);
render::convert_pixel_format(
image1.get(), image2.get(), IMAGE_INDEXED,
m_algo, m_matrix, nullptr, palette, true, -1, nullptr);
render_rgba_linear_gradient(
image1.get(),
gfx::Point(0, 0),
gfx::Point(w-1, 0),
doc::rgba(0, 0, 0, 255),
doc::rgba(255, 255, 255, 255),
m_matrixOnly ? m_matrix: render::DitheringMatrix());

doc::ImageRef image2;
if (m_matrixOnly) {
image2 = image1;
}
else {
image2.reset(doc::Image::create(doc::IMAGE_INDEXED, w, h));
doc::clear_image(image2.get(), 0);
render::convert_pixel_format(
image1.get(), image2.get(), IMAGE_INDEXED,
m_algo, m_matrix, nullptr, palette, true, -1, nullptr);
}

m_preview = she::instance()->createRgbaSurface(w, h);
doc::convert_image_to_surface(image2.get(), palette, m_preview,
Expand Down Expand Up @@ -124,6 +146,7 @@ class DitherItem : public ListItem {
rc.y+4*guiscale()+textsz.h);
}

bool m_matrixOnly;
render::DitheringAlgorithm m_algo;
render::DitheringMatrix m_matrix;
she::Surface* m_preview;
Expand All @@ -133,24 +156,35 @@ class DitherItem : public ListItem {

} // anonymous namespace

DitheringSelector::DitheringSelector()
DitheringSelector::DitheringSelector(Type type)
{
setUseCustomWidget(true);

addItem(new DitherItem(render::DitheringAlgorithm::None,
render::DitheringMatrix(), "No Dithering"));
addItem(new DitherItem(render::DitheringAlgorithm::Ordered,
render::BayerMatrix(8), "Ordered Dithering - Bayer Matrix 8x8"));
addItem(new DitherItem(render::DitheringAlgorithm::Ordered,
render::BayerMatrix(4), "Ordered Dithering - Bayer Matrix 4x4"));
addItem(new DitherItem(render::DitheringAlgorithm::Ordered,
render::BayerMatrix(2), "Ordered Dithering - Bayer Matrix 2x2"));
addItem(new DitherItem(render::DitheringAlgorithm::Old,
render::BayerMatrix(8), "Old Dithering - Bayer Matrix 8x8"));
addItem(new DitherItem(render::DitheringAlgorithm::Old,
render::BayerMatrix(4), "Old Dithering - Bayer Matrix 4x4"));
addItem(new DitherItem(render::DitheringAlgorithm::Old,
render::BayerMatrix(2), "Old Dithering - Bayer Matrix 2x2"));
switch (type) {
case SelectBoth:
addItem(new DitherItem(render::DitheringAlgorithm::None,
render::DitheringMatrix(), "No Dithering"));
addItem(new DitherItem(render::DitheringAlgorithm::Ordered,
render::BayerMatrix(8), "Ordered Dithering - Bayer Matrix 8x8"));
addItem(new DitherItem(render::DitheringAlgorithm::Ordered,
render::BayerMatrix(4), "Ordered Dithering - Bayer Matrix 4x4"));
addItem(new DitherItem(render::DitheringAlgorithm::Ordered,
render::BayerMatrix(2), "Ordered Dithering - Bayer Matrix 2x2"));
addItem(new DitherItem(render::DitheringAlgorithm::Old,
render::BayerMatrix(8), "Old Dithering - Bayer Matrix 8x8"));
addItem(new DitherItem(render::DitheringAlgorithm::Old,
render::BayerMatrix(4), "Old Dithering - Bayer Matrix 4x4"));
addItem(new DitherItem(render::DitheringAlgorithm::Old,
render::BayerMatrix(2), "Old Dithering - Bayer Matrix 2x2"));
break;
case SelectMatrix:
addItem(new DitherItem(render::DitheringMatrix(), "No Dithering"));
addItem(new DitherItem(render::BayerMatrix(8), "Bayer Matrix 8x8"));
addItem(new DitherItem(render::BayerMatrix(4), "Bayer Matrix 4x4"));
addItem(new DitherItem(render::BayerMatrix(2), "Bayer Matrix 2x2"));
break;
}


setSelectedItemIndex(0);
setSizeHint(getItem(0)->sizeHint());
Expand Down
7 changes: 6 additions & 1 deletion src/app/ui/dithering_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ namespace app {

class DitheringSelector : public ui::ComboBox {
public:
DitheringSelector();
enum Type {
SelectBoth,
SelectMatrix,
};

DitheringSelector(Type type);

render::DitheringAlgorithm ditheringAlgorithm();
render::DitheringMatrix ditheringMatrix();
Expand Down

0 comments on commit 2a15c58

Please sign in to comment.