Improved success for the linux build; OPJViewer shows all the COM contents
[openjpeg.git] / OPJViewer / source / imagjp2.cpp
index 99ef23c99526d19540c244e5531f71e73afb83dc..3c61905bc97c9f391190d5e0f860926fa5153998 100644 (file)
@@ -86,36 +86,44 @@ IMPLEMENT_DYNAMIC_CLASS(wxJP2Handler,wxImageHandler)
 \r
 /* sample error callback expecting a FILE* client object */\r
 void jp2_error_callback(const char *msg, void *client_data) {\r
-       char m_msg[MAX_MESSAGE_LEN];\r
        int message_len = strlen(msg) - 1;\r
        if (msg[message_len] != '\n')\r
                message_len = MAX_MESSAGE_LEN;\r
-       sprintf(m_msg, "[ERROR] %.*s", message_len, msg);\r
-    wxMutexGuiEnter();\r
-       wxLogMessage(m_msg);\r
+#ifndef __WXGTK__ \r
+               wxMutexGuiEnter();\r
+#endif /* __WXGTK__ */\r
+       wxLogMessage(wxT("[ERROR] %.*s"), message_len, msg);\r
+#ifndef __WXGTK__ \r
     wxMutexGuiLeave();\r
+#endif /* __WXGTK__ */\r
 }\r
+\r
 /* sample warning callback expecting a FILE* client object */\r
 void jp2_warning_callback(const char *msg, void *client_data) {\r
-       char m_msg[MAX_MESSAGE_LEN];\r
        int message_len = strlen(msg) - 1;\r
        if (msg[message_len] != '\n')\r
                message_len = MAX_MESSAGE_LEN;\r
-       sprintf(m_msg, "[WARNING] %.*s", message_len, msg);\r
-    wxMutexGuiEnter();\r
-       wxLogMessage(m_msg);\r
+#ifndef __WXGTK__ \r
+               wxMutexGuiEnter();\r
+#endif /* __WXGTK__ */\r
+       wxLogMessage(wxT("[WARNING] %.*s"), message_len, msg);\r
+#ifndef __WXGTK__ \r
     wxMutexGuiLeave();\r
+#endif /* __WXGTK__ */\r
 }\r
+\r
 /* sample debug callback expecting no client object */\r
 void jp2_info_callback(const char *msg, void *client_data) {\r
-       char m_msg[MAX_MESSAGE_LEN];\r
        int message_len = strlen(msg) - 1;\r
        if (msg[message_len] != '\n')\r
                message_len = MAX_MESSAGE_LEN;\r
-       sprintf(m_msg, "[INFO] %.*s", message_len, msg);\r
-    wxMutexGuiEnter();\r
-       wxLogMessage(m_msg);\r
+#ifndef __WXGTK__ \r
+               wxMutexGuiEnter();\r
+#endif /* __WXGTK__ */\r
+       wxLogMessage(wxT("[INFO] %.*s"), message_len, msg);\r
+#ifndef __WXGTK__ \r
     wxMutexGuiLeave();\r
+#endif /* __WXGTK__ */\r
 }\r
 \r
 // load the jp2 file format\r
@@ -127,6 +135,7 @@ bool wxJP2Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose,
        unsigned char *src = NULL;\r
     unsigned char *ptr;\r
        int file_length;\r
+       opj_codestream_info_t cstr_info;  /* Codestream information structure */\r
 \r
        // destroy the image\r
     image->Destroy();\r
@@ -149,6 +158,19 @@ bool wxJP2Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose,
        strncpy(parameters.outfile, "", sizeof(parameters.outfile)-1);\r
        parameters.decod_format = JP2_CFMT;\r
        parameters.cod_format = BMP_DFMT;\r
+       if (m_reducefactor)\r
+               parameters.cp_reduce = m_reducefactor;\r
+       if (m_qualitylayers)\r
+               parameters.cp_layer = m_qualitylayers;\r
+       /*if (n_components)\r
+               parameters. = n_components;*/\r
+\r
+       /* JPWL only */\r
+#ifdef USE_JPWL\r
+       parameters.jpwl_exp_comps = m_expcomps;\r
+       parameters.jpwl_max_tiles = m_maxtiles;\r
+       parameters.jpwl_correct = m_enablejpwl;\r
+#endif /* USE_JPWL */\r
 \r
        /* get a decoder handle */\r
        dinfo = opj_create_decompress(CODEC_JP2);\r
@@ -172,11 +194,15 @@ bool wxJP2Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose,
        cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);\r
 \r
        /* decode the stream and fill the image structure */\r
-       opjimage = opj_decode(dinfo, cio);\r
+       opjimage = opj_decode_with_info(dinfo, cio, &cstr_info);\r
        if (!opjimage) {\r
+#ifndef __WXGTK__ \r
                wxMutexGuiEnter();\r
-               wxLogError("JP2: failed to decode image!");\r
+#endif /* __WXGTK__ */\r
+               wxLogError(wxT("JP2: failed to decode image!"));\r
+#ifndef __WXGTK__ \r
                wxMutexGuiLeave();\r
+#endif /* __WXGTK__ */\r
                opj_destroy_decompress(dinfo);\r
                opj_cio_close(cio);\r
                free(src);\r
@@ -186,59 +212,16 @@ bool wxJP2Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose,
        /* close the byte stream */\r
        opj_cio_close(cio);\r
 \r
-       // check image size\r
-       if ((opjimage->numcomps != 1) && (opjimage->numcomps != 3)) {\r
-               wxMutexGuiEnter();\r
-               wxLogError("JP2: weird number of components");\r
-               wxMutexGuiLeave();\r
-               opj_destroy_decompress(dinfo);\r
-               free(src);\r
-               return false;\r
-       }\r
-\r
-\r
-       // prepare image size\r
-    image->Create(opjimage->comps[0].w, opjimage->comps[0].h, true );\r
-\r
-       // access image raw data\r
-    image->SetMask( false );\r
-    ptr = image->GetData();\r
-\r
-       // RGB color picture\r
-       if (opjimage->numcomps == 3) {\r
-               int row, col;\r
-               int *r = opjimage->comps[0].data;\r
-               int *g = opjimage->comps[1].data;\r
-               int *b = opjimage->comps[2].data;\r
-               for (row = 0; row < opjimage->comps[0].h; row++) {\r
-                       for (col = 0; col < opjimage->comps[0].w; col++) {\r
-                               \r
-                               *(ptr++) = *(r++);\r
-                               *(ptr++) = *(g++);\r
-                               *(ptr++) = *(b++);\r
-\r
-                       }\r
-               }\r
-       }\r
-\r
-       // B/W picture\r
-       if (opjimage->numcomps == 1) {\r
-               int row, col;\r
-               int *y = opjimage->comps[0].data;\r
-               for (row = 0; row < opjimage->comps[0].h; row++) {\r
-                       for (col = 0; col < opjimage->comps[0].w; col++) {\r
-                               \r
-                               *(ptr++) = *(y);\r
-                               *(ptr++) = *(y);\r
-                               *(ptr++) = *(y++);\r
-\r
-                       }\r
-               }\r
-       }\r
+       /* common rendering method */\r
+#include "imagjpeg2000.cpp"\r
 \r
-    wxMutexGuiEnter();\r
+#ifndef __WXGTK__ \r
+               wxMutexGuiEnter();\r
+#endif /* __WXGTK__ */\r
     wxLogMessage(wxT("JP2: image loaded."));\r
-    wxMutexGuiLeave();\r
+#ifndef __WXGTK__ \r
+               wxMutexGuiLeave();\r
+#endif /* __WXGTK__ */\r
 \r
        /* close openjpeg structs */\r
        opj_destroy_decompress(dinfo);\r
@@ -255,7 +238,14 @@ bool wxJP2Handler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose,
 // save the jp2 file format\r
 bool wxJP2Handler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )\r
 {\r
+#ifndef __WXGTK__ \r
+               wxMutexGuiEnter();\r
+#endif /* __WXGTK__ */\r
     wxLogError(wxT("JP2: Couldn't save image -> not implemented."));\r
+#ifndef __WXGTK__ \r
+               wxMutexGuiLeave();\r
+#endif /* __WXGTK__ */\r
+\r
     return false;\r
 }\r
 \r