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 138 of file STBImageWriter.cpp.

142{
143 const auto* src = data.as_uint8();
144 if (!src) {
145 m_last_error = "PNG requires uint8 ImageData";
147 return false;
148 }
149
150 const int compression = (options.compression >= 0 && options.compression <= 9)
151 ? options.compression
152 : 6;
153 stbi_write_png_compression_level = compression;
154 stbi_flip_vertically_on_write(options.flip_vertically ? 1 : 0);
155
156 std::vector<uint8_t> encoded;
157 encoded.reserve(static_cast<size_t>(data.width) * data.height * data.channels);
158
159 const int stride = static_cast<int>(data.width * data.channels);
160 const int ok = stbi_write_png_to_func(
161 &stbi_memory_writer,
162 &encoded,
163 static_cast<int>(data.width),
164 static_cast<int>(data.height),
165 static_cast<int>(data.channels),
166 src->data(),
167 stride);
168
169 if (!ok || encoded.empty()) {
170 m_last_error = "stbi_write_png_to_func failed";
172 return false;
173 }
174
175 if (!flush_to_disk(filepath, encoded)) {
176 m_last_error = "Failed to write PNG file: " + filepath;
178 return false;
179 }
180
182 "Wrote PNG: {} ({}x{}, {} channels, {} bytes)",
183 filepath, data.width, data.height, data.channels, encoded.size());
184 return true;
185}
#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: