Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 32
Registered: ‎03-19-2012
My Device: developer
My Carrier: Reliance

Emboss image effect

Hi everyone.

I want to convert an image into emboss effect. This is my Emboss Image effect code.

I got an arrayIndexOutOfBound exception.

also my newargb[][] remains empty.

 

can anybody tell me how to resolve this?

also if you have other logic for emboss image effect.Please share it.

Reply.

 

 

 

public final class ImageEmbossScreen extends MainScreen implements FieldChangeListener
{
    ButtonField embossEffect;

    static Bitmap STRIPE;
    static Bitmap bitmap,newbitmap;
    static int width,height;
    public ImageEmbossScreen()
    {        
        setTitle("Emboss Image Screen");
        STRIPE=Bitmap.getBitmapResource("monochrome.jpg");        
        add(new BitmapField(STRIPE));
        embossEffect = new ButtonField("Emboss button", ButtonField.CONSUME_CLICK);
        embossEffect.setChangeListener(this);
        add(embossEffect);      
    }
    
    public void fieldChanged(Field arg0, int arg1) {
        if(arg0==embossEffect){
            Dialog.alert("Emboss Effect");
            EmbossImage(STRIPE);
        }
    }


public Bitmap EmbossImage(Bitmap src) {
        double[][] EmbossConfig = new double[][] {
                { 0 , 0 , 0 },
                {  0 , 9 ,  -3 },
                { 0 , -3 , -3}
            };
            ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
            convMatrix.applyConfig(EmbossConfig);
            convMatrix.Factor = 1;
            convMatrix.Offset = 127;
            return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
}
}

 

package embossingimageapp;
import net.rim.device.api.system.Bitmap;
public class ConvolutionMatrix {

    public static final int SIZE = 3;
    public double[][] Matrix;
    public double Factor = 1;
    public double Offset = 127;

    public ConvolutionMatrix(int size) {
        Matrix = new double[size][size];
    }
    public void setAll(double value) {
        for (int x = 0; x < SIZE; ++x) {
            for (int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = value;
            }
        }
    }
    public void applyConfig(double[][] config) {
        for(int x = 0; x < SIZE; ++x) {
            for(int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = config[x][y];
            }
        }
    }

    public static Bitmap computeConvolution3x3(Bitmap src, ConvolutionMatrix matrix) {
        int R,G,B,A;
        int width = src.getWidth();
        int height = src.getHeight();
        
        int sumR, sumG, sumB;
        int[][] pixels = new int[3][3];

        int [][] newargb=new int[width][height];
        int[] argb=new int[width*height];
        //int a;
        
        src.getARGB(argb, 0, width, 0, 0, width, height);
        
        for(int a=0;a<=argb.length;a++)
        {
            A= argb[a] >> 24;
            R= argb[a] >> 16 & 0xFF;
            G= argb[a] >> 8 & 0xFF;
            B= argb[a] & 0xFF;
        }
        int b=0;
        for(int i = 0; i <= height; ++i) {
            for(int  j = 0; j <= width; ++j) {
                
                newargb[i][j]=argb[b];
                b++;
            }
        }
        
        for(int y = 0; y <= height; ++y) {
            for(int x = 0; x <= width; ++x) {
                // get pixel matrix
                for(int p = 0; p < SIZE; ++p) {
                    for(int k = 0; k < SIZE; ++k) {
                        
                        pixels[p][k] = newargb[x + p][ y + k];
                    }
                }
            
                // get alpha of center pixel
                
                A = pixels[1][1]>>24;
                // init color sum
                sumR = sumG = sumB = 0;

                // get sum of RGB on matrix
                for(int q = 0; q < SIZE; ++q) {
                    for( int r = 0; r < SIZE; ++r) {
                        
                        A=pixels[q][r]>>24;
                        R=pixels[q][r]>>16 ;
                        G=pixels[q][r]>>8 ;
                        B=pixels[q][r] ;
                        
                        sumR += (R * matrix.Matrix[q][r]);
                        sumG += (G * matrix.Matrix[q][r]);
                        sumB += (B * matrix.Matrix[q][r]);
                    }
                }

                // get final Red
                R = (int)(sumR / matrix.Factor + matrix.Offset);
                if(R < 0) { R = 0; }
                else if(R > 255) { R = 255; }

                // get final Green
                G = (int)(sumG / matrix.Factor + matrix.Offset);
                if(G < 0) { G = 0; }
                else if(G > 255) { G = 255; }

                // get final Blue
                B = (int)(sumB / matrix.Factor + matrix.Offset);
                if(B < 0) { B = 0; }
                else if(B > 255) { B = 255; }

                // apply new pixel
                //result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
           
                argb[y*width+x] =((0xff000000) | R<< 16 | G << 8 | B );
            }
        }
        src.setARGB(argb, 0, width, 0, 0, width, height);
        return src;
    }
}

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Emboss image effect

[ Edited ]

This appears to be a continuation of one of your other questions:

http://supportforums.blackberry.com/t5/Java-Development/Emboss-image-effect/m-p/1654243

 

Rather than bloating the forum with loads of Threads it is normally considered good practice to add posts to your existing Thread.

 

When asking questions like this, it is usual to give us as much information as you can about the problem.  In this case, the line that throw the Exception would seem like an essential bit of information.  Can you tell us that? 

 

I suspect this problem is not really BlackBerry Java related, it appears to be a coding related problem.  As I understand it, the algorithm you are using is used on other platforms so if you have a problem with it, the immediate suspicion is that the implementation is not right.  To find problems like this I use the debugger.  You will find information on using the debugger here:

http://supportforums.blackberry.com/t5/Java-Development/How-to-Debug-and-Optimize/ta-p/442791

 

have a go at finding the problem yourself using the debugger.  If you have no luck, then please come back to us.

 

I would also use the debugger to resolve your problem using this algorithm for hue and saturation changes. 

Contributor
Posts: 32
Registered: ‎03-19-2012
My Device: developer
My Carrier: Reliance

Re: Emboss image effect

I debugged my code it gives me the array out of bound exception on following line.---

pixels[p][k] = newargb[x + p][ y + k];

 

 

I think this is because image WIDTH and HEIGHT not equal.

 

 

when i use the image having same width  &  height ,It doesnt give exception.

But also the output coming is not the emboss as I expect...

 

How to do embossing then?