/** * Read data from encoded values and run len into regular data array * * @param ddata is encoded data values * @return the data array of row data */ public byte[] readOneRowData(byte[] ddata, int rLen, int xt) throws IOException, InvalidRangeException { int run; byte[] bdata = new byte[xt]; int nbin = 0; int total = 0; for (run = 0; run < rLen; run++) { int drun = DataType.unsignedByteToShort(ddata[run]) >> 4; byte dcode1 = (byte) (DataType.unsignedByteToShort(ddata[run]) & 0Xf); for (int i = 0; i < drun; i++) { bdata[nbin++] = dcode1; total++; } } if (total < xt) { for (run = total; run < xt; run++) { bdata[run] = 0; } } return bdata; }
int getInt(byte[] b, int offset, int num) { int base = 1; int i; int word = 0; int bv[] = new int[num]; for (i = 0; i < num; i++) { bv[i] = DataType.unsignedByteToShort(b[offset + i]); } if (bv[0] > 127) { bv[0] -= 128; base = -1; } /* * Calculate the integer value of the byte sequence */ for (i = num - 1; i >= 0; i--) { word += base * bv[i]; base *= 256; } return word; }
private void convertScaleOffsetUnsignedByte(IndexIterator iterIn, IndexIterator iterOut) { boolean checkMissing = useNaNs && hasMissing(); while (iterIn.hasNext()) { byte valb = iterIn.getByteNext(); double val = scale * DataType.unsignedByteToShort(valb) + offset; iterOut.setDoubleNext(checkMissing && isMissing_(val) ? Double.NaN : val); } }
public double convertScaleOffsetMissing(byte valb) { if (!hasScaleOffset) return useNaNs && isMissing((double) valb) ? Double.NaN : (double) valb; double convertedValue; if (isUnsigned) convertedValue = scale * DataType.unsignedByteToShort(valb) + offset; else convertedValue = scale * valb + offset; return useNaNs && isMissing(convertedValue) ? Double.NaN : convertedValue; }
// all the work is here, so can be called recursively public Object readOneScanData(ByteBuffer bos, NOWRadheader.Vinfo vinfo, String vName) throws IOException, InvalidRangeException { int doff = (int) vinfo.hoff; int npixel = vinfo.yt * vinfo.xt; byte[] rdata = null; byte[] ldata = new byte[vinfo.xt]; byte[] pdata = new byte[npixel]; byte[] b2 = new byte[2]; bos.position(doff); // begining of image data if ((DataType.unsignedByteToShort(bos.get()) != 0xF0) || (bos.get() != 0x0C)) { return null; } int ecode; int color; int datapos; int offset = 0; int roffset = 0; boolean newline = true; int linenum = 0; while (true) { // line number if (newline) { bos.get(b2); linenum = (DataType.unsignedByteToShort(b2[1]) << 8) + DataType.unsignedByteToShort(b2[0]); // System.out.println("Line Number = " + linenum); } // int linenum = bytesToInt(b2[0], b2[1], true); // System.out.println("Line Number = " + linenum); // if(linenum == 1225) // System.out.println(" HHHHH"); short b = DataType.unsignedByteToShort(bos.get()); color = b & 0xF; ecode = b >> 4; datapos = bos.position(); int datarun; if (ecode == 0xF) { byte bb1 = bos.get(datapos - 2); byte bb2 = bos.get(datapos); if ((color == 0x0) && (bb1 == 0x00) && (bb2 == 0x00)) { datapos += 1; } bos.position(datapos); datarun = 0; } else if (ecode == 0xE) { byte b0 = bos.get(datapos); datarun = DataType.unsignedByteToShort(b0) + 1; datapos += 1; bos.position(datapos); } else if (ecode == 0xD) { b2[0] = bos.get(datapos); b2[1] = bos.get(datapos + 1); datarun = (DataType.unsignedByteToShort(b2[1]) << 8) + DataType.unsignedByteToShort(b2[0]) + 1; datapos += 2; bos.position(datapos); } else { datarun = ecode + 1; } // move the unpacked data in the data line rdata = new byte[datarun]; for (int i = 0; i < datarun; i++) { rdata[i] = (byte) color; } System.arraycopy(rdata, 0, ldata, roffset, datarun); roffset = roffset + datarun; // System.out.println("run ecode = " + ecode + " and data run " + datarun + " and totalrun " + // roffset); // check to see if the beginning of the next line or at the end of the file short c0 = DataType.unsignedByteToShort(bos.get()); if (c0 == 0x00) { short c1 = DataType.unsignedByteToShort(bos.get()); short c2 = DataType.unsignedByteToShort(bos.get()); // System.out.println("c1 and c2 " + c1 + " " + c2); if ((c0 == 0x00) && (c1 == 0xF0) && (c2 == 0x0C)) { // beginning of next line // System.out.println("linenum " + linenum + " and this line total " + roffset); // if (roffset != 3661) { // System.out.println("ERROR missing data, this line total only " + roffset); // } System.arraycopy(ldata, 0, pdata, offset, roffset); offset = offset + vinfo.xt; roffset = 0; newline = true; ldata = new byte[vinfo.xt]; } else if ((c1 == 0xF0) && (c2 == 0x02)) { // end of the file break; } else { datapos = bos.position() - 3; bos.position(datapos); newline = false; } } else { newline = false; datapos = bos.position(); bos.position(datapos - 1); } } return pdata; }