qxbmhandler.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/image/qxbmhandler.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7-
8-
9-
10-
11-
12-
13-
14static inline int hex2byte(char *p)-
15{-
16 return
never executed: return ((isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4) | (isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10);
((isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4) |
never executed: return ((isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4) | (isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10);
0
17 (isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10);
never executed: return ((isdigit((uchar) *p) ? *p - '0' : toupper((uchar) *p) - 'A' + 10) << 4) | (isdigit((uchar) *(p+1)) ? *(p+1) - '0' : toupper((uchar) *(p+1)) - 'A' + 10);
0
18}-
19-
20static bool read_xbm_header(QIODevice *device, int& w, int& h)-
21{-
22 const int buflen = 300;-
23 const int maxlen = 4096;-
24 char buf[buflen + 1];-
25 QRegExp r1(QLatin1String("^#define[ \t]+[a-zA-Z0-9._]+[ \t]+"));-
26 QRegExp r2(QLatin1String("[0-9]+"));-
27-
28 qint64 readBytes = 0;-
29 qint64 totalReadBytes = 0;-
30-
31 buf[0] = '\0';-
32-
33-
34 while (buf[0] != '#'
buf[0] != '#'Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
35 readBytes = device->readLine(buf, buflen);-
36-
37-
38 if (readBytes <= 0
readBytes <= 0Description
TRUEnever evaluated
FALSEnever evaluated
|| readBytes >= buflen -1
readBytes >= buflen -1Description
TRUEnever evaluated
FALSEnever evaluated
)
0
39 return
never executed: return false;
false;
never executed: return false;
0
40-
41-
42-
43 totalReadBytes += readBytes;-
44 if (totalReadBytes >= maxlen
totalReadBytes >= maxlenDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
45 return
never executed: return false;
false;
never executed: return false;
0
46 }
never executed: end of block
0
47-
48 buf[readBytes - 1] = '\0';-
49 QString sbuf;-
50 sbuf = QString::fromLatin1(buf);-
51-
52-
53 if (r1.indexIn(sbuf) == 0
r1.indexIn(sbuf) == 0Description
TRUEnever evaluated
FALSEnever evaluated
&&
0
54 r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength()
r2.indexIn(sbu...atchedLength()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
55 w = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt();
never executed: w = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt();
0
56-
57-
58 readBytes = device->readLine(buf, buflen);-
59 if (readBytes <= 0
readBytes <= 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
60 return
never executed: return false;
false;
never executed: return false;
0
61 buf[readBytes - 1] = '\0';-
62-
63 sbuf = QString::fromLatin1(buf);-
64-
65 if (r1.indexIn(sbuf) == 0
r1.indexIn(sbuf) == 0Description
TRUEnever evaluated
FALSEnever evaluated
&&
0
66 r2.indexIn(sbuf, r1.matchedLength()) == r1.matchedLength()
r2.indexIn(sbu...atchedLength()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
67 h = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt();
never executed: h = QByteArray(&buf[r1.matchedLength()]).trimmed().toInt();
0
68-
69-
70 if (w <= 0
w <= 0Description
TRUEnever evaluated
FALSEnever evaluated
|| w > 32767
w > 32767Description
TRUEnever evaluated
FALSEnever evaluated
|| h <= 0
h <= 0Description
TRUEnever evaluated
FALSEnever evaluated
|| h > 32767
h > 32767Description
TRUEnever evaluated
FALSEnever evaluated
)
0
71 return
never executed: return false;
false;
never executed: return false;
0
72-
73 return
never executed: return true;
true;
never executed: return true;
0
74}-
75-
76static bool read_xbm_body(QIODevice *device, int w, int h, QImage *outImage)-
77{-
78 const int buflen = 300;-
79 char buf[buflen + 1];-
80-
81 qint64 readBytes = 0;-
82-
83 char *p;-
84-
85-
86 do {-
87 if ((
(readBytes = d... buflen)) <= 0Description
TRUEnever evaluated
FALSEnever evaluated
readBytes = device->readLine(buf, buflen)) <= 0
(readBytes = d... buflen)) <= 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
88-
89 return
never executed: return false;
false;
never executed: return false;
0
90 }-
91-
92 buf[readBytes] = '\0';-
93 p = strstr(buf, "0x");-
94 }
never executed: end of block
while (!p
!pDescription
TRUEnever evaluated
FALSEnever evaluated
);
0
95-
96 if (outImage->size() != QSize(w, h)
outImage->size...!= QSize(w, h)Description
TRUEnever evaluated
FALSEnever evaluated
|| outImage->format() != QImage::Format_MonoLSB
outImage->form...Format_MonoLSBDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
97 *outImage = QImage(w, h, QImage::Format_MonoLSB);-
98 if (outImage->isNull()
outImage->isNull()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
99 return
never executed: return false;
false;
never executed: return false;
0
100 }
never executed: end of block
0
101-
102 outImage->setColorCount(2);-
103 outImage->setColor(0, qRgb(255,255,255));-
104 outImage->setColor(1, qRgb(0,0,0));-
105-
106 int x = 0, y = 0;-
107 uchar *b = outImage->scanLine(0);-
108 w = (w+7)/8;-
109-
110 while (y < h
y < hDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
111 if (p
pDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
112 *b++ = hex2byte(p+2);-
113 p += 2;-
114 if (++
++x == wDescription
TRUEnever evaluated
FALSEnever evaluated
x == w
++x == wDescription
TRUEnever evaluated
FALSEnever evaluated
&& ++
++y < hDescription
TRUEnever evaluated
FALSEnever evaluated
y < h
++y < hDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
115 b = outImage->scanLine(y);-
116 x = 0;-
117 }
never executed: end of block
0
118 p = strstr(p, "0x");-
119 }
never executed: end of block
else {
0
120 if ((
(readBytes = d...,buflen)) <= 0Description
TRUEnever evaluated
FALSEnever evaluated
readBytes = device->readLine(buf,buflen)) <= 0
(readBytes = d...,buflen)) <= 0Description
TRUEnever evaluated
FALSEnever evaluated
)
0
121 break;
never executed: break;
0
122 buf[readBytes] = '\0';-
123 p = strstr(buf, "0x");-
124 }
never executed: end of block
0
125 }-
126-
127 return
never executed: return true;
true;
never executed: return true;
0
128}-
129-
130static bool read_xbm_image(QIODevice *device, QImage *outImage)-
131{-
132 int w = 0, h = 0;-
133 if (!read_xbm_header(device, w, h)
!read_xbm_header(device, w, h)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
134 return
never executed: return false;
false;
never executed: return false;
0
135 return
never executed: return read_xbm_body(device, w, h, outImage);
read_xbm_body(device, w, h, outImage);
never executed: return read_xbm_body(device, w, h, outImage);
0
136}-
137-
138static bool write_xbm_image(const QImage &sourceImage, QIODevice *device, const QString &fileName)-
139{-
140 QImage image = sourceImage;-
141 int w = image.width();-
142 int h = image.height();-
143 int i;-
144 QString s = fileName;-
145 int msize = s.length() + 100;-
146 char *buf = new char[msize];-
147-
148 qsnprintf(buf, msize, "#define %s_width %d\n", s.toUtf8().data(), w);-
149 device->write(buf, qstrlen(buf));-
150 qsnprintf(buf, msize, "#define %s_height %d\n", s.toUtf8().data(), h);-
151 device->write(buf, qstrlen(buf));-
152 qsnprintf(buf, msize, "static char %s_bits[] = {\n ", s.toUtf8().data());-
153 device->write(buf, qstrlen(buf));-
154-
155 if (image.format() != QImage::Format_MonoLSB
image.format()...Format_MonoLSBDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
156 image = image.convertToFormat(QImage::Format_MonoLSB);
never executed: image = image.convertToFormat(QImage::Format_MonoLSB);
0
157-
158 bool invert = qGray(image.color(0)) < qGray(image.color(1));-
159 char hexrep[16];-
160 for (i=0; i<10
i<10Description
TRUEnever evaluated
FALSEnever evaluated
; i++)
0
161 hexrep[i] = '0' + i;
never executed: hexrep[i] = '0' + i;
0
162 for (i=10; i<16
i<16Description
TRUEnever evaluated
FALSEnever evaluated
; i++)
0
163 hexrep[i] = 'a' -10 + i;
never executed: hexrep[i] = 'a' -10 + i;
0
164 if (invert
invertDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
165 char t;-
166 for (i=0; i<8
i<8Description
TRUEnever evaluated
FALSEnever evaluated
; i++) {
0
167 t = hexrep[15-i];-
168 hexrep[15-i] = hexrep[i];-
169 hexrep[i] = t;-
170 }
never executed: end of block
0
171 }
never executed: end of block
0
172 int bcnt = 0;-
173 char *p = buf;-
174 int bpl = (w+7)/8;-
175 for (int y = 0; y < h
y < hDescription
TRUEnever evaluated
FALSEnever evaluated
; ++y) {
0
176 const uchar *b = image.constScanLine(y);-
177 for (i = 0; i < bpl
i < bplDescription
TRUEnever evaluated
FALSEnever evaluated
; ++i) {
0
178 *p++ = '0'; *p++ = 'x';-
179 *p++ = hexrep[*b >> 4];-
180 *p++ = hexrep[*b++ & 0xf];-
181-
182 if (i < bpl - 1
i < bpl - 1Description
TRUEnever evaluated
FALSEnever evaluated
|| y < h - 1
y < h - 1Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
183 *p++ = ',';-
184 if (++
++bcnt > 14Description
TRUEnever evaluated
FALSEnever evaluated
bcnt > 14
++bcnt > 14Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
185 *p++ = '\n';-
186 *p++ = ' ';-
187 *p = '\0';-
188 if ((
(int)qstrlen(b... qstrlen(buf))Description
TRUEnever evaluated
FALSEnever evaluated
int)qstrlen(buf) != device->write(buf, qstrlen(buf))
(int)qstrlen(b... qstrlen(buf))Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
189 delete [] buf;-
190 return
never executed: return false;
false;
never executed: return false;
0
191 }-
192 p = buf;-
193 bcnt = 0;-
194 }
never executed: end of block
0
195 }
never executed: end of block
0
196 }
never executed: end of block
0
197 }
never executed: end of block
0
198-
199-
200-
201 strcpy(p, " };\n");-
202-
203 if ((
(int)qstrlen(b... qstrlen(buf))Description
TRUEnever evaluated
FALSEnever evaluated
int)qstrlen(buf) != device->write(buf, qstrlen(buf))
(int)qstrlen(b... qstrlen(buf))Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
204 delete [] buf;-
205 return
never executed: return false;
false;
never executed: return false;
0
206 }-
207-
208 delete [] buf;-
209 return
never executed: return true;
true;
never executed: return true;
0
210}-
211-
212QXbmHandler::QXbmHandler()-
213 : state(Ready)-
214{-
215}
never executed: end of block
0
216-
217bool QXbmHandler::readHeader()-
218{-
219 state = Error;-
220 if (!read_xbm_header(device(), width, height)
!read_xbm_head...width, height)Description
TRUEnever evaluated
FALSEnever evaluated
)
0
221 return
never executed: return false;
false;
never executed: return false;
0
222 state = ReadHeader;-
223 return
never executed: return true;
true;
never executed: return true;
0
224}-
225-
226bool QXbmHandler::canRead() const-
227{-
228 if (state == Ready
state == ReadyDescription
TRUEnever evaluated
FALSEnever evaluated
&& !canRead(device())
!canRead(device())Description
TRUEnever evaluated
FALSEnever evaluated
)
0
229 return
never executed: return false;
false;
never executed: return false;
0
230-
231 if (state != Error
state != ErrorDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
232 setFormat("xbm");-
233 return
never executed: return true;
true;
never executed: return true;
0
234 }-
235-
236 return
never executed: return false;
false;
never executed: return false;
0
237}-
238-
239bool QXbmHandler::canRead(QIODevice *device)-
240{-
241 QImage image;-
242-
243-
244-
245-
246 if (device->isSequential()
device->isSequential()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
247 return
never executed: return false;
false;
never executed: return false;
0
248-
249 qint64 oldPos = device->pos();-
250 bool success = read_xbm_image(device, &image);-
251 device->seek(oldPos);-
252-
253 return
never executed: return success;
success;
never executed: return success;
0
254}-
255-
256bool QXbmHandler::read(QImage *image)-
257{-
258 if (state == Error
state == ErrorDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
259 return
never executed: return false;
false;
never executed: return false;
0
260-
261 if (state == Ready
state == ReadyDescription
TRUEnever evaluated
FALSEnever evaluated
&& !readHeader()
!readHeader()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
262 state = Error;-
263 return
never executed: return false;
false;
never executed: return false;
0
264 }-
265-
266 if (!read_xbm_body(device(), width, height, image)
!read_xbm_body...height, image)Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
267 state = Error;-
268 return
never executed: return false;
false;
never executed: return false;
0
269 }-
270-
271 state = Ready;-
272 return
never executed: return true;
true;
never executed: return true;
0
273}-
274-
275bool QXbmHandler::write(const QImage &image)-
276{-
277 return
never executed: return write_xbm_image(image, device(), fileName);
write_xbm_image(image, device(), fileName);
never executed: return write_xbm_image(image, device(), fileName);
0
278}-
279-
280bool QXbmHandler::supportsOption(ImageOption option) const-
281{-
282 return
never executed: return option == Name || option == Size || option == ImageFormat;
option == Name
never executed: return option == Name || option == Size || option == ImageFormat;
0
283 || option == Size
never executed: return option == Name || option == Size || option == ImageFormat;
0
284 || option == ImageFormat;
never executed: return option == Name || option == Size || option == ImageFormat;
0
285}-
286-
287QVariant QXbmHandler::option(ImageOption option) const-
288{-
289 if (option == Name
option == NameDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
290 return
never executed: return fileName;
fileName;
never executed: return fileName;
0
291 } else if (option == Size
option == SizeDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
292 if (state == Error
state == ErrorDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
293 return
never executed: return QVariant();
QVariant();
never executed: return QVariant();
0
294 if (state == Ready
state == ReadyDescription
TRUEnever evaluated
FALSEnever evaluated
&& !const_cast<QXbmHandler*>(this)->readHeader()
!const_cast<QX...->readHeader()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
295 return
never executed: return QVariant();
QVariant();
never executed: return QVariant();
0
296 return
never executed: return QSize(width, height);
QSize(width, height);
never executed: return QSize(width, height);
0
297 } else if (option == ImageFormat
option == ImageFormatDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
298 return
never executed: return QImage::Format_MonoLSB;
QImage::Format_MonoLSB;
never executed: return QImage::Format_MonoLSB;
0
299 }-
300 return
never executed: return QVariant();
QVariant();
never executed: return QVariant();
0
301}-
302-
303void QXbmHandler::setOption(ImageOption option, const QVariant &value)-
304{-
305 if (option == Name
option == NameDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
306 fileName = value.toString();
never executed: fileName = value.toString();
0
307}
never executed: end of block
0
308-
309QByteArray QXbmHandler::name() const-
310{-
311 return
never executed: return "xbm";
"xbm";
never executed: return "xbm";
0
312}-
313-
314-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9