001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.feature.local.quantised;
031
032import java.io.DataInput;
033import java.io.DataOutput;
034import java.io.IOException;
035import java.io.PrintWriter;
036import java.util.Scanner;
037
038import org.openimaj.feature.IntFV;
039import org.openimaj.feature.local.LocalFeature;
040import org.openimaj.feature.local.Location;
041
042/**
043 * A QuantisedLocalFeature is a local feature with a single integer
044 * feature-vector.
045 * 
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 * 
048 * @param <L>
049 *            the type of Location
050 */
051public class QuantisedLocalFeature<L extends Location> implements LocalFeature<L, IntFV> {
052        /**
053         * The location of the local feature
054         */
055        public L location;
056
057        /**
058         * The identifier.
059         */
060        public int id;
061
062        /**
063         * Default constructor.
064         */
065        public QuantisedLocalFeature() {
066        }
067
068        /**
069         * Construct the QuantisedLocalFeature with the given location and
070         * identifier.
071         * 
072         * @param location
073         *            the location.
074         * @param id
075         *            the identifier.
076         */
077        public QuantisedLocalFeature(L location, int id) {
078                this.location = location;
079                this.id = id;
080        }
081
082        @Override
083        public void writeBinary(DataOutput out) throws IOException {
084                location.writeBinary(out);
085                out.writeInt(id);
086        }
087
088        @Override
089        public void writeASCII(PrintWriter out) throws IOException {
090                location.writeASCII(out);
091                out.println(id);
092        }
093
094        @Override
095        public void readBinary(DataInput in) throws IOException {
096                location.readBinary(in);
097                id = in.readInt();
098        }
099
100        @Override
101        public void readASCII(Scanner in) throws IOException {
102                location.readASCII(in);
103                id = Integer.parseInt(in.next());
104        }
105
106        @Override
107        public byte[] binaryHeader() {
108                return ("QLFI" + "." + new String(location.binaryHeader())).getBytes();
109        }
110
111        @Override
112        public String asciiHeader() {
113                return this.getClass().getName() + "." + location.asciiHeader();
114        }
115
116        @Override
117        public IntFV getFeatureVector() {
118                return new IntFV(new int[] { id });
119        }
120
121        @Override
122        public L getLocation() {
123                return location;
124        }
125}