-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmpto16.c
More file actions
85 lines (73 loc) · 1.76 KB
/
bmpto16.c
File metadata and controls
85 lines (73 loc) · 1.76 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "image16c.h"
#include "bmp.h"
int main(int argc, char** argv) {
FILE* bmpFile;
FILE* pltFile;
FILE* outFile;
uint32_t pltData[IMAGE16C_COLORCT];
uint32_t w, h;
uint8_t* data24bpp;
uint8_t* data16c;
size_t nBytes;
if(argc < 4) {
puts("Usage: bmpto16 <image.bmp> <palette.bin> <output.bin>");
puts("\t<palette.bin> = ##GEN to generate automatically.");
return 1;
}
bmpFile = fopen(argv[1], "rb");
if(!bmpFile || ferror(bmpFile)) {
puts("Failed to open bitmap file.");
return 1;
}
if(!strcmp(argv[2], "##GEN"))
pltFile = NULL;
else {
pltFile = fopen(argv[2], "rb");
if(!pltFile || ferror(pltFile)) {
puts("Failed to open palette file.");
return 1;
}
}
outFile = fopen(argv[3], "wb");
if(!outFile || ferror(outFile)) {
puts("Failed to open output file.");
return 1;
}
data24bpp = BMPRead(bmpFile, (int32_t*)&w, (int32_t*)&h, NULL, 0);
if(!data24bpp) {
puts("Failed to read bitmap.");
return 1;
}
if(pltFile) {
if(fread(pltData, sizeof(uint32_t), IMAGE16C_COLORCT, pltFile)
!= IMAGE16C_COLORCT) {
puts("Failed to read palette.");
return 1;
}
fclose(pltFile);
} else {
GeneratePaletteFrom24bpp(pltData, w, h, data24bpp,
IMAGE16C_PLTTHRESH_DEFAULT);
}
#ifdef IMAGE16_PACKED
nBytes = (w/2) * h;
#else
nBytes = w * h;
#endif
data16c = malloc(nBytes);
if(!data16c) {
puts("Failed to allocate space for converted image.");
return 1;
}
Convert24bppTo16c(w, h, data24bpp, data16c, pltData);
if(!WriteImage16c(outFile, w, h, pltData, data16c)) {
puts("Failed to write output image.");
return 1;
}
fclose(outFile);
return 0;
}