-4

I have a program for writing a text file to a png file, but it doesn't work - the image, when I decode it, returns the incorrect characters and sometimes the image does not show up correctly. Here is my code:

public static void readText(String text, int[] pixArr, BufferedImage im, File outFile)
throws FileNotFoundException, IOException{
    char[] txt = text.toCharArray(); //Changes text file to array of characters
    int[] eightBit=new int[8]; //stores binary representation of characters
    for (int i=0;i<txt.length;i++){
        int hey=txt[i];
        for (int a=0;a<8;a++){     //converting text to binary
            eightBit[a]=hey%2;
            hey=hey/2;
            }
        eightBit=reverseArray(eightBit);
        insertion(pixArr, eightBit);
        }
    BufferedImage norm = new BufferedImage(im.getWidth(), im.getHeight(),
                                   BufferedImage.TYPE_INT_ARGB);
    norm.getGraphics().drawImage(im, 0, 0, null);
    ImageIO.write(im, "png", outFile);
    }   

public static void insertion(int[] pixArr, int[]eightBit){
    for (int i=0;i<pixArr.length;i++){
        for (int a=0;a<eightBit.length;a++){
            int temp=pixArr[i];
            temp=temp/2;
            temp*=2;
            pixArr[i++]=eightBit[a]+temp;
            }
        }
    }
Akaraka
  • 173
  • 1
  • 2
  • 13
  • 4
    You want us to debug your home-grown steganography code? Without any errors or suspected lines or anything? Pass. – Chris Eberle Nov 22 '11 at 02:09

2 Answers2

2

I think you need to take a step back from your code and look at where you modify which data structures.

You declare an array eightBit to keep track of the eight bits of one byte. That's neat. (It should be a subroutine so that you can more easily debug it.) But you had the results of the output to insertion(pixArr,eightBit). Follow that insertion() call and you'll see that you give it the same pixArr array every single call -- and have no mechanism to write into different portions of the pixArr array on subsequent calls.

The best this routine can offer is writing eight bits.

Which eight bits? The last eight bits.

But I never see the int pixArr[] ever being passed back to the code that actually writes the png.

I'd strongly suggest breaking this problem apart into much smaller pieces and test each piece individually.

sarnold
  • 102,305
  • 22
  • 181
  • 238
2

This doesn't exactly answer your question, but you will find your code a lot easier to debug if you space it out and use meaningful variable names. It's not Tetris, you don't have to cram everything into as small a space as possible :)

I definitely agree with sarnold - you should decompose your problem into small, distinct, testable subroutines; this will help you identify the parts of your code that work as well as the parts that don't.

Chris Parton
  • 1,052
  • 9
  • 16