ImagePlus into postgresql

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

ImagePlus into postgresql

Albert-5
Hi all,

I have succeded in saving and reading an ImagePlus as a Tiff into a
postgresql database using standard java 1.4.2 and ImageJ classes, but
there are problems: when read, the ImagePlus is black. So either it was
not properly saved, which is what I suspect, or not properly read.
Reading is easy, with the Opener.openAsTiff(Inputstream is, String
name), but the saving can be problematic: I need an InputStream for the
java.sql.PreparedStatement(int, InputStream, int), but the
ij.io.TiffEncoder writes bytes to a DataOutputStream, not to an
InputStream! So I'm piping it one into the other, and I suspect the
problems lay at this point, in the concurrent thread that is writing
into the DataOutputStream that encapsulates the PipedInputStream:


PipedInputStream i_stream = new PipedInputStream();
DataOutputStream o_stream = new DataOutputStream(new
BufferedOutputStream(new PipedOutputStream(i_stream)));
FileInfo fi = img.getFileInfo();
TiffEncoder te = new TiffEncoder(fi);
new ReadOutputStream(te, o_stream).start();


the class ReadOutputStream has this run() method:

         public void run() {
                 try{
                         //write bytes from TiffEncoder to the
DataOutputStream
                         te.write(o_stream);
                         //write bytes to the underlying output stream
                         o_stream.flush();
                         //o_stream.close();
                 }catch(IOException ioe) {
                         IJ.log("Can't write to output stream:\n" + ioe);
                 }
         }

If the o_stream.flush() is not there, the writing does not happen at
all. But then could this flush() be cutting it short?

Does anyone have experience with this and provide some hints?

What is the best, fastest and easiest way to put an ImagePlus into an
InputStream?


Thanks for any help.

Albert



Albert Cardona
Institute of Neuroinformatics     Tel : +41 1 635 3052
University/ETH Zurich             Fax : +41 1 635 3053
Winterthurerstrasse 190           acardona (at) ini phys ethz ch
Zurich 8057, Switzerland          www.ini.unizh.ch
Reply | Threaded
Open this post in threaded view
|

Re: ImagePlus into postgresql

Abramoff-Michael
I cannot help you specifically, but generally, in my experience with
streams, especially ones that may be slow, it is a good thing to examine
the number of bytes actually written (returned by x.write) and loop
until the required number of bytes have been written.

This is from an imaging servlet but I think the argument remains the
same:
                       // Since the stream cannot always keep up with
the loop,
                        // I made it converge using 'trial' and the code
below.
                        // If you carefully read the docs, you will
notice it says: "block until SOME data
                        // is read". Apparently, read does not always
block until ALL data have been read.
                        for (int bytesRead = 0 ; length > 0; length -=
bytesRead)
                        {
                                int trial = 1;
                                if (length > buf.length)
                                        trial = buf.length;
                                else
                                        trial = (int) length;
                                bytesRead = in.read(buf, 0, trial);
                                out.write(buf, 0, bytesRead);
                                total += bytesRead;
                        }
                }


Hope this helps,

Michael

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
Albert
Sent: Monday, July 11, 2005 11:26 AM
To: [hidden email]
Subject: ImagePlus into postgresql

Hi all,

I have succeded in saving and reading an ImagePlus as a Tiff into a
postgresql database using standard java 1.4.2 and ImageJ classes, but
there are problems: when read, the ImagePlus is black. So either it was
not properly saved, which is what I suspect, or not properly read.
Reading is easy, with the Opener.openAsTiff(Inputstream is, String
name), but the saving can be problematic: I need an InputStream for the
java.sql.PreparedStatement(int, InputStream, int), but the
ij.io.TiffEncoder writes bytes to a DataOutputStream, not to an
InputStream! So I'm piping it one into the other, and I suspect the
problems lay at this point, in the concurrent thread that is writing
into the DataOutputStream that encapsulates the PipedInputStream:


PipedInputStream i_stream = new PipedInputStream();
DataOutputStream o_stream = new DataOutputStream(new
BufferedOutputStream(new PipedOutputStream(i_stream)));
FileInfo fi = img.getFileInfo();
TiffEncoder te = new TiffEncoder(fi);
new ReadOutputStream(te, o_stream).start();


the class ReadOutputStream has this run() method:

         public void run() {
                 try{
                         //write bytes from TiffEncoder to the
DataOutputStream
                         te.write(o_stream);
                         //write bytes to the underlying output stream
                         o_stream.flush();
                         //o_stream.close();
                 }catch(IOException ioe) {
                         IJ.log("Can't write to output stream:\n" +
ioe);
                 }
         }

If the o_stream.flush() is not there, the writing does not happen at
all. But then could this flush() be cutting it short?

Does anyone have experience with this and provide some hints?

What is the best, fastest and easiest way to put an ImagePlus into an
InputStream?


Thanks for any help.

Albert



Albert Cardona
Institute of Neuroinformatics     Tel : +41 1 635 3052
University/ETH Zurich             Fax : +41 1 635 3053
Winterthurerstrasse 190           acardona (at) ini phys ethz ch
Zurich 8057, Switzerland          www.ini.unizh.ch
Reply | Threaded
Open this post in threaded view
|

Re: ImagePlus into postgresql

P. Daniel Tyreus
In reply to this post by Albert-5
You can do it this way, but you are not using piped streams correctly. They
should be run in threads. See this brieft article for more information:
http://www.devx.com/tips/Tip/13013

Regards,
Daniel
CircuSoft Instrumentation

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Albert
Sent: Monday, July 11, 2005 9:26 AM
To: [hidden email]
Subject: ImagePlus into postgresql

Hi all,

I have succeded in saving and reading an ImagePlus as a Tiff into a
postgresql database using standard java 1.4.2 and ImageJ classes, but there
are problems: when read, the ImagePlus is black. So either it was not
properly saved, which is what I suspect, or not properly read.
Reading is easy, with the Opener.openAsTiff(Inputstream is, String name),
but the saving can be problematic: I need an InputStream for the
java.sql.PreparedStatement(int, InputStream, int), but the ij.io.TiffEncoder
writes bytes to a DataOutputStream, not to an InputStream! So I'm piping it
one into the other, and I suspect the problems lay at this point, in the
concurrent thread that is writing into the DataOutputStream that
encapsulates the PipedInputStream:


PipedInputStream i_stream = new PipedInputStream(); DataOutputStream
o_stream = new DataOutputStream(new BufferedOutputStream(new
PipedOutputStream(i_stream))); FileInfo fi = img.getFileInfo(); TiffEncoder
te = new TiffEncoder(fi); new ReadOutputStream(te, o_stream).start();


the class ReadOutputStream has this run() method:

         public void run() {
                 try{
                         //write bytes from TiffEncoder to the
DataOutputStream
                         te.write(o_stream);
                         //write bytes to the underlying output stream
                         o_stream.flush();
                         //o_stream.close();
                 }catch(IOException ioe) {
                         IJ.log("Can't write to output stream:\n" + ioe);
                 }
         }

If the o_stream.flush() is not there, the writing does not happen at
all. But then could this flush() be cutting it short?

Does anyone have experience with this and provide some hints?

What is the best, fastest and easiest way to put an ImagePlus into an
InputStream?


Thanks for any help.

Albert



Albert Cardona
Institute of Neuroinformatics     Tel : +41 1 635 3052
University/ETH Zurich             Fax : +41 1 635 3053
Winterthurerstrasse 190           acardona (at) ini phys ethz ch
Zurich 8057, Switzerland          www.ini.unizh.ch