2

I have written a steganography algorithm, but it takes a long time to complete. This is because I create a new instance of bitmap, BitmapStegan, and I take each pixel from my old bitmap, bitmap. Whether I modify it or not, I have to set it in the new bitmap object. Therefore, I end up looping through all of the pixels, even though I only need to edit a few of them.

How can I address that problem?

Bitmap BitmapStegan = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
for(int i=0; i<bitmap.getWidth(); i++){
    for(int j=0; j<bitmap.getHeight(); j++){
        int pixel=bitmap.getPixel(i, j);
        int red= Color.red(pixel);
        int green=Color.green(pixel);
        int blue=Color.blue(pixel);

        if (NumberBitsInMessage>0) {
            /*
            I put here my bit to red and greed and blue with LSB method
            */
        }
        BitmapStegan.setPixel(i, j, Color.argb(Color.alpha(pixel), red, green, blue));
    }
}
imageView.setImageBitmap(BitmapStegan);
  • You can get or set all pixels at one call, just use Bitmap.getPixels/setPixels method. It will be faster. – Volodymyr Baydalka Sep 11 '17 at 13:20
  • Since you don't show us your embedding logic, I have made the reasonable assumption that `NumberBitsInMessage` is the number of bits you have to embed and with each bit, you decrease it by 1. So, as long as it's positive, you have more bits to embed. If my assumption is incorrect, please clarify. – Reti43 Sep 11 '17 at 14:21
  • @Reti43 your assumption is correct . my Problem is when **NumberBitsInMessage ** is less than 0 I must continue to copy auther pixel to BitmapStegan – Amirouche Zeggagh Sep 11 '17 at 14:37

1 Answers1

3

First things first, do you really need a copy of your original image? If yes, because you want to compare statistical differences between the original and the stego image, you want to create a copy of your bitmap. This way, you create all the pixels in one go, which is faster. If you don't need a copy, just apply your changes directly to the original image object. Either way, you need to modify only one image, which from now on I will call image.

Now, you have two choices about how to iterate through only enough pixels for embedding. Either use loops for the rows and columns of your image and break out of them after you have embedded the whole secret, or create a counter for NumberBitsInMessage and explicitly change the pixel coordinates as you embed your bits.

1. Breaking out of the loops

embedding:
for (int i = 0; i < image.getWidth(); i++) {
    for (int j = 0; j < image.getHeight(); j++) {
        if (NumberBitsInMessage == 0) {
            break embedding;
        }

        int pixel = image.getPixel(i, j);
        int red = Color.red(pixel);
        int green = Color.green(pixel);
        int blue = Color.blue(pixel);

        /*
        modify pixel logic here
        */
        image.setPixel(i, j, Color.argb(Color.alpha(pixel), red, green, blue));
    }
}

2. Embedding bits counter

int width = 0;
int height = 0;
int maxHeight = image.getHeight();

for (int embeddedBits = 0; embeddedBits < NumberBitsInMessage; ) {
    int pixel = image.getPixel(width, height);
    int red = Color.red(pixel);
    int green = Color.green(pixel);
    int blue = Color.blue(pixel);

    /*
    modify pixel logic here
    don't forget to increase `embeddedBits` for each colour you modify
    */
    image.setPixel(width, height, Color.argb(Color.alpha(pixel), red, green, blue));

    height++;
    if (height == maxHeight) {
        width++;
        height = 0;
    }
}
Reti43
  • 9,656
  • 3
  • 28
  • 44
  • I apply my changes directly to the original image object but the the app cracked I think it must to do this line of code 'Bitmap BitmapStegan = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());' than i can't break out whene finished emboded my message secrite – Amirouche Zeggagh Sep 11 '17 at 15:34
  • I don't understand what you mean. Does your program throw an error at the `createBitmap()` line? Whatever code I provided shouldn't have affected that line. – Reti43 Sep 11 '17 at 16:12
  • I have this ERROR 'java.lang.IllegalStateException' at Bitmap.setPixel . I can't edit the original bitmap – Amirouche Zeggagh Sep 13 '17 at 10:17
  • You first say you have a problem with `createBitmap()` and then with `setPixel()`. My answer shouldn't interfere with any of that, other than provide you with the logic to not iterate through every single pixel. If you have made more changes other than the code/explanation I've described you're dealing with a new problem and you ought to ask a new question. If the problem is with the code I've provided, you should be more explicit about it. But maybe your [Bitmap is not mutable](https://stackoverflow.com/questions/6764839/error-with-setpixels)? – Reti43 Sep 13 '17 at 10:43
  • Thanks . when I implemented the code I don't have mutable bitmap . Now it work well and my code is take very short time – Amirouche Zeggagh Sep 13 '17 at 14:53