/
cw_emit.c
727 lines (636 loc) · 23.4 KB
/
cw_emit.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*************************************************************************
*
* This software module was originally contributed by Microsoft
* Corporation in the course of development of the
* ITU-T T.832 | ISO/IEC 29199-2 ("JPEG XR") format standard for
* reference purposes and its performance may not have been optimized.
*
* This software module is an implementation of one or more
* tools as specified by the JPEG XR standard.
*
* ITU/ISO/IEC give You a royalty-free, worldwide, non-exclusive
* copyright license to copy, distribute, and make derivative works
* of this software module or modifications thereof for use in
* products claiming conformance to the JPEG XR standard as
* specified by ITU-T T.832 | ISO/IEC 29199-2.
*
* ITU/ISO/IEC give users the same free license to this software
* module or modifications thereof for research purposes and further
* ITU/ISO/IEC standardization.
*
* Those intending to use this software module in products are advised
* that its use may infringe existing patents. ITU/ISO/IEC have no
* liability for use of this software module or modifications thereof.
*
* Copyright is not released for products that do not conform to
* to the JPEG XR standard as specified by ITU-T T.832 |
* ISO/IEC 29199-2.
*
******** Section to be removed when the standard is published ************
*
* Assurance that the contributed software module can be used
* (1) in the ITU-T "T.JXR" | ISO/IEC 29199 ("JPEG XR") standard once the
* standard has been adopted; and
* (2) to develop the JPEG XR standard:
*
* Microsoft Corporation and any subsequent contributors to the development
* of this software grant ITU/ISO/IEC all rights necessary to include
* the originally developed software module or modifications thereof in the
* JPEG XR standard and to permit ITU/ISO/IEC to offer such a royalty-free,
* worldwide, non-exclusive copyright license to copy, distribute, and make
* derivative works of this software module or modifications thereof for
* use in products claiming conformance to the JPEG XR standard as
* specified by ITU-T T.832 | ISO/IEC 29199-2, and to the extent that
* such originally developed software module or portions of it are included
* in an ITU/ISO/IEC standard. To the extent that the original contributors
* may own patent rights that would be required to make, use, or sell the
* originally developed software module or portions thereof included in the
* ITU/ISO/IEC standard in a conforming product, the contributors will
* assure ITU/ISO/IEC that they are willing to negotiate licenses under
* reasonable and non-discriminatory terms and conditions with
* applicants throughout the world and in accordance with their patent
* rights declarations made to ITU/ISO/IEC (if any).
*
* Microsoft, any subsequent contributors, and ITU/ISO/IEC additionally
* gives You a free license to this software module or modifications
* thereof for the sole purpose of developing the JPEG XR standard.
*
******** end of section to be removed when the standard is published *****
*
* Microsoft Corporation retains full right to modify and use the code
* for its own purpose, to assign or donate the code to a third party,
* and to inhibit third parties from using the code for products that
* do not conform to the JPEG XR standard as specified by ITU-T T.832 |
* ISO/IEC 29199-2.
*
* This copyright notice must be included in all copies or derivative
* works.
*
* Copyright (c) ITU-T/ISO/IEC 2008, 2009.
***********************************************************************/
#ifdef _MSC_VER
#pragma comment (user,"$Id: cw_emit.c,v 1.10 2011-11-19 20:52:34 thor Exp $")
#endif
# include "jxr_priv.h"
# include <stdlib.h>
# include <string.h>
# include <assert.h>
int jxrc_start_file(jxr_container_t cp, FILE*fd)
{
assert(cp->fd == 0);
/* initializations */
cp->separate_alpha_image_plane = 0;
cp->image_count_mark = 0;
cp->alpha_count_mark = 0;
cp->alpha_offset_mark = 0;
cp->alpha_band = 0;
cp->fd = fd;
cp->file_mark = ftell(cp->fd);
const unsigned char head_bytes[8] = {0x49, 0x49, 0xbc, 0x01,
0x08, 0x00, 0x00, 0x00 };
fwrite(head_bytes, 1, 8, cp->fd);
return 0;
}
int jxrc_begin_ifd_entry(jxr_container_t cp)
{
assert(cp->image_count == 0);
cp->image_count = 1;
return 0;
}
int jxrc_set_pixel_format(jxr_container_t cp, jxrc_t_pixelFormat fmt)
{
memcpy(cp->pixel_format, jxr_guids[fmt], 16);
return 0;
}
jxrc_t_pixelFormat jxrc_get_pixel_format(jxr_container_t cp)
{
int i;
for(i=0; i< NUM_GUIDS; i++)
{
if(isEqualGUID(cp->pixel_format, jxr_guids[i]))
{
break;
}
}
if(i==NUM_GUIDS)
assert(0);
return (jxrc_t_pixelFormat)i;
}
int jxrc_set_image_shape(jxr_container_t cp, unsigned wid, unsigned hei)
{
cp->wid = wid;
cp->hei = hei;
return 0;
}
int jxrc_set_image_band_presence(jxr_container_t cp, unsigned bands)
{
cp->image_band = (uint8_t) bands;
return 0;
}
/*
This will induce the encoder to write as many of the
IFD tags of table A.4 (the listing of IFD tags) as it can
Off by default.
#define WRITE_OPTIONAL_IFD_TAGS
*/
int jxrc_set_separate_alpha_image_plane(jxr_container_t cp, unsigned int alpha_present)
{
cp->separate_alpha_image_plane = alpha_present;
return 0;
}
static void emit_ifd(jxr_container_t cp)
{
unsigned long ifd_mark;
int idx;
assert(cp->image_count > 0);
if (cp->image_count > 1){
assert(0);
ifd_mark = 0;
} else {
ifd_mark = 8;
}
struct ifd_table ifd[64];
unsigned char buf[1024];
int num_ifd = 0;
int num_buf = 0;
#ifdef WRITE_OPTIONAL_IFD_TAGS
int ifd_cnt;
char * input_string;
/* various sample strings used to test storage */
char document_name[] = {"JPEG XR image created by JPEG XR sample encoder."};
char image_description[] = {"The core codestream begins at WMPHOTO"};
char equipment_make[] = {"???"};
char equipment_model[] = {"JXR 0X1"};
char page_name[]={"Test page name"};
unsigned short page_number[] = {1,2};
char software_name_version[]={"JPEG XR reference software v1.6"};
char date_time[]={"2011:11:19 21:21:21"};
char artist_name[]={"JPEG Committee"};
char host_computer[]={"JXR"};
char copyright_notice[]={"©JPEG Committee"};
unsigned long spatial_xfrm = 0;
unsigned char profile_idc = 111;
unsigned char level_idc = 255;
float width_res = 96.0;
float height_res = 96.0;
input_string = document_name;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x010d; /* DOCUMENT_NAME */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = image_description;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x010e; /* IMAGE_DESCRIPTION */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = equipment_make;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x010f; /* EQUIPMENT_MAKE */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = equipment_model;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x0110; /* EQUIPMENT_MODEL */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = page_name;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x011d; /* PAGE_NAME */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
ifd[num_ifd].tag = 0x0129; /* PAGE_NUMBER */
ifd[num_ifd].type = 3; /* USHORT */
ifd[num_ifd].cnt = 2;
ifd[num_ifd].value_.v_short[0] = page_number[0];
ifd[num_ifd].value_.v_short[1] = page_number[1];
num_ifd += 1;
input_string = software_name_version;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x0131; /* SOFTWARE_NAME_VERSION */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = date_time;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x0132; /* DATE_TIME */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = artist_name;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x013b; /* ARTIST_NAME */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = host_computer;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x013c; /* HOST_COMPUTER */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
input_string = copyright_notice;
ifd_cnt = strlen(input_string) + 1;
ifd[num_ifd].tag = 0x8298; /* COPYRIGHT_NOTICE */
ifd[num_ifd].type = 2; /* UTF8 */
ifd[num_ifd].cnt = ifd_cnt;
if (ifd_cnt > 4)
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
else
memcpy(ifd[num_ifd].value_.v_sbyte, input_string, ifd_cnt);
for (idx = 0 ; idx < ifd_cnt ; idx += 1)
buf[num_buf++] = input_string[idx];
if (num_buf & 0x1)
buf[num_buf++] = (unsigned char) 0;
num_ifd += 1;
ifd[num_ifd].tag = 0xa001; /* COLOR_SPACE */
ifd[num_ifd].type = 3; /* USHORT */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_short[0] = 0xffff; /* Just use this value for testing */
ifd[num_ifd].value_.v_short[1] = 0;
num_ifd += 1;
#endif
ifd[num_ifd].tag = 0xbc01; /* PIXEL_FORMAT */
ifd[num_ifd].type = 1; /* BYTE */
ifd[num_ifd].cnt = 16;
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
for (idx = 0 ; idx < 16 ; idx += 1)
buf[num_buf++] = cp->pixel_format[idx];
num_ifd += 1;
#ifdef WRITE_OPTIONAL_IFD_TAGS
ifd[num_ifd].tag = 0xbc02; /* SPATIAL_XFRM_PRIMARY */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = spatial_xfrm;
num_ifd += 1;
/* IMAGE_TYPE should only be present when multiple images are in the image */
/* Fix this condition if encoder can handle multiple images */
if (0){
ifd[num_ifd].tag = 0xbc04; /* IMAGE_TYPE */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = 0;
num_ifd += 1;
}
ifd[num_ifd].tag = 0xbc05; /* PTM_COLOR_INFO() */
ifd[num_ifd].type = 1; /* BYTE */
ifd[num_ifd].cnt = 4;
ifd[num_ifd].value_.v_byte[0] = 2; /* default to unspecified */
ifd[num_ifd].value_.v_byte[1] = 2; /* default to unspecified */
ifd[num_ifd].value_.v_byte[2] = 2; /* default to no rotation */
ifd[num_ifd].value_.v_byte[3] = 0; /* default to no rotation */
num_ifd += 1;
ifd[num_ifd].tag = 0xbc06; /* PROFILE_LEVEL_CONTAINER() */
ifd[num_ifd].type = 1; /* BYTE */
ifd[num_ifd].cnt = 4;
ifd[num_ifd].value_.v_byte[0] = profile_idc;
ifd[num_ifd].value_.v_byte[1] = level_idc;
ifd[num_ifd].value_.v_byte[2] = 0;
ifd[num_ifd].value_.v_byte[3] = 1;
num_ifd += 1;
#endif
ifd[num_ifd].tag = 0xbc80; /* IMAGE_WIDTH */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = cp->wid;
num_ifd += 1;
ifd[num_ifd].tag = 0xbc81; /* IMAGE_HEIGHT */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = cp->hei;
num_ifd += 1;
#ifdef WRITE_OPTIONAL_IFD_TAGS
ifd[num_ifd].tag = 0xbc82; /* WIDTH_RESOLUTION */
ifd[num_ifd].type = 11; /* FLOAT */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_float = width_res;
num_ifd += 1;
ifd[num_ifd].tag = 0xbc83; /* HEIGHT_RESOLUTION */
ifd[num_ifd].type = 11; /* FLOAT */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_float = height_res;
num_ifd += 1;
#endif
ifd[num_ifd].tag = 0xbcc0; /* IMAGE_OFFSET */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = 0;
num_ifd += 1;
ifd[num_ifd].tag = 0xbcc1; /* IMAGE_COUNT */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = 0;
num_ifd += 1;
if (cp->separate_alpha_image_plane) {
ifd[num_ifd].tag = 0xbcc2; /* ALPHA_OFFSET */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = 0;
num_ifd += 1;
ifd[num_ifd].tag = 0xbcc3; /* ALPHA_COUNT */
ifd[num_ifd].type = 4; /* ULONG */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_long = 0;
num_ifd += 1;
}
#ifdef WRITE_OPTIONAL_IFD_TAGS
ifd[num_ifd].tag = 0xbcc4; /* IMAGE_BAND_PRESENCE */
ifd[num_ifd].type = 1; /* BYTE */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_byte[0] = cp->image_band;
num_ifd += 1;
if (cp->separate_alpha_image_plane) {
ifd[num_ifd].tag = 0xbcc5; /* ALPHA_BAND_PRESENCE */
ifd[num_ifd].type = 1; /* BYTE */
ifd[num_ifd].cnt = 1;
ifd[num_ifd].value_.v_byte[0] = cp->alpha_band;
num_ifd += 1;
}
ifd[num_ifd].tag = 0xea1c; /* PADDING_DATA */
ifd[num_ifd].type = 7; /* UNDEFINED */
ifd[num_ifd].cnt = 21; /* Value set arbitrarily to tests this IFD entry */
ifd[num_ifd].value_.v_long = ifd_mark + num_buf;
assert(ifd[num_ifd].cnt > 1);
buf[num_buf++] = 0x1c;
buf[num_buf++] = 0xea;
for (idx = 2 ; idx < ifd[num_ifd].cnt ; idx += 1)
buf[num_buf++] = 0;
num_ifd += 1;
#endif
unsigned long ifd_len = 2 + 12*num_ifd + 4; /* NUM_ENTRIES + 12 bytes per entry + ZERO_OR_NEXT_IFD_OFFSET */
cp->image_offset_mark = ifd_mark + ifd_len + num_buf;
for (idx = 0 ; idx < num_ifd ; idx += 1) {
switch (ifd[idx].type) {
case 1: /* BYTE */
case 2: /* UTF8 */
case 6: /* SBYTE */
case 7: /* UNDEFINED */
if (ifd[idx].cnt > 4)
ifd[idx].value_.v_long += ifd_len;
break;
case 3: /* USHORT */
case 8: /* SSHORT */
if (ifd[idx].cnt > 2)
ifd[idx].value_.v_long += ifd_len;
break;
case 4: /* ULONG */
case 9: /* SLONG */
case 11: /* FLOAT */
if (ifd[idx].cnt > 1)
ifd[idx].value_.v_long += ifd_len;
/* If we find the IMAGE_OFFSET, we know its value
now. Write it into v_long */
if (ifd[idx].tag == 0xbcc0) /* IMAGE_OFFSET */
ifd[idx].value_.v_long = cp->image_offset_mark;
/* For the following markers, record the locations to be filled in later */
if (ifd[idx].tag == 0xbcc1) /* IMAGE_COUNT */
cp->image_count_mark = ifd_mark + 2 + 12*idx + 8;
if (ifd[idx].tag == 0xbcc2) /* ALPHA_OFFSET */
cp->alpha_offset_mark = ifd_mark + 2 + 12*idx + 8;
if (ifd[idx].tag == 0xbcc3) /* ALPHA_COUNT */
cp->alpha_count_mark = ifd_mark + 2 + 12*idx + 8;
break;
case 5: /* URATIONAL */
case 10: /* SRATIONAL */
case 12: /* DOUBLE */
ifd[idx].value_.v_long += ifd_len;
break;
default: /* RESERVED */
assert(0);
}
}
unsigned char scr[12];
scr[0] = (num_ifd>>0) & 0xff;
scr[1] = (num_ifd>>8) & 0xff;
fwrite(scr, 1, 2, cp->fd);
for (idx = 0 ; idx < num_ifd ; idx += 1) {
scr[0] = (ifd[idx].tag >> 0) & 0xff;
scr[1] = (ifd[idx].tag >> 8) & 0xff;
scr[2] = (ifd[idx].type>> 0) & 0xff;
scr[3] = (ifd[idx].type>> 8) & 0xff;
scr[4] = (ifd[idx].cnt >> 0) & 0xff;
scr[5] = (ifd[idx].cnt >> 8) & 0xff;
scr[6] = (ifd[idx].cnt >>16) & 0xff;
scr[7] = (ifd[idx].cnt >>24) & 0xff;
switch (ifd[idx].type) {
case 1: /* BYTE */
case 2: /* UTF8 */
case 6: /* SBYTE */
case 7: /* UNDEFINED */
if (ifd[idx].cnt <= 4) {
scr[ 8] = ifd[idx].value_.v_byte[0];
scr[ 9] = ifd[idx].value_.v_byte[1];
scr[10] = ifd[idx].value_.v_byte[2];
scr[11] = ifd[idx].value_.v_byte[3];
} else {
scr[ 8] = (ifd[idx].value_.v_long >> 0) & 0xff;
scr[ 9] = (ifd[idx].value_.v_long >> 8) & 0xff;
scr[10] = (ifd[idx].value_.v_long >>16) & 0xff;
scr[11] = (ifd[idx].value_.v_long >>24) & 0xff;
}
break;
case 3: /* USHORT */
case 8: /* SSHORT */
if (ifd[idx].cnt <= 2) {
scr[ 8] = (ifd[idx].value_.v_short[0] >> 0) & 0xff;
scr[ 9] = (ifd[idx].value_.v_short[0] >> 8) & 0xff;
scr[10] = (ifd[idx].value_.v_short[1] >> 0) & 0xff;
scr[11] = (ifd[idx].value_.v_short[1] >> 8) & 0xff;
} else {
scr[ 8] = (ifd[idx].value_.v_long >> 0) & 0xff;
scr[ 9] = (ifd[idx].value_.v_long >> 8) & 0xff;
scr[10] = (ifd[idx].value_.v_long >>16) & 0xff;
scr[11] = (ifd[idx].value_.v_long >>24) & 0xff;
}
break;
case 4: /* ULONG */
case 9: /* SLONG */
case 11: /* FLOAT */
case 5: /* URATIONAL */
case 10: /* SRATIONAL */
case 12: /* DOUBLE */
scr[ 8] = (ifd[idx].value_.v_long >> 0) & 0xff;
scr[ 9] = (ifd[idx].value_.v_long >> 8) & 0xff;
scr[10] = (ifd[idx].value_.v_long >>16) & 0xff;
scr[11] = (ifd[idx].value_.v_long >>24) & 0xff;
break;
default:
assert(0);
break;
}
fwrite(scr, 1, 12, cp->fd);
}
cp->next_ifd_mark = ftell(cp->fd);
scr[0] = 0;
scr[1] = 0;
scr[2] = 0;
scr[3] = 0;
fwrite(scr, 1, 4, cp->fd);
fwrite(buf, 1, num_buf, cp->fd);
}
int jxrc_begin_image_data(jxr_container_t cp)
{
emit_ifd(cp);
return 0;
}
int jxrc_write_container_post(jxr_container_t cp)
{
uint32_t mark = ftell(cp->fd);
assert(mark > cp->image_offset_mark);
uint32_t count = mark - cp->image_offset_mark;
DEBUG("CONTAINER: measured bitstream count=%u\n", count);
fseek(cp->fd, cp->image_count_mark, SEEK_SET);
unsigned char scr[4];
scr[0] = (count >> 0) & 0xff;
scr[1] = (count >> 8) & 0xff;
scr[2] = (count >> 16) & 0xff;
scr[3] = (count >> 24) & 0xff;
fwrite(scr, 1, 4, cp->fd);
if(cp->separate_alpha_image_plane)
{
fseek(cp->fd, cp->alpha_offset_mark, SEEK_SET);
count = (mark+1) & (~1);
scr[0] = (count >> 0) & 0xff;
scr[1] = (count >> 8) & 0xff;
scr[2] = (count >> 16) & 0xff;
scr[3] = (count >> 24) & 0xff;
fwrite(scr, 1, 4, cp->fd);
fseek(cp->fd, mark, SEEK_SET);
// Make the mark even if it is odd by inserting a pad byte
if (mark & 1) {
fputc(0,cp->fd);
mark++;
}
}
cp->alpha_begin_mark = mark;
return 0;
}
int jxrc_write_container_post_alpha(jxr_container_t cp)
{
uint32_t mark = ftell(cp->fd);
uint32_t count = mark - cp->alpha_begin_mark;
DEBUG("CONTAINER: measured alpha count=%u\n", count);
if(cp->separate_alpha_image_plane)
{
unsigned char scr[4];
fseek(cp->fd, cp->alpha_count_mark, SEEK_SET);
count = mark;
scr[0] = (count >> 0) & 0xff;
scr[1] = (count >> 8) & 0xff;
scr[2] = (count >> 16) & 0xff;
scr[3] = (count >> 24) & 0xff;
fwrite(scr, 1, 4, cp->fd);
}
/*
** Why do we actually need all this??
**
fseek(cp->fd, mark, SEEK_SET);
// Make the mark even in case it is odd.
if (mark & 1) {
mark++;
}
**
*/
return 0;
}
/*
* $Log: cw_emit.c,v $
* Revision 1.10 2011-11-19 20:52:34 thor
* Fixed decoding of YUV422 in 10bpp, fixed 10bpp tiff reading and writing.
*
* Revision 1.9 2011-04-28 08:45:42 thor
* Fixed compiler warnings, ported to gcc 4.4, removed obsolete files.
*
* Revision 1.8 2010-10-03 13:14:42 thor
* Fixed missing preshift for BD32 images, added alpha-quantizer
* parameter. Fixed alpha plane container offset.
*
* Revision 1.7 2010-10-03 12:35:27 thor
* Fixed misaligned container sizes.
*
* Revision 1.6 2010-03-31 07:50:58 thor
* Replaced by the latest MS version.
*
* Revision 1.2 2009/05/29 12:00:00 microsoft
* Reference Software v1.6 updates.
*
* Revision 1.1 2009/04/13 12:00:00 microsoft
* Reference Software v1.5 updates.
*
*/