FastLED 3.10.8
Loading...
Searching...
No Matches

◆ loadJpeg()

FramePtr fl::FileSystem::loadJpeg ( const char * path,
const JpegConfig & config = JpegConfig(),
fl::string * error_message = nullptr )

Definition at line 104 of file file_system_codecs.cpp.hpp.

105 {
106 fl::ifstream file = openRead(path);
107 if (!file.is_open()) {
108 if (error_message) { *error_message = "Failed to open file: "; error_message->append(path); }
109 FL_WARN("Failed to open JPEG file: " << path); return FramePtr();
110 }
111 fl::size fileSize = file.size();
112 if (fileSize == 0) {
113 if (error_message) { *error_message = "File is empty: "; error_message->append(path); }
114 file.close(); return FramePtr();
115 }
116 fl::vector<u8> buffer;
117 buffer.reserve(fileSize); buffer.resize(fileSize);
118 fl::size bytesRead = 0;
119 while (bytesRead < fileSize && file.available()) {
120 fl::size chunkSize = min<fl::size>(4096, fileSize - bytesRead);
121 fl::size n = file.read(buffer.data() + bytesRead, chunkSize);
122 if (n == 0) break;
123 bytesRead += n;
124 }
125 file.close();
126 if (bytesRead != fileSize) {
127 if (error_message) {
128 *error_message = "Failed to read complete file. Expected ";
129 error_message->append(static_cast<u32>(fileSize));
130 error_message->append(" bytes, got ");
131 error_message->append(static_cast<u32>(bytesRead));
132 }
133 FL_WARN("Failed to read complete JPEG file: " << path); return FramePtr();
134 }
135 fl::span<const u8> jpegData(buffer.data(), buffer.size());
136 FramePtr frame = Jpeg::decode(config, jpegData, error_message);
137 if (!frame && error_message && error_message->empty()) {
138 *error_message = "Failed to decode JPEG from file: "; error_message->append(path);
139 }
140 return frame;
141}
fl::ifstream openRead(const char *path) FL_NO_EXCEPT
Definition fs.cpp.hpp:170
static bool decode(const JpegConfig &config, fl::span< const fl::u8 > data, Frame *frame, fl::string *error_message=nullptr) FL_NO_EXCEPT
Definition jpeg.cpp.hpp:296
bool empty() const FL_NO_EXCEPT
fl::size_t size() const FL_NO_EXCEPT
Definition fstream.h:146
ifstream & read(char *buffer, fl::size_t count) FL_NO_EXCEPT
bool available() const FL_NO_EXCEPT
Definition fstream.h:161
bool is_open() const FL_NO_EXCEPT
Definition fstream.h:111
void close() FL_NO_EXCEPT
string & append(const bitset_fixed< N > &bs) FL_NO_EXCEPT
Definition string.h:293
fl::size size() const FL_NO_EXCEPT
void reserve(fl::size n) FL_NO_EXCEPT
Definition vector.h:591
void resize(fl::size n) FL_NO_EXCEPT
Definition vector.h:593
T * data() FL_NO_EXCEPT
Definition vector.h:619
#define FL_WARN(...)
Definition log.h:437
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71

References fl::ifstream::available(), fl::ifstream::close(), fl::vector< T >::data(), fl::Jpeg::decode(), fl::FL_NO_EXCEPT, FL_WARN, fl::ifstream::is_open(), fl::min(), openRead(), fl::ifstream::read(), fl::vector< T >::reserve(), fl::vector< T >::resize(), fl::ifstream::size(), and fl::vector_basic::size().

Referenced by fl::loadJpegFromSD().

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