Exemplo n.º 1
0
        /// <summary>
        /// Write a given number of bytes from the current stream position
        /// </summary>
        /// <param name="position">Position in the carrier stream</param>
        /// <param name="buffer">Byte array to write to the current stream position</param>
        /// <param name="offset">Zero-based byte offset in buffer at which to begin reading the data</param>
        /// <param name="count">The maximum number of bytes to be read from the buffer</param>
        public override void Write(long position, byte[] buffer, int offset, int count)
        {
            BackingStream.Position = position + HEADER_LENGTH;
            Logger.Debug(h => h("Write: Position = {0}, Offset = {1}, Count = {2}",
                                BackingStream.Position, offset, count));

            BackingStream.Write(buffer, offset, count);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Write a given number of bytes from the current stream position
        /// </summary>
        /// <param name="position">Position in the carrier stream</param>
        /// <param name="buffer">Byte array to write to the current stream position</param>
        /// <param name="offset">Zero-based byte offset in buffer at which to begin reading the data</param>
        /// <param name="count">The maximum number of bytes to be read from the buffer</param>
        /// <remarks>
        /// http://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching
        /// </remarks>
        public override void Write(long position, byte[] buffer, int offset, int count)
        {
            int pixelCount = count * 8 * _fileHeader.BytesPerPixel;

            BackingStream.Position = _fileHeader.BitmapOffset + (position * 8 * _fileHeader.BytesPerPixel);
            byte[] pixels = new byte[pixelCount];
            if (BackingStream.Read(pixels, 0, pixelCount) != pixelCount)
            {
                throw new InvalidOperationException(
                          string.Format("Unable to read enough carrier pixels for writing, {0}bytes", pixelCount));
            }

            int pixelIndex = 0;

            for (int i = 0; i < count; ++i)
            {
                //Reverse the byte, so the value isn't written backwards
                // http://stackoverflow.com/a/2602885
                byte reverse = (byte)((buffer[offset + i] & 0xF0) >> 4 | (buffer[offset + i] & 0x0F) << 4);
                reverse = (byte)((reverse & 0xCC) >> 2 | (reverse & 0x33) << 2);
                reverse = (byte)((reverse & 0xAA) >> 1 | (reverse & 0x55) << 1);



                int writeIndex = 0;
                while (writeIndex < 8)
                {
                    //http://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching
                    pixels[pixelIndex] = (byte)((pixels[pixelIndex] & ~0x01) | (-(reverse >> writeIndex++) & 0x01));
                    pixelIndex        += _fileHeader.BytesPerPixel;
                }
            }

            BackingStream.Position = _fileHeader.BitmapOffset + (position * 8 * _fileHeader.BytesPerPixel);
            BackingStream.Write(pixels, 0, pixelCount);
            BackingStream.Flush();
        }
Exemplo n.º 3
0
 public void Write(long address, byte[] bytes)
 {
     BackingStream.Seek(address, SeekOrigin.Begin);
     BackingStream.Write(bytes, 0, (int)bytes.Length);
 }