Hi Juliana,
I'm not sure, but maybe I've found the point of error. Reply embedded
below. Hope this helps.
Regards,
Nico
--- In [EMAIL PROTECTED], "Juliana Lewis"
<[EMAIL PROTECTED]> wrote:
>
> hello there.
> I am working on image processing in Java. I was writing a
> simple application which can gray-scale an image, sharpen
> it , blur it, invert its colors.
>
> Now i've writting the following code to accomplish the following
> task. I used simple java.awt.image class to write the convolution
> filter, instead of using java ConvolveOp operator, to clarify the
> concepts but now it gives me the error which is
>
> "Array index out of bounds Exception" and IndexColorModel sort of
> error, can someone please help me, i am posting the code with it.
>
<snip>
> listing 7
> import java.applet.*;
> import java.awt.*;
> import java.awt.image.*;
>
> abstract class Convolver implements ImageConsumer, PlugInFilter {
> int width, height;
> int imgpixels[], newimgpixels[];
>
> abstract void convolve(); // filter goes here...
>
> public Image filter(Applet a, Image in) {
> in.getSource().startProduction(this);
> waitForImage();
> newimgpixels = new int[width*height];
###########################################
I hope you are aware of the fact that the index of an array always
starts at 0 and goes up to (in this case) width*height - 1.
Q: are you sure that at the time this code is executed the variables
width and height have correct values?
>
> try {
> convolve();
> } catch (Exception e) {
> System.out.println("Convolver failed: " + e);
> e.printStackTrace();
> }
>
> return a.createImage(
> new MemoryImageSource(width, height, newimgpixels, 0, width));
> }
>
> synchronized void waitForImage() {
> try { wait(); } catch (Exception e) { };
> }
>
> public void setProperties(java.util.Hashtable dummy) { }
> public void setColorModel(ColorModel dummy) { }
> public void setHints(int dummy) { }
>
> public synchronized void imageComplete(int dummy) {
> notifyAll();
> }
>
> public void setDimensions(int x, int y) {
> width = x;
> height = y;
> imgpixels = new int[x*y];
> }
>
> public void setPixels(int x1, int y1, int w, int h,
> ColorModel model, byte pixels[], int off, int scansize) {
> int pix, x, y, x2, y2, sx, sy;
>
> x2 = x1+w;
> y2 = y1+h;
> sy = off;
> for(y=y1; y<y2; y++) {
> sx = sy;
> for(x=x1; x<x2; x++) {
> pix = model.getRGB(pixels[sx++]);
> if((pix & 0xff000000) == 0)
> pix = 0x00ffffff;
> imgpixels[y*width+x] = pix;
###########################################
With regard to my previous remark: this works correctly only if sy
starts at 0; so I dare to suppose that the fourth-to-last line above
should read:
pix = model.getRGB( pixels[ sx++ - off]);
And what about the imgpixels[] index? I dare to suspect that y*width+x
does not start at index 0.
> }
> sy += scansize;
> }
> }
>
> public void setPixels(int x1, int y1, int w, int h,
> ColorModel model, int pixels[], int off, int scansize) {
> int pix, x, y, x2, y2, sx, sy;
>
> x2 = x1+w;
> y2 = y1+h;
> sy = off;
> for(y=y1; y<y2; y++) {
> sx = sy;
> for(x=x1; x<x2; x++) {
> pix = model.getRGB(pixels[sx++]);
> if((pix & 0xff000000) == 0)
> pix = 0x00ffffff;
> imgpixels[y*width+x] = pix;
##########################
Same remarks apply here.
> }
> sy += scansize;
> }
> }
> }
>
> listing 8
> public class Blur extends Convolver {
> public void convolve() {
> for(int y=1; y<height-1; y++) {
> for(int x=1; x<width-1; x++) {
> int rs = 0;
> int gs = 0;
> int bs = 0;
>
> for(int k=-1; k<=1; k++) {
> for(int j=-1; j<=1; j++) {
> int rgb = imgpixels[(y+k)*width+x+j];
################################################
Huh? Sorry, probably this code is correct, but nevertheless a bit of
commenting would help to understand this expression...
> int r = (rgb >> 16) & 0xff;
> int g = (rgb >> 8) & 0xff;
> int b = rgb & 0xff;
> rs += r;
> gs += g;
> bs += b;
> }
> }
>
> rs /= 9;
> gs /= 9;
> bs /= 9;
>
> newimgpixels[y*width+x] = (0xff000000 |
> rs << 16 | gs << 8 | bs);
##########################################
And once more my question: are you sure that y*width+x starts at value 0?
In listing 9 you also have a few places where these questions arise to me.
Yahoo! Groups Links
<*> To reply to this message, go to:
http://groups.yahoo.com/group/beginnersclub/post?act=reply&messageNum=5752
Please do not reply to this message via email. More information here:
http://help.yahoo.com/help/us/groups/messages/messages-23.html
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/beginnersclub/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/