Hi Jeroen:
The problem is that the data is unsigned, which you can tell by looking for the attribute
"_unsigned" :
byte image_data(765, 700);
:CLASS = "IMAGE";
:VERSION = "1.2";
:PALETTE = 7144; // long
:_lastModified = "2008-08-07T08:13:33Z";
:_unsigned = "true";
or by calling variable.isUnsigned().
Because Java has no unsigned data type, to handle this transparently we would
have to promote to a short. For efficiency, we decided not to automatically
widen it, since it would double the memory storage. So you have to handle it
yourself.
A simple way for your app is:
byte bval = array.getByte(index);
double dval = v.isUnsigned() ? (double) DataType.unsignedByteToShort(bval) :
(double) bval;
of course, this assumes that the data is a byte, and presumably you'd like your code to work for any type. Really, you'd like any widening operation like Array.getDouble() to handle this, but at the moment, Array doesnt know about unsigned. Ive been considering pushing that info into the Array object for that reason.
If you have any thoughts about an elegant way to do this, they would be welcome.
John
Jeroen van der Vegt wrote:
Hello all,
While implementing the new NetCDF-java 4.0-alpha library, I ran into a
data-value error that seems to exist in 2.2.22 as well. If I read the
HDF5-file I added to this email, data values larger than 127 are
returned as <actual value>-256. It seems there for that the bytes are
interpreted as two’s complement, but they are actually unsigned.
We use this code to convert the HDF5 image to PNG (the computation of
pval removes some information on purpose):
*public* *static* *byte*[] convert(String srcPath,
*double* a, *double* b) *throws* IOException {
NetcdfFile ncfile = NetcdfFile./open/(srcPath);
*try* {
Variable v = ncfile.findVariable("image1/image_data");
Array array = v.read();
*int*[] cmap = *new* *int*[256]; // palette
cmap[0] = 0x00FFFFFF; // transparent and white
*for* (*int* i = 1; i != 256; i++) {
// 1 to 255 renders as (almost) white to black
cmap[i] = 0xFF000000 | ((0xFF - i) * 0x010101);
}
IndexColorModel colorModel = *new* IndexColorModel(8,
cmap.length, cmap, 0, *true*, Transparency./OPAQUE/,
DataBuffer./TYPE_BYTE/);
*int*[] shape = array.getShape();
BufferedImage bi = *new* BufferedImage(shape[1], shape[0],
BufferedImage./TYPE_BYTE_INDEXED/, colorModel);
Index index = array.getIndex();
*for* (*int* y = 0; y < shape[0]; y++) {
*for* (*int* x = 0; x < shape[1]; x++) {
index.set(y, x);
*double* dval = array.getDouble(index);
// Fix for NetCDF returning all values larger than
127 as (value - 256):
*if* (dval < -1) {
dval += 256;
}
*int* pval = (*int*) Math./round/(a * dval + b);
pval = Math./min/(Math./max/(pval, 0), 255);
bi.getRaster().setSample(x, y, 0, pval);
}
}
ByteArrayOutputStream os = *new* ByteArrayOutputStream();
ImageIO./write/(bi, "png", os);
*return* os.toByteArray();
} *finally* {
ncfile.close();
}
}
Is this a bug in the library, a bug in the h5 file, an error on our
side, or something else?
Regards,
Jeroen van der Vegt
System designer
------------------------------------------------------------------------
*Technolution B.V.*
Telephone:
+31(0)182 59 40 00
Fax:
+31(0)182 53 97 36
E-mail:
Jeroen.van.der.Vegt@xxxxxxxxxxxxxxx
<mailto:Jeroen.van.der.Vegt@xxxxxxxxxxxxxxx>
Visit us at:
www.technolution.eu <http://www.technolution.eu>
Mailing address:
P.O. Box 2013 - 2800 BD Gouda - The Netherlands
Address:
Zuidelijk Halfrond 1 - 2801 DD Gouda - The Netherlands
------------------------------------------------------------------------
/This e-mail is intended exclusively for the addressee(s), and may not
be passed on to, or made available for use by any person other than the
addressee(s). Technolution B.V. rules out any and every liability
resulting from any electronic transmission./
------------------------------------------------------------------------
_______________________________________________
netcdf-java mailing list
netcdf-java@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/