[trunk] [trunk] remove old opj_tcp and rename opj_tcp_v2 to opj_tcp
[openjpeg.git] / src / lib / openjp2 / tpix_manager.c
1 /*
2  * $Id: tpix_manager.c 897 2011-08-28 21:43:57Z Kaori.Hagihara@gmail.com $
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, 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 #define MAX(a,b) ((a)>(b)?(a):(b))
39
40 /* 
41  * Get number of maximum tile parts per tile
42  *
43  * @param[in] cstr_info codestream information
44  * @return              number of maximum tile parts per tile
45  */
46 int get_num_max_tile_parts( opj_codestream_info_t cstr_info);
47
48
49 /* 
50  * Write faix box of tpix
51  *
52  * @param[in] coff offset of j2k codestream
53  * @param[in] compno    component number
54  * @param[in] cstr_info codestream information
55  * @param[in] j2klen    length of j2k codestream
56  * @param[in] cio       file output handle
57  * @return              length of faix box
58  */
59
60 int opj_write_tpix( int coff, 
61                     opj_codestream_info_t cstr_info, 
62                     int j2klen, opj_stream_private_t *cio,
63                     opj_event_mgr_t * p_manager )
64 {
65   OPJ_BYTE l_data_header [4];
66   OPJ_UINT32 len;
67   OPJ_OFF_T lenp;
68
69   lenp = opj_stream_tell(cio);
70   opj_stream_skip(cio, 4, p_manager);
71   opj_write_bytes(l_data_header,JPIP_TPIX,4); /* TPIX */
72   opj_stream_write_data(cio,l_data_header,4,p_manager);
73   
74   opj_write_tpixfaix( coff, 0, cstr_info, j2klen, cio,p_manager);
75
76   len = (OPJ_UINT32)(opj_stream_tell(cio)-lenp);
77  
78   opj_stream_skip(cio, lenp, 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   return len;
84 }
85
86 int opj_write_tpixfaix( int coff,
87                         int compno, 
88                         opj_codestream_info_t cstr_info, 
89                         int j2klen, 
90                         opj_stream_private_t *cio,
91                         opj_event_mgr_t * p_manager )
92 {
93   OPJ_UINT32 len;
94   OPJ_OFF_T lenp;
95   int i, j;
96   int Aux;
97   int num_max_tile_parts;
98   int size_of_coding; /* 4 or 8 */
99   opj_tp_info_t tp;
100   OPJ_BYTE l_data_header [8];
101   int version;
102
103   num_max_tile_parts = get_num_max_tile_parts( cstr_info);
104
105   if( j2klen > pow( 2, 32)){
106     size_of_coding =  8;
107     version = num_max_tile_parts == 1 ? 1:3;
108   }
109   else{
110     size_of_coding = 4;
111     version = num_max_tile_parts == 1 ? 0:2;
112   }
113
114   lenp = opj_stream_tell(cio);
115   opj_stream_skip(cio, 4, p_manager);         /* L [at the end]      */
116   opj_write_bytes(l_data_header,JPIP_FAIX,4); /* FAIX */
117   opj_stream_write_data(cio,l_data_header,4,p_manager);
118   opj_write_bytes(l_data_header,version,1);   /* Version 0 = 4 bytes */
119   opj_stream_write_data(cio,l_data_header,1,p_manager);
120
121   opj_write_bytes(l_data_header,num_max_tile_parts,size_of_coding);         /* NMAX           */
122   opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
123   opj_write_bytes(l_data_header,cstr_info.tw*cstr_info.th,size_of_coding);  /* M              */
124   opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
125
126   for (i = 0; i < cstr_info.tw*cstr_info.th; i++)
127     {
128     for (j = 0; j < cstr_info.tile[i].num_tps; j++)
129       {
130       tp = cstr_info.tile[i].tp[j];
131
132       opj_write_bytes(l_data_header,tp.tp_start_pos-coff,size_of_coding);            /* start position */
133       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
134       opj_write_bytes(l_data_header,tp.tp_end_pos-tp.tp_start_pos+1,size_of_coding); /* length         */
135       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
136
137       if (version & 0x02)
138         {
139         if( cstr_info.tile[i].num_tps == 1 && cstr_info.numdecompos[compno] > 1)
140           Aux = cstr_info.numdecompos[compno] + 1;
141         else
142           Aux = j + 1;
143
144         opj_write_bytes(l_data_header,Aux,4);
145         opj_stream_write_data(cio,l_data_header,4,p_manager);
146
147         /*cio_write(img.tile[i].tile_parts[j].num_reso_AUX,4);*/ /* Aux_i,j : Auxiliary value */
148         /* fprintf(stderr,"AUX value %d\n",Aux);*/
149         }
150       /*cio_write(0,4);*/
151       }
152     /* PADDING */
153     while (j < num_max_tile_parts)
154       {
155
156           opj_write_bytes(l_data_header,0,size_of_coding);/* start position            */
157       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
158       opj_write_bytes(l_data_header,0,size_of_coding);/* length                    */
159       opj_stream_write_data(cio,l_data_header,size_of_coding,p_manager);
160
161       if (version & 0x02)
162       opj_write_bytes(l_data_header,0,4);      /* Aux_i,j : Auxiliary value */
163       opj_stream_write_data(cio,l_data_header,4,p_manager);
164       j++;
165       }
166     }
167   
168   len = (OPJ_UINT32)(opj_stream_tell(cio)-lenp);
169   opj_stream_seek(cio, lenp,p_manager);
170   opj_write_bytes(l_data_header,len,4);/* L  */
171   opj_stream_write_data(cio,l_data_header,4,p_manager);
172   opj_stream_seek(cio, lenp+len,p_manager);
173
174   return len;
175 }
176
177 int get_num_max_tile_parts( opj_codestream_info_t cstr_info)
178 {
179   int num_max_tp = 0, i;
180
181   for( i=0; i<cstr_info.tw*cstr_info.th; i++)
182     num_max_tp = MAX( cstr_info.tile[i].num_tps, num_max_tp);
183   
184   return num_max_tp;
185 }