-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.h
96 lines (70 loc) · 2.7 KB
/
strategy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <memory>
#include "misc/structs.h"
#include "image.h"
namespace png_decoder::scanline_reader {
class PixelStrategy {
public:
PixelStrategy(uint8_t bitDepth, PLTE plte);
virtual ~PixelStrategy() = default;
uint32_t bpp() const;
uint32_t sampleSizeBits() const noexcept;
virtual RGB pixelAt(const Scanline& scanline, size_t index) const = 0;
virtual uint32_t samplesCount() const noexcept = 0;
public:
static std::unique_ptr<PixelStrategy> create(uint8_t colorType, uint8_t bitDepth, PLTE plte);
private:
static constexpr uint8_t PIXEL_GRAYSCALE_COLOR_TYPE = 0;
static constexpr uint8_t PIXEL_RGB_COLOR_TYPE = 2;
static constexpr uint8_t PIXEL_PALETTE_INDEX_COLOR_TYPE = 3;
static constexpr uint8_t PIXEL_GRAYSCALE_ALPHA_COLOR_TYPE = 4;
static constexpr uint8_t PIXEL_RGB_ALPHA_COLOR_TYPE = 6;
protected:
uint8_t m_bitDepth;
PLTE m_plte;
};
class PixelBitsMixin {
protected:
/*
* Extract bits of pixel with bitDepth <= 8 from scanline data.
* This is valid because grayscale color type has exactly one sample
* which leads to the fact of pixel being equivalent to the sample.
*/
uint8_t getPixelBits(const Scanline& scanline, size_t index, uint32_t sampleSizeBits) const;
};
// PixelGrayscaleStrategy
class PixelGrayscaleStrategy : public PixelStrategy, private PixelBitsMixin {
public:
PixelGrayscaleStrategy(uint8_t bitDepth, PLTE plte);
RGB pixelAt(const Scanline& scanline, size_t index) const override;
uint32_t samplesCount() const noexcept override;
};
// PixelRGBStrategy
class PixelRGBStrategy : public PixelStrategy {
public:
PixelRGBStrategy(uint8_t bitDepth, PLTE plte);
RGB pixelAt(const Scanline& scanline, size_t index) const override;
uint32_t samplesCount() const noexcept override;
};
// PixelPaletteIndexStrategy
class PixelPaletteIndexStrategy : public PixelStrategy, private PixelBitsMixin {
public:
PixelPaletteIndexStrategy(uint8_t bitDepth, PLTE plte);
RGB pixelAt(const Scanline& scanline, size_t index) const override;
uint32_t samplesCount() const noexcept override;
};
// PixelGrayscaleAlphaStrategy
class PixelGrayscaleAlphaStrategy : public PixelStrategy {
public:
PixelGrayscaleAlphaStrategy(uint8_t bitDepth, PLTE plte);
RGB pixelAt(const Scanline& scanline, size_t index) const override;
uint32_t samplesCount() const noexcept override;
};
// PixelRGBAlphaStrategy
class PixelRGBAlphaStrategy : public PixelStrategy {
public:
PixelRGBAlphaStrategy(uint8_t bitDepth, PLTE plte);
RGB pixelAt(const Scanline& scanline, size_t index) const override;
uint32_t samplesCount() const noexcept override;
};
} // namespace png_decoder::scanline_reader