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

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