image/qxbmhandler.cpp

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

Generated by Squish Coco Non-Commercial