-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbmpfont_create_core.c
738 lines (664 loc) · 19.8 KB
/
bmpfont_create_core.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#include <windows.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
#include "bmpfont_create.h"
typedef enum
{
// Plain RGBA output
UNPACKED_RGBA,
// The mode used by the game. Each color channel is used as a grayscale image.
PACKED_RGBA,
// Grayscale output
UNPACKED_GRAYSCALE,
// Grayscale output, with:
// (0;0) containing the 1st pixel of the 1st character,
// (1;0) containing the 1st pixel of the 2st character,
// (2;0) containing the 1st pixel of the 3st character,
// (3;0) containing the 1st pixel of the 4st character,
// (4;0) containing the 2st pixel of the 1st character,
// etc. It is the format used by thcrap (to make conversion to packed RGBA easier).
PACKED_GRAYSCALE,
INVALID_FORMAT
} OutputType;
typedef enum
{
ROTATE_0,
ROTATE_90,
ROTATE_180,
ROTATE_270
} RotateType;
#pragma pack(push, 1)
typedef struct
{
int16_t x;
int16_t y;
uint8_t width;
uint8_t height;
uint8_t y_offset;
uint8_t channel;
} CharDetail;
#pragma pack(pop)
typedef struct
{
unsigned int x;
unsigned int y;
int channel;
unsigned int w;
unsigned int h;
unsigned int char_w;
unsigned int line_h;
struct {
void* (*init)();
int (*consume_option)(void*, const char*, const char*);
int (*consume_option_binary)(void*, const char*, const void*, size_t);
void (*put_char)(void*, WCHAR, BYTE**, int*, int*);
void (*free)(void*);
} func;
void *graphics;
HMODULE hMod;
const char *exe;
unsigned int cp;
char chars_list[65536];
OutputType output_type;
RotateType rotate_type;
int export_png;
const char *out_fn;
BYTE **out_mem;
size_t *out_size;
} State;
wchar_t *to_unicode(State *state, const char *src)
{
wchar_t *dst = malloc((strlen(src) + 1) * sizeof(wchar_t));
MultiByteToWideChar(state->cp, 0, src, -1, dst, strlen(src) + 1);
return dst;
}
void rotate(int *x, int *y, int w, int h, RotateType rotateType)
{
int old_x = *x;
int old_y = *y;
switch (rotateType)
{
case ROTATE_0:
return ;
case ROTATE_90:
*x = old_y;
*y = h - old_x - 1;
return ;
case ROTATE_180:
*x = w - old_x - 1;
*y = h - old_y - 1;
return ;
case ROTATE_270:
*x = w - old_y - 1;
*y = old_x;
return ;
}
}
int put_char(State *state, WCHAR c, BYTE** dest, CharDetail* charDetail)
{
int w;
int h;
int w_r;
int h_r;
BYTE *buffer = (BYTE*) malloc(256 * 256 * 4);
BYTE **buffer_rows = (BYTE**)malloc(256 * sizeof(BYTE*));
memset(buffer, 0, 256 * 256 * 4);
int i;
for (i = 0; i < 256; i++)
buffer_rows[i] = &buffer[256 * 4 * i];
state->func.put_char(state->graphics, c, buffer_rows, &w, &h);
if (c == L' ')
w += 8;
if (state->rotate_type == ROTATE_90 || state->rotate_type == ROTATE_270)
{
w_r = h;
h_r = w;
}
else
{
w_r = w;
h_r = h;
}
if (state->x + w_r >= state->w)
{
free(buffer);
free(buffer_rows);
return 0;
}
if (w_r > (int)state->char_w)
state->char_w = w_r;
if (h_r > (int)state->line_h)
state->line_h = h_r;
int x;
int y;
for (x = 0; x < w_r; x++)
for (y = 0; y < h_r; y++) {
int x2 = (state->x + x) * 4 + state->channel;
int y2 = state->y + y;
int x_r = x;
int y_r = y;
rotate(&x_r, &y_r, w, h, state->rotate_type);
switch (state->output_type)
{
case UNPACKED_RGBA:
dest[y2][x2 + 0] = buffer_rows[y_r][x_r * 4 + 0];
dest[y2][x2 + 1] = buffer_rows[y_r][x_r * 4 + 1];
dest[y2][x2 + 2] = buffer_rows[y_r][x_r * 4 + 2];
dest[y2][x2 + 3] = buffer_rows[y_r][x_r * 4 + 3];
break;
case UNPACKED_GRAYSCALE:
dest[y2][state->x + x] = buffer_rows[y_r][x_r * 4];
break;
case PACKED_RGBA:
case PACKED_GRAYSCALE:
dest[y2][x2] = buffer_rows[y_r][x_r * 4];
break;
case INVALID_FORMAT:
break;
}
}
charDetail->x = state->x;
charDetail->y = state->y;
charDetail->width = w_r;
charDetail->height = h_r;
charDetail->y_offset = 0;
charDetail->channel = state->channel;
free(buffer_rows);
free(buffer);
return 1;
}
int export_to_PNG(uint8_t** rows, size_t width, size_t height, char* out_png)
{
png_structp png_ptr;
png_infop info_ptr;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
if (!png_ptr || !info_ptr || setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
return 0;
}
FILE *out = fopen(out_png, "wb");
if (!out) {
perror(out_png);
png_destroy_write_struct(&png_ptr, &info_ptr);
return 0;
}
png_init_io(png_ptr, out);
// TODO: change bit depth to 4
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
uint8_t* out_data = malloc(width * height);
uint8_t** out_rows = malloc(height * sizeof(uint8_t*));
for (uint32_t i = 0; i < height; i++) {
out_rows[i] = &out_data[i * width];
for (uint32_t j = 0; j < width; j++)
out_rows[i][j] = rows[i][j];
}
png_write_info(png_ptr, info_ptr);
for (uint32_t i = 0; i < height; i++) {
png_write_row(png_ptr, out_rows[i]);
}
png_write_end(png_ptr, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
free(out_rows);
free(out_data);
fclose(out);
return 1;
}
void *bmpfont_init()
{
State *bmpfont = malloc(sizeof(State));
memset(bmpfont, 0, sizeof(State));
bmpfont->out_mem = NULL;
bmpfont->exe = "bmpfont";
bmpfont->cp = CP_OEMCP;
bmpfont->output_type = INVALID_FORMAT;
for (uint16_t c = 0; c < 65535; c++)
bmpfont->chars_list[c] = 1;
return bmpfont;
}
void usage(State *state)
{
printf("Usage: %s option...\n"
" --help Display this help message.\n"
" --cp codepage Codepage to use when processing strings (defaults to CP_OEMCP).\n"
" It may or may not apply to options passed before it. In doubt,\n"
" pass it before any text-based option.\n"
" --format format unpacked_rgba, packed_rgba, unpacked_grayscale\n"
" or packed_grayscale.\n"
" unpacked_rgba and unpacked_grayscale can be opened\n"
" with an image editor.\n"
" packed_rgba is the format used by the game.\n"
" packed_grayscale is the format expected by thcrap.\n"
" It also creates a PNG output.\n"
" --out output_file Output file name (required).\n"
" --plugin plugin Plugin to be used to render the texts (required).\n"
" --png true Exports the file as png (requires --format packed_grayscale).\n"
" --rotate angle Rotate the characters.\n"
" Angle can be 0, 90, 180 or 270.\n"
, state->exe);
if (state->func.consume_option)
{
printf("\nPlugin-specific options:\n");
state->func.consume_option(state->graphics, "--help", NULL);
}
else if (!state->func.init)
printf("For plugin-specific options, use --plugin [plugin] --help\n");
}
int init_plugin(State *state, const char* fn)
{
LPWSTR fn_w = to_unicode(state, fn);
state->hMod = LoadLibraryW(fn_w);
free(fn_w);
if (!state->hMod)
{
printf("%s: can't open plugin %s\n", state->exe, fn);
return 0;
}
state->func.init = (void*(*)()) GetProcAddress(state->hMod, "graphics_init");
state->func.consume_option = (int (*)(void*, const char*, const char*)) GetProcAddress(state->hMod, "graphics_consume_option");
state->func.consume_option_binary = (int (*)(void*, const char*, const void*, size_t)) GetProcAddress(state->hMod, "graphics_consume_option_binary");
state->func.put_char = (void (*)(void*, WCHAR, BYTE**, int*, int*)) GetProcAddress(state->hMod, "graphics_put_char");
state->func.free = (void (*)(void*)) GetProcAddress(state->hMod, "graphics_free");
int error = 0;
if (!state->func.init || !state->func.free || !state->func.put_char)
{
printf("%s: the %s plugin doesn's define all the functions needed by this program.\n", state->exe, fn);
error = 1;
}
else
{
state->graphics = state->func.init();
if (state->graphics == NULL)
error = 1;
}
if (error)
{
FreeLibrary(state->hMod);
state->func.init = NULL;
state->func.consume_option = NULL;
state->func.consume_option_binary = NULL;
state->func.put_char = NULL;
state->func.free = NULL;
state->hMod = NULL;
return 0;
}
return 1;
}
int bmpfont_add_option(void *bmpfont, const char *name, const char *value)
{
State *state = (State*)bmpfont;
if (strcmp(name, "--exe") == 0)
state->exe = value;
else if (strcmp(name, "--help") == 0)
{
usage(state);
return 0;
}
else if (strcmp(name, "--cp") == 0)
{
state->cp = atoi(value);
if (state->func.consume_option && !state->func.consume_option(state->graphics, name, value))
return 0;
}
else if (strcmp(name, "--plugin") == 0)
{
if (!init_plugin(state, value))
return 0;
char cp[11];
sprintf(cp, "%u", state->cp);
if (state->func.consume_option && !state->func.consume_option(state->graphics, "--cp", cp))
return 0;
}
else if (strcmp(name, "--format") == 0)
{
if (strcmp(value, "unpacked_rgba") == 0)
state->output_type = UNPACKED_RGBA;
else if (strcmp(value, "packed_rgba") == 0)
state->output_type = PACKED_RGBA;
else if (strcmp(value, "unpacked_grayscale") == 0)
state->output_type = UNPACKED_GRAYSCALE;
else if (strcmp(value, "packed_grayscale") == 0)
state->output_type = PACKED_GRAYSCALE;
else
{
printf("%s: unknown format %s\n", state->exe, value);
return 0;
}
}
else if (strcmp(name, "--out") == 0)
state->out_fn = value;
else if (strcmp(name, "--png") == 0)
state->export_png = 1;
else if (strcmp(name, "--rotate") == 0)
{
if (strcmp(value, "0") == 0)
state->rotate_type = ROTATE_0;
else if (strcmp(value, "90") == 0)
state->rotate_type = ROTATE_90;
else if (strcmp(value, "180") == 0)
state->rotate_type = ROTATE_180;
else if (strcmp(value, "270") == 0)
state->rotate_type = ROTATE_270;
else
{
printf("%s: unknown angle %s\n", state->exe, value);
return 0;
}
}
else
{
if (!state->func.consume_option)
{
fprintf(stderr, "%s: unrecognized option '%s'\n"
"Try '%s --help' for more information.",
state->exe, name, state->exe);
return 0;
}
if (!state->func.consume_option(state->graphics, name, value))
return 0;
}
return 1;
}
int bmpfont_add_option_binary(void *bmpfont, const char *name, void *value, size_t value_size)
{
State *state = (State*)bmpfont;
if (strcmp(name, "--out-buffer") == 0)
{
if (value_size != sizeof(void*))
{
printf("%s: binary option %s needs a value_size of %d\n", state->exe, name, sizeof(void*));
return 0;
}
state->out_mem = (BYTE**)value;
*state->out_mem = NULL;
}
else if (strcmp(name, "--out-size") == 0)
{
if (value_size != sizeof(void*))
{
printf("%s: binary option %s needs a value_size of %d\n", state->exe, name, sizeof(size_t));
return 0;
}
state->out_size = (size_t*)value;
}
else if (strcmp(name, "--chars-list") == 0)
{
char *chars_list = (char*)value;
memset(state->chars_list, 0, 65535);
for (uint16_t i = 0; i < 65535 && i < value_size; i++)
state->chars_list[i] = chars_list[i];
}
else
{
if (!state->func.consume_option_binary)
{
fprintf(stderr, "%s: unrecognized binary option '%s'\n"
"Check the header files for more information.",
state->exe, name);
return 0;
}
if (!state->func.consume_option_binary(state->graphics, name, value, value_size))
return 0;
}
return 1;
}
int check_state(State *state)
{
// Did we get all our required arguments?
if (state->output_type == INVALID_FORMAT)
printf("--format is required\n\n");
if (!state->out_fn && !state->out_mem)
printf("--out is required\n\n"); // If using as a library, --out-mem can be used instead
if (!state->func.init)
printf("--plugin is required\n\n");
if (state->output_type == INVALID_FORMAT || (!state->out_fn && !state->out_mem) || !state->func.init)
{
usage(state);
return 0;
}
// Ask the same question to our plugin
if (state->func.consume_option && state->func.consume_option(state->graphics, NULL, NULL) == 0)
return 0;
return 1;
}
static void make_bmpfont(State *state, BYTE **pData, BYTE ***pRows, CharDetail **pCharDetails)
{
state->x = 0;
state->y = 0;
state->channel = 0;
state->w = 2048;
state->h = 1024;
state->char_w = 0;
state->line_h = 0;
BYTE *data = (BYTE*) malloc(state->w * state->h * 4);
BYTE **rows = (BYTE**)malloc(state->h * sizeof(BYTE*));
memset(data, 0, state->w * state->h * 4);
for (unsigned int i = 0; i < state->h; i++)
rows[i] = data + i * state->w * 4;
CharDetail *charDetails = (CharDetail*)malloc(65536 * sizeof(CharDetail));
for (uint16_t c = 0; c < 65535; c++)
{
if (!state->chars_list[c])
continue;
if (put_char(state, c, rows, &charDetails[c]) == 0)
{
// TODO: clear (x;y) -> (x+char_w;y+line_h)
if (state->y + state->line_h + 256 > state->h)
{
state->h += 1024;
data = (BYTE*) realloc(data, state->w * state->h * 4);
rows = (BYTE**)realloc(rows, state->h * sizeof(BYTE*));
for (unsigned int i = 0; i < state->h; i++)
rows[i] = data + i * state->w * 4;
}
state->y += state->line_h;
state->x = 0;
state->line_h = 0;
// Redraw all the characters in the current cell
state->char_w = 0;
while (state->channel > 0)
{
c--;
state->channel--;
}
c--;
continue;
}
if (state->output_type != UNPACKED_RGBA && state->output_type != UNPACKED_GRAYSCALE)
state->channel++;
else
state->channel += 4;
if (state->channel == 4)
{
state->x += state->char_w;
state->char_w = 0;
state->channel = 0;
}
}
state->h = state->y + state->line_h;
*pData = data;
*pRows = rows;
*pCharDetails = charDetails;
}
static void fill_bmp_headers(State *state, BITMAPFILEHEADER *header, BITMAPINFOHEADER *info)
{
int bpp;
int palette_size;
int bitmap_size;
if (state->output_type == PACKED_GRAYSCALE)
state->w *= 4;
if (state->output_type == UNPACKED_RGBA || state->output_type == PACKED_RGBA)
{
bpp = 32;
palette_size = 0;
bitmap_size = state->w * state->h * 4;
}
else
{
bpp = 8;
palette_size = 256 * 4;
bitmap_size = state->w * state->h;
}
header->bfType = 'B' | ('M' << 8);
header->bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + palette_size + bitmap_size;
header->bfReserved1 = 0;
header->bfReserved2 = 0;
header->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + palette_size;
info->biSize = sizeof(BITMAPINFOHEADER);
info->biWidth = state->w;
info->biHeight = state->h;
info->biPlanes = 1;
info->biBitCount = bpp;
info->biCompression = BI_RGB;
info->biSizeImage = 0;
info->biXPelsPerMeter = 0;
info->biYPelsPerMeter = 0;
info->biClrUsed = 0;
info->biClrImportant = 0;
}
int bmpfont_run(void *bmpfont)
{
State *state = (State*)bmpfont;
if (!check_state(state))
return 0;
// Paint the font to a bitmap
BYTE *data;
BYTE **rows;
CharDetail *charDetails;
make_bmpfont(state, &data, &rows, &charDetails);
state->func.free(state->graphics);
// Generate metadatas
char *metadata = NULL;
size_t metadata_size = 0;
if (state->output_type == PACKED_RGBA || state->output_type == PACKED_GRAYSCALE)
{
// Maximum size, we may need less than that.
metadata = malloc(2 * sizeof(uint16_t) + 65536 * (sizeof(uint16_t) + sizeof(CharDetail)));
char *metadata_ptr = metadata;
// I don't know what is that, so I take the bytes in spell_font.bmp for now.
uint16_t unk = 0x0215;
memcpy(metadata_ptr, &unk, 2);
metadata_ptr += 2;
// Skip nb_chars for now - we will count them while we copy them.
uint16_t nb_chars = 0;
metadata_ptr += 2;
for (uint16_t c = 0; c < 65535; c++) {
if (state->chars_list[c]) {
memcpy(metadata_ptr, &c, 2);
metadata_ptr += 2;
nb_chars++;
}
}
for (uint16_t c = 0; c < 65535; c++) {
if (state->chars_list[c]) {
memcpy(metadata_ptr, &charDetails[c], sizeof(CharDetail));
metadata_ptr += sizeof(CharDetail);
}
}
// We now have nb_chars, write it.
memcpy(metadata + 2, &nb_chars, 2);
metadata_size = metadata_ptr - metadata;
}
// Generate the BMP file headers
BITMAPFILEHEADER header;
BITMAPINFOHEADER info;
fill_bmp_headers(state, &header, &info);
// Allocate the output buffer
size_t output_size = header.bfSize;
if (state->output_type == PACKED_RGBA || state->output_type == PACKED_GRAYSCALE)
output_size += metadata_size;
BYTE *output = malloc(output_size);
BYTE *output_ptr = output;
// Write the header
memcpy(output_ptr, &header, sizeof(header));
output_ptr += sizeof(header);
memcpy(output_ptr, &info, sizeof(info));
output_ptr += sizeof(info);
// Write the palette
if (state->output_type == UNPACKED_GRAYSCALE || state->output_type == PACKED_GRAYSCALE)
{
uint8_t c = 0;
do
{
output_ptr[0] = c;
output_ptr[1] = c;
output_ptr[2] = c;
output_ptr[3] = 0;
output_ptr += 4;
c++;
} while (c != 0);
}
// Write the bitmap
int line;
for (line = state->h - 1; line >= 0; line--)
{
memcpy(output_ptr, rows[line], state->w * info.biBitCount / 8);
output_ptr += state->w * info.biBitCount / 8;
}
// Write the metadatas
if (state->output_type == PACKED_RGBA || state->output_type == PACKED_GRAYSCALE)
{
memcpy(output_ptr, metadata, metadata_size);
output_ptr += metadata_size;
}
// Write the output buffer to the file
if (state->out_fn)
{
FILE *fout = fopen(state->out_fn, "wb");
if (!fout)
{
perror(state->out_fn);
return 1;
}
fwrite(output, output_size, 1, fout);
fclose(fout);
}
// PNG output for thcrap
if (state->output_type == PACKED_GRAYSCALE && state->export_png && state->out_fn)
{
char *path_png = (char*)malloc(strlen(state->out_fn) + 5);
strcpy(path_png, state->out_fn);
strcat(path_png, ".png");
export_to_PNG(rows, state->w, state->h, path_png);
free(path_png);
}
// Metadata output
if (state->output_type == PACKED_GRAYSCALE && state->out_fn)
{
// We'll store the metadatas in another file, because the output will probably be converted to PNG.
char *path_bin = (char*)malloc(strlen(state->out_fn) + 5);
strcpy(path_bin, state->out_fn);
strcat(path_bin, ".bin");
FILE *fout = fopen(path_bin, "wb");
if (fout == NULL)
perror(path_bin);
fwrite(metadata, metadata_size, 1, fout);
fclose(fout);
free(path_bin);
}
if (metadata)
free(metadata);
if (state->out_mem)
*state->out_mem = output;
else
free(output);
if (state->out_size)
*state->out_size = output_size;
free(charDetails);
free(rows);
free(data);
return 1;
}
void bmpfont_free(void *bmpfont)
{
if (!bmpfont)
return ;
State *state = (State*)bmpfont;
if (state->out_mem && *state->out_mem)
free(*state->out_mem);
free(state);
}