mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 19:45:10 +00:00
LibGfx: Extraction of Streamer from P*MLoader
Problem: - `Streamer` is the same in [PBM,PGM,PPM]Loader class implementations. Solution: - Extract it to its own header file to reduce maintenance burden. - Implement `read` in terms of `read_bytes` to make the class "DRY". - Decorate all functions with `constexpr`.
This commit is contained in:
parent
9ca34c3047
commit
58c52b155a
4 changed files with 81 additions and 126 deletions
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include "PPMLoader.h"
|
||||
#include "Streamer.h"
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/MappedFile.h>
|
||||
|
@ -64,48 +65,6 @@ struct PPMLoadingContext {
|
|||
RefPtr<Gfx::Bitmap> bitmap;
|
||||
};
|
||||
|
||||
class Streamer {
|
||||
public:
|
||||
Streamer(const u8* data, size_t size)
|
||||
: m_data_ptr(data)
|
||||
, m_size_remaining(size)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool read(T& value)
|
||||
{
|
||||
if (m_size_remaining < sizeof(T))
|
||||
return false;
|
||||
value = *((const NetworkOrdered<T>*)m_data_ptr);
|
||||
m_data_ptr += sizeof(T);
|
||||
m_size_remaining -= sizeof(T);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_bytes(u8* buffer, size_t count)
|
||||
{
|
||||
if (m_size_remaining < count)
|
||||
return false;
|
||||
memcpy(buffer, m_data_ptr, count);
|
||||
m_data_ptr += count;
|
||||
m_size_remaining -= count;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool at_end() const { return !m_size_remaining; }
|
||||
|
||||
void step_back()
|
||||
{
|
||||
m_data_ptr -= 1;
|
||||
m_size_remaining += 1;
|
||||
}
|
||||
|
||||
private:
|
||||
const u8* m_data_ptr { nullptr };
|
||||
size_t m_size_remaining { 0 };
|
||||
};
|
||||
|
||||
ALWAYS_INLINE static Color adjust_color(u16 max_val, Color& color)
|
||||
{
|
||||
color.set_red((color.red() * 255) / max_val);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue