001/**
002 * FaceTracker Licence
003 * -------------------
004 * (Academic, non-commercial, not-for-profit licence)
005 *
006 * Copyright (c) 2010 Jason Mora Saragih
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without
010 * modification, are permitted provided that the following conditions are met:
011 *
012 *     * The software is provided under the terms of this licence stricly for
013 *       academic, non-commercial, not-for-profit purposes.
014 *     * Redistributions of source code must retain the above copyright notice,
015 *       this list of conditions (licence) and the following disclaimer.
016 *     * Redistributions in binary form must reproduce the above copyright
017 *       notice, this list of conditions (licence) and the following disclaimer
018 *       in the documentation and/or other materials provided with the
019 *       distribution.
020 *     * The name of the author may not be used to endorse or promote products
021 *       derived from this software without specific prior written permission.
022 *     * As this software depends on other libraries, the user must adhere to and
023 *       keep in place any licencing terms of those libraries.
024 *     * Any publications arising from the use of this software, including but
025 *       not limited to academic journal and conference publications, technical
026 *       reports and manuals, must cite the following work:
027 *
028 *       J. M. Saragih, S. Lucey, and J. F. Cohn. Face Alignment through Subspace
029 *       Constrained Mean-Shifts. International Journal of Computer Vision
030 *       (ICCV), September, 2009.
031 *
032 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
033 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
034 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
035 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
036 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
037 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
038 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
039 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
040 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
041 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042 */
043package com.jsaragih;
044
045import java.io.BufferedWriter;
046import java.io.FileInputStream;
047import java.io.FileNotFoundException;
048import java.io.IOException;
049import java.io.InputStream;
050import java.util.Locale;
051import java.util.Scanner;
052
053import org.openimaj.image.FImage;
054
055import Jama.Matrix;
056
057/**
058 * IO Utilities
059 * 
060 * @author Jason Mora Saragih
061 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
062 */
063public class IO {
064        static {
065                Tracker.init();
066        }
067
068        /**
069         * Types of object
070         */
071        public enum Types {
072                /** PDM */
073                PDM,
074                /** PAW */
075                PAW,
076                /** PATCH */
077                PATCH,
078                /** MPATCH */
079                MPATCH,
080                /** CLM */
081                CLM,
082                /** FDET */
083                FDET,
084                /** FCHECK */
085                FCHECK,
086                /** MFCHECK */
087                MFCHECK,
088                /** TRACKER */
089                TRACKER
090        };
091
092        /**
093         * Read a matrix
094         * 
095         * @param s
096         * @return the matrix
097         */
098        public static Matrix readMat(Scanner s) {
099                s.useLocale(Locale.UK);
100
101                final int r = s.nextInt();
102                final int c = s.nextInt();
103                s.nextInt(); // types are ignored
104
105                final Matrix M = new Matrix(r, c);
106                final double[][] Mv = M.getArray();
107
108                for (int rr = 0; rr < r; rr++)
109                        for (int cc = 0; cc < c; cc++)
110                                Mv[rr][cc] = s.nextDouble();
111
112                return M;
113        }
114
115        static void writeMat(BufferedWriter s, Matrix M) throws IOException {
116                final int r = M.getRowDimension();
117                final int c = M.getColumnDimension();
118
119                s.write(r + " " + c + " 0"); // type always 0 for java version as its
120                                                                                // ignored
121
122                final double[][] Mv = M.getArray();
123                for (int rr = 0; rr < r; rr++)
124                        for (int cc = 0; cc < c; cc++)
125                                s.write(Mv[rr][cc] + " ");
126        }
127
128        static void writeImg(BufferedWriter s, FImage img) throws IOException {
129                final int r = img.height;
130                final int c = img.width;
131
132                s.write(r + " " + c + " 0"); // type always 0 for java version as its
133                                                                                // ignored
134
135                final float[][] Mv = img.pixels;
136                for (int rr = 0; rr < r; rr++)
137                        for (int cc = 0; cc < c; cc++)
138                                s.write(Mv[rr][cc] + " ");
139        }
140
141        static FImage readImg(Scanner s) {
142                s.useLocale(Locale.UK);
143
144                final int r = s.nextInt();
145                final int c = s.nextInt();
146                s.nextInt(); // types are ignored
147
148                final FImage M = new FImage(c, r);
149                final float[][] Mv = M.pixels;
150
151                for (int rr = 0; rr < r; rr++)
152                        for (int cc = 0; cc < c; cc++)
153                                Mv[rr][cc] = s.nextFloat();
154
155                return M;
156        }
157
158        static void writeIntArray(BufferedWriter s, int[][] arr) throws IOException {
159                final int r = arr.length;
160                final int c = arr[0].length;
161
162                s.write(r + " " + c + " 0"); // type always 0 for java version as its
163                                                                                // ignored
164
165                for (int rr = 0; rr < r; rr++)
166                        for (int cc = 0; cc < c; cc++)
167                                s.write(arr[rr][cc] + " ");
168        }
169
170        static int[][] readIntArray(Scanner s) {
171                s.useLocale(Locale.UK);
172
173                final int r = s.nextInt();
174                final int c = s.nextInt();
175                s.nextInt(); // types are ignored
176
177                final int[][] M = new int[r][c];
178                for (int rr = 0; rr < r; rr++)
179                        for (int cc = 0; cc < c; cc++)
180                                M[rr][cc] = s.nextInt();
181
182                return M;
183        }
184
185        static int[][] loadCon(final String fname) throws FileNotFoundException {
186                return loadCon(new FileInputStream(fname));
187        }
188
189        /**
190         * Load connections
191         * 
192         * @param in
193         * @return the connections
194         */
195        public static int[][] loadCon(final InputStream in) {
196                final Scanner s = new Scanner(in);
197                s.useLocale(Locale.UK);
198
199                while (true) {
200                        final String str = s.next();
201                        if ("n_connections:".equals(str))
202                                break;
203                }
204
205                final int n = s.nextInt();
206                final int[][] con = new int[2][n];
207
208                while (true) {
209                        final String c = s.next();
210                        if (c.equals("{"))
211                                break;
212                }
213
214                for (int i = 0; i < n; i++) {
215                        con[0][i] = s.nextInt();
216                        con[1][i] = s.nextInt();
217                }
218                s.close();
219
220                return con;
221        }
222
223        static int[][] loadTri(final String fname) throws FileNotFoundException {
224                return loadTri(new FileInputStream(fname));
225        }
226
227        /**
228         * Load triangles
229         * 
230         * @param in
231         * @return triangles
232         */
233        public static int[][] loadTri(final InputStream in) {
234                final Scanner s = new Scanner(in);
235                s.useLocale(Locale.UK);
236
237                while (true) {
238                        final String str = s.next();
239                        if ("n_tri:".equals(str))
240                                break;
241                }
242
243                final int n = s.nextInt();
244                final int[][] tri = new int[n][3];
245
246                while (true) {
247                        final String c = s.next();
248                        if (c.equals("{"))
249                                break;
250                }
251
252                for (int i = 0; i < n; i++) {
253                        tri[i][0] = s.nextInt();
254                        tri[i][1] = s.nextInt();
255                        tri[i][2] = s.nextInt();
256                }
257                s.close();
258
259                return tri;
260        }
261
262        static FImage readImgByte(Scanner s) {
263                s.useLocale(Locale.UK);
264
265                final int r = s.nextInt();
266                final int c = s.nextInt();
267                s.nextInt(); // types are ignored
268
269                final FImage M = new FImage(c, r);
270                final float[][] Mv = M.pixels;
271
272                for (int rr = 0; rr < r; rr++)
273                        for (int cc = 0; cc < c; cc++)
274                                Mv[rr][cc] = s.next("[^ ]").codePointAt(0); // Integer.parseInt(s.next());//s.nextByte();
275
276                return M;
277        }
278}