1 // Copyright 2020 Intel Corporation 2 // SPDX-License-Identifier: Apache-2.0 3 4 #include "ImageExporter.h" 5 // rkcommon 6 #include "rkcommon/os/FileName.h" 7 // stb 8 #include "stb_image_write.h" 9 10 namespace ospray { 11 namespace sg { 12 13 struct PNGExporter : public ImageExporter 14 { 15 PNGExporter() = default; 16 ~PNGExporter() = default; 17 18 void doExport() override; 19 }; 20 21 OSP_REGISTER_SG_NODE_NAME(PNGExporter, exporter_png); 22 23 // PNGExporter definitions ////////////////////////////////////////////////// 24 doExport()25 void PNGExporter::doExport() 26 { 27 auto file = FileName(child("file").valueAs<std::string>()); 28 29 if (child("data").valueAs<const void *>() == nullptr) { 30 std::cerr << "Warning: image data null; not exporting" << std::endl; 31 return; 32 } 33 34 std::string format = child("format").valueAs<std::string>(); 35 if (format == "float") { 36 std::cerr << "Warning: saving a 32-bit float buffer as PNG; color space " 37 "will be limited." 38 << std::endl; 39 floatToChar(); 40 } 41 42 vec2i size = child("size").valueAs<vec2i>(); 43 const void *fb = child("data").valueAs<const void *>(); 44 stbi_flip_vertically_on_write(1); 45 int res = stbi_write_png(file.c_str(), size.x, size.y, 4, fb, 4 * size.x); 46 47 if (res == 0) 48 std::cerr << "STBI error; could not save image" << std::endl; 49 else 50 std::cout << "Saved to " << file << std::endl; 51 } 52 53 } // namespace sg 54 } // namespace ospray 55