forked from lilith/GifLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamHelper.cs
More file actions
312 lines (288 loc) · 11.3 KB
/
StreamHelper.cs
File metadata and controls
312 lines (288 loc) · 11.3 KB
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
#region Copyright & License
/*----------------------------------------------------------------
// Copyright (C) 2008 jillzhang 版权所有。
//
// 文件名:StreamHelper.cs
// 文件功能描述:
//
// 创建标识:jillzhang
// 修改标识:
// 修改描述:
//
// 修改标识:
// 修改描述:
//----------------------------------------------------------------*/
/*-------------------------New BSD License ------------------
Copyright (c) 2008, jillzhang
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of jillzhang nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Jillzhang.GifUtility
{
internal class StreamHelper
{
Stream stream;
internal StreamHelper(Stream stream)
{
this.stream = stream;
}
//读取指定长度的字节字节
internal byte[] ReadByte(int len)
{
byte[] buffer = new byte[len];
stream.Read(buffer, 0, len);
return buffer;
}
/// <summary>
/// 读取一个字节
/// </summary>
/// <returns></returns>
internal int Read()
{
return stream.ReadByte();
}
internal short ReadShort()
{
byte[] buffer = new byte[2];
stream.Read(buffer, 0, buffer.Length);
return BitConverter.ToInt16(buffer, 0);
}
internal string ReadString(int length)
{
return new string(ReadChar(length));
}
internal char[] ReadChar(int length)
{
byte[] buffer = new byte[length];
stream.Read(buffer, 0, length);
char[] charBuffer = new char[length];
buffer.CopyTo(charBuffer, 0);
return charBuffer;
}
internal void WriteString(string str)
{
char[] charBuffer = str.ToCharArray();
byte[] buffer = new byte[charBuffer.Length];
int index = 0;
foreach (char c in charBuffer)
{
buffer[index] = (byte)c;
index++;
}
WriteBytes(buffer);
}
internal void WriteBytes(byte[] buffer)
{
stream.Write(buffer, 0, buffer.Length);
}
#region 从文件流中读取应用程序扩展块
/// <summary>
/// 从文件流中读取应用程序扩展块
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
internal ApplicationEx GetApplicationEx(Stream stream)
{
ApplicationEx appEx = new ApplicationEx();
int blockSize = Read();
if (blockSize != ApplicationEx.BlockSize)
{
throw new Exception("数据格式错误!");
}
appEx.ApplicationIdentifier = ReadChar(8);
appEx.ApplicationAuthenticationCode = ReadChar(3);
int nextFlag = Read();
appEx.Datas = new List<DataStruct>();
while (nextFlag != 0)
{
DataStruct data = new DataStruct(nextFlag, stream);
appEx.Datas.Add(data);
nextFlag = Read();
}
return appEx;
}
#endregion
#region 从文件数据流中读取注释扩展块
internal CommentEx GetCommentEx(Stream stream)
{
CommentEx cmtEx = new CommentEx();
StreamHelper streamHelper = new StreamHelper(stream);
cmtEx.CommentDatas = new List<string>();
int nextFlag = streamHelper.Read();
cmtEx.CommentDatas = new List<string>();
while (nextFlag != 0)
{
int blockSize = nextFlag;
string data = streamHelper.ReadString(blockSize);
cmtEx.CommentDatas.Add(data);
nextFlag = streamHelper.Read();
}
return cmtEx;
}
#endregion
#region 从文件数据流中读取注释扩展块
/// <summary>
/// 从文件数据流中读取图形文本扩展(Plain Text Extension)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
internal PlainTextEx GetPlainTextEx(Stream stream)
{
PlainTextEx pltEx = new PlainTextEx();
int blockSize = Read();
if (blockSize != PlainTextEx.BlockSize)
{
throw new Exception("数据格式错误!");
}
pltEx.XOffSet = ReadShort();
pltEx.YOffSet = ReadShort();
pltEx.Width = ReadShort();
pltEx.Height = ReadShort();
pltEx.CharacterCellWidth = (byte)Read();
pltEx.CharacterCellHeight = (byte)Read();
pltEx.ForegroundColorIndex = (byte)Read();
pltEx.BgColorIndex = (byte)Read();
int nextFlag = Read();
pltEx.TextDatas = new List<string>();
while (nextFlag != 0)
{
blockSize = nextFlag;
string data = ReadString(blockSize);
pltEx.TextDatas.Add(data);
nextFlag = Read();
}
return pltEx;
}
#endregion
#region 从文件数据流中读取注释扩展块
/// <summary>
/// 从文件数据流中读取 图象标识符(Image Descriptor)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
internal ImageDescriptor GetImageDescriptor(Stream stream)
{
ImageDescriptor ides = new ImageDescriptor();
ides.XOffSet = ReadShort();
ides.YOffSet = ReadShort();
ides.Width = ReadShort();
ides.Height = ReadShort();
ides.Packed = (byte)Read();
ides.LctFlag = ((ides.Packed & 0x80) >> 7) == 1;
ides.InterlaceFlag = ((ides.Packed & 0x40) >> 6) == 1;
ides.SortFlag = ((ides.Packed & 0x20) >> 5) == 1;
ides.LctSize = (2 << (ides.Packed & 0x07));
return ides;
}
#endregion
#region 从文件数据流中读取图形控制扩展(Graphic Control Extension)
/// <summary>
/// 从文件数据流中读取图形控制扩展(Graphic Control Extension)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
internal GraphicEx GetGraphicControlExtension(Stream stream)
{
GraphicEx gex = new GraphicEx();
int blockSize = Read();
if (blockSize != GraphicEx.BlockSize)
{
throw new Exception("数据格式错误!");
}
gex.Packed = (byte)Read();
gex.TransparencyFlag = (gex.Packed & 0x01) == 1;
gex.DisposalMethod = (gex.Packed & 0x1C) >> 2;
gex.Delay = ReadShort();
gex.TranIndex = (byte)Read();
Read();
return gex;
}
#endregion
#region 从文件数据流中逻辑屏幕标识符(Logical Screen Descriptor)
/// <summary>
/// 从文件数据流中读取图形控制扩展(Graphic Control Extension)
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
internal LogicalScreenDescriptor GetLCD(Stream stream)
{
LogicalScreenDescriptor lcd = new LogicalScreenDescriptor();
lcd.Width = ReadShort();
lcd.Height = ReadShort();
lcd.Packed = (byte)Read();
lcd.GlobalColorTableFlag = ((lcd.Packed & 0x80) >> 7) == 1;
lcd.ColorResoluTion = (byte)((lcd.Packed & 0x60) >> 5);
lcd.SortFlag = (byte)(lcd.Packed & 0x10) >> 4;
lcd.GlobalColorTableSize = 2 << (lcd.Packed & 0x07);
lcd.BgColorIndex = (byte)Read();
lcd.PixcelAspect = (byte)Read();
return lcd;
}
#endregion
#region 写文件头
/// <summary>
/// 写文件头
/// </summary>
/// <param name="header">文件头</param>
internal void WriteHeader(string header)
{
WriteString(header);
}
#endregion
#region 写逻辑屏幕标识符
/// <summary>
/// 写逻辑屏幕标识符
/// </summary>
/// <param name="lsd"></param>
internal void WriteLSD(LogicalScreenDescriptor lsd)
{
WriteBytes(lsd.GetBuffer());
}
#endregion
#region 写全局颜色表
/// <summary>
/// 写全局颜色表
/// </summary>
/// <param name="buffer">全局颜色表</param>
internal void SetGlobalColorTable(byte[] buffer)
{
WriteBytes(buffer);
}
#endregion
#region 写入注释扩展集合
/// <summary>
/// 写入注释扩展集合
/// </summary>
/// <param name="comments">注释扩展集合</param>
internal void SetCommentExtensions(List<CommentEx> comments)
{
foreach (CommentEx ce in comments)
{
WriteBytes(ce.GetBuffer());
}
}
#endregion
#region 写入应用程序展集合
/// <summary>
/// 写入应用程序展集合
/// </summary>
/// <param name="comments">写入应用程序展集合</param>
internal void SetApplicationExtensions(List<ApplicationEx> applications)
{
foreach (ApplicationEx ap in applications)
{
WriteBytes(ap.GetBuffer());
}
}
#endregion
}
}