/
bimg.h
603 lines (535 loc) · 10.6 KB
/
bimg.h
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
/*
* Copyright 2011-2017 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#ifndef BIMG_IMAGE_H_HEADER_GUARD
#define BIMG_IMAGE_H_HEADER_GUARD
#include <stdint.h> // uint32_t
#include <stdlib.h> // NULL
#define BIMG_API_VERSION UINT32_C(5)
namespace bx
{
struct AllocatorI;
class Error;
struct ReaderSeekerI;
struct WriterI;
} // namespace bx
namespace bimg
{
typedef void (*PackFn)(void*, const float*);
typedef void (*UnpackFn)(float*, const void*);
/// Texture format enum.
///
/// Notation:
///
/// RGBA16S
/// ^ ^ ^
/// | | +-- [ ]Unorm
/// | | [F]loat
/// | | [S]norm
/// | | [I]nt
/// | | [U]int
/// | +---- Number of bits per component
/// +-------- Components
///
/// @attention Availability depends on Caps (see: formats).
///
/// @attention C99 equivalent is `bgfx_texture_format_t`.
///
struct TextureFormat
{
/// Texture formats:
enum Enum
{
BC1, //!< DXT1
BC2, //!< DXT3
BC3, //!< DXT5
BC4, //!< LATC1/ATI1
BC5, //!< LATC2/ATI2
BC6H, //!< BC6H
BC7, //!< BC7
ETC1, //!< ETC1 RGB8
ETC2, //!< ETC2 RGB8
ETC2A, //!< ETC2 RGBA8
ETC2A1, //!< ETC2 RGB8A1
PTC12, //!< PVRTC1 RGB 2BPP
PTC14, //!< PVRTC1 RGB 4BPP
PTC12A, //!< PVRTC1 RGBA 2BPP
PTC14A, //!< PVRTC1 RGBA 4BPP
PTC22, //!< PVRTC2 RGBA 2BPP
PTC24, //!< PVRTC2 RGBA 4BPP
Unknown, // Compressed formats above.
R1,
A8,
R8,
R8I,
R8U,
R8S,
R16,
R16I,
R16U,
R16F,
R16S,
R32I,
R32U,
R32F,
RG8,
RG8I,
RG8U,
RG8S,
RG16,
RG16I,
RG16U,
RG16F,
RG16S,
RG32I,
RG32U,
RG32F,
RGB8,
RGB8I,
RGB8U,
RGB8S,
RGB9E5F,
BGRA8,
RGBA8,
RGBA8I,
RGBA8U,
RGBA8S,
RGBA16,
RGBA16I,
RGBA16U,
RGBA16F,
RGBA16S,
RGBA32I,
RGBA32U,
RGBA32F,
R5G6B5,
RGBA4,
RGB5A1,
RGB10A2,
RG11B10F,
UnknownDepth, // Depth formats below.
D16,
D24,
D24S8,
D32,
D16F,
D24F,
D32F,
D0S8,
Count
};
};
///
struct Orientation
{
///
enum Enum
{
R0,
R90,
R180,
R270,
HFlip,
HFlipR90,
HFlipR270,
VFlip,
};
};
/// Texture info.
///
/// @attention C99 equivalent is `bgfx_texture_info_t`.
///
struct TextureInfo
{
TextureFormat::Enum format; //!< Texture format.
uint32_t storageSize; //!< Total amount of bytes required to store texture.
uint16_t width; //!< Texture width.
uint16_t height; //!< Texture height.
uint16_t depth; //!< Texture depth.
uint16_t numLayers; //!< Number of layers in texture array.
uint8_t numMips; //!< Number of MIP maps.
uint8_t bitsPerPixel; //!< Format bits per pixel.
bool cubeMap; //!< Texture is cubemap.
};
struct ImageContainer
{
bx::AllocatorI* m_allocator;
void* m_data;
TextureFormat::Enum m_format;
Orientation::Enum m_orientation;
uint32_t m_size;
uint32_t m_offset;
uint32_t m_width;
uint32_t m_height;
uint32_t m_depth;
uint16_t m_numLayers;
uint8_t m_numMips;
bool m_hasAlpha;
bool m_cubeMap;
bool m_ktx;
bool m_ktxLE;
bool m_srgb;
};
struct ImageMip
{
TextureFormat::Enum m_format;
uint32_t m_width;
uint32_t m_height;
uint32_t m_depth;
uint32_t m_blockSize;
uint32_t m_size;
uint8_t m_bpp;
bool m_hasAlpha;
const uint8_t* m_data;
};
struct ImageBlockInfo
{
uint8_t bitsPerPixel;
uint8_t blockWidth;
uint8_t blockHeight;
uint8_t blockSize;
uint8_t minBlockX;
uint8_t minBlockY;
uint8_t depthBits;
uint8_t stencilBits;
uint8_t rBits;
uint8_t gBits;
uint8_t bBits;
uint8_t aBits;
uint8_t encoding;
};
/// Returns true if texture format is compressed.
bool isCompressed(TextureFormat::Enum _format);
/// Returns true if texture format is uncompressed.
bool isColor(TextureFormat::Enum _format);
/// Returns true if texture format is depth.
bool isDepth(TextureFormat::Enum _format);
/// Returns true if texture format is valid.
bool isValid(TextureFormat::Enum _format);
/// returns true if texture format encoding is float.
bool isFloat(TextureFormat::Enum _format);
/// Returns bits per pixel.
uint8_t getBitsPerPixel(TextureFormat::Enum _format);
/// Returns texture block info.
const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format);
/// Converts format to string.
const char* getName(TextureFormat::Enum _format);
/// Converts string to format.
TextureFormat::Enum getFormat(const char* _name);
/// Returns number of mip-maps required for complete mip-map chain.
uint8_t imageGetNumMips(
TextureFormat::Enum _format
, uint16_t _width
, uint16_t _height
, uint16_t _depth = 0
);
/// Returns image size.
uint32_t imageGetSize(
TextureInfo* _info
, uint16_t _width
, uint16_t _height
, uint16_t _depth
, bool _cubeMap
, bool _hasMips
, uint16_t _numLayers
, TextureFormat::Enum _format
);
///
void imageSolid(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _solid
);
///
void imageCheckerboard(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _step
, uint32_t _0
, uint32_t _1
);
///
void imageRgba8Downsample2x2(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint32_t _srcPitch
, const void* _src
);
///
void imageRgba32fToLinear(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint32_t _srcPitch
, const void* _src
);
///
void imageRgba32fToGamma(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint32_t _srcPitch
, const void* _src
);
///
void imageRgba32fLinearDownsample2x2(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint32_t _srcPitch
, const void* _src
);
///
void imageRgba32fDownsample2x2NormalMap(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _srcPitch
, const void* _src
);
///
void imageSwizzleBgra8(
void* _dst
, uint32_t _dstPitch
, uint32_t _width
, uint32_t _height
, const void* _src
, uint32_t _srcPitch
);
///
void imageCopy(
void* _dst
, uint32_t _height
, uint32_t _srcPitch
, const void* _src
, uint32_t _dstPitch
);
///
void imageCopy(
void* _dst
, uint32_t _width
, uint32_t _height
, uint32_t _bpp
, uint32_t _srcPitch
, const void* _src
);
///
PackFn getPack(TextureFormat::Enum _format);
///
UnpackFn getUnpack(TextureFormat::Enum _format);
///
bool imageConvert(
TextureFormat::Enum _dstFormat
, TextureFormat::Enum _srcFormat
);
///
void imageConvert(
void* _dst
, uint32_t _bpp
, PackFn _pack
, const void* _src
, UnpackFn _unpack
, uint32_t _size
);
///
void imageConvert(
void* _dst
, uint32_t _dstBpp
, PackFn _pack
, const void* _src
, uint32_t _srcBpp
, UnpackFn _unpack
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint32_t _srcPitch
);
///
bool imageConvert(
void* _dst
, TextureFormat::Enum _dstFormat
, const void* _src
, TextureFormat::Enum _srcFormat
, uint32_t _width
, uint32_t _height
, uint32_t _depth
);
///
ImageContainer* imageConvert(
bx::AllocatorI* _allocator
, TextureFormat::Enum _dstFormat
, const void* _src
, uint32_t _size
);
///
ImageContainer* imageConvert(
bx::AllocatorI* _allocator
, TextureFormat::Enum _dstFormat
, const ImageContainer& _input
);
///
ImageContainer* imageAlloc(
bx::AllocatorI* _allocator
, TextureFormat::Enum _format
, uint16_t _width
, uint16_t _height
, uint16_t _depth
, uint16_t _numLayers
, bool _cubeMap
, bool _hasMips
, const void* _data = NULL
);
///
void imageFree(
ImageContainer* _imageContainer
);
///
int32_t imageWriteTga(
bx::WriterI* _writer
, uint32_t _width
, uint32_t _height
, uint32_t _srcPitch
, const void* _src
, bool _grayscale
, bool _yflip
, bx::Error* _err = NULL
);
///
int32_t imageWritePng(
bx::WriterI* _writer
, uint32_t _width
, uint32_t _height
, uint32_t _srcPitch
, const void* _src
, bool _grayscale
, bool _yflip
, bx::Error* _err = NULL
);
///
int32_t imageWriteDds(
bx::WriterI* _writer
, ImageContainer& _imageContainer
, const void* _data
, uint32_t _size
, bx::Error* _err
);
///
int32_t imageWriteKtx(
bx::WriterI* _writer
, TextureFormat::Enum _format
, bool _cubeMap
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint8_t _numMips
, const void* _src
, bx::Error* _err = NULL
);
///
int32_t imageWriteKtx(
bx::WriterI* _writer
, ImageContainer& _imageContainer
, const void* _data
, uint32_t _size
, bx::Error* _err = NULL
);
///
bool imageParse(
ImageContainer& _imageContainer
, bx::ReaderSeekerI* _reader
, bx::Error* _err
);
///
bool imageParse(
ImageContainer& _imageContainer
, const void* _data
, uint32_t _size
, bx::Error* _err = NULL
);
///
ImageContainer* imageParseDds(
bx::AllocatorI* _allocator
, const void* _src
, uint32_t _size
, bx::Error* _err
);
///
ImageContainer* imageParseKtx(
bx::AllocatorI* _allocator
, const void* _src
, uint32_t _size
, bx::Error* _err
);
///
ImageContainer* imageParsePvr3(
bx::AllocatorI* _allocator
, const void* _src
, uint32_t _size
, bx::Error* _err
);
///
ImageContainer* imageParseGnf(
bx::AllocatorI* _allocator
, const void* _src
, uint32_t _size
, bx::Error* _err
);
///
void imageDecodeToR8(
bx::AllocatorI* _allocator
, void* _dst
, const void* _src
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint32_t _dstPitch
, TextureFormat::Enum _srcFormat
);
///
void imageDecodeToBgra8(
void* _dst
, const void* _src
, uint32_t _width
, uint32_t _height
, uint32_t _dstPitch
, TextureFormat::Enum _format
);
///
void imageDecodeToRgba8(
void* _dst
, const void* _src
, uint32_t _width
, uint32_t _height
, uint32_t _dstPitch
, TextureFormat::Enum _format
);
///
void imageDecodeToRgba32f(
bx::AllocatorI* _allocator
, void* _dst
, const void* _src
, uint32_t _width
, uint32_t _height
, uint32_t _depth
, uint32_t _dstPitch
, TextureFormat::Enum _format
);
///
bool imageGetRawData(
const ImageContainer& _imageContainer
, uint16_t _side
, uint8_t _lod
, const void* _data
, uint32_t _size
, ImageMip& _mip
);
} // namespace bimg
#endif // BIMG_IMAGE_H_HEADER_GUARD