Skip to content Skip to sidebar Skip to footer

Optimize Color Range Check With Nested For Loop

I know their are already some 'optimize for loop' questions, but I think this one is a little different. I got a code that reads all pixels from a image. From each pixel I must hav

Solution 1:

I found a fix, the code is a total change however.

bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.four_colors);
        System.out.println("START");
        int orgWidth = bmp.getWidth();
        int orgHeight = bmp.getHeight();
        //define the array size
        int[] pixels = new int[orgWidth * orgHeight];

                 bmp.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);

                 for (inty = 0; y < orgHeight; y++){
                     for (intx = 0; x < orgWidth; x++){
                         intindex = y * orgWidth + x;
                         int R = (pixels[index] >> 16) & 0xff;     //bitwise shifting
                         int G = (pixels[index] >> 8) & 0xff;
                         int B = pixels[index] & 0xff;
                         total++;
                         if ((G > R)&&(G > B)){
                             counter++;
                             //do something
                        }
                     }
                 }

I hope it will help other people too.

Post a Comment for "Optimize Color Range Check With Nested For Loop"