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
|
/** \file
* \brief Image Conversion
*
* See Copyright Notice in im_lib.h
* $Id: im_convertbitmap.cpp,v 1.4 2010/10/25 18:29:07 scuri Exp $
*/
#include "im.h"
#include "im_util.h"
#include "im_complex.h"
#include "im_image.h"
#include "im_convert.h"
#include "im_counter.h"
#include <stdlib.h>
#include <assert.h>
#include <memory.h>
int imConvertToBitmap(const imImage* src_image, imImage* dst_image, int cpx2real, float gamma, int abssolute, int cast_mode)
{
assert(src_image);
assert(dst_image);
if (!imImageMatchSize(src_image, dst_image) || !imImageIsBitmap(dst_image))
return IM_ERR_DATA;
int counter = imCounterBegin("Building Bitmap");
int ret;
if (src_image->data_type == IM_BYTE)
{
// NO data type conversion, only color mode conversion
ret = imConvertColorSpace(src_image, dst_image);
}
else
{
if (src_image->color_space == IM_RGB ||
src_image->color_space == IM_GRAY)
{
// data type conversion, but NO color mode conversion
ret = imConvertDataType(src_image, dst_image, cpx2real, gamma, abssolute, cast_mode);
}
else
{
// data type conversion AND color mode conversion
imImage* temp_image = imImageCreate(src_image->width, src_image->height, dst_image->color_space, src_image->data_type);
if (!temp_image)
ret = IM_ERR_MEM;
else
{
// first convert color_mode in the bigger precision
ret = imConvertColorSpace(src_image, temp_image);
if (ret == IM_ERR_NONE)
{
// second just convert data type
ret = imConvertDataType(temp_image, dst_image, cpx2real, gamma, abssolute, cast_mode);
}
imImageDestroy(temp_image);
}
}
}
imCounterEnd(counter);
return ret;
}
template <class T>
void iDoChangePacking(const T* src_data, T* dst_data, int width, int height, int src_depth, int dst_depth,
int src_is_packed)
{
int count = width*height;
if (src_is_packed)
{
for (int i = 0; i < count; i++)
{
for (int d = 0; d < dst_depth; d++)
{
*(dst_data + d*count) = *(src_data + d);
}
src_data += src_depth;
dst_data++;
}
}
else
{
for (int i = 0; i < count; i++)
{
for (int d = 0; d < src_depth; d++)
{
*(dst_data + d) = *(src_data + d*count);
}
dst_data += dst_depth;
src_data++;
}
}
}
void imConvertPacking(const void* src_data, void* dst_data, int width, int height, int src_depth, int dst_depth,
int data_type, int src_is_packed)
{
switch(data_type)
{
case IM_BYTE:
iDoChangePacking((const imbyte*)src_data, (imbyte*)dst_data, width, height, src_depth, dst_depth, src_is_packed);
break;
case IM_USHORT:
iDoChangePacking((const imushort*)src_data, (imushort*)dst_data, width, height, src_depth, dst_depth, src_is_packed);
break;
case IM_INT:
iDoChangePacking((const int*)src_data, (int*)dst_data, width, height, src_depth, dst_depth, src_is_packed);
break;
case IM_FLOAT:
iDoChangePacking((const float*)src_data, (float*)dst_data, width, height, src_depth, dst_depth, src_is_packed);
break;
case IM_CFLOAT:
iDoChangePacking((const imcfloat*)src_data, (imcfloat*)dst_data, width, height, src_depth, dst_depth, src_is_packed);
break;
}
}
static void iImageMakeGray(imbyte *map, int gldepth, int count)
{
for(int i = 0; i < count; i++)
{
if (*map)
*map = 255;
map += gldepth;
}
}
static void iImageGLCopyMapAlpha(imbyte *map, imbyte *gldata, int gldepth, int count)
{
/* gldata can be GL_RGBA or GL_LUMINANCE_ALPHA */
gldata += gldepth-1; /* position at first alpha */
for(int i = 0; i < count; i++)
{
*gldata = *map;
map++;
gldata += gldepth; /* skip to next alpha */
}
}
static void iImageGLSetTranspColor(imbyte *gldata, int count, imbyte r, imbyte g, imbyte b)
{
/* gldata is GL_RGBA */
for(int i = 0; i < count; i++)
{
if (*(gldata+0) == r &&
*(gldata+1) == g &&
*(gldata+2) == b)
*(gldata+3) = 0; /* transparent */
else
*(gldata+3) = 255; /* opaque */
gldata += 4;
}
}
static void iImageGLSetTranspMap(imbyte *map, imbyte *gldata, int count, imbyte *transp_map, int transp_count)
{
/* gldata is GL_RGBA */
gldata += 3; /* position at first alpha */
for(int i = 0; i < count; i++)
{
if (*map < transp_count)
*gldata = transp_map[*map];
else
*gldata = 255; /* opaque */
map++;
gldata += 4;
}
}
static void iImageGLSetTranspIndex(imbyte *map, imbyte *gldata, int gldepth, int count, imbyte index)
{
/* gldata can be GL_RGBA or GL_LUMINANCE_ALPHA */
gldata += gldepth-1; /* position at first alpha */
for(int i = 0; i < count; i++)
{
if (*map == index)
*gldata = 0; /* full transparent */
else
*gldata = 255; /* opaque */
map++;
gldata += gldepth; /* skip to next alpha */
}
}
/* To avoid including gl.h */
#define GL_RGB 0x1907
#define GL_RGBA 0x1908
#define GL_LUMINANCE 0x1909
#define GL_LUMINANCE_ALPHA 0x190A
imImage* imImageCreateFromOpenGLData(int width, int height, int glformat, const void* gldata)
{
int color_space, has_alpha, depth;
imImage* image;
switch(glformat)
{
case GL_RGBA:
color_space = IM_RGB;
has_alpha = 1;
depth = 4;
break;
case GL_RGB:
color_space = IM_RGB;
has_alpha = 0;
depth = 3;
break;
case GL_LUMINANCE_ALPHA:
color_space = IM_GRAY;
depth = 2;
has_alpha = 1;
default: /* GL_LUMINANCE */
color_space = IM_GRAY;
depth = 1;
has_alpha = 0;
break;
}
image = imImageCreate(width, height, color_space, IM_BYTE);
if (!image)
return NULL;
if (has_alpha)
imImageAddAlpha(image);
imConvertPacking(gldata, image->data[0], image->width, image->height, depth, depth, IM_BYTE, 1);
return image;
}
void* imImageGetOpenGLData(const imImage* image, int *format)
{
if (!imImageIsBitmap(image))
return NULL;
int transp_count;
imbyte* transp_index = (imbyte*)imImageGetAttribute(image, "TransparencyIndex", NULL, NULL);
imbyte* transp_map = (imbyte*)imImageGetAttribute(image, "TransparencyMap", NULL, &transp_count);
imbyte* transp_color = (imbyte*)imImageGetAttribute(image, "TransparencyColor", NULL, NULL);
int glformat;
switch(image->color_space)
{
case IM_MAP:
if (image->has_alpha || transp_map || transp_index)
glformat = GL_RGBA;
else
glformat = GL_RGB;
break;
case IM_RGB:
if (image->has_alpha || transp_color)
glformat = GL_RGBA;
else
glformat = GL_RGB;
break;
case IM_BINARY:
default: /* IM_GRAY */
if (image->has_alpha || transp_map || transp_index)
glformat = GL_LUMINANCE_ALPHA;
else
glformat = GL_LUMINANCE;
break;
}
int gldepth;
switch (glformat)
{
case GL_RGB:
gldepth = 3;
break;
case GL_RGBA:
gldepth = 4;
break;
case GL_LUMINANCE_ALPHA:
gldepth = 2;
break;
default: /* GL_LUMINANCE */
gldepth = 1;
break;
}
int size = image->count*gldepth;
imImageSetAttribute(image, "GLDATA", IM_BYTE, size, NULL);
imbyte* gldata = (imbyte*)imImageGetAttribute(image, "GLDATA", NULL, NULL);
int depth = image->depth;
if (image->has_alpha)
depth++;
/* copy data, including alpha */
if (image->color_space != IM_MAP)
{
imConvertPacking(image->data[0], gldata, image->width, image->height, depth, gldepth, IM_BYTE, 0);
if (image->color_space == IM_BINARY)
iImageMakeGray(gldata, gldepth, image->count);
}
else
{
/* copy map data */
memcpy(gldata, image->data[0], image->size); /* size does not include alpha */
/* expand MAP to RGB or RGBA */
imConvertMapToRGB(gldata, image->count, gldepth, 1, image->palette, image->palette_count);
if (image->has_alpha)
iImageGLCopyMapAlpha((imbyte*)image->data[1], gldata, gldepth, image->count); /* copy the alpha plane */
}
/* set alpha based on attributes */
if (!image->has_alpha)
{
if (image->color_space == IM_RGB)
{
if (transp_color)
iImageGLSetTranspColor(gldata, image->count, *(transp_color+0), *(transp_color+1), *(transp_color+2));
}
else
{
if (transp_map)
iImageGLSetTranspMap((imbyte*)image->data[0], gldata, image->count, transp_map, transp_count);
else if (transp_index)
iImageGLSetTranspIndex((imbyte*)image->data[0], gldata, gldepth, image->count, *transp_index);
}
}
if (format) *format = glformat;
return gldata;
}
|