1 // Scintilla source code edit control
3 ** Define a class that holds data in the X Pixmap (XPM) format.
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.
19 using namespace Scintilla
;
22 static const char *NextField(const char *s
) {
23 // In case there are leading spaces in the string
24 while (*s
&& *s
== ' ') {
27 while (*s
&& *s
!= ' ') {
30 while (*s
&& *s
== ' ') {
36 // Data lines in XPM can be terminated either with NUL or "
37 static size_t MeasureLength(const char *s
) {
39 while (s
[i
] && (s
[i
] != '\"'))
44 ColourDesired
XPM::ColourFromCode(int ch
) const {
45 return colourCodeTable
[ch
];
48 void XPM::FillRun(Surface
*surface
, int code
, int startX
, int y
, int x
) {
49 if ((code
!= codeTransparent
) && (startX
!= x
)) {
50 PRectangle rc
= PRectangle::FromInts(startX
, y
, x
, y
+ 1);
51 surface
->FillRectangle(rc
, ColourFromCode(code
));
55 XPM::XPM(const char *textForm
) {
59 XPM::XPM(const char *const *linesForm
) {
66 void XPM::Init(const char *textForm
) {
67 // Test done is two parts to avoid possibility of overstepping the memory
68 // if memcmp implemented strangely. Must be 4 bytes at least at destination.
69 if ((0 == memcmp(textForm
, "/* X", 4)) && (0 == memcmp(textForm
, "/* XPM */", 9))) {
70 // Build the lines form out of the text form
71 std::vector
<const char *> linesForm
= LinesFormFromTextForm(textForm
);
72 if (!linesForm
.empty()) {
76 // It is really in line form
77 Init(reinterpret_cast<const char * const *>(textForm
));
81 void XPM::Init(const char *const *linesForm
) {
86 codeTransparent
= ' ';
90 std::fill(colourCodeTable
, colourCodeTable
+256, 0);
91 const char *line0
= linesForm
[0];
93 line0
= NextField(line0
);
95 pixels
.resize(width
*height
);
96 line0
= NextField(line0
);
97 nColours
= atoi(line0
);
98 line0
= NextField(line0
);
99 if (atoi(line0
) != 1) {
100 // Only one char per pixel is supported
104 for (int c
=0; c
<nColours
; c
++) {
105 const char *colourDef
= linesForm
[c
+1];
106 int code
= static_cast<unsigned char>(colourDef
[0]);
108 ColourDesired
colour(0xff, 0xff, 0xff);
109 if (*colourDef
== '#') {
110 colour
.Set(colourDef
);
112 codeTransparent
= static_cast<char>(code
);
114 colourCodeTable
[code
] = colour
;
117 for (int y
=0; y
<height
; y
++) {
118 const char *lform
= linesForm
[y
+nColours
+1];
119 size_t len
= MeasureLength(lform
);
120 for (size_t x
= 0; x
<len
; x
++)
121 pixels
[y
* width
+ x
] = static_cast<unsigned char>(lform
[x
]);
125 void XPM::Draw(Surface
*surface
, PRectangle
&rc
) {
126 if (pixels
.empty()) {
130 int startY
= static_cast<int>(rc
.top
+ (rc
.Height() - height
) / 2);
131 int startX
= static_cast<int>(rc
.left
+ (rc
.Width() - width
) / 2);
132 for (int y
=0; y
<height
; y
++) {
135 for (int x
=0; x
<width
; x
++) {
136 int code
= pixels
[y
* width
+ x
];
137 if (code
!= prevCode
) {
138 FillRun(surface
, prevCode
, startX
+ xStartRun
, startY
+ y
, startX
+ x
);
143 FillRun(surface
, prevCode
, startX
+ xStartRun
, startY
+ y
, startX
+ width
);
147 void XPM::PixelAt(int x
, int y
, ColourDesired
&colour
, bool &transparent
) const {
148 if (pixels
.empty() || (x
<0) || (x
>= width
) || (y
<0) || (y
>= height
)) {
153 int code
= pixels
[y
* width
+ x
];
154 transparent
= code
== codeTransparent
;
158 colour
= ColourFromCode(code
).AsLong();
162 std::vector
<const char *> XPM::LinesFormFromTextForm(const char *textForm
) {
163 // Build the lines form out of the text form
164 std::vector
<const char *> linesForm
;
168 for (; countQuotes
< (2*strings
) && textForm
[j
] != '\0'; j
++) {
169 if (textForm
[j
] == '\"') {
170 if (countQuotes
== 0) {
171 // First field: width, height, number of colors, chars per pixel
172 const char *line0
= textForm
+ j
+ 1;
174 line0
= NextField(line0
);
175 // Add 1 line for each pixel of height
176 strings
+= atoi(line0
);
177 line0
= NextField(line0
);
178 // Add 1 line for each colour
179 strings
+= atoi(line0
);
181 if (countQuotes
/ 2 >= strings
) {
182 break; // Bad height or number of colors!
184 if ((countQuotes
& 1) == 0) {
185 linesForm
.push_back(textForm
+ j
+ 1);
190 if (textForm
[j
] == '\0' || countQuotes
/ 2 > strings
) {
191 // Malformed XPM! Height + number of colors too high or too low
197 RGBAImage::RGBAImage(int width_
, int height_
, float scale_
, const unsigned char *pixels_
) :
198 height(height_
), width(width_
), scale(scale_
) {
200 pixelBytes
.assign(pixels_
, pixels_
+ CountBytes());
202 pixelBytes
.resize(CountBytes());
206 RGBAImage::RGBAImage(const XPM
&xpm
) {
207 height
= xpm
.GetHeight();
208 width
= xpm
.GetWidth();
210 pixelBytes
.resize(CountBytes());
211 for (int y
=0; y
<height
; y
++) {
212 for (int x
=0; x
<width
; x
++) {
213 ColourDesired colour
;
214 bool transparent
= false;
215 xpm
.PixelAt(x
, y
, colour
, transparent
);
216 SetPixel(x
, y
, colour
, transparent
? 0 : 255);
221 RGBAImage::~RGBAImage() {
224 int RGBAImage::CountBytes() const {
225 return width
* height
* 4;
228 const unsigned char *RGBAImage::Pixels() const {
229 return &pixelBytes
[0];
232 void RGBAImage::SetPixel(int x
, int y
, ColourDesired colour
, int alpha
) {
233 unsigned char *pixel
= &pixelBytes
[0] + (y
*width
+x
) * 4;
235 pixel
[0] = static_cast<unsigned char>(colour
.GetRed());
236 pixel
[1] = static_cast<unsigned char>(colour
.GetGreen());
237 pixel
[2] = static_cast<unsigned char>(colour
.GetBlue());
238 pixel
[3] = static_cast<unsigned char>(alpha
);
241 RGBAImageSet::RGBAImageSet() : height(-1), width(-1) {
244 RGBAImageSet::~RGBAImageSet() {
248 /// Remove all images.
249 void RGBAImageSet::Clear() {
250 for (ImageMap::iterator it
=images
.begin(); it
!= images
.end(); ++it
) {
260 void RGBAImageSet::Add(int ident
, RGBAImage
*image
) {
261 ImageMap::iterator it
=images
.find(ident
);
262 if (it
== images
.end()) {
263 images
[ident
] = image
;
273 RGBAImage
*RGBAImageSet::Get(int ident
) {
274 ImageMap::iterator it
= images
.find(ident
);
275 if (it
!= images
.end()) {
281 /// Give the largest height of the set.
282 int RGBAImageSet::GetHeight() const {
284 for (ImageMap::const_iterator it
=images
.begin(); it
!= images
.end(); ++it
) {
285 if (height
< it
->second
->GetHeight()) {
286 height
= it
->second
->GetHeight();
290 return (height
> 0) ? height
: 0;
293 /// Give the largest width of the set.
294 int RGBAImageSet::GetWidth() const {
296 for (ImageMap::const_iterator it
=images
.begin(); it
!= images
.end(); ++it
) {
297 if (width
< it
->second
->GetWidth()) {
298 width
= it
->second
->GetWidth();
302 return (width
> 0) ? width
: 0;