IEnumerable <byte> GetFormatedData(BitStream bitStream, UsedBitCount usedBitCount) { int count = 0; switch (usedBitCount) { case UsedBitCount.One: count = 1; break; case UsedBitCount.Two: count = 2; break; } bool isEnd; byte shiftedByte; do { // count * 3 nes piksely yra 3 baitai: RGB, o mes naudojam count kiekį bitų vienam baitui (isEnd, shiftedByte) = ShiftInBytes(bitStream, count * 3); yield return(shiftedByte); }while (!isEnd); }
public string ReadImage(string imageFileName, UsedBitCount usedBitCount) { bool pushTwoBitPerByte = false; switch (usedBitCount) { case UsedBitCount.One: pushTwoBitPerByte = false; break; case UsedBitCount.Two: pushTwoBitPerByte = true; break; } Bitmap bitmap = GetBitmap(imageFileName); BitCollector bitCollector = new BitCollector(); for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { var pixel = bitmap.GetPixel(i, j); // alpha chanelis daro kažkokias nesąmones :D. Reiks išsiaiškint if (pixel.A < ALPHA_CHANNEL_LIMIT) { continue; } if (pushTwoBitPerByte) { bitCollector.PushBit((pixel.R >> 1) & 0b0000_0001); } bitCollector.PushBit(pixel.R & 0b0000_0001); if (pushTwoBitPerByte) { bitCollector.PushBit((pixel.G >> 1) & 0b0000_0001); } bitCollector.PushBit(pixel.G & 0b0000_0001); if (pushTwoBitPerByte) { bitCollector.PushBit((pixel.B >> 1) & 0b0000_0001); } bitCollector.PushBit(pixel.B & 0b0000_0001); } } return(Encoding.ASCII.GetString(bitCollector.GetBuffer())); }
public void EncodeMessage(string message, string imageFileName, UsedBitCount bitCountToUse) { Bitmap bitmap = GetBitmap(imageFileName); bitmap.Save(imageFileName + ".orig"); BitStream msgData = new BitStream(Encoding.ASCII.GetBytes(message)); //jeigu neužtenka baitų paveikslėlyje if (bitmap.Width * bitmap.Height < (msgData.Length / (int)bitCountToUse)) { throw new Exception("image not big enough for message to fit"); //TODO (never :) ) handle dis } // j i ---> // | // * for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { Color pixelColor = bitmap.GetPixel(i, j); // alpha chanelis daro kažkokias nesąmones :D. Reiks išsiaiškint if (pixelColor.A < ALPHA_CHANNEL_LIMIT) { continue; } var(isEnd, newPixel) = InjectDataIntoPixel(pixelColor, msgData, bitCountToUse); bitmap.SetPixel(i, j, newPixel); // jeigu nebeturim daugiau message if (isEnd) { bitmap.Save(imageFileName + ".steg"); return; } } } }
// byte data naudoja tik (usedBitCount) kiekį duomenų public (bool end, Color pixel) InjectDataIntoPixel(Color pixelToSet, BitStream data, UsedBitCount usedBitCount) { byte shiftInCount = (byte)usedBitCount; var(end, temp) = ShiftInBytes(data, shiftInCount); int newR = ZeroOutBottomBits(pixelToSet.R, shiftInCount) | temp; (end, temp) = ShiftInBytes(data, shiftInCount); int newG = ZeroOutBottomBits(pixelToSet.G, shiftInCount) | temp; (end, temp) = ShiftInBytes(data, shiftInCount); int newB = ZeroOutBottomBits(pixelToSet.B, shiftInCount) | temp; return(end, Color.FromArgb(pixelToSet.A, newR, newG, newB)); }