This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pnm2txt.c
187 lines (165 loc) · 5.01 KB
/
pnm2txt.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Next Video Tool for Linux* OS - Video4Linux2 API tool for developers.
*
* Copyright (c) 2017 Intel Corporation. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 as published by the Free Software Foundation.
*
* 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "utillib.h"
enum format {
SBGGR10,
SGBRG10,
SRGGB10,
SGRBG10,
};
static void error(char *s)
{
fprintf(stderr, "%s\n", s);
exit(1);
}
/*
* Read 16-bit word at the given memory location, MSB first.
* Return 10 most significant bits of the value.
*/
static inline unsigned int get_word(unsigned char *src)
{
unsigned int hi = src[0];
unsigned int lo = src[1];
unsigned int w = (hi << 8) | lo;
w >>= 6; /* 16 bits per pixel to 10 bits per pixel */
return w;
}
/*
* Store the given 16-bit pixel value into given memory location as two bytes.
*/
static inline void write_pixel(unsigned char *p, unsigned int v)
{
p[0] = v & 0xff;
p[1] = (v >> 8) & 0xff;
}
/*
* Convert RGB-image into RAW 10 bits-per-pixel format.
* Image size is size[0]*size[1] pixels.
* Input: size[0]*size[1] pixels, each pixel contains R, G, and B components,
* in this order, and each component contains 16 bits from 0..65535,
* MSB first. Input buffer size must be size[0]*size[1]*3*2 bytes.
* Output: raw bayer format size[0]*size[1] pixels, each pixel containing either
* R, G, or B according to the given format. Each pixel is stored as
* 16-bit value, LSB first, and has a value from 0..1023
* (6 most significant bits are zero). The output buffer takes
* size[0]*size[1]*2 bytes of memory.
*/
static unsigned char *rgb2raw10(unsigned char *rgb16, int size[2], enum format format)
{
int oddrow, oddpix, initrow, initpix;
unsigned char *buf, *p;
int y, x;
p = buf = malloc(size[0] * size[1] * 2);
if (format == SBGGR10) { initrow = 1; initpix = 0; } else
if (format == SGBRG10) { initrow = 1; initpix = 1; } else
if (format == SRGGB10) { initrow = 0; initpix = 1; } else
if (format == SGRBG10) { initrow = 0; initpix = 0; } else error("bad format");
oddrow = initrow;
for (y = 0; y < size[1]; y++) {
oddpix = initpix;
for (x = 0; x < size[0]; x++) {
unsigned int r = get_word(&rgb16[0]);
unsigned int g = get_word(&rgb16[2]);
unsigned int b = get_word(&rgb16[4]);
unsigned int v;
if (!oddrow && !oddpix) v = g;
if (!oddrow && oddpix) v = r;
if ( oddrow && !oddpix) v = b;
if ( oddrow && oddpix) v = g;
write_pixel(p, v);
rgb16 += 3 * 2;
p += 2;
oddpix ^= 1;
}
oddrow ^= 1;
}
return buf;
}
/*
* Input: raw bayer format size[0]*size[1] pixels. Each pixel is stored as
* 16-bit value, LSB first, and has a value from 0..1023.
* Output: From each four sequential input pixels p1 p2 p3 p4, five bytes are
* stored into output buffer as in MIPI standard:
* Byte#0 Byte#1 Byte#2 Byte#3 Byte#4
* p1[9:2] p2[9:2] p3[9:2] p4[9:2] p4[1:0]p3[1:0]p2[1:0]p1[1:0]
* Output buffer size is size[0] * size[1] * 5 / 4 bytes.
* outsize[0] = size[0] * 5 / 4 (with rounding) and
* outsize[1] = size[1]
*/
static unsigned char *raw2mipi(unsigned char *raw10, int size[2], int outsize[2])
{
unsigned char *buf, *src, *dst, *srcp, *dstp;
int y, x, x0;
outsize[0] = (size[0] * 5 + 3) / 4;
outsize[1] = size[1];
src = raw10;
dst = buf = calloc(outsize[0] * outsize[1], 1);
for (y = 0; y < size[1]; y++) {
srcp = src;
dstp = dst;
for (x = 0; x < size[0] - 3; x += 4) {
unsigned char lsbs = 0; /* Store all least significant 2-bits to 5th byte */
for (x0 = 0; x0 < 4; x0++) {
unsigned int v = srcp[0] | (srcp[1] << 8);
*dstp++ = v >> 2;
lsbs |= (v & 3) << (x0 * 2);
srcp += 2;
}
*dstp++ = lsbs;
}
src += size[0] * 2;
dst += outsize[0];
}
return buf;
}
static void write_txt(char *output, unsigned char *mipi, int size[2])
{
FILE *f;
int x, y;
f = fopen(output, "wb");
if (!f) error("failed to open output file");
for (y = 0; y < size[1]; y++) {
for (x = 0; x < size[0]; x++) {
fprintf(f, "%02x", *mipi++);
if (x < size[0] - 1) fputc(' ', f);
else fputc('\n', f);
}
}
fclose(f);
}
int main(int argc, char *argv[])
{
char *input, *output;
int size[2];
int datasize[2];
unsigned char *rgb16, *raw10, *mipi;
if (argc != 3) error("give exactly too arguments: input and output filename\n");
input = argv[1];
output = argv[2];
rgb16 = read_pnm(input, size);
raw10 = rgb2raw10(rgb16, size, SGRBG10);
mipi = raw2mipi(raw10, size, datasize);
write_txt(output, mipi, datasize);
printf("Converted %ix%i pixels of data from %s to %s\n", size[0], size[1], input, output);
free(rgb16);
free(raw10);
free(mipi);
return 0;
}