filters
xcfexport.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qcstring.h>
00021 #include <qdatastream.h>
00022 #include <qdom.h>
00023 #include <qfile.h>
00024 #include <qstring.h>
00025 #include <qvaluelist.h>
00026
00027 #include <kgenericfactory.h>
00028 #include <KoFilter.h>
00029 #include <KoFilterChain.h>
00030 #include <KoStore.h>
00031
00032 #include "vdocument.h"
00033 #include "vlayer.h"
00034 #include "xcfexport.h"
00035
00036 #include <kdebug.h>
00037
00038
00039
00040 const unsigned XcfExport::m_tileWidth = 64;
00041 const unsigned XcfExport::m_tileHeight = 64;
00042
00043
00044 typedef KGenericFactory<XcfExport, KoFilter> XcfExportFactory;
00045 K_EXPORT_COMPONENT_FACTORY( libkarbonxcfexport, XcfExportFactory( "kofficefilters" ) )
00046
00047
00048 XcfExport::XcfExport( KoFilter*, const char*, const QStringList& )
00049 : KoFilter()
00050 {
00051 m_zoomX = 1.0;
00052 m_zoomY = 1.0;
00053 }
00054
00055 KoFilter::ConversionStatus
00056 XcfExport::convert( const QCString& from, const QCString& to )
00057 {
00058 if( to != "image/x-xcf-gimp" || from != "application/x-karbon" )
00059 {
00060 return KoFilter::NotImplemented;
00061 }
00062
00063
00064 KoStoreDevice* storeIn = m_chain->storageFile( "root", KoStore::Read );
00065
00066 if( !storeIn )
00067 return KoFilter::StupidError;
00068
00069
00070 QFile fileOut( m_chain->outputFile() );
00071
00072 if( !fileOut.open( IO_WriteOnly ) )
00073 return KoFilter::StupidError;
00074
00075
00076 QDomDocument domIn;
00077 domIn.setContent( storeIn );
00078 QDomElement docNode = domIn.documentElement();
00079
00080 m_stream = new QDataStream( &fileOut );
00081
00082
00083
00084 VDocument doc;
00085 doc.load( docNode );
00086
00087
00088 doc.accept( *this );
00089
00090
00091 delete m_stream;
00092 fileOut.close();
00093
00094 return KoFilter::OK;
00095 }
00096
00097 void
00098 XcfExport::visitVDocument( VDocument& document )
00099 {
00100
00101 QIODevice::Offset current = 0;
00102 QIODevice::Offset start = 0;
00103 QIODevice::Offset end = 0;
00104
00105
00106 m_width = static_cast<unsigned>( document.width() * m_zoomX );
00107 m_height = static_cast<unsigned>( document.height() * m_zoomY );
00108
00109
00110
00111 m_stream->writeRawBytes( "gimp xcf file", 14 );
00112
00113
00114 *m_stream << static_cast<Q_UINT32>( m_width );
00115
00116
00117 *m_stream << static_cast<Q_UINT32>( m_height );
00118
00119
00120 *m_stream << static_cast<Q_UINT32>( 0 );
00121
00122
00123 *m_stream
00124
00125 << static_cast<Q_UINT32>( 0 )
00126
00127 << static_cast<Q_UINT32>( 0 );
00128
00129
00130
00131 current = m_stream->device()->at();
00132
00133
00134 m_stream->device()->at(
00135
00136 current + ( document.layers().count() + 3 + 2 ) * 4 );
00137
00138
00139
00140 VLayerListIterator itr( document.layers() );
00141
00142 for( ; itr.current(); ++itr )
00143 {
00144
00145 start = m_stream->device()->at();
00146
00147
00148
00149 itr.current()->accept( *this );
00150
00151
00152
00153 end = m_stream->device()->at();
00154
00155
00156 m_stream->device()->at( current );
00157
00158
00159 *m_stream << start;
00160
00161
00162 current = m_stream->device()->at();
00163
00164
00165 m_stream->device()->at( end );
00166 }
00167
00168
00169
00170 m_stream->device()->at( current );
00171
00172
00173 *m_stream << static_cast<Q_UINT32>( 0 );
00174
00175
00176
00177 m_stream->device()->at( end );
00178
00179
00180 *m_stream << static_cast<Q_UINT32>( 0 );
00181 }
00182
00183 void
00184 XcfExport::visitVLayer( VLayer& layer )
00185 {
00186
00187 *m_stream << static_cast<Q_UINT32>( m_width );
00188
00189
00190 *m_stream << static_cast<Q_UINT32>( m_height );
00191
00192
00193 *m_stream << static_cast<Q_UINT32>( 1 );
00194
00195
00196 *m_stream << layer.name().latin1();
00197
00198
00199 *m_stream << static_cast<Q_UINT32>( 6 );
00200
00201 *m_stream << static_cast<Q_UINT32>( 4 );
00202
00203 *m_stream << static_cast<Q_UINT32>( 255 );
00204
00205
00206 *m_stream << static_cast<Q_UINT32>( 8 );
00207
00208 *m_stream << static_cast<Q_UINT32>( 4 );
00209
00210 *m_stream << static_cast<Q_UINT32>( 1 );
00211
00212
00213 *m_stream << static_cast<Q_UINT32>( 9 );
00214
00215 *m_stream << static_cast<Q_UINT32>( 4 );
00216
00217 *m_stream << static_cast<Q_UINT32>( 0 );
00218
00219
00220 *m_stream << static_cast<Q_UINT32>( 10 );
00221
00222 *m_stream << static_cast<Q_UINT32>( 4 );
00223
00224 *m_stream << static_cast<Q_UINT32>( 0 );
00225
00226
00227 *m_stream << static_cast<Q_UINT32>( 11 );
00228
00229 *m_stream << static_cast<Q_UINT32>( 4 );
00230
00231 *m_stream << static_cast<Q_UINT32>( 0 );
00232
00233
00234 *m_stream << static_cast<Q_UINT32>( 12 );
00235
00236 *m_stream << static_cast<Q_UINT32>( 4 );
00237
00238 *m_stream << static_cast<Q_UINT32>( 0 );
00239
00240
00241 *m_stream << static_cast<Q_UINT32>( 13 );
00242
00243 *m_stream << static_cast<Q_UINT32>( 4 );
00244
00245 *m_stream << static_cast<Q_UINT32>( 0 );
00246
00247
00248 *m_stream << static_cast<Q_UINT32>( 15 );
00249
00250 *m_stream << static_cast<Q_UINT32>( 8 );
00251
00252 *m_stream << static_cast<Q_UINT32>( 0 );
00253
00254 *m_stream << static_cast<Q_UINT32>( 0 );
00255
00256
00257 *m_stream << static_cast<Q_UINT32>( 7 );
00258
00259 *m_stream << static_cast<Q_UINT32>( 4 );
00260
00261 *m_stream << static_cast<Q_UINT32>( 0 );
00262
00263
00264 *m_stream << static_cast<Q_UINT32>( 20 );
00265
00266 *m_stream << static_cast<Q_UINT32>( 4 );
00267
00268 *m_stream << static_cast<Q_UINT32>( 0 );
00269
00270
00271 *m_stream << static_cast<Q_UINT32>( 0 );
00272
00273 *m_stream << static_cast<Q_UINT32>( 0 );
00274
00275
00276
00277 QIODevice::Offset current = 0;
00278 QIODevice::Offset start = 0;
00279 QIODevice::Offset end = 0;
00280
00281
00282 current = m_stream->device()->at();
00283
00284
00285 m_stream->device()->at( current + 8 );
00286
00287
00288 start = m_stream->device()->at();
00289
00290
00291
00292 writeHierarchy();
00293
00294
00295
00296 end = m_stream->device()->at();
00297
00298
00299 m_stream->device()->at( current );
00300
00301
00302 *m_stream << start;
00303
00304
00305
00306 *m_stream << static_cast<Q_UINT32>( 0 );
00307 }
00308
00309 void
00310 XcfExport::writeHierarchy()
00311 {
00312
00313 QIODevice::Offset current = 0;
00314 QIODevice::Offset start = 0;
00315 QIODevice::Offset end = 0;
00316
00317
00318 *m_stream << m_width;
00319
00320
00321 *m_stream << m_height;
00322
00323
00324 *m_stream << static_cast<Q_UINT32>( 3 );
00325
00326
00327
00328 int levX = levels( m_width, m_tileWidth );
00329 int levY = levels( m_height, m_tileHeight );
00330 int levels = QMAX( levX, levY );
00331
00332 int width = m_width;
00333 int height = m_height;
00334
00335
00336 current = m_stream->device()->at();
00337
00338
00339 m_stream->device()->at( current + ( levels + 1 ) * 4 );
00340
00341 for( int i = 0; i < levels; ++i )
00342 {
00343
00344 start = m_stream->device()->at();
00345
00346 if( i == 0 )
00347 {
00348
00349 writeLevel();
00350 }
00351 else
00352 {
00353
00354 width /= 2;
00355 height /= 2;
00356
00357 *m_stream << static_cast<Q_UINT32>( width );
00358 *m_stream << static_cast<Q_UINT32>( height );
00359 *m_stream << static_cast<Q_UINT32>( 0 );
00360 }
00361
00362
00363 end = m_stream->device()->at();
00364
00365
00366 m_stream->device()->at( current );
00367
00368
00369 *m_stream << start;
00370
00371
00372 current = m_stream->device()->at();
00373
00374
00375 m_stream->device()->at( end );
00376 }
00377
00378
00379 m_stream->device()->at( current );
00380
00381
00382 *m_stream << static_cast<Q_UINT32>( 0 );
00383 }
00384
00385 void
00386 XcfExport::writeLevel()
00387 {
00388
00389 QIODevice::Offset current = 0;
00390 QIODevice::Offset start = 0;
00391 QIODevice::Offset end = 0;
00392
00393 *m_stream << static_cast<Q_UINT32>( m_width );
00394 *m_stream << static_cast<Q_UINT32>( m_height );
00395
00396 int rows = ( m_height + m_tileHeight - 1 ) / m_tileHeight;
00397 int cols = ( m_width + m_tileWidth - 1 ) / m_tileWidth;
00398 int tiles = rows * cols;
00399
00400
00401 current = m_stream->device()->at();
00402
00403
00404 m_stream->device()->at( current + ( tiles + 1 ) * 4 );
00405
00406 for( int i = 0; i < tiles; ++i )
00407 {
00408
00409 start = m_stream->device()->at();
00410
00411
00412
00413 *m_stream << static_cast<Q_UINT8>( 1 );
00414 *m_stream << static_cast<Q_UINT8>( 1 );
00415 *m_stream << static_cast<Q_UINT8>( 1 );
00416 *m_stream << static_cast<Q_UINT8>( 1 );
00417 *m_stream << static_cast<Q_UINT8>( 1 );
00418 *m_stream << static_cast<Q_UINT8>( 1 );
00419 *m_stream << static_cast<Q_UINT8>( 1 );
00420 *m_stream << static_cast<Q_UINT8>( 1 );
00421 *m_stream << static_cast<Q_UINT8>( 1 );
00422 *m_stream << static_cast<Q_UINT8>( 1 );
00423 *m_stream << static_cast<Q_UINT8>( 1 );
00424 *m_stream << static_cast<Q_UINT8>( 1 );
00425
00426
00427
00428 end = m_stream->device()->at();
00429
00430
00431 m_stream->device()->at( current );
00432
00433
00434 *m_stream << start;
00435
00436
00437 current = m_stream->device()->at();
00438
00439
00440 m_stream->device()->at( end );
00441 }
00442 }
00443
00444 int
00445 XcfExport::levels( int layerSize, int tileSize )
00446 {
00447 int l = 1;
00448
00449 while( layerSize > tileSize )
00450 {
00451 layerSize /= 2;
00452 l += 1;
00453 }
00454
00455 return l;
00456 }
00457
00458 #include "xcfexport.moc"
|