Open an ImagePlus from a stream or bytearray

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Open an ImagePlus from a stream or bytearray

Johannes Hermen
Hi,

Does anybody know if it is possible to create an ImagePlus directly from an
InputStream or a ByteArray.
I need to create ImagePlus objects from DICOM files that are retrieved from
a Database, so that i can only get them as Stream or ByteArray.


Thanks in advance

       _Johannes

-----------------------------------------------------------------
Johannes Hermen  -  Ingenieur de Recherche
[hidden email]
-----------------------------------------------------------------
CRP Henri Tudor  http://www.santec.tudor.lu
29, Avenue John F. Kennedy
L-1855 Luxembourg
-----------------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImagePlus from a stream or bytearray

dscho
Hi,

On Fri, 16 Mar 2007, Johannes Hermen wrote:

> Does anybody know if it is possible to create an ImagePlus directly from
> an InputStream or a ByteArray.

AFAICT, no. The openImage() method of ij.io.Opener wants to look at the
file type first, which means that it also wants to look at the filename.

> I need to create ImagePlus objects from DICOM files that are retrieved
> from a Database, so that i can only get them as Stream or ByteArray.

That might be easier: you already _know_ that it is a DICOM file. But you
have to hack the class DicomDecoder in ij.plugin.DICOM so that it actually
takes a preinitialised BufferedInputStream instead of opening it itself
(you might do this by wrapping lines 365-369 in an "if (f == null) {...}",
and adding a new constructor which also takes a BufferedInputStream and
initialises "f" with that.

As for the ByteArray: there is java.io.ByteArrayInputStream which wraps
the array in a stream.

AFAICT DICOM.java is self-contained, so to test all this, you can just
copy it somewhere else (but do not forget to adjust the package name! I do
that regulary, and it compiles, but throws an exception...).

Hth,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImagePlus from a stream or bytearray

ctrueden
In reply to this post by Johannes Hermen
Hi Johannes,

You can construct an ImageProcessor subclass corresponding to the type
of pixel data you need, and then pass it to the ImagePlus constructor.
For example, for an 8-bit image stored as bytes:

    byte[] pix = ...; // get your bytes somehow
    int w = ...; // width of image
    int h = ...; // height of image
    ByteProcessor proc = new ByteProcessor(w, h, pix, null);
    ImagePlus imp = new ImagePlus("My Image", proc);
    imp.show();

We do a lot of array-to-ImagePlus conversions in the Bio-Formats
Importer plugin. You can check the source for a more complex example:

https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/plugins/Importer.java

Lines 718-868, which constructs ImageProcessor and ImagePlus objects,
are probably most relevant.

-Curtis

On 3/16/07, Johannes Hermen <[hidden email]> wrote:

> Hi,
>
> Does anybody know if it is possible to create an ImagePlus directly from an
> InputStream or a ByteArray.
> I need to create ImagePlus objects from DICOM files that are retrieved from
> a Database, so that i can only get them as Stream or ByteArray.
>
>
> Thanks in advance
>
>        _Johannes
>
> -----------------------------------------------------------------
> Johannes Hermen  -  Ingenieur de Recherche
> [hidden email]
> -----------------------------------------------------------------
> CRP Henri Tudor  http://www.santec.tudor.lu
> 29, Avenue John F. Kennedy
> L-1855 Luxembourg
> -----------------------------------------------------------------
>
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImagePlus from a stream or bytearray

Wayne Rasband
In reply to this post by Johannes Hermen
> Does anybody know if it is possible to create an ImagePlus directly
> from an
> InputStream or a ByteArray.
> I need to create ImagePlus objects from DICOM files that are retrieved
> from
> a Database, so that i can only get them as Stream or ByteArray.

Starting with ImageJ 1.38m, a plugin can open and display a DICOM with
a BufferedInputStream using

     DICOM dcm = new DICOM(bis);
     dcm.run("Title");
     dcm.show();

where 'bis' is a BufferedInputStream. Note that 'dcm' is also an
ImagePlus since the DICOM class extends ImagePlus.

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImagePlus from a stream or bytearray

Deepak naik
I need to save Gray and RGB images.ImageJ says it as unsupported format.How can I pass BufferedImage to ImagePlus constructor?