-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageToolsBitmap.cpp
More file actions
763 lines (706 loc) · 22.8 KB
/
Copy pathImageToolsBitmap.cpp
File metadata and controls
763 lines (706 loc) · 22.8 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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
// Copyright © 2014 CCP ehf.
#include "StdAfx.h"
#include "ImageToolsBitmap.h"
#include "FileStream.h"
#include "CompressionOptions.h"
#include "MemoryOutputHandler.h"
#include "MemoryStream.h"
#include "AllowThreads.h"
using namespace ImageIO;
#define CBR_RETURN_BR( x ) { auto ret = x; if( !BeIsSuccess( ret ) ) { return ret; } }
namespace
{
#if WITH_COMPRESSONATOR
CMP_FORMAT PixelFormatToCmpFormat( Tr2RenderContextEnum::PixelFormat format )
{
switch( format )
{
case Tr2RenderContextEnum::PIXEL_FORMAT_B8G8R8A8_UNORM:
case Tr2RenderContextEnum::PIXEL_FORMAT_B8G8R8X8_UNORM:
return CMP_FORMAT_BGRA_8888;
case Tr2RenderContextEnum::PIXEL_FORMAT_R8_UNORM:
return CMP_FORMAT_R_8;
case Tr2RenderContextEnum::PIXEL_FORMAT_BC1_UNORM:
return CMP_FORMAT_BC1;
case Tr2RenderContextEnum::PIXEL_FORMAT_BC2_UNORM:
return CMP_FORMAT_BC2;
case Tr2RenderContextEnum::PIXEL_FORMAT_BC3_UNORM:
return CMP_FORMAT_BC3;
case Tr2RenderContextEnum::PIXEL_FORMAT_BC4_UNORM:
return CMP_FORMAT_BC4;
case Tr2RenderContextEnum::PIXEL_FORMAT_BC5_UNORM:
return CMP_FORMAT_BC5;
case Tr2RenderContextEnum::PIXEL_FORMAT_BC6H_UF16:
return CMP_FORMAT_BC6H;
case Tr2RenderContextEnum::PIXEL_FORMAT_BC7_UNORM:
return CMP_FORMAT_BC7;
default:
return CMP_FORMAT_Unknown;
}
}
#endif
}
ImageToolsBitmap::ImageToolsBitmap( IRoot* lockobj )
{
}
StdOrImageIOResult ImageToolsBitmap::Load( const wchar_t* filename )
{
AllowThreads allowThreads;
FileStream stream( filename, FileStream::READ );
if( !stream.IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_IO_ERROR, "failed to open file" );
}
return ImageIOResult( ImageIO::ReadImage( stream, ImageIO::LoadParameters( filename ), *this, &m_metadata ) );
}
StdOrImageIOResult ImageToolsBitmap::Save( const wchar_t* filename )
{
AllowThreads allowThreads;
if( !IsValid() )
{
return StdOrImageIOResult( ImageIO::Result( ImageIO::Result::INVALID_BITMAP, "cannot save invalid bitmap" ) );
}
FileStream stream( filename, FileStream::WRITE );
if( !stream.IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_IO_ERROR, "could not open file for saving" );
}
return StdOrImageIOResult( ImageIO::SaveImage( filename, *this, stream, &m_metadata ) );
}
Be::Result<std::string> ImageToolsBitmap::CheckedCopyChannel( ImageToolsBitmap* source, unsigned srcChannel, unsigned dstChannel )
{
if( !CopyChannel( source, srcChannel, dstChannel ) )
{
return Be::Result<std::string>( "Copy channel failed" );
}
return std::string();
}
Be::Result<std::string> ImageToolsBitmap::CheckedDownsample2x2()
{
AllowThreads allowThreads;
if( !Downsample2x2() )
{
return Be::Result<std::string>( "error while downsampling the bitmap" );
}
return std::string();
}
Be::Result<std::string> ImageToolsBitmap::CheckedCrop( unsigned left, unsigned top, unsigned right, unsigned bottom )
{
if( !Crop( left, top, right, bottom ) )
{
return Be::Result<std::string>( "error while cropping the bitmap" );
}
return std::string();
}
Be::Result<std::string> ImageToolsBitmap::CheckedGenerateMipMaps( unsigned levels )
{
AllowThreads allowThreads;
if( !GenerateMipMaps( levels ) )
{
return Be::Result<std::string>( "error while generating mip levels" );
}
return std::string();
}
BlueStdResult ImageToolsBitmap::CheckedConvertFormat( PixelFormat format )
{
AllowThreads allowThreads;
if( IsCompressed() && ( format == PIXEL_FORMAT_B8G8R8A8_UNORM || format == PIXEL_FORMAT_B8G8R8X8_UNORM ) )
{
return Decompress( format );
}
if( !ConvertFormat( format ) )
{
return BlueStdResult( BLUE_STD_RESULT_RUNTIME_ERROR, "error converting pixel format" );
}
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::CreateFromArray( const std::vector<ImageToolsBitmap*>& elements )
{
if( elements.empty() )
{
Destroy();
return BLUE_STD_RESULT_OK;
}
if( !elements[0] || elements[0]->GetType() != TEX_TYPE_2D || elements[0]->GetArraySize() != 1 || elements[0] == this )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid first bitmap in the list" );
}
for( size_t i = 1; i < elements.size(); ++i )
{
if( !elements[i] || elements[i]->GetArraySize() != 1 || elements[i] == this )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid bitmap in the list" );
}
if( elements[0]->GetType() != elements[i]->GetType() ||
elements[0]->GetFormat() != elements[i]->GetFormat() ||
elements[0]->GetWidth() != elements[i]->GetWidth() ||
elements[0]->GetHeight() != elements[i]->GetHeight() ||
elements[0]->GetTrueMipCount() != elements[i]->GetTrueMipCount() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "bitmap formats/sizes do not match" );
}
}
Destroy();
*static_cast<BitmapDimensions*>( this ) = *elements[0];
m_arraySize = uint32_t( elements.size() );
auto elementSize = elements[0]->m_data.size();
m_data.resize( "HostBitmap::m_data", elementSize * elements.size() );
for( size_t i = 0; i < elements.size(); ++i )
{
memcpy( m_data.get() + elementSize * i, elements[i]->m_data.get(), elementSize );
}
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::Decompress( PixelFormat format )
{
if( !IsCompressed() )//|| GetType() != TEX_TYPE_2D )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "cannot decompress an uncompressed image" );
}
nvtt::Surface surface;
nvtt::Format nvttFormat;
switch( m_format )
{
case PIXEL_FORMAT_BC1_TYPELESS:
case PIXEL_FORMAT_BC1_UNORM:
case PIXEL_FORMAT_BC1_UNORM_SRGB:
nvttFormat = nvtt::Format_BC1;
break;
case PIXEL_FORMAT_BC2_TYPELESS:
case PIXEL_FORMAT_BC2_UNORM:
case PIXEL_FORMAT_BC2_UNORM_SRGB:
nvttFormat = nvtt::Format_BC2;
break;
case PIXEL_FORMAT_BC3_TYPELESS:
case PIXEL_FORMAT_BC3_UNORM:
case PIXEL_FORMAT_BC3_UNORM_SRGB:
nvttFormat = nvtt::Format_BC3;
break;
case PIXEL_FORMAT_BC4_TYPELESS:
case PIXEL_FORMAT_BC4_UNORM:
case PIXEL_FORMAT_BC4_SNORM:
nvttFormat = nvtt::Format_BC4;
break;
case PIXEL_FORMAT_BC5_TYPELESS:
case PIXEL_FORMAT_BC5_UNORM:
case PIXEL_FORMAT_BC5_SNORM:
nvttFormat = nvtt::Format_BC5;
break;
case PIXEL_FORMAT_BC6H_TYPELESS:
case PIXEL_FORMAT_BC6H_UF16:
case PIXEL_FORMAT_BC6H_SF16:
nvttFormat = nvtt::Format_BC6;
break;
case PIXEL_FORMAT_BC7_TYPELESS:
case PIXEL_FORMAT_BC7_UNORM:
case PIXEL_FORMAT_BC7_UNORM_SRGB:
nvttFormat = nvtt::Format_BC7;
break;
default:
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid image format" );
}
const size_t bpp = 4;
const size_t pixelCount = GetRawDataSize() * 16 / GetBlockByteSize( m_format );
const size_t newSize = pixelCount * bpp;
CcpMallocBuffer data( "HostBitmap::m_data", newSize );
size_t mipStart = 0;
for( uint32_t mip = 0; mip < GetTrueMipCount(); ++mip )
{
const size_t mipSize = GetMipWidth( mip ) * GetMipHeight( mip );
for( uint32_t face = 0; face < GetArraySize(); ++face )
{
if( !surface.setImage2D( nvttFormat, nvtt::Decoder_D3D10, GetMipWidth( mip ), GetMipHeight( mip ), GetMipRawData( mip, face ) ) )
{
Destroy();
return BlueStdResult( BLUE_STD_RESULT_RUNTIME_ERROR, "could not decompress image" );
}
for( size_t channel = 0; channel < 4; ++channel )
{
static const int channelMap[] = { 2, 1, 0, 3 };
const float* src = surface.channel( channelMap[channel] );
uint8_t* dest = reinterpret_cast<uint8_t*>( data.get() ) + mipStart + channel;
for( size_t i = 0; i < mipSize; ++i )
{
*dest = uint8_t( std::max( std::min( int( *src++ * 255.f + 0.5f ), 255 ), 0 ) );
dest += bpp;
}
}
mipStart += mipSize * bpp;
}
}
m_data.swap( data );
m_format = format;
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::Copy( ImageToolsBitmapPtr& result ) const
{
result.CreateInstance();
if( !result )
{
return BLUE_STD_RESULT_MEMORY_ERROR;
}
static_cast<BitmapDimensions&>( *result ) = *this;
result->m_name = m_name;
result->m_data.resize( "HostBitmap::m_data", m_data.size() );
if( !result->m_data.get() )
{
result = nullptr;
return BLUE_STD_RESULT_MEMORY_ERROR;
}
memcpy( result->m_data.get(), m_data.get(), m_data.size() );
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::FlattenSlices( bool horizontally, ImageToolsBitmapPtr& result ) const
{
if( !IsValid() || IsCompressed() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "source bitmap is invalid" );
}
result.CreateInstance();
uint32_t sliceCount = 1;
switch( GetType() )
{
case TEX_TYPE_2D:
sliceCount = GetArraySize();
break;
case TEX_TYPE_CUBE:
sliceCount = 6;
break;
case TEX_TYPE_3D:
sliceCount = GetDepth();
break;
default:
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "unsupported bitmap" );
}
if( !result->Create( horizontally ? GetWidth() * sliceCount : GetWidth(), !horizontally ? GetHeight() * sliceCount : GetHeight(), 0, GetFormat() ) )
{
return BLUE_STD_RESULT_MEMORY_ERROR;
}
auto dst = result->GetRawData();
for( uint32_t i = 0; i < sliceCount; ++i )
{
auto src = GetType() == TEX_TYPE_3D ? GetMipRawData( 0 ) + GetMipPitch( 0 ) * GetMipHeight( 0 ) * i : GetMipRawData( 0, i );
auto size = GetMipSize( 0 ) / sliceCount;
auto sourcePitch = GetMipPitch( 0 );
auto destPitch = result->GetMipPitch( 0 );
auto height = GetMipHeight( 0 );
auto row = GetMipWidth( 0 ) * GetBytesPerPixel( GetFormat() );
char *d;
if( horizontally )
{
d = dst + row * i;
}
else
{
d = dst + destPitch * height * i;
}
for( uint32_t y = 0; y < height; ++y )
{
memcpy( d, src, row );
d += destPitch;
src += sourcePitch;
}
}
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::ExtractMipLevel( uint32_t mipLevel, Be::OptionalWithDefaultValue<uint32_t, 1> count, ImageToolsBitmapPtr& result ) const
{
if( !IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "source bitmap is invalid" );
}
if( mipLevel >= GetTrueMipCount() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid mip level" );
}
result.CreateInstance();
if( !result )
{
return BLUE_STD_RESULT_MEMORY_ERROR;
}
bool createResult = false;
uint32_t faceCount = 1;
uint32_t mipCount = std::min( uint32_t( count ), GetTrueMipCount() - mipLevel );
switch( GetType() )
{
case TEX_TYPE_CUBE:
createResult = result->CreateCube( GetMipWidth( mipLevel ), mipCount, GetFormat() );
faceCount = 6;
break;
case TEX_TYPE_3D:
createResult = result->CreateVolume( GetMipWidth( mipLevel ), GetMipHeight( mipLevel ), GetMipDepth( mipLevel ), mipCount, GetFormat() );
break;
default:
createResult = result->Create( GetMipWidth( mipLevel ), GetMipHeight( mipLevel ), mipCount, GetFormat() );
}
if( !createResult )
{
return BLUE_STD_RESULT_MEMORY_ERROR;
}
for( uint32_t face = 0; face < faceCount; ++face )
{
for( uint32_t m = 0; m < mipCount; ++m )
{
memcpy( result->GetMipRawData( m, CubemapFace( face ) ), GetMipRawData( mipLevel + m, CubemapFace( face ) ), result->GetMipSize( m ) );
}
}
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::ToYuv( std::pair<ImageToolsBitmapPtr, ImageToolsBitmapPtr>& result ) const
{
if( !IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "source bitmap is invalid" );
}
if( GetFormat() != PIXEL_FORMAT_B8G8R8A8_UNORM && GetFormat() != PIXEL_FORMAT_B8G8R8X8_UNORM )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid format" );
}
result.first.CreateInstance();
result.second.CreateInstance();
if( !result.first || !result.second )
{
return BLUE_STD_RESULT_MEMORY_ERROR;
}
bool createResult = false;
uint32_t faceCount = 1;
switch( GetType() )
{
case TEX_TYPE_CUBE:
createResult = result.first->CreateCube( GetWidth(), GetMipCount(), PIXEL_FORMAT_R8_UNORM );
createResult = result.second->CreateCube( GetWidth() / 2, GetMipCount() ? std::max( GetMipCount() - 1, 1u ) : 0, PIXEL_FORMAT_B8G8R8A8_UNORM ) && createResult;
faceCount = 6;
break;
default:
break;
}
if( !createResult )
{
return BLUE_STD_RESULT_MEMORY_ERROR;
}
memset( result.second->GetRawData(), 0, result.second->GetRawDataSize() );
for( uint32_t mip = 0; mip < GetMipCount(); ++mip )
{
for( uint32_t face = 0; face < faceCount; ++face )
{
const uint8_t* src = reinterpret_cast<const uint8_t*>( GetMipRawData( mip, CubemapFace( face ) ) );
uint8_t* y = reinterpret_cast<uint8_t*>( result.first->GetMipRawData( mip, CubemapFace( face ) ) );
uint8_t* u = nullptr;
uint8_t* v = nullptr;
if( mip < result.second->GetMipCount() )
{
u = reinterpret_cast<uint8_t*>( result.second->GetMipRawData( mip, CubemapFace( face ) ) ) + 1;
v = reinterpret_cast<uint8_t*>( result.second->GetMipRawData( mip, CubemapFace( face ) ) ) + 2;
}
for( uint32_t j = 0; j < GetMipHeight( mip ); ++j )
{
for( uint32_t i = 0; i < GetMipWidth( mip ); ++i )
{
float r = src[i * 4 + 2];
float g = src[i * 4 + 1];
float b = src[i * 4 + 0];
int yy = int( r * 0.299f + g * 0.587f + b * 0.114f );
y[i] = yy;
if( mip < result.second->GetMipCount() && i % 2 == 0 && j % 2 == 0 )
{
u[i * 2] = int( r * -0.168736f + g * -0.331264f + b * 0.500000f + 128 );
v[i * 2] = int( r * 0.500000f + g * -0.418688f + b * -0.081312f + 128 );
}
}
src += GetMipPitch( mip );
y += result.first->GetMipPitch( mip );
if( mip < result.second->GetMipCount() && j % 2 == 0 )
{
u += result.second->GetMipPitch( mip );
v += result.second->GetMipPitch( mip );
}
}
}
}
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::SetMipData( uint32_t mipLevel, ImageToolsBitmap* result, uint32_t sourceMip )
{
if( !IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid bitmap" );
}
if( !result || !result->IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid source bitmap" );
}
if( mipLevel >= GetTrueMipCount() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid mip level" );
}
if( sourceMip >= result->GetTrueMipCount() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "invalid source mip level" );
}
if( GetType() != result->GetType() || GetFormat() != result->GetFormat() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "incompatible bitmaps" );
}
if( GetMipWidth( mipLevel ) != result->GetMipWidth( sourceMip ) ||
GetMipHeight( mipLevel ) != result->GetMipHeight( sourceMip ) ||
GetMipDepth( mipLevel ) != result->GetMipDepth( sourceMip ) )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "mip sizes do not match" );
}
uint32_t faces = 1;
if( GetType() == TEX_TYPE_CUBE )
{
faces = 6;
}
for( uint32_t face = 0; face < faces; ++face )
{
memcpy( GetMipRawData( mipLevel, CubemapFace( face ) ), result->GetMipRawData( sourceMip, CubemapFace( face ) ), GetMipSize( mipLevel ) );
}
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::CreateNvttInputOptions(
CompressionOptions* compressionOptions,
nvtt::InputOptions& inputOptions )
{
switch( GetType() )
{
case TEX_TYPE_1D:
case TEX_TYPE_2D:
inputOptions.setTextureLayout( nvtt::TextureType_2D, GetWidth(), GetHeight() );
break;
case TEX_TYPE_CUBE:
inputOptions.setTextureLayout( nvtt::TextureType_Cube, GetWidth(), GetHeight() );
break;
case TEX_TYPE_3D:
if( GetMipCount() != 1 || ( compressionOptions && compressionOptions->GetGenerateMipsMaps() ) )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "compressing volume textures with mip maps is not supported" );
}
if( GetWidth() % 4 || GetHeight() % 4 || GetDepth() % 4 )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "compressing volume textures with sizes not divisible by 4 is not supported" );
}
inputOptions.setTextureLayout( nvtt::TextureType_2D, GetWidth(), GetHeight() * GetDepth() );
break;
default:
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "unexpected bitmap type" );
}
PixelFormat inputFormat = GetFormat();
switch( inputFormat )
{
case PIXEL_FORMAT_R32G32B32A32_FLOAT:
inputOptions.setFormat( nvtt::InputFormat_RGBA_32F );
break;
case PIXEL_FORMAT_R32G32B32_FLOAT:
inputOptions.setFormat( nvtt::InputFormat_RGBA_32F );
break;
case PIXEL_FORMAT_R16G16B16A16_FLOAT:
inputOptions.setFormat( nvtt::InputFormat_RGBA_16F );
break;
case PIXEL_FORMAT_B8G8R8A8_UNORM:
case PIXEL_FORMAT_B8G8R8X8_UNORM:
case PIXEL_FORMAT_R8_UNORM:
case PIXEL_FORMAT_R8G8_UNORM:
inputOptions.setFormat( nvtt::InputFormat_BGRA_8UB );
break;
default:
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "unsupported input pixel format" );
}
if( compressionOptions )
{
PixelFormat compressionFormat = compressionOptions->GetFormat();
if( (compressionFormat == PIXEL_FORMAT_BC7_UNORM || compressionFormat == PIXEL_FORMAT_BC7_UNORM_SRGB) &&
( inputFormat != PIXEL_FORMAT_B8G8R8A8_UNORM && inputFormat != PIXEL_FORMAT_B8G8R8X8_UNORM ))
{
// NVTT compression on BC7 floating point formatted textures seems to result in assertion errors.
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "Unsupported input pixel format for BC7" );
}
}
inputOptions.setAlphaMode( nvtt::AlphaMode_None );
if( compressionOptions && compressionOptions->GetGenerateMipsMaps() )
{
inputOptions.setMipmapGeneration( true );
}
else
{
if( GetMipCount() == 1 )
{
inputOptions.setMipmapGeneration( false );
}
else
{
inputOptions.setMipmapGeneration( true, GetTrueMipCount() );
}
}
int faceCount = GetType() == TEX_TYPE_CUBE ? 6 : 1;
uint32_t mipCount = compressionOptions && compressionOptions->GetGenerateMipsMaps() ? 1 : GetTrueMipCount();
for( int face = 0; face < faceCount; ++face )
{
for( uint32_t i = 0; i < mipCount; ++i )
{
uint32_t width = GetMipWidth( i );
uint32_t height = GetType() == TEX_TYPE_3D ? GetMipHeight( i ) * GetMipDepth( i ) : GetMipHeight( i );
auto data = GetMipRawData( i, CubemapFace( face ) );
switch( inputFormat )
{
case PIXEL_FORMAT_R8_UNORM:
{
std::unique_ptr<uint8_t, TrackableDelete<uint8_t>> copy( CCP_NEW( "ImageToolsBitmap::CreateNvttInputOptions/copy" ) uint8_t[width * height * 4] );
auto src = reinterpret_cast<const uint8_t*>( data );
auto dst = copy.get();
for( uint32_t p = 0; p < width * height; ++p )
{
*dst++ = *src;
*dst++ = *src;
*dst++ = *src;
*dst++ = *src;
++src;
}
inputOptions.setMipmapData( copy.get(), width, height, 1, face, i );
}
break;
case PIXEL_FORMAT_R8G8_UNORM:
{
std::unique_ptr<uint8_t, TrackableDelete<uint8_t>> copy( CCP_NEW( "ImageToolsBitmap::CreateNvttInputOptions/copy" ) uint8_t[width * height * 4] );
auto src = reinterpret_cast<const uint8_t*>( data );
auto dst = copy.get();
for( uint32_t p = 0; p < width * height; ++p )
{
*dst++ = *src;
*dst++ = *src++;
*dst++ = *src;
*dst++ = *src;
++src;
}
inputOptions.setMipmapData( copy.get(), width, height, 1, face, i );
}
break;
default:
inputOptions.setMipmapData( data, width, height, 1, face, i );
}
}
}
return BLUE_STD_RESULT_OK;
}
BlueStdResult ImageToolsBitmap::CompressWithOptions( CompressionOptions* options, const nvtt::OutputOptions& output )
{
if( !IsValid() )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "cannot compress invalid bitmap" );
}
nvtt::InputOptions input;
CBR_RETURN_BR( CreateNvttInputOptions( options, input ) );
nvtt::CompressionOptions compression;
nvtt::Compressor compressor;
if( options )
{
options->FillNvttOptions( compression );
compressor.enableCudaAcceleration( options->UseCuda() );
}
if( !compressor.process( input, compression, output ) )
{
return BlueStdResult( BLUE_STD_RESULT_RUNTIME_ERROR, "error during image compression" );
}
return BLUE_STD_RESULT_OK;
}
StdOrImageIOResult ImageToolsBitmap::Compress( CompressionOptions* options, ImageToolsBitmapPtr& result )
{
AllowThreads allowThreads;
#if WITH_COMPRESSONATOR
CompressionOptions::Compressor compressor = CompressionOptions::NVTT;
if( options )
{
compressor = options->GetCompressor();
}
if( compressor == CompressionOptions::COMPRESSONATOR )
{
result.CreateInstance();
Tr2RenderContextEnum::PixelFormat destFormat;
CMP_CompressOptions cmpOptions = { 0 };
if( !options )
{
CompressionOptionsPtr defaultOptions;
defaultOptions.CreateInstance();
defaultOptions->FillCompressonatorOptions( cmpOptions );
destFormat = defaultOptions->GetFormat();
}
else
{
options->FillCompressonatorOptions( cmpOptions );
destFormat = options->GetFormat();
}
Tr2BitmapDimensions dim( GetType(), destFormat, GetWidth(), GetHeight(), GetDepth(), GetTrueMipCount(), GetArraySize() );
if( !result->CreateFromBitmapDimensions( dim ) )
{
return BlueStdResult( BLUE_STD_RESULT_MEMORY_ERROR );
}
for( uint32_t ai = 0; ai < GetArraySize(); ++ai )
{
for( uint32_t mi = 0; mi < GetTrueMipCount(); ++mi )
{
CMP_Texture srcTexture;
srcTexture.dwSize = sizeof( srcTexture );
srcTexture.dwWidth = GetMipWidth( mi );
srcTexture.dwHeight = GetMipHeight( mi );
srcTexture.dwPitch = 0;
srcTexture.format = PixelFormatToCmpFormat( GetFormat() );
srcTexture.dwDataSize = GetMipSize( mi );
srcTexture.pData = (CMP_BYTE*)GetMipRawData( mi, ai );
CMP_Texture destTexture;
destTexture.dwSize = sizeof( destTexture );
destTexture.dwWidth = GetMipWidth( mi ); result->GetMipWidth( mi );
destTexture.dwHeight = GetMipHeight( mi ); result->GetMipHeight( mi );
destTexture.dwPitch = 0;
destTexture.format = PixelFormatToCmpFormat( destFormat );
destTexture.dwDataSize = result->GetMipSize( mi );
destTexture.pData = (CMP_BYTE*)result->GetMipRawData( mi, ai );
auto cmpStatus = CMP_ConvertTexture( &srcTexture, &destTexture, &cmpOptions, nullptr, 0, 0 );
if( cmpStatus != CMP_OK )
{
return BlueStdResult( BLUE_STD_RESULT_RUNTIME_ERROR, "compressonator error" );
}
}
}
}
else
#endif
{
MemoryOutputHandler outputHandler;
nvtt::OutputOptions output;
output.setOutputHandler( &outputHandler );
if( options && IsDds10Format( options->GetFormat() ) )
{
output.setContainer( nvtt::Container_DDS10 );
}
CBR_RETURN_BR( CompressWithOptions( options, output ) );
MemoryStream memStream( outputHandler.GetData(), outputHandler.GetSize() );
result.CreateInstance();
CBR_RETURN_BR( ImageIOResult( ImageIO::ReadImage( memStream, ImageIO::LoadParameters( L"out.dds" ), *result ) ) );
if( result && GetType() == TEX_TYPE_3D )
{
result->m_type = GetType();
result->m_height /= GetDepth();
result->m_volumeDepth = GetDepth();
}
}
return BlueStdResult( BLUE_STD_RESULT_OK );
}
BlueStdResult ImageToolsBitmap::CompressToFile( const wchar_t* filename, CompressionOptions* options )
{
AllowThreads allowThreads;
nvtt::OutputOptions output;
output.setFileName( CW2A( filename ) );
return CompressWithOptions( options, output );
}
ImageIO::MetadataStrings ImageToolsBitmap::GetMetadataStrings() const
{
return m_metadata.metadata;
}
void ImageToolsBitmap::SetMetadataStrings( const ImageIO::MetadataStrings& strings )
{
m_metadata.metadata = strings;
}
void ImageToolsBitmap::ClearMetadata()
{
m_metadata = ImageIO::Metadata();
}