Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

inheriting BufferedImage and using ImageIO.read()

64 views
Skip to first unread message

mx

unread,
Nov 20, 2011, 6:57:14 AM11/20/11
to
So, I have a SubImage class which extends BufferedImage and I would like
to use ImageIO.read() to create a SubImage, but since it returns a
BufferedImage, I can't find a way of doing that.
Should I be using another design? or is there a way to get past this
problem?
I would also add that right now my SubImage simply adds methods to the
BufferedImage class. Yet downcasting causes an exception.

Lew

unread,
Nov 20, 2011, 11:11:22 AM11/20/11
to
Since you absolutely show no code whatsoever, much less a Simple, Self-Contained Compilable Example (SSCCE http://sscce.org/), I'll guess, but the lack of information you provide may cause me to miss the mark. (Follow the advice in http://sscce.org/.)

Subclassing 'BufferedImage' is almost certainly a mistake, but without knowing your reasons or what changes you made it's hard to be 100% certain. Still, I'd take 10:1 that you should "favor composition over inheritance". (http://java.sun.com/docs/books/effective/) That is, use a class that has a member reference to a 'BufferedImage' rather than extending it.

As for the downcast, I'll take 100:1 odds that you created a 'BufferedImage' that was not a 'SubImage' then tried to downcast it. You can only downcast a reference if it points to an instance of the subtype. Otherwise you get a 'ClassCastException'. You did get a 'ClassCastException', right? You didn't even bother to tell us that much, much less what the message said or what code it applied to.

To answer your questions: Yes, you should be using another design. Maybe even if you give us more information we could help you figure out what that other design should be. Like, what are you trying to accomplish, and why did you imaging subclassing 'BufferedImage' would help, and *WHAT IS YOUR CODE?* (http://sscce.org/) (Note the "Self-Contained" in that initialism's expansion!)

The way to get around your problems (there are more than one), read the Oracle tutorials about inheritance and casting in Java. Buy and study (repeatedly) the book /Effective Java/, 2nd ed., by Joshua Bloch, referenced above.

Somebody have the FAQ for asking smart questions handy? Between that and http://sscce.org/ we should be able to help this guy if he follows that advice.

So: Don't extend 'BufferedImage'. Don't try to downcast unless you know the instance is of the proper subtype. Do tell us what you want to accomplish, rather than merely how you imagined you should accomplish it. Do study diligently the tutorials, /Effective Java/ and http://sscce.org/.

--
Lew

John B. Matthews

unread,
Nov 20, 2011, 3:27:25 PM11/20/11
to
In article
<15176118.252.1321805482227.JavaMail.geo-discussion-forums@prdy11>,
Lew <lewb...@gmail.com> wrote:

> mx wrote:
> > So, I have a SubImage class which extends BufferedImage and I would
> > like to use ImageIO.read() to create a SubImage, but since it
> > returns a BufferedImage, I can't find a way of doing that. Should I
> > be using another design? or is there a way to get past this
> > problem? I would also add that right now my SubImage simply adds
> > methods to the BufferedImage class. Yet downcasting causes an
> > exception.

This problem may arise when one wishes to obtain the result of
getSubimage() [1] without allocating the memory required to read the
entire image into a BufferedImage.

I often resort to rasterizing the data using an available (possibly
domain specific) tool, e.g. Ossim [2] for geodetic data or a DICOM [3]
implementation for medical imaging data. Reading a portion of the image
then becomes an exercise in random access to a file. ImageJ [4] is a
valuable adjunct.

> [...]
>
> Subclassing 'BufferedImage' is almost certainly a mistake, but
> without knowing your reasons or what changes you made it's hard to be
> 100% certain. Still, I'd take 10:1 that you should "favor
> composition over inheritance".
> (http://java.sun.com/docs/books/effective/)
> That is, use a class that has a member reference to a 'BufferedImage'
> rather than extending it.

As the rasterization process necessarily externalizes the image's
metadata, composition works particularly well.

> [...]
>
> Somebody have the FAQ for asking smart questions handy? Between that
> and http://sscce.org/ we should be able to help this guy if he
> follows that advice.

"How To Ask Questions The Smart Way" [5] comes to mind.

[1] <http://download.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html>
[2] <http://www.ossim.org/OSSIM/OSSIM_Home.html>
[3] <http://en.wikipedia.org/wiki/DICOM>
[4] <http://rsbweb.nih.gov/ij/>
[5] <http://catb.org/~esr/faqs/smart-questions.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Roedy Green

unread,
Nov 20, 2011, 3:41:13 PM11/20/11
to
On Sun, 20 Nov 2011 12:57:14 +0100, mx <m...@nomail.com> wrote, quoted
or indirectly quoted someone who said :

>So, I have a SubImage class which extends BufferedImage a

This sounds like a is-a vs has-a problem.

See http://mindprod.com/jgloss/isa.html

If you implemented your ExtendedBufferImage like this:

OtherStuff os;
BufferedImage bi.

Then you could convert that BufferedImage result back with a
constructor like this:

ExtendedBufferImage( BufferedImage bi, OtherStuff os)

--
Roedy Green Canadian Mind Products
http://mindprod.com
I can't come to bed just yet. Somebody is wrong on the Internet.

markspace

unread,
Nov 20, 2011, 4:02:20 PM11/20/11
to
On 11/20/2011 3:57 AM, mx wrote:
> So, I have a SubImage class which extends BufferedImage and I would like
> to use ImageIO.read() to create a SubImage, but since it returns a
> BufferedImage, I can't find a way of doing that.
> Should I be using another design?


In all seriousness, probably. We'll need to know a lot more about your
design and requirements, but it very probable you've subclassed
BufferedImage here unnecessarily.


>or is there a way to get past this
> problem?


Add a constructor for SubImage that creates a SubImage from a BufferedImage?

public class SubImage extends BufferedImage {

public SubImage() { ...// no argument ctor

public SubImage( BufferedImage bi ) { ... // add this

...


> Yet downcasting causes an exception.


Yup.

0 new messages