Merge pull request #706 from mayeut/issue135
[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   if(box == NULL){
61                 return 0;
62   }
63   for( i=0;i<2;i++){
64     if (i)
65       opj_stream_seek( cio, lenp, p_manager);
66       
67     lenp = opj_stream_tell(cio);
68     opj_stream_skip(cio, 4, p_manager);         /* L [at the end]      */
69     opj_write_bytes(l_data_header,JPIP_PHIX,4); /* PHIX */
70     opj_stream_write_data(cio,l_data_header,4,p_manager);
71       
72     opj_write_manf( (int)i, cstr_info.numcomps, box, cio, p_manager );
73
74     for( compno=0; compno<(OPJ_UINT32)cstr_info.numcomps; compno++){       
75       box[compno].length = (OPJ_UINT32)opj_write_phixfaix( coff, (int)compno, cstr_info, EPHused, j2klen, cio,p_manager);
76       box[compno].type = JPIP_FAIX;
77     }
78
79     len = (OPJ_UINT32)(opj_stream_tell(cio)-lenp);
80     opj_stream_seek(cio, 4, p_manager);
81     opj_write_bytes(l_data_header,len,4);/* L              */
82     opj_stream_write_data(cio,l_data_header,4,p_manager);
83     opj_stream_seek( cio, lenp+len,p_manager);
84   }
85
86   opj_free(box);
87
88   return (int)len;
89 }
90
91
92 int opj_write_phixfaix( int coff, int compno, opj_codestream_info_t cstr_info, OPJ_BOOL EPHused, int j2klen, opj_stream_private_t *cio,
93               opj_event_mgr_t * p_manager )
94 {
95   OPJ_UINT32 tileno, version, i, nmax, size_of_coding; /* 4 or 8 */
96   opj_tile_info_t *tile_Idx;
97   opj_packet_info_t packet;
98   int resno, precno, layno;
99   OPJ_UINT32 num_packet;
100   int numOfres, numOfprec, numOflayers;
101   OPJ_BYTE l_data_header [8];
102   OPJ_OFF_T lenp;
103   OPJ_UINT32 len;
104
105   packet.end_ph_pos = packet.start_pos = -1;
106   (void)EPHused; /* unused ? */
107
108
109   if( j2klen > pow( 2, 32)){
110     size_of_coding =  8;
111     version = 1;
112   }
113   else{
114     size_of_coding = 4;
115     version = 0;
116   }
117
118   lenp = opj_stream_tell(cio);
119   opj_stream_skip(cio, 4, p_manager);         /* L [at the end]      */
120   opj_write_bytes(l_data_header,JPIP_FAIX,4); /* FAIX */
121   opj_stream_write_data(cio,l_data_header,4,p_manager);
122   opj_write_bytes(l_data_header,version,1);   /* Version 0 = 4 bytes */
123   opj_stream_write_data(cio,l_data_header,1,p_manager);
124
125   nmax = 0;
126   for( i=0; i<=(OPJ_UINT32)cstr_info.numdecompos[compno]; i++)
127     nmax += (OPJ_UINT32)(cstr_info.tile[0].ph[i] * cstr_info.tile[0].pw[i] * cstr_info.numlayers);
128   
129   opj_write_bytes(l_data_header,nmax,size_of_coding);         /* NMAX           */
130   opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
131   opj_write_bytes(l_data_header,(OPJ_UINT32)(cstr_info.tw*cstr_info.th),size_of_coding);  /* M              */
132   opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
133   
134   for( tileno=0; tileno<(OPJ_UINT32)(cstr_info.tw*cstr_info.th); tileno++){
135     tile_Idx = &cstr_info.tile[ tileno];
136     
137     num_packet = 0;
138     numOfres = cstr_info.numdecompos[compno] + 1;
139
140     for( resno=0; resno<numOfres ; resno++){
141       numOfprec = tile_Idx->pw[resno]*tile_Idx->ph[resno];
142       for( precno=0; precno<numOfprec; precno++){
143         numOflayers = cstr_info.numlayers;
144         for( layno=0; layno<numOflayers; layno++){
145           
146           switch ( cstr_info.prog){
147           case OPJ_LRCP:
148             packet = tile_Idx->packet[ ((layno*numOfres+resno)*cstr_info.numcomps+compno)*numOfprec+precno];
149             break;
150           case OPJ_RLCP:
151             packet = tile_Idx->packet[ ((resno*numOflayers+layno)*cstr_info.numcomps+compno)*numOfprec+precno];
152             break;
153           case OPJ_RPCL:
154             packet = tile_Idx->packet[ ((resno*numOfprec+precno)*cstr_info.numcomps+compno)*numOflayers+layno];
155             break;
156           case OPJ_PCRL:
157             packet = tile_Idx->packet[ ((precno*cstr_info.numcomps+compno)*numOfres+resno)*numOflayers + layno];
158             break;
159           case OPJ_CPRL:
160             packet = tile_Idx->packet[ ((compno*numOfprec+precno)*numOfres+resno)*numOflayers + layno];
161             break;
162           default:
163             fprintf( stderr, "failed to ppix indexing\n");
164           }
165
166     opj_write_bytes(l_data_header,(OPJ_UINT32)(packet.start_pos-coff),size_of_coding);            /* start position */
167     opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
168     opj_write_bytes(l_data_header,(OPJ_UINT32)(packet.end_ph_pos-packet.start_pos+1),size_of_coding); /* length         */
169     opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
170           
171           num_packet++;
172         }
173       }
174     }
175
176     /* PADDING */
177     while( num_packet < nmax){
178       opj_write_bytes(l_data_header,0,size_of_coding);/* start position            */
179       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
180       opj_write_bytes(l_data_header,0,size_of_coding);/* length                    */
181       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
182       num_packet++;
183     }
184   }
185
186   len = (OPJ_UINT32)(opj_stream_tell(cio)-lenp);
187   opj_stream_seek(cio, lenp,p_manager);
188   opj_write_bytes(l_data_header,len,4);/* L  */
189   opj_stream_write_data(cio,l_data_header,4,p_manager);
190   opj_stream_seek(cio, lenp+len,p_manager);
191
192   return (int)len;
193 }