本文整理汇总了C#中BitWriter.WriteBits方法的典型用法代码示例。如果您正苦于以下问题:C# BitWriter.WriteBits方法的具体用法?C# BitWriter.WriteBits怎么用?C# BitWriter.WriteBits使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitWriter
的用法示例。
在下文中一共展示了BitWriter.WriteBits方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BitWriterThreeUnitsTest
public void BitWriterThreeUnitsTest()
{
using (MemoryStream ms = new MemoryStream()) {
BitWriter bw = new BitWriter()
{
Output = ms,
CodeLength = 4,
};
bw.WriteBits(5);
bw.WriteBits(7);
bw.WriteBits(1);
bw.FlushBits();
bw.FlushBytes();
byte[] bytes = ms.ToArray();
Assert.AreEqual(3, bytes.Length);
Assert.AreEqual(0x02, bytes[0]);
Assert.AreEqual(0x75, bytes[1]);
Assert.AreEqual(0x01, bytes[2]);
}
}
示例2: WriteRIFFHeader
public void WriteRIFFHeader(Stream outputStream)
{
uint dataSize = (uint)Math.Ceiling(((double)TotalSamples * ((double)BitsPerSample / 8d) * (double)(NumberOfChannels)));
var streamSize = dataSize + WAV_HEADER_SIZE;
uint fileSize = dataSize + 36;
BitWriter wr = new BitWriter(outputStream);
wr.WriteBits(WAV_HDR_RIFF, 32);
wr.WriteLE(fileSize);
wr.WriteBits(WAV_HDR_WAVE, 32);
wr.WriteBits(WAV_HDR_FMT, 32);
wr.WriteLE(WAV_FORMATEX_SIZE);
wr.WriteLE((ushort)AudioFormat);
wr.WriteLE(NumberOfChannels);
wr.WriteLE(SampleRate);
wr.WriteLE((uint)(SampleRate * BitsPerSample * NumberOfChannels / 8)); //avg bytes
wr.WriteLE((ushort)(BitsPerSample * NumberOfChannels / 8));
wr.WriteLE(BitsPerSample);
wr.WriteBits(WAV_HDR_DATA, 32);
wr.WriteLE(dataSize);
}
示例3: Write
internal void Write(BitWriter writer)
{
writer.WriteBits((ulong)nodetype, 2);
Helpers.WriteNumber(writer, stackid);
if(nodetype == NodeType.AssemblyLoad)
{
Helpers.WriteNumber(writer, nameId);
}
Helpers.WriteNumber(writer, 1 + kidOffset);
Helpers.WriteNumber(writer, 1 + prevOffset);
Helpers.WriteNumber(writer, nodeOffset);
data.Write(writer);
}
示例4: MessageResourceList
private void MessageResourceList()
{
Int32 startByteIndex = parser.BitBuffer.CurrentByte;
// read message
if (demo.NetworkProtocol <= 43)
{
parser.BitBuffer.Endian = BitBuffer.EndianType.Big;
}
UInt32 nEntries = parser.BitBuffer.ReadUnsignedBits(12);
List<Resource> resourceList = new List<Resource>((Int32)nEntries);
for (Int32 i = 0; i < nEntries; i++)
{
Resource r = new Resource();
r.type = parser.BitBuffer.ReadUnsignedBits(4);
r.name = parser.BitBuffer.ReadString();
r.index = parser.BitBuffer.ReadUnsignedBits(12);
r.fileSize = parser.BitBuffer.ReadBits(24); // signed?
r.flags = parser.BitBuffer.ReadUnsignedBits(3);
if ((r.flags & 4) != 0) // md5 hash (RES_CUSTOM?)
{
r.md5Hash = parser.BitBuffer.ReadBytes(16);
}
r.hasReservedData = parser.BitBuffer.ReadBoolean();
if (r.hasReservedData)
{
r.reservedData = parser.BitBuffer.ReadBytes(32);
}
if (demo.Game == null || demo.Game.ConvertResourceListCallback(demo, r.type, r.index, ref r.name))
{
resourceList.Add(r);
}
}
// consistency list
// indices of resources to force consistency upon?
if (parser.BitBuffer.ReadBoolean())
{
while (parser.BitBuffer.ReadBoolean())
{
Int32 nBits = (parser.BitBuffer.ReadBoolean() ? 5 : 10);
parser.BitBuffer.SeekBits(nBits);
}
}
parser.BitBuffer.SkipRemainingBits();
parser.BitBuffer.Endian = BitBuffer.EndianType.Little;
// stop now if we're not converting network protocols
if (!demo.ConvertNetworkProtocol())
{
return;
}
// create new message
BitWriter bitWriter = new BitWriter();
bitWriter.WriteUnsignedBits((UInt32)resourceList.Count, 12);
foreach (Resource r in resourceList)
{
bitWriter.WriteUnsignedBits(r.type, 4);
bitWriter.WriteString(r.name);
bitWriter.WriteUnsignedBits(r.index, 12);
bitWriter.WriteBits(r.fileSize, 24);
bitWriter.WriteUnsignedBits(r.flags, 3);
if ((r.flags & 4) != 0) // md5 hash
{
bitWriter.WriteBytes(r.md5Hash);
}
bitWriter.WriteBoolean(r.hasReservedData);
if (r.hasReservedData)
{
bitWriter.WriteBytes(r.reservedData);
}
}
bitWriter.WriteBoolean(false); // consistency list
// remove old message
Int32 endByteIndex = parser.BitBuffer.CurrentByte;
parser.Seek(startByteIndex, SeekOrigin.Begin);
parser.BitBuffer.RemoveBytes(endByteIndex - startByteIndex);
// insert new message into bitbuffer
parser.BitBuffer.InsertBytes(bitWriter.Data);
}
示例5: WriteNumber
static internal void WriteNumber(BitWriter bw, long n)
{
ulong number = (ulong)n;
if(number < 1)
{
bw.WriteBits(0, 1);
}
else if(number < 16)
{
bw.WriteBits(128 + number, 8);
}
else if(number < 256)
{
bw.WriteBits(7168 + number, 13);
}
else if(number < 4096)
{
bw.WriteBits(499712 + number, 19);
}
else if(number < 65536)
{
bw.WriteBits(1900544 + number, 21);
}
else if(number < 1048576)
{
bw.WriteBits(6291456 + number, 23);
}
else if(number < 16777216)
{
bw.WriteBits(8287944704 + number, 33);
}
else if(number < 268435456)
{
bw.WriteBits(16106127360 + number, 34);
}
else if(number < 4294967296)
{
bw.WriteBits(21474836480 + number, 35);
}
else if(number < 68719476736)
{
bw.WriteBits(618475290624 + number, 40);
}
else if(number < 1099511627776)
{
bw.WriteBits(34084860461056 + number, 45);
}
else if(number < 17592186044416)
{
bw.WriteBits(4327677766926336 + number, 52);
}
else if(number < 281474976710656)
{
bw.WriteBits(558164878817230848 + number, 59);
}
else if(number < 4503599627370496)
{
bw.WriteBits(8926134461448323072 + number, 63);
}
else if(number < 72057594037927936)
{
bw.WriteBits(1981, 11);
bw.WriteBits(number, 56);
}
else if(number < 1152921504606846976)
{
bw.WriteBits(3961, 12);
bw.WriteBits(number, 60);
}
else
{
bw.WriteBits(3960, 12);
bw.WriteBits(number, 64);
}
}
示例6: WriteImageData
private void WriteImageData(Image image)
{
// Figure out which color table is in use and deduce the code size
ColorTable colorTable = image.LocalColorTable ?? GlobalColorTable;
int colorTableBitSize = colorTable.BitSize();
int codeSize = (colorTableBitSize == 1 ? 2 : colorTableBitSize);
if (Debug) {
Console.Error.WriteLine("Writing code size {0} bits", codeSize);
}
WriteByte(codeSize);
// Initial code length is one more than code size, to accomodate the
// Clear and End codes.
BitWriter writer = new BitWriter()
{
Output = Output,
CodeLength = 1 + codeSize,
Debug = this.Debug,
};
// Initialize the prefix table with matches for all single-unit codes
PrefixTable prefixes = new PrefixTable();
for (int n = 0; n < (1 << codeSize); ++n) {
prefixes.Add(n, -1, (byte)n);
}
int minCode = (1 << codeSize) + 2;
int nextCode = minCode;
// Start with a Clear. (Pointless - the decoder knows perfectly well what
// the initial state is.)
if (Debug) {
Console.Error.WriteLine("Bit-writing clear marker {0}", 1 << codeSize);
}
writer.WriteBits(1 << codeSize);
int pos = 0;
if (Debug) {
Console.Error.WriteLine("Bit-writing {0} pixels of image data", image.ImageData.Length);
}
while (pos < image.ImageData.Length) {
// Find the longest known prefix that matches this point in the image data
int bestLength;
int bestCode = prefixes.Find(image.ImageData, pos, out bestLength);
// Output the best code we found
writer.WriteBits(bestCode);
// Add a new dictionary entry
if (nextCode < 4096 && pos + bestLength < image.ImageData.Length) {
if (Debug) {
Console.Error.WriteLine("Define code {0:X} as code {1:X} + byte {2:X2}", nextCode, bestCode, image.ImageData[pos + bestLength]);
}
prefixes.Add(nextCode, bestCode, image.ImageData[pos + bestLength]);
// Update the output bit size
if(nextCode >= (1 << writer.CodeLength)) {
++writer.CodeLength;
}
++nextCode;
}
pos += bestLength;
}
// Finish with an End. (Also pointless - the decoder knows what size the image data should be.)
if (Debug) {
Console.Error.WriteLine("Bit-writing end marker {0}", (1 << codeSize) + 1);
}
writer.WriteBits((1 << codeSize) + 1);
if (Debug) {
Console.Error.WriteLine("Flushing bits");
}
writer.FlushBits();
if (Debug) {
Console.Error.WriteLine("Flushing bytes");
}
writer.FlushBytes();
// A zero-sized sub-block terminates.
if (Debug) {
Console.Error.WriteLine("Writing image terminator");
}
WriteByte(0);
}
示例7: EncodePrediction
private unsafe byte[] EncodePrediction(Bitmap Frame)
{
//We'll just look if there are blocks that are 100% the same first
BitmapData d = Frame.LockBits(new Rectangle(0, 0, Frame.Width, Frame.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData prev = PastFrames[1].LockBits(new Rectangle(0, 0, Frame.Width, Frame.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
for (int y = 0; y < Height; y += 16)
{
for (int x = 0; x < Width; x += 16)
{
MacroBlocks[y / 16][x / 16] = new MacroBlock(d, x, y);
bool same = true;
for (int y2 = 0; y2 < 16; y2++)
{
ulong* pA = (ulong*)(((byte*)d.Scan0) + (y + y2) * d.Stride + x * 4);
ulong* pB = (ulong*)(((byte*)prev.Scan0) + (y + y2) * prev.Stride + x * 4);
for (int x2 = 0; x2 < 16; x2+=2)
{
ulong color = *pA++;
ulong color2 = *pB++;
if (color != color2)
{
same = false;
break;
}
}
if (!same) break;
}
if (same) MacroBlocks[y / 16][x / 16].Predict = true;
else
{
Analyzer.ConfigureBlockY(this, MacroBlocks[y / 16][x / 16]);
int blocktype2 = Analyzer.AnalyzeBlockUV(this, MacroBlocks[y / 16][x / 16]);
MacroBlocks[y / 16][x / 16].UVPredictionMode = blocktype2;
MacroBlocks[y / 16][x / 16].UVUseComplex8x8[0] = true;
MacroBlocks[y / 16][x / 16].UVUseComplex8x8[1] = true;
MacroBlocks[y / 16][x / 16].SetupDCTs(this);
}
}
}
Frame.UnlockBits(d);
PastFrames[1].UnlockBits(prev);
BitWriter b = new BitWriter();
b.WriteBits(0, 1);//Interframe
b.WriteVarIntSigned(0);
for (int y = 0; y < Height; y += 16)
{
for (int x = 0; x < Width; x += 16)
{
MacroBlock curblock = MacroBlocks[y / 16][x / 16];
if (curblock.Predict)
{
b.WriteBits(1, 1);
b.WriteVarIntUnsigned(0);
}
else
{
b.WriteBits(0xE >> 1, 5);
EncodeBlockIntra(curblock, b);
}
}
}
b.WriteBits(0, 16);
b.Flush();
byte[] result = b.ToArray();
return result;
}
示例8: EncodeDCT
private void EncodeDCT(int[] DCT, int Table, BitWriter b)
{
ushort[] r11A = (Table == 1 ? MobiConst.Vx2Table1_A : MobiConst.Vx2Table0_A);
byte[] r11B = (Table == 1 ? MobiConst.Vx2Table1_B : MobiConst.Vx2Table0_B);
int lastnonzero = 0;
for (int i = 0; i < DCT.Length; i++)
{
if (DCT[i] != 0) lastnonzero = i;
}
int skip = 0;
for (int i = 0; i < DCT.Length; i++)
{
if (DCT[i] == 0 && lastnonzero != 0)
{
skip++;
continue;
}
int val = DCT[i];
if (val < 0) val = -val;
if (val <= 31)
{
int idx = MobiConst.VxTable0_A_Ref[val, skip, (i == lastnonzero) ? 1 : 0];
if (idx >= 0)
{
int nrbits = (r11A[idx] & 0xF);
uint tidx = (uint)idx;
if (nrbits < 12)
tidx >>= (12 - nrbits);
else if (nrbits > 12)
tidx <<= (nrbits - 12);
if (DCT[i] < 0) tidx |= 1;
b.WriteBits((uint)tidx, nrbits);
skip = 0;
goto end;
}
int newskip = skip - MobiConst.Vx2Table0_B[(val | (((i == lastnonzero) ? 1 : 0) << 6)) + 0x80];
if (newskip >= 0)
{
idx = MobiConst.VxTable0_A_Ref[val, newskip, (i == lastnonzero) ? 1 : 0];
if (idx >= 0)
{
b.WriteBits(3, 7);
b.WriteBits(1, 1);
b.WriteBits(0, 1);
int nrbits = (r11A[idx] & 0xF);
uint tidx = (uint)idx;
if (nrbits < 12)
tidx >>= (12 - nrbits);
else if (nrbits > 12)
tidx <<= (nrbits - 12);
if (DCT[i] < 0) tidx |= 1;
b.WriteBits((uint)tidx, nrbits);
skip = 0;
goto end;
}
}
}
int newval = val - MobiConst.Vx2Table0_B[skip | (((i == lastnonzero) ? 1 : 0) << 6)];
if (newval >= 0 && newval <= 31)
{
int idx = MobiConst.VxTable0_A_Ref[newval, skip, (i == lastnonzero) ? 1 : 0];
if (idx >= 0)
{
b.WriteBits(3, 7);
b.WriteBits(0, 1);
int nrbits = (r11A[idx] & 0xF);
uint tidx = (uint)idx;
if (nrbits < 12)
tidx >>= (12 - nrbits);
else if (nrbits > 12)
tidx <<= (nrbits - 12);
if (DCT[i] < 0) tidx |= 1;
b.WriteBits((uint)tidx, nrbits);
skip = 0;
goto end;
}
}
//This is easiest way of writing the DCT, but also costs the most bits
b.WriteBits(3, 7);
b.WriteBits(1, 1);
b.WriteBits(1, 1);
if (i == lastnonzero) b.WriteBits(1, 1);
else b.WriteBits(0, 1);
b.WriteBits((uint)skip, 6);
skip = 0;
b.WriteBits((uint)DCT[i], 12);
end:
if (i == lastnonzero) break;
}
}
示例9: EncodeBlockIntra
private void EncodeBlockIntra(MacroBlock Block, BitWriter b)
{
uint dctmask =
(Block.YUseComplex8x8[0] ? 1u : 0) |
((Block.YUseComplex8x8[1] ? 1u : 0) << 1) |
((Block.YUseComplex8x8[2] ? 1u : 0) << 2) |
((Block.YUseComplex8x8[3] ? 1u : 0) << 3) |
((Block.UVUseComplex8x8[0] ? 1u : 0) << 4) |
((Block.UVUseComplex8x8[1] ? 1u : 0) << 5);
b.WriteVarIntUnsigned(REV_byte_115FC4[dctmask]);
b.WriteBits((uint)Block.YPredictionMode, 3);//Block type
for (int y2 = 0; y2 < 2; y2++)
{
for (int x2 = 0; x2 < 2; x2++)
{
if (Block.YUseComplex8x8[x2 + y2 * 2] && !Block.YUse4x4[x2 + y2 * 2])
{
b.WriteBits(1, 1);//Don't use 4x4 blocks
EncodeDCT(Block.YDCT8x8[x2 + y2 * 2], 0, b);
}
else if (Block.YUseComplex8x8[x2 + y2 * 2] && Block.YUse4x4[x2 + y2 * 2])
{
uint dctmask2 =
(Block.YUseDCT4x4[x2 + y2 * 2][0] ? 1u : 0) |
((Block.YUseDCT4x4[x2 + y2 * 2][1] ? 1u : 0) << 1) |
((Block.YUseDCT4x4[x2 + y2 * 2][2] ? 1u : 0) << 2) |
((Block.YUseDCT4x4[x2 + y2 * 2][3] ? 1u : 0) << 3);
b.WriteVarIntUnsigned(REV_byte_1164F4[dctmask2]);
for (int y3 = 0; y3 < 2; y3++)
{
for (int x3 = 0; x3 < 2; x3++)
{
if (Block.YUseDCT4x4[x2 + y2 * 2][x3 + y3 * 2])
EncodeDCT(Block.YDCT4x4[x2 + y2 * 2][x3 + y3 * 2], 0, b);
}
}
}
}
}
b.WriteBits((uint)Block.UVPredictionMode, 3);//Block type
for (int q = 0; q < 2; q++)
{
if (Block.UVUseComplex8x8[q] && !Block.UVUse4x4[q])
{
b.WriteBits(1, 1);//Don't use 4x4 blocks
EncodeDCT(Block.UVDCT8x8[q], 0, b);
}
}
}
示例10: EncodeIntra
private byte[] EncodeIntra(Bitmap Frame)
{
BitmapData d = Frame.LockBits(new Rectangle(0, 0, Frame.Width, Frame.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
//Setup the macroblocks
for (int y = 0; y < Height; y += 16)
{
for (int x = 0; x < Width; x += 16)
{
MacroBlocks[y / 16][x / 16] = new MacroBlock(d, x, y);
Analyzer.ConfigureBlockY(this, MacroBlocks[y / 16][x / 16]);
int blocktype2 = Analyzer.AnalyzeBlockUV(this, MacroBlocks[y / 16][x / 16]);
MacroBlocks[y / 16][x / 16].UVPredictionMode = blocktype2;
MacroBlocks[y / 16][x / 16].UVUseComplex8x8[0] = true;
MacroBlocks[y / 16][x / 16].UVUseComplex8x8[1] = true;
MacroBlocks[y / 16][x / 16].SetupDCTs(this);
}
}
Frame.UnlockBits(d);
BitWriter b = new BitWriter();
b.WriteBits(1, 1);//Interframe
b.WriteBits(1, 1);//YUV format
//TODO: determine table (when we actually use it...)
b.WriteBits(0, 1);//Table
b.WriteBits((uint)Quantizer, 6);//Quantizer
for (int y = 0; y < Height; y += 16)
{
for (int x = 0; x < Width; x += 16)
{
MacroBlock curblock = MacroBlocks[y / 16][x / 16];
//Todo use predicted prediction mode
/*if (x > 0 && y > 0)
{
int r12 = MacroBlocks[y / 16 - 1][x / 16].YPredictionMode;
int r6 = MacroBlocks[y / 16][x / 16 - 1].YPredictionMode;
if (r12 > r6) r12 = r6;
if (r12 == 9) r12 = 3;
r6 = r3 >> 28;
if (r6 >= r12) r6++;
int r7;
if (r6 < 9)
{
r12 = r6;
r7 = 4;
}
else r7 = 1;
r3 <<= r7;
nrBitsRemaining -= r7;
}*/
b.WriteBits(0, 1);//Func
EncodeBlockIntra(curblock, b);
}
}
b.WriteBits(0, 16);
b.Flush();
byte[] result = b.ToArray();
return result;
}
示例11: BitWriterWpTest
public void BitWriterWpTest()
{
using (MemoryStream ms = new MemoryStream()) {
BitWriter bw = new BitWriter()
{
Output = ms,
CodeLength = 9,
};
// http://en.wikipedia.org/wiki/Graphics_Interchange_Format
bw.WriteBits(0x100);
bw.WriteBits(0x028);
bw.WriteBits(0x0FF);
bw.WriteBits(0x103);
bw.WriteBits(0x102);
bw.WriteBits(0x103);
bw.WriteBits(0x106);
bw.WriteBits(0x107);
bw.WriteBits(0x101);
bw.FlushBits();
bw.FlushBytes();
byte[] bytes = ms.ToArray();
Assert.AreEqual(12, bytes.Length);
Assert.AreEqual(0x0B, bytes[0]);
Assert.AreEqual(0x00, bytes[1]);
Assert.AreEqual(0x51, bytes[2]);
Assert.AreEqual(0xFC, bytes[3]);
Assert.AreEqual(0x1B, bytes[4]);
Assert.AreEqual(0x28, bytes[5]);
Assert.AreEqual(0x70, bytes[6]);
Assert.AreEqual(0xA0, bytes[7]);
Assert.AreEqual(0xC1, bytes[8]);
Assert.AreEqual(0x83, bytes[9]);
Assert.AreEqual(0x01, bytes[10]);
Assert.AreEqual(0x01, bytes[11]);
}
}
示例12: BitWriterWideTest
public void BitWriterWideTest()
{
using (MemoryStream ms = new MemoryStream()) {
BitWriter bw = new BitWriter()
{
Output = ms,
CodeLength = 12,
};
bw.WriteBits(5);
bw.WriteBits(7);
bw.WriteBits(3);
bw.WriteBits(7);
bw.FlushBits();
bw.FlushBytes();
byte[] bytes = ms.ToArray();
Assert.AreEqual(7, bytes.Length);
Assert.AreEqual(0x06, bytes[0]);
Assert.AreEqual(0x05, bytes[1]);
Assert.AreEqual(0x70, bytes[2]);
Assert.AreEqual(0x00, bytes[3]);
Assert.AreEqual(0x03, bytes[4]);
Assert.AreEqual(0x70, bytes[5]);
Assert.AreEqual(0x00, bytes[6]);
}
}
示例13: BitWriterUnalignedTest
public void BitWriterUnalignedTest()
{
using (MemoryStream ms = new MemoryStream()) {
BitWriter bw = new BitWriter()
{
Output = ms,
CodeLength = 5,
};
bw.WriteBits(5);
bw.WriteBits(7);
bw.WriteBits(3);
bw.WriteBits(7);
bw.FlushBits();
bw.FlushBytes();
byte[] bytes = ms.ToArray();
Assert.AreEqual(4, bytes.Length);
Assert.AreEqual(0x03, bytes[0]);
Assert.AreEqual(0xE5, bytes[1]);
Assert.AreEqual(0x8C, bytes[2]);
Assert.AreEqual(0x03, bytes[3]);
}
}
示例14: Serialize
public override void Serialize( Stream stream )
{
base.Serialize( stream );
var bw = new BitWriter();
bw.WriteInt32( Protocol );
bw.WriteInt32(AuthProtocol);
bw.WriteInt32(ServerChallenge);
bw.WriteInt32(ClientChallenge);
bw.WriteString( Name );
bw.WriteString( Password );
bw.WriteByte( ( byte )Players.Count );
foreach ( var player in Players )
{
bw.WriteByte( ( byte )CLC_Messages.clc_SplitPlayerConnect );
using ( var ms = new MemoryStream() )
{
Serializer.SerializeWithLengthPrefix( ms, player, PrefixStyle.Base128 );
bw.WriteBytes( ms.ToArray() );
}
}
// bLowViolence
bw.WriteBits( 0, 1 );
bw.WriteUInt16( ( ushort )Ticket.Length );
bw.WriteBytes( Ticket );
stream.Write( bw.Data, 0, bw.Data.Length - 1 );
}