summaryrefslogtreecommitdiffstats
path: root/ktnef/gui/qwmf.cpp
blob: e4baf41d2ae1f7bee2339e912d0998dfb7895669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
/* Windows Meta File Loader/Painter Class Implementation
 *
 * Copyright ( C ) 1998 Stefan Taferner
 * Modified 2002 thierry lorthiois
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or ( at your
 * option ) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details. You should have received a copy
 * of the GNU General Public License along with this program; if not, write
 * to the Free Software Foundation, Inc, 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

#include <config.h>

#include <math.h>
#include <assert.h>
#include <tqfileinfo.h>
#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqdatastream.h>
#include <tqapplication.h>
#include <tqbuffer.h>
#include <kdebug.h>

bool qwmfDebug = false;

#include "qwmf.h"
#include "wmfstruct.h"
#include "metafuncs.h"

#define TQWMF_DEBUG  0


class WmfCmd
{
public:
    ~WmfCmd() { if ( next ) delete next; }
    WmfCmd* next;
    unsigned short funcIndex;
    long  numParm;
    short* parm;
};


class WinObjHandle
{
public:
    WinObjHandle() {}
    virtual ~WinObjHandle() {}
    virtual void apply( TQPainter& p ) = 0;
};

class WinObjBrushHandle: public WinObjHandle
{
public:
    virtual void apply( TQPainter& p );
    TQBrush brush;
    virtual ~WinObjBrushHandle() {};
};

class WinObjPenHandle: public WinObjHandle
{
public:
    virtual void apply( TQPainter& p );
    TQPen pen;
    virtual ~WinObjPenHandle() {};
};

class WinObjPatternBrushHandle: public WinObjHandle
{
public:
    virtual void apply( TQPainter& p );
    TQBrush brush;
    TQPixmap image;
    virtual ~WinObjPatternBrushHandle() {};
};

class WinObjFontHandle: public WinObjHandle
{
public:
    virtual void apply( TQPainter& p );
    TQFont font;
    int rotation;
    virtual ~WinObjFontHandle() {};
};

void WinObjBrushHandle::apply( TQPainter& p )
{
    p.setBrush( brush );
}

void WinObjPenHandle::apply( TQPainter& p )
{
    p.setPen( pen );
}

void WinObjPatternBrushHandle::apply( TQPainter& p )
{
    p.setBrush( brush );
}

void WinObjFontHandle::apply( TQPainter& p )
{
    p.setFont( font );
}

#define MAX_OBJHANDLE 64



//-----------------------------------------------------------------------------
TQWinMetaFile::TQWinMetaFile()
{
    mValid       = false;
    mFirstCmd    = NULL;
    mObjHandleTab = NULL;
    mDpi         = 1000;
}


//-----------------------------------------------------------------------------
TQWinMetaFile::~TQWinMetaFile()
{
    if ( mFirstCmd ) delete mFirstCmd;
    if ( mObjHandleTab ) delete[] mObjHandleTab;
}


//-----------------------------------------------------------------------------
bool TQWinMetaFile::load( const TQString &filename )
{
    TQFile file( filename );

    if ( !file.exists() )
    {
        kdDebug() << TQString("File ") << TQString(TQFile::encodeName(filename)) << TQString(" does not exist") << endl;
        return false;
    }

    if ( !file.open( IO_ReadOnly ) )
    {
        kdDebug() << TQString("Cannot open file ") << TQString(TQFile::encodeName(filename)) << endl;
        return false;
    }

    TQByteArray ba = file.readAll();
    file.close();

    TQBuffer buffer( ba );
    buffer.open( IO_ReadOnly );
    return load( buffer );
}

//-----------------------------------------------------------------------------
bool TQWinMetaFile::load( TQBuffer &buffer )
{
    TQDataStream st;
    WmfEnhMetaHeader eheader;
    WmfMetaHeader header;
    WmfPlaceableHeader pheader;
    WORD checksum;
    int filePos, idx, i;
    WmfCmd *cmd, *last;
    DWORD rdSize;
    WORD rdFunc;

    mTextAlign = 0;
    mRotation = 0;
    mTextColor = TQt::black;
    if ( mFirstCmd ) delete mFirstCmd;
    mFirstCmd = NULL;

    st.setDevice( &buffer );
    st.setByteOrder( TQDataStream::LittleEndian ); // Great, I love TQt !

    //----- Read placeable metafile header
    st >> pheader.key;
    mIsPlaceable = ( pheader.key==( DWORD )APMHEADER_KEY );
    if ( mIsPlaceable )
    {
        st >> pheader.hmf;
        st >> pheader.bbox.left;
        st >> pheader.bbox.top;
        st >> pheader.bbox.right;
        st >> pheader.bbox.bottom;
        st >> pheader.inch;
        st >> pheader.reserved;
        st >> pheader.checksum;
        checksum = calcCheckSum( &pheader );
        if ( pheader.checksum!=checksum ) mIsPlaceable = false;

        mDpi = pheader.inch;
        mBBox.setLeft( pheader.bbox.left );
        mBBox.setTop( pheader.bbox.top );
        mBBox.setRight( pheader.bbox.right );
        mBBox.setBottom( pheader.bbox.bottom );
        mHeaderBoundingBox = mBBox;
        if ( TQWMF_DEBUG )
        {
            kdDebug() << endl << "-------------------------------------------------" << endl;
            kdDebug() << "WMF Placeable Header ( " << static_cast<int>(sizeof( pheader ) ) << "):" << endl;
            kdDebug() << "  bbox=( " << mBBox.left() << "; " << mBBox.top() << "; " << mBBox.width()
                      << "; " << mBBox.height() << ")" << endl;
            kdDebug() << "  inch=" << pheader.inch << endl;
            kdDebug() << "  checksum=" << pheader.checksum << "( "
                      << (pheader.checksum==checksum?"ok":"wrong") << " )" << endl;
        }
    }
    else buffer.at( 0 );

    //----- Read as enhanced metafile header
    filePos = buffer.at();
    st >> eheader.iType;
    st >> eheader.nSize;
    st >> eheader.rclBounds.left;
    st >> eheader.rclBounds.top;
    st >> eheader.rclBounds.right;
    st >> eheader.rclBounds.bottom;
    st >> eheader.rclFrame.left;
    st >> eheader.rclFrame.top;
    st >> eheader.rclFrame.right;
    st >> eheader.rclFrame.bottom;
    st >> eheader.dSignature;
    mIsEnhanced = ( eheader.dSignature==ENHMETA_SIGNATURE );
    if ( mIsEnhanced ) // is it really enhanced ?
    {
        st >> eheader.nVersion;
        st >> eheader.nBytes;
        st >> eheader.nRecords;
        st >> eheader.nHandles;
        st >> eheader.sReserved;
        st >> eheader.nDescription;
        st >> eheader.offDescription;
        st >> eheader.nPalEntries;
        st >> eheader.szlDevice.width;
        st >> eheader.szlDevice.height;
        st >> eheader.szlMillimeters.width;
        st >> eheader.szlMillimeters.height;

        if ( TQWMF_DEBUG )
        {
            kdDebug() << endl << "-------------------------------------------------" << endl;
            kdDebug() << "WMF Extended Header:" << endl;
            kdDebug() << "  iType=" << eheader.iType << endl;
            kdDebug() << "  nSize=" << eheader.nSize << endl;
            kdDebug() << "  rclBounds=( " << eheader.rclBounds.left << "; " << eheader.rclBounds.top << "; "
                      << eheader.rclBounds.right << "; " << eheader.rclBounds.bottom << ")" << endl;
            kdDebug() << "  rclFrame=( " << eheader.rclFrame.left << "; " << eheader.rclFrame.top << "; "
                      << eheader.rclFrame.right << "; " << eheader.rclFrame.bottom << ")" << endl;
            kdDebug() << "  nBytes=" << eheader.nBytes << endl;
            kdDebug() << "\nNOT YET IMPLEMENTED, SORRY." << endl;
        }
    }
    else // no, not enhanced
    {
        //----- Read as standard metafile header
        buffer.at( filePos );
        st >> header.mtType;
        st >> header.mtHeaderSize;
        st >> header.mtVersion;
        st >> header.mtSize;
        st >> header.mtNoObjects;
        st >> header.mtMaxRecord;
        st >> header.mtNoParameters;
        if ( TQWMF_DEBUG ) {
            kdDebug() << "WMF Header: " <<  "mtSize=" << header.mtSize << endl;
        }
    }

    //----- Test header validity
    mValid = ((header.mtHeaderSize == 9) && (header.mtNoParameters == 0)) || mIsEnhanced || mIsPlaceable;
    if ( mValid )
    {
        //----- Read Metafile Records
        last = NULL;
        rdFunc = -1;
        while ( !st.eof() && (rdFunc != 0) )
        {
            st >> rdSize;
            st >> rdFunc;
            idx = findFunc( rdFunc );
            rdSize -= 3;

            cmd = new WmfCmd;
            cmd->next = NULL;
            if ( last ) last->next = cmd;
            else mFirstCmd = cmd;

            cmd->funcIndex = idx;
            cmd->numParm = rdSize;
            cmd->parm = new WORD[ rdSize ];
            last = cmd;

            for ( i=0; i<rdSize && !st.eof(); i++ )
                st >> cmd->parm[ i ];


            if ( rdFunc == 0x020B ) {         // SETWINDOWORG: dimensions
                mBBox.setLeft( cmd->parm[ 1 ] );
                mBBox.setTop( cmd->parm[ 0 ] );
            }
            if ( rdFunc == 0x020C ) {         // SETWINDOWEXT: dimensions
                mBBox.setWidth( cmd->parm[ 1 ] );
                mBBox.setHeight( cmd->parm[ 0 ] );
            }

            if ( i<rdSize )
            {
                kdDebug() << "WMF : file truncated !" << endl;
                return false;
            }
        }
        //----- Test records validities
        mValid = (rdFunc == 0) && (mBBox.width() != 0) && (mBBox.height() != 0);
        if ( !mValid ) {
            kdDebug() << "WMF : incorrect file format !" << endl;
        }
    }
    else {
        kdDebug() << "WMF Header : incorrect header !" << endl;
    }

    buffer.close();
    return mValid;
}


//-----------------------------------------------------------------------------
bool TQWinMetaFile::paint( const TQPaintDevice* aTarget, bool absolute )
{
    int idx, i;
    WmfCmd* cmd;

    if ( !mValid )  return false;

    assert( aTarget!=NULL );
    if ( mPainter.isActive() ) return false;

    if ( mObjHandleTab ) delete[] mObjHandleTab;
    mObjHandleTab = new WinObjHandle* [ MAX_OBJHANDLE ];
    for ( i=MAX_OBJHANDLE-1; i>=0; i-- )
        mObjHandleTab[ i ] = NULL;

    mPainter.resetXForm();
    mWinding = false;
    mAbsoluteCoord = absolute;

    mPainter.begin( TQT_TQPAINTDEVICE(const_cast<TQPaintDevice*>(aTarget)) );
    if ( TQWMF_DEBUG )  {
        kdDebug() << "Bounding box : " << mBBox.left()
        << " " << mBBox.top() << " " << mBBox.right() << " " << mBBox.bottom() << endl;
    }

    if ( mAbsoluteCoord ) {
        mPainter.setWindow( mBBox.top(), mBBox.left(), mBBox.width(), mBBox.height() );
    }
    mInternalWorldMatrix.reset();

    for ( cmd=mFirstCmd; cmd; cmd=cmd->next )
    {
        idx = cmd->funcIndex;
        ( this->*metaFuncTab[ idx ].method )( cmd->numParm, cmd->parm );

        if ( TQWMF_DEBUG )  {
            TQString str = "", param;
            if ( metaFuncTab[ idx ].name == NULL ) {
                str += "UNKNOWN ";
            }
            if ( metaFuncTab[ idx ].method == &TQWinMetaFile::noop ) {
                str += "UNIMPLEMENTED ";
            }
            str += metaFuncTab[ idx ].name;
            str += " : ";

            for ( i=0 ; i < cmd->numParm ; i++ ) {
                param.setNum( cmd->parm[ i ] );
                str += param;
                str += " ";
            }
            kdDebug() << str << endl;
        }
    }
/*
    // TODO: cleanup this code when TQPicture::setBoundingBox() is possible in KOClipart (QT31)
    // because actually TQPicture::boundingBox() != mBBox()
    mWindowsCoord += 1;
    if ( mWindowsCoord == 2 )  {
        kdDebug() << "DRAW ANGLES " << endl;
        mPainter.setPen( TQt::white );
        mPainter.drawPoint( mBBox.left(), mBBox.top()  );
        mPainter.drawPoint( mBBox.right(), mBBox.bottom() );
    }
*/
    mPainter.end();
    return true;
}


//----------------s-------------------------------------------------------------
// Metafile painter methods
//-----------------------------------------------------------------------------
void TQWinMetaFile::setWindowOrg( long, short* parm )
{
    if ( mAbsoluteCoord ) {
        TQRect r = mPainter.window();
        mPainter.setWindow( parm[ 1 ], parm[ 0 ], r.width(), r.height() );
    }
    else {
        double dx = mInternalWorldMatrix.dx();
        double dy = mInternalWorldMatrix.dy();

        mInternalWorldMatrix.translate( -dx, -dy );
        mInternalWorldMatrix.translate( -parm[ 1 ], -parm[ 0 ] );
        mPainter.translate( -dx, -dy );
        mPainter.translate( -parm[ 1 ], -parm[ 0 ] );
    }
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::setWindowExt( long, short* parm )
{
    // negative value allowed for width and height : TQABS() forbidden
    if ( mAbsoluteCoord ) {
        TQRect r = mPainter.window();
        mPainter.setWindow( r.left(), r.top(), parm[ 1 ], parm[ 0 ] );
    }
    else {
        if ( (parm[ 0 ] != 0) && (parm[ 1 ] != 0) ) {
            TQRect r = mPainter.window();
            double dx = mInternalWorldMatrix.dx();
            double dy = mInternalWorldMatrix.dy();
            double sx = mInternalWorldMatrix.m11();
            double sy = mInternalWorldMatrix.m22();

            mInternalWorldMatrix.translate( -dx, -dy );
            mInternalWorldMatrix.scale( 1/sx, 1/sy );
            mPainter.translate( -dx, -dy );
            mPainter.scale( 1/sx, 1/sy );

            sx = (double)r.width() / (double)parm[ 1 ];
            sy = (double)r.height() / (double)parm[ 0 ];

            mInternalWorldMatrix.scale( sx, sy );
            mInternalWorldMatrix.translate( dx, dy );
            mPainter.scale( sx, sy );
            mPainter.translate( dx, dy );
        }
    }
}


//-----------------------------------------------------------------------------
// Drawing
//-----------------------------------------------------------------------------
void TQWinMetaFile::lineTo( long, short* parm )
{
    mPainter.lineTo( parm[ 1 ], parm[ 0 ] );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::moveTo( long, short* parm )
{
    mPainter.moveTo( parm[ 1 ], parm[ 0 ] );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::ellipse( long, short* parm )
{
    mPainter.drawEllipse( parm[ 3 ], parm[ 2 ], parm[ 1 ]-parm[ 3 ], parm[ 0 ]-parm[ 2 ] );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::polygon( long, short* parm )
{
    TQPointArray* pa;

    pa = pointArray( parm[ 0 ], &parm[ 1 ] );
    mPainter.drawPolygon( *pa, mWinding );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::polyPolygon( long, short* parm )
{
    TQRegion region;
    int  i, j, startPolygon;

    mPainter.save();

    // define clipping region
    TQRect win = bbox();
    startPolygon = 1+parm[ 0 ];
    for ( i=0 ; i < parm[ 0 ] ; i++ ) {
        TQPointArray pa1( parm[ 1+i ] );
        for ( j=0 ; j < parm[ 1+i ] ; j++) {
            pa1.setPoint ( j, parm[ startPolygon ], parm[ startPolygon+1 ] );
            startPolygon += 2;
        }
        TQRegion r( pa1 );
        region = region.eor( r );
    }
    mPainter.setClipRegion( region, TQPainter::CoordPainter );

    // fill polygons
    mPainter.fillRect( win.left(), win.top(), win.width(), win.height(), mPainter.brush() );

    // draw polygon's border if necessary
    if ( mPainter.pen().style() != TQt::NoPen ) {
        mPainter.setClipping( false );
        mPainter.setBrush( TQt::NoBrush );

        TQPointArray* pa;
        int idxPolygon = 1 + parm[ 0 ];
        for ( i=0 ; i < parm[ 0 ] ; i++ ) {
            pa = pointArray( parm[ 1+i ], &parm[ idxPolygon ] );
            mPainter.drawPolygon( *pa );
            idxPolygon += parm[ 1+i ] * 2;
        }
    }

    mPainter.restore();
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::polyline( long, short* parm )
{
    TQPointArray* pa;

    pa = pointArray( parm[ 0 ], &parm[ 1 ] );
    mPainter.drawPolyline( *pa );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::rectangle( long, short* parm )
{
    mPainter.drawRect( parm[ 3 ], parm[ 2 ], parm[ 1 ]-parm[ 3 ], parm[ 0 ]-parm[ 2 ] );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::roundRect( long, short* parm )
{
    int xRnd = 0, yRnd = 0;

    // convert (xRound, yRound) in percentage
    if ( (parm[ 3 ] - parm[ 5 ]) != 0  )
        xRnd = (parm[ 1 ] * 100) / (parm[ 3 ] - parm[ 5 ])  ;
    if ( (parm[ 2 ] - parm[ 4 ]) != 0  )
        yRnd = (parm[ 0 ] * 100) / (parm[ 2 ] - parm[ 4 ])  ;

    mPainter.drawRoundRect( parm[ 5 ], parm[ 4 ], parm[ 3 ]-parm[ 5 ], parm[ 2 ]-parm[ 4 ], xRnd, yRnd );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::arc( long, short* parm )
{
    int xCenter, yCenter, angleStart, aLength;

    xCenter = parm[ 7 ] + ((parm[ 5 ] - parm[ 7 ]) / 2);
    yCenter = parm[ 6 ] + ((parm[ 4 ] - parm[ 6 ]) / 2);

    xyToAngle ( parm[ 3 ] - xCenter, yCenter - parm[ 2 ], parm[ 1 ] - xCenter, yCenter - parm[ 0 ], angleStart, aLength );

    mPainter.drawArc( parm[ 7 ], parm[ 6 ], parm[ 5 ]-parm[ 7 ], parm[ 4 ]-parm[ 6 ], angleStart, aLength);
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::chord( long, short* parm )
{
    int xCenter, yCenter, angleStart, aLength;

    xCenter = parm[ 7 ] + ((parm[ 5 ] - parm[ 7 ]) / 2);
    yCenter = parm[ 6 ] + ((parm[ 4 ] - parm[ 6 ]) / 2);

    xyToAngle ( parm[ 3 ] - xCenter, yCenter - parm[ 2 ], parm[ 1 ] - xCenter, yCenter - parm[ 0 ], angleStart, aLength );

    mPainter.drawChord( parm[ 7 ], parm[ 6 ], parm[ 5 ]-parm[ 7 ], parm[ 4 ]-parm[ 6 ], angleStart, aLength);
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::pie( long, short* parm )
{
    int xCenter, yCenter, angleStart, aLength;

    xCenter = parm[ 7 ] + ((parm[ 5 ] - parm[ 7 ]) / 2);
    yCenter = parm[ 6 ] + ((parm[ 4 ] - parm[ 6 ]) / 2);

    xyToAngle ( parm[ 3 ] - xCenter, yCenter - parm[ 2 ], parm[ 1 ] - xCenter, yCenter - parm[ 0 ], angleStart, aLength );

    mPainter.drawPie( parm[ 7 ], parm[ 6 ], parm[ 5 ]-parm[ 7 ], parm[ 4 ]-parm[ 6 ], angleStart, aLength);
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::setPolyFillMode( long, short* parm )
{
    mWinding = parm[ 0 ];
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::setBkColor( long, short* parm )
{
    mPainter.setBackgroundColor( color( parm ) );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::setBkMode( long, short* parm )
{
    if ( parm[ 0 ]==1 ) mPainter.setBackgroundMode( Qt::TransparentMode );
    else mPainter.setBackgroundMode( Qt::OpaqueMode );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::setPixel( long, short* parm )
{
    TQPen pen = mPainter.pen();
    mPainter.setPen( color( parm ) );
    mPainter.drawPoint( parm[ 3 ], parm[ 2 ] );
    mPainter.setPen( pen );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::setRop( long, short* parm )
{
    mPainter.setRasterOp( winToTQtRaster( parm[ 0 ] ) );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::saveDC( long, short* )
{
    mPainter.save();
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::restoreDC( long, short *parm )
{
    for ( int i=0; i > parm[ 0 ] ; i-- )
        mPainter.restore();
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::intersectClipRect( long, short* parm )
{
/*  TODO: better implementation : need QT 3.0.2
    TQRegion region = mPainter.clipRegion();
    if ( region.isEmpty() )
        region = bbox();
*/
    TQRegion region( bbox() );

    TQRegion newRegion( parm[ 3 ], parm[ 2 ], parm[ 1 ] - parm[ 3 ], parm[ 0 ] - parm[ 2 ] );
    region = region.intersect( newRegion );

    mPainter.setClipRegion( region, TQPainter::CoordPainter );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::excludeClipRect( long, short* parm )
{
/*  TODO: better implementation : need QT 3.0.2
    TQRegion region = mPainter.clipRegion();
    if ( region.isEmpty() )
        region = bbox();
*/
    TQRegion region( bbox() );

    TQRegion newRegion( parm[ 3 ], parm[ 2 ], parm[ 1 ] - parm[ 3 ], parm[ 0 ] - parm[ 2 ] );
    region = region.subtract( newRegion );

    mPainter.setClipRegion( region, TQPainter::CoordPainter );
}


//-----------------------------------------------------------------------------
// Text
//-----------------------------------------------------------------------------
void TQWinMetaFile::setTextColor( long, short* parm )
{
    mTextColor = color( parm );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::setTextAlign( long, short* parm )
{
    mTextAlign = parm[ 0 ];
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::textOut( long num, short* parm )
{

    short *copyParm = new short[ num + 1 ];

    // re-order parameters
    int idxOffset = (parm[ 0 ] / 2) + 1 + (parm[ 0 ] & 1);
    copyParm[ 0 ] = parm[ idxOffset ];
    copyParm[ 1 ] = parm[ idxOffset + 1 ];
    copyParm[ 2 ] = parm[ 0 ];
    copyParm[ 3 ] = 0;
    memcpy( &copyParm[ 4 ], &parm[ 1 ], parm[ 0 ] );

    extTextOut( num + 1, copyParm );
    delete [] copyParm;
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::extTextOut( long num, short* parm )
{
    char* ptStr;
    int x, y, width, height;
    int idxOffset;

    if ( parm[ 3 ] != 0 )       // ETO_CLIPPED flag add 4 parameters
        ptStr = (char*)&parm[ 8 ];
    else
        ptStr = (char*)&parm[ 4 ];

    TQCString text( ptStr, parm[ 2 ] + 1 );

    TQFontMetrics fm( mPainter.font() );
    width = fm.width( text ) + fm.descent();  // because fm.width(text) isn't rigth with Italic text
    height = fm.height();

    mPainter.save();

    if ( mTextAlign & 0x01 ) {       // (left, top) position = current logical position
        TQPoint pos = mPainter.pos();
        x = pos.x();
        y = pos.y();
    }
    else {                           // (left, top) position = parameters
        x = parm[ 1 ];
        y = parm[ 0 ];
    }

    if ( mRotation ) {
        mPainter.translate( parm[ 1 ], parm[ 0 ]);
        mPainter.rotate ( mRotation );
        mPainter.translate( -parm[ 1 ], -parm[ 0 ] );
    }

    // alignment
    if ( mTextAlign & 0x06 )
        x -= ( width / 2 );
    if ( mTextAlign & 0x08 )
        y -= (height - fm.descent());

    mPainter.setPen( mTextColor );
    idxOffset = (parm[ 2 ] / 2) + 4 + (parm[ 2 ] & 1);
    if ( ( parm[ 2 ] > 1 ) && ( num >= (idxOffset + parm[ 2 ]) ) && ( parm[ 3 ] == 0 ) ) {
        // offset for each char
        int left = x;
        mPainter.drawText( left, y, width, height, TQt::AlignLeft | TQt::AlignTop, text.mid(0, 1) );
        for ( int i = 1; i < parm[ 2 ] ; i++ ) {
            left += parm[ idxOffset + i - 1 ];
            mPainter.drawText( left, y, width, height, TQt::AlignLeft | TQt::AlignTop, text.mid(i, 1) );
        }
    }
    else {
        mPainter.drawText( x, y, width, height, TQt::AlignLeft | TQt::AlignTop, text );
    }

    mPainter.restore();

}



//-----------------------------------------------------------------------------
// Bitmap
//-----------------------------------------------------------------------------
void TQWinMetaFile::dibBitBlt( long num, short* parm )
{
    if ( num > 9 ) {      // DIB image
        TQImage bmpSrc;

        if ( dibToBmp( bmpSrc, (char*)&parm[ 8 ], (num - 8) * 2 ) ) {
            long raster = toDWord( parm );

            mPainter.setRasterOp( winToTQtRaster( raster )  );

            // wmf file allow negative width or height
            mPainter.save();
            if ( parm[ 5 ] < 0 ) {  // width < 0 => horizontal flip
                TQWMatrix m( -1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F );
                mPainter.setWorldMatrix( m, true );
            }
            if ( parm[ 4 ] < 0 ) {  // height < 0 => vertical flip
                TQWMatrix m( 1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F );
                mPainter.setWorldMatrix( m, true );
            }
            mPainter.drawImage( parm[ 7 ], parm[ 6 ], bmpSrc, parm[ 3 ], parm[ 2 ], parm[ 5 ], parm[ 4 ] );
            mPainter.restore();
        }
    }
    else {
        kdDebug() << "TQWinMetaFile::dibBitBlt without image: not implemented " << endl;
    }
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::dibStretchBlt( long num, short* parm )
{
    TQImage bmpSrc;

    if ( dibToBmp( bmpSrc, (char*)&parm[ 10 ], (num - 10) * 2 ) ) {
        long raster = toDWord( parm );

        mPainter.setRasterOp( winToTQtRaster( raster )  );

        // wmf file allow negative width or height
        mPainter.save();
        if ( parm[ 7 ] < 0 ) {  // width < 0 => horizontal flip
            TQWMatrix m( -1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F );
            mPainter.setWorldMatrix( m, true );
        }
        if ( parm[ 6 ] < 0 ) {  // height < 0 => vertical flip
            TQWMatrix m( 1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F );
            mPainter.setWorldMatrix( m, true );
        }
        bmpSrc = bmpSrc.copy( parm[ 5 ], parm[ 4 ], parm[ 3 ], parm[ 2 ] );
        // TODO: scale the bitmap ( TQImage::scale(parm[ 7 ], parm[ 6 ]) is actually too slow )

        mPainter.drawImage( parm[ 9 ], parm[ 8 ], bmpSrc );
        mPainter.restore();
    }
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::stretchDib( long num, short* parm )
{
    TQImage bmpSrc;

    if ( dibToBmp( bmpSrc, (char*)&parm[ 11 ], (num - 11) * 2 ) ) {
        long raster = toDWord( parm );

        mPainter.setRasterOp( winToTQtRaster( raster )  );

        // wmf file allow negative width or height
        mPainter.save();
        if ( parm[ 8 ] < 0 ) {  // width < 0 => horizontal flip
            TQWMatrix m( -1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F );
            mPainter.setWorldMatrix( m, true );
        }
        if ( parm[ 7 ] < 0 ) {  // height < 0 => vertical flip
            TQWMatrix m( 1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F );
            mPainter.setWorldMatrix( m, true );
        }
        bmpSrc = bmpSrc.copy( parm[ 6 ], parm[ 5 ], parm[ 4 ], parm[ 3 ] );
        // TODO: scale the bitmap ( TQImage::scale(parm[ 8 ], parm[ 7 ]) is actually too slow )

        mPainter.drawImage( parm[ 10 ], parm[ 9 ], bmpSrc );
        mPainter.restore();
    }
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::dibCreatePatternBrush( long num, short* parm )
{
    WinObjPatternBrushHandle* handle = new WinObjPatternBrushHandle;
    addHandle( handle );
    TQImage bmpSrc;

    if ( dibToBmp( bmpSrc, (char*)&parm[ 2 ], (num - 2) * 2 ) ) {
        handle->image = bmpSrc;
        handle->brush.setPixmap( handle->image );
    }
}


//-----------------------------------------------------------------------------
// Object handle
//-----------------------------------------------------------------------------
void TQWinMetaFile::selectObject( long, short* parm )
{
    int idx = parm[ 0 ];
    if ( idx>=0 && idx < MAX_OBJHANDLE && mObjHandleTab[ idx ] )
        mObjHandleTab[ idx ]->apply( mPainter );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::deleteObject( long, short* parm )
{
    deleteHandle( parm[ 0 ] );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::createEmptyObject( long, short* )
{
    // allocation of an empty object (to keep object counting in sync)
    WinObjPenHandle* handle = new WinObjPenHandle;
    addHandle( handle );
    kdDebug() << "TQWinMetaFile: unimplemented createObject " << endl;
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::createBrushIndirect( long, short* parm )
{
    static Qt::BrushStyle hatchedStyleTab[] =
    {
        Qt::HorPattern,
        Qt::FDiagPattern,
        Qt::BDiagPattern,
        Qt::CrossPattern,
        Qt::DiagCrossPattern
    };
    static Qt::BrushStyle styleTab[] =
    { Qt::SolidPattern,
      Qt::NoBrush,
      Qt::FDiagPattern,   /* hatched */
      Qt::Dense4Pattern,  /* should be custom bitmap pattern */
      Qt::HorPattern,     /* should be BS_INDEXED (?) */
      Qt::VerPattern,     /* should be device-independent bitmap */
      Qt::Dense6Pattern,  /* should be device-independent packed-bitmap */
      Qt::Dense2Pattern,  /* should be BS_PATTERN8x8 */
      Qt::Dense3Pattern   /* should be device-independent BS_DIBPATTERN8x8 */
    };
    Qt::BrushStyle style;
    short arg;
    WinObjBrushHandle* handle = new WinObjBrushHandle;
    addHandle( handle );

    arg = parm[ 0 ];
    if ( arg==2 )
    {
        arg = parm[ 3 ];
        if ( arg>=0 && arg<5 ) style = hatchedStyleTab[ arg ];
        else
        {
            kdDebug() << "TQWinMetaFile::createBrushIndirect: invalid hatched brush " << arg << endl;
            style = Qt::SolidPattern;
        }
    }
    else if ( arg>=0 && arg<9 )
        style = styleTab[ arg ];
    else
    {
        kdDebug() << "TQWinMetaFile::createBrushIndirect: invalid brush " << arg << endl;
        style = Qt::SolidPattern;
    }
    handle->brush.setStyle( style );
    handle->brush.setColor( color( parm+1 ) );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::createPenIndirect( long, short* parm )
{
    static Qt::PenStyle styleTab[] =
    { Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine, Qt::DashDotDotLine,
      Qt::NoPen, Qt::SolidLine };
    Qt::PenStyle style;
    WinObjPenHandle* handle = new WinObjPenHandle;
    addHandle( handle );

    if ( parm[ 0 ]>=0 && parm[ 0 ]<6 ) style=styleTab[ parm[ 0 ] ];
    else
    {
        kdDebug() << "TQWinMetaFile::createPenIndirect: invalid pen " << parm[ 0 ] << endl;
        style = Qt::SolidLine;
    }

    handle->pen.setStyle( style );
    handle->pen.setColor( color( parm+3 ) );
    handle->pen.setCapStyle( Qt::RoundCap );

    //int width = 0;
    // TODO : width of pen proportional to device context width
    // DOESN'T WORK
/*
    TQRect devRec;
    devRec = mPainter.xForm( mBBox );
    width = ( parm[ 0 ] * devRec.width() ) / mBBox.width() ;
    kdDebug() << "CreatePenIndirect: " <<  endl;
    kdDebug() << "   log coord. : " << mBBox.width() << "   " << mBBox.height() << endl;
    kdDebug() << "   log. pen : " << parm[ 1 ] << "   " << parm[ 2 ] << endl;
    kdDebug() << "   dev. pen : " << width << endl;
    handle->pen.setWidth( width );
*/
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::createFontIndirect( long , short* parm)
{
    WinObjFontHandle* handle = new WinObjFontHandle;
    addHandle( handle );

    TQString family( (const char*)&parm[ 9 ] );

    mRotation = -parm[ 2 ]  / 10;               // text rotation (in 1/10 degree)
                                                // TODO: memorisation of rotation in object Font
    handle->font.setFamily( family );
    handle->font.setFixedPitch( ((parm[ 8 ] & 0x01) == 0) );
    // TODO: investigation why some test case need -2. (size of font in logical point)
    handle->font.setPointSize( TQABS(parm[ 0 ]) - 2 );
    handle->font.setWeight( (parm[ 4 ] >> 3) );
    handle->font.setItalic( (parm[ 5 ] & 0x01) );
    handle->font.setUnderline( (parm[ 5 ] & 0x100) );
}


//-----------------------------------------------------------------------------
// Misc
//-----------------------------------------------------------------------------
void TQWinMetaFile::noop( long, short* )
{
}


void TQWinMetaFile::end( long, short* )
{
    // end of file :
//    kdDebug() << "END bbox=( " << mBBox.left() << "; " << mBBox.top() << "; " << mBBox.width() << "; " << mBBox.height() << ")" << endl;
}


//-----------------------------------------------------------------------------
unsigned short TQWinMetaFile::calcCheckSum( WmfPlaceableHeader* apmfh )
{
    WORD*  lpWord;
    WORD   wResult, i;

    // Start with the first word
    wResult = *( lpWord = ( WORD* )( apmfh ) );
    // XOR in each of the other 9 words
    for( i=1; i<=9; i++ )
    {
        wResult ^= lpWord[ i ];
    }
    return wResult;
}


//-----------------------------------------------------------------------------
int TQWinMetaFile::findFunc( unsigned short aFunc ) const
{
    int i;

    for ( i=0; metaFuncTab[ i ].name; i++ )
        if ( metaFuncTab[ i ].func == aFunc ) return i;

    // here : unknown function
    return i;
}

//-----------------------------------------------------------------------------
TQPointArray* TQWinMetaFile::pointArray( short num, short* parm )
{
    int i;

    mPoints.resize( num );

    for ( i=0; i<num; i++, parm+=2 )
        mPoints.setPoint( i, parm[ 0 ], parm[ 1 ] );

    return &mPoints;
}

//-----------------------------------------------------------------------------
unsigned int TQWinMetaFile::toDWord( short* parm )
{
    unsigned int l;

#if !defined( WORDS_BIGENDIAN )
    l = *( unsigned int* )( parm );
#else
    char *bytes;
    char swap[ 4 ];
    bytes = ( char* )parm;
    swap[ 0 ] = bytes[ 2 ];
    swap[ 1 ] = bytes[ 3 ];
    swap[ 2 ] = bytes[ 0 ];
    swap[ 3 ] = bytes[ 1 ];
    l = *( unsigned int* )( swap );
#endif

    return l;
}


//-----------------------------------------------------------------------------
TQColor TQWinMetaFile::color( short* parm )
{
    unsigned int colorRef;
    int red, green, blue;

    colorRef = toDWord( parm ) & 0xffffff;
    red      = colorRef & 255;
    green    = ( colorRef>>8 ) & 255;
    blue     = ( colorRef>>16 ) & 255;

    return TQColor( red, green, blue );
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::xyToAngle( int xStart, int yStart, int xEnd, int yEnd, int& angleStart, int& angleLength )
{
    float aStart, aLength;

    aStart = atan2( double(yStart),  double(xStart) );
    aLength = atan2( double(yEnd), double(xEnd) ) - aStart;

    angleStart = (int)(aStart * 2880 / 3.14166);
    angleLength = (int)(aLength * 2880 / 3.14166);
    if ( angleLength < 0 ) angleLength = 5760 + angleLength;
}


//-----------------------------------------------------------------------------
void TQWinMetaFile::addHandle( WinObjHandle* handle )
{
    int idx;

    for ( idx=0; idx < MAX_OBJHANDLE ; idx++ )
        if ( mObjHandleTab[ idx ] == NULL )  break;

    if ( idx < MAX_OBJHANDLE )
        mObjHandleTab[ idx ] = handle;
    else
        kdDebug() << "TQWinMetaFile error: handle table full !" << endl;
}

//-----------------------------------------------------------------------------
void TQWinMetaFile::deleteHandle( int idx )
{
    if ( idx >= 0 && idx < MAX_OBJHANDLE && mObjHandleTab[ idx ] )
    {
        delete mObjHandleTab[ idx ];
        mObjHandleTab[ idx ] = NULL;
    }
}

//-----------------------------------------------------------------------------
TQt::RasterOp  TQWinMetaFile::winToTQtRaster( short parm ) const
{
    static const TQt::RasterOp opTab[] =
    {
        TQt::CopyROP,
        TQt::ClearROP, TQt::NandROP, TQt::NotAndROP, TQt::NotCopyROP,
        TQt::AndNotROP, TQt::NotROP, TQt::XorROP, TQt::NorROP,
        TQt::AndROP, TQt::NotXorROP, TQt::NopROP, TQt::NotOrROP,
        TQt::CopyROP, TQt::OrNotROP, TQt::OrROP, TQt::SetROP
    };

    if ( parm > 0 && parm <= 16 )
        return opTab[ parm ];
    else
        return TQt::CopyROP;
}

//-----------------------------------------------------------------------------
TQt::RasterOp  TQWinMetaFile::winToTQtRaster( long parm ) const
{
    /* TODO: Ternary raster operations
    0x00C000CA  dest = (source AND pattern)
    0x00F00021  dest = pattern
    0x00FB0A09  dest = DPSnoo
    0x005A0049  dest = pattern XOR dest   */
    static const struct OpTab
    {
        long winRasterOp;
        TQt::RasterOp qtRasterOp;
    } opTab[] =
    {
        { 0x00CC0020, TQt::CopyROP },
        { 0x00EE0086, TQt::OrROP },
        { 0x008800C6, TQt::AndROP },
        { 0x00660046, TQt::XorROP },
        { 0x00440328, TQt::AndNotROP },
        { 0x00330008, TQt::NotCopyROP },
        { 0x001100A6, TQt::NandROP },
        { 0x00C000CA, TQt::CopyROP },
        { 0x00BB0226, TQt::NotOrROP },
        { 0x00F00021, TQt::CopyROP },
        { 0x00FB0A09, TQt::CopyROP },
        { 0x005A0049, TQt::CopyROP },
        { 0x00550009, TQt::NotROP },
        { 0x00000042, TQt::ClearROP },
        { 0x00FF0062, TQt::SetROP }
    };

    int i;
    for ( i=0 ; i < 15 ; i++ )
        if ( opTab[ i ].winRasterOp == parm )
            break;

    if ( i < 15 )
        return opTab[ i ].qtRasterOp;
    else
        return TQt::CopyROP;
}

//-----------------------------------------------------------------------------
bool TQWinMetaFile::dibToBmp( TQImage& bmp, const char* dib, long size )
{
    typedef struct _BMPFILEHEADER {
        WORD bmType;
        DWORD bmSize;
        WORD bmReserved1;
        WORD bmReserved2;
        DWORD bmOffBits;
    }  BMPFILEHEADER;

    int sizeBmp = size + 14;

    TQByteArray pattern( sizeBmp );       // BMP header and DIB data
    pattern.fill(0);
    memcpy( &pattern[ 14 ], dib, size );

    // add BMP header
    BMPFILEHEADER* bmpHeader;
    bmpHeader = (BMPFILEHEADER*)((const char*)pattern);
    bmpHeader->bmType = 0x4D42;
    bmpHeader->bmSize = sizeBmp;

    if ( !bmp.loadFromData( (const uchar*)bmpHeader, pattern.size(), "BMP" ) ) {
        kdDebug() << "TQWinMetaFile::dibToBmp: invalid bitmap " << endl;
        return false;
    }
    else {
//        if ( bmp.save("/home/software/kde-cvs/qt/examples/wmf/test.bmp", "BMP") )
//        if ( bmp.load( "/home/software/kde-cvs/qt/examples/wmf/test.bmp", "BMP" ) )
//            fprintf(stderr, "Bitmap ok \n");
        return true;
    }
}