@@ -26,6 +26,8 @@ public class FrameData {
26
26
volatile Texture2D _texture ;
27
27
volatile FrameData _frameData ;
28
28
volatile Image _image ;
29
+ volatile GifDecoder _decoder ;
30
+ volatile MemoryStream _stream ;
29
31
IEnumerator _coroutine ;
30
32
31
33
public GifCodec ( byte [ ] bytes ) {
@@ -38,59 +40,51 @@ public GifCodec(byte[] bytes) {
38
40
this . _frameIndex = 0 ;
39
41
this . _bytes = bytes ;
40
42
this . _coroutine = this . _startDecoding ( ) ;
41
- this . _init ( ) ;
42
- this . _texture = new Texture2D ( this . _width , this . _height , TextureFormat . BGRA32 , false ) ;
43
- this . _texture . hideFlags = HideFlags . HideAndDontSave ;
44
- this . _image = new Image ( this . _texture ) ;
43
+ this . _decoder = new GifDecoder ( ) ;
44
+ this . _stream = new MemoryStream ( this . _bytes ) ;
45
+ this . _frameData = new FrameData ( ) {
46
+ frameInfo = new FrameInfo ( )
47
+ } ;
45
48
}
46
49
47
- void _init ( ) {
48
- var bytesStream = new MemoryStream ( this . _bytes ) ;
50
+ IEnumerator _startDecoding ( ) {
51
+ this . _stream . Seek ( 0 , SeekOrigin . Begin ) ;
49
52
50
- var gifDecoder = new GifDecoder ( ) ;
51
- if ( gifDecoder . read ( bytesStream ) != GifDecoder . STATUS_OK ) {
53
+ if ( this . _decoder . read ( this . _stream ) != GifDecoder . STATUS_OK ) {
52
54
throw new Exception ( "Failed to decode gif." ) ;
53
55
}
54
56
55
- this . _width = gifDecoder . frameWidth ;
56
- this . _height = gifDecoder . frameHeight ;
57
- }
58
-
59
- IEnumerator _startDecoding ( ) {
60
- var bytesStream = new MemoryStream ( this . _bytes ) ;
57
+ this . _width = this . _decoder . frameWidth ;
58
+ this . _height = this . _decoder . frameHeight ;
61
59
62
- var gifDecoder = new GifDecoder ( ) ;
63
- if ( gifDecoder . read ( bytesStream ) != GifDecoder . STATUS_OK ) {
64
- throw new Exception ( "Failed to decode gif." ) ;
60
+ if ( this . _texture == null ) {
61
+ this . _texture = new Texture2D ( this . _width , this . _height , TextureFormat . BGRA32 , false ) ;
62
+ this . _texture . hideFlags = HideFlags . HideAndDontSave ;
63
+ this . _image = new Image ( this . _texture ) ;
64
+ this . _frameData . frameInfo . image = this . _image ;
65
65
}
66
-
67
- this . _width = gifDecoder . frameWidth ;
68
- this . _height = gifDecoder . frameHeight ;
66
+ this . _frameData . gifFrame = this . _decoder . currentFrame ;
67
+ D . assert ( this . _frameData . gifFrame != null ) ;
69
68
70
69
int i = 0 ;
71
70
while ( true ) {
72
- if ( gifDecoder . nextFrame ( ) != GifDecoder . STATUS_OK ) {
71
+ if ( this . _decoder . nextFrame ( ) != GifDecoder . STATUS_OK ) {
73
72
throw new Exception ( "Failed to decode gif." ) ;
74
73
}
75
74
76
- if ( gifDecoder . done ) {
75
+ if ( this . _decoder . done ) {
77
76
break ;
78
77
}
79
78
80
- var frameData = new FrameData {
81
- gifFrame = gifDecoder . currentFrame
82
- } ;
83
-
84
- this . _frameData = frameData ;
85
79
86
80
i ++ ;
87
81
88
82
yield return null ;
89
83
}
90
84
91
- D . assert ( gifDecoder . frameCount == i ) ;
92
- this . _frameCount = gifDecoder . frameCount ;
93
- this . _repetitionCount = gifDecoder . loopCount ;
85
+ D . assert ( this . _decoder . frameCount == i ) ;
86
+ this . _frameCount = this . _decoder . frameCount ;
87
+ this . _repetitionCount = this . _decoder . loopCount ;
94
88
this . _isDone = true ;
95
89
}
96
90
@@ -120,10 +114,7 @@ public FrameInfo getNextFrame() {
120
114
this . _nextFrame ( ) ;
121
115
this . _texture . LoadRawTextureData ( this . _frameData . gifFrame . bytes ) ;
122
116
this . _texture . Apply ( ) ;
123
- this . _frameData . frameInfo = new FrameInfo ( ) {
124
- image = this . _image ,
125
- duration = TimeSpan . FromMilliseconds ( this . _frameData . gifFrame . delay )
126
- } ;
117
+ this . _frameData . frameInfo . duration = TimeSpan . FromMilliseconds ( this . _frameData . gifFrame . delay ) ;
127
118
return this . _frameData . frameInfo ;
128
119
}
129
120
0 commit comments