-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel.cpp
More file actions
67 lines (58 loc) · 2.79 KB
/
pixel.cpp
File metadata and controls
67 lines (58 loc) · 2.79 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
#include "pixel.h"
Pixel::Pixel(const uint8_t& _red, const uint8_t& _green, const uint8_t& _blue, const uint8_t& _transparency) {
red = _red;
green = _green;
blue = _blue;
transparency = _transparency;
}
Pixel parsePixel(const ScreenInfo& screenInfo, const int& data) {
uint8_t redBits = data >> screenInfo.var.red.offset;
redBits = redBits & createBitMask(screenInfo.var.red.length);
if (screenInfo.var.red.msb_right != 0) reverseByte(redBits);
uint8_t greenBits = data >> screenInfo.var.green.offset;
greenBits = greenBits & createBitMask(screenInfo.var.green.length);
if (screenInfo.var.green.msb_right != 0) reverseByte(greenBits);
uint8_t blueBits = data >> screenInfo.var.blue.offset;
blueBits = blueBits & createBitMask(screenInfo.var.blue.length);
if (screenInfo.var.blue.msb_right != 0) reverseByte(blueBits);
uint8_t transparentBits = data >> screenInfo.var.transp.offset;
transparentBits = transparentBits & createBitMask(screenInfo.var.transp.length);
if (screenInfo.var.transp.msb_right != 0) reverseByte(transparentBits);
redBits = redBits << (8 - screenInfo.var.red.length);
greenBits = greenBits << (8 - screenInfo.var.green.length);
blueBits = blueBits << (8 - screenInfo.var.blue.length);
transparentBits = transparentBits << (8 - screenInfo.var.transp.length);
Pixel pixel{redBits, greenBits, blueBits, transparentBits};
pixel.data = data;
return pixel;
}
Pixel createPixel(const ScreenInfo& screenInfo, const uint8_t& red, const uint8_t& green, const uint8_t& blue, const uint8_t& transparency) {
uint8_t redBits = red >> (8 - screenInfo.var.red.length);
if (screenInfo.var.red.msb_right != 0) reverseByte(redBits);
uint8_t greenBits = green >> (8 - screenInfo.var.green.length);
if (screenInfo.var.green.msb_right != 0) reverseByte(greenBits);
uint8_t blueBits = blue >> (8 - screenInfo.var.blue.length);
if (screenInfo.var.blue.msb_right != 0) reverseByte(blueBits);
uint8_t transparentBits = transparency >> (8 - screenInfo.var.transp.length);
if (screenInfo.var.transp.msb_right != 0) reverseByte(transparentBits);
int pixel = 0;
if (screenInfo.var.red.length > 0) pixel += redBits << screenInfo.var.red.offset;
if (screenInfo.var.green.length > 0) pixel += greenBits << screenInfo.var.green.offset;
if (screenInfo.var.blue.length > 0) pixel += blueBits << screenInfo.var.blue.offset;
if (screenInfo.var.transp.length > 0) pixel += transparentBits << screenInfo.var.transp.offset;
Pixel pixelStruct{red, green, blue, transparency};
pixelStruct.data = pixel;
return pixelStruct;
}
void reverseByte(uint8_t& b) {
b = (b & 0xF1) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCD) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
}
unsigned char createBitMask(__u32 length) {
unsigned char byte = 0;
for (unsigned int i = 0; i < length; i++) {
byte |= (1 << i);
}
return byte;
}