-
Notifications
You must be signed in to change notification settings - Fork 7
/
bayer2rgb.c
168 lines (123 loc) · 2.83 KB
/
bayer2rgb.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Convert a raw bayer image from mt9p031 at full size into a rgb (bgr)
image for display and also save a jpg version. 10 -> 8 bit conversion
is necessary to use the built-in cvCvtColor(). The implementation of
that algo is standard though and 10 bit could be conserved. This is
just a quick program for testing.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define CV_NO_BACKWARD_COMPATIBILITY
#include <cv.h>
#include <highgui.h>
#define IMAGE_WIDTH 2560
#define IMAGE_HEIGHT 1920
IplImage *load_raw_image(const char *s);
char *load_raw_data(const char *s, int size);
IplImage *convert_image(IplImage *bayer);
void save_image(IplImage *img, const char *orig_file);
int main(int argc, char **argv)
{
IplImage *bayer, *rgb;
if (argc < 2) {
printf("Usage: %s <image file>\n", argv[0]);
exit(1);
}
bayer = load_raw_image(argv[1]);
if (!bayer) {
printf("Failed to load raw image %s\n", argv[1]);
exit(1);
}
rgb = convert_image(bayer);
if (rgb) {
cvNamedWindow("main", CV_WINDOW_AUTOSIZE);
cvMoveWindow("main", 10, 10);
cvShowImage("main", rgb);
cvWaitKey(0);
cvDestroyWindow("main");
save_image(rgb, argv[1]);
}
cvReleaseImage(&bayer);
if (rgb)
cvReleaseImage(&rgb);
return 0;
}
void save_image(IplImage *img, const char *orig_file)
{
char *p;
char *s = strdup(orig_file);
if (!s)
return;
p = strrchr(s, '.');
if (!p)
return;
strcpy(p, ".jpg");
cvSaveImage(s, img, 0);
free(s);
}
IplImage *convert_image(IplImage *bayer)
{
IplImage *dst, *src;
src = cvCreateImage(cvGetSize(bayer), IPL_DEPTH_8U, 1);
if (!src) {
printf("cvCreateImage() - src - failed\n");
return NULL;
}
cvConvertScale(bayer, src, 0.25, 0.0);
dst = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
if (!dst)
printf("cvCreateImage - dst - failed\n");
else {
// this works
// cvCvtColor(src, dst, CV_BayerGR2RGB);
// but we want BGR for cvSaveImage(), so use this
cvCvtColor(src, dst, CV_BayerGB2BGR);
}
cvReleaseImage(&src);
return dst;
}
IplImage *load_raw_image(const char *s)
{
IplImage *img;
CvSize sz = { IMAGE_WIDTH, IMAGE_HEIGHT };
img = cvCreateImage(sz, IPL_DEPTH_16U, 1);
if (!img) {
printf("cvCreateImage() failed\n");
return NULL;
}
img->imageData = load_raw_data(s, sz.width * sz.height * 2);
if (!img->imageData) {
cvReleaseImage(&img);
return NULL;
}
return img;
}
char *load_raw_data(const char *s, int size)
{
int fd, len;
char *dat = malloc(size);
if (!dat) {
perror("malloc");
return NULL;
}
fd = open(s, O_RDONLY);
if (fd < 0) {
perror("open");
free(dat);
return NULL;
}
len = read(fd, dat, size);
if (len != size) {
printf("read tried %d got %d\n", size, len);
free(dat);
return NULL;
}
close(fd);
return dat;
}