예제 #1
0
		public void Encode(Stream stream)
		{
			var first = true;
			int suffix = Nullcode;

			var codeTable = new Dictionary<string, int>();

			var codeSize = (byte) (this.colorsDepth + 1);
			var availableCode = this.endCode + 1;

			var bitEncoder = new BitEncoder(codeSize);

			stream.WriteByte(this.colorsDepth);
			bitEncoder.Add(this.clearCode);

			var releaseCount = 0;
			while (releaseCount < this.imageData.Length)
			{
				if (first)
				{
					suffix = this.imageData[releaseCount++];
					if (releaseCount == this.imageData.Length)
					{
						this.EncodeBits(stream, bitEncoder, suffix);
						break;
					}

					first = false;
					continue;
				}

				var prefix = suffix;
				suffix = this.imageData[releaseCount++];
				var entry = string.Format("{0},{1}", prefix, suffix);

				if (!codeTable.ContainsKey(entry))
				{
					bitEncoder.Add(prefix);
					codeTable.Add(entry, availableCode++);

					if (availableCode > (MaxStackSize - 3))
					{
						codeTable.Clear();
						this.colorsDepth = this.initialColorsDepth;
						codeSize = (byte) (this.colorsDepth + 1);
						availableCode = this.endCode + 1;

						bitEncoder.Add(this.clearCode);
						bitEncoder.InitialBit = codeSize;
					}
					else if (availableCode > (1 << codeSize))
					{
						this.colorsDepth++;
						codeSize = (byte) (this.colorsDepth + 1);
						bitEncoder.InitialBit = codeSize;
					}

					if (bitEncoder.Length >= 255)
					{
						stream.WriteByte(255);
						stream.Write(bitEncoder.ToArray(), 0, 255);
						if (bitEncoder.Length > 255)
						{
							var leftBuffer = new byte[bitEncoder.Length - 255];
							bitEncoder.CopyTo(255, leftBuffer, 0, leftBuffer.Length);
							bitEncoder.Clear();
							bitEncoder.AddRange(leftBuffer);
						}
						else
						{
							bitEncoder.Clear();
						}
					}
				}
				else
				{
					suffix = codeTable[entry];
				}

				if (releaseCount != this.imageData.Length)
				{
					continue;
				}

				this.EncodeBits(stream, bitEncoder, suffix);
				break;
			}
		}
예제 #2
0
		private void EncodeBits(Stream stream, BitEncoder bitEncoder, int suffix)
		{
			bitEncoder.Add(suffix);
			bitEncoder.Add(this.endCode);
			bitEncoder.End();

			if (bitEncoder.Length > 255)
			{
				var leftBuffer = new byte[bitEncoder.Length - 255];
				bitEncoder.CopyTo(255, leftBuffer, 0, leftBuffer.Length);
				bitEncoder.Clear();
				bitEncoder.AddRange(leftBuffer);
				stream.WriteByte((byte) leftBuffer.Length);
				stream.WriteBytes(leftBuffer);
			}
			else
			{
				stream.WriteByte((byte) (bitEncoder.Length));
				stream.WriteBytes(bitEncoder.ToArray());
				bitEncoder.Clear();
			}
		}