下面列出了javax.imageio.plugins.tiff.TIFFTag#TIFF_LONG 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Sets the value of the {@code metadata} field.
*
* <p> The implementation in this class also sets local options
* from the T4_OPTIONS field if it exists, and if it doesn't, adds
* it with default values.</p>
*
* @param metadata the {@code IIOMetadata} object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_T4_OPTIONS);
if (f != null) {
int options = f.getAsInt(0);
is1DMode = (options & 0x1) == 0;
isEOLAligned = (options & 0x4) == 0x4;
} else {
long[] oarray = new long[1];
oarray[0] = (isEOLAligned ? 0x4 : 0x0) |
(is1DMode ? 0x0 : 0x1);
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField T4Options =
new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T4_OPTIONS),
TIFFTag.TIFF_LONG,
1,
oarray);
tim.rootIFD.addTIFFField(T4Options);
}
}
}
/**
* Sets the value of the {@code metadata} field.
*
* <p> The implementation in this class also sets local options
* from the T4_OPTIONS field if it exists, and if it doesn't, adds
* it with default values.</p>
*
* @param metadata the {@code IIOMetadata} object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_T4_OPTIONS);
if (f != null) {
int options = f.getAsInt(0);
is1DMode = (options & 0x1) == 0;
isEOLAligned = (options & 0x4) == 0x4;
} else {
long[] oarray = new long[1];
oarray[0] = (isEOLAligned ? 0x4 : 0x0) |
(is1DMode ? 0x0 : 0x1);
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField T4Options =
new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T4_OPTIONS),
TIFFTag.TIFF_LONG,
1,
oarray);
tim.rootIFD.addTIFFField(T4Options);
}
}
}
public int encode(byte[] b, int off,
int width, int height,
int[] bitsPerSample,
int scanlineStride) throws IOException {
if (bitsPerSample.length != 1 || bitsPerSample[0] != 1) {
throw new IIOException(
"Bits per sample must be 1 for T6 compression!");
}
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
long[] options = new long[1];
options[0] = 0;
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField T6Options =
new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T6_OPTIONS),
TIFFTag.TIFF_LONG,
1,
options);
tim.rootIFD.addTIFFField(T6Options);
}
// See comment in TIFFT4Compressor
int maxBits = 9*((width + 1)/2) + 2;
int bufSize = (maxBits + 7)/8;
bufSize = height*(bufSize + 2) + 12;
byte[] compData = new byte[bufSize];
int bytes = encodeT6(b, scanlineStride, 8*off, width, height,
compData);
stream.write(compData, 0, bytes);
return bytes;
}
public int encode(byte[] b, int off,
int width, int height,
int[] bitsPerSample,
int scanlineStride) throws IOException {
if (bitsPerSample.length != 1 || bitsPerSample[0] != 1) {
throw new IIOException(
"Bits per sample must be 1 for T6 compression!");
}
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata)metadata;
long[] options = new long[1];
options[0] = 0;
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField T6Options =
new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T6_OPTIONS),
TIFFTag.TIFF_LONG,
1,
options);
tim.rootIFD.addTIFFField(T6Options);
}
// See comment in TIFFT4Compressor
int maxBits = 9*((width + 1)/2) + 2;
int bufSize = (maxBits + 7)/8;
bufSize = height*(bufSize + 2) + 12;
byte[] compData = new byte[bufSize];
int bytes = encodeT6(b, scanlineStride, 8*off, width, height,
compData);
stream.write(compData, 0, bytes);
return bytes;
}
private static void writeTIFFFieldToStream(TIFFField field,
ImageOutputStream stream)
throws IOException {
int count = field.getCount();
Object data = field.getData();
switch (field.getType()) {
case TIFFTag.TIFF_ASCII:
for (int i = 0; i < count; i++) {
String s = ((String[])data)[i];
int length = s.length();
for (int j = 0; j < length; j++) {
stream.writeByte(s.charAt(j) & 0xff);
}
stream.writeByte(0);
}
break;
case TIFFTag.TIFF_UNDEFINED:
case TIFFTag.TIFF_BYTE:
case TIFFTag.TIFF_SBYTE:
stream.write((byte[])data);
break;
case TIFFTag.TIFF_SHORT:
stream.writeChars((char[])data, 0, ((char[])data).length);
break;
case TIFFTag.TIFF_SSHORT:
stream.writeShorts((short[])data, 0, ((short[])data).length);
break;
case TIFFTag.TIFF_SLONG:
stream.writeInts((int[])data, 0, ((int[])data).length);
break;
case TIFFTag.TIFF_LONG:
for (int i = 0; i < count; i++) {
stream.writeInt((int)(((long[])data)[i]));
}
break;
case TIFFTag.TIFF_IFD_POINTER:
stream.writeInt(0); // will need to be backpatched
break;
case TIFFTag.TIFF_FLOAT:
stream.writeFloats((float[])data, 0, ((float[])data).length);
break;
case TIFFTag.TIFF_DOUBLE:
stream.writeDoubles((double[])data, 0, ((double[])data).length);
break;
case TIFFTag.TIFF_SRATIONAL:
for (int i = 0; i < count; i++) {
stream.writeInt(((int[][])data)[i][0]);
stream.writeInt(((int[][])data)[i][1]);
}
break;
case TIFFTag.TIFF_RATIONAL:
for (int i = 0; i < count; i++) {
long num = ((long[][])data)[i][0];
long den = ((long[][])data)[i][1];
stream.writeInt((int)num);
stream.writeInt((int)den);
}
break;
default:
// error
}
}
private static boolean isIFD(TIFFField f) {
int type = f.getType();
return f.hasDirectory() &&
(type == TIFFTag.TIFF_LONG || type == TIFFTag.TIFF_IFD_POINTER);
}
private static void writeTIFFFieldToStream(TIFFField field,
ImageOutputStream stream)
throws IOException {
int count = field.getCount();
Object data = field.getData();
switch (field.getType()) {
case TIFFTag.TIFF_ASCII:
for (int i = 0; i < count; i++) {
String s = ((String[])data)[i];
int length = s.length();
for (int j = 0; j < length; j++) {
stream.writeByte(s.charAt(j) & 0xff);
}
stream.writeByte(0);
}
break;
case TIFFTag.TIFF_UNDEFINED:
case TIFFTag.TIFF_BYTE:
case TIFFTag.TIFF_SBYTE:
stream.write((byte[])data);
break;
case TIFFTag.TIFF_SHORT:
stream.writeChars((char[])data, 0, ((char[])data).length);
break;
case TIFFTag.TIFF_SSHORT:
stream.writeShorts((short[])data, 0, ((short[])data).length);
break;
case TIFFTag.TIFF_SLONG:
stream.writeInts((int[])data, 0, ((int[])data).length);
break;
case TIFFTag.TIFF_LONG:
for (int i = 0; i < count; i++) {
stream.writeInt((int)(((long[])data)[i]));
}
break;
case TIFFTag.TIFF_IFD_POINTER:
stream.writeInt(0); // will need to be backpatched
break;
case TIFFTag.TIFF_FLOAT:
stream.writeFloats((float[])data, 0, ((float[])data).length);
break;
case TIFFTag.TIFF_DOUBLE:
stream.writeDoubles((double[])data, 0, ((double[])data).length);
break;
case TIFFTag.TIFF_SRATIONAL:
for (int i = 0; i < count; i++) {
stream.writeInt(((int[][])data)[i][0]);
stream.writeInt(((int[][])data)[i][1]);
}
break;
case TIFFTag.TIFF_RATIONAL:
for (int i = 0; i < count; i++) {
long num = ((long[][])data)[i][0];
long den = ((long[][])data)[i][1];
stream.writeInt((int)num);
stream.writeInt((int)den);
}
break;
default:
// error
}
}
private static boolean isIFD(TIFFField f) {
int type = f.getType();
return f.hasDirectory() &&
(type == TIFFTag.TIFF_LONG || type == TIFFTag.TIFF_IFD_POINTER);
}