8 #if !defined(__STDC_LIMIT_MACROS)
9 #define __STDC_LIMIT_MACROS
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
18 #include <tdetempfile.h>
20 #include <tqcstring.h>
27 #include <jasper/jasper.h>
31 #define DEFAULT_RATE 0.10
38 int cmptlut[MAXCMPTS];
40 jas_image_t* altimage;
45 read_image(
const TQImageIO* io )
52 if( ( qf = dynamic_cast<TQFile*>( io->ioDevice() ) ) ) {
54 in = jas_stream_fopen( TQFile::encodeName( qf->name() ),
"rb" );
57 tempf =
new KTempFile();
58 if( tempf->status() != 0 ) {
62 tempf->setAutoDelete(
true );
63 TQFile* out = tempf->file();
65 TQByteArray b( 4096 );
68 while( ( size = io->ioDevice()->readBlock( b.data(), 4096 ) ) > 0 ) {
70 if( ( out->writeBlock( b.data(), size ) ) == -1 )
break;
75 in = jas_stream_fopen( TQFile::encodeName( tempf->name() ),
"rb" );
82 jas_image_t* image = jas_image_decode( in, -1, 0 );
83 jas_stream_close( in );
91 convert_colorspace( gs_t& gs )
93 jas_cmprof_t *outprof = jas_cmprof_createfromclrspc( JAS_CLRSPC_SRGB );
94 if( !outprof )
return false;
96 gs.altimage = jas_image_chclrspc( gs.image, outprof,
97 JAS_CMXFORM_INTENT_PER );
98 if( !gs.altimage )
return false;
104 render_view( gs_t& gs, TQImage& qti )
106 if((gs.cmptlut[0] = jas_image_getcmptbytype(gs.altimage,
107 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 ||
108 (gs.cmptlut[1] = jas_image_getcmptbytype(gs.altimage,
109 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G))) < 0 ||
110 (gs.cmptlut[2] = jas_image_getcmptbytype(gs.altimage,
111 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))) < 0) {
115 const int* cmptlut = gs.cmptlut;
119 const int width = jas_image_cmptwidth( gs.altimage, cmptlut[0] );
120 const int height = jas_image_cmptheight( gs.altimage, cmptlut[0] );
121 for(
int i = 1; i < 3; ++i ) {
122 if (jas_image_cmptwidth( gs.altimage, cmptlut[i] ) != width ||
123 jas_image_cmptheight( gs.altimage, cmptlut[i] ) != height)
127 if( !qti.create( jas_image_width( gs.altimage ),
128 jas_image_height( gs.altimage ), 32 ) )
131 uint32_t* data = (uint32_t*)qti.bits();
133 for(
int y = 0; y < height; ++y ) {
134 for(
int x = 0; x < width; ++x ) {
135 for(
int k = 0; k < 3; ++k ) {
136 v[k] = jas_image_readcmptsample( gs.altimage, cmptlut[k], x, y );
139 v[k] <<= 8 - jas_image_cmptprec( gs.altimage, cmptlut[k] );
141 if( v[k] < 0 ) v[k] = 0;
142 else if( v[k] > 255 ) v[k] = 255;
145 *data++ = tqRgb( v[0], v[1], v[2] );
153 kimgio_jp2_read( TQImageIO* io )
155 if( jas_init() )
return;
158 if( !(gs.image = read_image( io )) )
return;
160 if( !convert_colorspace( gs ) )
return;
163 render_view( gs, image );
165 if( gs.image ) jas_image_destroy( gs.image );
166 if( gs.altimage ) jas_image_destroy( gs.altimage );
168 io->setImage( image );
174 create_image(
const TQImage& qi )
177 jas_image_cmptparm_t* cmptparms =
new jas_image_cmptparm_t[ 3 ];
179 for (
int i = 0; i < 3; ++i ) {
181 cmptparms[i].tlx = 0;
182 cmptparms[i].tly = 0;
185 cmptparms[i].hstep = 1;
186 cmptparms[i].vstep = 1;
187 cmptparms[i].width = qi.width();
188 cmptparms[i].height = qi.height();
191 cmptparms[i].prec = 8;
192 cmptparms[i].sgnd =
false;
195 jas_image_t* ji = jas_image_create( 3 , cmptparms, JAS_CLRSPC_UNKNOWN );
204 write_components( jas_image_t* ji,
const TQImage& qi )
206 const unsigned height = qi.height();
207 const unsigned width = qi.width();
209 jas_matrix_t* m = jas_matrix_create( height, width );
210 if( !m )
return false;
212 jas_image_setclrspc( ji, JAS_CLRSPC_SRGB );
214 jas_image_setcmpttype( ji, 0, JAS_IMAGE_CT_RGB_R );
215 for( uint y = 0; y < height; ++y )
216 for( uint x = 0; x < width; ++x )
217 jas_matrix_set( m, y, x, tqRed( qi.pixel( x, y ) ) );
218 jas_image_writecmpt( ji, 0, 0, 0, width, height, m );
220 jas_image_setcmpttype( ji, 1, JAS_IMAGE_CT_RGB_G );
221 for( uint y = 0; y < height; ++y )
222 for( uint x = 0; x < width; ++x )
223 jas_matrix_set( m, y, x, tqGreen( qi.pixel( x, y ) ) );
224 jas_image_writecmpt( ji, 1, 0, 0, width, height, m );
226 jas_image_setcmpttype( ji, 2, JAS_IMAGE_CT_RGB_B );
227 for( uint y = 0; y < height; ++y )
228 for( uint x = 0; x < width; ++x )
229 jas_matrix_set( m, y, x, tqBlue( qi.pixel( x, y ) ) );
230 jas_image_writecmpt( ji, 2, 0, 0, width, height, m );
231 jas_matrix_destroy( m );
237 kimgio_jp2_write( TQImageIO* io )
239 if( jas_init() )
return;
243 jas_stream_t* stream = 0;
246 KTempFile* ktempf = 0;
247 if( ( qf = dynamic_cast<TQFile*>( io->ioDevice() ) ) ) {
249 stream = jas_stream_fdopen( dup( qf->handle() ),
"w" );
251 ktempf =
new KTempFile;
252 ktempf->setAutoDelete(
true );
253 stream = jas_stream_fdopen( dup( ktempf->handle()),
"w" );
258 if( !stream )
return;
260 jas_image_t* ji = create_image( io->image() );
263 jas_stream_close( stream );
267 if( !write_components( ji, io->image() ) ) {
269 jas_stream_close( stream );
270 jas_image_destroy( ji );
279 TQTextStream ts( &rate, IO_WriteOnly );
281 << ( (io->quality() < 0) ? DEFAULT_RATE : io->quality() / 100.0F );
282 int i = jp2_encode( ji, stream, rate.utf8().data() );
284 jas_image_destroy( ji );
285 jas_stream_close( stream );
287 if( i != 0 ) {
delete ktempf;
return; }
291 TQFile* in = ktempf->file();
293 TQByteArray b( 4096 );
297 if( !in->at( 0 ) ) {
delete ktempf;
return; }
300 while( ( size = in->readBlock( b.data(), 4096 ) ) > 0 ) {
301 if( ( io->ioDevice()->writeBlock( b.data(), size ) ) == -1 ) {
306 io->ioDevice()->flush();
310 if( size == -1 )
return;
315 io->setStatus( IO_Ok );
318 #endif // HAVE_JASPER