-
Notifications
You must be signed in to change notification settings - Fork 0
/
RawImageTranslator.c
78 lines (60 loc) · 1.95 KB
/
RawImageTranslator.c
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
/*
Written in 2016 by Steven Valsesia <steven.valsesia@gmail.com>
This program convert 24-bit raw RGB pixels to 16-bit BGR565 pixels
RawImageTranslator file.rgb888 file.bgr565
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv)
{
FILE *input, *output;
unsigned long input_l=0, i=0;
unsigned short bgr565;
unsigned char buf[3], red, green, blue;
if(argc < 3) {
printf("Usage: %s [input RGB888] [output BGR565]\n", argv[0]);
return 0;
}
input = fopen(argv[1], "r");
if(input == NULL) {
perror("fopen");
return -1;
}
output = fopen(argv[2], "w");
if(output == NULL) {
perror("fopen");
fclose(input);
return -1;
}
// Get input file length
rewind(input);
fseek(input, 0, SEEK_END);
input_l = ftell(input);
rewind(input);
for(i=0 ; i < input_l ; i+=3) {
fread(buf, 1, 3, input);
red = buf[0];
green = buf[1];
blue = buf[2];
// This part was under CC0 Licenses and was written by Glenn Randers-Pehrson
bgr565 = (unsigned short)(blue * 31.0 / 255.0) |
(unsigned short)(green * 63.0 / 255.0) << 5 |
(unsigned short)(red * 31.0 / 255.0) << 11;
fwrite(&bgr565, 2, 1, output);
}
printf("Done, %s : %ld bytes\n", argv[2], ftell(output));
printf("Have fun\n");
fclose(input);
fclose(output);
return 0;
}