Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9b8e36b9 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Avoiding duplicate calls to Bitmap.getPixel(x, y)

Change-Id: I24414b41e9b0e9833cd4e962187a08672bc86c46
parent 9315c9a8
Loading
Loading
Loading
Loading
+24 −20
Original line number Original line Diff line number Diff line
@@ -309,6 +309,9 @@ public final class Utilities {
        float highScore = -1;
        float highScore = -1;
        int bestHue = -1;
        int bestHue = -1;


        int[] pixels = new int[samples];
        int pixelCount = 0;

        for (int y = 0; y < height; y += sampleStride) {
        for (int y = 0; y < height; y += sampleStride) {
            for (int x = 0; x < width; x += sampleStride) {
            for (int x = 0; x < width; x += sampleStride) {
                int argb = bitmap.getPixel(x, y);
                int argb = bitmap.getPixel(x, y);
@@ -326,6 +329,9 @@ public final class Utilities {
                    // Defensively avoid array bounds violations.
                    // Defensively avoid array bounds violations.
                    continue;
                    continue;
                }
                }
                if (pixelCount < samples) {
                    pixels[pixelCount++] = rgb;
                }
                float score = hsv[1] * hsv[2];
                float score = hsv[1] * hsv[2];
                hueScoreHistogram[hue] += score;
                hueScoreHistogram[hue] += score;
                if (hueScoreHistogram[hue] > highScore) {
                if (hueScoreHistogram[hue] > highScore) {
@@ -335,15 +341,14 @@ public final class Utilities {
            }
            }
        }
        }


        SparseArray<Float> rgbScores = new SparseArray<Float>();
        SparseArray<Float> rgbScores = new SparseArray<>();
        int bestColor = 0xff000000;
        int bestColor = 0xff000000;
        highScore = -1;
        highScore = -1;
        // Go back over the RGB colors that match the winning hue,
        // Go back over the RGB colors that match the winning hue,
        // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
        // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
        // The highest-scoring RGB color wins.
        // The highest-scoring RGB color wins.
        for (int y = 0; y < height; y += sampleStride) {
        for (int i = 0; i < pixelCount; i++) {
            for (int x = 0; x < width; x += sampleStride) {
            int rgb = pixels[i];
                int rgb = bitmap.getPixel(x, y) | 0xff000000;
            Color.colorToHSV(rgb, hsv);
            Color.colorToHSV(rgb, hsv);
            int hue = (int) hsv[0];
            int hue = (int) hsv[0];
            if (hue == bestHue) {
            if (hue == bestHue) {
@@ -362,7 +367,6 @@ public final class Utilities {
                }
                }
            }
            }
        }
        }
        }
        return bestColor;
        return bestColor;
    }
    }