36 const int buflen = 300;
37 const int maxlen = 4096;
41 qint64 totalReadBytes = 0;
46 while (buf[0] !=
'#') {
47 readBytes = device->readLine(buf, buflen);
50 if (readBytes <= 0 || readBytes >= buflen -1)
55 totalReadBytes += readBytes;
56 if (totalReadBytes >= maxlen)
60 auto parseDefine = [] (
const char *buf,
int len) ->
int {
61 auto checkChar = [] (
char ch) ->
bool {
62 return isAsciiLetterOrNumber(ch)
63 || ch ==
'_' || ch ==
'.';
65 auto isAsciiSpace = [] (
char ch) ->
bool {
66 return ch ==
' ' || ch ==
'\t';
68 const char define[] =
"#define";
69 constexpr size_t defineLen =
sizeof(define) - 1;
70 if (strncmp(buf, define, defineLen) != 0)
72 int index = defineLen;
73 while (buf[index] && isAsciiSpace(buf[index]))
75 while (buf[index] && checkChar(buf[index]))
77 while (buf[index] && isAsciiSpace(buf[index]))
80 return QByteArray(buf + index, len - index).toInt();
85 w = parseDefine(buf, readBytes - 1);
87 readBytes = device->readLine(buf, buflen);
89 h = parseDefine(buf, readBytes - 1);
92 if (w <= 0 || w > 32767 || h <= 0 || h > 32767)
98static bool read_xbm_body(QIODevice *device,
int w,
int h, QImage *outImage)
100 const int buflen = 300;
101 char buf[buflen + 1];
103 qint64 readBytes = 0;
109 if ((readBytes = device->readLine(buf, buflen)) <= 0) {
114 buf[readBytes] =
'\0';
115 p = strstr(buf,
"0x");
118 if (!QImageIOHandler::allocateImage(QSize(w, h), QImage::Format_MonoLSB, outImage))
121 outImage->fill(Qt::color0);
123 outImage->setColorCount(2);
124 outImage->setColor(0, qRgb(255,255,255));
125 outImage->setColor(1, qRgb(0,0,0));
128 uchar *b = outImage->scanLine(0);
132 if (p && p < (buf + readBytes - 3)) {
138 if (++x == w && ++y < h) {
139 b = outImage->scanLine(y);
144 if ((readBytes = device->readLine(buf,buflen)) <= 0)
146 buf[readBytes] =
'\0';
147 p = strstr(buf,
"0x");
162static bool write_xbm_image(
const QImage &sourceImage, QIODevice *device,
const QString &fileName)
164 QImage image = sourceImage;
165 int w = image.width();
166 int h = image.height();
169 const auto msize = s.size() + 100;
170 char *buf =
new char[msize];
172 std::snprintf(buf, msize,
"#define %s_width %d\n", s.data(), w);
173 device->write(buf, qstrlen(buf));
174 std::snprintf(buf, msize,
"#define %s_height %d\n", s.data(), h);
175 device->write(buf, qstrlen(buf));
176 std::snprintf(buf, msize,
"static char %s_bits[] = {\n ", s.data());
177 device->write(buf, qstrlen(buf));
179 if (image.format() != QImage::Format_MonoLSB)
180 image = image.convertToFormat(QImage::Format_MonoLSB);
182 bool invert = qGray(image.color(0)) < qGray(image.color(1));
186 for (i=10; i<16; i++)
187 hexrep[i] =
'a' -10 + i;
190 for (i=0; i<8; i++) {
192 hexrep[15-i] = hexrep[i];
199 for (
int y = 0; y < h; ++y) {
200 const uchar *b = image.constScanLine(y);
201 for (i = 0; i < bpl; ++i) {
202 *p++ =
'0'; *p++ =
'x';
203 *p++ = hexrep[*b >> 4];
204 *p++ = hexrep[*b++ & 0xf];
206 if (i < bpl - 1 || y < h - 1) {
212 if ((
int)qstrlen(buf) != device->write(buf, qstrlen(buf))) {
223 strcpy_s(p,
sizeof(
" };\n"),
" };\n");
227 if ((
int)qstrlen(buf) != device->write(buf, qstrlen(buf))) {