| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard, |
| * Regent L'Archeveque, |
| <<<<<<< HEAD |
| * Sebastien Gemme |
| * |
| ======= |
| * Sebastien Gemme - initial API and implementation |
| * |
| >>>>>>> refs/heads/eclipse_pa |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.geometry.data3d.pif; |
| |
| import java.io.BufferedInputStream; |
| import java.io.DataInputStream; |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import javax.vecmath.Color4b; |
| import javax.vecmath.Matrix4d; |
| import javax.vecmath.Point3f; |
| |
| /** |
| * @author SGemme |
| * |
| * TODO To change the template for this generated type comment go to |
| * Window - Preferences - Java - Code Style - Code Templates |
| */ |
| public class PifReader { |
| |
| // Point3f. |
| private List<Point3f> points; |
| |
| private DataInputStream din = null; |
| |
| private PifHeader header = null; |
| |
| // Colors, 4 bytes. Class Color4b. |
| private List<Color4b> colors = null; |
| |
| public PifReader(String fileName) throws IOException { |
| |
| FileInputStream fis = new FileInputStream(new File(fileName)); |
| BufferedInputStream is = new BufferedInputStream(fis); |
| |
| this.din = new DataInputStream(is); |
| |
| processData(); |
| |
| } |
| |
| private void processData() throws IOException { |
| readHeader(); |
| readPoints(); |
| readColors(); |
| } |
| |
| public PifReader(InputStream in) throws IOException { |
| |
| BufferedInputStream is = new BufferedInputStream(in); |
| |
| this.din = new DataInputStream(is); |
| |
| processData(); |
| } |
| |
| private void readColors() throws IOException { |
| |
| this.colors = new ArrayList<Color4b>(this.header.getArrayHeight() * this.header.getArrayWidth()); |
| |
| System.out.println("Image flag: " + this.header.getImageColorFlag()); |
| |
| // A color flag of 0 means there is no color information. |
| if (this.header.getImageColorFlag() != 0) { |
| |
| // We read the colors. |
| for (int i = 0; i < this.header.getArrayHeight() * this.header.getArrayWidth(); i++) { |
| |
| // We read the color. |
| Color4b nextColor = null; |
| |
| byte r, g, b, a; |
| |
| r = g = b = a = 0; |
| |
| switch (this.header.getImageColorFlag()) { |
| case 1: |
| byte value = this.din.readByte(); |
| r = g = b = value; |
| a = 1; |
| break; |
| case 3: |
| r = this.din.readByte(); |
| g = this.din.readByte(); |
| b = this.din.readByte(); |
| a = 1; |
| break; |
| case 4: |
| r = this.din.readByte(); |
| g = this.din.readByte(); |
| b = this.din.readByte(); |
| a = this.din.readByte(); |
| break; |
| default: |
| System.err.println("Invalid image color flag"); |
| break; |
| |
| } |
| |
| nextColor = new Color4b(r, g, b, a); |
| |
| this.colors.add(nextColor); |
| |
| } |
| |
| } |
| |
| } |
| |
| private void readHeader() throws IOException { |
| |
| this.header = new PifHeader(); |
| |
| // We read all the fields. |
| |
| // Format version. |
| byte[] buffer = new byte[PifHeader.FORMAT_VERSION_SIZE]; |
| this.din.readFully(buffer, 0, PifHeader.FORMAT_VERSION_SIZE); |
| |
| this.header.setFormatVersion(new String(buffer)); |
| |
| // The user_comments. |
| buffer = new byte[PifHeader.FORMAT_USER_COMMENTS_SIZE]; |
| this.din.readFully(buffer, 0, PifHeader.FORMAT_USER_COMMENTS_SIZE); |
| |
| this.header.setUserComments(new String(buffer)); |
| |
| // The dummy1 field. |
| buffer = new byte[PifHeader.DUMMY1_SIZE]; |
| |
| this.din.readFully(buffer, 0, PifHeader.DUMMY1_SIZE); |
| |
| this.header.setDummy1(new String(buffer)); |
| |
| // We get the image param flag. |
| int imageParamFlag = this.din.readInt(); |
| |
| this.header.setImageParamFlag(imageParamFlag); |
| |
| // We get the image data type. |
| int imageDataType = this.din.readInt(); |
| |
| this.header.setImageDataType(imageDataType); |
| |
| // We get the invalid point. |
| float invalidPoint = this.din.readFloat(); |
| |
| this.header.setInvalidPoint(invalidPoint); |
| |
| // Array width. |
| int arrayWidth = this.din.readInt(); |
| |
| this.header.setArrayWidth(arrayWidth); |
| |
| // Array height. |
| int arrayHeight = this.din.readInt(); |
| |
| this.header.setArrayHeight(arrayHeight); |
| |
| // Data block length. |
| int dataBlockLength = this.din.readInt(); |
| |
| this.header.setDataBlockLength(dataBlockLength); |
| |
| // Scale flag. |
| int scaleFlag = this.din.readInt(); |
| |
| this.header.setScaleFlag(scaleFlag); |
| |
| // scaling in i. |
| float iScale = this.din.readFloat(); |
| |
| this.header.setIScale(iScale); |
| |
| // scaling in i. |
| float jScale = this.din.readFloat(); |
| |
| this.header.setJScale(jScale); |
| |
| // transfoMatrixFlag |
| int transfoMatrixFlag = this.din.readInt(); |
| |
| this.header.setTransfoMatrixFlag(transfoMatrixFlag); |
| |
| // The transformation matrix. |
| double[] transMatrixData = new double[16]; |
| |
| for (int i = 0; i < transMatrixData.length; i++) { |
| transMatrixData[i] = this.din.readDouble(); |
| } |
| |
| Matrix4d transMatrix = new Matrix4d(transMatrixData); |
| |
| this.header.setTransfoMatrix(transMatrix); |
| |
| // Image color flag. |
| int imageColorFlag = this.din.readInt(); |
| |
| this.header.setImageColorFlag(imageColorFlag); |
| |
| // Color block length. |
| int colorBlockLength = this.din.readInt(); |
| |
| this.header.setColorBlockLength(colorBlockLength); |
| |
| // Camera position flag. |
| int cameraPositionFlag = this.din.readInt(); |
| |
| this.header.setCameraPositionFlag(cameraPositionFlag); |
| |
| // Camera x,y and z. |
| float cameraX = this.din.readFloat(); |
| |
| this.header.setCameraX(cameraX); |
| |
| float cameraY = this.din.readFloat(); |
| |
| this.header.setCameraY(cameraY); |
| |
| float cameraZ = this.din.readFloat(); |
| |
| this.header.setCameraZ(cameraZ); |
| |
| // Dummy 2. |
| int[] dummy2 = new int[PifHeader.DUMMY2_SIZE]; |
| |
| for (int i = 0; i < PifHeader.DUMMY2_SIZE; i++) { |
| dummy2[i] = this.din.readInt(); |
| } |
| |
| this.header.setDummy2(dummy2); |
| |
| } |
| |
| private void readPoints() throws IOException { |
| this.points = new ArrayList<Point3f>(this.header.getArrayHeight() * this.header.getArrayWidth()); |
| |
| // We read the points. |
| if (this.header.getImageDataType() == 1) { |
| |
| float[] pointData = new float[3]; |
| |
| for (int i = 0; i < this.header.getArrayHeight(); i++) { |
| |
| for (int j = 0; j < this.header.getArrayWidth(); j++) { |
| |
| for (int k = 0; k < 3; k++) { |
| |
| pointData[k] = this.din.readFloat(); |
| |
| } |
| |
| this.points.add(new Point3f(pointData)); |
| |
| } |
| |
| } |
| |
| } else { |
| System.err.println("Only data type 1 is supported at this moment."); |
| } |
| |
| } |
| |
| /** |
| * @return Returns the header. |
| */ |
| public PifHeader getHeader() { |
| return this.header; |
| } |
| |
| /** |
| * @return Returns the points. |
| */ |
| public List<Point3f> getPoints() { |
| return this.points; |
| } |
| |
| public List<Color4b> getColors() { |
| return this.colors; |
| } |
| } |