diff options
Diffstat (limited to 'kimgio/jp2.cpp')
| -rw-r--r-- | kimgio/jp2.cpp | 108 |
1 files changed, 97 insertions, 11 deletions
diff --git a/kimgio/jp2.cpp b/kimgio/jp2.cpp index 9ae7d3a89..7cd6879b1 100644 --- a/kimgio/jp2.cpp +++ b/kimgio/jp2.cpp @@ -2,7 +2,7 @@ #include "config.h" #ifdef HAVE_JASPER - +#include <unistd.h> #include "jp2.h" #if !defined(__STDC_LIMIT_MACROS) @@ -15,6 +15,7 @@ #ifdef HAVE_STDINT_H #include <stdint.h> #endif +#include <kdebug.h> #include <tdetempfile.h> #include <tqcolor.h> #include <tqcstring.h> @@ -45,7 +46,7 @@ jas_image_t* read_image( const TQImageIO* io ) { jas_stream_t* in = 0; - // for QIODevice's other than TQFile, a temp. file is used. + // for TQIODevice's other than TQFile, a temp. file is used. KTempFile* tempf = 0; TQFile* qf = 0; @@ -57,7 +58,7 @@ read_image( const TQImageIO* io ) tempf = new KTempFile(); if( tempf->status() != 0 ) { delete tempf; - return 0; + return nullptr; } // if tempf->setAutoDelete( true ); TQFile* out = tempf->file(); @@ -76,7 +77,7 @@ read_image( const TQImageIO* io ) } // else if( !in ) { delete tempf; - return 0; + return nullptr; } // if jas_image_t* image = jas_image_decode( in, -1, 0 ); @@ -148,16 +149,77 @@ render_view( gs_t& gs, TQImage& qti ) return true; } // render_view +static bool initializeJasper() +{ +#if defined(JAS_VERSION_MAJOR) && (JAS_VERSION_MAJOR >= 3) + jas_conf_clear(); + + // Limit JasPer memory usage to at most 512 MB + size_t memoryLimit = (512 * 1024) * 1024; + size_t jasperTotalMemory = jas_get_total_mem_size(); + if (!jasperTotalMemory) + { + jasperTotalMemory = JAS_DEFAULT_MAX_MEM_USAGE; + } + memoryLimit = memoryLimit < jasperTotalMemory ? memoryLimit : jasperTotalMemory; + jas_conf_set_max_mem_usage(memoryLimit); + + if (jas_init_library()) + { + return false; + } + + if (jas_init_thread()) + { + jas_cleanup_library(); + return false; + } + +#else + if (jas_init()) + { + return false; + } +#endif // defined(JAS_VERSION_MAJOR) && (JAS_VERSION_MAJOR >= 3) + + return true; +} + +static void cleanupJasper() +{ +#if defined(JAS_VERSION_MAJOR) && (JAS_VERSION_MAJOR >= 3) + jas_cleanup_thread(); + jas_cleanup_library(); +#endif +} -KDE_EXPORT void + + +TDE_EXPORT void kimgio_jp2_read( TQImageIO* io ) { - if( jas_init() ) return; + if (!initializeJasper()) + { + kdError(399) << "Failed to initialize JasPer library" << endl; + return; + } gs_t gs; - if( !(gs.image = read_image( io )) ) return; + gs.image = read_image(io); - if( !convert_colorspace( gs ) ) return; + if (!gs.image) + { + kdError(399) << "Failed to read JP2 image from IO." << endl; + cleanupJasper(); + return; + } + + if (!convert_colorspace(gs)) + { + kdError(399) << "Could not convert JP2 colorspace." << endl; + cleanupJasper(); + return; + } TQImage image; render_view( gs, image ); @@ -165,6 +227,8 @@ kimgio_jp2_read( TQImageIO* io ) if( gs.image ) jas_image_destroy( gs.image ); if( gs.altimage ) jas_image_destroy( gs.altimage ); + cleanupJasper(); + io->setImage( image ); io->setStatus( 0 ); } // kimgio_jp2_read @@ -233,10 +297,14 @@ write_components( jas_image_t* ji, const TQImage& qi ) return true; } // write_components -KDE_EXPORT void +TDE_EXPORT void kimgio_jp2_write( TQImageIO* io ) { - if( jas_init() ) return; + if (!initializeJasper()) + { + kdError(399) << "Failed to initialize JasPer library." << endl; + return; + } // open the stream. we write directly to the file if possible, to a // temporary file otherwise. @@ -255,12 +323,19 @@ kimgio_jp2_write( TQImageIO* io ) // by here, a jas_stream_t is open - if( !stream ) return; + if (!stream) + { + kdError(399) + << "Failed to create a stream to write JP2 image" << endl; + cleanupJasper(); + return; + } jas_image_t* ji = create_image( io->image() ); if( !ji ) { delete ktempf; jas_stream_close( stream ); + cleanupJasper(); return; } // if @@ -268,6 +343,7 @@ kimgio_jp2_write( TQImageIO* io ) delete ktempf; jas_stream_close( stream ); jas_image_destroy( ji ); + cleanupJasper(); return; } // if @@ -279,10 +355,20 @@ kimgio_jp2_write( TQImageIO* io ) TQTextStream ts( &rate, IO_WriteOnly ); ts << "rate=" << ( (io->quality() < 0) ? DEFAULT_RATE : io->quality() / 100.0F ); +# if defined(JAS_VERSION_MAJOR) && (JAS_VERSION_MAJOR >= 3) + const jas_image_fmtinfo_t *jp2_fmtinfo = jas_image_lookupfmtbyname("jp2"); + int i = -1; + if (jp2_fmtinfo) + { + i = jas_image_encode(ji, stream, jp2_fmtinfo->id, rate.utf8().data()); + } +# else int i = jp2_encode( ji, stream, rate.utf8().data() ); +# endif jas_image_destroy( ji ); jas_stream_close( stream ); + cleanupJasper(); if( i != 0 ) { delete ktempf; return; } |
