-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw2dng.c
117 lines (106 loc) · 4.34 KB
/
raw2dng.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <byteswap.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
unsigned char DNG_HEADER[256 + 61] = {
0x49,0x49,0x2A,0x00,0x08,0x00,0x00,0x00,0x04,0x00,0x12,0xC6,0x01,0x00,0x04,0x00,
0x00,0x00,0x01,0x01,0x00,0x00,0x13,0xC6,0x01,0x00,0x04,0x00,0x00,0x00,0x01,0x01,
0x00,0x00,0x4A,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x50,0x00,0x00,0x00,0x14,0xC6,
0x02,0x00,0x11,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x46,0x65,
0x69,0x63,0x68,0x65,0x6E,0x20,0x49,0x4D,0x58,0x33,0x33,0x34,0x4C,0x51,0x52,0x00,
0x0E,0x00,0xFE,0x00,0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
0x03,0x00,0x01,0x00,0x00,0x00,0x80,0x10,0x00,0x00,0x01,0x01,0x03,0x00,0x01,0x00,
0x00,0x00,0x78,0x0C,0x00,0x00,0x02,0x01,0x01,0x00,0x01,0x00,0x00,0x00,0x0C,0x00,
0x00,0x00,0x03,0x01,0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x06,0x01,
0x03,0x00,0x01,0x00,0x00,0x00,0x23,0x80,0x00,0x00,0x11,0x01,0x04,0x00,0x01,0x00,
0x00,0x00,0x00,0x01,0x00,0x00,0x15,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x01,0x00,
0x00,0x00,0x17,0x01,0x04,0x00,0x01,0x00,0x00,0x00,0x00,0x9A,0x34,0x01,0x1C,0x01,
0x03,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x8D,0x82,0x03,0x00,0x02,0x00,
0x00,0x00,0x02,0x00,0x02,0x00,0x8E,0x82,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x01,
0x01,0x02,0x1A,0xC6,0x03,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1D,0xC6,
0x03,0x00,0x01,0x00,0x00,0x00,0xFF,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xd8,
0xff,0xc3,0x00,0x0b, //Start of Frame
0x0C, //Bitdepth
0x0c,0x7a, //Height
0x10,0x80, //Width
0x01, //No. of Components
0x00,0x11,0x00,
0xff,0xc4,0x00,0x20, //Define Huffman
0x00, //Table index
0x00,0x01,0x04,0x02,0x03,0x01,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x05,0x04,0x06,0x03,0x07,0x02,0x08,0x01,0x09,0x00,0x0a,0x0b,0x0c, //Leaf Value
0,0,0,0,0,0,0,0,0,0
};
void update_dng_header(unsigned short width, unsigned short height, unsigned char bitdepth, bool comp) {
*(unsigned short*) (DNG_HEADER + 0x66) = width;
*(unsigned short*) (DNG_HEADER + 0x72) = height;
DNG_HEADER[0x7E] = bitdepth;
*(unsigned int*) (DNG_HEADER + 0xBA) = (width * height * bitdepth) >> 3;
// Black level
*(unsigned short*) (DNG_HEADER + 0xEA) = 15;
// White level
*(unsigned short*) (DNG_HEADER + 0xF6) = (1 << bitdepth) - 1;
unsigned char * LJPG_HEADER = DNG_HEADER + 256;
if (comp) {
LJPG_HEADER[6] = bitdepth;
LJPG_HEADER[7] = height >> 8;
LJPG_HEADER[8] = height & 0xFF;
LJPG_HEADER[9] = width >> 8;
LJPG_HEADER[10] = width & 0xFF;
LJPG_HEADER[18] = 20 + bitdepth;
}
}
int main(int argc, char **argv) {
int res;
unsigned short width, height;
width = atoi(argv[1]);
height = atoi(argv[2]);
unsigned char bitdepth = atoi(argv[3]);
char * raw_fname = argv[4];
char * dng_fname = argv[5];
FILE * raw_file = fopen(raw_fname, "rb");
unsigned short * raw_buf = malloc(width * height * sizeof(unsigned short));
fread(raw_buf, sizeof(unsigned short), width * height, raw_file);
unsigned char * out_buf = malloc((width * height * bitdepth) >> 3);
int j = 0;
unsigned char bits = 0;
unsigned short bitstream = 0;
unsigned int hist[4096] = {0};
for (int i = 0; i < width * height; i++) {
// Left justified 16 bit pixel
unsigned short pix_val = __bswap_16(raw_buf[i]);
hist[pix_val >> 4]++;
bitstream |= pix_val >> bits;
bits += 12;
while (bits >= 8) {
out_buf[j] = bitstream >> 8;
bitstream <<= 8;
bits -= 8;
j++;
}
}
free(raw_buf);
update_dng_header(width, height, bitdepth, 0);
FILE * outfile = fopen(dng_fname, "wb");
fwrite(DNG_HEADER, 1, 256, outfile);
fwrite(out_buf, 1, j, outfile);
free(out_buf);
fclose(outfile);
// Analyze histogram
float total_val = 0.0;
int highlight_count = 0;
int one_thousandths = width * height / 10000;
for (int i = 4095; i >= 0; i--) {
if (hist[i]) {
highlight_count += hist[i];
total_val += ((float) hist[i]) * i;
if (highlight_count > one_thousandths) {
total_val /= highlight_count;
printf("%f\n", total_val);
break;
}
}
}
return 0;
}