Fixed the handling of 16bit TIFF files for cinema compression. Modified "convert.c".
[openjpeg.git] / OPJViewer / source / imagjp2.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:        imagjp2.cpp\r
28 // Purpose:     wxImage JPEG 2000 file format handler\r
29 // Author:      Giuseppe Baruffa - based on imagjpeg.cpp, Vaclav Slavik\r
30 // RCS-ID:      $Id: imagjp2.cpp,v 0.00 2007/02/08 23:59:00 MW Exp $\r
31 // Copyright:   (c) Giuseppe Baruffa\r
32 // Licence:     wxWindows licence\r
33 /////////////////////////////////////////////////////////////////////////////\r
34 \r
35 // For compilers that support precompilation, includes "wx.h".\r
36 #include "wx/wxprec.h"\r
37 \r
38 #ifdef __BORLANDC__\r
39     #pragma hdrstop\r
40 #endif\r
41 \r
42 #if wxUSE_IMAGE && wxUSE_LIBOPENJPEG\r
43 \r
44 #include "imagjp2.h"\r
45 \r
46 #ifndef WX_PRECOMP\r
47     #include "wx/log.h"\r
48     #include "wx/app.h"\r
49     #include "wx/intl.h"\r
50     #include "wx/bitmap.h"\r
51     #include "wx/module.h"\r
52 #endif\r
53 \r
54 \r
55 #include "libopenjpeg/openjpeg.h"\r
56 \r
57 \r
58 #include "wx/filefn.h"\r
59 #include "wx/wfstream.h"\r
60 \r
61 // ----------------------------------------------------------------------------\r
62 // types\r
63 // ----------------------------------------------------------------------------\r
64 \r
65 \r
66 //-----------------------------------------------------------------------------\r
67 // wxJP2Handler\r
68 //-----------------------------------------------------------------------------\r
69 \r
70 IMPLEMENT_DYNAMIC_CLASS(wxJP2Handler,wxImageHandler)\r
71 \r
72 #if wxUSE_STREAMS\r
73 \r
74 //------------- JPEG 2000 Data Source Manager\r
75 \r
76 #define J2K_CFMT 0\r
77 #define JP2_CFMT 1\r
78 #define JPT_CFMT 2\r
79 #define MJ2_CFMT 3\r
80 #define PXM_DFMT 0\r
81 #define PGX_DFMT 1\r
82 #define BMP_DFMT 2\r
83 #define YUV_DFMT 3\r
84 \r
85 #define MAX_MESSAGE_LEN 200\r
86 \r
87 /* sample error callback expecting a FILE* client object */\r
88 void jp2_error_callback(const char *msg, void *client_data) {\r
89         int message_len = strlen(msg) - 1;\r
90         if (msg[message_len] != '\n')\r
91                 message_len = MAX_MESSAGE_LEN;\r
92     wxMutexGuiEnter();\r
93         wxLogMessage(wxT("[ERROR] %.*s"), message_len, msg);\r
94     wxMutexGuiLeave();\r
95 }\r
96 /* sample warning callback expecting a FILE* client object */\r
97 void jp2_warning_callback(const char *msg, void *client_data) {\r
98         int message_len = strlen(msg) - 1;\r
99         if (msg[message_len] != '\n')\r
100                 message_len = MAX_MESSAGE_LEN;\r
101     wxMutexGuiEnter();\r
102         wxLogMessage(wxT("[WARNING] %.*s"), message_len, msg);\r
103     wxMutexGuiLeave();\r
104 }\r
105 /* sample debug callback expecting no client object */\r
106 void jp2_info_callback(const char *msg, void *client_data) {\r
107         int message_len = strlen(msg) - 1;\r
108         if (msg[message_len] != '\n')\r
109                 message_len = MAX_MESSAGE_LEN;\r
110     wxMutexGuiEnter();\r
111         wxLogMessage(wxT("[INFO] %.*s"), message_len, msg);\r
112     wxMutexGuiLeave();\r
113 }\r
114 \r
115 \r
116 // load the jp2 file format\r
117 bool wxJP2Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose, int index)\r
118 {\r
119         opj_dparameters_t parameters;   /* decompression parameters */\r
120         opj_event_mgr_t event_mgr;              /* event manager */\r
121         opj_image_t *opjimage = NULL;\r
122         unsigned char *src = NULL;\r
123     unsigned char *ptr;\r
124         int file_length;\r
125 \r
126         // destroy the image\r
127     image->Destroy();\r
128 \r
129         /* handle to a decompressor */\r
130         opj_dinfo_t* dinfo = NULL;      \r
131         opj_cio_t *cio = NULL;\r
132 \r
133         /* configure the event callbacks (not required) */\r
134         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));\r
135         event_mgr.error_handler = jp2_error_callback;\r
136         event_mgr.warning_handler = jp2_warning_callback;\r
137         event_mgr.info_handler = jp2_info_callback;\r
138 \r
139         /* set decoding parameters to default values */\r
140         opj_set_default_decoder_parameters(&parameters);\r
141 \r
142         /* prepare parameters */\r
143         strncpy(parameters.infile, "", sizeof(parameters.infile)-1);\r
144         strncpy(parameters.outfile, "", sizeof(parameters.outfile)-1);\r
145         parameters.decod_format = JP2_CFMT;\r
146         parameters.cod_format = BMP_DFMT;\r
147         if (m_reducefactor)\r
148                 parameters.cp_reduce = m_reducefactor;\r
149         if (m_qualitylayers)\r
150                 parameters.cp_layer = m_qualitylayers;\r
151         /*if (n_components)\r
152                 parameters. = n_components;*/\r
153 \r
154         /* JPWL only */\r
155 #ifdef USE_JPWL\r
156         parameters.jpwl_exp_comps = m_expcomps;\r
157         parameters.jpwl_max_tiles = m_maxtiles;\r
158         parameters.jpwl_correct = m_enablejpwl;\r
159 #endif /* USE_JPWL */\r
160 \r
161         /* get a decoder handle */\r
162         dinfo = opj_create_decompress(CODEC_JP2);\r
163 \r
164         /* find length of the stream */\r
165         stream.SeekI(0, wxFromEnd);\r
166         file_length = (int) stream.TellI();\r
167 \r
168         /* get data */\r
169         stream.SeekI(0, wxFromStart);\r
170     src = (unsigned char *) malloc(file_length);\r
171         stream.Read(src, file_length);\r
172 \r
173         /* catch events using our callbacks and give a local context */\r
174         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);\r
175 \r
176         /* setup the decoder decoding parameters using user parameters */\r
177         opj_setup_decoder(dinfo, &parameters);\r
178 \r
179         /* open a byte stream */\r
180         cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);\r
181 \r
182         /* decode the stream and fill the image structure */\r
183         opjimage = opj_decode(dinfo, cio);\r
184         if (!opjimage) {\r
185                 wxMutexGuiEnter();\r
186                 wxLogError(wxT("JP2: failed to decode image!"));\r
187                 wxMutexGuiLeave();\r
188                 opj_destroy_decompress(dinfo);\r
189                 opj_cio_close(cio);\r
190                 free(src);\r
191                 return false;\r
192         }\r
193 \r
194         /* close the byte stream */\r
195         opj_cio_close(cio);\r
196 \r
197         // check image size\r
198         if ((opjimage->numcomps != 1) && (opjimage->numcomps != 3)) {\r
199                 wxMutexGuiEnter();\r
200                 wxLogError(wxT("JP2: weird number of components"));\r
201                 wxMutexGuiLeave();\r
202                 opj_destroy_decompress(dinfo);\r
203                 free(src);\r
204                 return false;\r
205         }\r
206 \r
207 \r
208         // prepare image size\r
209     image->Create(opjimage->comps[0].w, opjimage->comps[0].h, true );\r
210 \r
211         // access image raw data\r
212     image->SetMask( false );\r
213     ptr = image->GetData();\r
214 \r
215         // RGB color picture\r
216         if (opjimage->numcomps == 3) {\r
217                 int row, col;\r
218                 int *r = opjimage->comps[0].data;\r
219                 int *g = opjimage->comps[1].data;\r
220                 int *b = opjimage->comps[2].data;\r
221                 for (row = 0; row < opjimage->comps[0].h; row++) {\r
222                         for (col = 0; col < opjimage->comps[0].w; col++) {\r
223                                 \r
224                                 *(ptr++) = *(r++);\r
225                                 *(ptr++) = *(g++);\r
226                                 *(ptr++) = *(b++);\r
227 \r
228                         }\r
229                 }\r
230         }\r
231 \r
232         // B/W picture\r
233         if (opjimage->numcomps == 1) {\r
234                 int row, col;\r
235                 int *y = opjimage->comps[0].data;\r
236                 for (row = 0; row < opjimage->comps[0].h; row++) {\r
237                         for (col = 0; col < opjimage->comps[0].w; col++) {\r
238                                 \r
239                                 *(ptr++) = *(y);\r
240                                 *(ptr++) = *(y);\r
241                                 *(ptr++) = *(y++);\r
242 \r
243                         }\r
244                 }\r
245         }\r
246 \r
247     wxMutexGuiEnter();\r
248     wxLogMessage(wxT("JP2: image loaded."));\r
249     wxMutexGuiLeave();\r
250 \r
251         /* close openjpeg structs */\r
252         opj_destroy_decompress(dinfo);\r
253         opj_image_destroy(opjimage);\r
254         free(src);\r
255 \r
256         if (!image->Ok())\r
257                 return false;\r
258         else\r
259                 return true;\r
260 \r
261 }\r
262 \r
263 // save the jp2 file format\r
264 bool wxJP2Handler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )\r
265 {\r
266     wxLogError(wxT("JP2: Couldn't save image -> not implemented."));\r
267     return false;\r
268 }\r
269 \r
270 #ifdef __VISUALC__\r
271     #pragma warning(default:4611)\r
272 #endif /* VC++ */\r
273 \r
274 // recognize the JPEG 2000 starting box\r
275 bool wxJP2Handler::DoCanRead( wxInputStream& stream )\r
276 {\r
277     unsigned char hdr[23];\r
278 \r
279     if ( !stream.Read(hdr, WXSIZEOF(hdr)) )\r
280         return false;\r
281 \r
282     return (hdr[0] == 0x00 &&\r
283                         hdr[1] == 0x00 &&\r
284                         hdr[2] == 0x00 &&\r
285                         hdr[3] == 0x0C &&\r
286                         hdr[4] == 0x6A &&\r
287                         hdr[5] == 0x50 &&\r
288                         hdr[6] == 0x20 &&\r
289                         hdr[7] == 0x20 &&\r
290                         hdr[20] == 0x6A &&\r
291                         hdr[21] == 0x70 &&\r
292                         hdr[22] == 0x32);\r
293 }\r
294 \r
295 #endif   // wxUSE_STREAMS\r
296 \r
297 #endif   // wxUSE_LIBOPENJPEG\r