forked from bkaradzic/bimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_encode.cpp
672 lines (584 loc) · 19.3 KB
/
image_encode.cpp
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
* Copyright 2011-2022 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bimg/blob/master/LICENSE
*/
#include <bimg/encode.h>
#include "bimg_p.h"
#include <libsquish/squish.h>
#include <etc1/etc1.h>
#include <etc2/ProcessRGB.hpp>
#include <nvtt/nvtt.h>
#include <pvrtc/PvrTcEncoder.h>
#include <edtaa3/edtaa3func.h>
#include <astcenc.h>
BX_PRAGMA_DIAGNOSTIC_PUSH();
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // warning C4100: 'alloc_context': unreferenced formal parameter
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4702) // warning C4702: unreachable code
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter") // warning: unused parameter ‘alloc_context’ [-Wunused-parameter]
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb/stb_image_resize.h>
BX_PRAGMA_DIAGNOSTIC_POP();
extern "C" {
#include <iqa.h>
}
namespace bimg
{
static uint32_t s_squishQuality[] =
{
// Standard
squish::kColourClusterFit, // Default
squish::kColourIterativeClusterFit, // Highest
squish::kColourRangeFit, // Fastest
// Normal map
squish::kColourClusterFit, // Default
squish::kColourIterativeClusterFit, // Highest
squish::kColourRangeFit, // Fastest
};
BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_squishQuality) );
static const float s_astcQuality[] =
{
// Standard
ASTCENC_PRE_MEDIUM, // Default
ASTCENC_PRE_THOROUGH, // Highest
ASTCENC_PRE_FAST, // Fastest
// Normal map
ASTCENC_PRE_MEDIUM, // Default
ASTCENC_PRE_THOROUGH, // Highest
ASTCENC_PRE_FAST, // Fastest
};
BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_astcQuality));
void imageEncodeFromRgba8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _format, Quality::Enum _quality, bx::Error* _err)
{
const uint8_t* src = (const uint8_t*)_src;
uint8_t* dst = (uint8_t*)_dst;
const uint32_t srcPitch = _width*4;
const uint32_t srcSlice = _height*srcPitch;
const uint32_t dstBpp = getBitsPerPixel(_format);
const uint32_t dstPitch = _width*dstBpp/8;
const uint32_t dstSlice = _height*dstPitch;
for (uint32_t zz = 0; zz < _depth && _err->isOk(); ++zz, src += srcSlice, dst += dstSlice)
{
switch (_format)
{
case TextureFormat::BC1:
case TextureFormat::BC2:
case TextureFormat::BC3:
case TextureFormat::BC4:
case TextureFormat::BC5:
squish::CompressImage(src, _width, _height, dst
, s_squishQuality[_quality]
| (_format == TextureFormat::BC2 ? squish::kDxt3
: _format == TextureFormat::BC3 ? squish::kDxt5
: _format == TextureFormat::BC4 ? squish::kBc4
: _format == TextureFormat::BC5 ? squish::kBc5
: squish::kDxt1)
);
break;
case TextureFormat::BC6H:
case TextureFormat::BC7:
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
break;
case TextureFormat::ETC1:
etc1_encode_image(src, _width, _height, 4, _width*4, dst);
break;
case TextureFormat::ETC2:
{
const uint32_t blockWidth = (_width +3)/4;
const uint32_t blockHeight = (_height+3)/4;
uint64_t* dstBlock = (uint64_t*)dst;
for (uint32_t yy = 0; yy < blockHeight; ++yy)
{
for (uint32_t xx = 0; xx < blockWidth; ++xx)
{
uint8_t block[4*4*4];
const uint8_t* ptr = &src[(yy*srcPitch+xx*4)*4];
for (uint32_t ii = 0; ii < 16; ++ii)
{ // BGRx
bx::memCopy(&block[ii*4], &ptr[(ii%4)*srcPitch + (ii&~3)], 4);
bx::swap(block[ii*4+0], block[ii*4+2]);
}
*dstBlock++ = ProcessRGB_ETC2(block);
}
}
}
break;
case TextureFormat::PTC14:
{
using namespace Javelin;
RgbaBitmap bmp;
bmp.width = _width;
bmp.height = _height;
bmp.data = const_cast<uint8_t*>(src);
PvrTcEncoder::EncodeRgb4Bpp(dst, bmp);
bmp.data = NULL;
}
break;
case TextureFormat::PTC14A:
{
using namespace Javelin;
RgbaBitmap bmp;
bmp.width = _width;
bmp.height = _height;
bmp.data = const_cast<uint8_t*>(src);
PvrTcEncoder::EncodeRgba4Bpp(dst, bmp);
bmp.data = NULL;
}
break;
case TextureFormat::ASTC4x4:
case TextureFormat::ASTC5x4:
case TextureFormat::ASTC5x5:
case TextureFormat::ASTC6x5:
case TextureFormat::ASTC6x6:
case TextureFormat::ASTC8x5:
case TextureFormat::ASTC8x6:
case TextureFormat::ASTC8x8:
case TextureFormat::ASTC10x5:
case TextureFormat::ASTC10x6:
case TextureFormat::ASTC10x8:
case TextureFormat::ASTC10x10:
case TextureFormat::ASTC12x10:
case TextureFormat::ASTC12x12:
{
const unsigned int thread_count = 1;
const bimg::ImageBlockInfo& astcBlockInfo = bimg::getBlockInfo(_format);
const float quality = s_astcQuality[_quality];
const astcenc_profile profile = ASTCENC_PRF_LDR; //Linear LDR color profile
astcenc_error status;
//Create and init config and context
astcenc_config config{};
unsigned int astcFlags = ASTCENC_FLG_SELF_DECOMPRESS_ONLY;
if (Quality::NormalMapDefault <= _quality) {
astcFlags |= ASTCENC_FLG_MAP_NORMAL;
}
status = astcenc_config_init(profile, astcBlockInfo.blockWidth, astcBlockInfo.blockHeight, 1, quality, astcFlags, &config);
if (status != ASTCENC_SUCCESS) {
BX_TRACE("astc error in config init %s", astcenc_get_error_string(status));
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to initialize astc config!");
break;
}
astcenc_context* context;
status = astcenc_context_alloc(&config, thread_count, &context);
if (status != ASTCENC_SUCCESS) {
BX_TRACE("astc error in context alloc %s", astcenc_get_error_string(status));
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to alloc astc context!");
break;
}
//Put image data into an astcenc_image
astcenc_image image{};
image.dim_x = _width;
image.dim_y = _height;
image.dim_z = 1;
image.data_type = ASTCENC_TYPE_U8;
image.data = reinterpret_cast<void**>(const_cast<uint8_t**>(&src));
const size_t block_count_x = (_width + astcBlockInfo.blockWidth - 1) / astcBlockInfo.blockWidth;
const size_t block_count_y = (_height + astcBlockInfo.blockHeight - 1) / astcBlockInfo.blockHeight;
const size_t comp_len = block_count_x * block_count_y * 16;
if (Quality::NormalMapDefault <= _quality)
{
static const astcenc_swizzle swizzle { //0001/rrrg swizzle corresponds to ASTC_ENC_NORMAL_RA
ASTCENC_SWZ_R, ASTCENC_SWZ_R, ASTCENC_SWZ_R, ASTCENC_SWZ_G
};
status = astcenc_compress_image(context, &image, &swizzle, dst, comp_len, 0);
}
else
{
static const astcenc_swizzle swizzle { //0123/rgba swizzle corresponds to ASTC_RGBA
ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
};
status = astcenc_compress_image(context, &image, &swizzle, dst, comp_len, 0);
}
if (status != ASTCENC_SUCCESS) {
BX_TRACE("astc error in compress image %s", astcenc_get_error_string(status));
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to compress astc image!");
astcenc_context_free(context);
break;
}
astcenc_context_free(context);
}
break;
case TextureFormat::BGRA8:
imageSwizzleBgra8(dst, dstPitch, _width, _height, src, srcPitch);
break;
case TextureFormat::RGBA8:
bx::memCopy(_dst, dstPitch, _src, srcPitch, srcPitch, _height);
break;
default:
if (!imageConvert(_allocator, dst, _format, src, TextureFormat::RGBA8, _width, _height, 1) )
{
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
}
break;
}
}
}
void imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _dstFormat, Quality::Enum _quality, bx::Error* _err)
{
BX_ERROR_SCOPE(_err);
const uint8_t* src = (const uint8_t*)_src;
switch (_dstFormat)
{
case TextureFormat::BC6H:
nvtt::compressBC6H(src, _width, _height, _width*16, _dst);
break;
case TextureFormat::BC7:
nvtt::compressBC7(src, _width, _height, _width*16, _dst);
break;
default:
if (!imageConvert(_allocator, _dst, _dstFormat, _src, TextureFormat::RGBA32F, _width, _height, _depth) )
{
uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*_depth*4);
if (imageConvert(_allocator, temp, TextureFormat::RGBA8, _src, TextureFormat::RGBA32F, _width, _height, _depth) )
{
for (uint32_t zz = 0; zz < _depth; ++zz)
{
const uint32_t zoffset = zz*_width*_height;
for (uint32_t yy = 0; yy < _height; ++yy)
{
const uint32_t yoffset = zoffset + yy*_width;
for (uint32_t xx = 0; xx < _width; ++xx)
{
const uint32_t offset = yoffset + xx;
const float* input = (const float*)&src[offset * 16];
uint8_t* output = &temp[offset * 4];
output[0] = uint8_t(bx::clamp(input[0], 0.0f, 1.0f)*255.0f + 0.5f);
output[1] = uint8_t(bx::clamp(input[1], 0.0f, 1.0f)*255.0f + 0.5f);
output[2] = uint8_t(bx::clamp(input[2], 0.0f, 1.0f)*255.0f + 0.5f);
output[3] = uint8_t(bx::clamp(input[3], 0.0f, 1.0f)*255.0f + 0.5f);
}
}
}
imageEncodeFromRgba8(_allocator, _dst, temp, _width, _height, _depth, _dstFormat, _quality, _err);
}
else
{
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
}
BX_FREE(_allocator, temp);
}
break;
}
}
void imageEncode(bx::AllocatorI* _allocator, void* _dst, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, TextureFormat::Enum _dstFormat, Quality::Enum _quality, bx::Error* _err)
{
switch (_dstFormat)
{
case TextureFormat::BC1:
case TextureFormat::BC2:
case TextureFormat::BC3:
case TextureFormat::BC4:
case TextureFormat::BC5:
case TextureFormat::ETC1:
case TextureFormat::ETC2:
case TextureFormat::PTC14:
case TextureFormat::PTC14A:
case TextureFormat::ASTC4x4:
case TextureFormat::ASTC5x5:
case TextureFormat::ASTC6x6:
case TextureFormat::ASTC8x5:
case TextureFormat::ASTC8x6:
case TextureFormat::ASTC10x5:
{
uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*_depth*4);
imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat);
imageEncodeFromRgba8(_allocator, _dst, temp, _width, _height, _depth, _dstFormat, _quality, _err);
BX_FREE(_allocator, temp);
}
break;
case bimg::TextureFormat::BC6H:
case bimg::TextureFormat::BC7:
{
uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*_depth*16);
imageDecodeToRgba32f(_allocator, temp, _src, _width, _height, _depth, _width*16, _srcFormat);
imageEncodeFromRgba32f(_allocator, _dst, temp, _width, _height, _depth, _dstFormat, _quality, _err);
BX_FREE(_allocator, temp);
}
break;
default:
if (!imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, 1) )
{
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
}
break;
}
}
ImageContainer* imageEncode(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, Quality::Enum _quality, const ImageContainer& _input)
{
ImageContainer* output = imageAlloc(_allocator
, _dstFormat
, uint16_t(_input.m_width)
, uint16_t(_input.m_height)
, uint16_t(_input.m_depth)
, _input.m_numLayers
, _input.m_cubeMap
, 1 < _input.m_numMips
);
const uint16_t numSides = _input.m_numLayers * (_input.m_cubeMap ? 6 : 1);
bx::Error err;
for (uint16_t side = 0; side < numSides && err.isOk(); ++side)
{
for (uint8_t lod = 0, num = _input.m_numMips; lod < num && err.isOk(); ++lod)
{
ImageMip mip;
if (imageGetRawData(_input, side, lod, _input.m_data, _input.m_size, mip) )
{
ImageMip dstMip;
imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
imageEncode(
_allocator
, dstData
, mip.m_data
, mip.m_format
, mip.m_width
, mip.m_height
, mip.m_depth
, _dstFormat
, _quality
, &err
);
}
}
}
if (err.isOk() )
{
return output;
}
imageFree(output);
return NULL;
}
void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _pitch, const void* _src)
{
const uint8_t* src = (const uint8_t*)_src;
uint8_t* dst = (uint8_t*)_dst;
for (uint32_t zz = 0; zz < _depth; ++zz)
{
for (uint32_t yy = 0; yy < _height; ++yy)
{
for (uint32_t xx = 0; xx < _width; ++xx)
{
const uint32_t offset = yy*_pitch + xx * 16;
const float* input = (const float*)&src[offset];
float* output = (float*)&dst[offset];
output[0] = input[0]*0.5f + 0.5f;
output[1] = input[1]*0.5f + 0.5f;
output[2] = input[2]*0.5f + 0.5f;
output[3] = input[3]*0.5f + 0.5f;
}
}
}
}
static void edtaa3(bx::AllocatorI* _allocator, double* _dst, uint32_t _width, uint32_t _height, double* _src)
{
const uint32_t numPixels = _width*_height;
short* xdist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
short* ydist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
double* gx = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
double* gy = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
::computegradient(_src, _width, _height, gx, gy);
::edtaa3(_src, gx, gy, _width, _height, xdist, ydist, _dst);
for (uint32_t ii = 0; ii < numPixels; ++ii)
{
if (_dst[ii] < 0.0)
{
_dst[ii] = 0.0;
}
}
BX_FREE(_allocator, xdist);
BX_FREE(_allocator, ydist);
BX_FREE(_allocator, gx);
BX_FREE(_allocator, gy);
}
void imageMakeDist(bx::AllocatorI* _allocator, void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
{
const uint32_t numPixels = _width*_height;
double* imgIn = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
double* outside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
double* inside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
for (uint32_t yy = 0; yy < _height; ++yy)
{
const uint8_t* src = (const uint8_t*)_src + yy*_srcPitch;
double* dst = &imgIn[yy*_width];
for (uint32_t xx = 0; xx < _width; ++xx)
{
dst[xx] = double(src[xx])/255.0;
}
}
edtaa3(_allocator, outside, _width, _height, imgIn);
for (uint32_t ii = 0; ii < numPixels; ++ii)
{
imgIn[ii] = 1.0 - imgIn[ii];
}
edtaa3(_allocator, inside, _width, _height, imgIn);
BX_FREE(_allocator, imgIn);
uint8_t* dst = (uint8_t*)_dst;
for (uint32_t ii = 0; ii < numPixels; ++ii)
{
double dist = bx::clamp( (outside[ii] - inside[ii]) * 1.0/16.0 + 0.5, 0.0, 1.0);
dst[ii] = 255-uint8_t(dist * 255.0);
}
BX_FREE(_allocator, inside);
BX_FREE(_allocator, outside);
}
static const iqa_ssim_args s_iqaArgs =
{
0.39f, // alpha
0.731f, // beta
1.12f, // gamma
187, // L
0.025987f, // K1
0.0173f, // K2
1 // factor
};
float imageQualityRgba8(
const void* _reference
, const void* _data
, uint16_t _width
, uint16_t _height
)
{
float result = iqa_ssim( (const uint8_t*)_reference
, (const uint8_t*)_data
, _width
, _height
, _width*4
, 0
, &s_iqaArgs
);
return result;
}
bool imageResizeRgba32fLinear(ImageContainer* _dst, const ImageContainer* _src)
{
const uint16_t numSides = _src->m_numLayers * (_src->m_cubeMap ? 6 : 1);
for (uint16_t side = 0; side < numSides; ++side)
{
bimg::ImageMip srcMip;
bimg::imageGetRawData(*_src, side, 0, _src->m_data, _src->m_size, srcMip);
bimg::ImageMip dstMip;
bimg::imageGetRawData(*_dst, side, 0, _dst->m_data, _dst->m_size, dstMip);
uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
const uint32_t srcPitch = _src->m_width*16;
const uint32_t srcSlice = _src->m_height*srcPitch;
const uint32_t dstPitch = _dst->m_width*16;
const uint32_t dstSlice = _dst->m_height*dstPitch;
for (uint32_t zz = 0, depth = _dst->m_depth; zz < depth; ++zz, dstData += dstSlice)
{
const uint32_t srcDataStep = uint32_t(bx::floor(zz * _src->m_depth / float(_dst->m_depth) ) );
const uint8_t* srcData = &srcMip.m_data[srcDataStep*srcSlice];
int result = stbir_resize_float_generic(
(const float*)srcData, _src->m_width, _src->m_height, srcPitch
, ( float*)dstData, _dst->m_width, _dst->m_height, dstPitch
, 4, 3
, STBIR_FLAG_ALPHA_PREMULTIPLIED
, STBIR_EDGE_CLAMP
, STBIR_FILTER_BOX
, STBIR_COLORSPACE_LINEAR
, NULL
);
if (1 != result)
{
return false;
}
}
}
return true;
}
static float getAlpha(UnpackFn _unpack, const void* _data)
{
float rgba[4];
_unpack(rgba, _data);
return rgba[3];
}
float imageAlphaTestCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, float _alphaRef, float _scale, uint32_t _upscale)
{
UnpackFn unpack = getUnpack(_format);
if (NULL == unpack)
{
return 0.0f;
}
float coverage = 0.0f;
const uint8_t* src = (const uint8_t*)_src;
const uint32_t xstep = getBitsPerPixel(_format) / 8;
const uint32_t numSamples = _upscale;
const float sampleStep = 1.0f / numSamples;
for (uint32_t yy = 0, ystep = _srcPitch; yy < _height-1; ++yy, src += ystep)
{
const uint8_t* data = src;
for (uint32_t xx = 0; xx < _width-1; ++xx, data += xstep)
{
float alpha00 = _scale * getAlpha(unpack, data);
float alpha10 = _scale * getAlpha(unpack, data+xstep);
float alpha01 = _scale * getAlpha(unpack, data+ystep);
float alpha11 = _scale * getAlpha(unpack, data+ystep+xstep);
for (float fy = 0.0f; fy < 1.0f; fy += sampleStep)
{
for (float fx = 0.0f; fx < 1.0f; fx += sampleStep)
{
float alpha = 0.0f
+ alpha00 * (1.0f - fx) * (1.0f - fy)
+ alpha10 * ( fx) * (1.0f - fy)
+ alpha01 * (1.0f - fx) * ( fy)
+ alpha11 * ( fx) * ( fy)
;
if (alpha > _alphaRef)
{
coverage += 1.0f;
}
}
}
}
}
return coverage / float(_width*_height*numSamples*numSamples);
}
void imageScaleAlphaToCoverage(TextureFormat::Enum _format, uint32_t _width, uint32_t _height, uint32_t _srcPitch, void* _src, float _desiredCoverage, float _alphaRef, uint32_t _upscale)
{
PackFn pack = getPack(_format);
UnpackFn unpack = getUnpack(_format);
if (NULL == pack
|| NULL == unpack)
{
return;
}
float min = 0.0f;
float max = 4.0f;
float scale = 1.0f;
for (uint32_t ii = 0; ii < 10; ++ii)
{
float coverage = imageAlphaTestCoverage(
_format
, _width
, _height
, _srcPitch
, _src
, _alphaRef
, scale
, _upscale
);
if (coverage < _desiredCoverage)
{
min = scale;
}
else if (coverage > _desiredCoverage)
{
max = scale;
}
else
{
break;
}
scale = (min + max) * 0.5f;
}
uint8_t* src = (uint8_t*)_src;
const uint32_t xstep = getBitsPerPixel(_format) / 8;
for (uint32_t yy = 0, ystep = _srcPitch; yy < _height; ++yy, src += ystep)
{
uint8_t* data = src;
for (uint32_t xx = 0; xx < _width; ++xx, data += xstep)
{
float rgba[4];
unpack(rgba, data);
rgba[3] = bx::clamp(rgba[3]*scale, 0.0f, 1.0f);
pack(data, rgba);
}
}
}
} // namespace bimg