Last active
March 16, 2020 17:53
-
-
Save ciniml/bcba794112681b32dc88052c382c27ac to your computer and use it in GitHub Desktop.
draw_jpg with buffer support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct JpegDecodeBuffer | |
{ | |
std::uint8_t body[4096]; | |
}; | |
bool draw_jpg(DataWrapper* data, int16_t x, int16_t y, int16_t maxWidth, int16_t maxHeight, int16_t offX, int16_t offY, jpeg_div_t scale, JpegDecodeBuffer& decodeBuffer) | |
{ | |
//... | |
auto jres = jd_prepare(&jpegdec, jpg_read_data, decodeBuffer.body, sz_pool, &jpeg); | |
//... | |
} | |
// User can select where the buffer is allocated from. | |
// The buffer is allocated from the stack by default. | |
bool drawJpgFile( fs::FS &fs, const char *path, int16_t x=0, int16_t y=0, int16_t maxWidth=0, int16_t maxHeight=0, int16_t offX=0, int16_t offY=0, jpeg_div_t scale=JPEG_DIV_NONE) { | |
JpegDecodeBuffer decodeBuffer; | |
return drawJpgFile(fs, path, x, y, maxWidth, maxHeight, offX, offY, scale, decodeBuffer); | |
} | |
bool drawJpgFile( fs::FS &fs, const char *path, int16_t x=0, int16_t y=0, int16_t maxWidth=0, int16_t maxHeight=0, int16_t offX=0, int16_t offY=0, jpeg_div_t scale=JPEG_DIV_NONE, JpegDecodeBuffer& decodeBuffer) { | |
bool res; | |
FileWrapper file; | |
file.setFS(fs); | |
// drawJpgFile(&file, path, x, y, maxWidth, maxHeight, offX, offY, scale); | |
file.need_transaction &= this->_has_transaction; | |
if (file.need_transaction) this->endTransaction(); | |
if (file.open(path, "rb")) { | |
DecodeBufferType decodeBuffer; // Allocate a decode buffer. | |
res = draw_jpg(&file, x, y, maxWidth, maxHeight, offX, offY, scale, decodeBuffer); | |
file.close(); | |
} | |
if (file.need_transaction && this->_transaction_count) { this->beginTransaction(); } | |
return res; | |
} | |
drawJpgFile(...); // Allocate decode buffer from the stack. | |
auto decodeBuffer = new JpegDecodeBuffer() // Allocate decode buffer from the heap. | |
drawJpgFile(..., *decodeBuffer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct StaticJpegDecodeBuffer | |
{ | |
std::uint8_t body[4096]; | |
std::uint8_t* get() const { return this->body; } | |
}; | |
class DynamicJpegDecodeBuffer | |
{ | |
public: | |
DynamicJpegDecodeBuffer() : buffer(nullptr) { | |
this->buffer = new StaticJpegDecodeBuffer(); | |
} | |
~DynamicJpegDecodeBuffer() { | |
if(this->buffer != nullptr) { | |
delete this->buffer; | |
this->buffer = nullptr; | |
} | |
} | |
std::uint8_t* get() const { return this->buffer->get(); } | |
private: | |
StaticJpegDecodeBuffer* buffer; | |
}; | |
template<typename DecodeBufferType> | |
bool draw_jpg(DataWrapper* data, int16_t x, int16_t y, int16_t maxWidth, int16_t maxHeight, int16_t offX, int16_t offY, jpeg_div_t scale, DecodeBufferType& decodeBuffer) | |
{ | |
//... | |
auto jres = jd_prepare(&jpegdec, jpg_read_data, decodeBuffer.get(), sz_pool, &jpeg); | |
//... | |
} | |
// User can select where the buffer is allocated from. | |
// The buffer is allocated from the stack by default. | |
template<typename DecodeBufferType = StaticJpegDecodeBuffer> | |
bool drawJpgFile( fs::FS &fs, const char *path, int16_t x=0, int16_t y=0, int16_t maxWidth=0, int16_t maxHeight=0, int16_t offX=0, int16_t offY=0, jpeg_div_t scale=JPEG_DIV_NONE) { | |
bool res; | |
FileWrapper file; | |
file.setFS(fs); | |
// drawJpgFile(&file, path, x, y, maxWidth, maxHeight, offX, offY, scale); | |
file.need_transaction &= this->_has_transaction; | |
if (file.need_transaction) this->endTransaction(); | |
if (file.open(path, "rb")) { | |
DecodeBufferType decodeBuffer; // Allocate a decode buffer. | |
res = draw_jpg(&file, x, y, maxWidth, maxHeight, offX, offY, scale, decodeBuffer); | |
file.close(); | |
} | |
if (file.need_transaction && this->_transaction_count) { this->beginTransaction(); } | |
return res; | |
} | |
drawJpgFile(...); // Allocate decode buffer from the stack. | |
drawJpgFile<DynamicDecodeBuffer>(...); // Allocate decode buffer from the heap. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment