Extra data within image (PPM/PAM/PNM) - image

Is it possible to store extra data in pixels of a binary PNM file in such a way that it can still be read as an image (hopefully by any decoder, but specifically by ffmpeg)?
I have a simulation that saves its data as PPM currently and I'd like a way to record more than three values per pixel in the file, and yet still be able to use it as an image (obviously only the first three/four values will actually affect the image).
In particle I think the TUPLTYPE of PAM should allow me to do this, but I don't know how make something that's also a readable image from that.

There are two tricks which together can get up to 5 additional bytes per pixel in PAM file.
First trick:
You can try store additional byte of information in alpha channel and then choose to ignore that information in decoder. Enabling alpha channel in PAM is done by adding _APLHA to TUPLTYPE argument, so instead TUPLTYPE RGB you have TUPLTYPE RGB_ALPHA.
Second trick:
You can set MAXVAL in PAM (or equivalent field in PPM and others) to 65535 instead of 255, which means that every pixel will be described by three 16-bit values instead of three 8-bit ones. Now, for these 16-bit values the 8 least significant bits can be used to store information as they do not affect visual properties of image when shown on typical computer screen.
First + second trick:
This gives you additional 3 x 8 = 24 bits for RGB planes and 16 bits in alpha channel. Which means: 5 bytes.

I've not used PNM file format, but I've done this trick with a .bmp file.
Hijack the least significant bit of the image data and stuff it with whatever binary data you want. Nobody will see the difference between a pixel value of a 0 or 1 (00000000 or 00000001), or the the difference between a 254 or 255 (1111110 or 11111111). For every 8 bytes of image data a byte of extra data can be embedded (6 bytes if you use a limited character set). The file viewing software won't know any difference. Any software which could open the file before the encoding, would be able to read it after.
If you want the data to be more covert/hidden, the bits can be stuffed into the image data with a shuffle routine, where the first bit might be location 50, the second in 123, the third in 32... and after locations 0-255 (first 256 bytes if image data) are used (first 32 bytes of extra data), start the shuffle again.

Related

how to read correct pixels from windows clipboard copy image?

i'm using this:
GetClipboardData(CF_DIB);
i view it as a BITMAPINFOHEADER and take into account
biWidth
biHeight
biBitCount 24 vs 32
if biCompression is BI_BITFIELDS then i skip over the first 3*4 bytes of the pixel data.
most of the time when i copy an image i can get the pixel data from it just fine. but when copying from certain applications like Telegram, sometimes the colors are messed up, sometimes the image is skewed, all sorts of crazy artifacts happen when trying to view the clipboard image. i don't see anything special in the BITMAPINFOHEADER of these images i'm rendering incorrectly. what could i possibly be forgetting to handle?
A little-known fact about Microsoft bitmaps is that every row is padded to an even multiple of 4 bytes. It is natural to assume that one row follows the next immediately, but there may be 1 to 3 bytes of padding on each row that will throw your alignment off. You will see this as colors that aren't what they should be, or a skewed image as each row is shifted from its proper position.
If your pixels are all 4 bytes (RGBA) or your image width is a multiple of 4 (more common than random chance would suggest) each row will already be a multiple of 4 bytes, and you won't notice a problem.

Converted byte array to image and back to byte array, the values changed

I tried to implement steganography with the following steps :
1. Converted image to buffered image
2. Converted buffered image to Bytes array
3. Made modifications in the byte array
4. Converted byte array back to buffered image
5. Saved it as a jpg file
The problem arose when i read the saved file again, converted it to byte array and found that byte array is different from what i obtained after Step 3. (although there were not much difference as 6 converted to 7, 9 to 8 and so on)
I really have no idea why did this happen.
If you save as a JPEG, the RGB data gets converted to YCbCr. Those two color spaces have different gamuts so values get clamped.
JPEG data may be subsampled, causing data to be changed. You can avoid these changes by not subsampling.
The JPEG DCT may introduce small errors (limited to +/-1 if implemented correctly)
Quantization will make rather large changes to the data. You can avoid changes at this step by having all 1s in your quantization tables.
No matter what you do, #1 and #3 can introduce changes in the JPEG compression process.
JPG is a lossy image format, so you can't expect it to hold the data exactly after it is saved. It is especially unsuited for steganography, as it will destroy the small details required for this use, even when using the highest quality setting.
Solution is to use a lossless format, like PNG.
A BufferedImage may be already a byte array. If when you create you BufferedImage you use the encoding TYPE_BYTE_GRAY, 3BYTE_BGR or 4BYTE_ABGR, then your BufferedImage is already a byte array. To access the byte array, you do: byte[] buffer = ((DataBufferByte)my image.getRaster().getDataBuffer()).getData() ;
And when you write an image as a JPEG, you compress with loss your image. So the information you save is altered and cannot be retrieved as before. You should use PNG/TIFF/BMP, PNG being the most common.

Bitmap storage / memory representation for N-bit RGB bitmaps

Typically, the most common RGB format seems to be 24-bit RGB (8-bits for each channel). However, historically, RGB has been represented in many other formats, including 3-bit RGB (1-bit per channel), 6-bit RGB (2-bits per channel), 9-bit RGB (3-bits per channel), etc.
When an N-bit RGB file has a value of N that is not a multiple of 8, how are these bitmaps typically represented in memory? For example, if we have 6-bit RGB, it means each pixel is 6 bits, and thus each pixel is not directly addressable by a modern computer without using bitwise operations.
So, is it common practice to simply covert N-bit RGB files into bitmaps where each pixel is of an addressable size (e.g. convert 6-bit to 8-bit)? Or is it more common to simply use bitwise operations to manipulate bitmaps where the pixel size is not addressable?
And what about disk storage? How is, say, a 6-bit RGB file stored on disk, when the last byte of the bitmap may not even contain a full pixel?
Images are often heavy and bandwidth is critical when transferring them. So a 6 bit per channel image is a reasonable choice if some loss in chrominance is acceptable (usually unnoticeable with textures and photos)
How is, say, a 6-bit RGB file stored on disk, when the last byte of
the bitmap may not even contain a full pixel?
If the smallest unit of storage is a Byte then yes you need to add some padding to be 8 bit aligned. This is fine because the space saving compared to an 8 bit per channel image can be considerable.
A power of 2 value that is divisible by 6 is very large. Better numbers are 5 bits for the red and blue channels and 6 bits for the green channel and the total is 16 bits per pixel. R5G6B5 is a very common pixel format.
Apologies for the archeological dig, but I just couldn't resist, as there's no good answer imho.
In the old days memory was the most expensive part of a computer. These days memory is dirt-cheap, so the most sensible way to handle N-bit channel images in modern hardware is to blow up every channel to the number of bits that fits your API or hardware (usually 8 bits).
For files, you could do the same, as disk space is also cheap these days. (and you can use one of the many compressed file formats out there.)
That said, the way it worked in the old days when these formats were common, is this:
The total number of bits per pixel was not a multiple of 8, but the number of pixels per scan line always was a multiple of 8. In this case you can store your scan lines "a bit at a time" and not waste any memory space when storing it in bytes.
So if your pixels were 9 bits per pixel, and a scan line was 320 pixels, you would have 320/8 = 40 bytes containing bit #0 of each pixel, followed by 40 bytes containing all bit #1's etc. up to and including bit #8. Hence all pixel info for your scan line would be exactly 360 bytes.
The video chips would have a different hardware wiring to the memory, so rendering such scan lines was fast. In fact, this is the easiest way to implement a variable amount of bits/pixel hardware support: by pulling bits from N adresses at once.
Note that this method does not change the amount of 'bitshifting' required to find the bits for pixel number X in a scanline, based on the total number of bits you use. You just read less addresses ahead at once.

Compressing/packing "don't care" bits into 3 states

At the moment I am working on an on screen display project with black, white and transparent pixels. (This is an open source project: http://code.google.com/p/super-osd; that shows the 256x192 pixel set/clear OSD in development but I'm migrating to a white/black/clear OSD.)
Since each pixel is black, white or transparent I can use a simple 2 bit/4 state encoding where I store the black/white selection and the transparent selection. So I would have a truth table like this (x = don't care):
B/W T
x 0 pixel is transparent
0 1 pixel is black
1 1 pixel is white
However as can be clearly seen this wastes one bit when the pixel is transparent. I'm designing for a memory constrained microcontroller, so whenever I can save memory it is good.
So I'm trying to think of a way to pack these 3 states into some larger unit (say, a byte.) I am open to using lookup tables to decode and encode the data, so a complex algorithm can be used, but it cannot depend on the states of the pixels before or after the current unit/byte (this rules out any proper data compression algorithm) and the size must be consistent; that is, a scene with all transparent pixels must be the same as a scene with random noise. I was imagining something on the level of densely packed decimal which packs 3 x 4-bit (0-9) BCD numbers in only 10 bits with something like 24 states remaining out of the 1024, which is great. So does anyone have any ideas?
Any suggestions? Thanks!
In a byte (256 possible values) you can store 5 of your three-bit values. One way to look at it: three to the fifth power is 243, slightly less than 256. The fact that it's slightly less also shows that you're not wasting much of a fraction of a bit (hardly any, either).
For encoding five of your 3-bit "digits" into a byte, think of taking a number in base 3 made from your five "digits" in succession -- the resulting value is guaranteed to be less than 243 and therefore directly storable in a byte. Similarly, for decoding, do the base-3 conversion of a byte's value.

How can you hide information inside a jpg or gif photo?

How can I write some information inside a photo file like jpg or gif without destroying the image? and of course without showing it on the photo since the whole idea is to send information in the file of photo undetected by anyone (to provide security/privacy to some extent)!
You can concatenate a gif and a zip (the information you want to hide) into one file. Gifs are read from the start of the file, while zips are read from the end of the file.
To create such a file in linux:
$ cat file1.gif >> outfile.gif
$ cat file2.zip >> outfile.gif
The resulting file should have the size of file1.gif and file2.zip together and should be openable by any gif viewer and zip file handler.
I'm sure there are many ways. Here's one:
In a photograph, minor variations in color would often be unnoticable to the naked eye, or even if noticed, might easily be mistaken for flaws in the quality of the picture.
So to take a simple example, suppose you had a gray-scale GIF image where the pallette is arranged in order from white to black with a smooth range of grays in between. I'm not sure how much you know about graphic file formats, but in GIF you have one byte per pixel, with each possible byte value mapping to some specific color. So in this case we could say pallette #0=RGB(0,0,0), pallette #1=RGB(1,1,1), ... palette #255=RGB(255,255,255).
Then you take an ordinary, real photograph. Break your secret message into individual bits. Set the last bit of each pallette index number to successive bits of your message.
For example, suppose the first eight pixels of the original photo are, say, 01 00 C9 FF FF C8 42 43. Your message begins with the letter "C", ascii code 0110 0111. So you change the last bit of the first byte to 0, changing the byte from 01 to 00. You change the last bit of the second byte to 1, changing the byte from 00 to 01. You change the last bit of the third byte to 1. It's already 1, so that makes no difference. Etc. You end up with the coded 8 bytes being 00 01 C9 FE FF C9 43 43.
The changes to the colors would be so subtle that it's unlikely that anyone looking at the picture would notice. Even if they did notice, unless they had a reason to be suspicious, they would likely just conclude that the picture was of less-than-perfect quality.
Of course nothing says you have to use 1 bit per byte for the secret message. Depending on how much degradation in quality you think you can get away with, you could use 2 bits per byte, or just change 1 bit in every other byte, etc.
Of course the same technique can be used with color photos: change the last bit in each of the RGB components to encode 3 bits per pixel, etc.
Hey that method is called as Steganography. With that we can hide messages in not just images but also in audio,vedeo and other formats.
Here is an opensouce Steganography software called steganotool This project is an open source steganography tool that can be used to hide and extract text to/ from Bitmap images.
About Steganography Mediums
Steganography in images
This type of steganography is very effective against discovery and can serve a variety of purposes. These purposes can include authentication, concealing of messages, and transmission of encryption keys. The most effective method for this type of steganography is normally the least significant bit method. This simply means that the hidden message will alter the last bit of a byte in a picture. By altering that last bit, there will be relatively no change to the color of that pixel within the carrier image. This keeps the message from being easily detected. The best type of image file to hide information inside of is a 24 bit Bitmap. This is due the large file size and high quality.
Steganography in Audio
In audio files, the most prominent method for concealing information is the low bit encoding method. The low bit encoding method is somewhat similar to the least significant bit method used in image files. The secret information is attached to the end of the file. One of the issues with low bit encoding is that it can be noticeable to the human ear. If someone is trying to hide information, this could be risky, since it is so easily detectable. The spread spectrum method is another method that has been used in the concealment of information in audio files. What this method does, is it adds random noise to the audio broadcast. This method enables for the information to be spread accross the frequency spectrum and remain hiddden under the random noise. The last method seen in audio steganography is echo hiding data. This method seeks to hide information by using the echos that occur naturally within sound files. Then, extra sound can be added to these echos, extra sound being the concealed message. This is a sufficient way to hide information, expecially since it even improves the sound of the original audio file in some cases.
Steganography In Video
Steganography in Videos is basically hiding of information in each frame of video. Only a small amount of information is hidden inside of video it generally isn’t noticeable at all, however the more information that is hidden the more noticeable it will become. This method is effective as well, but must be done right or else reveal more information instead of hiding.
Steganography In Documents
This is basically adding white space and tabs to the ends of the lines of a document. This type of
Steganography is extremely effective, because the use white space and tabs is not visible to the human eye in most text/document editors.
You can also refer to this open source project
This article can be very useful.
You can store some information in image metadata. In fact that's how man digital cameras 'tag' the photos their making (camera model, date and time, GPS coords etc.).
This data format is called EXIF (Exchangeable Image File Format). There are a lot of examples how to use it in programming languages. Here's the example in Java.
If you want to prevent users from reading this data you can encrypt them somehow, but they will always be able to remove it from your picture (by opening in Photoshop and using Save As for example).
If you want to hide data (text, another image, whatever) in a jpeg file, you can simply append it to the end of the file. When the image is viewed, you'll only see the original image and your added data will be ignored.
While not a super-duper way of hiding data, this is a good way of hiding another jpeg, as if anyone opens the file in notepad or a hex editor, they will probably not notice that there are two jpegs and not one because the end of the second image will just look like the first anyway.
In windows you can use simple command to hide archive in image : copy /b cat.jpg + Documents.rar cat_new.jpg.
And then use for example winrar to extract data ftom image as from archive.
But better way is to use Steganography.
simple program for it for linux and windows : http://linux01.gwdg.de/~alatham/stego.html
using this program you will use pass phrase, without it nobody even will know, that you hided some data in picture ))
compressed data in gif files is in variable sized chunks. each chunk starts with a length byte. Usually these chunks are 255 bytes of data (and the length byte says 255) except for the last two chunks (the last one is 0)
But you could re-code the gif with chunks whose size spells out the message
eg using characters for ASCII text or bytes 1 2 3 4 representing 00 01 10 11 binary data,
byte-frequency analysis on the gif would reveal fewer than expected 255 bytes.
and the size would be larger than the original, but the image would look exactly the same.
You don't need any type of advanced tool to hide a file in an image –– you just you need to know some basic DOS commands… just follow the link below, to see how to do this (it’s a short description of image steganography):
https://www.youtube.com/watch?v=ox9ArqXtaWw&list=PLqEKOAKK4IbSTfDJZE_lH-DiOjOzZUczx&index=2

Resources