1 // Scintilla source code edit control
2 /** @file XPM.h
3  ** Define a classes to hold image data in the X Pixmap (XPM) and RGBA formats.
4  **/
5 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7 
8 #ifndef XPM_H
9 #define XPM_H
10 
11 namespace Scintilla {
12 
13 /**
14  * Hold a pixmap in XPM format.
15  */
16 class XPM {
17 	int height=1;
18 	int width=1;
19 	int nColours=1;
20 	std::vector<unsigned char> pixels;
21 	ColourDesired colourCodeTable[256];
22 	char codeTransparent=' ';
23 	ColourDesired ColourFromCode(int ch) const noexcept;
24 	void FillRun(Surface *surface, int code, int startX, int y, int x) const;
25 public:
26 	explicit XPM(const char *textForm);
27 	explicit XPM(const char *const *linesForm);
28 	XPM(const XPM &) = default;
29 	XPM(XPM &&) noexcept = default;
30 	XPM &operator=(const XPM &) = default;
31 	XPM &operator=(XPM &&) noexcept = default;
32 	~XPM();
33 	void Init(const char *textForm);
34 	void Init(const char *const *linesForm);
35 	/// Decompose image into runs and use FillRectangle for each run
36 	void Draw(Surface *surface, const PRectangle &rc);
GetHeight()37 	int GetHeight() const noexcept { return height; }
GetWidth()38 	int GetWidth() const noexcept { return width; }
39 	void PixelAt(int x, int y, ColourDesired &colour, bool &transparent) const noexcept;
40 private:
41 	static std::vector<const char *>LinesFormFromTextForm(const char *textForm);
42 };
43 
44 /**
45  * A translucent image stored as a sequence of RGBA bytes.
46  */
47 class RGBAImage {
48 	int height;
49 	int width;
50 	float scale;
51 	std::vector<unsigned char> pixelBytes;
52 public:
53 	static constexpr size_t bytesPerPixel = 4;
54 	RGBAImage(int width_, int height_, float scale_, const unsigned char *pixels_);
55 	explicit RGBAImage(const XPM &xpm);
56 	RGBAImage(const RGBAImage &) = default;
57 	RGBAImage(RGBAImage &&) noexcept = default;
58 	RGBAImage &operator=(const RGBAImage &) = default;
59 	RGBAImage &operator=(RGBAImage &&) noexcept = default;
60 	virtual ~RGBAImage();
GetHeight()61 	int GetHeight() const noexcept { return height; }
GetWidth()62 	int GetWidth() const noexcept { return width; }
GetScale()63 	float GetScale() const noexcept { return scale; }
GetScaledHeight()64 	float GetScaledHeight() const noexcept { return height / scale; }
GetScaledWidth()65 	float GetScaledWidth() const noexcept { return width / scale; }
66 	int CountBytes() const noexcept;
67 	const unsigned char *Pixels() const noexcept;
68 	void SetPixel(int x, int y, ColourDesired colour, int alpha) noexcept;
69 	static void BGRAFromRGBA(unsigned char *pixelsBGRA, const unsigned char *pixelsRGBA, size_t count) noexcept;
70 };
71 
72 /**
73  * A collection of RGBAImage pixmaps indexed by integer id.
74  */
75 class RGBAImageSet {
76 	typedef std::map<int, std::unique_ptr<RGBAImage>> ImageMap;
77 	ImageMap images;
78 	mutable int height;	///< Memorize largest height of the set.
79 	mutable int width;	///< Memorize largest width of the set.
80 public:
81 	RGBAImageSet();
82 	// Deleted so RGBAImageSet objects can not be copied.
83 	RGBAImageSet(const RGBAImageSet &) = delete;
84 	RGBAImageSet(RGBAImageSet &&) = delete;
85 	RGBAImageSet &operator=(const RGBAImageSet &) = delete;
86 	RGBAImageSet &operator=(RGBAImageSet &&) = delete;
87 	~RGBAImageSet();
88 	/// Remove all images.
89 	void Clear() noexcept;
90 	/// Add an image.
91 	void Add(int ident, RGBAImage *image);
92 	/// Get image by id.
93 	RGBAImage *Get(int ident);
94 	/// Give the largest height of the set.
95 	int GetHeight() const;
96 	/// Give the largest width of the set.
97 	int GetWidth() const;
98 };
99 
100 }
101 
102 #endif
103