@@ -73,18 +73,18 @@ public GifDecoder(byte[] gifData)
73
73
74
74
#region CurrentImageData
75
75
76
- private int _imageLeft ; // 该帧图像绘制起点的左值(x)
77
- private int _imageTop ; // 该帧图像绘制起点的顶值(y)
78
- private int _imageWidth ; // 该帧图像绘制的宽度
79
- private int _imageHeight ; // 该帧图像绘制的高度
76
+ private int _imageLeft ; // 该帧图像绘制起点的左值(x)
77
+ private int _imageTop ; // 该帧图像绘制起点的顶值(y)
78
+ private int _imageWidth ; // 该帧图像绘制的宽度
79
+ private int _imageHeight ; // 该帧图像绘制的高度
80
80
81
81
#endregion
82
82
83
83
#region Data for LZWDecompress
84
84
85
- private int [ ] _indices = new int [ 4096 ] ; // 编译表 ,存储了实际code在codes中的下标
86
- private ushort [ ] _codes = new ushort [ 128 * 1024 ] ; // 颜色表,存储实际的code
87
- private uint [ ] _curBlock = new uint [ 64 ] ; // 当前Image sub block 的数据 最多 256/4
85
+ private int [ ] _indices = new int [ 4096 ] ; // 编译表 ,存储了实际code在codes中的下标
86
+ private ushort [ ] _codes = new ushort [ 128 * 1024 ] ; // 颜色表,存储实际的code
87
+ private uint [ ] _curBlock = new uint [ 64 ] ; // 当前Image sub block 的数据 最多 256/4
88
88
89
89
#endregion
90
90
@@ -288,26 +288,26 @@ private void Interlace()
288
288
289
289
private void LZWDecompress ( Color32 [ ] colorTable , int width , int height )
290
290
{
291
- // 图像数据使用一维数组存储
292
- int row = ( height - _imageTop - 1 ) * width ; // y值,gif从左上角开始,unity texture2d从左下角开始
293
- int col = _imageLeft ; // x 的 最小值
294
- int rightEdge = _imageLeft + _imageWidth ; // x的最大值
291
+ // 图像数据使用一维数组存储
292
+ int row = ( height - _imageTop - 1 ) * width ; // y值,gif从左上角开始,unity texture2d从左下角开始
293
+ int col = _imageLeft ; //x 的 最小值
294
+ int rightEdge = _imageLeft + _imageWidth ; // x的最大值
295
295
//Debug.LogError(col + " " + rightEdge + " " + (height - _imageTop));
296
- int minTableSize = gifData [ _byteIndex ++ ] ; // 最小/起始 颜色长度(单位为bit),该值范围为2-8
296
+ int minTableSize = gifData [ _byteIndex ++ ] ; // 最小/起始 颜色长度(单位为bit),该值范围为2-8
297
297
//if (minTableSize > 11)
298
298
// minTableSize = 11;
299
299
300
- // 初始化编译表
300
+ // 初始化编译表
301
301
302
- int codeSize = minTableSize + 1 ; // 单个数字的初始的编码长度,最长不超过12位
303
- int nextSize = Pow2 [ codeSize ] ; // 下一次编译表扩容时的最大容量
304
- int maxCodesLength = Pow2 [ minTableSize ] ; // 当前编译表中的最大容量(不计下面两个)
305
- int clearCode = maxCodesLength ; // 清除标记
306
- int endCode = maxCodesLength + 1 ; // 结束标记
302
+ int codeSize = minTableSize + 1 ; // 单个数字的初始的编码长度,最长不超过12位
303
+ int nextSize = Pow2 [ codeSize ] ; // 下一次编译表扩容时的最大容量
304
+ int maxCodesLength = Pow2 [ minTableSize ] ; // 当前编译表中的最大容量(不计下面两个)
305
+ int clearCode = maxCodesLength ; // 清除标记
306
+ int endCode = maxCodesLength + 1 ; // 结束标记
307
307
//Debug.Log("endCode:" + endCode);
308
308
309
- int codesEnd = 0 ; // 实际code的index
310
- int codesNum = maxCodesLength + 2 ; // 编译表长度
309
+ int codesEnd = 0 ; // 实际code的index
310
+ int codesNum = maxCodesLength + 2 ; // 编译表长度
311
311
//Debug.Log("Start:" + codesNum);
312
312
//Dictionary<uint, int[]> _dic = new Dictionary<uint, int[]>();
313
313
//for (uint i = 0; i < maxCodesLength; i++)
@@ -318,41 +318,41 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
318
318
for ( ushort i = 0 ; i < codesNum ; i ++ )
319
319
{
320
320
_indices [ i ] = codesEnd ;
321
- _codes [ codesEnd ++ ] = 1 ; // 之后跟着的code个数
321
+ _codes [ codesEnd ++ ] = 1 ; // 之后跟着的code个数
322
322
_codes [ codesEnd ++ ] = i ; // code
323
323
}
324
324
//Debug.Log("_codes end:"+codesEnd+" " + _codes[codesEnd-1]);
325
- //LZW解压缩 loop
325
+ //LZW解压缩 loop
326
326
327
- uint previousCode = NoCode ; // 前缀
328
- // tip: gif存储颜色数字时采用可变编码(即舍弃高位无用的0),
329
- uint mask = ( uint ) ( nextSize - 1 ) ; //用于提取数字,从低位开始
330
- uint shiftRegister = 0 ; //移位缓存器
327
+ uint previousCode = NoCode ; // 前缀
328
+ // tip: gif存储颜色数字时采用可变编码(即舍弃高位无用的0),
329
+ uint mask = ( uint ) ( nextSize - 1 ) ; //用于提取数字,从低位开始
330
+ uint shiftRegister = 0 ; //移位缓存器
331
331
332
- int bitsAvailable = 0 ; // 当前在移位缓存器可读取的bit数
333
- int bytesAvailable = 0 ; // 当前Image sub block中剩余的byte数
332
+ int bitsAvailable = 0 ; // 当前在移位缓存器可读取的bit数
333
+ int bytesAvailable = 0 ; // 当前Image sub block中剩余的byte数
334
334
335
335
int blockPos = 0 ;
336
336
337
337
while ( true )
338
338
{
339
- // 读取数字
339
+ // 读取数字
340
340
uint curCode = shiftRegister & mask ;
341
341
342
342
if ( bitsAvailable >= codeSize )
343
343
{
344
- // 如果剩余的bits可供读取
344
+ // 如果剩余的bits可供读取
345
345
bitsAvailable -= codeSize ;
346
346
shiftRegister >>= codeSize ;
347
347
}
348
348
else
349
349
{
350
- // 否则,此时curCode中存储了剩余的bits(实际数字的部分bits)
350
+ // 否则,此时curCode中存储了剩余的bits(实际数字的部分bits)
351
351
// reload shift register
352
352
353
353
354
354
// if start of new block
355
- // 当前子块已用完,读取下一个子块
355
+ // 当前子块已用完,读取下一个子块
356
356
if ( bytesAvailable <= 0 )
357
357
{
358
358
// read blocksize
@@ -372,7 +372,7 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
372
372
}
373
373
374
374
// load shift register
375
- // 刷新缓存器
375
+ // 刷新缓存器
376
376
shiftRegister = _curBlock [ blockPos ++ ] ;
377
377
int newBits = bytesAvailable >= 4 ? 32 : bytesAvailable * 8 ;
378
378
bytesAvailable -= 4 ;
@@ -381,17 +381,17 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
381
381
382
382
if ( bitsAvailable > 0 )
383
383
{
384
- // 读取数字时,可能会跨越不同的ImageSubBlock(即可简单理解为ImageData初始是连续的数字流,为切成一个个子块,
385
- // 人为插入了子块的大小)
386
- // 若curCode中已存储部分bits
384
+ // 读取数字时,可能会跨越不同的ImageSubBlock(即可简单理解为ImageData初始是连续的数字流,为切成一个个子块,
385
+ // 人为插入了子块的大小)
386
+ // 若curCode中已存储部分bits
387
387
var bitsRemaining = codeSize - bitsAvailable ;
388
388
curCode |= ( shiftRegister << bitsAvailable ) & mask ;
389
389
shiftRegister >>= bitsRemaining ;
390
390
bitsAvailable = newBits - bitsRemaining ;
391
391
}
392
392
else
393
393
{
394
- // 此时curCode为0
394
+ // 此时curCode为0
395
395
curCode = shiftRegister & mask ;
396
396
shiftRegister >>= codeSize ;
397
397
bitsAvailable = newBits - codeSize ;
@@ -422,7 +422,7 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
422
422
break ;
423
423
}
424
424
425
- // 处理数字大致流程如下
425
+ // 处理数字大致流程如下
426
426
// let CODE be the next code in the code stream
427
427
// is CODE in the code table?
428
428
// Yes:
@@ -436,8 +436,8 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
436
436
// add { PREVCODE} +K to code table
437
437
// set PREVCODE = CODE
438
438
439
- // 比较可知,两种情况下,均要将{ PREVCODE} +K 填入编译表,只是k的来源不同
440
- // 均要PREVCODE = CODE,不同点在于“Yes”时只输出{ CODE}到output,而“No”时,还需将k输出到output
439
+ // 比较可知,两种情况下,均要将{ PREVCODE} +K 填入编译表,只是k的来源不同
440
+ // 均要PREVCODE = CODE,不同点在于“Yes”时只输出{ CODE}到output,而“No”时,还需将k输出到output
441
441
442
442
bool plusOne = false ;
443
443
int codePos = 0 ;
@@ -451,7 +451,7 @@ private void LZWDecompress(Color32[] colorTable,int width,int height)
451
451
{
452
452
// write previous code
453
453
codePos = _indices [ previousCode ] ;
454
- plusOne = true ; //标记需将k输出到output
454
+ plusOne = true ; //标记需将k输出到output
455
455
}
456
456
else
457
457
{
@@ -636,16 +636,16 @@ private void ReadHeader()
636
636
}
637
637
}
638
638
639
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] // 积极内联
639
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] // 积极内联
640
640
private byte ReadByte ( )
641
641
{
642
642
return gifData [ _byteIndex ++ ] ;
643
643
}
644
644
645
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] // 积极内联
645
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] // 积极内联
646
646
private ushort ReadUInt16 ( )
647
647
{
648
- // gif 默认小端,即数字以从低到高位存储
648
+ // gif 默认小端,即数字以从低到高位存储
649
649
return ( ushort ) ( gifData [ _byteIndex ++ ] | gifData [ _byteIndex ++ ] << 8 ) ;
650
650
}
651
651
0 commit comments