|
| 1 | +//===- Image.cpp - Image Description ----------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include "Image/Image.h" |
| 13 | + |
| 14 | +#include "llvm/ADT/ScopeExit.h" |
| 15 | +#include "llvm/ADT/StringRef.h" |
| 16 | +#include "llvm/Support/Error.h" |
| 17 | +#include "llvm/Support/SwapByteOrder.h" |
| 18 | + |
| 19 | +#include <limits.h> |
| 20 | +#include <png.h> |
| 21 | +#include <stdio.h> |
| 22 | + |
| 23 | +using namespace hlsltest; |
| 24 | + |
| 25 | +template <typename DstType, typename SrcType> |
| 26 | +void TranslatePixelData(Image &Dst, ImageRef Src) { |
| 27 | + uint64_t Pixels = Dst.getHeight() * Dst.getWidth(); |
| 28 | + uint32_t CopiedChannels = std::min(Dst.getChannels(), Src.getChannels()); |
| 29 | + DstType *DstPtr = reinterpret_cast<DstType *>(Dst.data()); |
| 30 | + const SrcType *SrcPtr = reinterpret_cast<const SrcType *>(Src.data()); |
| 31 | + for (uint64_t I = 0; I < Pixels; ++I) { |
| 32 | + for (uint32_t J = 0; J < CopiedChannels; ++J, ++SrcPtr, ++DstPtr) { |
| 33 | + double Tmp = static_cast<double>(*SrcPtr); |
| 34 | + // If the source type is not a float, normalize it between 0 & 1. |
| 35 | + if constexpr (!std::is_floating_point<SrcType>()) |
| 36 | + Tmp /= std::numeric_limits<SrcType>::max(); |
| 37 | + // If the destination type is not a float, scale it into the integer type. |
| 38 | + if constexpr (!std::is_floating_point<DstType>()) |
| 39 | + Tmp *= std::numeric_limits<DstType>::max(); |
| 40 | + |
| 41 | + *DstPtr = static_cast<DstType>(Tmp); |
| 42 | + if constexpr (sizeof(DstType) > 1 && !llvm::sys::IsBigEndianHost) |
| 43 | + llvm::sys::swapByteOrder(*DstPtr); |
| 44 | + } |
| 45 | + // If the destination has more channels fill it with saturated values. |
| 46 | + for (uint32_t J = 0; J < CopiedChannels - Dst.getChannels(); |
| 47 | + ++J, ++DstPtr) { |
| 48 | + if constexpr (std::is_floating_point<DstType>()) |
| 49 | + *DstPtr = 1.0; |
| 50 | + else |
| 51 | + *DstPtr = std::numeric_limits<DstType>::max(); |
| 52 | + } |
| 53 | + // If the source has more channels skip them. |
| 54 | + for (uint32_t J = 0; J < CopiedChannels - Src.getChannels(); ++J, ++SrcPtr) |
| 55 | + ; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +template <typename SrcType> void translatePixelSrc(Image &Dst, ImageRef Src) { |
| 60 | + |
| 61 | + switch (Dst.getDepth()) { |
| 62 | + case 1: |
| 63 | + assert(!Dst.isFloat() && "No float8 support!"); |
| 64 | + TranslatePixelData<uint8_t, SrcType>(Dst, Src); |
| 65 | + break; |
| 66 | + case 2: |
| 67 | + assert(!Dst.isFloat() && "No float16 support!"); |
| 68 | + TranslatePixelData<uint16_t, SrcType>(Dst, Src); |
| 69 | + break; |
| 70 | + default: |
| 71 | + llvm_unreachable("Destination depth out of expected range."); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void translatePixels(Image &Dst, ImageRef Src) { |
| 76 | + switch (Src.getDepth()) { |
| 77 | + case 1: |
| 78 | + assert(!Src.isFloat() && "No float8 support!"); |
| 79 | + translatePixelSrc<uint8_t>(Dst, Src); |
| 80 | + break; |
| 81 | + case 2: |
| 82 | + assert(!Src.isFloat() && "No float16 support!"); |
| 83 | + translatePixelSrc<uint16_t>(Dst, Src); |
| 84 | + break; |
| 85 | + case 4: |
| 86 | + if (Src.isFloat()) |
| 87 | + translatePixelSrc<float>(Dst, Src); |
| 88 | + else |
| 89 | + translatePixelSrc<uint32_t>(Dst, Src); |
| 90 | + break; |
| 91 | + case 8: |
| 92 | + if (Src.isFloat()) |
| 93 | + translatePixelSrc<double>(Dst, Src); |
| 94 | + else |
| 95 | + translatePixelSrc<uint64_t>(Dst, Src); |
| 96 | + break; |
| 97 | + default: |
| 98 | + llvm_unreachable("Source depth out of expected range."); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +Image Image::translateImage(ImageRef Src, uint8_t Depth, uint8_t Channels, |
| 103 | + bool Float) { |
| 104 | + Image NewImage = |
| 105 | + Image(Src.getHeight(), Src.getWidth(), Depth, Channels, Float); |
| 106 | + translatePixels(NewImage, Src); |
| 107 | + return NewImage; |
| 108 | +} |
| 109 | + |
| 110 | +llvm::Error WritePNGImpl(ImageRef Img, llvm::StringRef OutputPath) { |
| 111 | + png_structp PNG = |
| 112 | + png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 113 | + if (!PNG) |
| 114 | + return llvm::createStringError(std::errc::io_error, |
| 115 | + "Failed creating PNG data"); |
| 116 | + |
| 117 | + png_infop PNGInfo = png_create_info_struct(PNG); |
| 118 | + FILE *F = nullptr; |
| 119 | + auto ScopeExit = llvm::make_scope_exit([&PNG, &PNGInfo, &F]() { |
| 120 | + png_destroy_write_struct(&PNG, &PNGInfo); |
| 121 | + if (F) |
| 122 | + fclose(F); |
| 123 | + }); |
| 124 | + if (!PNGInfo) |
| 125 | + return llvm::createStringError(std::errc::io_error, |
| 126 | + "Failed writing PNG info"); |
| 127 | + |
| 128 | + F = fopen(OutputPath.data(), "wb"); |
| 129 | + if (!F) |
| 130 | + return llvm::createStringError(std::errc::io_error, "Failed openiong file"); |
| 131 | + png_init_io(PNG, F); |
| 132 | + png_set_IHDR(PNG, PNGInfo, Img.getWidth(), Img.getHeight(), Img.getBitDepth(), |
| 133 | + PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, |
| 134 | + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 135 | + png_colorp ColorP = |
| 136 | + (png_colorp)png_malloc(PNG, PNG_MAX_PALETTE_LENGTH * sizeof(png_color)); |
| 137 | + if (!ColorP) |
| 138 | + return llvm::createStringError(std::errc::io_error, |
| 139 | + "Failed creating color palette"); |
| 140 | + png_set_PLTE(PNG, PNGInfo, ColorP, PNG_MAX_PALETTE_LENGTH); |
| 141 | + png_write_info(PNG, PNGInfo); |
| 142 | + png_set_packing(PNG); |
| 143 | + |
| 144 | + png_bytepp Rows = |
| 145 | + (png_bytepp)png_malloc(PNG, Img.getHeight() * sizeof(png_bytep)); |
| 146 | + uint64_t RowSize = Img.getWidth() * Img.getChannels() * Img.getDepth(); |
| 147 | + // Step one row back from the end |
| 148 | + const uint8_t *Row = reinterpret_cast<const uint8_t *>(Img.data()) + |
| 149 | + (RowSize * Img.getHeight()) - RowSize; |
| 150 | + for (uint32_t I = 0; I < Img.getHeight(); ++I, Row -= RowSize) |
| 151 | + Rows[I] = const_cast<png_bytep>(Row); |
| 152 | + |
| 153 | + png_write_image(PNG, Rows); |
| 154 | + png_write_end(PNG, PNGInfo); |
| 155 | + png_free(PNG, ColorP); |
| 156 | + return llvm::Error::success(); |
| 157 | +} |
| 158 | + |
| 159 | +llvm::Error Image::WritePNG(ImageRef Img, llvm::StringRef Path) { |
| 160 | + uint32_t NewDepth = std::min(static_cast<uint32_t>(Img.getDepth()), 2u); |
| 161 | + if (Img.isFloat() || Img.getDepth() != NewDepth) { |
| 162 | + Image Translated = translateImage(Img, NewDepth, Img.getChannels(), false); |
| 163 | + return WritePNGImpl(Translated, Path); |
| 164 | + } |
| 165 | + return WritePNGImpl(Img, Path); |
| 166 | +} |
0 commit comments