[1.5][JPIP] new feature to target JP2 files from www (libcurl required)
[openjpeg.git] / applications / jpip / libopenjpip / faixbox_manager.c
1 /*
2  * $Id: faixbox_manager.c 53 2011-05-09 16:55:39Z kaori $
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) 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
35 #ifdef SERVER
36 #include "fcgi_stdio.h"
37 #define logstream FCGI_stdout
38 #else
39 #define FCGI_stdout stdout
40 #define FCGI_stderr stderr
41 #define logstream stderr
42 #endif //SERVER
43
44 faixbox_param_t * gene_faixbox( box_param_t *box)
45 {
46   faixbox_param_t *faix;
47   size_t numOfelem;
48   long pos = 0;
49
50   faix = ( faixbox_param_t *)malloc( sizeof(faixbox_param_t));
51   
52   faix->version = fetch_DBox1byte( box, (pos+=1)-1);
53   
54   if( faix->version < 0 || 3< faix->version){
55     fprintf( FCGI_stderr, "Error: version %d in faix box is reserved for ISO use.\n", faix->version);
56     free(faix);
57     return NULL;
58   }
59
60   if( faix->version%2){
61     subfaixbox8_param_t *subfaixbox;
62     int i;
63     
64     faix->subfaixbox.byte8_params = (subfaixbox8_param_t *)malloc( sizeof(subfaixbox8_param_t));
65     
66     subfaixbox = faix->subfaixbox.byte8_params;
67     subfaixbox->nmax = fetch_DBox8bytebigendian( box, (pos+=8)-8);
68     subfaixbox->m    = fetch_DBox8bytebigendian( box, (pos+=8)-8);
69     
70     numOfelem = subfaixbox->nmax*subfaixbox->m;
71     
72     subfaixbox->elem = ( faixelem8_param_t *)malloc( numOfelem*sizeof(faixelem8_param_t));
73     
74     if( faix->version == 3)
75       subfaixbox->aux = ( Byte4_t *)malloc( numOfelem*sizeof(Byte4_t));
76     
77     for( i=0; i<numOfelem; i++){
78       subfaixbox->elem[i].off = fetch_DBox8bytebigendian( box, (pos+=8)-8);
79       subfaixbox->elem[i].len = fetch_DBox8bytebigendian( box, (pos+=8)-8);
80       if( faix->version == 3)
81         subfaixbox->aux[i] = fetch_DBox4bytebigendian( box, (pos+=4)-4);
82     }
83   }
84   else{
85     subfaixbox4_param_t *subfaixbox;
86     int i;
87
88     faix->subfaixbox.byte4_params = (subfaixbox4_param_t *)malloc( sizeof(subfaixbox4_param_t));
89     
90     subfaixbox = faix->subfaixbox.byte4_params;
91     subfaixbox->nmax = fetch_DBox4bytebigendian( box, (pos+=4)-4);
92     subfaixbox->m    = fetch_DBox4bytebigendian( box, (pos+=4)-4);
93     
94     numOfelem = subfaixbox->nmax*subfaixbox->m;
95     
96     subfaixbox->elem = ( faixelem4_param_t *)malloc( numOfelem*sizeof(faixelem4_param_t));
97     
98     if( faix->version == 2)
99       subfaixbox->aux = ( Byte4_t *)malloc( numOfelem*sizeof(Byte4_t));
100     
101     for( i=0; i<numOfelem; i++){
102       subfaixbox->elem[i].off = fetch_DBox4bytebigendian( box, (pos+=4)-4);
103       subfaixbox->elem[i].len = fetch_DBox4bytebigendian( box, (pos+=4)-4);
104       if( faix->version == 2)
105         subfaixbox->aux[i] = fetch_DBox4bytebigendian( box, (pos+=4)-4);
106     }
107   }
108   return faix;
109 }
110
111 void print_faixbox( faixbox_param_t *faix)
112 {
113   Byte8_t i, j;
114
115   fprintf( logstream, "faix box info\n");
116   fprintf( logstream, "\tversion: %d\n", faix->version);
117   
118   fprintf( logstream, "\t nmax: %#llx = %lld\n", get_nmax( faix), get_nmax( faix));
119   fprintf( logstream, "\t m: %#llx = %lld\n", get_m( faix), get_m( faix));
120
121   for( i=0; i<get_m( faix); i++){
122     for( j=0; j<get_nmax( faix); j++){
123       fprintf( logstream, "\t off = %#llx, len = %#llx", get_elemOff( faix, j, i), get_elemLen( faix, j, i));
124       if( 2 <= faix->version)
125         fprintf( logstream, ", aux = %#x", get_elemAux( faix, j, i));
126       fprintf( logstream, "\n");
127     }
128     fprintf( logstream, "\n");
129   }
130 }
131
132 void delete_faixbox( faixbox_param_t **faix)
133 {
134   if((*faix)->version%2){
135     free((*faix)->subfaixbox.byte8_params->elem);
136     if( (*faix)->version == 3)
137       free((*faix)->subfaixbox.byte8_params->aux);
138     free((*faix)->subfaixbox.byte8_params);
139   }
140   else{
141     free((*faix)->subfaixbox.byte4_params->elem);
142     if( (*faix)->version == 2)
143       free((*faix)->subfaixbox.byte4_params->aux);
144     free((*faix)->subfaixbox.byte4_params);
145   }
146   free( *faix);
147 }
148
149 Byte8_t get_nmax( faixbox_param_t *faix)
150 {
151   if( faix->version%2)
152     return faix->subfaixbox.byte8_params->nmax;
153   else
154     return (Byte8_t)faix->subfaixbox.byte4_params->nmax;
155 }
156
157 Byte8_t get_m( faixbox_param_t *faix)
158 {
159   if( faix->version%2)
160     return faix->subfaixbox.byte8_params->m;
161   else
162     return (Byte8_t)faix->subfaixbox.byte4_params->m;
163 }
164
165 Byte8_t get_elemOff( faixbox_param_t *faix, Byte8_t elem_id, Byte8_t row_id)
166 {
167   Byte8_t nmax = get_nmax( faix);
168   if( faix->version%2)
169     return faix->subfaixbox.byte8_params->elem[ row_id*nmax+elem_id].off;
170   else
171     return (Byte8_t)faix->subfaixbox.byte4_params->elem[ row_id*nmax+elem_id].off;
172 }
173
174 Byte8_t get_elemLen( faixbox_param_t *faix, Byte8_t elem_id, Byte8_t row_id)
175 {
176   Byte8_t nmax = get_nmax( faix);
177   if( faix->version%2)
178     return faix->subfaixbox.byte8_params->elem[ row_id*nmax+elem_id].len;
179   else
180     return (Byte8_t)faix->subfaixbox.byte4_params->elem[ row_id*nmax+elem_id].len;
181 }
182
183 Byte4_t get_elemAux( faixbox_param_t *faix, Byte8_t elem_id, Byte8_t row_id)
184 {
185   if( faix->version <2)
186     return -1;
187
188   Byte8_t nmax = get_nmax( faix);
189   if( faix->version%2)
190     return faix->subfaixbox.byte8_params->aux[ row_id*nmax+elem_id];
191   else
192     return faix->subfaixbox.byte4_params->aux[ row_id*nmax+elem_id];
193 }