4 #ifdef HAVE_SYS_TYPES_H
15 #include <ktempfile.h>
17 #include <tqcstring.h>
24 #include <jasper/jasper.h>
28 #define DEFAULT_RATE 0.10
35 int cmptlut[MAXCMPTS];
37 jas_image_t* altimage;
42 read_image(
const TQImageIO* io )
49 if( ( qf = dynamic_cast<TQFile*>( io->ioDevice() ) ) ) {
51 in = jas_stream_fopen( TQFile::encodeName( qf->name() ),
"rb" );
54 tempf =
new KTempFile();
55 if( tempf->status() != 0 ) {
59 tempf->setAutoDelete(
true );
60 TQFile* out = tempf->file();
62 TQByteArray b( 4096 );
65 while( ( size = io->ioDevice()->readBlock( b.data(), 4096 ) ) > 0 ) {
67 if( ( out->writeBlock( b.data(), size ) ) == -1 )
break;
72 in = jas_stream_fopen( TQFile::encodeName( tempf->name() ),
"rb" );
79 jas_image_t* image = jas_image_decode( in, -1, 0 );
80 jas_stream_close( in );
88 convert_colorspace( gs_t& gs )
90 jas_cmprof_t *outprof = jas_cmprof_createfromclrspc( JAS_CLRSPC_SRGB );
91 if( !outprof )
return false;
93 gs.altimage = jas_image_chclrspc( gs.image, outprof,
94 JAS_CMXFORM_INTENT_PER );
95 if( !gs.altimage )
return false;
101 render_view( gs_t& gs, TQImage& qti )
103 if((gs.cmptlut[0] = jas_image_getcmptbytype(gs.altimage,
104 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 ||
105 (gs.cmptlut[1] = jas_image_getcmptbytype(gs.altimage,
106 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G))) < 0 ||
107 (gs.cmptlut[2] = jas_image_getcmptbytype(gs.altimage,
108 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))) < 0) {
112 const int* cmptlut = gs.cmptlut;
116 const int width = jas_image_cmptwidth( gs.altimage, cmptlut[0] );
117 const int height = jas_image_cmptheight( gs.altimage, cmptlut[0] );
118 for(
int i = 1; i < 3; ++i ) {
119 if (jas_image_cmptwidth( gs.altimage, cmptlut[i] ) != width ||
120 jas_image_cmptheight( gs.altimage, cmptlut[i] ) != height)
124 if( !qti.create( jas_image_width( gs.altimage ),
125 jas_image_height( gs.altimage ), 32 ) )
128 uint32_t* data = (uint32_t*)qti.bits();
130 for(
int y = 0; y < height; ++y ) {
131 for(
int x = 0; x < width; ++x ) {
132 for(
int k = 0; k < 3; ++k ) {
133 v[k] = jas_image_readcmptsample( gs.altimage, cmptlut[k], x, y );
136 v[k] <<= 8 - jas_image_cmptprec( gs.altimage, cmptlut[k] );
138 if( v[k] < 0 ) v[k] = 0;
139 else if( v[k] > 255 ) v[k] = 255;
142 *data++ = tqRgb( v[0], v[1], v[2] );
150 kimgio_jp2_read( TQImageIO* io )
152 if( jas_init() )
return;
155 if( !(gs.image = read_image( io )) )
return;
157 if( !convert_colorspace( gs ) )
return;
160 render_view( gs, image );
162 if( gs.image ) jas_image_destroy( gs.image );
163 if( gs.altimage ) jas_image_destroy( gs.altimage );
165 io->setImage( image );
171 create_image(
const TQImage& qi )
174 jas_image_cmptparm_t* cmptparms =
new jas_image_cmptparm_t[ 3 ];
176 for (
int i = 0; i < 3; ++i ) {
178 cmptparms[i].tlx = 0;
179 cmptparms[i].tly = 0;
182 cmptparms[i].hstep = 1;
183 cmptparms[i].vstep = 1;
184 cmptparms[i].width = qi.width();
185 cmptparms[i].height = qi.height();
188 cmptparms[i].prec = 8;
189 cmptparms[i].sgnd =
false;
192 jas_image_t* ji = jas_image_create( 3 , cmptparms, JAS_CLRSPC_UNKNOWN );
201 write_components( jas_image_t* ji,
const TQImage& qi )
203 const unsigned height = qi.height();
204 const unsigned width = qi.width();
206 jas_matrix_t* m = jas_matrix_create( height, width );
207 if( !m )
return false;
209 jas_image_setclrspc( ji, JAS_CLRSPC_SRGB );
211 jas_image_setcmpttype( ji, 0, JAS_IMAGE_CT_RGB_R );
212 for( uint y = 0; y < height; ++y )
213 for( uint x = 0; x < width; ++x )
214 jas_matrix_set( m, y, x, tqRed( qi.pixel( x, y ) ) );
215 jas_image_writecmpt( ji, 0, 0, 0, width, height, m );
217 jas_image_setcmpttype( ji, 1, JAS_IMAGE_CT_RGB_G );
218 for( uint y = 0; y < height; ++y )
219 for( uint x = 0; x < width; ++x )
220 jas_matrix_set( m, y, x, tqGreen( qi.pixel( x, y ) ) );
221 jas_image_writecmpt( ji, 1, 0, 0, width, height, m );
223 jas_image_setcmpttype( ji, 2, JAS_IMAGE_CT_RGB_B );
224 for( uint y = 0; y < height; ++y )
225 for( uint x = 0; x < width; ++x )
226 jas_matrix_set( m, y, x, tqBlue( qi.pixel( x, y ) ) );
227 jas_image_writecmpt( ji, 2, 0, 0, width, height, m );
228 jas_matrix_destroy( m );
234 kimgio_jp2_write( TQImageIO* io )
236 if( jas_init() )
return;
240 jas_stream_t* stream = 0;
243 KTempFile* ktempf = 0;
244 if( ( qf = dynamic_cast<TQFile*>( io->ioDevice() ) ) ) {
246 stream = jas_stream_fdopen( dup( qf->handle() ),
"w" );
248 ktempf =
new KTempFile;
249 ktempf->setAutoDelete(
true );
250 stream = jas_stream_fdopen( dup( ktempf->handle()),
"w" );
255 if( !stream )
return;
257 jas_image_t* ji = create_image( io->image() );
260 jas_stream_close( stream );
264 if( !write_components( ji, io->image() ) ) {
266 jas_stream_close( stream );
267 jas_image_destroy( ji );
276 TQTextStream ts( &rate, IO_WriteOnly );
278 << ( (io->quality() < 0) ? DEFAULT_RATE : io->quality() / 100.0F );
279 int i = jp2_encode( ji, stream, rate.utf8().data() );
281 jas_image_destroy( ji );
282 jas_stream_close( stream );
284 if( i != 0 ) {
delete ktempf;
return; }
288 TQFile* in = ktempf->file();
290 TQByteArray b( 4096 );
294 if( !in->at( 0 ) ) {
delete ktempf;
return; }
297 while( ( size = in->readBlock( b.data(), 4096 ) ) > 0 ) {
298 if( ( io->ioDevice()->writeBlock( b.data(), size ) ) == -1 ) {
303 io->ioDevice()->flush();
307 if( size == -1 )
return;
312 io->setStatus( IO_Ok );
315 #endif // HAVE_JASPER