Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 09:12:19

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