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
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger                                  |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/graphics/pixmapimages.h"
#include "gate/strings.h"
#include "gate/results.h"
#include "gate/maps.h"
#include "gate/comparers.h"
#include "gate/debugging.h"
#include "gate/arrays.h"

gate_result_t gate_pixmapimage_parse(char const* const* lines, gate_rasterimage_t* image,
    gate_int32_t* hotspot_x, gate_int32_t* hotspot_y)
{
    gate_int64_t width, height, colors, depth, hx, hy, x, y;
    gate_result_t ret;
    char const* lineptr;
    gate_size_t linelen;
    gate_size_t result;
    gate_size_t ndx;
    gate_size_t col_count;
    gate_map_t colormap = GATE_INIT_EMPTY;
    gate_string_t line;
    gate_size_t colmarkerlen;
    gate_size_t coldescroffset;
    gate_string_t colmarker;
    gate_string_t coldescriptor;
    gate_color_t color;
    gate_color_t* colorptr;
    gate_color_t const* currentcolor;
    static gate_color_t const transparent_color = { { 1, 1, 1, 0 } };

    GATE_UNUSED_ARG(hotspot_x);
    GATE_UNUSED_ARG(hotspot_y);

    do
    {
        if (lines == NULL)
        {
            ret = GATE_RESULT_NULLPOINTER;
            break;
        }
        lineptr = lines[0];
        linelen = gate_str_length(lineptr);

        if (0 == (result = gate_str_parse_int64(lineptr, linelen, &width))) { ret = GATE_RESULT_INVALIDHEADER; break; };
        lineptr += result; linelen -= result;

        if (0 == (result = gate_str_parse_int64(lineptr, linelen, &height))) { ret = GATE_RESULT_INVALIDHEADER; break; };
        lineptr += result; linelen -= result;

        if (0 == (result = gate_str_parse_int64(lineptr, linelen, &colors))) { ret = GATE_RESULT_INVALIDHEADER; break; };
        lineptr += result; linelen -= result;
        col_count = (gate_size_t)colors;

        if (0 == (result = gate_str_parse_int64(lineptr, linelen, &depth))) { ret = GATE_RESULT_INVALIDHEADER; break; };
        lineptr += result; linelen -= result;

        if (0 == (result = gate_str_parse_int64(lineptr, linelen, &hx))) { hx = 0; };
        lineptr += result; linelen -= result;

        if (0 == (result = gate_str_parse_int64(lineptr, linelen, &hy))) { hy = 0; };
        lineptr += result; linelen -= result;

        if (NULL == gate_map_create(&colormap, &gate_compare_string,
            sizeof(gate_string_t), &gate_string_copy_constructor, &gate_string_destructor,
            sizeof(gate_color_t), NULL, NULL))
        {
            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }

        ret = GATE_RESULT_OK;
        ++lines;

        colmarkerlen = (gate_size_t)depth;
        coldescroffset = colmarkerlen + 1;

        /* build the color map that connects all text-tokens to a parsed color value */
        for (ndx = 0; ndx != col_count; ++ndx, ++lines)
        {
            if (NULL == gate_string_create_static(&line, *lines))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
                break;
            }
            if (gate_string_length(&line) <= coldescroffset)
            {
                ret = GATE_RESULT_INVALIDDATA;
                break;
            }

            switch (line.str[coldescroffset])
            {
            case 'c':
            case 'g':
            {
                if (NULL == gate_string_substr(&colmarker, &line, 0, colmarkerlen))
                {
                    continue;
                }
                if (NULL == gate_string_substr(&coldescriptor, &line, coldescroffset + 2, GATE_STR_NPOS))
                {
                    gate_string_release(&colmarker);
                    continue;
                }

                if (gate_str_starts_with_ic(coldescriptor.str, coldescriptor.length, "none", 4))
                {
                    gate_map_add(&colormap, &colmarker, &transparent_color);
                }
                else
                {
                    if (gate_color_parse_hex(coldescriptor.str, coldescriptor.length, &color))
                    {
                        gate_map_add(&colormap, &colmarker, &color);
                    }
                }

                gate_string_release(&coldescriptor);
                gate_string_release(&colmarker);
                break;
            }
            }
            gate_string_release(&line);
        }

        if (GATE_FAILED(ret))
        {
            break;
        }

        if (NULL == gate_rasterimage_create(image, GATE_IMAGE_PIXELFORMAT_DEFAULT,
            (gate_uint32_t)width, (gate_uint32_t)height, NULL))
        {

            ret = GATE_RESULT_OUTOFMEMORY;
            break;
        }

        /* set pixels of image by finding the text token in the colormap */

        colorptr = (gate_color_t*)gate_rasterimage_get_line_ptr(image, 0);
        for (y = 0; y != height; ++y, ++lines)
        {
            if (NULL == gate_string_create_static(&line, *lines))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
                break;
            }

            GATE_DEBUG_ASSERT(line.length == (gate_size_t)(width * depth));

            for (x = 0, ndx = 0; x != width; ++x, ++colorptr)
            {

                if (NULL != gate_string_substr(&colmarker, &line, ndx, (gate_size_t)depth))
                {
                    currentcolor = (gate_color_t const*)gate_map_get_value(&colormap, &colmarker);
                    if (currentcolor != NULL)
                    {
                        *colorptr = *currentcolor;
                    }
                    gate_string_release(&colmarker);
                }
                ndx += (gate_size_t)depth;
            }

            gate_string_release(&line);
        }

    } while (0);

    gate_map_destroy(&colormap);

    return ret;
}

typedef enum pixmap_loader_state_enum
{
    pls_header,
    pls_info,
    pls_palette,
    pls_content,
    pls_end,
    pls_error,
} pixmap_loader_state_t;

typedef struct pixmap_info_class
{
    unsigned width;
    unsigned height;
    unsigned colors;
    unsigned bytes_per_pixel;
} pixmap_info_t;

static gate_string_t* decode_pixmap_line(gate_string_t* target, gate_string_t const* src)
{
    gate_size_t pos;
    if (!gate_string_starts_with_char(src, '\"'))
    {
        return NULL;
    }
    pos = gate_string_char_pos_last(src, '\"');
    if ((GATE_STR_NPOS == pos) || (0 == pos))
    {
        return NULL;
    }
    gate_string_substr(target, src, 1, pos - 1);
    return target;
}

static pixmap_info_t* parse_info(pixmap_info_t* info, gate_string_t const* content)
{
    static gate_string_t const sep = GATE_STRING_INIT_STATIC(" ");
    gate_string_t data = GATE_STRING_INIT_EMPTY;
    gate_string_t item = GATE_STRING_INIT_EMPTY;
    unsigned numbers[32] = { 0 };
    unsigned ndx = 0;
    gate_bool_t continue_parse = true;
    gate_size_t len_parsed;
    gate_uint64_t num;

    gate_string_duplicate(&data, content);
    while (continue_parse && (ndx < sizeof(numbers) / sizeof(numbers[0])))
    {
        len_parsed = gate_string_parse(&data, &sep, 0, &item, &data, false);
        if (len_parsed > 0)
        {
            if (0 != gate_string_parse_uint(&item, &num))
            {
                numbers[ndx] = (unsigned)num;
                ++ndx;
            }
        }
        else
        {
            if (0 != gate_string_parse_uint(&data, &num))
            {
                numbers[ndx] = (unsigned)num;
                ++ndx;
            }
            continue_parse = false;
        }
    }
    gate_string_release(&data);
    if (ndx >= 4)
    {
        info->width = numbers[0];
        info->height = numbers[1];
        info->colors = numbers[2];
        info->bytes_per_pixel = numbers[3];
        if ((info->width > 0) && (info->width <= 4096)
            && (info->height > 0) && (info->height <= 4096)
            && (info->colors > 0) && (info->colors <= 4096)
            && (info->bytes_per_pixel > 0) && (info->bytes_per_pixel <= 4)
            )
        {
            return info;
        }
    }
    return NULL;
}

typedef struct pixmap_color_class
{
    char			token[4];
    gate_color_t	col;
} pixmap_color_t;

static void parse_color_token(gate_color_t* col, char const* str, gate_size_t len)
{
    gate_mem_clear(col, sizeof(gate_color_t));
    if (str[0] == '#')
    {
        gate_str_parse_hex_byte(&str[1], &col->r);
        gate_str_parse_hex_byte(&str[3], &col->g);
        gate_str_parse_hex_byte(&str[5], &col->b);
        col->a = 255;
    }
}

static pixmap_color_t* parse_color(pixmap_color_t* col, pixmap_info_t const* info, gate_string_t const* data)
{
    char type_chr;
    gate_mem_clear(col, sizeof(pixmap_color_t));
    gate_mem_copy(&col->token[0], data->str, info->bytes_per_pixel);
    type_chr = data->str[info->bytes_per_pixel + 1];
    if ((type_chr == 'c') || (type_chr == 'g'))
    {
        parse_color_token(&col->col, &data->str[info->bytes_per_pixel + 3], data->length - info->bytes_per_pixel - 3);
    }
    return col;
}

static void parse_image_line(gate_color_t* ptr_pixels, pixmap_info_t const* info, gate_arraylist_t colors, gate_string_t const* data)
{
    gate_size_t len = gate_string_length(data) / info->bytes_per_pixel;
    gate_size_t ndx;
    pixmap_color_t const* ptr_colentries = (pixmap_color_t const*)gate_arraylist_get(colors, 0);
    pixmap_color_t const* ptr_curentry;
    gate_size_t colcount = gate_arraylist_length(colors);
    char const* ptr_curdata;
    unsigned x;
    if (len > info->width)
    {
        len = info->width;
    }
    ptr_curdata = data->str;
    for (x = 0; x != len; ++x, ++ptr_pixels)
    {
        ptr_curentry = ptr_colentries;
        for (ndx = 0; ndx != colcount; ++ndx, ++ptr_curentry)
        {
            if (0 == gate_mem_compare(ptr_curdata, ptr_curentry->token, info->bytes_per_pixel))
            {
                *ptr_pixels = ptr_curentry->col;
                break;
            }
        }
        ptr_curdata += info->bytes_per_pixel;
    }
}

gate_result_t gate_pixmapimage_load(gate_stream_t* srcstream, gate_rasterimage_t* image, gate_enumint_t flags)
{
    gate_result_t result = GATE_RESULT_FAILED;
    char linebuffer[4096];
    gate_size_t linelen;
    pixmap_loader_state_t state = pls_header;
    gate_string_t line = GATE_STRING_INIT_EMPTY;
    gate_string_t content = GATE_STRING_INIT_EMPTY;
    pixmap_info_t info = GATE_INIT_EMPTY;
    gate_arraylist_t colors = NULL;
    pixmap_color_t colmapping;
    gate_color_t* ptr_pixel;
    unsigned y = 0;
    GATE_UNUSED_ARG(flags);

    do
    {
        if (!srcstream || !image)
        {
            result = GATE_RESULT_INVALIDARG;
            break;
        }

        colors = gate_arraylist_create(sizeof(pixmap_color_t), NULL, 0, NULL, NULL);

        gate_mem_clear(image, sizeof(gate_rasterimage_t));

        while ((state != pls_end) && (state != pls_error))
        {
            result = gate_stream_read_line(srcstream, linebuffer, sizeof(linebuffer), &linelen);
            GATE_BREAK_IF_FAILED(result);
            if (linelen == 0)
            {
                break;
            }

            gate_string_create_static_len(&line, linebuffer, linelen);
            gate_string_trim(&line, &line);
            if (0 == gate_string_length(&line))
            {
                continue;
            }
            if (gate_string_starts_with_str(&line, "/*") || gate_string_ends_with_str(&line, "*/"))
            {
                continue;
            }

            switch (state)
            {
            case pls_header:
            {
                if (gate_string_ends_with_char(&line, '{'))
                {
                    /* header is skipped -> read info line */
                    state = pls_info;
                }
                break;
            }
            case pls_info:
            {
                if (NULL == decode_pixmap_line(&content, &line))
                {
                    result = GATE_RESULT_INVALIDINPUT;
                    state = pls_error;
                    break;
                }
                if (NULL == parse_info(&info, &content))
                {
                    result = GATE_RESULT_INVALIDINPUT;
                    state = pls_error;
                    break;
                }
                if (NULL == gate_rasterimage_create(image, GATE_IMAGE_PIXELFORMAT_RGBA, info.width, info.height, NULL))
                {
                    result = GATE_RESULT_OUTOFMEMORY;
                    state = pls_error;
                    break;
                }
                state = pls_palette;
                break;
            }
            case pls_palette:
            {
                if (NULL == decode_pixmap_line(&content, &line))
                {
                    result = GATE_RESULT_INVALIDINPUT;
                    state = pls_error;
                    break;
                }
                if (NULL == parse_color(&colmapping, &info, &content))
                {
                    result = GATE_RESULT_INVALIDINPUT;
                    state = pls_error;
                    break;
                }
                if (NULL == gate_arraylist_add(colors, &colmapping))
                {
                    result = GATE_RESULT_OUTOFMEMORY;
                    state = pls_error;
                    break;
                }
                if (gate_arraylist_length(colors) >= info.colors)
                {
                    state = pls_content;
                }
                break;
            }
            case pls_content:
            {
                if (NULL == decode_pixmap_line(&content, &line))
                {
                    result = GATE_RESULT_INVALIDINPUT;
                    state = pls_error;
                    break;
                }
                ptr_pixel = (gate_color_t*)gate_rasterimage_get_line_ptr(image, y);
                if (ptr_pixel)
                {
                    parse_image_line(ptr_pixel, &info, colors, &content);
                }
                ++y;
                if (y >= info.height)
                {
                    state = pls_end;
                }
                break;
            }
            case pls_end:
            case pls_error:
            {
                break;
            }
            }
            gate_string_release(&content);
        }

        GATE_BREAK_IF_FAILED(result);
        if (state != pls_end)
        {
            gate_rasterimage_release(image);
            result = GATE_RESULT_INVALIDSTATE;
        }
    } while (0);

    gate_arraylist_release(colors);

    return result;
}

static void copy_rgba(gate_uint8_t* dst, gate_uint8_t const* src, gate_size_t len)
{
    gate_mem_copy(dst, src, len * 4);
}

static gate_string_t const pix_chars = GATE_STRING_INIT_STATIC(" _.,:;~#=+-*/|^!$%&()[]{}<>@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");

static void print_color(gate_stream_t* strm, gate_uint32_t col, gate_uint16_t n)
{
    char const* ptr = gate_string_ptr(&pix_chars, 0);
    const gate_size_t ccount = gate_string_length(&pix_chars);
    gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)n / (gate_size_t)ccount);
    gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)n % (gate_size_t)ccount);
    gate_color_t c;
    c.rgba = col;

    if (ndx1 >= ccount)
    {
        ndx1 = 0;
    }

    gate_stream_print(strm,
        GATE_PRINT_CSTR, "\"",
        GATE_PRINT_CHAR, ptr[ndx1],
        GATE_PRINT_CHAR, ptr[ndx2],
        GATE_PRINT_CSTR, " g ",
        GATE_PRINT_END);

    if (c.a < 128)
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "None",
            GATE_PRINT_END);
    }
    else
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "#",
            GATE_PRINT_H8, c.r,
            GATE_PRINT_H8, c.g,
            GATE_PRINT_H8, c.b,
            GATE_PRINT_END);
    }

    gate_stream_print(strm,
        GATE_PRINT_CSTR, "\",\n",
        GATE_PRINT_END);
}

gate_result_t gate_pixmapimage_save(gate_rasterimage_t const* image, gate_stream_t* deststream, gate_enumint_t flags)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_map_t col_map = GATE_INIT_EMPTY;
    char const* ptr = gate_string_ptr(&pix_chars, 0);
    const gate_size_t ccount = gate_string_length(&pix_chars);
    gate_uint16_t ncol = 0;
    unsigned y, x;
    gate_uint8_t const* ptr_image_line;
    gate_color_converter_t line_writer = NULL;
    gate_uint8_t buffer[4096];
    gate_uint8_t* ptr_buffer = &buffer[0];
    gate_color_t const* ptr_pixel;
    gate_uint16_t col_count;
    gate_color_t c;
    gate_map_iterator_t col_iter;

    do
    {
        switch (image->pixel_format)
        {
        case GATE_IMAGE_PIXELFORMAT_RGBA:
        case GATE_IMAGE_PIXELFORMAT_RGB32:	line_writer = &copy_rgba; break;
        case GATE_IMAGE_PIXELFORMAT_BGRA:
        case GATE_IMAGE_PIXELFORMAT_BGR32:	line_writer = &gate_color_convert_rgba_from_bgra_32; break;

        case GATE_IMAGE_PIXELFORMAT_RGB24:	line_writer = &gate_color_convert_rgba_from_rgb_24; break;
        case GATE_IMAGE_PIXELFORMAT_BGR24:	line_writer = &gate_color_convert_rgba_from_bgr_24; break;

        case GATE_IMAGE_PIXELFORMAT_RGB555:	line_writer = &gate_color_convert_rgba_from_rgb_15; break;
        case GATE_IMAGE_PIXELFORMAT_RGB565:	line_writer = &gate_color_convert_rgba_from_rgb_16; break;
        case GATE_IMAGE_PIXELFORMAT_ARGB4:	line_writer = &gate_color_convert_rgba_from_argb_16; break;
        case GATE_IMAGE_PIXELFORMAT_YUV2:	line_writer = &gate_color_convert_rgba_from_yuv2_16; break;

        case GATE_IMAGE_PIXELFORMAT_PAL8:	line_writer = &gate_color_convert_rgba_from_pal_8; break;
        case GATE_IMAGE_PIXELFORMAT_GRAY8:	line_writer = &gate_color_convert_rgba_from_gray_8; break;
        default:
            break;
        }

        if (line_writer == NULL)
        {
            ret = GATE_RESULT_NOTSUPPORTED;
            break;
        }

        c.rgba = 0;
        gate_map_create(&col_map, gate_compare_uint32, sizeof(gate_uint32_t), NULL, NULL, sizeof(gate_uint16_t), NULL, NULL);
        gate_map_add(&col_map, &c.rgba, &ncol);
        ++ncol;

        for (y = 0; y != image->height; ++y)
        {
            ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y);
            line_writer(ptr_buffer, ptr_image_line, image->width);
            ptr_pixel = (gate_color_t const*)ptr_buffer;
            for (x = 0; x != image->width; ++x)
            {
                if (NULL == gate_map_get(&col_map, &ptr_pixel[x].rgba))
                {
                    gate_map_add(&col_map, &ptr_pixel[x].rgba, &ncol);
                    ++ncol;
                }
            }
        }

        col_count = (gate_uint16_t)gate_map_count(&col_map);

        gate_stream_print(deststream,
            GATE_PRINT_CSTR, "/* XPM */\nstatic char const * gate_icon_brick_xpm[] = {\n\"",
            GATE_PRINT_UI16, (gate_uint16_t)image->width,
            GATE_PRINT_CSTR, " ",
            GATE_PRINT_UI16, (gate_uint16_t)image->height,
            GATE_PRINT_CSTR, " ",
            GATE_PRINT_UI16, (gate_uint16_t)col_count,
            GATE_PRINT_CSTR, " ",
            GATE_PRINT_UI16, (gate_uint16_t)2,
            GATE_PRINT_CSTR, "\",\n",
            GATE_PRINT_END
        );

        col_iter = gate_map_first(&col_map);
        while (col_iter)
        {
            print_color(deststream,
                *(gate_uint32_t const*)gate_map_iterator_key(col_iter),
                *(gate_uint16_t const*)gate_map_iterator_value(col_iter));
            col_iter = gate_map_iterator_next(col_iter);
        }

        for (y = 0; y != image->height; ++y)
        {
            ptr_image_line = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(image, y);
            line_writer(ptr_buffer, ptr_image_line, image->width);
            ptr_pixel = (gate_color_t const*)ptr_buffer;
            gate_stream_print(deststream, GATE_PRINT_CSTR, "\"", GATE_PRINT_END);
            for (x = 0; x != image->width; ++x)
            {
                gate_uint16_t const* ptr_n = gate_map_get_value(&col_map, &ptr_pixel[x].rgba);
                if (ptr_n == NULL)
                {
                    gate_stream_print(deststream, GATE_PRINT_CSTR, "  ", GATE_PRINT_END);
                }
                else
                {
                    gate_uint16_t ndx1 = (gate_uint16_t)((gate_size_t)*ptr_n / (gate_size_t)ccount);
                    gate_uint16_t ndx2 = (gate_uint16_t)((gate_size_t)*ptr_n % (gate_size_t)ccount);
                    if (ndx1 >= ccount)
                    {
                        ndx1 = 0;
                    }
                    gate_stream_print(deststream,
                        GATE_PRINT_CHAR, ptr[ndx1],
                        GATE_PRINT_CHAR, ptr[ndx2],
                        GATE_PRINT_END);
                }
            }
            gate_stream_print(deststream, GATE_PRINT_CSTR, "\",\n", GATE_PRINT_END);
        }
        gate_stream_print(deststream,
            GATE_PRINT_CSTR, "};\n",
            GATE_PRINT_END
        );

        ret = GATE_RESULT_OK;

    } while (0);

    gate_map_destroy(&col_map);
    return ret;
}