@@ -40,24 +40,42 @@ llvm::Error WritePNG(llvm::StringRef OutputPath, const Resource &R) {
40
40
// Translate pixel data into uint8_t.
41
41
uint64_t Stride = R.getSingleElementSize ();
42
42
uint64_t PixelComponents = R.Size / Stride;
43
- std::unique_ptr<uint8_t []> PixelData =
44
- std::make_unique<uint8_t []>(PixelComponents * (Depth / 8 ));
43
+ std::unique_ptr<uint8_t []> TranslatedPixels;
44
+ bool PixelsNeedTranslation =
45
+ R.Format == DataFormat::Float32 || R.Format == DataFormat::Float64;
46
+ if (PixelsNeedTranslation)
47
+ TranslatedPixels =
48
+ std::make_unique<uint8_t []>(PixelComponents * (Depth / 8 ));
45
49
46
50
switch (R.Format ) {
47
51
case DataFormat::Float32:
48
52
if (Depth == 16 )
49
- TranslatePixelData (reinterpret_cast <uint16_t *>(PixelData .get ()),
53
+ TranslatePixelData (reinterpret_cast <uint16_t *>(TranslatedPixels .get ()),
50
54
reinterpret_cast <const float *>(R.Data .get ()),
51
55
PixelComponents);
52
56
else
53
- TranslatePixelData (reinterpret_cast <uint8_t *>(PixelData .get ()),
57
+ TranslatePixelData (reinterpret_cast <uint8_t *>(TranslatedPixels .get ()),
54
58
reinterpret_cast <const float *>(R.Data .get ()),
55
59
PixelComponents);
56
60
break ;
61
+ case DataFormat::Float64:
62
+ if (Depth == 16 )
63
+ TranslatePixelData (reinterpret_cast <uint16_t *>(TranslatedPixels.get ()),
64
+ reinterpret_cast <const double *>(R.Data .get ()),
65
+ PixelComponents);
66
+ else
67
+ TranslatePixelData (reinterpret_cast <uint8_t *>(TranslatedPixels.get ()),
68
+ reinterpret_cast <const double *>(R.Data .get ()),
69
+ PixelComponents);
70
+ break ;
57
71
default :
58
72
llvm_unreachable (" Unsupported format for png output" );
59
73
}
60
74
75
+ uint8_t *PixelData = PixelsNeedTranslation
76
+ ? TranslatedPixels.get ()
77
+ : reinterpret_cast <uint8_t *>(R.Data .get ());
78
+
61
79
png_structp PNG =
62
80
png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL , NULL , NULL ); // 8
63
81
if (!PNG)
@@ -95,7 +113,7 @@ llvm::Error WritePNG(llvm::StringRef OutputPath, const Resource &R) {
95
113
uint64_t DepthBytes = Depth / 8 ;
96
114
uint64_t RowSize = Width * R.Channels * DepthBytes;
97
115
// Step one row back from the end
98
- uint8_t *Row = PixelData. get () + (PixelComponents * DepthBytes) - RowSize;
116
+ uint8_t *Row = PixelData + (PixelComponents * DepthBytes) - RowSize;
99
117
for (int I = 0 ; I < Height; ++I, Row -= RowSize)
100
118
Rows[I] = (png_bytep)Row;
101
119
0 commit comments