[netcdf-java] Bug in HDF parser?

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


Visit us at:

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.

 

Attachment: RAD_NL25_PCP_NA_200808070810.zip
Description: Zip compressed data

  • 2008 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: