From 2a15c58b5b78f0695a9651e599bbf8393809bf28 Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 9 Jun 2017 17:31:41 -0300 Subject: [PATCH] Now DitheringSelector is used to select just a DitheringMatrix for gradients --- src/app/commands/cmd_change_pixel_format.cpp | 2 +- src/app/ui/context_bar.cpp | 2 +- src/app/ui/dithering_selector.cpp | 86 ++++++++++++++------ src/app/ui/dithering_selector.h | 7 +- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/src/app/commands/cmd_change_pixel_format.cpp b/src/app/commands/cmd_change_pixel_format.cpp index b10cd6fe7..4de0a6325 100644 --- a/src/app/commands/cmd_change_pixel_format.cpp +++ b/src/app/commands/cmd_change_pixel_format.cpp @@ -183,7 +183,7 @@ public: 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(&ColorModeWindow::onDithering, this)); diff --git a/src/app/ui/context_bar.cpp b/src/app/ui/context_bar.cpp index c589cad4f..c764cc54c 100644 --- a/src/app/ui/context_bar.cpp +++ b/src/app/ui/context_bar.cpp @@ -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)); diff --git a/src/app/ui/dithering_selector.cpp b/src/app/ui/dithering_selector.cpp index d2bac6f28..0fa0c04ea 100644 --- a/src/app/ui/dithering_selector.cpp +++ b/src/app/ui/dithering_selector.cpp @@ -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" @@ -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) @@ -45,6 +48,18 @@ public: { } + 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; } @@ -66,18 +81,25 @@ private: 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; yputPixel(x, y, doc::rgba(v, v, v, 255)); - } + 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(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); + 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, @@ -124,6 +146,7 @@ private: rc.y+4*guiscale()+textsz.h); } + bool m_matrixOnly; render::DitheringAlgorithm m_algo; render::DitheringMatrix m_matrix; she::Surface* m_preview; @@ -133,24 +156,35 @@ private: } // 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()); diff --git a/src/app/ui/dithering_selector.h b/src/app/ui/dithering_selector.h index 34e57d67c..2d7a01d01 100644 --- a/src/app/ui/dithering_selector.h +++ b/src/app/ui/dithering_selector.h @@ -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();