/// <summary> /// Extracts the content from the image /// </summary> /// <param name="img">the image from which to extract</param> /// <param name="planes">the planes to embed in</param> /// <param name="bitDepth">the bit depth of the embedding</param> /// <returns>the embedded content, null if a problem occurs</returns> public static EmbeddingContent Extract(Bitmap img, EmbeddingPlane planes, int bitDepth) { // short circuit if ((img == null) || (bitDepth < 1) || (bitDepth > 8)) { return(null); } // var declatation EmbeddingContent retVal = null; try { // get the header to determine what to extract EmbeddingHeader eh = new EmbeddingHeader(img, planes, bitDepth); // read the bytes int totalBytesToExtract = eh.ToString().Length + eh.SizeOfContent; retVal = new EmbeddingContent(SteganographyMethods.ExtractBytes(img, planes, bitDepth, totalBytesToExtract)); } catch { retVal = null; } return(retVal); }
/// <summary> /// Constructor /// </summary> /// <param name="content">the file to embed</param> public EmbeddingContent(FileInfo content) { // short circuit if ((content == null) || (content.Exists == false)) { throw new SteganographyException("Incorrect parameters for EmbeddingContent"); } this.Header = new EmbeddingHeader(ContentType.File, Convert.ToInt32(content.Length), content.Name); this.HeaderAndContent = EmbeddingContent.MergeHeaderAndContent(this.Header, File.ReadAllBytes(content.FullName)); }
/// <summary> /// Constructor /// </summary> /// <param name="typeOfContent">the type of content</param> /// <param name="filename">the filename (if the content is file)</param> /// <param name="content">byte array of content</param> public EmbeddingContent(ContentType typeOfContent, String filename, byte[] content) { // short circuit if ((content == null) || (content.Length < 1)) { throw new SteganographyException("Incorrect parameters for EmbeddingContent"); } this.Header = new EmbeddingHeader(typeOfContent, content.Length, filename); this.HeaderAndContent = EmbeddingContent.MergeHeaderAndContent(this.Header, content); }
/// <summary> /// Constructor /// </summary> /// <param name="content">the text content</param> public EmbeddingContent(String content) { // short circuit content = content.Trim(); if (content.Length < 1) { throw new SteganographyException("Incorrect parameters for EmbeddingContent"); } this.Header = new EmbeddingHeader(ContentType.Text, content.Length, String.Empty); this.HeaderAndContent = EmbeddingContent.MergeHeaderAndContent(this.Header, Encoding.ASCII.GetBytes(content)); }
/// Extracts the content from the image /// </summary> /// <param name="imageFile">the image file from which to extract</param> /// <param name="planes">the planes to embed in</param> /// <param name="bitDepth">the bit depth of the embedding</param> /// <returns>the embedded content, null if a problem occurs</returns> public static EmbeddingContent Extract(String imageFile, EmbeddingPlane planes, int bitDepth) { // var init EmbeddingContent retVal = null; // wrap call try { using (Bitmap img = new Bitmap(imageFile)) { retVal = SteganographyMethods.Extract(img, planes, bitDepth); } } catch { retVal = null; } return(retVal);; }
/// <summary> /// Embeds content in the image /// </summary> /// <param name="imageFile">the image file in which to embed</param> /// <param name="planes">the planes to embed in</param> /// <param name="bitDepth">the bit depth of the embedding</param> /// <param name="content">the content to embed</param> /// <returns>the image with the data embedded if successful, otherwise null</returns> public static Bitmap Embed(String imageFile, EmbeddingPlane planes, int bitDepth, EmbeddingContent content) { // var init Bitmap retVal = null; // wrap call try { retVal = new Bitmap(imageFile); if (SteganographyMethods.Embed(retVal, planes, bitDepth, content) == false) { throw new SteganographyException(String.Empty); } } catch { if (retVal != null) { retVal.Dispose(); } retVal = null; } return(retVal); }
/// <summary> /// Embeds content in the image /// </summary> /// <param name="img">the image in which to embed</param> /// <param name="planes">the planes to embed in</param> /// <param name="bitDepth">the bit depth of the embedding</param> /// <param name="content">the content to embed</param> /// <returns>true if successful</returns> public static Boolean Embed(Bitmap img, EmbeddingPlane planes, int bitDepth, EmbeddingContent content) { // short circuit if ((img == null) || (bitDepth < 1) || (bitDepth > 8) || (content == null) || (content.HeaderAndContent.Length > SteganographyMethods.MaxBytesInImage(img, planes, bitDepth))) { return(false); } // var init Boolean retVal = true; int currentByteIndex = 0; byte currentBitIndex = 1; try { // loop through the x and y pixels of the image, as well as the correct bit depths for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { for (byte embedBit = 1; embedBit <= bitDepth; embedBit++) { // embed in R if ((planes == EmbeddingPlane.R) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte r = SteganographyMethods.SetBitValueInByte(rgb.R, embedBit, SteganographyMethods.GetBitValueFromByte(content.HeaderAndContent[currentByteIndex], currentBitIndex)); img.SetPixel(x, y, Color.FromArgb(r, rgb.G, rgb.B)); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= content.HeaderAndContent.Length) { x = img.Width; y = img.Height; break; } } // embed in G if ((planes == EmbeddingPlane.G) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte g = SteganographyMethods.SetBitValueInByte(rgb.G, embedBit, SteganographyMethods.GetBitValueFromByte(content.HeaderAndContent[currentByteIndex], currentBitIndex)); img.SetPixel(x, y, Color.FromArgb(rgb.R, g, rgb.B)); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= content.HeaderAndContent.Length) { x = img.Width; y = img.Height; break; } } // embed in B if ((planes == EmbeddingPlane.B) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte b = SteganographyMethods.SetBitValueInByte(rgb.B, embedBit, SteganographyMethods.GetBitValueFromByte(content.HeaderAndContent[currentByteIndex], currentBitIndex)); img.SetPixel(x, y, Color.FromArgb(rgb.R, rgb.G, b)); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= content.HeaderAndContent.Length) { x = img.Width; y = img.Height; break; } } } } } } catch { retVal = false; } return(retVal); }