Reformat whole codebase with astyle.options (#128)
[openjpeg.git] / src / lib / openjpip / imgreg_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 <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,
66                &imgreg.yosiz, &xmax, &ymax);
67
68     if (rx == -1 ||  ry == -1) {
69         imgreg.ox = 0;
70         imgreg.oy = 0;
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     } else {
80         px = (int)ceil((double)((rx + rw) * imgreg.fx) / (double)fx);
81         py = (int)ceil((double)((ry + rh) * imgreg.fy) / (double)fy);
82
83         if (imgreg.fx < px) {
84             px = imgreg.fx;
85         }
86         if (imgreg.fy < py) {
87             py = imgreg.fy;
88         }
89
90         imgreg.sx = px - imgreg.ox;
91         imgreg.sy = py - imgreg.oy;
92     }
93
94     if (fx != imgreg.fx || fy != imgreg.fy) {
95         fprintf(FCGI_stdout, "JPIP-fsiz: %d,%d\r\n", imgreg.fx, imgreg.fy);
96     }
97
98     if (rw != imgreg.sx || rh != imgreg.sy) {
99         fprintf(FCGI_stdout, "JPIP-rsiz: %d,%d\r\n", imgreg.sx, imgreg.sy);
100     }
101
102     if (rx != imgreg.ox || ry != imgreg.oy) {
103         fprintf(FCGI_stdout, "JPIP-roff: %d,%d\r\n", imgreg.ox, imgreg.oy);
104     }
105
106     return imgreg;
107 }
108
109 void find_level(int maxlev, int *lev, int *fx, int *fy, int *xmin, int *ymin,
110                 int *xmax, int *ymax)
111 {
112     int xwidth = *xmax - *xmin;
113     int ywidth = *ymax - *ymin;
114
115     /* Find smaller frame size for now (i.e. assume "round-down"). */
116     if ((*fx < 1 && xwidth != 0) || (*fy < 1 && ywidth != 0)) {
117         fprintf(FCGI_stderr, "Frame size must be strictly positive");
118         exit(-1);
119     } else if (*lev < maxlev - 1 && (*fx < xwidth || *fy < ywidth)) {
120         /* Simulate the ceil function. */
121         *xmin = (int)ceil((double) * xmin / (double)2.0);
122         *ymin = (int)ceil((double) * ymin / (double)2.0);
123         *xmax = (int)ceil((double) * xmax / (double)2.0);
124         *ymax = (int)ceil((double) * ymax / (double)2.0);
125
126         (*lev) ++;
127         find_level(maxlev, lev, fx, fy, xmin, ymin, xmax, ymax);
128     } else {
129         *fx = xwidth;
130         *fy = ywidth;
131     }
132 }
133
134 int comp_decomplev(int fw, int fh, int Xsiz, int Ysiz)
135 {
136     int level;
137     int xmin, xmax, ymin, ymax;
138
139     level = 0;
140     xmin = ymin = 0;
141     xmax = Xsiz;
142     ymax = Ysiz;
143
144     find_level(1000, &level, &fw, &fh, &xmin, &ymin, &xmax, &ymax);
145
146     assert(level >= 0);
147     return level;
148 }
149
150 void print_imgreg(imgreg_param_t imgreg)
151 {
152 #ifndef SERVER
153     fprintf(logstream, "codestream image region:\n");
154     fprintf(logstream, "\t fsiz: %d, %d\n", imgreg.fx, imgreg.fy);
155     fprintf(logstream, "\t roff: %d, %d\n", imgreg.ox, imgreg.oy);
156     fprintf(logstream, "\t rsiz: %d, %d\n", imgreg.sx, imgreg.sy);
157     fprintf(logstream, "\t level: %d\n", imgreg.level);
158 #else
159     (void)imgreg;
160 #endif
161 }