public OutputMessage Decode(Bitmap inputImage) { Bitmap image = new Bitmap(inputImage); int colorIndex = 0; // last bit position const int lastBitIndex = 7; StringBuilder textMessage = new StringBuilder(); OutputMessage outputMessage = new OutputMessage(); HeaderData headerData = new HeaderData(); // remaining bits to read int remainingBits = (int)LSBHeaderConstants.MessageTypeLength; bool isTypeDecoded = false; bool isHeaderLengthDecoded = false; bool isLengthDecoded = false; bool isImageDimensionsDecoded = false; MessageType messageType = MessageType.Plaintext; for (int i = 0; i < image.Height; i++) { for (int j = 0; j < image.Width; j++) { Color pixel = image.GetPixel(j, i); for (int n = 0; n < 3; n++) { byte currentColor = 0; switch (colorIndex % 3) { case 0: { currentColor = pixel.R; break; } case 1: { currentColor = pixel.G; break; } case 2: { currentColor = pixel.B; break; } } string bitRep = TypeConverter.Instance.ByteToString(currentColor); int lastBitValue = TypeConverter.Instance.CharToInt(bitRep[lastBitIndex]); textMessage.Append(lastBitValue); remainingBits--; colorIndex++; if (remainingBits != 0) { continue; } if (!isTypeDecoded) { messageType = LSBHeaderDecoder.Instance.DecodeMessageType(textMessage.ToString()); headerData.SetMessageType(messageType); isTypeDecoded = true; remainingBits = LSBHeaderDecoder.Instance.GetHeaderLengthWithoutMessageType(messageType); } else if (messageType != MessageType.Plaintext && !isImageDimensionsDecoded) { ImageDimensions imageDimensions = LSBHeaderDecoder.Instance.DecodeImageDimensions(textMessage.ToString(), messageType); remainingBits = LSBHeaderDecoder.Instance.GetHeaderLengthWithoutImageDimensions(messageType); isImageDimensionsDecoded = true; headerData.SetImageHeight(imageDimensions.GetImageHeight()); headerData.SetImageWidth(imageDimensions.GetImageWidth()); } else if (!isHeaderLengthDecoded) { isHeaderLengthDecoded = true; remainingBits = LSBHeaderDecoder.Instance.DecodeMessageLength(textMessage.ToString(), messageType); } /* else if (!isLengthDecoded) * { * isLengthDecoded = true; * remainingBits = LSBHeaderDecoder.Instance.DecodeMessageLength(inputMessage.ToString(), messageType); * }*/ // inputMessage decoded else { outputMessage.SetHeaderData(headerData); outputMessage.SetContent(textMessage.ToString()); return(outputMessage); } } } } return(null); }
private OutputMessage decodeMessageLeft(Bitmap inputImage, int minIndex, int maxIndex) { //temporary int remainingBits = (int)HSHeaderConstants.MessageTypeLength; bool isTypeDecoded = false; bool isHeaderLengthDecoded = false; bool isImageDimensionsDecoded = false; int messageIndex = 0; int R = 0; int G = 0; int B = 0; int mIndex = 0; Color pixel; OutputMessage outputMessage = new OutputMessage(); HeaderData headerData = new HeaderData(); // need to be replaced MessageType messageType = MessageType.Plaintext; StringBuilder message = new StringBuilder(); for (int i = 0; i < inputImage.Height; i++) { for (int j = 0; j < inputImage.Width; j++) { pixel = inputImage.GetPixel(j, i); R = pixel.R; G = pixel.G; B = pixel.B; if (R == maxIndex - 1) { message.Append("1"); messageIndex++; remainingBits--; } else if (R == maxIndex) { message.Append("0"); messageIndex++; remainingBits--; } // header decoding if (remainingBits == 0) { if (isTypeDecoded == false) { messageType = HSHeaderDecoder.Instance.DecodeMessageType(message.ToString()); headerData.SetMessageType(messageType); isTypeDecoded = true; remainingBits = HSHeaderDecoder.Instance.GetHeaderLengthWithoutMessageType(messageType); } else if (messageType != MessageType.Plaintext && !isImageDimensionsDecoded) { ImageDimensions imageDimensions = HSHeaderDecoder.Instance.DecodeImageDimensions(message.ToString(), messageType); remainingBits = HSHeaderDecoder.Instance.GetHeaderLengthWithoutImageDimensions(messageType); isImageDimensionsDecoded = true; headerData.SetImageHeight(imageDimensions.GetImageHeight()); headerData.SetImageWidth(imageDimensions.GetImageWidth()); } else if (!isHeaderLengthDecoded) { isHeaderLengthDecoded = true; remainingBits = HSHeaderDecoder.Instance.DecodeMessageLength(message.ToString(), messageType); } else { outputMessage.SetHeaderData(headerData); outputMessage.SetContent(message.ToString()); return(outputMessage); } } } } return(null); }