1 | unit UMainForm;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
|
---|
9 | UBitStream;
|
---|
10 |
|
---|
11 | type
|
---|
12 | TArrayOfByte = array of Byte;
|
---|
13 |
|
---|
14 | { TMainForm }
|
---|
15 |
|
---|
16 | TMainForm = class(TForm)
|
---|
17 | Button1: TButton;
|
---|
18 | Button2: TButton;
|
---|
19 | Button3: TButton;
|
---|
20 | Memo1: TMemo;
|
---|
21 | procedure Button1Click(Sender: TObject);
|
---|
22 | procedure Button2Click(Sender: TObject);
|
---|
23 | procedure Button3Click(Sender: TObject);
|
---|
24 | procedure FormShow(Sender: TObject);
|
---|
25 | private
|
---|
26 | procedure PrintBitStream(Stream: TBitStream; StringList: TStrings);
|
---|
27 | procedure PrintData(Data: TArrayOfByte; Count: Integer; StringList: TStrings);
|
---|
28 | procedure PrintStream(Stream: TStream; StringList: TStrings);
|
---|
29 | public
|
---|
30 | end;
|
---|
31 |
|
---|
32 | var
|
---|
33 | MainForm: TMainForm;
|
---|
34 |
|
---|
35 | implementation
|
---|
36 |
|
---|
37 | {$R *.lfm}
|
---|
38 |
|
---|
39 | procedure TMainForm.FormShow(Sender: TObject);
|
---|
40 | begin
|
---|
41 | WindowState := wsMaximized;
|
---|
42 | end;
|
---|
43 |
|
---|
44 | procedure TMainForm.Button1Click(Sender: TObject);
|
---|
45 | var
|
---|
46 | BitStream: TMemoryBitStream;
|
---|
47 | BitStream2: TMemoryBitStream;
|
---|
48 | Buffer: array of Byte;
|
---|
49 | I: Integer;
|
---|
50 | Y: Integer;
|
---|
51 | C: Integer;
|
---|
52 | begin
|
---|
53 | with Memo1, Lines do try
|
---|
54 | BeginUpdate;
|
---|
55 | BitStream := TMemoryBitStream.Create;
|
---|
56 | BitStream2 := TMemoryBitStream.Create;
|
---|
57 | SetLength(Buffer, 4);
|
---|
58 | Buffer[0] := $21;
|
---|
59 | Buffer[1] := $43;
|
---|
60 | Buffer[2] := $65;
|
---|
61 | Buffer[3] := $87;
|
---|
62 | Add('Source data:');
|
---|
63 | PrintData(Buffer, Length(Buffer) * 8, Lines);
|
---|
64 |
|
---|
65 | BitStream.Write(Buffer[0], Length(Buffer) * 8);
|
---|
66 | Add('Write data to stream:');
|
---|
67 | PrintBitStream(BitStream, Lines);
|
---|
68 |
|
---|
69 | // Write second bit array after first which lead to store data as shifted
|
---|
70 | BitStream.Write(Buffer[0], Length(Buffer) * 8);
|
---|
71 | Add('Append shifted data to stream:');
|
---|
72 | PrintBitStream(BitStream, Lines);
|
---|
73 |
|
---|
74 | // Read test of sub bit array
|
---|
75 | BitStream.Position := 1;
|
---|
76 | BitStream.Read(Buffer[0], 32);
|
---|
77 | Add('Read bit data part:');
|
---|
78 | PrintData(Buffer, Length(Buffer) * 8, Lines);
|
---|
79 |
|
---|
80 | (* // Test stream copy
|
---|
81 | Add('Copy bit block to another stream:');
|
---|
82 | for I := 0 to BitStream.Size do begin
|
---|
83 | BitStream.Position := I;
|
---|
84 | BitStream2.Size := 0;
|
---|
85 | BitStream2.CopyFrom(BitStream, BitStream.Size);
|
---|
86 | PrintBitStream(BitStream2, Lines);
|
---|
87 | end;
|
---|
88 | for I := 0 to BitStream.Size do begin
|
---|
89 | BitStream.Position := 0;
|
---|
90 | BitStream2.Size := 0;
|
---|
91 | BitStream2.Position := I;
|
---|
92 | BitStream2.CopyFrom(BitStream, BitStream.Size);
|
---|
93 | PrintBitStream(BitStream2, Lines);
|
---|
94 | end; *)
|
---|
95 | finally
|
---|
96 | BitStream.Free;
|
---|
97 | BitStream2.Free;
|
---|
98 | EndUpdate;
|
---|
99 | end;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TMainForm.Button2Click(Sender: TObject);
|
---|
103 | var
|
---|
104 | BitStream: TMemoryBitStream;
|
---|
105 | BitStream2: TMemoryBitStream;
|
---|
106 | Buffer: array of Byte;
|
---|
107 | I: Integer;
|
---|
108 | Y: Integer;
|
---|
109 | C: Integer;
|
---|
110 | begin
|
---|
111 | with Memo1, Lines do try
|
---|
112 | BeginUpdate;
|
---|
113 | BitStream := TMemoryBitStream.Create;
|
---|
114 | BitStream2 := TMemoryBitStream.Create;
|
---|
115 |
|
---|
116 | SetLength(Buffer, 4);
|
---|
117 | Buffer[0] := $21;
|
---|
118 | Buffer[1] := $43;
|
---|
119 | Buffer[2] := $65;
|
---|
120 | Buffer[3] := $87;
|
---|
121 | Add('Source data:');
|
---|
122 | PrintData(Buffer, Length(Buffer) * 8, Lines);
|
---|
123 |
|
---|
124 | BitStream.Write(Buffer[0], Length(Buffer) * 8);
|
---|
125 | Add('Write data to stream:');
|
---|
126 | PrintBitStream(BitStream, Lines);
|
---|
127 |
|
---|
128 | Add('Bit copy of substreams');
|
---|
129 | SetLength(Buffer, 1000);
|
---|
130 | for Y := 1 to BitStream.Size do begin
|
---|
131 | Add('Size: ' + IntToStr(Y));
|
---|
132 | for I := 0 to BitStream.Size - Y do begin
|
---|
133 | BitStream.Position := I;
|
---|
134 | BitStream2.Size := 0;
|
---|
135 | //BitStream2.CopyFrom(BitStream, Y);
|
---|
136 | //PrintBitStream(BitStream2, Lines);
|
---|
137 | SetLength(Buffer, 1000);
|
---|
138 | C := BitStream.Read(Buffer[0], Y);
|
---|
139 | SetLength(Buffer, C div 8 + 1);
|
---|
140 | PrintData(Buffer, C, Lines);
|
---|
141 | end;
|
---|
142 | Add('');
|
---|
143 | end;
|
---|
144 | finally
|
---|
145 | BitStream.Free;
|
---|
146 | BitStream2.Free;
|
---|
147 | EndUpdate;
|
---|
148 | end;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | procedure TMainForm.Button3Click(Sender: TObject);
|
---|
152 | var
|
---|
153 | BitStream: TMemoryBitStream;
|
---|
154 | BitStream2: TMemoryBitStream;
|
---|
155 | Buffer: array of Byte;
|
---|
156 | I: Integer;
|
---|
157 | Y: Integer;
|
---|
158 | C: Integer;
|
---|
159 | begin
|
---|
160 | with Memo1, Lines do try
|
---|
161 | BeginUpdate;
|
---|
162 | BitStream := TMemoryBitStream.Create;
|
---|
163 | BitStream2 := TMemoryBitStream.Create;
|
---|
164 |
|
---|
165 | SetLength(Buffer, 5);
|
---|
166 | Buffer[0] := $10;
|
---|
167 | Buffer[1] := $32;
|
---|
168 | Buffer[2] := $54;
|
---|
169 | Buffer[3] := $76;
|
---|
170 | Buffer[4] := $98;
|
---|
171 | Add('Source data:');
|
---|
172 | PrintData(Buffer, Length(Buffer) * 8, Lines);
|
---|
173 |
|
---|
174 | Add('Bit copy of substreams');
|
---|
175 | BitStream.Size := Length(Buffer) * 8;
|
---|
176 | SetLength(Buffer, 1000);
|
---|
177 | for Y := 0 to BitStream.Size do begin
|
---|
178 | Add('Size: ' + IntToStr(Y));
|
---|
179 | for I := 0 to BitStream.Size - Y do begin
|
---|
180 | BitStream.Position := 0;
|
---|
181 | for C := 0 to BitStream.Size - 1 do
|
---|
182 | BitStream.WriteBit(True);
|
---|
183 | BitStream.Position := I;
|
---|
184 | C := BitStream.Write(Buffer[0], Y);
|
---|
185 | PrintBitStream(BitStream, Lines);
|
---|
186 | end;
|
---|
187 | Add('');
|
---|
188 | end;
|
---|
189 |
|
---|
190 | Add('Bit copy of substreams');
|
---|
191 | SetLength(Buffer, 1000);
|
---|
192 | for Y := 0 to BitStream.Size do begin
|
---|
193 | Add('Size: ' + IntToStr(Y));
|
---|
194 | for I := 0 to BitStream.Size - Y do begin
|
---|
195 | BitStream.Position := 0;
|
---|
196 | for C := 0 to BitStream.Size - 1 do
|
---|
197 | BitStream.WriteBit(False);
|
---|
198 | BitStream.Position := I;
|
---|
199 | C := BitStream.Write(Buffer[0], Y);
|
---|
200 | PrintBitStream(BitStream, Lines);
|
---|
201 | end;
|
---|
202 | Add('');
|
---|
203 | end;
|
---|
204 | finally
|
---|
205 | BitStream.Free;
|
---|
206 | BitStream2.Free;
|
---|
207 | EndUpdate;
|
---|
208 | end;
|
---|
209 | end;
|
---|
210 |
|
---|
211 | procedure TMainForm.PrintData(Data: TArrayOfByte; Count: Integer; StringList: TStrings);
|
---|
212 | var
|
---|
213 | I: Integer;
|
---|
214 | B: Integer;
|
---|
215 | OneByte: Byte;
|
---|
216 | Line: string;
|
---|
217 | begin
|
---|
218 | Text := '';
|
---|
219 | if Length(Data) > 0 then
|
---|
220 | for I := 0 to High(Data) do begin
|
---|
221 | OneByte := Data[I];
|
---|
222 | for B := 0 to 7 do begin
|
---|
223 | if ((I * 8) + B) >= Count then Break;
|
---|
224 | Line := Line + IntToStr((OneByte shr B) and 1);
|
---|
225 | end;
|
---|
226 | end;
|
---|
227 | StringList.Add(Line);
|
---|
228 | //StringList.Add('');
|
---|
229 | end;
|
---|
230 |
|
---|
231 | procedure TMainForm.PrintStream(Stream: TStream; StringList: TStrings);
|
---|
232 | var
|
---|
233 | I: Integer;
|
---|
234 | B: Integer;
|
---|
235 | Data: Byte;
|
---|
236 | Line: string;
|
---|
237 | begin
|
---|
238 | Line := '';
|
---|
239 | Stream.Position := 0;
|
---|
240 | for I := 0 to Stream.Size - 1 do begin
|
---|
241 | Data := Stream.ReadByte;
|
---|
242 | for B := 0 to 7 do
|
---|
243 | Line := Line + IntToStr((Data shr B) and 1);
|
---|
244 | end;
|
---|
245 | StringList.Add(Line);
|
---|
246 | StringList.Add('');
|
---|
247 | end;
|
---|
248 |
|
---|
249 | procedure TMainForm.PrintBitStream(Stream: TBitStream; StringList: TStrings);
|
---|
250 | begin
|
---|
251 | StringList.Add(Stream.AsString);
|
---|
252 | end;
|
---|
253 |
|
---|
254 | end.
|
---|
255 |
|
---|