summaryrefslogtreecommitdiffstats
path: root/src/kernel/qmovie.cpp
blob: 3379fba5e3f2b267fef6b8ffe7c7fce4af25da8e (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
/****************************************************************************
**
** Implementation of movie classes
**
** Created : 970617
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of the kernel module of the Qt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free Qt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.QPL
** included in the packaging of this file.  Licensees holding valid Qt
** Commercial licenses may use this file in accordance with the Qt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

// #define QT_SAVE_MOVIE_HACK

#include "qtimer.h"
#include "qpainter.h"
#include "qptrlist.h"
#include "qbitmap.h"
#include "qmovie.h"
#include "qfile.h"
#include "qbuffer.h"
#include "qobject.h"
#include "qpixmapcache.h"

#ifndef QT_NO_MOVIE

#ifdef Q_WS_QWS
#include "qgfx_qws.h"
#endif

#include "qasyncio.h"
#include "qasyncimageio.h"

#include <stdlib.h>

/*!
    \class QMovie qmovie.h
    \brief The QMovie class provides incremental loading of animations or images, signalling as it progresses.

    \ingroup images
    \ingroup graphics
    \ingroup multimedia
    \mainclass

    The simplest way to display a QMovie is to use a QLabel and
    QLabel::setMovie().

    A QMovie provides a QPixmap as the framePixmap(); connections can
    be made via connectResize() and connectUpdate() to receive
    notification of size and pixmap changes. All decoding is driven
    by the normal event-processing mechanisms.

    The movie begins playing as soon as the QMovie is created
    (actually, once control returns to the event loop). When the last
    frame in the movie has been played, it may loop back to the start
    if such looping is defined in the input source.

    QMovie objects are explicitly shared. This means that a QMovie
    copied from another QMovie will be displaying the same frame at
    all times. If one shared movie pauses, all pause. To make \e
    independent movies, they must be constructed separately.

    The set of data formats supported by QMovie is determined by the
    decoder factories that have been installed; the format of the
    input is determined as the input is decoded.

    The supported formats are MNG (if Qt is configured with MNG
    support enabled) and GIF (if Qt is configured with GIF support
    enabled, see qgif.h).

    If Qt is configured to support GIF reading, we are required to
    state that "The Graphics Interchange Format(c) is the Copyright
    property of CompuServe Incorporated. GIF(sm) is a Service Mark
    property of CompuServe Incorporated.

    \warning If you are in a country that recognizes software patents
    and in which Unisys holds a patent on LZW compression and/or
    decompression and you want to use GIF, Unisys may require you to
    license that technology. Such countries include Canada, Japan,
    the USA, France, Germany, Italy and the UK.

    GIF support may be removed completely in a future version of Qt.
    We recommend using the MNG or PNG format.

    \img qmovie.png QMovie

    \sa QLabel::setMovie()
*/

/*!
    \enum QMovie::Status

    \value SourceEmpty
    \value UnrecognizedFormat
    \value Paused
    \value EndOfFrame
    \value EndOfLoop
    \value EndOfMovie
    \value SpeedChanged
*/

class QMoviePrivate : public QObject, public QShared,
		      private QDataSink, private QImageConsumer
{
    Q_OBJECT

public: // for QMovie

    // Creates a null Private
    QMoviePrivate();

    // NOTE:  The ownership of the QDataSource is transferred to the Private
    QMoviePrivate(QDataSource* src, QMovie* movie, int bufsize);

    virtual ~QMoviePrivate();

    bool isNull() const;

    // Initialize, possibly to the null state
    void init(bool fully);
    void flushBuffer();
    void updatePixmapFromImage();
    void updatePixmapFromImage(const QPoint& off, const QRect& area);
    void showChanges();

    // This as QImageConsumer
    void changed(const QRect& rect);
    void end();
    void preFrameDone(); //util func
    void frameDone();
    void frameDone(const QPoint&, const QRect& rect);
    void restartTimer();
    void setLooping(int l);
    void setFramePeriod(int milliseconds);
    void setSize(int w, int h);

    // This as QDataSink
    int readyToReceive();
    void receive(const uchar* b, int bytecount);
    void eof();
    void pause();

signals:
    void sizeChanged(const QSize&);
    void areaChanged(const QRect&);
    void dataStatus(int);

public slots:
    void refresh();

public:
    QMovie *that;
    QWidget * display_widget;

    QImageDecoder *decoder;

    // Cyclic buffer
    int buf_size;
    uchar *buffer;
    int buf_r, buf_w, buf_usage;

    int framenumber;
    int frameperiod;
    int speed;
    QTimer *frametimer;
    int lasttimerinterval;
    int loop;
    bool movie_ended;
    bool dirty_cache;
    bool waitingForFrameTick;
    int stepping;
    QRect changed_area;
    QRect valid_area;
    QDataPump *pump;
    QDataSource *source;
    QPixmap mypixmap;
    QBitmap mymask;
    QColor bg;

    int error;
    bool empty;

#ifdef QT_SAVE_MOVIE_HACK
  bool save_image;
  int image_number;
#endif
};


QMoviePrivate::QMoviePrivate()
{
    dirty_cache = FALSE;
    buffer = 0;
    pump = 0;
    source = 0;
    decoder = 0;
    display_widget=0;
    buf_size = 0;
    init(FALSE);
}

// NOTE:  The ownership of the QDataSource is transferred to the Private
QMoviePrivate::QMoviePrivate(QDataSource* src, QMovie* movie, int bufsize) :
    that(movie),
    buf_size(bufsize)
{
    frametimer = new QTimer(this);
    pump = src ? new QDataPump(src, this) : 0;
    QObject::connect(frametimer, SIGNAL(timeout()), this, SLOT(refresh()));
    dirty_cache = FALSE;
    source = src;
    buffer = 0;
    decoder = 0;
    speed = 100;
    display_widget=0;
    init(TRUE);
}

QMoviePrivate::~QMoviePrivate()
{
    if ( buffer )				// Avoid purify complaint
	delete [] buffer;
    delete pump;
    delete decoder;
    delete source;

    // Too bad.. but better be safe than sorry
    if ( dirty_cache )
        QPixmapCache::clear();
}

bool QMoviePrivate::isNull() const
{
    return !buf_size;
}

// Initialize. Only actually allocate any space if \a fully is TRUE,
// otherwise, just enough to be a valid null Private.
void QMoviePrivate::init(bool fully)
{
#ifdef QT_SAVE_MOVIE_HACK
    save_image = TRUE;
    image_number = 0;
#endif

    buf_usage = buf_r = buf_w = 0;
    if ( buffer )				// Avoid purify complaint
	delete [] buffer;
    buffer = fully ? new uchar[buf_size] : 0;
    if ( buffer )
	memset( buffer, 0, buf_size );

    delete decoder;
    decoder = fully ? new QImageDecoder(this) : 0;

#ifdef AVOID_OPEN_FDS
    if ( source && !source->isOpen() )
	source->open(IO_ReadOnly);
#endif

    waitingForFrameTick = FALSE;
    stepping = -1;
    framenumber = 0;
    frameperiod = -1;
    if (fully) frametimer->stop();
    lasttimerinterval = -1;
    changed_area.setRect(0,0,-1,-1);
    valid_area = changed_area;
    loop = -1;
    movie_ended = FALSE;
    error = 0;
    empty = TRUE;
}

void QMoviePrivate::flushBuffer()
{
    int used;
    while (buf_usage && !waitingForFrameTick && stepping != 0 && !error) {
	used = decoder->decode(buffer + buf_r, QMIN(buf_usage, buf_size - buf_r));
	if (used <= 0) {
	    if ( used < 0 ) {
		error = 1;
		emit dataStatus(QMovie::UnrecognizedFormat);
	    }
	    break;
	}
	buf_r = (buf_r + used) % buf_size;
	buf_usage -= used;
    }

    // Some formats, like MNG, can make stuff happen without any extra data.
    // Only do this if the movie hasn't ended, however or we'll never get the end of loop signal.
    if (!movie_ended) {
	used = decoder->decode(buffer + buf_r, 0);
	if (used <= 0) {
	    if ( used < 0 ) {
		error = 1;
		emit dataStatus(QMovie::UnrecognizedFormat);
	    }
	}
    }

    if (error)
	frametimer->stop();
    maybeReady();
}

void QMoviePrivate::updatePixmapFromImage()
{
    if (changed_area.isEmpty()) return;
    updatePixmapFromImage(QPoint(0,0),changed_area);
}

void QMoviePrivate::updatePixmapFromImage(const QPoint& off,
						const QRect& area)
{
    // Create temporary QImage to hold the part we want
    const QImage& gimg = decoder->image();
    QImage img = gimg.copy(area);

#ifdef QT_SAVE_MOVIE_HACK
    if ( save_image ) {
	QString name;
	name.sprintf("movie%i.ppm",image_number++);
	gimg.save( name, "PPM" );
    }
#endif

    // Resize to size of image
    if (mypixmap.width() != gimg.width() || mypixmap.height() != gimg.height())
	mypixmap.resize(gimg.width(), gimg.height());

    // Convert to pixmap and paste that onto myself
    QPixmap lines;

#ifndef QT_NO_SPRINTF
    if (!(frameperiod < 0 && loop == -1)) {
        // its an animation, lets see if we converted
        // this frame already.
        QString key;
        key.sprintf( "%08lx:%04d", ( long )this, framenumber );
        if ( !QPixmapCache::find( key, lines ) ) {
            lines.convertFromImage(img, Qt::ColorOnly);
            QPixmapCache::insert( key, lines );
            dirty_cache = TRUE;
        }
    } else
#endif
    {
        lines.convertFromImage(img, Qt::ColorOnly);
    }

    if (bg.isValid()) {
	QPainter p;
	p.begin(&mypixmap);
	p.fillRect(area, bg);
	p.drawPixmap(area, lines);
	p.end();
    } else {
	if (gimg.hasAlphaBuffer()) {
	    // Resize to size of image
	    if (mymask.isNull()) {
		mymask.resize(gimg.width(), gimg.height());
		mymask.fill( Qt::color1 );
	    }
	}
	mypixmap.setMask(QBitmap()); // Remove reference to my mask
	copyBlt( &mypixmap, area.left(), area.top(),
		 &lines, off.x(), off.y(), area.width(), area.height() );
    }

#ifdef Q_WS_QWS
    if(display_widget) {
	QGfx * mygfx=display_widget->graphicsContext();
	if(mygfx) {
	    double xscale,yscale;
	    xscale=display_widget->width();
	    yscale=display_widget->height();
	    xscale=xscale/((double)mypixmap.width());
	    yscale=yscale/((double)mypixmap.height());
	    double xh,yh;
	    xh=xscale*((double)area.left());
	    yh=yscale*((double)area.top());
	    mygfx->setSource(&mypixmap);
	    mygfx->setAlphaType(QGfx::IgnoreAlpha);
	    mygfx->stretchBlt(0,0,display_widget->width(),
			      display_widget->height(),mypixmap.width(),
			      mypixmap.height());
	    delete mygfx;
	}
    }
#endif
}

void QMoviePrivate::showChanges()
{
    if (changed_area.isValid()) {
	updatePixmapFromImage();

	valid_area = valid_area.unite(changed_area);
	emit areaChanged(changed_area);

	changed_area.setWidth(-1); // make empty
    }
}

// Private as QImageConsumer
void QMoviePrivate::changed(const QRect& rect)
{
    if (!frametimer->isActive())
	frametimer->start(0);
    changed_area = changed_area.unite(rect);
}

void QMoviePrivate::end()
{
    movie_ended = TRUE;
}

void QMoviePrivate::preFrameDone()
{
    if (stepping > 0) {
	stepping--;
	if (!stepping) {
	    frametimer->stop();
	    emit dataStatus( QMovie::Paused );
	}
    } else {
	waitingForFrameTick = TRUE;
	restartTimer();
    }
}
void QMoviePrivate::frameDone()
{
    preFrameDone();
    showChanges();
    emit dataStatus(QMovie::EndOfFrame);
    framenumber++;
}
void QMoviePrivate::frameDone(const QPoint& p,
				const QRect& rect)
{
    preFrameDone();
    const QImage& gimg = decoder->image();
    QPoint point = p - gimg.offset();
    if (framenumber==0)
	emit sizeChanged(gimg.size());
    valid_area = valid_area.unite(QRect(point,rect.size()));
    updatePixmapFromImage(point,rect);
    emit areaChanged(QRect(point,rect.size()));
    emit dataStatus(QMovie::EndOfFrame);
    framenumber++;
}

void QMoviePrivate::restartTimer()
{
    if (speed > 0) {
	int i = frameperiod >= 0 ? frameperiod * 100/speed : 0;
	if ( i != lasttimerinterval || !frametimer->isActive() ) {
	    lasttimerinterval = i;
	    frametimer->start( i );
	}
    } else {
	frametimer->stop();
    }
}

void QMoviePrivate::setLooping(int nloops)
{
    if (loop == -1) { // Only if we don't already know how many loops!
	if (source && source->rewindable()) {
	    source->enableRewind(TRUE);
	    loop = nloops;
	} else {
	    // Cannot loop from this source
	    loop = -2;
	}
    }
}

void QMoviePrivate::setFramePeriod(int milliseconds)
{
    // Animation:  only show complete frame
    frameperiod = milliseconds;
    if (stepping<0 && frameperiod >= 0) restartTimer();
}

void QMoviePrivate::setSize(int w, int h)
{
    if (mypixmap.width() != w || mypixmap.height() != h) {
	mypixmap.resize(w, h);
	emit sizeChanged(QSize(w, h));
    }
}


// Private as QDataSink

int QMoviePrivate::readyToReceive()
{
    // Could pre-fill buffer, but more efficient to just leave the
    // data back at the source.
    return (waitingForFrameTick || !stepping || buf_usage || error)
	? 0 : buf_size;
}

void QMoviePrivate::receive(const uchar* b, int bytecount)
{
    if ( bytecount ) empty = FALSE;

    while (bytecount && !waitingForFrameTick && stepping != 0) {
	int used = decoder->decode(b, bytecount);
	if (used<=0) {
	    if ( used < 0 ) {
		error = 1;
		emit dataStatus(QMovie::UnrecognizedFormat);
	    }
	    break;
	}
	b+=used;
	bytecount-=used;
    }

    // Append unused to buffer
    while (bytecount--) {
	buffer[buf_w] = *b++;
	buf_w = (buf_w+1)%buf_size;
	buf_usage++;
    }
}

void QMoviePrivate::eof()
{
    if ( !movie_ended )
	return;

    if ( empty )
	emit dataStatus(QMovie::SourceEmpty);

#ifdef QT_SAVE_MOVIE_HACK
    save_image = FALSE;
#endif

    emit dataStatus(QMovie::EndOfLoop);

    if (loop >= 0) {
	if (loop) {
	    loop--;
	    if (!loop) return;
	}
	delete decoder;
	decoder = new QImageDecoder(this);
	source->rewind();
	framenumber = 0;
	movie_ended = FALSE;
    } else {
	delete decoder;
	decoder = 0;
	if ( buffer )				// Avoid purify complaint
	    delete [] buffer;
	buffer = 0;
	emit dataStatus(QMovie::EndOfMovie);
#ifdef AVOID_OPEN_FDS
	if ( source )
	    source->close();
#endif
    }
}

void QMoviePrivate::pause()
{
    if ( stepping ) {
	stepping = 0;
	frametimer->stop();
	emit dataStatus( QMovie::Paused );
    }
}

void QMoviePrivate::refresh()
{
    if (!decoder) {
	frametimer->stop();
	return;
    }

    if (frameperiod < 0 && loop == -1) {
	// Only show changes if probably not an animation
	showChanges();
    }

    if (!buf_usage) {
	frametimer->stop();
    }

    waitingForFrameTick = FALSE;
    flushBuffer();
}

///////////////// End of Private /////////////////





/*!
    Constructs a null QMovie. The only interesting thing to do with
    such a movie is to assign another movie to it.

    \sa isNull()
*/
QMovie::QMovie()
{
    d = new QMoviePrivate();
}

/*!
    Constructs a QMovie with an external data source. You should later
    call pushData() to send incoming animation data to the movie.

    The \a bufsize argument sets the maximum amount of data the movie
    will transfer from the data source per event loop. The lower this
    value, the better interleaved the movie playback will be with
    other event processing, but the slower the overall processing will
    be.

    \sa pushData()
*/
QMovie::QMovie(int bufsize)
{
    d = new QMoviePrivate(0, this, bufsize);
}

/*!
    Returns the maximum amount of data that can currently be pushed
    into the movie by a call to pushData(). This is affected by the
    initial buffer size, but varies as the movie plays and data is
    consumed.
*/
int QMovie::pushSpace() const
{
    return d->readyToReceive();
}

/*!
    Pushes \a length bytes from \a data into the movie. \a length must
    be no more than the amount returned by pushSpace() since the
    previous call to pushData().
*/
void QMovie::pushData(const uchar* data, int length)
{
    d->receive(data,length);
}

#ifdef Q_WS_QWS // ##### Temporary performance experiment
/*!
    \internal
*/
void QMovie::setDisplayWidget(QWidget * w)
{
    d->display_widget=w;
}
#endif

/*!
    Constructs a QMovie that reads an image sequence from the given
    data source, \a src. The source must be allocated dynamically,
    because QMovie will take ownership of it and will destroy it when
    the movie is destroyed. The movie starts playing as soon as event
    processing continues.

    The \a bufsize argument sets the maximum amount of data the movie
    will transfer from the data source per event loop. The lower this
    value, the better interleaved the movie playback will be with
    other event processing, but the slower the overall processing will
    be.
*/
QMovie::QMovie(QDataSource* src, int bufsize)
{
    d = new QMoviePrivate(src, this, bufsize);
}

/*!
    Constructs a QMovie that reads an image sequence from the file, \a
    fileName.

    The \a bufsize argument sets the maximum amount of data the movie
    will transfer from the data source per event loop. The lower this
    value, the better interleaved the movie playback will be with
    other event processing, but the slower the overall processing will
    be.
*/
QMovie::QMovie(const QString &fileName, int bufsize)
{
    QFile* file = new QFile(fileName);
    if ( !fileName.isEmpty() )
 	file->open(IO_ReadOnly);
    d = new QMoviePrivate(new QIODeviceSource(file, bufsize), this, bufsize);
}

/*!
    Constructs a QMovie that reads an image sequence from the byte
    array, \a data.

    The \a bufsize argument sets the maximum amount of data the movie
    will transfer from the data source per event loop. The lower this
    value, the better interleaved the movie playback will be with
    other event processing, but the slower the overall processing will
    be.
*/
QMovie::QMovie(QByteArray data, int bufsize)
{
    QBuffer* buffer = new QBuffer(data);
    buffer->open(IO_ReadOnly);
    d = new QMoviePrivate(new QIODeviceSource(buffer, bufsize), this, bufsize);
}

/*!
    Constructs a movie that uses the same data as movie \a movie.
    QMovies use explicit sharing, so operations on the copy will
    affect both.
*/
QMovie::QMovie(const QMovie& movie)
{
    d = movie.d;
    d->ref();
}

/*!
    Destroys the QMovie. If this is the last reference to the data of
    the movie, the data is deallocated.
*/
QMovie::~QMovie()
{
    if (d->deref()) delete d;
}

/*!
    Returns TRUE if the movie is null; otherwise returns FALSE.
*/
bool QMovie::isNull() const
{
    return d->isNull();
}

/*!
    Makes this movie use the same data as movie \a movie. QMovies use
    explicit sharing.
*/
QMovie& QMovie::operator=(const QMovie& movie)
{
    movie.d->ref();
    if (d->deref()) delete d;
    d = movie.d;
    return *this;
}


/*!
    Sets the background color of the pixmap to \a c. If the background
    color isValid(), the pixmap will never have a mask because the
    background color will be used in transparent regions of the image.

    \sa backgroundColor()
*/
void QMovie::setBackgroundColor(const QColor& c)
{
    d->bg = c;
}

/*!
    Returns the background color of the movie set by
    setBackgroundColor().
*/
const QColor& QMovie::backgroundColor() const
{
    return d->bg;
}

/*!
    Returns the area of the pixmap for which pixels have been
    generated.
*/
const QRect& QMovie::getValidRect() const
{
    return d->valid_area;
}

/*!
    Returns the current frame of the movie, as a QPixmap. It is not
    generally useful to keep a copy of this pixmap. It is better to
    keep a copy of the QMovie and get the framePixmap() only when
    needed for drawing.

    \sa frameImage()
*/
const QPixmap& QMovie::framePixmap() const
{
    return d->mypixmap;
}

/*!
    Returns the current frame of the movie, as a QImage. It is not
    generally useful to keep a copy of this image. Also note that you
    must not call this function if the movie is finished(), since by
    then the image will not be available.

    \sa framePixmap()
*/
const QImage& QMovie::frameImage() const
{
    return d->decoder->image();
}

/*!
    Returns the number of steps remaining after a call to step(). If
    the movie is paused, steps() returns 0. If it's running normally
    or is finished, steps() returns a negative number.
*/
int QMovie::steps() const
{
    return d->stepping;
}

/*!
    Returns the number of times EndOfFrame has been emitted since the
    start of the current loop of the movie. Thus, before any
    EndOfFrame has been emitted the value will be 0; within slots
    processing the first signal, frameNumber() will be 1, and so on.
*/
int QMovie::frameNumber() const { return d->framenumber; }

/*!
    Returns TRUE if the image is paused; otherwise returns FALSE.
*/
bool QMovie::paused() const
{
    return d->stepping == 0;
}

/*!
    Returns TRUE if the image is no longer playing: this happens when
    all loops of all frames are complete; otherwise returns FALSE.
*/
bool QMovie::finished() const
{
    return !d->decoder;
}

/*!
    Returns TRUE if the image is not single-stepping, not paused, and
    not finished; otherwise returns FALSE.
*/
bool QMovie::running() const
{
    return d->stepping<0 && d->decoder;
}

/*!
    Pauses the progress of the animation.

    \sa unpause()
*/
void QMovie::pause()
{
    d->pause();
}

/*!
    Unpauses the progress of the animation.

    \sa pause()
*/
void QMovie::unpause()
{
    if ( d->stepping >= 0 )	{
	if (d->isNull())
	    return;
	d->stepping = -1;
	d->restartTimer();
    }
}

/*!
    \overload

    Steps forward, showing \a steps frames, and then pauses.
*/
void QMovie::step(int steps)
{
    if (d->isNull())
	return;
    d->stepping = steps;
    d->frametimer->start(0);
    d->waitingForFrameTick = FALSE; // Full speed ahead!
}

/*!
    Steps forward 1 frame and then pauses.
*/
void QMovie::step()
{
    step(1);
}

/*!
    Rewinds the movie to the beginning. If the movie has not been
    paused, it begins playing again.
*/
void QMovie::restart()
{
    if (d->isNull())
	return;
    if (d->source->rewindable()) {
	d->source->enableRewind(TRUE);
	d->source->rewind();
	int s = d->stepping;
	d->init(TRUE);
	if ( s>0 )
	    step(s);
	else if ( s==0 )
	    pause();
    }
}

/*!
    Returns the movie's play speed as a percentage. The default is 100
    percent.

    \sa setSpeed()
*/
int QMovie::speed() const
{
    return d->speed;
}

/*!
    Sets the movie's play speed as a percentage, to \a percent. This
    is a percentage of the speed dictated by the input data format.
    The default is 100 percent.
*/
void QMovie::setSpeed(int percent)
{
    int oldspeed = d->speed;
    if ( oldspeed != percent && percent >= 0 ) {
	d->speed = percent;
	// Restart timer only if really needed
	if (d->stepping < 0) {
	    if ( !percent || !oldspeed    // To or from zero
		 || oldspeed*4 / percent > 4   // More than 20% slower
		 || percent*4 / oldspeed > 4   // More than 20% faster
		 )
		d->restartTimer();
	}
    }
}

/*!
    Connects the \a{receiver}'s \a member of type \c{void member(const
    QSize&)} so that it is signalled when the movie changes size.

    Note that due to the explicit sharing of QMovie objects, these
    connections persist until they are explicitly disconnected with
    disconnectResize() or until \e every shared copy of the movie is
    deleted.
*/
void QMovie::connectResize(QObject* receiver, const char *member)
{
    QObject::connect(d, SIGNAL(sizeChanged(const QSize&)), receiver, member);
}

/*!
    Disconnects the \a{receiver}'s \a member (or all members if \a
    member is zero) that were previously connected by connectResize().
*/
void QMovie::disconnectResize(QObject* receiver, const char *member)
{
    QObject::disconnect(d, SIGNAL(sizeChanged(const QSize&)), receiver, member);
}

/*!
    Connects the \a{receiver}'s \a member of type \c{void member(const
    QRect&)} so that it is signalled when an area of the framePixmap()
    has changed since the previous frame.

    Note that due to the explicit sharing of QMovie objects, these
    connections persist until they are explicitly disconnected with
    disconnectUpdate() or until \e every shared copy of the movie is
    deleted.
*/
void QMovie::connectUpdate(QObject* receiver, const char *member)
{
    QObject::connect(d, SIGNAL(areaChanged(const QRect&)), receiver, member);
}

/*!
    Disconnects the \a{receiver}'s \a member (or all members if \q
    member is zero) that were previously connected by connectUpdate().
*/
void QMovie::disconnectUpdate(QObject* receiver, const char *member)
{
    QObject::disconnect(d, SIGNAL(areaChanged(const QRect&)), receiver, member);
}

/*!
    Connects the \a{receiver}'s \a member, of type \c{void
    member(int)} so that it is signalled when the movie changes
    status. The status codes are negative for errors and positive for
    information.

    \table
    \header \i Status Code \i Meaning
    \row \i QMovie::SourceEmpty
	 \i signalled if the input cannot be read.
    \row \i QMovie::UnrecognizedFormat
	 \i signalled if the input data is unrecognized.
    \row \i QMovie::Paused
	  \i signalled when the movie is paused by a call to paused()
	  or by after \link step() stepping \endlink pauses.
    \row \i QMovie::EndOfFrame
	 \i signalled at end-of-frame after any update and Paused signals.
    \row \i QMovie::EndOfLoop
	 \i signalled at end-of-loop, after any update signals,
	 EndOfFrame - but before EndOfMovie.
    \row \i QMovie::EndOfMovie
	 \i signalled when the movie completes and is not about to loop.
    \endtable

    More status messages may be added in the future, so a general test
    for errors would test for negative.

    Note that due to the explicit sharing of QMovie objects, these
    connections persist until they are explicitly disconnected with
    disconnectStatus() or until \e every shared copy of the movie is
    deleted.
*/
void QMovie::connectStatus(QObject* receiver, const char *member)
{
    QObject::connect(d, SIGNAL(dataStatus(int)), receiver, member);
}

/*!
    Disconnects the \a{receiver}'s \a member (or all members if \a
    member is zero) that were previously connected by connectStatus().
*/
void QMovie::disconnectStatus(QObject* receiver, const char *member)
{
    QObject::disconnect(d, SIGNAL(dataStatus(int)), receiver, member);
}


#include "qmovie.moc"

#endif	// QT_NO_MOVIE