Exemplo n.º 1
0
        /// <summary>
        /// Encodes up to eight bitmap values into a byte. The values are stored from the most significant bit downward,
        /// i.e. the leftmost value is stored in the most significant bit.
        /// </summary>
        /// <returns>The bitmap pixels encoded into a byte.</returns>
        /// <param name="image">The image being encoded.</param>
        /// <param name="pixels">The bitmap pixels to encode into a byte.</param>
        /// <typeparam name="TPixelComponent">The type of the pixel component.</typeparam>
        private byte EncodeBitmapValuesIntoByte <TPixelComponent>(NetpbmImage <TPixelComponent> image, IList <TPixelComponent> pixels)
        {
            Contract.Assert(pixels.Count > 0 && pixels.Count < 9);

            byte theByte = 0;

            // earliest value = most significant bit
            for (int i = 0; i < 8; ++i)
            {
                if (!image.IsComponentValueZero(pixels[i]))
                {
                    theByte |= (byte)(1 << (7 - i));
                }
            }

            return(theByte);
        }