@@ -30,15 +30,18 @@ int main(int argc, char **argv)
30
30
FILE * fpout ;
31
31
int length , i , j , horz , vert , index = 0 ;
32
32
int vertical_flip = TRUE;
33
- int r , g , b ;
33
+ int black_white ;
34
+ int image_ident ;
35
+ int bits_pixel ;
36
+ int r , g , b , alpha ;
34
37
double y ;
35
38
double cr , cg , cb ;
36
39
unsigned char * yp ;
37
40
int matrix_coefficients = 1 ;
38
- unsigned int py ;
39
- static unsigned char buffer_422output [ 3840 * 2160 * 2 ] ;
40
- static unsigned char buffer_444Y [ 3840 * 2160 ] ;
41
- static unsigned char buffer_444input [ 3840 * 2160 * 3 ] ;
41
+ static unsigned char buffer_header [ 18 ] ;
42
+ unsigned char * buffer_ident ;
43
+ unsigned char * buffer_444Y ;
44
+ unsigned char * buffer_444input ;
42
45
static double coef [8 ][3 ] = {
43
46
{0.2126 , 0.7152 , 0.0722 }, /* ITU-R Rec. 709 */
44
47
{0.299 , 0.587 , 0.114 }, /* unspecified */
@@ -68,67 +71,155 @@ int main(int argc, char **argv)
68
71
exit (-1 );
69
72
}
70
73
71
- length = fread (& buffer_444input [0 ], 1 , 18 , fp );
74
+ length = fread (& buffer_header [0 ], 1 , 18 , fp );
72
75
if (length != 18 ) {
73
76
fprintf (stderr , "Length Error reading input file <%s>\n" , argv [1 ]);
74
77
exit (-1 );
75
78
}
76
79
77
- horz = buffer_444input [13 ] << 8 ;
78
- horz |= buffer_444input [12 ];
79
- vert = buffer_444input [15 ] << 8 ;
80
- vert |= buffer_444input [14 ];
80
+ image_ident = buffer_header [0 ];
81
+ bits_pixel = buffer_header [16 ];
82
+
83
+ horz = buffer_header [13 ] << 8 ;
84
+ horz |= buffer_header [12 ];
85
+ vert = buffer_header [15 ] << 8 ;
86
+ vert |= buffer_header [14 ];
81
87
printf ("horz = %d, vert = %d\n" , horz , vert );
82
88
83
- if (buffer_444input [17 ] & 0x20 )
89
+ if (buffer_header [17 ] & 0x20 )
84
90
{
85
91
vertical_flip = FALSE;
86
92
}
87
- length = fread (& buffer_444input [0 ], 1 , (horz * vert * 3 ), fp );
88
- if (length != (horz * vert * 3 )) {
89
- fprintf (stderr , "Length Error reading input file <%s>\n" , argv [1 ]);
90
- exit (-1 );
91
- }
92
93
93
- i = matrix_coefficients ;
94
- cr = coef [i - 1 ][0 ];
95
- cg = coef [i - 1 ][1 ];
96
- cb = coef [i - 1 ][2 ];
94
+ if (image_ident != 0 ) {
95
+ buffer_ident = (unsigned char * )malloc (image_ident );
96
+ length = fread (& buffer_ident [0 ], 1 , image_ident , fp );
97
+ if (length != image_ident ) {
98
+ free (buffer_ident );
99
+ fprintf (stderr , "Length Error reading input file <%s>\n" , argv [1 ]);
100
+ exit (-1 );
101
+ }
102
+ free (buffer_ident );
103
+ }
97
104
98
- if (vertical_flip == FALSE )
105
+ if (buffer_header [ 2 ] == 0x3 )
99
106
{
100
- index = 0 ;
107
+ black_white = TRUE;
108
+ buffer_444input = (unsigned char * )malloc (horz * vert );
109
+ length = fread (& buffer_444input [0 ], 1 , (horz * vert ), fp );
110
+ if (length != (horz * vert )) {
111
+ free (buffer_444input );
112
+ fprintf (stderr , "Length Error reading input file <%s>\n" , argv [1 ]);
113
+ exit (-1 );
114
+ }
101
115
}
102
116
else
103
117
{
104
- index = ((horz * (vert - 1 )) * 3 );
118
+ black_white = FALSE;
119
+ if (bits_pixel == 32 ) {
120
+ buffer_444input = (unsigned char * )malloc (horz * vert * 4 );
121
+ length = fread (& buffer_444input [0 ], 1 , (horz * vert * 4 ), fp );
122
+ if (length != (horz * vert * 4 )) {
123
+ free (buffer_444input );
124
+ fprintf (stderr , "Length Error reading input file <%s>\n" , argv [1 ]);
125
+ exit (-1 );
126
+ }
127
+ }
128
+ else {
129
+ buffer_444input = (unsigned char * )malloc (horz * vert * 3 );
130
+ length = fread (& buffer_444input [0 ], 1 , (horz * vert * 3 ), fp );
131
+ if (length != (horz * vert * 3 )) {
132
+ free (buffer_444input );
133
+ fprintf (stderr , "Length Error reading input file <%s>\n" , argv [1 ]);
134
+ exit (-1 );
135
+ }
136
+ }
105
137
}
106
138
107
- for (i = 0 ; i < vert ; i ++ )
108
- {
109
- yp = & buffer_444Y [0 ] + i * horz ;
139
+ buffer_444Y = (unsigned char * )malloc (horz * vert );
110
140
111
- for (j = 0 ; j < horz ; j ++ )
112
- {
113
- b = buffer_444input [index ++ ];
114
- g = buffer_444input [index ++ ];
115
- r = buffer_444input [index ++ ];
141
+ i = matrix_coefficients ;
142
+ cr = coef [i - 1 ][0 ];
143
+ cg = coef [i - 1 ][1 ];
144
+ cb = coef [i - 1 ][2 ];
116
145
117
- /* convert to Y */
118
- y = cr * r + cg * g + cb * b ;
119
- yp [j ] = (219.0 / 255.0 ) * y + 16.5 ; /* nominal range: 16..235 */
146
+ if (black_white == TRUE)
147
+ {
148
+ if (vertical_flip == FALSE)
149
+ {
150
+ index = 0 ;
120
151
}
121
- if ( vertical_flip )
152
+ else
122
153
{
123
- index = index - (horz * 6 );
154
+ index = (horz * (vert - 1 ));
155
+ }
156
+
157
+ for (i = 0 ; i < vert ; i ++ )
158
+ {
159
+ yp = & buffer_444Y [0 ] + i * horz ;
160
+
161
+ for (j = 0 ; j < horz ; j ++ )
162
+ {
163
+ /* convert to Y */
164
+ y = buffer_444input [index ++ ];
165
+ yp [j ] = (219.0 / 255.0 ) * y + 16.5 ; /* nominal range: 16..235 */
166
+ }
167
+ if (vertical_flip )
168
+ {
169
+ index = index - (horz * 2 );
170
+ }
124
171
}
125
172
}
126
- py = 0 ;
127
- for (i = 0 ; i < (horz * vert ); i ++ )
173
+ else
128
174
{
129
- buffer_422output [i ] = buffer_444Y [py ++ ];
175
+ if (vertical_flip == FALSE)
176
+ {
177
+ index = 0 ;
178
+ }
179
+ else
180
+ {
181
+ if (bits_pixel == 32 ) {
182
+ index = ((horz * (vert - 1 )) * 4 );
183
+ }
184
+ else {
185
+ index = ((horz * (vert - 1 )) * 3 );
186
+ }
187
+ }
188
+
189
+ for (i = 0 ; i < vert ; i ++ )
190
+ {
191
+ yp = & buffer_444Y [0 ] + i * horz ;
192
+
193
+ for (j = 0 ; j < horz ; j ++ )
194
+ {
195
+ b = buffer_444input [index ++ ];
196
+ g = buffer_444input [index ++ ];
197
+ r = buffer_444input [index ++ ];
198
+ if (bits_pixel == 32 ) {
199
+ alpha = buffer_444input [index ++ ];
200
+ if (alpha == 0 ) {
201
+ r = g = b = 235 ;
202
+ }
203
+ }
204
+
205
+ /* convert to Y */
206
+ y = cr * r + cg * g + cb * b ;
207
+ yp [j ] = (219.0 / 255.0 ) * y + 16.5 ; /* nominal range: 16..235 */
208
+ }
209
+ if (vertical_flip )
210
+ {
211
+ if (bits_pixel == 32 ) {
212
+ index = index - (horz * 8 );
213
+ }
214
+ else {
215
+ index = index - (horz * 6 );
216
+ }
217
+ }
218
+ }
130
219
}
131
- fwrite (& buffer_422output [0 ], 1 , (horz * vert ), fpout );
220
+ fwrite (& buffer_444Y [0 ], 1 , (horz * vert ), fpout );
221
+ free (buffer_444Y );
222
+ free (buffer_444input );
132
223
fclose (fp );
133
224
fclose (fpout );
134
225
return 0 ;
0 commit comments