EMF image conversion.

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

EMF image conversion.

Keerthi G
Hi,
I am tired of finding an open source API to convert my 32 bit EMF image and
finally found this imagej.

Now, I am trying to convert a 32 bit EMF image to JPEG using imagejAPI
source. The following is the piece of code i am using to convert my EMF
image. But the output image (image1.jpeg) looks inverted, mirrored and
skewed. May i know what am I doing wrong here...
Please note that am not able to share either the original EMF image or the
image zipped, since the filter rejects my message saying that the
attachment of that type is not permitted.


public class IJUtil {

 public static void main(String[] args) throws Exception {
  File file = new File("D:/xmlword/1324445681526/image1.emf");
  FileInputStream in = new FileInputStream(file);
  FileInfo fi = new FileInfo();
  fi.fileType = 11;
  fi.width = 1748;
  fi.height = 1324;
  fi.pixelDepth = 32;
  fi.compression = fi.COMPRESSION_UNKNOWN;
  ImageReader reader = new ImageReader(fi);
  float[] pix = (float[]) reader.readPixels(in);
  double[] doublePixels = convertFloatsToDoubles(pix);
  JpegWriter writer = new JpegWriter();
  FloatProcessor processor = new FloatProcessor(1748, 1324, doublePixels);
  Image image = processor.createImage();
  ImagePlus ip = new ImagePlus("Title", image);
  writer.saveAsJpeg(ip, "D:/xmlword/1324445681526/image1.jpeg");
 }

 public static double[] convertFloatsToDoubles(float[] input) {
  if (input == null) {
   return null;
  }
  double[] output = new double[input.length];
  for (int i = 0; i < input.length; i++) {
   output[i] = input[i];
  }
  return output;
 }
}

Thanks in advance,

Cheers,
Keerthi G.

image1.jpeg (252K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: EMF image conversion.

Michael Schmid
Hi Keerthi G.,

the image is skewed like this if the width is wrong by one pixel.
I would suggest to use variables for width&height, then you need not change it in two places.
As far as I know, ImageJ has no option to flip it vertically while importing, you have to flip it afterwards.

By the way, there is no reason to convert the pixels to double[], you can use the constructor FloatProcessor(int width, int height, float[] pixels, ColorModel cm) with null for the ColorModel.

Michael
________________________________________________________________
On Dec 22, 2011, at 15:03, Keerthi G wrote:

> Hi,
> I am tired of finding an open source API to convert my 32 bit EMF image and
> finally found this imagej.
>
> Now, I am trying to convert a 32 bit EMF image to JPEG using imagejAPI
> source. The following is the piece of code i am using to convert my EMF
> image. But the output image (image1.jpeg) looks inverted, mirrored and
> skewed. May i know what am I doing wrong here...
> Please note that am not able to share either the original EMF image or the
> image zipped, since the filter rejects my message saying that the
> attachment of that type is not permitted.
>
>
> public class IJUtil {
>
> public static void main(String[] args) throws Exception {
>  File file = new File("D:/xmlword/1324445681526/image1.emf");
>  FileInputStream in = new FileInputStream(file);
>  FileInfo fi = new FileInfo();
>  fi.fileType = 11;
>  fi.width = 1748;
>  fi.height = 1324;
>  fi.pixelDepth = 32;
>  fi.compression = fi.COMPRESSION_UNKNOWN;
>  ImageReader reader = new ImageReader(fi);
>  float[] pix = (float[]) reader.readPixels(in);
>  double[] doublePixels = convertFloatsToDoubles(pix);
>  JpegWriter writer = new JpegWriter();
>  FloatProcessor processor = new FloatProcessor(1748, 1324, doublePixels);
>  Image image = processor.createImage();
>  ImagePlus ip = new ImagePlus("Title", image);
>  writer.saveAsJpeg(ip, "D:/xmlword/1324445681526/image1.jpeg");
> }
>
> public static double[] convertFloatsToDoubles(float[] input) {
>  if (input == null) {
>   return null;
>  }
>  double[] output = new double[input.length];
>  for (int i = 0; i < input.length; i++) {
>   output[i] = input[i];
>  }
>  return output;
> }
> }
>
> Thanks in advance,
>
> Cheers,
> Keerthi G.
> <image1.jpeg>
Reply | Threaded
Open this post in threaded view
|

Re: EMF image conversion.

Keerthi G
Hi,
Adjusting the width helped me in converting the image to a proper output.
Now am trying to convert an image of EMF type which is colored with the
following source code. All i get is an image filled with just black color.
Please help me out.
*

public* *static* *void* main(String[] args) *throws* Exception {

File file = *new* File("D:/GK/test/image4.emf");

FileInputStream in = *new* FileInputStream(file);

FileInfo fi = *new* FileInfo();

fi.fileType = 15;

fi.width = 935;

fi.height = 544;

fi.pixelDepth = 32;

ImageReader reader = *new* ImageReader(fi);

*int*[] obj = reader.readPixels(in);

JpegWriter writer = *new* JpegWriter();

ColorProcessor cProcessor = *new* ColorProcessor(fi.width, fi.height, (obj);

Image colorImage = cProcessor.createImage();

ImagePlus colorIP = *new* ImagePlus("Title", colorImage);

writer.saveAsJpeg(colorIP, "D:/GK/test/color.jpeg");

}
Thanks in advance,
Keerthi G.

On Fri, Dec 23, 2011 at 6:53 PM, Michael Schmid <[hidden email]>wrote:

> Hi Keerthi G.,
>
> the image is skewed like this if the width is wrong by one pixel.
> I would suggest to use variables for width&height, then you need not
> change it in two places.
> As far as I know, ImageJ has no option to flip it vertically while
> importing, you have to flip it afterwards.
>
> By the way, there is no reason to convert the pixels to double[], you can
> use the constructor FloatProcessor(int width, int height, float[] pixels,
> ColorModel cm) with null for the ColorModel.
>
> Michael
> ________________________________________________________________
>  On Dec 22, 2011, at 15:03, Keerthi G wrote:
>
> > Hi,
> > I am tired of finding an open source API to convert my 32 bit EMF image
> and
> > finally found this imagej.
> >
> > Now, I am trying to convert a 32 bit EMF image to JPEG using imagejAPI
> > source. The following is the piece of code i am using to convert my EMF
> > image. But the output image (image1.jpeg) looks inverted, mirrored and
> > skewed. May i know what am I doing wrong here...
> > Please note that am not able to share either the original EMF image or
> the
> > image zipped, since the filter rejects my message saying that the
> > attachment of that type is not permitted.
> >
> >
> > public class IJUtil {
> >
> > public static void main(String[] args) throws Exception {
> >  File file = new File("D:/xmlword/1324445681526/image1.emf");
> >  FileInputStream in = new FileInputStream(file);
> >  FileInfo fi = new FileInfo();
> >  fi.fileType = 11;
> >  fi.width = 1748;
> >  fi.height = 1324;
> >  fi.pixelDepth = 32;
> >  fi.compression = fi.COMPRESSION_UNKNOWN;
> >  ImageReader reader = new ImageReader(fi);
> >  float[] pix = (float[]) reader.readPixels(in);
> >  double[] doublePixels = convertFloatsToDoubles(pix);
> >  JpegWriter writer = new JpegWriter();
> >  FloatProcessor processor = new FloatProcessor(1748, 1324, doublePixels);
> >  Image image = processor.createImage();
> >  ImagePlus ip = new ImagePlus("Title", image);
> >  writer.saveAsJpeg(ip, "D:/xmlword/1324445681526/image1.jpeg");
> > }
> >
> > public static double[] convertFloatsToDoubles(float[] input) {
> >  if (input == null) {
> >   return null;
> >  }
> >  double[] output = new double[input.length];
> >  for (int i = 0; i < input.length; i++) {
> >   output[i] = input[i];
> >  }
> >  return output;
> > }
> > }
> >
> > Thanks in advance,
> >
> > Cheers,
> > Keerthi G.
> > <image1.jpeg>
>