-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.h
61 lines (49 loc) · 1.53 KB
/
image.h
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
#pragma once
#include <cassert>
#include <vector>
// Downscale an image
// using a really dodgy nearest neighbour algorithm
template<typename Pixel>
std::vector<Pixel> downscale_image(Pixel** rows, int sourceWidth, int sourceHeight, int destWidth, int destHeight) {
assert(sourceWidth > destWidth);
assert(sourceHeight > destHeight);
std::vector<Pixel> result;
result.resize(destWidth * destHeight);
for(unsigned i = 0; i < destHeight; i++) {
Pixel* source = rows[sourceWidth * i / destWidth];
Pixel* row = &result[destWidth * i];
for(unsigned j = 0; j < destWidth; j++) {
row[j] = source[sourceHeight * j / destHeight];
}
}
return std::move(result);
}
// For now just mask the highest 3-bits to determine whether to treat a gray
// as white or black
#define GRAY_TO_MONOCHROME(x) (x & 0xc0)
// Packs up to 8 gray pixels into an 8-bit integer
static inline void pack_monochrome_pxls(uint8_t* buffer, uint8_t* grayPixels, unsigned amount) {
uint8_t packed = 0;
while(amount >= 8) {
packed = 0;
int i = 8;
while(i--) {
packed <<= 1;
uint8_t pixel = *(grayPixels++);
if(GRAY_TO_MONOCHROME(pixel)) {
packed |= 1;
}
}
*(buffer++) = packed;
amount -= 8;
}
packed = 0;
while(amount--) {
packed <<= 1;
uint8_t pixel = *(grayPixels++);
if(GRAY_TO_MONOCHROME(pixel)) {
packed |= 1;
}
}
*buffer = packed;
}