aboutsummaryrefslogtreecommitdiff
path: root/Src/bmp/rle.cpp
blob: 657ff4116c90506f38c8c09264f548e3d50b07c6 (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "rle.h"
static bool CheckOverflow(size_t total_size, int current_position, int read_size)
{
	if (read_size > (int)total_size) // check separate to avoid overflow
		return true;
	if (((int)total_size - read_size) < current_position)
		return true;
	return false;
}


void RLE16(const uint8_t *rle, size_t rle_size_bytes, uint16_t *video_frame, size_t video_frame_size, int stride)
{
	int input = 0;
	int output = 0;
	video_frame_size >>= 1; // divide by 2 since we're indexing as uint16_t
	int next_line = output + stride;
	while (input < (int)rle_size_bytes && output < (int)video_frame_size)
	{
		if (CheckOverflow(rle_size_bytes, input, 2)) // we always read at least two bytes
			break;

		uint8_t b0 = rle[input++];
		if (b0)
		{
			if (CheckOverflow(rle_size_bytes, input, 2))
				break;

			if (CheckOverflow(video_frame_size, output, b0))
			{
				b0 = (uint8_t)(video_frame_size - output);
			}

			uint16_t pixel = *(uint16_t *)(&rle[input]);
			input += 2;
			while (b0--)
			{
				memcpy(&video_frame[output], &pixel, 2);
				output++;
			}
		}
		else
		{
			uint8_t b1 = rle[input++];
			if (b1 == 0)
			{
				output = next_line;
				next_line = output + stride;
			}
			else if (b1 == 1)
			{
				return;
			}
			else if (b1 == 2)
			{
				if (CheckOverflow(rle_size_bytes, input, 2)) 
					break;

				uint8_t p1 = rle[input++];
				uint8_t p2 = rle[input++];
				output += p1;
				output += p2*stride;
				next_line += p2*stride;
			}
			else
			{
				if (CheckOverflow(rle_size_bytes, input, b1*2))
					break;

				if (CheckOverflow(video_frame_size, output, b1))
					break;
				for (uint8_t i=0;i!=b1;i++)
				{
					video_frame[output++] = *(uint16_t *)(&rle[input]);
					input+=2;
				}
			}
		}
	}
}

void RLE8(const uint8_t *rle, size_t rle_size_bytes, uint8_t *video_frame, size_t video_frame_size, int stride)
{
	int input = 0;
	int output = 0;
	int next_line = output + stride;
	while (input < (int)rle_size_bytes && output < (int)video_frame_size)
	{
		if (CheckOverflow(rle_size_bytes, input, 2)) // we always read at least two bytes
			break;
		uint8_t b0 = rle[input++];
		if (b0)
		{
			if (CheckOverflow(video_frame_size, output, b0))
			{
				b0 = (uint8_t)(video_frame_size - output);
			}

			uint8_t pixel = rle[input++];
			memset(&video_frame[output], pixel, b0);
			output+=b0;
		}
		else
		{
			uint8_t b1 = rle[input++];
			if (b1 == 0)
			{
				output = next_line;
				next_line = output + stride;
			}
			else if (b1 == 1)
			{
				break;
			}
			else if (b1 == 2)
			{
				if (CheckOverflow(rle_size_bytes, input, 2)) 
					break;

				uint8_t p1 = rle[input++];
				uint8_t p2 = rle[input++];
				output += p1;
				output += p2*stride;
				next_line += p2*stride;
			}
			else
			{
				if (CheckOverflow(rle_size_bytes, input, b1))
					break;

				if (CheckOverflow(video_frame_size, output, b1))
					break;
				memcpy(&video_frame[output], &rle[input], b1);
				input += b1;
				output += b1;
				if (b1 & 1)
					input++;
			}
		}
	}
}