967bbad3033f8ccfc81f27a228aa9606e1289593
[openjpeg.git] / OPJViewer / source / imagjpeg2000.cpp
1 /*\r
2  * Copyright (c) 2007, Digital Signal Processing Laboratory, Universit� degli studi di Perugia (UPG), Italy\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  * 1. Redistributions of source code must retain the above copyright\r
9  *    notice, this list of conditions and the following disclaimer.\r
10  * 2. Redistributions in binary form must reproduce the above copyright\r
11  *    notice, this list of conditions and the following disclaimer in the\r
12  *    documentation and/or other materials provided with the distribution.\r
13  *\r
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
24  * POSSIBILITY OF SUCH DAMAGE.\r
25  */\r
26 /////////////////////////////////////////////////////////////////////////////\r
27 // Name:        imagjpeg2000.cpp\r
28 // Purpose:     wxImage JPEG 2000 imagage rendering common functions\r
29 // Author:      Giuseppe Baruffa\r
30 // RCS-ID:      $Id: imagjpeg2000.cpp,v 0.00 2007/04/27 22:11:00 MW Exp $\r
31 // Copyright:   (c) Giuseppe Baruffa\r
32 // Licence:     wxWindows licence\r
33 /////////////////////////////////////////////////////////////////////////////\r
34 \r
35 /*\r
36 \r
37 - At this point, we have the structure "opjimage" that is filled with decompressed\r
38   data, as processed by the OpenJPEG decompression engine\r
39 \r
40 - We need to fill the class "image" with the proper pixel sample values\r
41 \r
42 */\r
43 {\r
44         int shiftbpp;\r
45         int c, tempcomps;\r
46 \r
47         // check components number\r
48         if (m_components > opjimage->numcomps)\r
49                 m_components = opjimage->numcomps;\r
50 \r
51         // check image depth (only on the first one, for now)\r
52         if (m_components)\r
53                 shiftbpp = opjimage->comps[m_components - 1].prec - 8;\r
54         else\r
55                 shiftbpp = opjimage->comps[0].prec - 8;\r
56 \r
57         // prepare image size\r
58         if (m_components)\r
59                 image->Create(opjimage->comps[m_components - 1].w, opjimage->comps[m_components - 1].h, true);\r
60         else\r
61                 image->Create(opjimage->comps[0].w, opjimage->comps[0].h, true);\r
62 \r
63         // access image raw data\r
64     image->SetMask(false);\r
65     ptr = image->GetData();\r
66 \r
67         // workaround for components different from 1 or 3\r
68         if ((opjimage->numcomps != 1) && (opjimage->numcomps != 3)) {\r
69 #ifndef __WXGTK__ \r
70                 wxMutexGuiEnter();\r
71 #endif /* __WXGTK__ */\r
72                 wxLogMessage(wxT("JPEG2000: weird number of components"));\r
73 #ifndef __WXGTK__ \r
74                 wxMutexGuiLeave();\r
75 #endif /* __WXGTK__ */\r
76                 tempcomps = 1;\r
77         } else\r
78                 tempcomps = opjimage->numcomps;\r
79 \r
80         // workaround for subsampled components\r
81         for (c = 1; c < tempcomps; c++) {\r
82                 if ((opjimage->comps[c].w != opjimage->comps[c - 1].w) || (opjimage->comps[c].h != opjimage->comps[c - 1].h)) {\r
83                         tempcomps = 1;\r
84                         break;\r
85                 }\r
86         }\r
87 \r
88         // workaround for different precision components\r
89         for (c = 1; c < tempcomps; c++) {\r
90                 if (opjimage->comps[c].bpp != opjimage->comps[c - 1].bpp) {\r
91                         tempcomps = 1;\r
92                         break;\r
93                 }\r
94         }\r
95 \r
96         // only one component selected\r
97         if (m_components)\r
98                 tempcomps = 1;\r
99 \r
100         // RGB color picture\r
101         if (tempcomps == 3) {\r
102                 int row, col;\r
103                 int *r = opjimage->comps[0].data;\r
104                 int *g = opjimage->comps[1].data;\r
105                 int *b = opjimage->comps[2].data;\r
106                 if (shiftbpp > 0) {\r
107                         for (row = 0; row < opjimage->comps[0].h; row++) {\r
108                                 for (col = 0; col < opjimage->comps[0].w; col++) {\r
109                                         \r
110                                         *(ptr++) = (*(r++)) >> shiftbpp;\r
111                                         *(ptr++) = (*(g++)) >> shiftbpp;\r
112                                         *(ptr++) = (*(b++)) >> shiftbpp;\r
113 \r
114                                 }\r
115                         }\r
116 \r
117                 } else if (shiftbpp < 0) {\r
118                         for (row = 0; row < opjimage->comps[0].h; row++) {\r
119                                 for (col = 0; col < opjimage->comps[0].w; col++) {\r
120                                         \r
121                                         *(ptr++) = (*(r++)) << -shiftbpp;\r
122                                         *(ptr++) = (*(g++)) << -shiftbpp;\r
123                                         *(ptr++) = (*(b++)) << -shiftbpp;\r
124 \r
125                                 }\r
126                         }\r
127                         \r
128                 } else {\r
129                         for (row = 0; row < opjimage->comps[0].h; row++) {\r
130                                 for (col = 0; col < opjimage->comps[0].w; col++) {\r
131 \r
132                                         *(ptr++) = *(r++);\r
133                                         *(ptr++) = *(g++);\r
134                                         *(ptr++) = *(b++);\r
135                                 \r
136                                 }\r
137                         }\r
138                 }\r
139         }\r
140 \r
141         // B/W picture\r
142         if (tempcomps == 1) {\r
143                 int row, col;\r
144                 int selcomp;\r
145 \r
146                 if (m_components)\r
147                         selcomp = m_components - 1;\r
148                 else\r
149                         selcomp = 0;\r
150 \r
151                 int *y = opjimage->comps[selcomp].data;\r
152                 if (shiftbpp > 0) {\r
153                         for (row = 0; row < opjimage->comps[selcomp].h; row++) {\r
154                                 for (col = 0; col < opjimage->comps[selcomp].w; col++) {\r
155                                         \r
156                                         *(ptr++) = (*(y)) >> shiftbpp;\r
157                                         *(ptr++) = (*(y)) >> shiftbpp;\r
158                                         *(ptr++) = (*(y++)) >> shiftbpp;\r
159 \r
160                                 }\r
161                         }\r
162                 } else if (shiftbpp < 0) {\r
163                         for (row = 0; row < opjimage->comps[selcomp].h; row++) {\r
164                                 for (col = 0; col < opjimage->comps[selcomp].w; col++) {\r
165                                         \r
166                                         *(ptr++) = (*(y)) << -shiftbpp;\r
167                                         *(ptr++) = (*(y)) << -shiftbpp;\r
168                                         *(ptr++) = (*(y++)) << -shiftbpp;\r
169 \r
170                                 }\r
171                         }\r
172                 } else {\r
173                         for (row = 0; row < opjimage->comps[selcomp].h; row++) {\r
174                                 for (col = 0; col < opjimage->comps[selcomp].w; col++) {\r
175                                         \r
176                                         *(ptr++) = *(y);\r
177                                         *(ptr++) = *(y);\r
178                                         *(ptr++) = *(y++);\r
179 \r
180                                 }\r
181                         }\r
182                 }\r
183         }\r
184 \r
185 \r
186 }