Discussion:
16-bit gray PNG write broken?
G. Jehle
2008-09-09 17:48:02 UTC
Permalink
Hi everyone,

I'm running ImageJ 1.39u on Ubuntu GNU/Linux (x86_64).

Whenever I try to save a simple '16-bit grayscale' image
as PNG, ImageJ saves a '8-bit/color RGB' image instead.

I generated a 16-bit gray PNG using Matlab.
Opening the PNG in ImageJ; works just fine with ImageJ
recognizing the file as 16-bit gray, all values look ok.

However, using 'File > Save As > PNG...' on the very
file just opened results in a '8-bit/color RGB' PNG to be
saved.

Any ideas to why this might happen?

//
Best regards,

G. Jehle

HTWG Konstanz - University of Applied Sciences
Fakultät Informatik

HTWG Konstanz
Fakultät Informatik
Brauneggerstraße 55
D-78462 Konstanz
www.htwg-konstanz.de
Wayne Rasband
2008-09-09 19:10:05 UTC
Permalink
Post by G. Jehle
Hi everyone,
I'm running ImageJ 1.39u on Ubuntu GNU/Linux (x86_64).
Whenever I try to save a simple '16-bit grayscale' image
as PNG, ImageJ saves a '8-bit/color RGB' image instead.
I generated a 16-bit gray PNG using Matlab.
Opening the PNG in ImageJ; works just fine with ImageJ
recognizing the file as 16-bit gray, all values look ok.
However, using 'File > Save As > PNG...' on the very
file just opened results in a '8-bit/color RGB' PNG to be
saved.
Any ideas to why this might happen?
ImageJ 1.41d and later saves 16-bit and 32-bit images as 8-bit PNGs.
Earlier versions saved as RGB. ImageJ converts to 8-bits when saving in
PNG format so that the images are more likely to display correctly in
other applications. Save in TIFF format if you need to preserve 16-bit
or 32-bit values.

-wayne
G. Jehle
2008-09-09 19:22:40 UTC
Permalink
Post by Wayne Rasband
Post by G. Jehle
Hi everyone,
I'm running ImageJ 1.39u on Ubuntu GNU/Linux (x86_64).
Whenever I try to save a simple '16-bit grayscale' image
as PNG, ImageJ saves a '8-bit/color RGB' image instead.
I generated a 16-bit gray PNG using Matlab.
Opening the PNG in ImageJ; works just fine with ImageJ
recognizing the file as 16-bit gray, all values look ok.
However, using 'File > Save As > PNG...' on the very
file just opened results in a '8-bit/color RGB' PNG to be
saved.
Any ideas to why this might happen?
ImageJ 1.41d and later saves 16-bit and 32-bit images as 8-bit PNGs.
Earlier versions saved as RGB. ImageJ converts to 8-bits when saving in
PNG format so that the images are more likely to display correctly in
other applications. Save in TIFF format if you need to preserve 16-bit
or 32-bit values.
Thanks for the info wayne.

Although I have to say that, IMHO, this is bad.
Changing bit depth without giving a warning, even more so
in a case where the image format actually supports the higher
bit depth is very, very misleading and a source for error and frustration.

Are there any considerations for future versions of ImageJ
to make this behavior optional?
I'd really like to use 16-bit grayscale PNG.

Where can I submit a bug report? (I'm serious)
Post by Wayne Rasband
-wayne
//
Best regards,

G. Jehle

HTWG Konstanz - University of Applied Sciences
Fakultät Informatik

HTWG Konstanz
Fakultät Informatik
Brauneggerstraße 55
D-78462 Konstanz
www.htwg-konstanz.de
Wayne Rasband
2008-09-09 19:51:51 UTC
Permalink
Here is a plugin that saves a 16-bit image as a 16-bit PNG.

-wayne

import ij.plugin.*;
import ij.*;
import ij.io.*;
import ij.process.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
/** Saves a 16-bit image in PNG format. */
public class PNG16_Writer implements PlugIn {
public void run(String path) {
ImagePlus imp = IJ.getImage();
if (imp.getBitDepth()!=16)
{IJ.error("16-bit image reqired"); return;}
SaveDialog sd = new SaveDialog("Save as PNG...",
imp.getTitle(), ".png");
String name = sd.getFileName();
if (name==null) return;
String dir = sd.getDirectory();
try {
ShortProcessor sp = (ShortProcessor )imp.getProcessor();
BufferedImage bi = sp.get16BitBufferedImage();
ImageIO.write(bi, "png", new File( dir+name));
} catch (Exception e) {
IJ.showMessage("PNG16 Writer", "An error occured writing
the file.\n \n" + e);
}
}
}
Post by G. Jehle
Post by Wayne Rasband
Post by G. Jehle
Hi everyone,
I'm running ImageJ 1.39u on Ubuntu GNU/Linux (x86_64).
Whenever I try to save a simple '16-bit grayscale' image
as PNG, ImageJ saves a '8-bit/color RGB' image instead.
I generated a 16-bit gray PNG using Matlab.
Opening the PNG in ImageJ; works just fine with ImageJ
recognizing the file as 16-bit gray, all values look ok.
However, using 'File > Save As > PNG...' on the very
file just opened results in a '8-bit/color RGB' PNG to be
saved.
Any ideas to why this might happen?
ImageJ 1.41d and later saves 16-bit and 32-bit images as 8-bit PNGs.
Earlier versions saved as RGB. ImageJ converts to 8-bits when saving in
PNG format so that the images are more likely to display correctly in
other applications. Save in TIFF format if you need to preserve 16-bit
or 32-bit values.
Thanks for the info wayne.
Although I have to say that, IMHO, this is bad.
Changing bit depth without giving a warning, even more so
in a case where the image format actually supports the higher
bit depth is very, very misleading and a source for error and
frustration.
Are there any considerations for future versions of ImageJ
to make this behavior optional?
I'd really like to use 16-bit grayscale PNG.
Where can I submit a bug report? (I'm serious)
Post by Wayne Rasband
-wayne
//
Best regards,
G. Jehle
HTWG Konstanz - University of Applied Sciences
Fakultät Informatik
HTWG Konstanz
Fakultät Informatik
Brauneggerstraße 55
D-78462 Konstanz
www.htwg-konstanz.de
G. Jehle
2008-09-09 20:24:26 UTC
Permalink
Post by Wayne Rasband
Here is a plugin that saves a 16-bit image as a 16-bit PNG.
-wayne
I bow to you in awe and thankfulness!

Thanks a lot for the extremely quick workaround.

//
Regards,
Gregor
Post by Wayne Rasband
import ij.plugin.*;
import ij.*;
import ij.io.*;
import ij.process.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
/** Saves a 16-bit image in PNG format. */
public class PNG16_Writer implements PlugIn {
public void run(String path) {
ImagePlus imp = IJ.getImage();
if (imp.getBitDepth()!=16)
{IJ.error("16-bit image reqired"); return;}
SaveDialog sd = new SaveDialog("Save as PNG...",
imp.getTitle(), ".png");
String name = sd.getFileName();
if (name==null) return;
String dir = sd.getDirectory();
try {
ShortProcessor sp = (ShortProcessor )imp.getProcessor();
BufferedImage bi = sp.get16BitBufferedImage();
ImageIO.write(bi, "png", new File( dir+name));
} catch (Exception e) {
IJ.showMessage("PNG16 Writer", "An error occured writing
the file.\n \n" + e);
}
}
}
Post by G. Jehle
Post by Wayne Rasband
Post by G. Jehle
Hi everyone,
I'm running ImageJ 1.39u on Ubuntu GNU/Linux (x86_64).
Whenever I try to save a simple '16-bit grayscale' image
as PNG, ImageJ saves a '8-bit/color RGB' image instead.
I generated a 16-bit gray PNG using Matlab.
Opening the PNG in ImageJ; works just fine with ImageJ
recognizing the file as 16-bit gray, all values look ok.
However, using 'File > Save As > PNG...' on the very
file just opened results in a '8-bit/color RGB' PNG to be
saved.
Any ideas to why this might happen?
ImageJ 1.41d and later saves 16-bit and 32-bit images as 8-bit PNGs.
Earlier versions saved as RGB. ImageJ converts to 8-bits when saving in
PNG format so that the images are more likely to display correctly in
other applications. Save in TIFF format if you need to preserve 16-bit
or 32-bit values.
Thanks for the info wayne.
Although I have to say that, IMHO, this is bad.
Changing bit depth without giving a warning, even more so
in a case where the image format actually supports the higher
bit depth is very, very misleading and a source for error and frustration.
Are there any considerations for future versions of ImageJ
to make this behavior optional?
I'd really like to use 16-bit grayscale PNG.
Where can I submit a bug report? (I'm serious)
Post by Wayne Rasband
-wayne
//
Best regards,
G. Jehle
HTWG Konstanz - University of Applied Sciences
Fakultät Informatik
HTWG Konstanz
Fakultät Informatik
Brauneggerstraße 55
D-78462 Konstanz
www.htwg-konstanz.de
Harry Parker
2008-09-09 18:35:17 UTC
Permalink
This is a long standing limitation of ImageJ's built-in PNG writer.

The solution is to install the "Image IO" plugin. This adds the ability to save 16 bit PNG files correctly, as well as other formats such as compressed and tiled TIFF images.

See the Image IO plugin home page at
http://ij-plugins.sourceforge.net/plugins/imageio

and download it from
http://sourceforge.net/project/showfiles.php?group_id=44711&package_id=37687
--
Harry Parker
Senior Imaging Systems Engineer
Digital Imaging Systems, Inc.



----- Original Message ----
From: G. Jehle <gjehle-YkE1h+hO+***@public.gmane.org>
To: IMAGEJ-9srhZJH3/***@public.gmane.org
Sent: Tuesday, September 9, 2008 1:48:02 PM
Subject: 16-bit gray PNG write broken?

Hi everyone,

I'm running ImageJ 1.39u on Ubuntu GNU/Linux (x86_64).

Whenever I try to save a simple '16-bit grayscale' image
as PNG, ImageJ saves a '8-bit/color RGB' image instead.

I generated a 16-bit gray PNG using Matlab.
Opening the PNG in ImageJ; works just fine with ImageJ
recognizing the file as 16-bit gray, all values look ok.

However, using 'File > Save As > PNG...' on the very
file just opened results in a '8-bit/color RGB' PNG to be
saved.

Any ideas to why this might happen?

//
Best regards,

G. Jehle

HTWG Konstanz - University of Applied Sciences
Fakultät Informatik

HTWG Konstanz
Fakultät Informatik
Brauneggerstraße 55
D-78462 Konstanz
www.htwg-konstanz.de
Loading...