Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:59:51

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file geocel/rasterize/ImageWriter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 #include <vector>
0012 
0013 #include "corecel/Config.hh"
0014 
0015 #include "corecel/cont/Array.hh"
0016 #include "corecel/cont/Span.hh"
0017 #include "geocel/Types.hh"
0018 
0019 #include "Color.hh"
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 /*!
0025  * Write a 2D array of colors as a PNG file.
0026  *
0027  * Each row is written progressively. All rows must be written. Currently alpha
0028  * values are ignored due to my poor understanding of libpng.
0029  */
0030 class ImageWriter
0031 {
0032   public:
0033     //!@{
0034     //! \name Type aliases
0035     using SpanColor = Span<Color>;
0036     //!@}
0037 
0038   public:
0039     // Construct with a filename and dimensions
0040     ImageWriter(std::string const& filename, Size2 height_width);
0041 
0042     // Close on destruction
0043     inline ~ImageWriter();
0044 
0045     // Write a row
0046     void operator()(Span<Color const>);
0047 
0048     //! Close the file early so that exceptions can be caught
0049     void close() { this->close_impl(/* validate = */ true); }
0050 
0051     //! Whether the output is available for writing
0052     explicit operator bool() const { return static_cast<bool>(impl_); }
0053 
0054   private:
0055     struct Impl;
0056     struct ImplDeleter
0057     {
0058         void operator()(Impl*);
0059     };
0060 
0061     std::unique_ptr<Impl, ImplDeleter> impl_;
0062     Size2 size_;
0063     size_type rows_written_{0};
0064     std::vector<char> row_buffer_;
0065 
0066     void close_impl(bool validate);
0067 };
0068 
0069 //---------------------------------------------------------------------------//
0070 // INLINE DEFINITIONS
0071 //---------------------------------------------------------------------------//
0072 /*!
0073  * Close on destruction.
0074  */
0075 ImageWriter::~ImageWriter()
0076 {
0077     if (*this)
0078     {
0079         this->close_impl(/* validate = */ false);
0080     }
0081 }
0082 
0083 //---------------------------------------------------------------------------//
0084 #if !CELERITAS_USE_PNG
0085 inline ImageWriter::ImageWriter(std::string const&, Size2)
0086 {
0087     CELER_DISCARD(size_);
0088     CELER_DISCARD(rows_written_);
0089     CELER_DISCARD(row_buffer_);
0090     CELER_NOT_CONFIGURED("PNG");
0091 }
0092 inline void ImageWriter::operator()(Span<Color const>)
0093 {
0094     CELER_ASSERT_UNREACHABLE();
0095 }
0096 inline void ImageWriter::close_impl(bool) {}
0097 inline void ImageWriter::ImplDeleter::operator()(Impl*) {}
0098 #endif
0099 
0100 //---------------------------------------------------------------------------//
0101 }  // namespace celeritas