Flipping an image within a scifio plugin

Hi to all,

Completely newbie in ImageJ2, so please bear with me. To have to use git+maven+eclipse being used to the command line is a bit overhelming.

I put together a scifio plugin for the Elmitec UView data files (used in low-energy electron microscopy -LEEM-), after having a ImageJ1 one for quite a few years. Once I got over some of the new concepts, I have to say the new framework is great. But it takes quite some time to get used to it (the so-called ByteArrayReader confused me to no end because I assumed it would only read bytes, not int or whatever). The code is in https://github.com/jufiba/scifio-UView/.

But I need to flip vertically the images to make them look like in the original software. What is the best way to do that within the scifio plugin? Because what I have is a ByteArrayPlane, not a full image to use FlipVertical, right?

Thanks in advance,

Welcome to the forum, @Juan_de_la_Figuera, and great to hear that the SCIFIO framework is working for you.

My initial suggestion is to put some code here which flips the words in your byte array buffer. Since you have 16-bit data, this would look something like (untested):

final int bpp = 2;
for (int y=0; y<height; y++) {
  final int ny = height - y - 1;
  for (int x=0; x<width; x++) {
    int offset = bpp * (width*y + x);
    int newoff = bpp * (width*ny + x);
    int tmp0 = buf[newoff];
    int tmp1 = buf[newoff + 1];
    buf[newoff] = buf[offset];
    buf[newoff + 1] = buf[offset + 1];
    buf[offset] = tmp0;
    buf[offset + 1] = tmp1;

And then your image rows will be flipped, no?

Sorry there is no swankier way. We could add a generalized utility method for this if you wish. Feel free to file a PR for it.

Hmmm. Ok, if it has to be done “by hand” :smiley: I am now doing it row by row, so hopefully it is not too slow. We usually read hundreds of images at a time!:

for(int i=0;i<height;i++) {
	getStream().read(buf,(height-1-i)*width*2,width*2); // Need to flip vertically the image
1 Like

Sounds good.

One potentially nicer option would be to have metadata describing the units/calibration/orientation of each axis. We have this already, actually, and you could populate it, but I expect ImageJ would not respect inverted values per axis. Still, that would be the way forward in the long-term, I think.