View Javadoc

1   package de.desy.video.sw;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.DataInputStream;
6   import java.io.IOException;
7   import java.io.EOFException;
8   import java.lang.Double;
9   import de.desy.tine.types.IMAGE;
10  
11  /**
12  * <code>CIMMLoader</code> provides loading of oldfashioned VSv2 IMM file format
13  * to Video System v3 TINE IMAGE format type. Main interest was for internal 
14  * checking while developing.<br>
15  * <br>
16  * This class should be treated as rudimentary and quick hack. Parts were copy and pasted
17  * from CBasicImageLoader and it is not meant for production use!
18  *  
19  *    
20  * @author <a href="mailto:stefan.weisse@desy.de">Stefan Weisse</a>
21  * @version $Id: Templates.xml,v 1.10 2008/06/25 14:00:13 sweisse Exp $
22  *
23  */
24  
25  public final class CIMMLoader {
26  
27  	
28  	/**
29  	 * loads an IMM (proprietary, VSV2, raw, greyscale) image file with a give filename 
30  	 * from disk and puts its contents to TINE IMAGE data type. An IMM file can consist 
31  	 * of multiple video frames glued together. In such case, only the first one is loaded!
32  	 * <br>
33  	 * @param aFileName file name (may include path)
34  	 * @param aOutImage TINE IMAGE data type to put image contents to
35  	 * @return true - image contents were successfully put into aOutImage<br>
36  	 * 		   false - some error (aOutImage is null, IOException)
37  	 */
38  	public static final boolean loadImageFile( String aFileName, IMAGE aOutImage)
39  	{
40  		if (aOutImage == null) return false;
41  		
42  		int width;
43  		int height;
44  		int bytesperpixel;
45  		int bitsperpixel;
46  		double scale;
47  		FileInputStream fis=null;
48  		DataInputStream dis=null;
49  		byte[] imagebits = null;
50  		
51  		// read the full file into buffered array
52  		
53  		// read in first four bytes
54  		// read in second four bytes
55  		
56  		// analyse
57  		// check whether file length is big enough
58  		// read in image bits
59  
60  		// read in 8 bytes -> double scale
61  		
62  		width = -1;
63  		height = -1;
64  		bytesperpixel = -1;
65  		bitsperpixel = -1;
66  		scale = -1.0;
67  		
68  		try {
69  			fis = new FileInputStream(aFileName);
70  			dis = new DataInputStream(fis);
71  			
72  			long fsize = fis.getChannel().size();
73  			
74  			width = dis.readUnsignedShort();
75  			width = ((width&0xff)<<8)+(width>>8);
76  			bytesperpixel = dis.readUnsignedShort();
77  			bytesperpixel = ((bytesperpixel&0xff)<<8)+(bytesperpixel>>8);
78  			height = dis.readUnsignedShort();
79  			height = ((height&0xff)<<8)+(height>>8);
80  			bitsperpixel = dis.readUnsignedShort();
81  			bitsperpixel = ((bitsperpixel&0xff)<<8)+(bitsperpixel>>8);
82  			
83  			bytesperpixel /= 8;
84  			if (bytesperpixel == 0) bytesperpixel = 1;
85  			
86  			if ((bitsperpixel == 0) || (bitsperpixel > (8*bytesperpixel)))
87  				bitsperpixel = bytesperpixel*8;
88  			
89  			// now we can read in data, filesize should be bigger than 
90  			// width*height+16
91  			
92  			if (fsize<(width*height*bytesperpixel+16)) throw new EOFException();
93  			
94  			imagebits = new byte[width*height*bytesperpixel];
95  
96  			dis.read( imagebits, 0, imagebits.length);
97  			
98  			byte[] buf = new byte[8];
99  						
100 			dis.read(buf, 0, buf.length);
101 			
102 			int off=0;
103 			long scale_long = (((long)buf[off+0])&0xFF)|((((long)buf[off+1])&0xFF)<<8)|
104 			((((long)buf[off+2])&0xFF)<<16)|((((long)buf[off+3])&0xFF)<<24)| 
105 			((((long)buf[off+4])&0xFF)<<32)|((((long)buf[off+5])&0xFF)<<40)|
106 			((((long)buf[off+6])&0xFF)<<48)|((((long)buf[off+7])&0xFF)<<56);
107 
108 			scale = Double.longBitsToDouble(scale_long);
109 		}
110 		catch (EOFException eof) {
111 			//System.out.println( "EOF reached " );
112 			return false;
113 		}
114 		catch (IOException ioe) {
115 			//System.out.println( "IO error: " + ioe );
116 			return false;
117 		}   
118 		finally {
119 			try { if (dis != null) dis.close(); } catch( IOException ex) {}
120 			try { if (fis != null) fis.close(); } catch( IOException ex) {}
121 			dis = null;
122 			fis = null;
123 		}
124 		
125 		IMAGE.FrameHeader frameHeader = aOutImage.getFrameHeader();
126 		IMAGE.SourceHeader sourceHeader = aOutImage.getSourceHeader();
127 
128 		frameHeader.bytesPerPixel = bytesperpixel;
129 		frameHeader.effectiveBitsPerPixel = bitsperpixel;
130 		frameHeader.imageFormat = (int) CVideoHeader3.CF_IMAGE_FORMAT_GRAY;
131 		frameHeader.sourceFormat = (int) CVideoHeader3.CF_IMAGE_FORMAT_GRAY;
132 	
133 		frameHeader.appendedFrameSize = width*height*frameHeader.bytesPerPixel;
134 		
135 		frameHeader.aoiHeight = -1;
136 		frameHeader.aoiWidth = -1;
137 
138 		frameHeader.eventNumber = 0;
139 		frameHeader.frameNumber = 0;
140 		frameHeader.fspare1 = (float) -1.0;
141 		frameHeader.fspare2 = (float) -1.0;
142 		frameHeader.fspare3 = (float) -1.0;
143 		frameHeader.horizontalBinning = 0;
144 		frameHeader.imageFlags = (int) CVideoHeader3.CF_IMAGE_FLAG_IMAGE_LOSSLESS | (int) CVideoHeader3.CF_IMAGE_FLAG_LITTLE_ENDIAN_BYTE_ORDER;
145 		
146 		frameHeader.imageRotation = (float) 0.0;
147 		frameHeader.ispare1 = -1;
148 		frameHeader.ispare2 = -1;
149 		frameHeader.ispare3 = -1;
150 		frameHeader.sourceHeight = height;
151 		frameHeader.sourceWidth = width;
152 		frameHeader.verticalBinning = 0;
153 		frameHeader.xScale = (float) scale;
154 		frameHeader.xStart = 0;
155 		frameHeader.yScale = (float) scale;
156 		frameHeader.yStart = 0;
157 		
158 		File imgFile =  new File(aFileName);
159 		
160 		sourceHeader.baseTag = (int) CVideoHeader3.CF_IMAGE_MAGIC_01;
161 		sourceHeader.cameraPortName = "file://"+imgFile.getName(); // TODO: could be too long and truncated badly
162 		sourceHeader.cameraPortId = (int) CVideoHeader3.CF_IMAGE_NO_CAMERA_PORT_ID;
163 
164 		long msTimeEpoch = imgFile.lastModified();
165 		sourceHeader.timestampMicroseconds = (int) (((msTimeEpoch%((long)1000))*((long)1000)));
166 		sourceHeader.timestampSeconds = (int) (((msTimeEpoch)/((long)1000)));
167 
168 		sourceHeader.totalLength = CVideoHeader3.HDRSIZE + frameHeader.appendedFrameSize;
169 		sourceHeader.versionTag = CVideoHeader3.CF_IMAGE_VERSION;
170 		
171 		aOutImage.setImageFrameBuffer(imagebits);
172 		
173 		return true;
174 	}
175 	
176 }