2 #include "OmXmlWriter.h"
13 #define UNUSED(x) (void)(x)
21 bool put(uint8_t ch)
override
33 uint8_t byteInProgress;
35 bool putBmpHeader(
int width,
int height)
37 int rowBytes = ((width + 7) / 8 + 3) & 0xfffffffc;
38 int dataSize = rowBytes * height;
40 const int bmpHeaderSize = 14;
41 const int bmpInfoHeaderSize = 40;
42 int fileSize = bmpHeaderSize + bmpInfoHeaderSize + paletteSize + dataSize;
48 result &= put32(fileSize);
51 result &= put32(bmpHeaderSize + bmpInfoHeaderSize + paletteSize);
54 result &= put32(bmpInfoHeaderSize);
55 result &= put32(this->width);
56 result &= put32(this->height);
60 result &= put32(dataSize);
61 result &= put32(1000);
62 result &= put32(1000);
68 result &= put32(0x00101010);
69 result &= put32(0x00c0c0c0);
76 this->consumer = consumer;
77 this->didPutHeader =
false;
81 this->height = height;
82 this->byteInProgress = 0;
86 this->rowPadBytes = (4 - ((width % 32) + 7) / 8) & 3;
94 if(this->x >= this->width && this->y >= this->height)
100 result &= this->putBmpHeader(this->width, this->height);
101 this->didPutHeader =
true;
103 this->byteInProgress |= bit << (7 - this->x % 8);
105 if(this->x == this->width || (this->x & 0x0007) == 0)
107 this->consumer->
put(this->byteInProgress);
108 this->byteInProgress = 0;
111 if(this->x == this->width)
114 for(
int ix = 0; ix < this->rowPadBytes; ix++)
115 result &= this->put8(0);
124 bool putN(
int n, uint8_t *data)
128 result &= this->consumer->
put(*data++);
134 return this->putN(1, &x);
137 bool put16(uint16_t x)
139 return this->putN(2, (uint8_t *)&x);
142 bool put32(uint32_t x)
144 return this->putN(4, (uint8_t *)&x);
155 bool didPutHeader =
false;
163 this->consumer = consumer;
165 this->height = height;
166 this->didPutHeader =
false;
171 bool put(uint8_t ch)
override
180 int rowBytes = (this->width * 3 + 3) & 0xffffFFFC;
181 int dataSize = rowBytes * this->height;
183 const int bmpHeaderSize = 14;
184 const int bmpInfoHeaderSize = 40;
185 int fileSize = bmpHeaderSize + bmpInfoHeaderSize + paletteSize + dataSize;
191 result &= put32(fileSize);
194 result &= put32(bmpHeaderSize + bmpInfoHeaderSize + paletteSize);
197 result &= put32(bmpInfoHeaderSize);
198 result &= put32(this->width);
199 result &= put32(this->height);
203 result &= put32(dataSize);
204 result &= put32(1000);
205 result &= put32(1000);
218 if(this->x >= this->width && this->y >= this->height)
224 result &= this->putBmpHeader();
225 this->didPutHeader =
true;
227 result &= this->put8(b);
228 result &= this->put8(g);
229 result &= this->put8(r);
232 if(this->x >= this->width)
236 int extras = (4 - (this->width * 3) % 4) % 4;
243 if(this->y >= this->height)
245 this->consumer->done();
254 bool putN(
int n, uint8_t *data)
258 result &= this->consumer->
put(*data++);
264 return this->putN(1, &x);
267 bool put16(uint16_t x)
269 return this->putN(2, (uint8_t *)&x);
272 bool put32(uint32_t x)
274 return this->putN(4, (uint8_t *)&x);
279 extern const uint8_t base64_table[65];
293 this->consumer = consumer;
298 bool put(uint8_t ch)
override
304 if(this->bufSize < 3)
305 this->buf[this->bufSize++] = ch;
307 if(this->bufSize == 3)
310 result &= this->consumer->
put(base64_table[this->buf[0] >> 2]);
311 result &= this->consumer->
put(base64_table[((this->buf[0] & 0x03) << 4) | (this->buf[1] >> 4)]);
312 result &= this->consumer->
put(base64_table[((this->buf[1] & 0x0f) << 2) | (this->buf[2] >> 6)]);
313 result &= this->consumer->
put(base64_table[this->buf[2] & 0x3f]);
324 switch(this->bufSize)
330 result &= this->consumer->
put(base64_table[this->buf[0] >> 2]);
331 result &= this->consumer->
put(base64_table[(this->buf[0] & 0x03) << 4]);
332 result &= this->consumer->
put(
'=');
333 result &= this->consumer->
put(
'=');
337 result &= this->consumer->
put(base64_table[this->buf[0] >> 2]);
338 result &= this->consumer->
put(base64_table[((this->buf[0] & 0x03) << 4) | (this->buf[1] >> 4)]);
339 result &= this->consumer->
put(base64_table[(this->buf[1] & 0x0f) << 2]);
340 result &= this->consumer->
put(
'=');