FastLED 3.9.15
Loading...
Searching...
No Matches

◆ loadJpeg()

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

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

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

References fl::string::append(), fl::ifstream::available(), fl::ifstream::close(), fl::vector< T >::data(), fl::Jpeg::decode(), fl::basic_string::empty(), 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: