MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ write_png()

bool MayaFlux::IO::STBImageWriter::write_png ( const std::string &  filepath,
const ImageData data,
const ImageWriteOptions options 
)
private

Definition at line 135 of file STBImageWriter.cpp.

139{
140 const auto* src = data.as_uint8();
141 if (!src) {
142 m_last_error = "PNG requires uint8 ImageData";
144 return false;
145 }
146
147 const int compression = (options.compression >= 0 && options.compression <= 9)
148 ? options.compression
149 : 6;
150 stbi_write_png_compression_level = compression;
151 stbi_flip_vertically_on_write(options.flip_vertically ? 1 : 0);
152
153 std::vector<uint8_t> encoded;
154 encoded.reserve(static_cast<size_t>(data.width) * data.height * data.channels);
155
156 const int stride = static_cast<int>(data.width * data.channels);
157 const int ok = stbi_write_png_to_func(
158 &stbi_memory_writer,
159 &encoded,
160 static_cast<int>(data.width),
161 static_cast<int>(data.height),
162 static_cast<int>(data.channels),
163 src->data(),
164 stride);
165
166 if (!ok || encoded.empty()) {
167 m_last_error = "stbi_write_png_to_func failed";
169 return false;
170 }
171
172 if (!flush_to_disk(filepath, encoded)) {
173 m_last_error = "Failed to write PNG file: " + filepath;
175 return false;
176 }
177
179 "Wrote PNG: {} ({}x{}, {} channels, {} bytes)",
180 filepath, data.width, data.height, data.channels, encoded.size());
181 return true;
182}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.

References MayaFlux::IO::ImageData::as_uint8(), MayaFlux::IO::ImageData::channels, MayaFlux::IO::ImageWriteOptions::compression, MayaFlux::Journal::FileIO, MayaFlux::IO::ImageWriteOptions::flip_vertically, MayaFlux::IO::ImageData::height, MayaFlux::Journal::IO, m_last_error, MF_ERROR, MF_INFO, and MayaFlux::IO::ImageData::width.

Referenced by write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: