private void analyzeEdfFile() { if (!analyzed) { if (referenceFile != null) { HashMap<String, String> headerMap = new HashMap<String, String>(); try { FileInputStream fis = new FileInputStream(referenceFile.getAbsolutePath()); DataInputStream dis = new DataInputStream(fis); int character = -1; StringBuffer headerBuffer = new StringBuffer(); boolean newLine = false; while (true) { try { character = dis.read(); if (character == -1) { break; } headerBuffer.append((char) character); if (character == '\n') { newLine = true; } else { if ((character == '}') && newLine) { character = dis.read(); break; } newLine = false; } } catch (IOException e) { character = -1; break; } } if (character != -1) { char toCheck = headerBuffer.charAt(0); while (Character.isWhitespace(toCheck) || (toCheck == '{')) { headerBuffer.delete(0, 1); toCheck = headerBuffer.charAt(0); } toCheck = headerBuffer.charAt(headerBuffer.length() - 1); while (Character.isWhitespace(toCheck) || (toCheck == '}')) { headerBuffer.delete(headerBuffer.length() - 1, headerBuffer.length()); toCheck = headerBuffer.charAt(headerBuffer.length() - 1); } String[] lines = headerBuffer.toString().split(";"); for (String line : lines) { int separatorIndex = line.lastIndexOf('='); if (separatorIndex > -1) { headerMap.put( line.substring(0, separatorIndex).trim(), line.substring(separatorIndex + 1).trim()); } } readImageFromFile(headerMap, dis); // Cleaning keys bound to image headerMap.remove("ByteOrder"); headerMap.remove("Dim_1"); headerMap.remove("Dim_2"); headerMap.remove("DataType"); // Avoid having another item with the same name as image item headerMap.remove("Image"); // Other DataItems HashMap<String, EdfGroup> subGroupMap = new HashMap<String, EdfGroup>(); for (String key : headerMap.keySet()) { int openIndex = key.lastIndexOf('('); int closeIndex = key.lastIndexOf(')'); if ((openIndex > -1) && (closeIndex > openIndex)) { // Group items by name String groupName = key.substring(openIndex + 1, closeIndex); EdfGroup subGroup = subGroupMap.get(groupName); if (subGroup == null) { subGroup = new EdfGroup(null); subGroup.setName(groupName); subGroup.setShortName(groupName); subGroup.setAnalyzed(true); addSubgroup(subGroup); subGroupMap.put(groupName, subGroup); } String itemName = key.substring(0, openIndex).replaceAll("=", "").trim(); subGroup.addDataItem(buildDataItem(itemName, headerMap.get(key))); } else { // Build simple dataItem addDataItem(buildDataItem(key, headerMap.get(key))); } } subGroupMap.clear(); headerMap.clear(); dis.close(); } } catch (FileNotFoundException e) { // Just ignore this case } catch (IOException e) { // Just ignore this case } } } analyzed = true; }
private void readImageFromFile(HashMap<String, String> headerMap, DataInputStream dis) { // Image Recovery try { boolean littleEndian = "LowByteFirst".equals(headerMap.get("ByteOrder")); int dimX = Integer.valueOf(headerMap.get("Dim_1")); int dimY = Integer.valueOf(headerMap.get("Dim_2")); String dataType = headerMap.get("DataType"); int y = 0, x = 0; Object imageValue = null; boolean unsigned = dataType.startsWith("Unsigned"); if ("SignedByte".equals(dataType)) { imageValue = new byte[dimY][dimX]; byte[][] arrayImageValue = (byte[][]) imageValue; while (y < dimY) { int read = dis.read(); if (read == -1) { imageValue = null; break; } else { arrayImageValue[y][x] = (byte) read; x++; if (x >= dimX) { x = 0; y++; } } } } else if ("UnsignedByte".equals(dataType)) { imageValue = new short[dimY][dimX]; short[][] arrayImageValue = (short[][]) imageValue; while (y < dimY) { int read = dis.read(); if (read == -1) { imageValue = null; break; } else { arrayImageValue[y][x] = (short) read; x++; if (x >= dimX) { x = 0; y++; } } } } else if ("UnsignedShort".equals(dataType) || "SignedInteger".equals(dataType)) { // Specific imageValue = new int[dimY][dimX]; // short = 2 bytes // int = 4 bytes int factor = (unsigned) ? 2 : 4; int sizeToRead = factor * dimY * dimX; // Global Object flatImageValue = null; ByteBuffer byteBuffer = EdfFileReader.readAsBytes(sizeToRead, littleEndian, dis); // Specific if ("UnsignedShort".equals(dataType)) { flatImageValue = new short[dimY * dimX]; ShortBuffer shortBuffer = byteBuffer.asShortBuffer(); shortBuffer.get((short[]) flatImageValue); } else { flatImageValue = new int[dimY * dimX]; IntBuffer integerBuffer = byteBuffer.asIntBuffer(); integerBuffer.get((int[]) flatImageValue); } int globalIndex = 0; for (int yIndex = 0; yIndex < dimY; yIndex++) { for (int xIndex = 0; xIndex < dimX; xIndex++) { int value = Array.getInt(flatImageValue, globalIndex); value = (unsigned) ? value & 0xffff : value; ((int[][]) imageValue)[yIndex][xIndex] = value; globalIndex++; } } } else if ("SignedLong".equals(dataType) || "UnsignedInteger".equals(dataType)) { // Specific imageValue = new long[dimY][dimX]; // long = 8 bytes // int = 4 bytes int factor = (unsigned) ? 4 : 8; int sizeToRead = factor * dimY * dimX; // Global Object flatImageValue = null; ByteBuffer byteBuffer = EdfFileReader.readAsBytes(sizeToRead, littleEndian, dis); // Specific if ("UnsignedInteger".equals(dataType)) { flatImageValue = new int[dimY * dimX]; IntBuffer integerBuffer = byteBuffer.asIntBuffer(); integerBuffer.get((int[]) flatImageValue); } else { flatImageValue = new long[dimY * dimX]; LongBuffer longBuffer = byteBuffer.asLongBuffer(); longBuffer.get((long[]) flatImageValue); } int globalIndex = 0; for (int yIndex = 0; yIndex < dimY; yIndex++) { for (int xIndex = 0; xIndex < dimX; xIndex++) { long value = Array.getLong(flatImageValue, globalIndex); value = (unsigned) ? value & 0xFFFFFFFFL : value; ((long[][]) imageValue)[yIndex][xIndex] = value; globalIndex++; } } } else if ("Signed64".equals(dataType)) { throw new NotImplementedException(); } else if ("Unsigned64".equals(dataType)) { unsigned = true; throw new NotImplementedException(); } else if ("FloatValue".equals(dataType)) { throw new NotImplementedException(); } else if ("DoubleValue".equals(dataType)) { throw new NotImplementedException(); } if (imageValue != null) { EdfDataItem imageDataItem = new EdfDataItem("Image", new DefaultArrayMatrix(EdfFactory.NAME, imageValue), unsigned); addDataItem(imageDataItem); } } catch (Exception e) { e.printStackTrace(); // ignore exceptions for Image Recovery } }