| 1 | // Aseprite Render Library |
| 2 | // Copyright (c) 2019 Igara Studio S.A. |
| 3 | // Copyright (c) 2001-2017 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef RENDER_ORDERED_DITHER_H_INCLUDED |
| 9 | #define RENDER_ORDERED_DITHER_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "doc/color.h" |
| 13 | #include "doc/image_impl.h" |
| 14 | #include "doc/palette.h" |
| 15 | #include "doc/rgbmap.h" |
| 16 | #include "gfx/point.h" |
| 17 | #include "gfx/size.h" |
| 18 | #include "render/task_delegate.h" |
| 19 | |
| 20 | namespace render { |
| 21 | |
| 22 | class Dithering; |
| 23 | class DitheringMatrix; |
| 24 | |
| 25 | class DitheringAlgorithmBase { |
| 26 | public: |
| 27 | virtual ~DitheringAlgorithmBase() { } |
| 28 | |
| 29 | virtual int dimensions() const { return 1; } |
| 30 | virtual bool zigZag() const { return false; } |
| 31 | |
| 32 | virtual void start( |
| 33 | const doc::Image* srcImage, |
| 34 | doc::Image* dstImage, |
| 35 | const double factor) { } |
| 36 | |
| 37 | virtual void finish() { } |
| 38 | |
| 39 | virtual doc::color_t ditherRgbPixelToIndex( |
| 40 | const DitheringMatrix& matrix, |
| 41 | const doc::color_t color, |
| 42 | const int x, const int y, |
| 43 | const doc::RgbMap* rgbmap, |
| 44 | const doc::Palette* palette) { return 0; } |
| 45 | |
| 46 | virtual doc::color_t ditherRgbToIndex2D( |
| 47 | const int x, const int y, |
| 48 | const doc::RgbMap* rgbmap, |
| 49 | const doc::Palette* palette) { return 0; } |
| 50 | }; |
| 51 | |
| 52 | class OrderedDither : public DitheringAlgorithmBase { |
| 53 | public: |
| 54 | OrderedDither(int transparentIndex = -1); |
| 55 | doc::color_t ditherRgbPixelToIndex( |
| 56 | const DitheringMatrix& matrix, |
| 57 | const doc::color_t color, |
| 58 | const int x, |
| 59 | const int y, |
| 60 | const doc::RgbMap* rgbmap, |
| 61 | const doc::Palette* palette) override; |
| 62 | private: |
| 63 | int m_transparentIndex; |
| 64 | }; |
| 65 | |
| 66 | class OrderedDither2 : public DitheringAlgorithmBase { |
| 67 | public: |
| 68 | OrderedDither2(int transparentIndex = -1); |
| 69 | doc::color_t ditherRgbPixelToIndex( |
| 70 | const DitheringMatrix& matrix, |
| 71 | const doc::color_t color, |
| 72 | const int x, |
| 73 | const int y, |
| 74 | const doc::RgbMap* rgbmap, |
| 75 | const doc::Palette* palette) override; |
| 76 | private: |
| 77 | int m_transparentIndex; |
| 78 | }; |
| 79 | |
| 80 | void dither_rgb_image_to_indexed( |
| 81 | DitheringAlgorithmBase& algorithm, |
| 82 | const Dithering& dithering, |
| 83 | const doc::Image* srcImage, |
| 84 | doc::Image* dstImage, |
| 85 | const doc::RgbMap* rgbmap, |
| 86 | const doc::Palette* palette, |
| 87 | TaskDelegate* delegate = nullptr); |
| 88 | |
| 89 | } // namespace render |
| 90 | |
| 91 | #endif |
| 92 | |