-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageToolsBitmap_Blue.cpp
More file actions
359 lines (334 loc) · 10.3 KB
/
Copy pathImageToolsBitmap_Blue.cpp
File metadata and controls
359 lines (334 loc) · 10.3 KB
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
// Copyright © 2014 CCP ehf.
#include "StdAfx.h"
#include "ImageToolsBitmap.h"
#include "CompressionOptions.h"
BLUE_DEFINE( ImageToolsBitmap );
#if BLUE_WITH_PYTHON
PyObject* PyGetPixelData( PyObject* self, PyObject* args )
{
ImageToolsBitmap* pThis = BluePythonCast<ImageToolsBitmap*>( self );
#if PY_MAJOR_VERSION == 2
return PyString_FromStringAndSize( pThis->GetRawData(), pThis->GetRawDataSize() );
#else
return PyBytes_FromStringAndSize( pThis->GetRawData(), pThis->GetRawDataSize() );
#endif
}
#endif
const Be::ClassInfo* ImageToolsBitmap::ExposeToBlue()
{
EXPOSURE_BEGIN( ImageToolsBitmap, "" )
MAP_INTERFACE( IRoot )
MAP_INTERFACE( ImageToolsBitmap )
MAP_PROPERTY_READONLY( "type", GetType, "bitmap type (member of imagetools.BITMAP_TYPE)" );
MAP_PROPERTY_READONLY( "width", GetWidth, "bitmap width in pixels" );
MAP_PROPERTY_READONLY( "height", GetHeight, "bitmap height in pixels" );
MAP_PROPERTY_READONLY( "depth", GetDepth, "bitmap height in pixels (for volume bitmaps)" );
MAP_PROPERTY_READONLY( "mip_count", GetTrueMipCount, "number of mip levels" );
MAP_PROPERTY_READONLY( "format", GetFormat, "bitmap pixel format (member of imagetools.PIXEL_FORMAT)" );
MAP_PROPERTY_READONLY( "array_size", GetArraySize, "texture array size" );
MAP_METHOD_AND_WRAP(
"get_mip_width",
GetMipWidth,
"Returns mip level width\n"
"Arguments:\n"
"mip_index - mip level index" );
MAP_METHOD_AND_WRAP(
"get_mip_height",
GetMipHeight,
"Returns mip level height\n"
"Arguments:\n"
"mip_index - mip level index" );
MAP_METHOD_AND_WRAP(
"get_mip_depth",
GetMipDepth,
"Returns mip level depth for volume bitmaps\n"
"Arguments:\n"
"mip_index - mip level index" );
MAP_METHOD_AND_WRAP(
"save",
Save,
"Saves bitmap into a file\n"
"Arguments:\n"
"filename - path to file" );
MAP_METHOD_AND_WRAP( "downsample2x2", CheckedDownsample2x2, "Downsamples the bitmap by factor of 2" );
MAP_METHOD_AND_WRAP(
"crop",
CheckedCrop,
"Crops 2D bitmap\n"
"Arguments:\n"
"left - pixel offset to the left of the crop window\n"
"top - pixel offset to the top of the crop window\n"
"bottom - pixel offset to the bottom of the crop window\n"
"right - pixel offset to the right of the crop window" );
MAP_METHOD_AND_WRAP_OPTIONAL_ARGS(
"generate_mips",
CheckedGenerateMipMaps,
1,
"Generates mip levels for 2D bitmap\n"
"Arguments:\n"
"levels - optional amount of mip levels to generate" );
MAP_METHOD_AND_WRAP(
"drop_mips",
DropMipMaps,
"Removes all mips from the image. This function always returns true." );
MAP_METHOD_AND_WRAP(
"convert_format",
CheckedConvertFormat,
"Converts bitmap pixel format. Only some transitions are supported:\n"
"B8G8R8X8_UNORM -> B8G8R8A8_UNORM\n"
"R8_UNORM -> B8G8R8A8_UNORM\n"
"R8G8_UNORM -> B8G8R8A8_UNORM\n"
"Arguments:\n"
"format - new format" );
MAP_METHOD_AND_WRAP
(
"copy_channel",
CheckedCopyChannel,
"Copies a selected color channel from another bitmap to a color channel of this bitmap"
"Arguments:\n"
"srcChannel - the index of the color channel to copy\n"
"dstChannel - the index of the destination color channel which should be overwritten\n"
"source - the source bitmap from which to copy the channel from"
);
MAP_METHOD_AND_WRAP_OPTIONAL_ARGS(
"compress",
Compress,
1,
"Compresses the bitmap. Returns compressed bitmap.\n"
"Arguments:\n"
"options - (optional) compression options (imagetools.CompressionOptions)" );
MAP_METHOD_AND_WRAP(
"is_compressed",
IsCompressed,
"Returns True if the image uses a compressed format." );
MAP_METHOD_AND_WRAP(
"copy",
Copy,
"Returns a copy if this image." );
MAP_METHOD_AND_WRAP_OPTIONAL_ARGS(
"extract_mip_level",
ExtractMipLevel,
1,
"Returns a copy if this image mip level as a separate image.\n"
"Arguments:\n"
"mip_level - mip level index\n"
"count - optional number of mip levels to copy, defaults to one\n"
);
MAP_METHOD_AND_WRAP
(
"rotate_face_clockwise",
RotateFaceClockwise,
"Rotates a face clockwise a multiple of 90 degrees\n"
":param face: bitmap face (for cubemaps)\n"
":param times: number of times to rotate"
)
MAP_METHOD_AND_WRAP
(
"convert_crossmap_to_cubemap",
ConvertCrossmapToCubemap,
"Converts a 3x4 2D crossmap into a cubemap"
)
MAP_METHOD_AND_WRAP(
"flatten",
FlattenSlices,
"Returns a 2D image with all slices (cube faces) copied to it in a horizontal or vertical strip.\n"
"Arguments:\n"
"horizontally - copy slices in horizontal or vertical strip" );
MAP_METHOD_AND_WRAP(
"yuv",
ToYuv,
"Returns a (y, uv) tuple of YUV422 channels of the bitmap." );
MAP_METHOD_AND_WRAP_OPTIONAL_ARGS(
"set_mip_level_data",
SetMipData,
1,
"Copies mip pixels from source image to this image.\n"
"Arguments:\n"
"mip_level - destination mip level index\n"
"source - source image\n"
"source_mip - (optional) source image mip level, defaults to 0" );
MAP_METHOD_AND_WRAP_OPTIONAL_ARGS(
"compress_to_file",
CompressToFile,
1,
"Compresses the bitmap and writes compressed image into a file.\n"
"Arguments:\n"
"filename - path to output dds file\n"
"options - (optional) compression options (imagetools.CompressionOptions)" );
MAP_METHOD_AND_WRAP(
"get_metadata_strings",
GetMetadataStrings,
"Returns image metadata strings loaded from the file.\n"
"Result is a list of key value pairs of strings.\n"
":rtype: List[(str, str)]" )
MAP_METHOD_AND_WRAP(
"set_metadata_strings",
SetMetadataStrings,
"Assigns a new set of metadata text to the image.\n"
"Result is a list of key value pairs of strings.\n"
":param strings: list of key value pairs of strings. Keys do not have to be unique.\n"
":type strings: List[(str, str)]" )
MAP_METHOD_AND_WRAP(
"clear_metadata",
ClearMetadata,
"Clears all metadata from the image." )
#if BLUE_WITH_PYTHON
#if PY_MAJOR_VERSION == 2
const char* pixelDataDocString = "Returns pixel data of the bitmap as a string.\n\n:rtype: str"
#else
const char* pixelDataDocString = "Returns pixel data of the bitmap as a bytes object.\n\n:rtype: bytes";
#endif
MAP_METHOD(
"get_pixel_data",
PyGetPixelData,
pixelDataDocString
);
#endif
EXPOSURE_END()
}
namespace
{
StdOrImageIOResult LoadHostBitmap( const wchar_t* filename, ImageToolsBitmapPtr& result )
{
result.CreateInstance();
auto ret = result->Load( filename );
if( !BeIsSuccess( ret ) )
{
result = nullptr;
}
return ret;
}
StdOrImageIOResult CreateFromArray( const std::vector<ImageToolsBitmap*>& elements, ImageToolsBitmapPtr& result )
{
result.CreateInstance();
auto ret = result->CreateFromArray( elements );
if( !BeIsSuccess( ret ) )
{
result = nullptr;
}
return ret;
}
Be::Result<std::string> CreateBitmap2D(
uint32_t width,
uint32_t height,
uint32_t mipCount,
ImageIO::PixelFormat format,
ImageToolsBitmapPtr& result )
{
result.CreateInstance();
if( !result->Create( width, height, mipCount, format ) )
{
result = nullptr;
return Be::Result<std::string>( "error creating 2D bitmap" );
}
return std::string();
}
Be::Result<std::string> CreateBitmap2DFromString(
uint32_t width,
uint32_t height,
uint32_t mipCount,
ImageIO::PixelFormat format,
const char* pixelData,
ImageToolsBitmapPtr& result )
{
result.CreateInstance();
if( !result->Create( width, height, mipCount, format ) )
{
result = nullptr;
return Be::Result<std::string>( "error creating 2D bitmap" );
}
memcpy( result->GetRawData(), pixelData, result->GetRawDataSize() );
return std::string();
}
Be::Result<std::string> CreateBitmapCube(
uint32_t width,
uint32_t mipCount,
ImageIO::PixelFormat format,
ImageToolsBitmapPtr& result )
{
result.CreateInstance();
if( !result->CreateCube( width, mipCount, format ) )
{
result = nullptr;
return Be::Result<std::string>( "error creating cube bitmap" );
}
return std::string();
}
Be::Result<std::string> CreateBitmapVolume(
uint32_t width,
uint32_t height,
uint32_t depth,
uint32_t mipCount,
ImageIO::PixelFormat format,
ImageToolsBitmapPtr& result )
{
result.CreateInstance();
if( !result->CreateVolume( width, height, depth, mipCount, format ) )
{
result = nullptr;
return Be::Result<std::string>( "error creating volume bitmap" );
}
return std::string();
}
bool IsCudaAvailable()
{
nvtt::Compressor compressor;
return compressor.isCudaAccelerationEnabled();
}
}
MAP_FUNCTION_AND_WRAP(
"load",
LoadHostBitmap,
"Loads bitmap from file.\n"
"Arguments:\n"
"filename - path to image file" );
MAP_FUNCTION_AND_WRAP(
"create_from_array",
CreateFromArray,
"Creates an array bitmap from the given list of individual bitmaps. All bitmaps\n"
"in the list must be valid 2D bitmaps with the same sizes and formats.\n"
"Arguments:\n"
"elements - list of array element bitmaps" );
MAP_FUNCTION_AND_WRAP(
"create_2d",
CreateBitmap2D,
"Creates 2D bitmap.\n"
"Arguments:\n"
"width - bitmap width in pixels\n"
"height - bitmap height in pixels\n"
"mip_count - number of mip levels (pass 0 to create all)\n"
"format - bitmap pixel format (imagetools.PIXEL_FORMAT)" );
MAP_FUNCTION_AND_WRAP(
"create_2d_from_string",
CreateBitmap2DFromString,
"Creates 2D bitmap and initializes its pixel data with data\n"
"provided in a string.\n"
"Arguments:\n"
"width - bitmap width in pixels\n"
"height - bitmap height in pixels\n"
"mip_count - number of mip levels (pass 0 to create all)\n"
"format - bitmap pixel format (imagetools.PIXEL_FORMAT)\n"
"pixel_data - pixel data as a string\n" );
MAP_FUNCTION_AND_WRAP(
"create_cube",
CreateBitmapCube,
"Creates cube bitmap.\n"
"Arguments:\n"
"width - bitmap width/height in pixels\n"
"mip_count - number of mip levels (pass 0 to create all)\n"
"format - bitmap pixel format (imagetools.PIXEL_FORMAT)" );
MAP_FUNCTION_AND_WRAP(
"create_volume",
CreateBitmapVolume,
"Creates volume bitmap.\n"
"Arguments:\n"
"width - bitmap width in pixels\n"
"height - bitmap height in pixels\n"
"depth - bitmap depth in pixels\n"
"mip_count - number of mip levels (pass 0 to create all)\n"
"format - bitmap pixel format (imagetools.PIXEL_FORMAT)" );
MAP_FUNCTION_AND_WRAP(
"is_cuda_available",
IsCudaAvailable,
"Checks if CUDA is available in the system. DDS compression works\n"
"faster on systems with CUDA. Returns True if CUDA is available.\n" );