Skip to main content

ColorModel - ColorSpace: generic N (

5 replies [Last post]
Anonymous

Hi lists,

I'm working on a plugin which is able to read HDF4 format sources.

An HDF4 file may contain several images.
Some of these images may contain several Bands (sometime the number of bands
is greater than 10).

When building a BufferedImage, I need a Raster (SampleModel + DataBuffer)
and a ColorModel.

The SampleModel is not a problem because I can use a BandedSampleModel.
But I don't know how to handle the ColorModel for this type of images.

The ColorSpace class contains "static ints" for generic N component color
spaces (e.g. TYPE_2CLR, TYPE_3CLR, ..., TYPE_9CLR, TYPE_ACLR, ... ,
TYPE_FCLR).

What I have to do in order to correctly set-up a ColorModel with a 10
components color space?
Is this an achievable task?

Thanks a lot.

Best Regards,
Daniele

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Denis Bueno

I'm obviously confused about the right way to copy a BufferedImage instance. I
assume there's one piece of code which I have yet to discover that will copy (1)
a normal, full BufferedImage and (2) any sub image (obtained via getSubimage())
of (1).

Currently I'm using the code in copyBufferedImage() below, which generates the
following exception:

Exception in thread "main" java.lang.IllegalArgumentException: Raster
BytePackedRaster: width = 800 height = 600 #channels 1 xOff = -20 yOff = -20 has
minX or minY not equal to zero: -20 -20
at java.awt.image.BufferedImage.(BufferedImage.java:620)
at scratch.Aliasing.copyBufferedImage(Aliasing.java:73)
at scratch.Aliasing.createImage(Aliasing.java:54)
at scratch.Aliasing.(Aliasing.java:20)
at scratch.Aliasing.main(Aliasing.java:83);

I tried calling WritableRaster.createWritableTranslatedChild(0, 0) as was
suggested in a thread on JAI-interest [
http://archives.java.sun.com/cgi-bin/wa?A2=ind9907&L=jai-interest&D=0&P=... ],
but, that enlarges my subimage (which should be 20x20).

I'm trying to solve a larger problem where subimages of a larger BufferedImage
of type TYPE_BYTE_BINARY get distorted when they're painted onto another part of
the original image. (I'm implementing a selection+translation tool like in
Photoshop.) Eventually the code below will be a self-contained test case of that
behavior. (At the moment I get around the problem described above by copying the
entire BufferedImage before doing getSubimage().)

Any ideas? Thanks in advance.

-Denis

package scratch;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Aliasing extends JPanel {

private BufferedImage img = createImage();

public Aliasing() {
setPreferredSize(new Dimension(800, 600));
}

@Override
protected void paintComponent(final Graphics g) {
Graphics2D g2 = (Graphics2D) g;

g2.clearRect(0, 0, getWidth(), getHeight());

g2.drawImage(img, new AffineTransformOp(new AffineTransform(),
g2.getRenderingHints()),
0, 0);

g2.setColor(Color.red);
g2.setStroke(new BasicStroke(2f));
g2.draw(new Line2D.Double(50, 0, 150, 100));
}

@Override
public void update(final Graphics g) {
paintComponent(g);
}

private BufferedImage createImage() {
BufferedImage ret = new BufferedImage(800, 600,
BufferedImage.TYPE_BYTE_BINARY);
assert (ret.getColorModel() instanceof IndexColorModel);

final Graphics2D graphics = ret.createGraphics();
graphics.setStroke(new BasicStroke(2f));
graphics.draw(new Line2D.Double(0, 0, 100, 100));

BufferedImage subimage = ret.getSubimage(20, 20, 20, 20);
subimage = copyBufferedImage(subimage);

graphics.setColor(Color.black);
graphics.fillRect(0, 0, 20, 20);

graphics.drawImage(subimage,
new AffineTransformOp(new AffineTransform(),
graphics.getRenderingHints()),
0, 150);

// Outline the subimage.
graphics.setColor(Color.white);
graphics.drawRect(0, 150, subimage.getWidth(), subimage.getHeight());

return ret;
}

private BufferedImage copyBufferedImage(final BufferedImage source) {
final WritableRaster raster = source.copyData(null);
final BufferedImage copy = new BufferedImage(source.getColorModel(),
raster,

source.isAlphaPremultiplied(), null);
return copy;
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(800, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Aliasing aliasing = new Aliasing();

frame.add(aliasing);

frame.pack();
frame.setVisible(true);
}

}

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Chris Campbell

Hi Denis,

I believe the bug you're running into was fixed in JDK 6. However,
there's a more efficient way to copy a subregion of a BufferedImage
without using getSubimage() (the latter method has some non-obvious
side effects, so it's best to avoid it if at all possible).

The best way is to use the variant of Graphics.drawImage() that takes
both a src and dst region:
http://download.java.net/jdk6/docs/api/java/awt/
Graphics.html#drawImage(java.awt.Image,%20int,%20int,%20int,%20int,%
20int,%20int,%20int,%20int,%20java.awt.image.ImageObserver)

So in your case it should be as easy as:
g.drawImage(img,
dstx, dsty, dstx+w, dsty+w,
srcx, srcy, srcx+w, srcy+w, null);

Looks like a good question to add to our FAQ:
http://java.sun.com/products/java-media/2D/reference/faqs/index.html

Thanks,
Chris

PS... Go Jackets.

On Aug 10, 2006, at 10:55 AM, Denis Bueno wrote:
> I'm obviously confused about the right way to copy a BufferedImage
> instance. I
> assume there's one piece of code which I have yet to discover that
> will copy (1)
> a normal, full BufferedImage and (2) any sub image (obtained via
> getSubimage())
> of (1).
>
> Currently I'm using the code in copyBufferedImage() below, which
> generates the
> following exception:
>
> Exception in thread "main" java.lang.IllegalArgumentException:
> Raster
> BytePackedRaster: width = 800 height = 600 #channels 1 xOff = -20
> yOff = -20 has
> minX or minY not equal to zero: -20 -20
> at java.awt.image.BufferedImage.(BufferedImage.java:620)
> at scratch.Aliasing.copyBufferedImage(Aliasing.java:73)
> at scratch.Aliasing.createImage(Aliasing.java:54)
> at scratch.Aliasing.(Aliasing.java:20)
> at scratch.Aliasing.main(Aliasing.java:83);
>
> I tried calling WritableRaster.createWritableTranslatedChild(0, 0)
> as was
> suggested in a thread on JAI-interest [
> http://archives.java.sun.com/cgi-bin/wa?A2=ind9907&L=jai-
> interest&D=0&P=13579 ],
> but, that enlarges my subimage (which should be 20x20).
>
> I'm trying to solve a larger problem where subimages of a larger
> BufferedImage
> of type TYPE_BYTE_BINARY get distorted when they're painted onto
> another part of
> the original image. (I'm implementing a selection+translation tool
> like in
> Photoshop.) Eventually the code below will be a self-contained test
> case of that
> behavior. (At the moment I get around the problem described above
> by copying the
> entire BufferedImage before doing getSubimage().)
>
> Any ideas? Thanks in advance.
>
> -Denis
>
>
>
> package scratch;
>
> import java.awt.BasicStroke;
> import java.awt.Color;
> import java.awt.Dimension;
> import java.awt.Graphics;
> import java.awt.Graphics2D;
> import java.awt.geom.AffineTransform;
> import java.awt.geom.Line2D;
> import java.awt.image.AffineTransformOp;
> import java.awt.image.BufferedImage;
> import java.awt.image.IndexColorModel;
> import java.awt.image.WritableRaster;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
>
> public class Aliasing extends JPanel {
>
> private BufferedImage img = createImage();
>
> public Aliasing() {
> setPreferredSize(new Dimension(800, 600));
> }
>
> @Override
> protected void paintComponent(final Graphics g) {
> Graphics2D g2 = (Graphics2D) g;
>
> g2.clearRect(0, 0, getWidth(), getHeight());
>
> g2.drawImage(img, new AffineTransformOp(new AffineTransform(),
> g2.getRenderingHints()),
> 0, 0);
>
> g2.setColor(Color.red);
> g2.setStroke(new BasicStroke(2f));
> g2.draw(new Line2D.Double(50, 0, 150, 100));
> }
>
> @Override
> public void update(final Graphics g) {
> paintComponent(g);
> }
>
> private BufferedImage createImage() {
> BufferedImage ret = new BufferedImage(800, 600,
> BufferedImage.TYPE_BYTE_BINARY);
> assert (ret.getColorModel() instanceof IndexColorModel);
>
> final Graphics2D graphics = ret.createGraphics();
> graphics.setStroke(new BasicStroke(2f));
> graphics.draw(new Line2D.Double(0, 0, 100, 100));
>
> BufferedImage subimage = ret.getSubimage(20, 20, 20, 20);
> subimage = copyBufferedImage(subimage);
>
> graphics.setColor(Color.black);
> graphics.fillRect(0, 0, 20, 20);
>
> graphics.drawImage(subimage,
> new AffineTransformOp(new AffineTransform
> (),
>
> graphics.getRenderingHints()),
> 0, 150);
>
> // Outline the subimage.
> graphics.setColor(Color.white);
> graphics.drawRect(0, 150, subimage.getWidth(),
> subimage.getHeight());
>
> return ret;
> }
>
> private BufferedImage copyBufferedImage(final BufferedImage
> source) {
> final WritableRaster raster = source.copyData(null);
> final BufferedImage copy = new BufferedImage
> (source.getColorModel(),
> raster,
>
> source.isAlphaPremultiplied(), null);
> return copy;
> }
>
> public static void main(String[] args) {
> JFrame frame = new JFrame();
> frame.setSize(new Dimension(800, 600));
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>
> Aliasing aliasing = new Aliasing();
>
> frame.add(aliasing);
>
> frame.pack();
> frame.setVisible(true);
> }
>
> }
>
> ======================================================================
> =====
> To unsubscribe, send email to listserv@java.sun.com and include in
> the body
> of the message "signoff JAVA2D-INTEREST". For general help, send
> email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Jeannette Hung

But I'm assuming you don't really want a 10 component color model.
Sounds like you really want to be working with a Raster and then subband
it and associate those subbands with a RGB color model (or something
like that) to display it, right?

jeannette

Daniele Romagnoli wrote:
> Hi lists,
>
> I'm working on a plugin which is able to read HDF4 format sources.
>
> An HDF4 file may contain several images.
> Some of these images may contain several Bands (sometime the number of
> bands is greater than 10).
>
> When building a BufferedImage, I need a Raster (SampleModel +
> DataBuffer) and a ColorModel.
>
> The SampleModel is not a problem because I can use a BandedSampleModel.
> But I don't know how to handle the ColorModel for this type of images.
>
> The ColorSpace class contains "static ints" for generic N component
> color spaces (e.g. TYPE_2CLR, TYPE_3CLR, ..., TYPE_9CLR, TYPE_ACLR, ...
> , TYPE_FCLR).
>
> What I have to do in order to correctly set-up a ColorModel with a 10
> components color space?
> Is this an achievable task?
>
> Thanks a lot.
>
> Best Regards,
> Daniele
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body of the message "signoff JAVA2D-INTEREST". For general help, send
> email to listserv@java.sun.com and include in the body of the message
> "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Daniele Romagnoli

On 7/29/06, Jeannette Hung wrote:
>
> But I'm assuming you don't really want a 10 component color model.
> Sounds like you really want to be working with a Raster and then subband
> it and associate those subbands with a RGB color model (or something
> like that) to display it, right?

Yes, it's right.
We are using BogusColorSpace to do this.

Best Regards,
Daniele

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]

Jeannette Hung

Hi Daniele,
So could you subband? Or are you trying to process all 10 bands?

Thanks
jeannette

Daniele Romagnoli wrote:
> On 7/29/06, *Jeannette Hung* > > wrote:
>
> But I'm assuming you don't really want a 10 component color model.
> Sounds like you really want to be working with a Raster and then subband
> it and associate those subbands with a RGB color model (or something
> like that) to display it, right?
>
>
> Yes, it's right.
> We are using BogusColorSpace to do this.
>
>
> Best Regards,
> Daniele

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".