2

I am currently trying to write a program to encode text into a png file only changing the least significant bit for each letter i want to encode in the picture, example I have a 'A' which is 65 and I use 8 different bytes to encode the letter A. So 01010100<- 10101101<- 11011010<- 10101010<- each of these I change the last bit and the put 10110110<- 01010100<- 01010100<- 01010101<- them together so 65 is 01000001 each number by the arrow is changed according to the 65.

If I should approach this a different way suggestions would be awesome :). This is just a fun little project I wanted to do. But anyways back to my question.

When I read in a image that is only 4 pixels big I get like 680 bytes which is crazy, or at least I think it is, maybe im wrong? 4 pixels with ARGB at 8 bits each should be 16 bytes with a few bytes im sure to tell the operating system that it is a png and how to handle it. So i was expecting maybe like 30 bytes. Maybe less. Am I looking at this the wrong way? When png images are compressed do they become bigger if it is a small picture? And also, when I was saving it back to the Hard drive I always got a larger file. The original picture was 8,554 kb and then it turned into like 16kb when I saved it back. Here is the code for getting the image bytes and for saving the image. Maybe I am doing something wrong or I am just not understanding it correctly.

These are the ways I get the image (I tried 2 different things)

// BufferedImage img = ImageIO.read(new File("image.png"));
BufferedImage img= robot.createScreenCapture(new Rectangle(1,2,2,2));

how I saved two different ways again.

try {
InputStream in = new ByteArrayInputStream(imgBytes);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "png", new File( "image.png"));

//FileOutputStream fos = new FileOutputStream("image.png");
//fos.write(b);
//fos.close();
}catch(Exception e){}

How I got the bytes from the Image, again I tried two different ways, the second way that is commented out actually did give me the 16 bytes like I want but when I saved it the Windows couldnt Open it because it didnt know what it was i guess? Not sure, just said file not supported.

byte[] imageBytes = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos );
baos.flush();
imageBytes = baos.toByteArray();
baos.close();
}catch(IOException e){System.out.println(e.getMessage());}

// imageBytes = ((DataBufferByte) image.getData().getDataBuffer()).getData();
return imageBytes;

Thanks!

2 Answers 2

1

A png consists of a lot of image meta data as well as the raw image data. That is what is giving you crazy 680 bytes.

4
  • So should I use a different format? Or is that acceptable to change those bytes? I dont know what I should do xD Nov 29, 2012 at 6:23
  • All images have a certain amount of meta data, otherwise the renderer will not know how to render it. A png also chunks it's image data up and each chunk has some size overhead. w3.org/TR/2003/REC-PNG-20031110. I am a little confused about what you want to do, if you want to produce a renderable image with some modification you need to alter the image prior to encoding. ie before the ImageIO.write call otherwise all you will have is a corrupted file.
    – BevynQ
    Nov 29, 2012 at 20:38
  • for small images a png or any other compressible image will be larger than the raw image because of the header overhead. An uncompressible image will always be larger than the raw image data. PNG is good if you want an image that is lossless otherwise jpg is just as good.
    – BevynQ
    Nov 29, 2012 at 20:42
  • I need the image to be the same I cant have it lose data because I need to take the message back out afterwards. I will modify the image after I get the bytes from the original image, then I will change those bytes and write the image again to a different name therefore the image will look the same but be altered slightly so I can receive the image afterwards, which is steganography. So the question now is, which bytes am I able to alter without messing up the file? Which bytes are the bytes that are the ARGB? Thanks! Dec 1, 2012 at 0:01
0

I had pretty much the same problem with my barcode fonts. I just wanted to encode 5 bits of data into the smallest PNG I could get. What I didn't want to do is write a custom program, based on libpng. I tried quite a few editors and the smallest file size I could get was around 170 bytes.

Finally I found Gimp 2.0. There is a feature you can use to export a PNG file without all the metadata. I also changed to 8 bit grayscale. I think I could shave a couple of bytes off by switching to 2 bit grayscale, but Gimp wouldn't do that for me. In the end, I was happy with ~75 bytes per character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.