Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem using getPixel() method of WritabbleRaster  RSS feed

 
Hammad Rauf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have run into a problem when trying to read pixel data from a WritableRaster. My objective is to copy all pixels of an original BufferedImage into a new processed BufferedImage. The processing cannot be done using affine transforms and has to be done pixel by pixel.

I get the runtime Exception ArrayIndexOutOfBoundsException on line 37 (See attached code and Exception).

Is my approach to using WritableRaster and BufferedImage not correct?
How can I extract one pixel at a time, do some processing, and write it to another BufferedImage?

Thanks and Regards,

Hammad Rauf
+++++++++++++

Code
-------
line 28:public static BufferedImage copyByPixel(BufferedImage ii) {
BufferedImage oi = new BufferedImage(ii.getWidth(), ii.getHeight(), BufferedImage.TYPE_INT_RGB);
WritableRaster irast = ii.getRaster();
WritableRaster orast = oi.getRaster();
Rectangle rec = irast.getBounds();

for (int x=rec.x; x <= (rec.x+rec.width); x++)
for (int y=rec.y; y <= (rec.y+rec.height); y++) {
int[] pixelData= new int[3];
line 37:pixelData = irast.getPixel(x,y,pixelData);
//
//do some processing on pixel data
//
orast.setPixel(x,y,pixelData);
}

return oi;
}


Exception
-------------
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 60000
at java.awt.image.DataBufferInt.getElem(DataBufferInt.java:183)
at java.awt.image.SinglePixelPackedSampleModel.getPixel(SinglePixelPackedSampleModel.java:427)
at java.awt.image.Raster.getPixel(Raster.java:1251)
at ImageEnlarger.copyByPixel(ImageEnlarger.java:37)
at AppFrame.action_Copy(AppFrame.java:500)
at AppFrame$6.actionPerformed(AppFrame.java:490)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1461)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1515)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:392)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:264)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:254)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:244)
at java.awt.Component.processMouseEvent(Component.java:3799)
at java.awt.Component.processEvent(Component.java:3628)
at java.awt.Container.processEvent(Container.java:1205)
at java.awt.Component.dispatchEventImpl(Component.java:2678)
at java.awt.Container.dispatchEventImpl(Container.java:1254)
at java.awt.Component.dispatchEvent(Component.java:2581)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2500)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2265)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2174)
at java.awt.Container.dispatchEventImpl(Container.java:1241)
at java.awt.Window.dispatchEventImpl(Window.java:964)
at java.awt.Component.dispatchEvent(Component.java:2581)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:444)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:141)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:132)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:124)
 
Hammad Rauf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

I modified my code to use BufferedImage's getRGB method. It is copying only the top one third of my test image. How can I copy the full image?

Thanks and Regards,

Hammad Rauf


Modified Code
--------------------
public static BufferedImage copyByPixel(BufferedImage ii) {
BufferedImage oi = new BufferedImage(ii.getWidth(), ii.getHeight(),BufferedImage.TYPE_INT_RGB);

for (int x=0; x < ii.getWidth(); x++) {
for (int y=0; y < ii.getHeight(); y++) {
int pixelData = 0;
pixelData = ii.getRGB(x,y);
//
//do some processing on pixel data
//
oi.setRGB(x,y,pixelData);
}
}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You code works for me. Here's proof:
I suggest that whenever possible you post a minimal but complete program that demonstrates your problem. Then forum members can run it and see what's going wrong.
 
Hammad Rauf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.

The code for displaying the image had the bug. Solved now.

Thanks again.

Hammad Rauf
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!