java-org.hwo/src/org/hwo/image/tiff/IFDEntry.java

112 lines
2.3 KiB
Java

package org.hwo.image.tiff;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.hwo.image.UnsupportedFormatException;
abstract class IFDEntry
{
public static IFDEntry read(IFD ifd)
{
int save = ifd.tiffFile.sourceBuffer.position();
ifd.tiffFile.sourceBuffer.getShort();
TAGTYPE tagType = TAGTYPE.fromCode(ifd.tiffFile.sourceBuffer.getShort());
ifd.tiffFile.sourceBuffer.position(save);
switch (tagType)
{
case ASCII:
return new StringEntry(ifd);
case LONG:
return new IntegerEntry(ifd);
case SHORT:
return new ShortEntry(ifd);
default: // Skip Entry
ifd.tiffFile.sourceBuffer.getInt();
ifd.tiffFile.sourceBuffer.getInt();
ifd.tiffFile.sourceBuffer.getInt();
return null;
}
}
private final IFD ifd;
Short tag;
TAGTYPE tagType;
ByteBuffer buffer;
public IFDEntry(IFD ifd)
{
this.ifd = ifd;
System.err.println(String.format("readIFD(): reading entry at 0x%08x",this.ifd.tiffFile.sourceBuffer.position()));
Integer count,
offset;
tag = this.ifd.tiffFile.sourceBuffer.getShort();
tagType = TAGTYPE.fromCode(this.ifd.tiffFile.sourceBuffer.getShort());
System.err.println(String.format("TAG: %d TYPE: %s", tag, tagType));
count = this.ifd.tiffFile.sourceBuffer.getInt();
offset = this.ifd.tiffFile.sourceBuffer.getInt();
int fieldSize = tagType.getSize() * count;
byte[] ba = new byte[ fieldSize ];
if (fieldSize <= 4)
offset = this.ifd.tiffFile.sourceBuffer.position() - 4;
int save = this.ifd.tiffFile.sourceBuffer.position();
this.ifd.tiffFile.sourceBuffer.position(offset);
this.ifd.tiffFile.sourceBuffer.get(ba);
this.ifd.tiffFile.sourceBuffer.position(save);
buffer = ByteBuffer.wrap(ba);
buffer.order( this.ifd.tiffFile.bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN );
decodeBuffer();
}
public IFDEntry(IFD ifd,Short tag,TAGTYPE tagType)
{
this.ifd = ifd;
this.tag = tag;
this.tagType = tagType;
}
ByteBuffer getBuffer()
{
return buffer;
}
void releaseBuffer()
{
buffer = null;
}
protected void decodeBuffer()
{
}
protected void encodeBuffer()
{
}
abstract public Object getValues();
@Override
public String toString() {
return String.format("TAG: %d TYPE: %s",tag,tagType.toString());
}
}