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
|
/*
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <math.h>
#include <unistd.h>
#include "j2k.h"
#include "cio.h"
#include "tcd.h"
#include "int.h"
#define JPIP_CIDX 0x63696478
#define JPIP_CPTR 0x63707472
#define JPIP_MANF 0x6d616e66
#define JPIP_FAIX 0x66616978
#define JPIP_MHIX 0x6d686978
#define JPIP_TPIX 0x74706978
#define JPIP_THIX 0x74686978
#define JPIP_PPIX 0x70706978
#define JPIP_PHIX 0x70686978
#define JPIP_FIDX 0x66696478
#define JPIP_FPTR 0x66707472
#define JPIP_PRXY 0x70727879
#define JPIP_IPTR 0x69707472
#define JP2C 0x6a703263
static info_marker_t marker_jpip[32], marker_local_jpip[32]; // SIZE to precise !
static int num_marker_jpip, num_marker_local_jpip;
void jpip_write_cptr(int offset, info_image_t img) // Codestream finder box (box)
{
int len, lenp;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_CPTR,4); // T
cio_write(0,2); // DR A PRECISER !!
cio_write(0,2); // CONT
cio_write(offset,8); // COFF A PRECISER !!
cio_write(img.codestream_size,8); // CLEN
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
}
void jpip_write_manf(int second, int v, info_marker_t *marker) // Manifest box (box)
{
int len, lenp, i;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_MANF,4); // T
if (second) // Write only during the second pass
{
for(i=0;i<v;i++)
{
cio_write(marker[i].len,4);
cio_write(marker[i].type,4);
}
}
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
}
int jpip_write_mhix(info_image_t img) // Main Header Index Table (box)
{
int len, lenp, i;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_MHIX, 4); // CIDX
cio_write(img.Main_head_end,8); // TLEN
for(i=0;i<img.num_marker;i++) // Marker restricted to 1 apparition
{
cio_write(img.marker[i].type,2);
cio_write(0,2);
cio_write(img.marker[i].start_pos,8);
cio_write(img.marker[i].len,2);
}
// Marker NOT restricted to 1 apparition
for(i=img.marker_mul.num_COD-1;i>=0;i--) // COD
{
cio_write(img.marker_mul.COD[i].type,2);
cio_write(i,2);
cio_write(img.marker_mul.COD[i].start_pos,8);
cio_write(img.marker_mul.COD[i].len,2);
}
for(i=img.marker_mul.num_COC-1;i>=0;i--) // COC
{
cio_write(img.marker_mul.COC[i].type,2);
cio_write(i,2);
cio_write(img.marker_mul.COC[i].start_pos,8);
cio_write(img.marker_mul.COC[i].len,2);
}
for(i=img.marker_mul.num_RGN-1;i>=0;i--) // RGN
{
cio_write(img.marker_mul.RGN[i].type,2);
cio_write(i,2);
cio_write(img.marker_mul.RGN[i].start_pos,8);
cio_write(img.marker_mul.RGN[i].len,2);
}
for(i=img.marker_mul.num_QCC-1;i>=0;i--) // QCC
{
cio_write(img.marker_mul.QCC[i].type,2);
cio_write(i,2);
cio_write(img.marker_mul.QCC[i].start_pos,8);
cio_write(img.marker_mul.QCC[i].len,2);
}
for(i=img.marker_mul.num_TLM-1;i>=0;i--) // TLM
{
cio_write(img.marker_mul.TLM[i].type,2);
cio_write(i,2);
cio_write(img.marker_mul.TLM[i].start_pos,8);
cio_write(img.marker_mul.TLM[i].len,2);
}
for(i=img.marker_mul.num_PLM-1;i>=0;i--) // PLM
{
cio_write(img.marker_mul.PLM[i].type,2);
cio_write(i,2);
cio_write(img.marker_mul.PLM[i].start_pos,8);
cio_write(img.marker_mul.PLM[i].len,2);
}
for(i=img.marker_mul.num_COM-1;i>=0;i--) // COM
{
cio_write(img.marker_mul.COM[i].type,2);
cio_write(i,2);
cio_write(img.marker_mul.COM[i].start_pos,8);
cio_write(img.marker_mul.COM[i].len,2);
}
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
return len;
}
int jpip_write_faix(int v, int compno, info_image_t img, j2k_cp_t *j2k_cp) // Fragment array Index box
{
int len, lenp, i;
int version = 0;
int tileno, resno, precno, layno, num_packet=0;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_FAIX, 4); // MHIX
cio_write(version,1); // Version 0 = 4 bytes
switch(v)
{
case 0: // TPIX
cio_write(1,version?8:4); // NMAX
cio_write(img.tw*img.th,version?8:4); // M
for (i=0;i<img.tw*img.th;i++)
{
cio_write(img.tile[i].start_pos,version?8:4);
cio_write(img.tile[i].end_pos-img.tile[i].start_pos,version?8:4);
}
break;
case 1: // THIX
cio_write(1,version?8:4); // NMAX
cio_write(img.tw*img.th,version?8:4); // M
for (i=0;i<img.tw*img.th;i++)
{
cio_write(img.tile[i].start_pos,version?8:4);
cio_write(img.tile[i].end_header-img.tile[i].start_pos,version?8:4);
}
break;
case 2: // PPIX NOT FINISHED !!
cio_write(img.num_packet_max,version?8:4); // NMAX
cio_write(img.tw*img.th,version?8:4); // M
for(tileno=0;tileno<img.tw*img.th;tileno++)
{
info_tile_t *tile_Idx = &img.tile[tileno];
info_compo_t *compo_Idx = &tile_Idx->compo[compno];
int correction;
if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH)
correction=3;
else
correction=1;
for(resno=0;resno<img.Decomposition+1;resno++)
{
info_reso_t *reso_Idx = &compo_Idx->reso[resno];
for (precno=0;precno<img.tile[tileno].pw*img.tile[tileno].ph;precno++)
{
info_prec_t *prec_Idx = &reso_Idx->prec[precno];
for(layno=0;layno<img.Layer;layno++)
{
info_layer_t *layer_Idx = &prec_Idx->layer[layno];
cio_write(layer_Idx->offset,version?8:4);
cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len,version?8:4);
num_packet++;
}
}
}
}
// PADDING
while (num_packet<img.num_packet_max)
{
cio_write(0,version?8:4);
cio_write(0,version?8:4);
num_packet++;
}
break;
case 3: // PHIX NOT FINISHED !!
cio_write(img.num_packet_max,version?8:4); // NMAX
cio_write(img.tw*img.th,version?8:4); // M
for(tileno=0;tileno<img.tw*img.th;tileno++)
{
info_tile_t *tile_Idx = &img.tile[tileno];
info_compo_t *compo_Idx = &tile_Idx->compo[compno];
int correction;
if(j2k_cp->tcps[tileno].csty&J2K_CP_CSTY_EPH)
correction=3;
else
correction=1;
for(resno=0;resno<img.Decomposition+1;resno++)
{
info_reso_t *reso_Idx = &compo_Idx->reso[resno];
for (precno=0;precno<img.tile[tileno].pw*img.tile[tileno].ph;precno++)
{
info_prec_t *prec_Idx = &reso_Idx->prec[precno];
for(layno=0;layno<img.Layer;layno++)
{
info_layer_t *layer_Idx = &prec_Idx->layer[layno];
cio_write(layer_Idx->offset,version?8:4);
cio_write((layer_Idx->len_header-correction)?0:layer_Idx->len_header,version?8:4);
num_packet++;
}
}
}
}
// PADDING
while (num_packet<img.num_packet_max)
{
cio_write(0,version?8:4);
cio_write(0,version?8:4);
num_packet++;
}
break;
}
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
return len;
}
int jpip_write_tpix(info_image_t img, j2k_cp_t *j2k_cp) // Tile-part Index Table box (superbox)
{
int len, lenp;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_TPIX, 4); // TPIX
jpip_write_faix(0,0,img, j2k_cp);
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
return len;
}
int jpip_write_thix(info_image_t img, j2k_cp_t *j2k_cp) // Tile header Index Table box (superbox)
{
int len, lenp;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_THIX, 4); // THIX
jpip_write_faix(1,0,img, j2k_cp);
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
return len;
}
int jpip_write_ppix(info_image_t img,j2k_cp_t *j2k_cp) // Precinct Packet Index Table box (superbox)
{
int len, lenp, compno, i;
for (i=0;i<2;i++)
{
if (i) cio_seek(lenp);
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_PPIX, 4); // PPIX
jpip_write_manf(i,img.Comp,marker_local_jpip);
num_marker_local_jpip=img.Comp;
for (compno=0; compno<img.Comp; compno++)
{
marker_local_jpip[compno].len=jpip_write_faix(2,compno,img, j2k_cp);
marker_local_jpip[compno].type=JPIP_FAIX;
}
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
}
return len;
}
int jpip_write_phix(info_image_t img, j2k_cp_t *j2k_cp) // Packet Header Index Table Box (superbox)
{
int len, lenp=0, compno, i;
for (i=0;i<2;i++)
{
if (i) cio_seek(lenp);
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_PHIX, 4); // PHIX
jpip_write_manf(i,img.Comp,marker_local_jpip);
for (compno=0; compno<img.Comp; compno++)
{
marker_local_jpip[compno].len=jpip_write_faix(3,compno,img, j2k_cp);
marker_local_jpip[compno].type=JPIP_FAIX;
}
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
}
return len;
}
int jpip_write_cidx(int offset, info_image_t img, j2k_cp_t *j2k_cp) // Codestream index box (superbox)
{
int len, lenp=0, i;
num_marker_jpip=0;
for (i=0;i<2;i++)
{
if(i)
cio_seek(lenp);
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_CIDX, 4); // CIDX
jpip_write_cptr(offset, img);
jpip_write_manf(i,num_marker_jpip, marker_jpip); // A definir
num_marker_jpip=0;
marker_jpip[num_marker_jpip].len=jpip_write_mhix(img);
marker_jpip[num_marker_jpip].type=JPIP_MHIX;
num_marker_jpip++;
marker_jpip[num_marker_jpip].len=jpip_write_tpix(img, j2k_cp);
marker_jpip[num_marker_jpip].type=JPIP_TPIX;
num_marker_jpip++;
marker_jpip[num_marker_jpip].len=jpip_write_thix(img, j2k_cp);
marker_jpip[num_marker_jpip].type=JPIP_THIX;
num_marker_jpip++;
marker_jpip[num_marker_jpip].len=jpip_write_ppix(img, j2k_cp);
marker_jpip[num_marker_jpip].type=JPIP_PPIX;
num_marker_jpip++;
marker_jpip[num_marker_jpip].len=jpip_write_phix(img, j2k_cp);
marker_jpip[num_marker_jpip].type=JPIP_PHIX;
num_marker_jpip++;
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
}
return len;
}
void jpip_write_iptr(int offset, int length)
{
int len, lenp;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_IPTR, 4); // IPTR
cio_write(offset,8);
cio_write(length,8);
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
}
void jpip_write_prxy(int offset_jp2c, int length_jp2c, int offset_idx, int length_idx)
{
int len, lenp;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_PRXY, 4); // IPTR
cio_write(offset_jp2c,8); // OOFF
cio_write(length_jp2c,4); // OBH part 1
cio_write(JP2C,4); // OBH part 2
cio_write(1,1); // NI
cio_write(offset_idx,8); // IOFF
cio_write(length_idx,4); // IBH part 1
cio_write(JPIP_CIDX,4); // IBH part 2
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
}
int jpip_write_fidx(int offset_jp2c, int length_jp2c, int offset_idx, int length_idx)
{
int len, lenp;
lenp=cio_tell();
cio_skip(4); // L [at the end]
cio_write(JPIP_FIDX, 4); // IPTR
jpip_write_prxy(offset_jp2c, length_jp2c, offset_idx, offset_jp2c);
len=cio_tell()-lenp;
cio_seek(lenp);
cio_write(len, 4); // L
cio_seek(lenp+len);
return len;
}
|