summaryrefslogtreecommitdiff
path: root/modules/graphic_objects/src/cpp/MatPlotDecomposer.cpp
blob: 2d4e4ad2cf28e326e9defea3e5d5211dec1c294c (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
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
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2011-2012 - DIGITEO - Pierre Lando
 *  Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#include "DecompositionUtils.hxx"
#include "MatPlotDecomposer.hxx"
#include "DataProperties.hxx"
#include "ColorComputer.hxx"
#include <iostream>
#include <cstring>

extern "C" {
#include "getGraphicObjectProperty.h"
#include "setGraphicObjectProperty.h"
#include "graphicObjectProperties.h"
#include "Matplot.h"
}

int MatPlotDecomposer::getTextureWidth(int id)
{
    int width = 0;
    int* piWidth = &width;
    getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_X__, jni_int, (void**) &piWidth);

    return width - 1;
}


int MatPlotDecomposer::getTextureHeight(int id)
{
    int height = 0;
    int* piHeight = &height;
    getGraphicObjectProperty(id, __GO_DATA_MODEL_NUM_Y__, jni_int, (void**) &piHeight);

    return height - 1;
}


int MatPlotDecomposer::fillTextureData(int id, unsigned char* buffer, int bufferLength)
{
    // Indexed colors
    void * data = NULL;
    int parentFigure = 0;
    int * piParentFigure = &parentFigure;
    double * colormap = NULL;
    int colormapSize = 0;
    int * piColormapSize = &colormapSize;
    int datatype = -1;
    int * pidataType = &datatype;
    const int h = getTextureHeight(id);
    const int w = getTextureWidth(id);

    getGraphicObjectProperty(id, __GO_DATA_MODEL_Z__, jni_double_vector, &data);
    getGraphicObjectProperty(id, __GO_DATA_MODEL_MATPLOT_DATA_TYPE__, jni_int, (void**) &pidataType);
    getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_int, (void**) &piParentFigure);
    getGraphicObjectProperty(parentFigure, __GO_COLORMAP__, jni_double_vector, (void**) &colormap);
    getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);

    switch ((DataType)datatype)
    {
        case MATPLOT_HM1_Char :
        case MATPLOT_Char :
        {
            char * index = (char *)data;
            for (int i = 0 ; i < w * h; i++)
            {
                ColorComputer::getDirectByteColor(index[i] - 1, colormap, colormapSize, &buffer[4 * i], false);
            }

            break;
        }
        case MATPLOT_HM1_UChar :
        case MATPLOT_UChar :
        {
            unsigned char * index = (unsigned char *)data;
            for (int i = 0 ; i < w * h; i++)
            {
                ColorComputer::getDirectByteColor(index[i] - 1, colormap, colormapSize, &buffer[4 * i], false);
            }

            break;
        }
        case MATPLOT_Int :
        {
            int * index = (int *)data;
            for (int i = 0 ; i < w * h; i++)
            {
                ColorComputer::getDirectByteColor(index[i] - 1, colormap, colormapSize, &buffer[4 * i], false);
            }

            break;
        }
        case MATPLOT_UInt :
        {
            unsigned int * index = (unsigned int *)data;
            for (int i = 0 ; i < w * h; i++)
            {
                ColorComputer::getDirectByteColor(index[i] - 1, colormap, colormapSize, &buffer[4 * i], false);
            }

            break;
        }
        case MATPLOT_Short :
        {
            short * index = (short *)data;
            for (int i = 0 ; i < w * h; i++)
            {
                ColorComputer::getDirectByteColor(index[i] - 1, colormap, colormapSize, &buffer[4 * i], false);
            }

            break;
        }
        case MATPLOT_UShort :
        {
            unsigned short * index = (unsigned short *)data;
            for (int i = 0 ; i < w * h; i++)
            {
                ColorComputer::getDirectByteColor(index[i] - 1, colormap, colormapSize, &buffer[4 * i], false);
            }

            break;
        }
        case MATPLOT_HM1_Double :
        case MATPLOT_Double :
        {
            double * index = (double *)data;
            for (int i = 0 ; i < w * h ; i++)
            {
                ColorComputer::getDirectByteColor(index[i] - 1, colormap, colormapSize, &buffer[4 * i], false);
            }
            break;
        }
    }

    releaseGraphicObjectProperty(__GO_COLORMAP__, colormap, jni_double_vector, colormapSize);

    return bufferLength;
}


int MatPlotDecomposer::fillTextureData(int id, unsigned char* buffer, int bufferLength, int x, int y, int width, int height)
{
    double* value = NULL;
    getGraphicObjectProperty(id, __GO_DATA_MODEL_Z__, jni_double_vector, (void**) &value);
    if (width * height * 4 == bufferLength)
    {
        int parentFigure = 0;
        int * piParentFigure = &parentFigure;
        double* colormap = NULL;
        int colormapSize = 0;
        int* piColormapSize = &colormapSize;
        getGraphicObjectProperty(id, __GO_PARENT_FIGURE__, jni_int, (void**) &piParentFigure);
        getGraphicObjectProperty(parentFigure, __GO_COLORMAP__, jni_double_vector, (void**) &colormap);
        getGraphicObjectProperty(parentFigure, __GO_COLORMAP_SIZE__, jni_int, (void**) &piColormapSize);
        int textureHeight = getTextureHeight(id);
        int k = 0;
        for (int j = y ; j < y + height ; j++)
        {
            for (int i = x ; i < x + width ; i++)
            {
                ColorComputer::getDirectByteColor(value[j + i * textureHeight] - 1, colormap, colormapSize, &buffer[k]);
                buffer[k + 3] = 255;
                k += 4;
            }
        }

        releaseGraphicObjectProperty(__GO_COLORMAP__, colormap, jni_double_vector, colormapSize);

        return bufferLength;
    }
    else
    {
        return 0;
    }
}

int MatPlotDecomposer::getTextureImageType(int id)
{
    int type = -1;
    int * piType = &type;
    getGraphicObjectProperty(id, __GO_DATA_MODEL_MATPLOT_IMAGE_TYPE__, jni_int, (void**) &piType);

    return type;
}

int MatPlotDecomposer::getTextureDataType(int id)
{
    int type = -1;
    int * piType = &type;
    getGraphicObjectProperty(id, __GO_DATA_MODEL_MATPLOT_DATA_TYPE__, jni_int, (void**) &piType);

    return type;
}

int MatPlotDecomposer::getTextureGLType(int id)
{
    int type = -1;
    int * piType = &type;
    getGraphicObjectProperty(id, __GO_DATA_MODEL_MATPLOT_GL_TYPE__, jni_int, (void**) &piType);

    return type;
}

int MatPlotDecomposer::getTextureData(int id, void ** address, unsigned int * size)
{
    int type = getTextureImageType(id);
    if (type == MATPLOT_INDEX)
    {
        // Indexed colors
        const int h = getTextureHeight(id);
        const int w = getTextureWidth(id);
        const int bsize = w * h * sizeof(int);
        unsigned char * buffer = new unsigned char[bsize];
        fillTextureData(id, buffer, bsize);

        *address = buffer;
        *size = bsize;

        return 1;
    }

    getGraphicObjectProperty(id, __GO_DATA_MODEL_MATPLOT_IMAGE_DATA__, jni_double_vector, address);
    getGraphicObjectProperty(id, __GO_DATA_MODEL_MATPLOT_IMAGE_DATASIZE__, jni_int, (void **)&size);

    return 1;
}

void MatPlotDecomposer::disposeTextureData(int id, unsigned char * buffer)
{
    if (buffer)
    {
        delete[] buffer;
    }
}

int MatPlotDecomposer::isTextureRowOrder(int id)
{
    int rowOrder;
    int * piRowOrder = &rowOrder;
    getGraphicObjectProperty(id, __GO_DATA_MODEL_MATPLOT_DATA_ORDER__, jni_int, (void**) &piRowOrder);

    return rowOrder;
}

int buildMatplotType(DataType datatype, DataOrder dataorder, ImageType imagetype)
{
    return ((int)datatype) | ((int)dataorder << 8) | ((int)imagetype << 16);
}