15b24a4614b04cb33d17abce3d12f57dfd199863
[openjpeg.git] / src / lib / openjpip / imgreg_manager.c
1 /*
2  * $Id$
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 <math.h>
33 #include <stdlib.h>
34 #include <assert.h>
35 #include "imgreg_manager.h"
36
37 #ifdef SERVER
38 #include "fcgi_stdio.h"
39 #define logstream FCGI_stdout
40 #else
41 #define FCGI_stdout stdout
42 #define FCGI_stderr stderr
43 #define logstream stderr
44 #endif /*SERVER*/
45
46 imgreg_param_t map_viewin2imgreg( const int fx,    const int fy, 
47                                   const int rx,    const int ry,
48                                   const int rw,    const int rh,
49                                   const int XOsiz, const int YOsiz,
50                                   const int Xsiz,  const int Ysiz,
51                                   const int numOfreslev)
52 {
53   imgreg_param_t imgreg;
54   int px,py;
55   int xmax, ymax;
56
57   imgreg.xosiz = XOsiz;
58   imgreg.yosiz = YOsiz;
59   imgreg.fx = fx;
60   imgreg.fy = fy;
61   imgreg.level = 0;
62   xmax = Xsiz;
63   ymax = Ysiz;
64   
65   find_level( numOfreslev, &imgreg.level, &imgreg.fx, &imgreg.fy, &imgreg.xosiz, &imgreg.yosiz, &xmax, &ymax);
66
67   if( rx == -1 ||  ry == -1){
68     imgreg.ox = 0;
69     imgreg.oy = 0;
70   }
71   else{
72     imgreg.ox = rx*imgreg.fx/fx;
73     imgreg.oy = ry*imgreg.fy/fy;
74   }
75
76   if( rw == -1 || rh == -1){
77     imgreg.sx = imgreg.fx;
78     imgreg.sy = imgreg.fy;
79   }
80   else{
81     px = (int)ceil((double)((rx+rw)*imgreg.fx)/(double)fx);
82     py = (int)ceil((double)((ry+rh)*imgreg.fy)/(double)fy);
83     
84     if( imgreg.fx < px)
85       px = imgreg.fx;
86     if( imgreg.fy < py)
87       py = imgreg.fy;
88     
89     imgreg.sx = px - imgreg.ox;
90     imgreg.sy = py - imgreg.oy;
91   }
92
93   if( fx != imgreg.fx || fy != imgreg.fy)
94     fprintf( FCGI_stdout, "JPIP-fsiz: %d,%d\r\n", imgreg.fx, imgreg.fy);
95   
96   if( rw != imgreg.sx || rh != imgreg.sy)
97     fprintf( FCGI_stdout, "JPIP-rsiz: %d,%d\r\n", imgreg.sx, imgreg.sy);
98   
99   if( rx != imgreg.ox || ry != imgreg.oy)
100     fprintf( FCGI_stdout, "JPIP-roff: %d,%d\r\n", imgreg.ox, imgreg.oy);
101  
102   return imgreg;
103 }
104
105 void find_level( int maxlev, int *lev, int *fx, int *fy, int *xmin, int *ymin, int *xmax, int *ymax)
106 {
107   int xwidth = *xmax - *xmin;
108   int ywidth = *ymax - *ymin;
109
110   /* Find smaller frame size for now (i.e. assume "round-down"). */
111   if ((*fx < 1 && xwidth != 0) || (*fy < 1 && ywidth != 0)){
112     fprintf( FCGI_stderr, "Frame size must be strictly positive");
113     exit(-1);
114   }
115   else if( *lev < maxlev-1 && ( *fx < xwidth || *fy < ywidth)) {
116     /* Simulate the ceil function. */
117     *xmin = (int)ceil((double)*xmin/(double)2.0);
118     *ymin = (int)ceil((double)*ymin/(double)2.0);
119     *xmax = (int)ceil((double)*xmax/(double)2.0);
120     *ymax = (int)ceil((double)*ymax/(double)2.0);
121     
122     (*lev) ++;
123     find_level ( maxlev, lev, fx, fy, xmin, ymin, xmax, ymax);
124   } else {
125     *fx = xwidth;
126     *fy = ywidth;
127   }
128 }
129
130 int comp_decomplev( int fw, int fh, int Xsiz, int Ysiz)
131 {
132   int level;
133   int xmin, xmax, ymin, ymax;
134
135   level = 0;
136   xmin = ymin = 0;
137   xmax = Xsiz;
138   ymax = Ysiz;
139   
140   find_level( 1000, &level, &fw, &fh, &xmin, &ymin, &xmax, &ymax);
141
142   assert( level >= 0 );
143   return level;
144 }
145
146 void print_imgreg( imgreg_param_t imgreg)
147 {
148 #ifndef SERVER
149   fprintf( logstream, "codestream image region:\n");
150   fprintf( logstream, "\t fsiz: %d, %d\n", imgreg.fx, imgreg.fy);
151   fprintf( logstream, "\t roff: %d, %d\n", imgreg.ox, imgreg.oy);
152   fprintf( logstream, "\t rsiz: %d, %d\n", imgreg.sx, imgreg.sy);
153   fprintf( logstream, "\t level: %d\n", imgreg.level);
154 #else
155   (void)imgreg;
156 #endif
157 }