Skip to main content

basic question: BufferedImage to PlanarImage

6 replies [Last post]
youp12345
Offline
Joined: 2006-03-08
Points: 0

Hello

I encounter some difficulties in getting a PlanarImage from a BufferedImage.
I have a BufferedImage bi which is a subimage of a previous one.
To create the PlanarImage, I do:

ParameterBlockJAI pb = new ParameterBlockJAI("AWTImage");
pbttt.setParameter("awtImage", bi);
PlanarImage planarSource=(PlanarImage)JAI.create("AWTImage", pbttt);

I think that should be good, but, I encounter some problems when using it.
When printing bi and planarSource, I get:
bi : BufferedImage@14d7745: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width
= 555 height = 325 #Bands = 3 xOff = -745 yOff = -162 dataOffset[0] 259945
and
planarSource : PlanarImage[minX=0 minY=0 width=0 height=0 tileGridXOffset=0 tileGridYOffset=0 tileWidth=0 tileHeight=0 sampleModel=null col
orModel=null]

So somehing went wrong and I don't know what.

I tried others ways like :
PlanarImage planarSource=(PlanarImage)AWTImageDescriptor.create((Image)bi, null);
but it is the same.

On this Forum, it has been said :
Image someImage;
RenderedOp image = JAI.create("AWTImage", someImage);

but at runtime, JAI says this operation requires a ParameterBlock

Any idea on what's wrong with my code?

Youp.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vivks
Offline
Joined: 2010-03-05
Points: 0

Thanks ! it helped.

Aastha Bhardwaj

The right way to get a PlanarImage from a BufferedImage is

PlanarImage.wrapRenderedImage(bufferedImage);

since BufferedImage is a RenderedImage by virtue of implementing that
interface.

Note that the "awtimage" operation is intended to convert a
java.awt.image object (not a BufferedImage) to a PlanarImage. Please
refer to the Javadoc for the operation for details on the operation
requirements and functioning. All operations are described in the
javax.media.jai.operator package.

Aastha

---------------------
Aastha Bhardwaj
Java Advanced Imaging
Sun Microsystems

> Hello
>
> I encounter some difficulties in getting a PlanarImage from a BufferedImage.
> I have a BufferedImage bi which is a subimage of a previous one.
> To create the PlanarImage, I do:
>
> ParameterBlockJAI pb = new ParameterBlockJAI("AWTImage");
> pbttt.setParameter("awtImage", bi);
> PlanarImage planarSource=(PlanarImage)JAI.create("AWTImage", pbttt);
>
> I think that should be good, but, I encounter some problems when using it.
> When printing bi and planarSource, I get:
> bi : BufferedImage@14d7745: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width
> = 555 height = 325 #Bands = 3 xOff = -745 yOff = -162 dataOffset[0] 259945
> and
> planarSource : PlanarImage[minX=0 minY=0 width=0 height=0 tileGridXOffset=0 tileGridYOffset=0 tileWidth=0 tileHeight=0 sampleModel=null col
> orModel=null]
>
> So somehing went wrong and I don't know what.
>
> I tried others ways like :
> PlanarImage planarSource=(PlanarImage)AWTImageDescriptor.create((Image)bi, null);
> but it is the same.
>
> On this Forum, it has been said :
> Image someImage;
> RenderedOp image = JAI.create("AWTImage", someImage);
>
> but at runtime, JAI says this operation requires a ParameterBlock
>
> Any idea on what's wrong with my code?
>
> Youp.
> [Message sent by forum member 'youp12345' (youp12345)]
>
> http://forums.java.net/jive/thread.jspa?messageID=89970
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

youp12345
Offline
Joined: 2006-03-08
Points: 0

Thanks, it works.

Another question:
Now, I need to come back from the PlanarImage to a BufferedImage.

So, I did:
BufferedImage buffImg=myPlanarImage.getAsBufferedImage();

And I catch a NullPointerException...
myPlanarImage is not null
myPlanarImage.getColorModel()
returns:
DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0

Even when trying with
colmodel=theOriginalBufferedImage.getColorModel();
rect=new Rectangle(new Dimension(myPlanarImage.getWidth(), myPlanarImage.getHeight()));
myPlanarImage.getAsBufferedImage(rect, colmodel);
I get this Exception.

Where am i wrong, (again)?

Is there a good resource to get the basics of JAI? or how to port code from jai 1.0 to the current one ?

Aastha Bhardwaj

Hi,

PlanarImage.getAsBufferedImage() is the right method to convert from a
PlanarImage to a BufferedImage. However without the stack trace and/or a
compiling self contained test case, it is not possible to tell what is
causing the NullPointerException.

JAI 1.1.3-beta (the current version) is not radically different from JAI
1.0, so there should not be a major porting effort. The best place to
look for the current API is the Javadoc available from the main JAI
java.net page or directly here:

http://download.java.net/media/jai/javadoc/1.1.3-beta/index.html

The jaistuff java.net project contains many examples of JAI usage.
Other than that there is the Programmer's Guide, which is somewhat
outdated.

Aastha

---------------------
Aastha Bhardwaj
Java Advanced Imaging
Sun Microsystems

> Thanks, it works.
>
> Another question:
> Now, I need to come back from the PlanarImage to a BufferedImage.
>
> So, I did:
> BufferedImage buffImg=myPlanarImage.getAsBufferedImage();
>
> And I catch a NullPointerException...
> myPlanarImage is not null
> myPlanarImage.getColorModel()
> returns:
> DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0
>
> Even when trying with
> colmodel=theOriginalBufferedImage.getColorModel();
> rect=new Rectangle(new Dimension(myPlanarImage.getWidth(), myPlanarImage.getHeight()));
> myPlanarImage.getAsBufferedImage(rect, colmodel);
> I get this Exception.
>
> Where am i wrong, (again)?
>
> Is there a good resource to get the basics of JAI? or how to port code from jai 1.0 to the current one ?
> [Message sent by forum member 'youp12345' (youp12345)]
>
> http://forums.java.net/jive/thread.jspa?messageID=90379
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

youp12345
Offline
Joined: 2006-03-08
Points: 0

So, here is a full program that cause the segfault.
What it does:
- it loads an image stores it into a bufferedImage and then get a subImage of it (in the real app, it depends on the user input)
- it converts it into a PlanarImage to remove perspective effects on the texture
- and then it creates a new BufferedImage from the PlanarImage corrected in the previous step.

It seems that the nullPointerException comes only when using both perspective correction and (bufferedimage.)subImage.

To be complete, the software used:
jdk 1.5.06 and jai 1.1.2 under linux

Here is the code, to use it you need a 1600x1200 image named blank.jpg

import java.awt.image.*;
import javax.swing.*;
import javax.media.jai.*;
import java.awt.geom.*;

public class Test {

public static void main(String[] args) {

// some variables coming from user input in the original code
Point2D.Float v1=new Point2D.Float(745.0981f,162.0915f),
v2=new Point2D.Float(1283.6602f,394.77124f),
v3=new Point2D.Float(1299.3464f,486.2745f),
v4=new Point2D.Float(755.55554f,266.66666f);

float xMin=(float)745.0981,
yMin=(float)162.0915,
xMax=(float)1299.3464,
yMax=(float)486.2745;

// we load a file and put it into a temporaryImage
ImageIcon source=new ImageIcon("blank.jpg"); // blank.jpg is a 1600x1200 image

BufferedImage temporaryImage = new BufferedImage(source.getIconWidth(),source.getIconHeight(),BufferedImage.TYPE_INT_RGB);

temporaryImage.getGraphics().drawImage(source.getImage(),0,0,null);
System.out.println("temporaryImage = "+temporaryImage);

BufferedImage temporaryImage2 = temporaryImage.getSubimage((int) xMin,(int) yMin,(int) (xMax - xMin + 1),(int) (yMax - yMin + 1));

System.out.println("temporaryImage = "+temporaryImage2);

// PlanarImage planarSource=PlanarImage.wrapRenderedImage(temporaryImage2);
PlanarImage planarSource=PlanarImage.wrapRenderedImage(temporaryImage2);

// Projective transformation of the texture

PerspectiveTransform pt = PerspectiveTransform.getQuadToQuad(0,0,xMax - xMin,0,xMax - xMin,yMax - yMin,0,yMax - yMin,v1.x - xMin,v1.y - yMin,v2.x - xMin,v2.y - yMin,v3.x - xMin,v3.y - yMin,v4.x - xMin,v4.y - yMin);

ParameterBlockJAI pb = new ParameterBlockJAI("Warp");

pb.addSource(planarSource);
pb.setParameter("warp", new WarpPerspective(pt));
pb.setParameter("interpolation", new InterpolationBilinear());

RenderedOp rop=new RenderedOp("Warp",pb,null);
PlanarImage planarImage=rop.createInstance();
System.out.println("planarimage: "+planarImage);

BufferedImage tmpBuffImg=null;

try {
tmpBuffImg=planarImage.getAsBufferedImage();
} catch (Exception e)
{
System.out.println("Caught exception : "+e);
}
System.out.println("tmpBuffImg = "+tmpBuffImg);
}
}

// End of Code

youp12345
Offline
Joined: 2006-03-08
Points: 0

Is this bug from me? from JAI/java/... ?
Is it reproductible on another system than mine?