-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw2ppm.c
93 lines (70 loc) · 2.07 KB
/
raw2ppm.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libraw/libraw.h>
#define BLOCK_SIZE 1024*1024
#define SWAP(a, b) { a ^= b; a ^= (b ^= a); }
void libraw_error(int ret)
{
fprintf(stderr, "libraw %s\n", libraw_strerror(ret));
if (LIBRAW_FATAL_ERROR(ret))
exit(1);
}
int main(int argc, char *argv[])
{
char *buf = NULL;
size_t buf_size = 0;
char rbuf[BLOCK_SIZE];
size_t n;
for (;;) {
n = fread(rbuf, sizeof(char), BLOCK_SIZE, stdin);
buf_size += n;
buf = realloc(buf, buf_size);
memcpy(buf + (buf_size - n), rbuf, n);
if (n < BLOCK_SIZE) {
if (feof(stdin)) {
break;
} else {
fprintf(stderr, "Read error\n");
exit(1);
}
}
}
libraw_data_t *iprc = libraw_init(0);
if (!iprc) {
fprintf(stderr,"Cannot create libraw handle\n");
exit(1);
}
iprc->params.user_mul[0] = 1;
iprc->params.user_mul[1] = 0.5;
iprc->params.user_mul[2] = 1;
iprc->params.user_mul[3] = 0.5;
iprc->params.use_camera_wb = 1;
iprc->params.use_camera_matrix = 1;
iprc->params.highlight = 9;
iprc->params.output_color = 0;
iprc->params.output_bps = 16;
iprc->params.user_qual = 1;
iprc->params.four_color_rgb = 1;
iprc->params.no_auto_bright = 1;
int ret = libraw_open_buffer(iprc, buf, buf_size);
if (ret) libraw_error(ret);
ret = libraw_unpack(iprc);
if (ret) libraw_error(ret);
ret = libraw_dcraw_process(iprc);
if (ret) libraw_error(ret);
libraw_processed_image_t *out;
out = libraw_dcraw_make_mem_image(iprc, &ret);
if (ret) libraw_error(ret);
printf("P6\n%d %d\n%d\n", out->width, out->height, out->bits == 16 ? 65535 : 255);
int i;
for (i = 0; i < out->data_size; i += 2)
SWAP(out->data[i], out->data[i+1]);
n = fwrite(out->data, sizeof(char), out->data_size, stdout);
fflush(stdout);
if (n < out->data_size) {
fprintf(stderr, "Write error\n");
exit(1);
}
return 0;
}