[trunk] updated copyright and added copyright notice required by ISO, in each file...
[openjpeg.git] / src / lib / openjp2 / phix_manager.c
1 /*
2  * $Id: phix_manager.c 897 2011-08-28 21:43:57Z Kaori.Hagihara@gmail.com $
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, Professor Benoit Macq
6  * Copyright (c) 2003-2004, Yannick Verschueren
7  * Copyright (c) 2010-2011, Kaori Hagihara
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*! \file
33  *  \brief Modification of jpip.c from 2KAN indexer
34  */
35
36 #include "opj_includes.h"
37
38
39 /* 
40  * Write faix box of phix
41  *
42  * @param[in] coff      offset of j2k codestream
43  * @param[in] compno    component number
44  * @param[in] cstr_info codestream information
45  * @param[in] EPHused   true if if EPH option used
46  * @param[in] j2klen    length of j2k codestream
47  * @param[in] cio       file output handle
48  * @return              length of faix box
49  */
50
51 int opj_write_phix( int coff, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused, int j2klen, opj_stream_private_t *cio,
52               opj_event_mgr_t * p_manager )
53 {
54   OPJ_BYTE l_data_header [8];
55   OPJ_UINT32 len, compno, i;
56   opj_jp2_box_t *box;
57   OPJ_OFF_T lenp = 0;
58
59   box = (opj_jp2_box_t *)opj_calloc( (size_t)cstr_info.numcomps, sizeof(opj_jp2_box_t));
60   
61   for( i=0;i<2;i++){
62     if (i)
63       opj_stream_seek( cio, lenp, p_manager);
64       
65     lenp = opj_stream_tell(cio);
66     opj_stream_skip(cio, 4, p_manager);         /* L [at the end]      */
67     opj_write_bytes(l_data_header,JPIP_PHIX,4); /* PHIX */
68     opj_stream_write_data(cio,l_data_header,4,p_manager);
69       
70     opj_write_manf( (int)i, cstr_info.numcomps, box, cio, p_manager );
71
72     for( compno=0; compno<(OPJ_UINT32)cstr_info.numcomps; compno++){       
73       box[compno].length = (OPJ_UINT32)opj_write_phixfaix( coff, (int)compno, cstr_info, EPHused, j2klen, cio,p_manager);
74       box[compno].type = JPIP_FAIX;
75     }
76
77     len = (OPJ_UINT32)(opj_stream_tell(cio)-lenp);
78     opj_stream_seek(cio, 4, p_manager);
79     opj_write_bytes(l_data_header,len,4);/* L              */
80     opj_stream_write_data(cio,l_data_header,4,p_manager);
81     opj_stream_seek( cio, lenp+len,p_manager);
82   }
83
84   opj_free(box);
85
86   return (int)len;
87 }
88
89
90 int opj_write_phixfaix( int coff, int compno, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused, int j2klen, opj_stream_private_t *cio,
91               opj_event_mgr_t * p_manager )
92 {
93   OPJ_UINT32 tileno, version, i, nmax, size_of_coding; /* 4 or 8 */
94   opj_tile_info_t *tile_Idx;
95   opj_packet_info_t packet;
96   int resno, precno, layno;
97   OPJ_UINT32 num_packet;
98   int numOfres, numOfprec, numOflayers;
99   OPJ_BYTE l_data_header [8];
100   OPJ_OFF_T lenp;
101   OPJ_UINT32 len;
102
103   packet.end_ph_pos = packet.start_pos = -1;
104   (void)EPHused; /* unused ? */
105
106
107   if( j2klen > pow( 2, 32)){
108     size_of_coding =  8;
109     version = 1;
110   }
111   else{
112     size_of_coding = 4;
113     version = 0;
114   }
115
116   lenp = opj_stream_tell(cio);
117   opj_stream_skip(cio, 4, p_manager);         /* L [at the end]      */
118   opj_write_bytes(l_data_header,JPIP_FAIX,4); /* FAIX */
119   opj_stream_write_data(cio,l_data_header,4,p_manager);
120   opj_write_bytes(l_data_header,version,1);   /* Version 0 = 4 bytes */
121   opj_stream_write_data(cio,l_data_header,1,p_manager);
122
123   nmax = 0;
124   for( i=0; i<=(OPJ_UINT32)cstr_info.numdecompos[compno]; i++)
125     nmax += (OPJ_UINT32)(cstr_info.tile[0].ph[i] * cstr_info.tile[0].pw[i] * cstr_info.numlayers);
126   
127   opj_write_bytes(l_data_header,nmax,size_of_coding);         /* NMAX           */
128   opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
129   opj_write_bytes(l_data_header,(OPJ_UINT32)(cstr_info.tw*cstr_info.th),size_of_coding);  /* M              */
130   opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
131   
132   for( tileno=0; tileno<(OPJ_UINT32)(cstr_info.tw*cstr_info.th); tileno++){
133     tile_Idx = &cstr_info.tile[ tileno];
134     
135     num_packet = 0;
136     numOfres = cstr_info.numdecompos[compno] + 1;
137
138     for( resno=0; resno<numOfres ; resno++){
139       numOfprec = tile_Idx->pw[resno]*tile_Idx->ph[resno];
140       for( precno=0; precno<numOfprec; precno++){
141         numOflayers = cstr_info.numlayers;
142         for( layno=0; layno<numOflayers; layno++){
143           
144           switch ( cstr_info.prog){
145           case OPJ_LRCP:
146             packet = tile_Idx->packet[ ((layno*numOfres+resno)*cstr_info.numcomps+compno)*numOfprec+precno];
147             break;
148           case OPJ_RLCP:
149             packet = tile_Idx->packet[ ((resno*numOflayers+layno)*cstr_info.numcomps+compno)*numOfprec+precno];
150             break;
151           case OPJ_RPCL:
152             packet = tile_Idx->packet[ ((resno*numOfprec+precno)*cstr_info.numcomps+compno)*numOflayers+layno];
153             break;
154           case OPJ_PCRL:
155             packet = tile_Idx->packet[ ((precno*cstr_info.numcomps+compno)*numOfres+resno)*numOflayers + layno];
156             break;
157           case OPJ_CPRL:
158             packet = tile_Idx->packet[ ((compno*numOfprec+precno)*numOfres+resno)*numOflayers + layno];
159             break;
160           default:
161             fprintf( stderr, "failed to ppix indexing\n");
162           }
163
164     opj_write_bytes(l_data_header,(OPJ_UINT32)(packet.start_pos-coff),size_of_coding);            /* start position */
165     opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
166     opj_write_bytes(l_data_header,(OPJ_UINT32)(packet.end_ph_pos-packet.start_pos+1),size_of_coding); /* length         */
167     opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
168           
169           num_packet++;
170         }
171       }
172     }
173
174     /* PADDING */
175     while( num_packet < nmax){
176       opj_write_bytes(l_data_header,0,size_of_coding);/* start position            */
177       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
178       opj_write_bytes(l_data_header,0,size_of_coding);/* length                    */
179       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
180       num_packet++;
181     }
182   }
183
184   len = (OPJ_UINT32)(opj_stream_tell(cio)-lenp);
185   opj_stream_seek(cio, lenp,p_manager);
186   opj_write_bytes(l_data_header,len,4);/* L  */
187   opj_stream_write_data(cio,l_data_header,4,p_manager);
188   opj_stream_seek(cio, lenp+len,p_manager);
189
190   return (int)len;
191 }