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

◆ write_jpg()

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

Definition at line 187 of file STBImageWriter.cpp.

191{
192 const auto* src = data.as_uint8();
193 if (!src) {
194 m_last_error = "JPG requires uint8 ImageData";
196 return false;
197 }
198
199 if (data.channels != 1 && data.channels != 3) {
200 m_last_error = "JPG supports only 1 or 3 channels (got "
201 + std::to_string(data.channels) + ")";
203 return false;
204 }
205
206 const int quality = std::clamp(options.quality, 1, 100);
207 stbi_flip_vertically_on_write(options.flip_vertically ? 1 : 0);
208
209 std::vector<uint8_t> encoded;
210 const int ok = stbi_write_jpg_to_func(
211 &stbi_memory_writer,
212 &encoded,
213 static_cast<int>(data.width),
214 static_cast<int>(data.height),
215 static_cast<int>(data.channels),
216 src->data(),
217 quality);
218
219 if (!ok || encoded.empty()) {
220 m_last_error = "stbi_write_jpg_to_func failed";
222 return false;
223 }
224
225 if (!flush_to_disk(filepath, encoded)) {
226 m_last_error = "Failed to write JPG file: " + filepath;
228 return false;
229 }
230
232 "Wrote JPG: {} ({}x{}, quality {}, {} bytes)",
233 filepath, data.width, data.height, quality, encoded.size());
234 return true;
235}
#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::Journal::FileIO, MayaFlux::IO::ImageWriteOptions::flip_vertically, MayaFlux::IO::ImageData::height, MayaFlux::Journal::IO, m_last_error, MF_ERROR, MF_INFO, MayaFlux::IO::ImageWriteOptions::quality, and MayaFlux::IO::ImageData::width.

Referenced by write().

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