Пример #1
0
        private static Color SetColorValue(Color pixelColor, BitmapCodeDataConfig config, PixelRGBValue selectedPixel, int savedBitsInByte, int readedData, ref int savedBits)
        {
            int r = pixelColor.R;
            int g = pixelColor.G;
            int b = pixelColor.B;

            switch (selectedPixel)
            {
            case PixelRGBValue.R:
                r = CalculateNewColorPropertyValue(pixelColor.R, config.R, readedData, savedBitsInByte, ref savedBits);
                break;

            case PixelRGBValue.G:
                g = CalculateNewColorPropertyValue(pixelColor.G, config.G, readedData, savedBitsInByte, ref savedBits);
                break;

            case PixelRGBValue.B:
                b = CalculateNewColorPropertyValue(pixelColor.B, config.B, readedData, savedBitsInByte, ref savedBits);
                break;

            default:
                throw new ArgumentException("Wrong PixelRGBValue value", "selectedPixel");
            }
            return(Color.FromArgb(r, g, b));
        }
Пример #2
0
 private static Color CodeBitDataToPixelColor(Color pixelColor, BitmapCodeDataConfig config, int savedBitsInByte, int readedData, ref PixelRGBValue selectedPixel, ref int savedBits, ref bool shouldIncreaseRowAndColumn)
 {
     pixelColor = SetColorValue(pixelColor, config, selectedPixel, savedBitsInByte, readedData, ref savedBits);
     if (config.GetBitsCount(selectedPixel) == savedBits)
     {
         selectedPixel = IncreasePixelRGBValue(selectedPixel, ref shouldIncreaseRowAndColumn);
         savedBits     = 0;
     }
     return(pixelColor);
 }
 private byte EncodeBitDataFromPixelColor(Color pixelColor, BitmapCodeDataConfig config, byte result, ref PixelRGBValue selectedPixel, ref int readedBitsInPixelSegment, ref bool shouldIncreaseRowAndColumn)
 {
     result = AddBitToResultFromColor(pixelColor, config, selectedPixel, result, ref readedBitsInPixelSegment);
     if (config.GetBitsCount(selectedPixel) == readedBitsInPixelSegment)
     {
         selectedPixel            = IncreasePixelRGBValue(selectedPixel, ref shouldIncreaseRowAndColumn);
         readedBitsInPixelSegment = 0;
     }
     return(result);
 }
Пример #4
0
        public Bitmap CodeDataToImage(Bitmap bitmap, Stream data, BitmapCodeDataConfig config)
        {
            var           result = (Bitmap)bitmap.Clone();
            int           readedData;
            int           column = 0, row = 0;
            PixelRGBValue selectedPixel = PixelRGBValue.R;
            int           savedBits     = 0;

            while ((readedData = data.ReadByte()) != -1)
            {
                result = WriteDataToBitmap(result, readedData, config, ref column, ref row, ref selectedPixel, ref savedBits);
            }
            result = WriteDataToBitmap(result, 0, config, ref column, ref row, ref selectedPixel, ref savedBits);
            data.Dispose();
            return(result);
        }
Пример #5
0
        private static Bitmap WriteDataToBitmap(Bitmap result, int readedData, BitmapCodeDataConfig config, ref int column, ref int row, ref PixelRGBValue selectedPixel, ref int savedBits)
        {
            for (int savedBitsInByte = 0; savedBitsInByte < 8; savedBitsInByte++)
            {
                bool shouldIncreaseRowAndColumn = false;
                var  pixelColor    = result.GetPixel(row, column);
                var  newPixelColor = CodeBitDataToPixelColor(pixelColor, config, savedBitsInByte, readedData, ref selectedPixel, ref savedBits, ref shouldIncreaseRowAndColumn);
                result.SetPixel(row, column, newPixelColor);

                if (shouldIncreaseRowAndColumn)
                {
                    IncreaseRowAndColumn(result, ref row, ref column);
                }
            }
            return(result);
        }
        private byte EncodeByteFromBitmap(Bitmap bitmap, BitmapCodeDataConfig config, ref int row, ref int column, ref PixelRGBValue selectedPixel, ref int readedBitsInPixelSegment)
        {
            byte result = 0;

            for (int encodedBitsInByte = 0; encodedBitsInByte < 8 && RowAndColumnIsValid(bitmap, row, column); encodedBitsInByte++)
            {
                bool shouldIncreaseRowAndColumn = false;
                var  pixelColor = bitmap.GetPixel(row, column);
                result = EncodeBitDataFromPixelColor(pixelColor, config, result, ref selectedPixel, ref readedBitsInPixelSegment, ref shouldIncreaseRowAndColumn);
                if (shouldIncreaseRowAndColumn)
                {
                    IncreaseRowAndColumn(bitmap, ref row, ref column);
                }
            }
            return(result);
        }
        public Stream EncodeDataFromImage(Bitmap bitmap, BitmapCodeDataConfig config)
        {
            var           streamWriter = new MemoryStream();
            int           row = 0, column = 0;
            PixelRGBValue selectedPixel            = PixelRGBValue.R;
            int           readedBitsInPixelSegment = 0;
            byte          encodedByte              = 0;

            do
            {
                encodedByte = EncodeByteFromBitmap(bitmap, config, ref row, ref column, ref selectedPixel, ref readedBitsInPixelSegment);
                if (encodedByte != 0)
                {
                    streamWriter.WriteByte(encodedByte);
                }
            } while (encodedByte != 0 && RowAndColumnIsValid(bitmap, row, column));
            streamWriter.Position = 0;
            return(streamWriter);
        }
        private byte AddBitToResultFromColor(Color pixelColor, BitmapCodeDataConfig config, PixelRGBValue selectedPixel, byte result, ref int readedBitsInPixelSegment)
        {
            switch (selectedPixel)
            {
            case PixelRGBValue.R:
                result = AddBitToResult(pixelColor.R, config.R, result, ref readedBitsInPixelSegment);
                break;

            case PixelRGBValue.G:
                result = AddBitToResult(pixelColor.G, config.G, result, ref readedBitsInPixelSegment);
                break;

            case PixelRGBValue.B:
                result = AddBitToResult(pixelColor.B, config.B, result, ref readedBitsInPixelSegment);
                break;

            default:
                throw new ArgumentException("Wrong PixelRGBValue value", "selectedPixel");
            }
            return(result);
        }