forked from purdeaandrei/image_meta_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpo2jps.c
375 lines (340 loc) · 13.8 KB
/
mpo2jps.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Copyright (c) 2012, Movidius Ltd.
Released under the BSD 3-clause license.
Author: Purdea Andrei <andrei -at- purdea.ro>
<andrei.purdea -at- movidius.com> */
#include <stdio.h>
#include <mxml.h>
#include <jpeglib.h>
#include "image2xmlmem.h"
#include "xmlmem2xmlfile.h"
#include "xmlmem_data_source_manager.h"
void help(int argc, char ** argv) {
printf("Usage: %s input.mpo output.jps [ flags ]\n", argv[0]);
printf("-hh: half-height output (pixels are averaged\n");
printf("-hw: half-width output (pixels are averaged\n");
printf("-fr: right image first (topmost/leftmost/red)\n");
printf("-fl: left image first (topmost/leftmost/red) (default!)\n");
printf("-i: interleaved layout\n");
printf("-s: side by side layout (default!)\n");
printf("-o: over-under layout\n");
printf("-a: anaglyph\n");
printf("-qNUMBER: JPEG quality (0..100). remember, MPO->JPS conversion reencodes the image!\n");
printf("NOTE: monoscopic format is not implemented.\n");
}
mxml_node_t *getMPIndividualNumTag(mxml_node_t *image) {
if (!IS_ELEMENT(image, "image")) { printf("get_individual_image_number: this must be an image!\n"); return NULL; }
FOR_CHILDREN(marker, image) {
if (IS_ELEMENT(marker, "APP2") && IS_ELEMENT_ATTR(marker, "type", "MPF")) {
FOR_CHILDREN(ifd, marker) {
if (IS_ELEMENT(ifd, "ifd")) {
FOR_CHILDREN(tag, ifd) {
if (IS_ELEMENT(tag, "tag") && IS_ELEMENT_ATTR(tag, "id", "MPIndividualNum")) {
return tag;
}
}
}
}
}
}
return NULL;
}
int getMPIndividualNumValue(mxml_node_t *image) {
mxml_node_t *tag = getMPIndividualNumTag(image);
if (tag!=NULL) {
const char *value = mxmlElementGetAttr(tag, "value");
if (value==NULL) return 0;
int t;
if (1!=sscanf(value, "%d", &t)) return 0;
return t;
} else return 0;
}
static void my_concat(JSAMPARRAY out, JSAMPARRAY in1, JSAMPARRAY in2, size_t in1_n, size_t in2_n) {
int i;
for (i=0;i<in1_n;i++) {
out[0][i] = in1[0][i];
}
for (;i<in1_n + in2_n; i++) {
out[0][i] = in2[0][i - in1_n];
}
}
static void average_two_rows(JSAMPARRAY a, size_t n) {
int i;
for (i=0;i<n;i++) {
a[0][i] = (a[0][i] + a[1][i])/2;
}
}
static void half_row_length(JSAMPARRAY a, size_t n, size_t components) {
int i, j;
for (i=0;i<n/2/components;i++) {
for (j=0;j<components;j++) {
a[0][i*components + j] = (a[0][2*i*components + j] + a[0][(2*i+1)*components + j])/2;
}
}
}
static void compose_anaglyph(JSAMPARRAY red, JSAMPARRAY bluegreen, size_t n) {
int i;
for (i=0;i<n/3;i++) {
red[0][i*3+1] = bluegreen[0][i*3+1];
red[0][i*3+2] = bluegreen[0][i*3+2];
}
}
static void get_left_and_right(char *filename, mxml_node_t **left, mxml_node_t **right) {
mxml_node_t *xml = i2xm_load_filename(filename, /* interpret_concatenated true: */ 1);
if (xml==NULL) {
printf("ERROR: there was a problem opening the image file.\n");
exit(-1);
}
if (xml->value.element.name[0]=='?') xml = xml->child;
*left = *right = NULL;
FOR_CHILDREN(image, xml) {
if (IS_ELEMENT(image, "image")) {
if (*left==NULL) *left = image;
else if (*right==NULL) *right = image;
else WARNING("there are more then 2 images in this file. only considering the first two");
}
}
if (*left==NULL || *right==NULL) {
printf("ERROR: there aren't enaugh images in input\n");
exit(-2);
}
int left_n = getMPIndividualNumValue(*left), right_n = getMPIndividualNumValue(*right);
if (right_n != 0 && left_n != 0 && right_n < left_n) {
mxml_node_t *t;
t = *left;
*left = *right;
*right = t;
}
}
int first_right = 0;
int half_height = 0;
int half_width = 0;
char layout = 's';
int quality=-1; // by default, don't set the quality
static void interpret_arguments(int argc, char **argv) {
if (argc<3) {
help(argc, argv);
exit(-1);
}
int i;
for (i=3;i<argc;i++) {
if (argv[i][0]!='-') {
printf("argument nr %d (%s) should be a flag (start with -)!\n", i, argv[i]);
exit(-1);
}
switch (argv[i][1]) {
case 'h':
if (argv[i][2] == 'h') half_height = 1;
else if (argv[i][2] == 'w') half_width = 1;
else {
printf("argument nr %d (%s) in unrecognized!\n", i, argv[i]);
exit (-1);
}
break;
case 'f':
if (argv[i][2] == 'r') first_right = 1;
if (argv[i][2] == 'l') first_right = 0;
break;
case 'i':
case 's':
case 'o':
case 'a':
layout = argv[i][1];
break;
case 'q':
if (1!=sscanf(argv[i]+2, "%d", &quality)) {
printf("invalid argument: unknown quality. correct examples: -q100 -q75\n");
exit(-1);
}
break;
default:
printf("unknown argument nr %d (%s)\n", i, argv[i]);
exit(-1);
}
}
}
int main(int argc, char **argv) {
interpret_arguments(argc, argv);
mxml_node_t *left, *right;
get_left_and_right(argv[1], &left, &right);
if (first_right) { // ugly , but works
mxml_node_t *t;
t = left;
left = right;
right = t;
}
// LEFT:
struct jpeg_decompress_struct left_cinfo;
struct jpeg_error_mgr left_jerr;
left_cinfo.err = jpeg_std_error(&left_jerr);
jpeg_create_decompress(&left_cinfo);
jpeg_xmlmem_src(&left_cinfo, left);
jpeg_read_header(&left_cinfo, TRUE);
jpeg_start_decompress(&left_cinfo);
if (half_height && left_cinfo.output_height % 2 == 1) {
printf("error: left image must have a height divisible by two , if half-height output is desired!\n");
}
if (half_width && left_cinfo.output_width % 2 == 1) {
printf("error: left image must have a width divisible by two , if half-width output is desired!\n");
}
int left_row_stride = left_cinfo.output_width * left_cinfo.output_components;
JSAMPARRAY left_buffer = (*left_cinfo.mem->alloc_sarray)
((j_common_ptr) &left_cinfo, JPOOL_IMAGE, left_row_stride, half_height ? 2 : 1);
// RIGHT:
struct jpeg_decompress_struct right_cinfo;
struct jpeg_error_mgr right_jerr;
right_cinfo.err = jpeg_std_error(&right_jerr);
jpeg_create_decompress(&right_cinfo);
jpeg_xmlmem_src(&right_cinfo, right);
jpeg_read_header(&right_cinfo, TRUE);
jpeg_start_decompress(&right_cinfo);
if (half_height && right_cinfo.output_height % 2 == 1) {
printf("error: right image must have a height divisible by two , if half-height output is desired!\n");
}
if (half_width && right_cinfo.output_width % 2 == 1) {
printf("error: right image must have a width divisible by two , if half-width output is desired!\n");
}
int right_row_stride = right_cinfo.output_width * right_cinfo.output_components;
JSAMPARRAY right_buffer = (*right_cinfo.mem->alloc_sarray)
((j_common_ptr) &right_cinfo, JPOOL_IMAGE, right_row_stride, half_height ? 2 : 1);
if (right_cinfo.output_width != left_cinfo.output_width) {
printf("left, and right image width doesn't match!\n");
exit(-100);
}
if (right_cinfo.output_height != left_cinfo.output_height) {
printf("left, and right image height doesn't match!\n");
exit(-100);
}
if (right_cinfo.output_components != left_cinfo.output_components) {
printf("left, and right image components doesn't match!\n");
exit(-100);
}
if (right_cinfo.out_color_space != left_cinfo.out_color_space) {
printf("weird, colorspaces should match!\n");
exit(-100);
}
// OUTPUT:
struct jpeg_compress_struct out_cinfo;
struct jpeg_error_mgr out_jerr;
FILE *outfile;
out_cinfo.err = jpeg_std_error(&out_jerr);
jpeg_create_compress(&out_cinfo);
if ((outfile = fopen(argv[2], "wb")) == NULL) {
fprintf(stderr, "can't open output\n");
exit(1);
}
jpeg_stdio_dest(&out_cinfo, outfile);
out_cinfo.input_components = left_cinfo.output_components;
out_cinfo.in_color_space = left_cinfo.out_color_space;
if (layout=='a' && (out_cinfo.input_components!=3 || out_cinfo.in_color_space!=JCS_RGB)) {
printf("error: anaglyph works only with rgb\n");
exit (-55);
}
switch (layout) {
case 's': // side by side
out_cinfo.image_width = (left_cinfo.output_width + right_cinfo.output_width) / (half_width ? 2 : 1);
out_cinfo.image_height = left_cinfo.output_height / (half_height ? 2 : 1);
break;
case 'o': // over/under
case 'i': // interlaced
out_cinfo.image_width = left_cinfo.output_width / (half_width ? 2 : 1);
out_cinfo.image_height = (left_cinfo.output_height + right_cinfo.output_height) / (half_height ? 2 : 1);
break;
case 'a': // anaglyph
out_cinfo.image_width = left_cinfo.output_width / (half_width ? 2 : 1);
out_cinfo.image_height = left_cinfo.output_height / (half_height ? 2 : 1);
break;
}
jpeg_set_defaults(&out_cinfo);
if (quality!=-1)
jpeg_set_quality(&out_cinfo, quality, TRUE);
jpeg_start_compress(&out_cinfo, TRUE);
JOCTET jpsmarker[] = "_JPSJPS_" /* descriptor length: */ "\x00\x04" /*descriptor:*/ "\x00\x00\x00\x01" /*comment length:*/ "\x00\x00";
int jpsmarkerlength = 16;
switch (layout) {
case 'i': jpsmarker[12] = 0x01; break;
case 's': jpsmarker[12] = 0x02; break;
case 'o': jpsmarker[12] = 0x03; break;
case 'a': jpsmarker[12] = 0x04; break;
}
if (half_height) jpsmarker[11] |= 1;
if (half_width) jpsmarker[11] |= 2;
if (!first_right) jpsmarker[11] |= 4;
jpeg_write_marker(&out_cinfo, JPEG_APP0 + 3, jpsmarker, jpsmarkerlength);
switch (layout) {
case 'a':
case 'i':
case 's': {
JSAMPARRAY out_buffer = (*right_cinfo.mem->alloc_sarray)
((j_common_ptr) &right_cinfo, JPOOL_IMAGE, (left_row_stride + right_row_stride) / (half_width ? 2 : 1), 1);
while (left_cinfo.output_scanline < left_cinfo.output_height &&
right_cinfo.output_scanline < right_cinfo.output_height &&
out_cinfo.next_scanline < out_cinfo.image_height) {
jpeg_read_scanlines(&left_cinfo, left_buffer, 1);
jpeg_read_scanlines(&right_cinfo, right_buffer, 1);
if (half_height) {
JSAMPROW t[1];
t[0] = left_buffer[1];
jpeg_read_scanlines(&left_cinfo, t, 1);
t[0] = right_buffer[1];
jpeg_read_scanlines(&right_cinfo, t, 1);
average_two_rows(left_buffer, left_row_stride);
average_two_rows(right_buffer, right_row_stride);
}
if (half_width) {
half_row_length(left_buffer, left_row_stride, left_cinfo.output_components);
half_row_length(right_buffer, right_row_stride, right_cinfo.output_components);
}
if (layout=='s') {
my_concat(out_buffer, left_buffer, right_buffer, left_row_stride / (half_width ? 2 : 1), right_row_stride / (half_width ? 2 : 1));
jpeg_write_scanlines(&out_cinfo, out_buffer, 1);
} else if (layout=='i') {
jpeg_write_scanlines(&out_cinfo, left_buffer, 1);
jpeg_write_scanlines(&out_cinfo, right_buffer, 1);
} else if (layout=='a') {
compose_anaglyph(left_buffer, right_buffer, left_row_stride);
jpeg_write_scanlines(&out_cinfo, left_buffer, 1);
}
}
break;
}
case 'o': {
while (left_cinfo.output_scanline < left_cinfo.output_height &&
out_cinfo.next_scanline < out_cinfo.image_height) {
jpeg_read_scanlines(&left_cinfo, left_buffer, 1);
if (half_height) {
JSAMPROW t[1];
t[0] = left_buffer[1];
jpeg_read_scanlines(&left_cinfo, t, 1);
average_two_rows(left_buffer, left_row_stride);
}
if (half_width) {
half_row_length(left_buffer, left_row_stride, left_cinfo.output_components);
}
jpeg_write_scanlines(&out_cinfo, left_buffer, 1);
}
while (right_cinfo.output_scanline < right_cinfo.output_height &&
out_cinfo.next_scanline < out_cinfo.image_height) {
jpeg_read_scanlines(&right_cinfo, right_buffer, 1);
if (half_height) {
JSAMPROW t[1];
t[0] = right_buffer[1];
jpeg_read_scanlines(&right_cinfo, t, 1);
average_two_rows(right_buffer, right_row_stride);
}
if (half_width) {
half_row_length(right_buffer, right_row_stride, right_cinfo.output_components);
}
jpeg_write_scanlines(&out_cinfo, right_buffer, 1);
}
break;
}
}
jpeg_finish_decompress(&left_cinfo);
jpeg_finish_decompress(&right_cinfo);
jpeg_finish_compress(&out_cinfo);
fclose(outfile);
jpeg_destroy_compress(&out_cinfo);
jpeg_destroy_decompress(&left_cinfo);
jpeg_destroy_decompress(&right_cinfo);
return 0;
}