-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathemf2svg_img_utils.c
468 lines (426 loc) · 13.6 KB
/
emf2svg_img_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#ifdef __cplusplus
extern "C" {
#endif
#ifndef DARWIN
#define _POSIX_C_SOURCE 200809L
#endif
#include "emf2svg_img_utils.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef DARWIN
#include <memstream.h>
#endif
#include <png.h>
RGBAPixel *pixel_at(RGBABitmap *bitmap, int x, int y) {
return bitmap->pixels + (bitmap->width * y + x);
}
// return the size in octet of a pixel
float get_pixel_size(uint32_t colortype) {
switch (colortype) {
case U_BCBM_MONOCHROME:
return 0.125;
case U_BCBM_COLOR4:
return 0.5;
case U_BCBM_COLOR8:
return 1;
case U_BCBM_COLOR16:
return 2;
case U_BCBM_COLOR24:
return 3;
case U_BCBM_COLOR32:
return 4;
}
return 4;
}
/* Attempts to save PNG to file; returns 0 on success, non-zero on error. */
int rgb2png(RGBABitmap *bitmap, char **out, size_t *size) {
FILE *fp = open_memstream(out, size);
if (fp == NULL) {
return -1;
}
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
size_t width_by_height;
// png_uint_32 bytes_per_row;
png_byte **row_pointers = NULL;
bool alpha_channel_empty = true;
if (fp == NULL)
return -1;
/* Initialize the write struct. */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return -1;
}
/* Initialize the info struct. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return -1;
}
/* Set up error handling. */
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return -1;
}
/* Set image attributes. */
png_set_IHDR(png_ptr, info_ptr, bitmap->width, bitmap->height, 8,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
/* Initialize rows of PNG. */
// bytes_per_row = bitmap->width * bytes_per_pixel;
row_pointers = png_malloc(png_ptr, bitmap->height * sizeof(png_byte *));
// Check to see if alpha channel is used (nonzero)
width_by_height = bitmap->width * bitmap->height;
for (x = 0; x < width_by_height; ++x) {
if (bitmap->pixels[x].alpha) {
alpha_channel_empty = false;
break;
}
}
for (y = 0; y < bitmap->height; ++y) {
uint8_t *row = png_malloc(png_ptr, sizeof(uint8_t) * bitmap->width * 4);
// row_pointers[y] = (png_byte *)row;
row_pointers[bitmap->height - y - 1] = row;
if (alpha_channel_empty) {
for (x = 0; x < bitmap->width; ++x) {
// RGBPixel *color = pixel_at(bitmap, x, y);
RGBAPixel color = bitmap->pixels[((x + bitmap->width * y))];
// printf("(%d, %d)\n", bitmap->width, bitmap->height);
// printf("(%d, %d)\n", x, y);
// printf("color:0x%0X%0X%0x\n", color.red, color.green,
// color.blue);
*row++ = color.red;
*row++ = color.green;
*row++ = color.blue;
//*row++ = color.alpha;
*row++ = 0xFF;
}
} else {
for (x = 0; x < bitmap->width; ++x) {
RGBAPixel color = bitmap->pixels[((x + bitmap->width * y))];
*row++ = color.red;
*row++ = color.green;
*row++ = color.blue;
*row++ = color.alpha;
}
}
}
/* Actually write the image data. */
png_init_io(png_ptr, fp);
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
/* Cleanup. */
for (y = 0; y < bitmap->height; y++) {
png_free(png_ptr, row_pointers[y]);
}
png_free(png_ptr, row_pointers);
/* Finish writing. */
png_destroy_write_struct(&png_ptr, &info_ptr);
fflush(fp);
fclose(fp);
return 0;
}
// uncompress RLE8 to get bitmap (section 3.1.6.2 [MS-WMF].pdf)
RGBBitmap rle8ToRGB8(RGBBitmap img) {
FILE *stream;
bool decode = true;
char *out;
size_t size;
RGBBitmap out_img;
out_img.size = 0;
out_img.width = 0;
out_img.height = 0;
out_img.bytes_per_pixel = 3;
out_img.pixels = NULL;
uint8_t *bm = (uint8_t *)img.pixels;
uint32_t x = 0;
uint32_t y = img.height - 1;
uint8_t *bm_next;
if (img.width > MAX_BMP_WIDTH || img.height > MAX_BMP_HEIGHT ||
img.width == 0 || img.height == 0) {
return out_img;
}
stream = open_memstream(&out, &size);
if (stream == NULL) {
return out_img;
}
uint8_t *end = bm + img.size;
while (decode && (bm < end)) {
// check against potential overflow
if ((bm + 2) > end || x > MAX_BMP_WIDTH || y > MAX_BMP_HEIGHT) {
fclose(stream);
free(out);
return out_img;
};
switch (bm[0]) {
case RLE_MARK:
switch (bm[1]) {
case RLE_EOL:
// end of line, pad the rest of the line with zeros
for (int i = 0; i < ((int)img.width - (int)x); i++)
fputc(0x00, stream);
bm += 2;
x = 0;
y--;
break;
case RLE_EOB:
// end of bitmap
decode = false;
break;
case RLE_DELTA:
// offset handling, pad with (off.x + off.y * width) zeros
if ((bm + 3) > end) {
fclose(stream);
free(out);
return out_img;
};
for (int i = 0; i < (bm[2] + img.width * bm[3]); i++)
fputc(0x00, stream);
x += bm[2];
y -= bm[3];
bm += 4;
break;
default:
// absolute mode handling (no compression)
// data is padded to a word
// calculate next address accordingly
bm_next = bm + 1 + ((bm[1] + 1) / 2) * 2;
if (bm_next > end) {
fclose(stream);
free(out);
return out_img;
};
for (int i = 2; i < bm[1] + 2; i++)
fputc(bm[i], stream);
x += bm[1];
bm = bm_next + 1;
break;
}
break;
default:
for (int i = 0; i < bm[0]; i++)
fputc(bm[1], stream);
x += bm[0];
bm += 2;
if (x >= img.width) {
x = x % img.width;
y--;
}
break;
}
}
// pad the rest of the bitmap
for (int i = 0; i < (((int)img.width - x) + (int)img.width * y); i++)
fputc(0x00, stream);
fflush(stream);
fclose(stream);
out_img.pixels = (RGBPixel *)out;
out_img.size = size;
out_img.width = img.width;
out_img.height = img.height;
return out_img;
}
int e2s_get_DIB_params(PU_BITMAPINFO Bmi, const U_RGBQUAD **ct, uint32_t *numCt,
uint32_t *width, uint32_t *height, uint32_t *colortype,
uint32_t *invert) {
uint32_t bic;
/* if biCompression is not U_BI_RGB some or all of the following might not
* hold real values */
PU_BITMAPINFOHEADER Bmih = &(Bmi->bmiHeader);
bic = Bmih->biCompression;
*width = Bmih->biWidth;
*colortype = Bmih->biBitCount;
if (Bmih->biHeight < 0) {
*height = -Bmih->biHeight;
*invert = 1;
} else {
*height = Bmih->biHeight;
*invert = 0;
}
if (bic == U_BI_RGB) {
*numCt = get_real_color_count((const char *)Bmih);
if (numCt) {
*ct = (PU_RGBQUAD)((char *)Bmi + sizeof(U_BITMAPINFOHEADER));
} else {
*ct = NULL;
}
} else if (bic ==
U_BI_BITFIELDS) { /* to date only encountered once, for 32 bit,
from PPT*/
*numCt = 0;
*ct = NULL;
bic = U_BI_RGB; /* there seems to be no difference, at least for the 32
bit images */
} else {
*numCt = Bmih->biSizeImage;
*ct = NULL;
}
return (bic);
}
// uncompress RLE4 to get bitmap (section 3.1.6.2 [MS-WMF].pdf)
// FIXME (probably) (handling 4 bits stuff is kind of messy...)
RGBBitmap rle4ToRGB(RGBBitmap img) {
FILE *stream;
bool decode = true;
char *out;
size_t size;
RGBBitmap out_img;
out_img.size = 0;
out_img.width = 0;
out_img.height = 0;
out_img.bytes_per_pixel = 2;
out_img.pixels = NULL;
uint8_t *bm = (uint8_t *)img.pixels;
uint32_t x = 0;
uint32_t y = img.height - 1;
uint8_t *bm_next;
if (img.width > MAX_BMP_WIDTH || img.height > MAX_BMP_HEIGHT ||
img.width == 0 || img.height == 0) {
return out_img;
}
stream = open_memstream(&out, &size);
if (stream == NULL) {
return out_img;
}
// upper 4 bits of the stuff to write
uint8_t upper = 0x00;
// lower 4 bits of the stuff to write
uint8_t lower = 0x00;
// Is the current position on upper 4 bits?
// If it's the case, swap upper and lower of what we read
// and eventualy print the upper part of previous record
bool odd = false;
uint8_t tmp_u;
uint8_t tmp_l;
uint8_t *end = bm + img.size;
while (decode && (bm < end)) {
// check against potential overflow
if ((bm + 2) > end || x > MAX_BMP_WIDTH || y > MAX_BMP_HEIGHT) {
fclose(stream);
free(out);
return out_img;
};
switch (bm[0]) {
case RLE_MARK:
switch (bm[1]) {
case RLE_EOL:
if (odd) {
fputc(upper | 0x00, stream);
upper = 0x00;
lower = 0x00;
}
// end of line, pad the rest of the line with zeros
for (int i = 0; i < ((((int)img.width - (int)x) / 2) - 1); i++)
fputc(0x00, stream);
odd = img.width % 2;
bm += 2;
x = 0;
y--;
break;
case RLE_EOB:
// end of bitmap
decode = false;
break;
case RLE_DELTA:
// offset handling, pad with (off.x + off.y * width) zeros
if ((bm + 3) > end) {
fclose(stream);
free(out);
return out_img;
};
tmp_u = 0x00;
tmp_l = 0x00;
if (odd) {
fputc(upper | tmp_l, stream);
}
upper = tmp_u;
lower = tmp_l;
for (int i = 0; i < ((bm[2] + img.width * bm[3]) / 2); i++)
fputc(0x00, stream);
odd = ((bm[2] + img.width * bm[3]) + odd) % 2;
x += bm[2];
y -= bm[3];
bm += 4;
break;
default:
// absolute mode handling (no compression)
// data is padded to a word
// calculate next address accordingly
bm_next = bm + (bm[1] / 2) + 2;
if (bm_next > end) {
fclose(stream);
free(out);
return out_img;
};
for (int i = 2; i < (bm[1] / 2) + 2; i++) {
if (!odd) {
tmp_u = bm[i] & 0xF0;
tmp_l = bm[i] & 0x0F;
fputc(tmp_u | tmp_l, stream);
} else {
tmp_u = (bm[i] & 0x0F) << 4;
tmp_l = (bm[i] & 0xF0) >> 4;
fputc(upper | tmp_l, stream);
}
upper = tmp_u;
lower = tmp_l;
}
x += bm[1];
bm = bm_next + 1;
odd = (bm[1] + odd) % 2;
if (odd) {
upper = (bm[0] & 0x0F) << 4;
lower = (bm[0] & 0xF0) >> 4;
bm += 1;
}
break;
}
break;
default:
if (!odd) {
tmp_u = bm[1] & 0xF0;
tmp_l = bm[1] & 0x0F;
} else {
tmp_u = (bm[1] & 0x0F) << 4;
tmp_l = (bm[1] & 0xF0) >> 4;
fputc(upper | tmp_l, stream);
}
upper = tmp_u;
lower = tmp_l;
for (int i = 0; i < (bm[0] / 2); i++) {
fputc(upper | lower, stream);
}
odd = (bm[0] + odd) % 2;
x += bm[0];
bm += 2;
if (x >= img.width) {
x = x % img.width;
y--;
}
break;
}
}
// pad the rest of the bitmap
if (odd) {
fputc(upper | 0x00, stream);
}
// end of line, pad the rest of the line with zeros
for (int i = 0; i < (((int)img.width - x + (int)img.width * y) / 2); i++)
fputc(0x00, stream);
fflush(stream);
fclose(stream);
out_img.pixels = (RGBPixel *)out;
out_img.size = size;
out_img.width = img.width;
out_img.height = img.height;
return out_img;
}
#ifdef __cplusplus
}
#endif
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */