View Javadoc

1   /**
2    * This source code file is part of a direct port of Stan Birchfield's implementation
3    * of a Kanade-Lucas-Tomasi feature tracker. The original implementation can be found
4    * here: http://www.ces.clemson.edu/~stb/klt/
5    *
6    * As per the original code, the source code is in the public domain, available
7    * for both commercial and non-commercial use.
8    */
9   package org.openimaj.video.tracking.klt;
10  
11  class IOUtils {
12  	enum StructureType {FEATURE_LIST, FEATURE_HISTORY, FEATURE_TABLE}
13  
14  	static String warning_line = "!!! Warning:  This is a KLT data file. Do not modify below this line !!!\n";
15  	static String binheader_fl = "KLTFL1";
16  	static String binheader_fh = "KLTFH1";
17  	static String binheader_ft = "KLTFT1";
18  
19  	private IOUtils() {}
20  
21  	static String [] setupTxtFormat(
22  			String fmt)		/* Input: format (e.g., %5.1f or %3d) */
23  	{
24  		String format;	/* Output: format (e.g., (%5.1f,%5.1f)=%3d) */
25  		String type;	/* Output: either 'f' or 'd', based on input format */
26  		final int val_width = 5;
27  
28  		/* Parse format */
29  		if (fmt.charAt(0) != '%')
30  			throw new RuntimeException(String.format("(KLTWriteFeatures) Bad Format: %s\n", fmt));
31  
32  		type = fmt.substring(fmt.length()-1);
33  
34  		if (!type.equals("f") && !type.equals("d"))
35  			throw new RuntimeException("(KLTWriteFeatures) Format must end in 'f' or 'd'.");
36  
37  		/* Construct feature format */
38  		format = String.format("(%s,%s)=%%%dd ", fmt, fmt, val_width);
39  
40  		return new String[] {format, type};
41  	}
42  
43  	static String getNhyphens(int n)
44  	{
45  		String s = "";
46  		for (int i = 0 ; i < n ; i++)
47  			s += "-";
48  		return s;
49  	}
50  
51  	static String getInteger(int integer, int width)
52  	{
53  		String fmt;
54  		fmt = String.format("%%%dd", width);
55  		return String.format(fmt, integer);
56  	}
57  
58  	/*********************************************************************
59  	 * _findStringWidth
60  	 *
61  	 * Calculates the length of a string after expansion.  E.g., the
62  	 * length of "(%6.1f)" is eight -- six for the floating-point number,
63  	 * and two for the parentheses.
64  	 */
65  	private static int findStringWidth(String str)
66  	{
67  		int width = 0;
68  		int add;
69  		int maxi = str.length() - 1;
70  		int i = 0;
71  
72  
73  		while (i <= maxi)  {
74  			if (str.charAt(i) == '%')  {
75  
76  				if (Character.isDigit(str.charAt(i+1))) {
77  					//Scanner s = new Scanner(str.substring(i+1));
78  					//add = s.nextInt();
79  					int stop = i+2;
80  					while (stop<=maxi && Character.isDigit(str.charAt(stop))) stop++;
81  					add = Integer.parseInt(str.substring(i+1, stop));
82  					
83  					width += add;
84  					i += 2;
85  					while (!"diouxefgn".contains(str.charAt(i)+"")) {
86  						i++;
87  						if (i > maxi)
88  							throw new RuntimeException(
89  									String.format("(_findStringWidth) Can't determine length of string '%s'", str)
90  							);
91  					}
92  					i++;
93  				} else if (str.charAt(i+1) == 'c')  {
94  					width++;
95  					i += 2;
96  				} else 
97  					throw new RuntimeException(
98  							String.format("(_findStringWidth) Can't determine length of string '%s'", str)
99  					);
100 			} else {
101 				i++;
102 				width++;
103 			}
104 		}
105 
106 		return width;
107 	}
108 
109 	static String getHeader(
110 			String format,
111 			StructureType id,
112 			int nFrames,
113 			int nFeatures,
114 			boolean comments)
115 	{
116 		int width = findStringWidth(format);
117 		String s = "";
118 
119 		assert(id == StructureType.FEATURE_LIST || id == StructureType.FEATURE_HISTORY || id == StructureType.FEATURE_TABLE);
120 
121 		if (comments)  {
122 			s += "Feel free to place comments here.\n\n\n";
123 			s += "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
124 			s += warning_line;
125 			s += "\n";
126 		}
127 		s += "------------------------------\n";
128 		switch (id)  {
129 		case FEATURE_LIST: 		s += "KLT Feature List\n";    break;
130 		case FEATURE_HISTORY: 	s += "KLT Feature History\n"; break;
131 		case FEATURE_TABLE: 	s += "KLT Feature Table\n";   break;
132 		}
133 
134 		s += "------------------------------\n\n";
135 		switch (id)  {
136 		case FEATURE_LIST: 		s += String.format("nFeatures = %d\n\n", nFeatures);    break;
137 		case FEATURE_HISTORY: 	s += String.format("nFrames = %d\n\n", nFrames); 		break;
138 		case FEATURE_TABLE: 	s += String.format("nFrames = %d, nFeatures = %d\n\n", nFrames, nFeatures);   break;
139 		}
140 
141 		switch (id)  {
142 		case FEATURE_LIST: s += "feature | (x,y)=val\n";
143 		s += "--------+-";
144 		s += getNhyphens(width);
145 		s += "\n";   
146 		break;
147 		case FEATURE_HISTORY: s+= "frame | (x,y)=val\n";
148 		s += "------+-";
149 		s += getNhyphens(width);
150 		s += "\n";   
151 		break;
152 		case FEATURE_TABLE: s += "feature |          frame\n";   
153 		s += "        |";   
154 		for (int i = 0 ; i < nFrames ; i++) s += getInteger(i, width);
155 		s += "\n--------+-";   
156 		for (int i = 0 ; i < nFrames ; i++) s += getNhyphens(width);
157 		s += "\n";   
158 		break;
159 		}
160 
161 		return s;
162 	}
163 }