-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtgatoluma.c
227 lines (206 loc) · 6.5 KB
/
tgatoluma.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- c -*- */
/*
* Copyright 2015 Ron Economos.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software 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.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int main(int argc, char **argv)
{
FILE *fp;
FILE *fpout;
int length, i, j, horz, vert, index = 0;
int vertical_flip = TRUE;
int black_white;
int image_ident;
int bits_pixel;
int r, g, b, alpha;
double y;
double cr, cg, cb;
unsigned char *yp;
int matrix_coefficients = 1;
static unsigned char buffer_header[18];
unsigned char *buffer_ident;
unsigned char *buffer_444Y;
unsigned char *buffer_444input;
static double coef[8][3] = {
{0.2126, 0.7152, 0.0722}, /* ITU-R Rec. 709 */
{0.299, 0.587, 0.114}, /* unspecified */
{0.299, 0.587, 0.114}, /* reserved */
{0.30, 0.59, 0.11}, /* FCC */
{0.299, 0.587, 0.114}, /* ITU-R Rec. 624-4 System B, G */
{0.299, 0.587, 0.114}, /* SMPTE 170M */
{0.212, 0.701, 0.087}, /* SMPTE 240M (1987) */
{0.2627, 0.678, 0.0593}}; /* ITU-R BT.2020-1 */
if (argc != 3) {
fprintf(stderr, "usage: tgatoluma <infile> <outfile>\n");
exit(-1);
}
/*--- open binary file (for reading) ---*/
fp = fopen(argv[1], "rb");
if (fp == 0) {
fprintf(stderr, "Cannot open input file <%s>\n", argv[1]);
exit(-1);
}
/*--- open binary file (for writing) ---*/
fpout = fopen(argv[2], "wb");
if (fpout == 0) {
fprintf(stderr, "Cannot open output file <%s>\n", argv[2]);
exit(-1);
}
length = fread(&buffer_header[0], 1, 18, fp);
if (length != 18) {
fprintf(stderr, "Length Error reading input file <%s>\n", argv[1]);
exit(-1);
}
image_ident = buffer_header[0];
bits_pixel = buffer_header[16];
horz = buffer_header[13] << 8;
horz |= buffer_header[12];
vert = buffer_header[15] << 8;
vert |= buffer_header[14];
printf("horz = %d, vert = %d\n", horz, vert);
if (buffer_header[17] & 0x20)
{
vertical_flip = FALSE;
}
if (image_ident != 0) {
buffer_ident = (unsigned char *)malloc(image_ident);
length = fread(&buffer_ident[0], 1, image_ident, fp);
if (length != image_ident) {
free(buffer_ident);
fprintf(stderr, "Length Error reading input file <%s>\n", argv[1]);
exit(-1);
}
free(buffer_ident);
}
if (buffer_header[2] == 0x3)
{
black_white = TRUE;
buffer_444input = (unsigned char *)malloc(horz * vert);
length = fread(&buffer_444input[0], 1, (horz * vert), fp);
if (length != (horz * vert)) {
free(buffer_444input);
fprintf(stderr, "Length Error reading input file <%s>\n", argv[1]);
exit(-1);
}
}
else
{
black_white = FALSE;
if (bits_pixel == 32) {
buffer_444input = (unsigned char *)malloc(horz * vert * 4);
length = fread(&buffer_444input[0], 1, (horz * vert * 4), fp);
if (length != (horz * vert * 4)) {
free(buffer_444input);
fprintf(stderr, "Length Error reading input file <%s>\n", argv[1]);
exit(-1);
}
}
else {
buffer_444input = (unsigned char *)malloc(horz * vert * 3);
length = fread(&buffer_444input[0], 1, (horz * vert * 3), fp);
if (length != (horz * vert * 3)) {
free(buffer_444input);
fprintf(stderr, "Length Error reading input file <%s>\n", argv[1]);
exit(-1);
}
}
}
buffer_444Y = (unsigned char *)malloc(horz * vert);
i = matrix_coefficients;
cr = coef[i-1][0];
cg = coef[i-1][1];
cb = coef[i-1][2];
if (black_white == TRUE)
{
if (vertical_flip == FALSE)
{
index = 0;
}
else
{
index = (horz * (vert - 1));
}
for (i = 0; i < vert; i++)
{
yp = &buffer_444Y[0] + i * horz;
for (j = 0 ; j < horz; j++)
{
/* convert to Y */
y = buffer_444input[index++];
yp[j] = (219.0 / 255.0) * y + 16.5; /* nominal range: 16..235 */
}
if (vertical_flip)
{
index = index - (horz * 2);
}
}
}
else
{
if (vertical_flip == FALSE)
{
index = 0;
}
else
{
if (bits_pixel == 32) {
index = ((horz * (vert - 1)) * 4);
}
else {
index = ((horz * (vert - 1)) * 3);
}
}
for (i = 0; i < vert; i++)
{
yp = &buffer_444Y[0] + i * horz;
for (j = 0 ; j < horz; j++)
{
b = buffer_444input[index++];
g = buffer_444input[index++];
r = buffer_444input[index++];
if (bits_pixel == 32) {
alpha = buffer_444input[index++];
if (alpha == 0) {
r = g = b = 235;
}
}
/* convert to Y */
y = cr * r + cg * g + cb * b;
yp[j] = (219.0 / 255.0) * y + 16.5; /* nominal range: 16..235 */
}
if (vertical_flip)
{
if (bits_pixel == 32) {
index = index - (horz * 8);
}
else {
index = index - (horz * 6);
}
}
}
}
fwrite(&buffer_444Y[0], 1, (horz * vert), fpout);
free(buffer_444Y);
free(buffer_444input);
fclose(fp);
fclose(fpout);
return 0;
}