0

Here is some code i made to write succesively and repetitively to multiple filechannels.

The files get made, there are no errors, but nothing gets written. The important part is where the buffer is written : "listofChan[j].write(buff);" . Why is this not working?

    FileChannel[] listofChan = new FileChannel[mStackHeight];
    FileOutputStream[] outputFiles = new FileOutputStream[mStackHeight];

    ByteBuffer buff = ByteBuffer.allocate(mStackWidth * 2); //2 byte integers
    ShortBuffer SB = buff.asShortBuffer();

    ImageProcessor ip;

    try {
        for (int i = 0; i < mStackHeight; i++) {
            outputFiles[i] = new FileOutputStream(new File(mSavepath + "\\T_" + i + ".dat"), true);
            listofChan[i] = outputFiles[i].getChannel();
        }
        System.out.println("File streams created successfully.");

        for (int z = 0; z < mStackSz; z++) { //all slices in stack
            ip = mStack.getProcessor(z + 1);
            ip = ip.convertToShortProcessor();
            short[] Pixels = (short[]) ip.getPixels();
            for (int j = 0; j < mStackHeight; j++) {  //all lines in one frame
                SB.clear();                     
                SB.put(Pixels, j * mStackWidth, mStackWidth);
                listofChan[j].write(buff);                    
            }

            System.out.println("line " + z + " " + listofChan[mStackWidth-1].position());
            // System.out.println("Image line : " z-1+ p/mStackWidth );
        }

        for (int i = 0; i < mStackHeight; i++) {
            outputFiles[i].close();
        }

        System.out.println("Buffer contents written to file.");

    } catch (IOException e) {
        System.out.println("File IO exception : " + e.toString());

    }

Okay, I've found the solution:

I needed to add buff.clear() on a line just above SB.clear(); because after writing the ByteBuffer, the read/write postition is at end of the buffer, so it cannot be written from further. The same for the IntBuffer, this also needs to be reset separately because the positions of both views are independent (I'm assuming ...).

Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51

2 Answers2

1

I think you need to flip your byte buffer before it is written. It is still in read/put mode.

user207421
  • 305,947
  • 44
  • 307
  • 483
Nicholas
  • 15,916
  • 4
  • 42
  • 66
0

You should not need to go to such a low level programming to achieve your task. There are already tools available in ImageJ to save each channel to a separate file.

For example, you can do so by choosing Plugins > Bio-Formats > Bio-Formats Exporter (this plugin is included in Fiji) and it will present the following dialog:

Bioformats dialog to allow saving separate files

You can use the Recorder to get the required Java code:

IJ.run(imp, "Bio-Formats Exporter", "save=C:\\Path\\To\\Your\\File.ome.tif write_each_channel compression=Uncompressed");

If you really want to interact with the exporter on a lower level, have a look at the source code of Exporter.java.

Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51
  • Thanks for the info. Will this also work for extremely large vitual stacks (10000 images and over)? – chris vandert Apr 20 '16 at 18:51
  • I haven't tried, but it should. If it doesn't, it's a bug (and [should be reported](http://imagej.net/Bug_reporting_best_practices)). So why don't you just try it? – Jan Eglinger Apr 20 '16 at 19:04