summaryrefslogtreecommitdiffstats
path: root/src/trace/imagemap-gdk.cpp
blob: 10fe27e3afaead01bbef19c2fe3dbaedb8b387d9 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * TODO: insert short description here
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2018 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
#include <cstdlib>

#include "imagemap-gdk.h"


/*#########################################################################
## G R A Y M A P
#########################################################################*/

GrayMap *gdkPixbufToGrayMap(GdkPixbuf *buf)
{
    if (!buf)
        return nullptr;

    int width       = gdk_pixbuf_get_width(buf);
    int height      = gdk_pixbuf_get_height(buf);
    guchar *pixdata = gdk_pixbuf_get_pixels(buf);
    int rowstride   = gdk_pixbuf_get_rowstride(buf);
    int n_channels  = gdk_pixbuf_get_n_channels(buf);

    GrayMap *grayMap = GrayMapCreate(width, height);
    if (!grayMap)
        return nullptr;

    //### Fill in the odd cells with RGB values
    int x,y;
    int row  = 0;
    for (y=0 ; y<height ; y++)
        {
        guchar *p = pixdata + row;
        for (x=0 ; x<width ; x++)
            {
            int alpha = (int)p[3];
            int white = 3 * (255-alpha);
            unsigned long sample = (int)p[0] + (int)p[1] +(int)p[2];
            unsigned long bright = sample * alpha / 256 + white;
            grayMap->setPixel(grayMap, x, y, bright);
            p += n_channels;
            }
        row += rowstride;
        }

    return grayMap;
}

GdkPixbuf *grayMapToGdkPixbuf(GrayMap *grayMap)
{
    if (!grayMap)
        return nullptr;

    guchar *pixdata = (guchar *)
          malloc(sizeof(guchar) * grayMap->width * grayMap->height * 3);
    if (!pixdata)
        {
        g_warning("grayMapToGdkPixbuf: can not allocate memory for conversion.");
        return nullptr;
        }

    int n_channels = 3;
    int rowstride  = grayMap->width * 3;

    GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
                        0, 8, grayMap->width, grayMap->height,
                        rowstride, (GdkPixbufDestroyNotify)g_free, nullptr);

    //### Fill in the odd cells with RGB values
    int x,y;
    int row  = 0;
    for (y=0 ; y<grayMap->height ; y++)
        {
        guchar *p = pixdata + row;
        for (x=0 ; x<grayMap->width ; x++)
            {
            unsigned long pix = grayMap->getPixel(grayMap, x, y) / 3;
            p[0] = p[1] = p[2] = (guchar)(pix & 0xff);
            p += n_channels;
            }
        row += rowstride;
        }

    return buf;
}


/*#########################################################################
## R G B   M A P
#########################################################################*/

RgbMap *gdkPixbufToRgbMap(GdkPixbuf *buf)
{
    if (!buf)
        return nullptr;

    int width       = gdk_pixbuf_get_width(buf);
    int height      = gdk_pixbuf_get_height(buf);
    guchar *pixdata = gdk_pixbuf_get_pixels(buf);
    int rowstride   = gdk_pixbuf_get_rowstride(buf);
    int n_channels  = gdk_pixbuf_get_n_channels(buf);

    RgbMap *rgbMap = RgbMapCreate(width, height);
    if (!rgbMap)
        return nullptr;

    //### Fill in the cells with RGB values
    int x,y;
    int row  = 0;
    for (y=0 ; y<height ; y++)
        {
        guchar *p = pixdata + row;
        for (x=0 ; x<width ; x++)
            {
            int alpha = (int)p[3];
            int white = 255 - alpha;
            int r     = (int)p[0];  r = r * alpha / 256 + white;
            int g     = (int)p[1];  g = g * alpha / 256 + white;
            int b     = (int)p[2];  b = b * alpha / 256 + white;

            rgbMap->setPixel(rgbMap, x, y, r, g, b);
            p += n_channels;
            }
        row += rowstride;
        }

    return rgbMap;
}



/*#########################################################################
## I N D E X E D   M A P
#########################################################################*/


GdkPixbuf *indexedMapToGdkPixbuf(IndexedMap *iMap)
{
    if (!iMap)
        return nullptr;

    guchar *pixdata = (guchar *)
          malloc(sizeof(guchar) * iMap->width * iMap->height * 3);
    if (!pixdata)
        {
        g_warning("indexedMapToGdkPixbuf: can not allocate memory for conversion.");
        return nullptr;
        }

    int n_channels = 3;
    int rowstride  = iMap->width * 3;

    GdkPixbuf *buf = gdk_pixbuf_new_from_data(pixdata, GDK_COLORSPACE_RGB,
                        0, 8, iMap->width, iMap->height,
                        rowstride, (GdkPixbufDestroyNotify)g_free, nullptr);

    //### Fill in the cells with RGB values
    int x,y;
    int row  = 0;
    for (y=0 ; y<iMap->height ; y++)
        {
        guchar *p = pixdata + row;
        for (x=0 ; x<iMap->width ; x++)
            {
            RGB rgb = iMap->getPixelValue(iMap, x, y);
            p[0] = rgb.r & 0xff;
            p[1] = rgb.g & 0xff;
            p[2] = rgb.b & 0xff;
            p += n_channels;
            }
        row += rowstride;
        }

    return buf;
}

/*#########################################################################
## E N D    O F    F I L E
#########################################################################*/