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