-
Notifications
You must be signed in to change notification settings - Fork 13
/
image.h
59 lines (50 loc) · 1.8 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
/*
This file is part of the Fairytale project
Copyright (C) 2018 Márcio Pais
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IMAGE_H
#define IMAGE_H
#include "stream.h"
class Image {
public:
static bool isGrayscalePalette(Stream* input, const int n = 256, const bool isRGBA = false) {
off_t offset = input->curPos();
int stride = 3 + (int)isRGBA, res = (n > 0) << 8, order = 1;
for (int i = 0; i < n * stride && (res >> 8) > 0; i++) {
int b = input->getChar();
if (b == EOF) {
res = 0;
break;
}
if (i == 0) {
res = 0x100 | b;
order = 1 - 2 * (b > 0);
continue;
}
//"j" is the index of the current byte in this color entry
int j = i % stride;
if (j == 0) {
// load first component of this entry
res = (res & ((b - (res & 0xFF) == order) << 8));
res |= (res) ? b : 0;
}
else if (j == 3)
res &= ((b == 0 || b == 0xFF) * 0x1FF); // alpha/attribute component must be zero or 0xFF
else
res &= ((b == (res & 0xFF)) * 0x1FF);
}
input->setPos(offset);
return (res >> 8) > 0;
}
};
#endif