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
11namespace Scintilla::Internal {
12
13/**
14 * Hold a pixmap in XPM format.
15 */
16class XPM {
17 int height=1;
18 int width=1;
19 int nColours=1;
20 std::vector<unsigned char> pixels;
21 ColourRGBA colourCodeTable[256];
22 char codeTransparent=' ';
23 ColourRGBA ColourFromCode(int ch) const noexcept;
24 void FillRun(Surface *surface, int code, int startX, int y, int x) const;
25public:
26 explicit XPM(const char *textForm);
27 explicit XPM(const char *const *linesForm);
28 void Init(const char *textForm);
29 void Init(const char *const *linesForm);
30 /// Decompose image into runs and use FillRectangle for each run
31 void Draw(Surface *surface, const PRectangle &rc);
32 int GetHeight() const noexcept { return height; }
33 int GetWidth() const noexcept { return width; }
34 ColourRGBA PixelAt(int x, int y) const noexcept;
35private:
36 static std::vector<const char *>LinesFormFromTextForm(const char *textForm);
37};
38
39/**
40 * A translucent image stored as a sequence of RGBA bytes.
41 */
42class RGBAImage {
43 int height;
44 int width;
45 float scale;
46 std::vector<unsigned char> pixelBytes;
47public:
48 static constexpr size_t bytesPerPixel = 4;
49 RGBAImage(int width_, int height_, float scale_, const unsigned char *pixels_);
50 explicit RGBAImage(const XPM &xpm);
51 int GetHeight() const noexcept { return height; }
52 int GetWidth() const noexcept { return width; }
53 float GetScale() const noexcept { return scale; }
54 float GetScaledHeight() const noexcept { return height / scale; }
55 float GetScaledWidth() const noexcept { return width / scale; }
56 int CountBytes() const noexcept;
57 const unsigned char *Pixels() const noexcept;
58 void SetPixel(int x, int y, ColourRGBA colour) noexcept;
59 static void BGRAFromRGBA(unsigned char *pixelsBGRA, const unsigned char *pixelsRGBA, size_t count) noexcept;
60};
61
62/**
63 * A collection of RGBAImage pixmaps indexed by integer id.
64 */
65class RGBAImageSet {
66 typedef std::map<int, std::unique_ptr<RGBAImage>> ImageMap;
67 ImageMap images;
68 mutable int height; ///< Memorize largest height of the set.
69 mutable int width; ///< Memorize largest width of the set.
70public:
71 RGBAImageSet();
72 /// Remove all images.
73 void Clear() noexcept;
74 /// Add an image.
75 void AddImage(int ident, std::unique_ptr<RGBAImage> image);
76 /// Get image by id.
77 RGBAImage *Get(int ident);
78 /// Give the largest height of the set.
79 int GetHeight() const noexcept;
80 /// Give the largest width of the set.
81 int GetWidth() const noexcept;
82};
83
84}
85
86#endif
87