-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbitmap_decoder.cpp
More file actions
121 lines (106 loc) · 3.24 KB
/
bitmap_decoder.cpp
File metadata and controls
121 lines (106 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifdef RIVE_RENDERER_TESS
#include "viewer/tess/bitmap_decoder.hpp"
#include <cstring>
Bitmap::Bitmap(uint32_t width,
uint32_t height,
PixelFormat pixelFormat,
std::unique_ptr<const uint8_t[]> bytes) :
m_Width(width), m_Height(height), m_PixelFormat(pixelFormat), m_Bytes(std::move(bytes))
{}
Bitmap::Bitmap(uint32_t width, uint32_t height, PixelFormat pixelFormat, const uint8_t* bytes) :
Bitmap(width, height, pixelFormat, std::unique_ptr<const uint8_t[]>(bytes))
{}
size_t Bitmap::bytesPerPixel(PixelFormat format) const
{
switch (format)
{
case PixelFormat::R:
return 1;
case PixelFormat::RGB:
return 3;
case PixelFormat::RGBA:
return 4;
}
}
size_t Bitmap::byteSize(PixelFormat format) const
{
return m_Width * m_Height * bytesPerPixel(format);
}
size_t Bitmap::byteSize() const { return byteSize(m_PixelFormat); }
std::unique_ptr<Bitmap> DecodePng(rive::Span<const uint8_t> bytes);
std::unique_ptr<Bitmap> DecodeJpeg(rive::Span<const uint8_t> bytes) { return nullptr; }
std::unique_ptr<Bitmap> DecodeWebP(rive::Span<const uint8_t> bytes) { return nullptr; }
using BitmapDecoder = std::unique_ptr<Bitmap> (*)(rive::Span<const uint8_t> bytes);
struct ImageFormat
{
const char* name;
uint8_t fingerprintSize;
uint8_t fingerprint[4];
BitmapDecoder decodeImage;
};
std::unique_ptr<Bitmap> Bitmap::decode(rive::Span<const uint8_t> bytes)
{
static ImageFormat decoders[] = {
{
"png", 4,
{0x89, 0x50, 0x4E, 0x47},
DecodePng,
},
{
"jpeg", 3,
{0xFF, 0xD8, 0xFF},
DecodeJpeg,
},
{
"webp", 3,
{0x52, 0x49, 0x46},
DecodeWebP,
},
};
for (auto recognizer : decoders)
{
auto& fingerprint = recognizer.fingerprint;
// Immediately discard decoders with fingerprints that are longer than
// the file buffer.
if (recognizer.fingerprintSize > bytes.size())
{
continue;
}
// If the fingerprint doesn't match, discrd this decoder. These are all bytes so .size() is
// fine here.
if (std::memcmp(recognizer.fingerprint, bytes.data(), recognizer.fingerprintSize) != 0)
{
continue;
}
auto bitmap = recognizer.decodeImage(bytes);
if (!bitmap)
{
fprintf(stderr, "Bitmap::decode - failed to decode a %s.\n", recognizer.name);
}
return bitmap;
}
return nullptr;
}
void Bitmap::pixelFormat(PixelFormat format)
{
if (format == m_PixelFormat)
{
return;
}
auto nextByteSize = byteSize(format);
auto nextBytes = std::unique_ptr<uint8_t[]>(new uint8_t[nextByteSize]);
auto fromBytesPerPixel = bytesPerPixel(m_PixelFormat);
auto toBytesPerPixel = bytesPerPixel(format);
int writeIndex = 0;
int readIndex = 0;
for (int i = 0; i < m_Width * m_Height; i++)
{
for (int j = 0; j < toBytesPerPixel; j++)
{
nextBytes[writeIndex++] = j < fromBytesPerPixel ? m_Bytes[readIndex++] : 255;
}
}
m_Bytes = std::move(nextBytes);
m_PixelFormat = format;
}
#endif