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.list; 031 032import java.io.ByteArrayOutputStream; 033import java.io.DataOutput; 034import java.io.DataOutputStream; 035import java.io.File; 036import java.io.IOException; 037import java.io.PrintWriter; 038import java.lang.reflect.Array; 039 040import org.openimaj.feature.local.LocalFeature; 041import org.openimaj.io.IOUtils; 042import org.openimaj.util.list.AbstractFileBackedList; 043 044/** 045 * A {@link LocalFeatureList} backed by a file. Data is only read as necessary. 046 * 047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 048 * 049 * @param <T> 050 * the type of local feature 051 */ 052public class FileLocalFeatureList<T extends LocalFeature<?, ?>> extends AbstractFileBackedList<T> 053 implements 054 LocalFeatureList<T>, 055 Cloneable 056{ 057 protected final int veclen; 058 059 protected FileLocalFeatureList(int size, int veclen, boolean isBinary, int headerLength, int recordLength, File file, 060 Class<T> clz) 061 { 062 super(size, isBinary, headerLength, recordLength, file, clz); 063 this.veclen = veclen; 064 } 065 066 /*** 067 * 068 * Read a file containing a set of local features of a type clz. It is 069 * assumed that clz can instantiate itself either given a vec length or no 070 * parameters and furthermore, that this instantiated instance can write 071 * itself, even when filled with no other data. 072 * 073 * @param <T> 074 * the local feature class 075 * @param keypointFile 076 * the file 077 * @param clz 078 * the local feature class 079 * @return a list of local feature backed by the file 080 * @throws IOException 081 * if a problem occurs reading the file 082 */ 083 public static <T extends LocalFeature<?, ?>> FileLocalFeatureList<T> read(File keypointFile, Class<T> clz) 084 throws IOException 085 { 086 final boolean isBinary = IOUtils.isBinary(keypointFile, LocalFeatureList.BINARY_HEADER); 087 088 // read header 089 final int[] header = LocalFeatureListUtils.readHeader(keypointFile, isBinary); 090 final int size = header[0]; 091 final int veclen = header[1]; 092 final int headerLength = header[2]; 093 094 final T instance = LocalFeatureListUtils.newInstance(clz, veclen); 095 096 final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 097 instance.writeBinary(new DataOutputStream(buffer)); 098 099 final int recordLength = buffer.toByteArray().length; 100 101 return new FileLocalFeatureList<T>(size, veclen, isBinary, headerLength, recordLength, keypointFile, clz); 102 } 103 104 @Override 105 public int vecLength() { 106 return veclen; 107 } 108 109 @Override 110 public void writeBinary(DataOutput out) throws IOException { 111 LocalFeatureListUtils.writeBinary(out, this); 112 } 113 114 @Override 115 public void writeASCII(PrintWriter out) throws IOException { 116 LocalFeatureListUtils.writeASCII(out, this); 117 } 118 119 @Override 120 public byte[] binaryHeader() { 121 return LocalFeatureList.BINARY_HEADER; 122 } 123 124 @Override 125 public String asciiHeader() { 126 return ""; 127 } 128 129 @SuppressWarnings("unchecked") 130 @Override 131 public <Q> Q[] asDataArray(Q[] a) { 132 if (a.length < size()) { 133 a = (Q[]) Array.newInstance(a.getClass().getComponentType(), size()); 134 } 135 136 int i = 0; 137 for (final T t : this) { 138 a[i++] = (Q) t.getFeatureVector().getVector(); 139 } 140 141 return a; 142 } 143 144 @Override 145 protected AbstractFileBackedList<T> newInstance(int newSize, boolean isBinary, int newHeaderLength, int recordLength, 146 File file) 147 { 148 return new FileLocalFeatureList<T>(newSize, veclen, isBinary, newHeaderLength, recordLength, file, clz); 149 } 150 151 @Override 152 protected T newElementInstance() { 153 return LocalFeatureListUtils.newInstance(clz, this.veclen); 154 } 155 156 @Override 157 public MemoryLocalFeatureList<T> subList(int fromIndex, int toIndex) { 158 return new MemoryLocalFeatureList<T>(super.subList(fromIndex, toIndex)); 159 } 160}