714773c2f4d5ac08acdfd1264c808dc3200267ed
[openjpeg.git] / src / lib / openjpip / faixbox_manager.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "faixbox_manager.h"
34 #include "opj_inttypes.h"
35
36 #ifdef SERVER
37 #include "fcgi_stdio.h"
38 #define logstream FCGI_stdout
39 #else
40 #define FCGI_stdout stdout
41 #define FCGI_stderr stderr
42 #define logstream stderr
43 #endif /*SERVER*/
44
45 faixbox_param_t * gene_faixbox( box_param_t *box)
46 {
47   faixbox_param_t *faix;
48   size_t numOfelem;
49   long pos = 0;
50
51   faix = ( faixbox_param_t *)malloc( sizeof(faixbox_param_t));
52   
53   faix->version = fetch_DBox1byte( box, (pos+=1)-1);
54   
55   if( 3< faix->version){
56     fprintf( FCGI_stderr, "Error: version %d in faix box is reserved for ISO use.\n", faix->version);
57     free(faix);
58     return NULL;
59   }
60
61   if( faix->version%2){
62     subfaixbox8_param_t *subfaixbox;
63     size_t i;
64     
65     faix->subfaixbox.byte8_params = (subfaixbox8_param_t *)malloc( sizeof(subfaixbox8_param_t));
66     
67     subfaixbox = faix->subfaixbox.byte8_params;
68     subfaixbox->nmax = fetch_DBox8bytebigendian( box, (pos+=8)-8);
69     subfaixbox->m    = fetch_DBox8bytebigendian( box, (pos+=8)-8);
70     
71     numOfelem = subfaixbox->nmax*subfaixbox->m;
72     
73     subfaixbox->elem = ( faixelem8_param_t *)malloc( numOfelem*sizeof(faixelem8_param_t));
74     
75     if( faix->version == 3)
76       subfaixbox->aux = ( Byte4_t *)malloc( numOfelem*sizeof(Byte4_t));
77     
78     for( i=0; i<numOfelem; i++){
79       subfaixbox->elem[i].off = fetch_DBox8bytebigendian( box, (pos+=8)-8);
80       subfaixbox->elem[i].len = fetch_DBox8bytebigendian( box, (pos+=8)-8);
81       if( faix->version == 3)
82         subfaixbox->aux[i] = fetch_DBox4bytebigendian( box, (pos+=4)-4);
83     }
84   }
85   else{
86     subfaixbox4_param_t *subfaixbox;
87     size_t i;
88
89     faix->subfaixbox.byte4_params = (subfaixbox4_param_t *)malloc( sizeof(subfaixbox4_param_t));
90     
91     subfaixbox = faix->subfaixbox.byte4_params;
92     subfaixbox->nmax = fetch_DBox4bytebigendian( box, (pos+=4)-4);
93     subfaixbox->m    = fetch_DBox4bytebigendian( box, (pos+=4)-4);
94     
95     numOfelem = subfaixbox->nmax*subfaixbox->m;
96     
97     subfaixbox->elem = ( faixelem4_param_t *)malloc( numOfelem*sizeof(faixelem4_param_t));
98     
99     if( faix->version == 2)
100       subfaixbox->aux = ( Byte4_t *)malloc( numOfelem*sizeof(Byte4_t));
101     
102     for( i=0; i<numOfelem; i++){
103       subfaixbox->elem[i].off = fetch_DBox4bytebigendian( box, (pos+=4)-4);
104       subfaixbox->elem[i].len = fetch_DBox4bytebigendian( box, (pos+=4)-4);
105       if( faix->version == 2)
106         subfaixbox->aux[i] = fetch_DBox4bytebigendian( box, (pos+=4)-4);
107     }
108   }
109   return faix;
110 }
111
112 void print_faixbox( faixbox_param_t *faix)
113 {
114   Byte8_t i, j;
115
116   fprintf( logstream, "faix box info\n");
117   fprintf( logstream, "\tversion: %d\n", faix->version);
118   
119   fprintf( logstream, "\t nmax: %#" PRIx64 " = %" PRId64 "\n", get_nmax( faix), get_nmax( faix));
120   fprintf( logstream, "\t m: %#" PRIx64 " = %" PRId64 "\n", get_m( faix), get_m( faix));
121
122   for( i=0; i<get_m( faix); i++){
123     for( j=0; j<get_nmax( faix); j++){
124       fprintf( logstream, "\t off = %#" PRIx64 ", len = %#" PRIx64 "", get_elemOff( faix, j, i), get_elemLen( faix, j, i));
125       if( 2 <= faix->version)
126         fprintf( logstream, ", aux = %#x", get_elemAux( faix, j, i));
127       fprintf( logstream, "\n");
128     }
129     fprintf( logstream, "\n");
130   }
131 }
132
133 void delete_faixbox( faixbox_param_t **faix)
134 {
135   if((*faix)->version%2){
136     free((*faix)->subfaixbox.byte8_params->elem);
137     if( (*faix)->version == 3)
138       free((*faix)->subfaixbox.byte8_params->aux);
139     free((*faix)->subfaixbox.byte8_params);
140   }
141   else{
142     free((*faix)->subfaixbox.byte4_params->elem);
143     if( (*faix)->version == 2)
144       free((*faix)->subfaixbox.byte4_params->aux);
145     free((*faix)->subfaixbox.byte4_params);
146   }
147   free( *faix);
148 }
149
150 Byte8_t get_nmax( faixbox_param_t *faix)
151 {
152   if( faix->version%2)
153     return faix->subfaixbox.byte8_params->nmax;
154   else
155     return (Byte8_t)faix->subfaixbox.byte4_params->nmax;
156 }
157
158 Byte8_t get_m( faixbox_param_t *faix)
159 {
160   if( faix->version%2)
161     return faix->subfaixbox.byte8_params->m;
162   else
163     return (Byte8_t)faix->subfaixbox.byte4_params->m;
164 }
165
166 Byte8_t get_elemOff( faixbox_param_t *faix, Byte8_t elem_id, Byte8_t row_id)
167 {
168   Byte8_t nmax = get_nmax( faix);
169   if( faix->version%2)
170     return faix->subfaixbox.byte8_params->elem[ row_id*nmax+elem_id].off;
171   else
172     return (Byte8_t)faix->subfaixbox.byte4_params->elem[ row_id*nmax+elem_id].off;
173 }
174
175 Byte8_t get_elemLen( faixbox_param_t *faix, Byte8_t elem_id, Byte8_t row_id)
176 {
177   Byte8_t nmax = get_nmax( faix);
178   if( faix->version%2)
179     return faix->subfaixbox.byte8_params->elem[ row_id*nmax+elem_id].len;
180   else
181     return (Byte8_t)faix->subfaixbox.byte4_params->elem[ row_id*nmax+elem_id].len;
182 }
183
184 Byte4_t get_elemAux( faixbox_param_t *faix, Byte8_t elem_id, Byte8_t row_id)
185 {
186   Byte8_t nmax;
187   if( faix->version <2)
188     return (Byte4_t)-1;
189
190   nmax = get_nmax( faix);
191   if( faix->version%2)
192     return faix->subfaixbox.byte8_params->aux[ row_id*nmax+elem_id];
193   else
194     return faix->subfaixbox.byte4_params->aux[ row_id*nmax+elem_id];
195 }