2 * Copyright (c) 2007, Digital Signal Processing Laboratory, Universit� degli studi di Perugia (UPG), Italy
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\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
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
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
35 // For compilers that support precompilation, includes "wx.h".
\r
36 #include "wx/wxprec.h"
\r
42 #if wxUSE_IMAGE && wxUSE_LIBOPENJPEG
\r
44 #include "imagjp2.h"
\r
49 #include "wx/intl.h"
\r
50 #include "wx/bitmap.h"
\r
51 #include "wx/module.h"
\r
55 #include "libopenjpeg/openjpeg.h"
\r
58 #include "wx/filefn.h"
\r
59 #include "wx/wfstream.h"
\r
61 // ----------------------------------------------------------------------------
\r
63 // ----------------------------------------------------------------------------
\r
66 //-----------------------------------------------------------------------------
\r
68 //-----------------------------------------------------------------------------
\r
70 IMPLEMENT_DYNAMIC_CLASS(wxJP2Handler,wxImageHandler)
\r
74 //------------- JPEG 2000 Data Source Manager
\r
85 #define MAX_MESSAGE_LEN 200
\r
87 /* sample error callback expecting a FILE* client object */
\r
88 void jp2_error_callback(const char *msg, void *client_data) {
\r
89 char m_msg[MAX_MESSAGE_LEN];
\r
90 int message_len = strlen(msg) - 1;
\r
91 if (msg[message_len] != '\n')
\r
92 message_len = MAX_MESSAGE_LEN;
\r
93 sprintf(m_msg, "[ERROR] %.*s", message_len, msg);
\r
95 wxLogMessage(m_msg);
\r
98 /* sample warning callback expecting a FILE* client object */
\r
99 void jp2_warning_callback(const char *msg, void *client_data) {
\r
100 char m_msg[MAX_MESSAGE_LEN];
\r
101 int message_len = strlen(msg) - 1;
\r
102 if (msg[message_len] != '\n')
\r
103 message_len = MAX_MESSAGE_LEN;
\r
104 sprintf(m_msg, "[WARNING] %.*s", message_len, msg);
\r
106 wxLogMessage(m_msg);
\r
109 /* sample debug callback expecting no client object */
\r
110 void jp2_info_callback(const char *msg, void *client_data) {
\r
111 char m_msg[MAX_MESSAGE_LEN];
\r
112 int message_len = strlen(msg) - 1;
\r
113 if (msg[message_len] != '\n')
\r
114 message_len = MAX_MESSAGE_LEN;
\r
115 sprintf(m_msg, "[INFO] %.*s", message_len, msg);
\r
117 wxLogMessage(m_msg);
\r
121 // load the jp2 file format
\r
122 bool wxJP2Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose, int index)
\r
124 opj_dparameters_t parameters; /* decompression parameters */
\r
125 opj_event_mgr_t event_mgr; /* event manager */
\r
126 opj_image_t *opjimage = NULL;
\r
127 unsigned char *src = NULL;
\r
128 unsigned char *ptr;
\r
131 // destroy the image
\r
134 /* handle to a decompressor */
\r
135 opj_dinfo_t* dinfo = NULL;
\r
136 opj_cio_t *cio = NULL;
\r
138 /* configure the event callbacks (not required) */
\r
139 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
\r
140 event_mgr.error_handler = jp2_error_callback;
\r
141 event_mgr.warning_handler = jp2_warning_callback;
\r
142 event_mgr.info_handler = jp2_info_callback;
\r
144 /* set decoding parameters to default values */
\r
145 opj_set_default_decoder_parameters(¶meters);
\r
147 /* prepare parameters */
\r
148 strncpy(parameters.infile, "", sizeof(parameters.infile)-1);
\r
149 strncpy(parameters.outfile, "", sizeof(parameters.outfile)-1);
\r
150 parameters.decod_format = JP2_CFMT;
\r
151 parameters.cod_format = BMP_DFMT;
\r
153 /* get a decoder handle */
\r
154 dinfo = opj_create_decompress(CODEC_JP2);
\r
156 /* find length of the stream */
\r
157 stream.SeekI(0, wxFromEnd);
\r
158 file_length = (int) stream.TellI();
\r
161 stream.SeekI(0, wxFromStart);
\r
162 src = (unsigned char *) malloc(file_length);
\r
163 stream.Read(src, file_length);
\r
165 /* catch events using our callbacks and give a local context */
\r
166 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
\r
168 /* setup the decoder decoding parameters using user parameters */
\r
169 opj_setup_decoder(dinfo, ¶meters);
\r
171 /* open a byte stream */
\r
172 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
\r
174 /* decode the stream and fill the image structure */
\r
175 opjimage = opj_decode(dinfo, cio);
\r
178 wxLogError("JP2: failed to decode image!");
\r
180 opj_destroy_decompress(dinfo);
\r
181 opj_cio_close(cio);
\r
186 /* close the byte stream */
\r
187 opj_cio_close(cio);
\r
189 // check image size
\r
190 if ((opjimage->numcomps != 1) && (opjimage->numcomps != 3)) {
\r
192 wxLogError("JP2: weird number of components");
\r
194 opj_destroy_decompress(dinfo);
\r
200 // prepare image size
\r
201 image->Create(opjimage->comps[0].w, opjimage->comps[0].h, true );
\r
203 // access image raw data
\r
204 image->SetMask( false );
\r
205 ptr = image->GetData();
\r
207 // RGB color picture
\r
208 if (opjimage->numcomps == 3) {
\r
210 int *r = opjimage->comps[0].data;
\r
211 int *g = opjimage->comps[1].data;
\r
212 int *b = opjimage->comps[2].data;
\r
213 for (row = 0; row < opjimage->comps[0].h; row++) {
\r
214 for (col = 0; col < opjimage->comps[0].w; col++) {
\r
225 if (opjimage->numcomps == 1) {
\r
227 int *y = opjimage->comps[0].data;
\r
228 for (row = 0; row < opjimage->comps[0].h; row++) {
\r
229 for (col = 0; col < opjimage->comps[0].w; col++) {
\r
240 wxLogMessage(wxT("JP2: image loaded."));
\r
243 /* close openjpeg structs */
\r
244 opj_destroy_decompress(dinfo);
\r
245 opj_image_destroy(opjimage);
\r
255 // save the jp2 file format
\r
256 bool wxJP2Handler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
\r
258 wxLogError(wxT("JP2: Couldn't save image -> not implemented."));
\r
263 #pragma warning(default:4611)
\r
266 // recognize the JPEG 2000 starting box
\r
267 bool wxJP2Handler::DoCanRead( wxInputStream& stream )
\r
269 unsigned char hdr[23];
\r
271 if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
\r
274 return (hdr[0] == 0x00 &&
\r
287 #endif // wxUSE_STREAMS
\r
289 #endif // wxUSE_LIBOPENJPEG
\r