Skip to content

Commit 587340e

Browse files
committed
Handle more broad pixel transformations
This handles pixel data transformation across a wider array of pixel types. At some point this may make sense ot move to a GPU kernel for more efficient exectuion.
1 parent 681d94f commit 587340e

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

tools/offloader/WritePNG.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,42 @@ llvm::Error WritePNG(llvm::StringRef OutputPath, const Resource &R) {
4040
// Translate pixel data into uint8_t.
4141
uint64_t Stride = R.getSingleElementSize();
4242
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));
4549

4650
switch (R.Format) {
4751
case DataFormat::Float32:
4852
if (Depth == 16)
49-
TranslatePixelData(reinterpret_cast<uint16_t *>(PixelData.get()),
53+
TranslatePixelData(reinterpret_cast<uint16_t *>(TranslatedPixels.get()),
5054
reinterpret_cast<const float *>(R.Data.get()),
5155
PixelComponents);
5256
else
53-
TranslatePixelData(reinterpret_cast<uint8_t *>(PixelData.get()),
57+
TranslatePixelData(reinterpret_cast<uint8_t *>(TranslatedPixels.get()),
5458
reinterpret_cast<const float *>(R.Data.get()),
5559
PixelComponents);
5660
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;
5771
default:
5872
llvm_unreachable("Unsupported format for png output");
5973
}
6074

75+
uint8_t *PixelData = PixelsNeedTranslation
76+
? TranslatedPixels.get()
77+
: reinterpret_cast<uint8_t *>(R.Data.get());
78+
6179
png_structp PNG =
6280
png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); // 8
6381
if (!PNG)
@@ -95,7 +113,7 @@ llvm::Error WritePNG(llvm::StringRef OutputPath, const Resource &R) {
95113
uint64_t DepthBytes = Depth / 8;
96114
uint64_t RowSize = Width * R.Channels * DepthBytes;
97115
// Step one row back from the end
98-
uint8_t *Row = PixelData.get() + (PixelComponents * DepthBytes) - RowSize;
116+
uint8_t *Row = PixelData + (PixelComponents * DepthBytes) - RowSize;
99117
for (int I = 0; I < Height; ++I, Row -= RowSize)
100118
Rows[I] = (png_bytep)Row;
101119

0 commit comments

Comments
 (0)