Bonjour, le but est de griser une image.
Pour cela j'ai deux méthodes. La mienne et celle d'un autre éleve.
La mienne, qui prend 5025 millisecondes sur du (1200*1920):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
void griser(Bitmap b){
        int[] couleurs=getbitcouleurs(b);
        int height=b.getHeight();
        int width=b.getWidth();
        for (int i =0;i < height*width; i++){
            int tmp = couleurs[i];
            int gris= (int)((Color.blue(tmp)+Color.red(tmp)+Color.green(tmp))*0.33);
            couleurs[i]=Color.argb(alpha(tmp),gris,gris,gris);
        }
        b.setPixels(couleurs,0,width, 0, 0, width, height);
    }
 
int[] getbitcouleurs(Bitmap b){
        int height=b.getHeight();
        int width=b.getWidth();
        int couleurs[]=new int[height*width];
        b.getPixels(couleurs, 0, width, 0, 0, width, height);
        return couleurs;
    }
Celle de mon collègue, qui prend 363 secondes:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
public void toGray2(Bitmap img) {
        int w = img.getWidth();
        int h = img.getHeight();
        int [] tab = new int[h*w];
        img.getPixels(tab, 0, w, 0, 0, w, h);//Gets the array of the bitmap's pixels
 
        for (int i = 0; i<w*h; i++) {
            int tmp = tab[i];
            int blue = (int) ((tmp & 0x000000FF) * 0.11);//Gets the blue component of the pixel by filtering the integer Color and weight the average
            int green = (int) (((tmp & 0x0000FF00) >> 8) * 0.59);//same for the green component
            int red = (int) (((tmp & 0x00FF0000) >> 16) * 0.3);//same for the red component
            int color_custom = blue + green + red;
            int final_pix = 0xFF000000 | (color_custom << 16) | (color_custom << 8) | color_custom;//Makes an integer matching the Color's formatting
            tab[i] = final_pix;
        }
        img.setPixels(tab, 0, w, 0, 0, w, h);
    }
Personne dans ma classe, ni même le prof, ne comprend la raison d'une telle différence d'efficacité. Une idée?

PS: mon collègue a récrit les fonctions Color.blue() etc...
J'ai déjà testé de les récrire sur le mêmes modelé mais le problème ne vient pas de la car le programme a mis 7650 millisecondes.