View Single Post
Old 09-18-2007, 11:06 AM   #2 (permalink)
Pook
New Member
 
Join Date: Sep 2007
Model: 7100
PIN: N/A
Carrier: Vodafone
Posts: 10
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is not very robust code (Bitmap dimensions must be even) and I got the Alpha wrong but it sort of does the job.

Code:
    public static Bitmap getHalfScale( Bitmap bitmap )
    {
        int height, width, red, green, blue, alpha;
        int[] pixelInput;
        int[] pixelOutput;
        
        width = bitmap.getWidth();
        height = bitmap.getHeight();

        Bitmap result = new Bitmap( bitmap.getType(), width / 2, height /2 );
        
        pixelInput = new int[width * height];
        pixelOutput = new int[result.getWidth() * result.getHeight()];
        
        bitmap.getARGB( pixelInput, 0, width, 0, 0, width, height );
        
        for ( int x = 0; x < width; x += 2 )
        for ( int y = 0; y < height; y += 2 )
        {
            alpha = 0;
            red = 0;
            green = 0;
            blue = 0;
            
            for ( int dx = 0; dx < 2; dx++ )
            for ( int dy = 0; dy < 2; dy++ )
            {
                alpha += ( pixelInput[indexFromCoordinates(x+dx,y+dy,width)] & 0xff000000 ) >> 24;
                red += ( pixelInput[indexFromCoordinates(x+dx,y+dy,width)] & 0x00ff0000 ) >> 16;
                green += ( pixelInput[indexFromCoordinates(x+dx,y+dy,width)] & 0x0000ff00 ) >> 8;
                blue += ( pixelInput[indexFromCoordinates(x+dx,y+dy,width)] & 0x000000ff );
            }

            alpha >>= 2;
            red >>= 2;
            green >>= 2;
            blue >>= 2;
            pixelOutput[indexFromCoordinates(x/2,y/2,result.getWidth())] = 0x70000000 | ( red << 16 ) | ( green << 8 ) | blue;
        }
        
        result.setARGB( pixelOutput, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight() );

        return result;
    }
    
    public static int indexFromCoordinates( int x, int y, int width )
    {
        int result;
        result = y * width + x;
        return result;
    }
Offline   Reply With Quote