package org.apache.fop.afp.ioca;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.fop.afp.modca.AbstractStructuredObject;
import org.apache.fop.afp.util.BinaryUtils;
public class Tile extends AbstractStructuredObject {
private static final int MAX_DATA_LEN = 8191;
private TilePosition tilePosition;
private TileSize tileSize;
private BandImage bandImage;
private byte[] data;
private IDEStructureParameter ideStructureParameter;
private byte encoding = (byte) 0x03;
private byte ideSize = 1;
@Override
public void writeContent(OutputStream os) throws IOException {
tilePosition.writeToStream(os);
tileSize.writeToStream(os);
os.write(getImageEncodingParameter());
os.write(getImageIDESizeParameter());
if (bandImage != null) {
bandImage.writeToStream(os);
}
if (ideStructureParameter != null) {
ideStructureParameter.writeToStream(os);
}
if (data != null) {
byte[] c = new byte[data.length / 4];
byte[] m = new byte[data.length / 4];
byte[] y = new byte[data.length / 4];
byte[] k = new byte[data.length / 4];
for (int j = 0; j < data.length / 4; j++) {
c[j] = data[4 * j];
m[j] = data[4 * j + 1];
y[j] = data[4 * j + 2];
k[j] = data[4 * j + 3];
}
final byte[] dataHeader = new byte[] {(byte) 0xFE,
(byte) 0x9C,
0x00,
0x00,
0x00,
0x00,
0x00
};
final int lengthOffset = 2;
dataHeader[4] = (byte) 0x01;
writeChunksToStream(c, dataHeader, lengthOffset, MAX_DATA_LEN, os);
dataHeader[4] = (byte) 0x02;
writeChunksToStream(m, dataHeader, lengthOffset, MAX_DATA_LEN, os);
dataHeader[4] = (byte) 0x03;
writeChunksToStream(y, dataHeader, lengthOffset, MAX_DATA_LEN, os);
dataHeader[4] = (byte) 0x04;
writeChunksToStream(k, dataHeader, lengthOffset, MAX_DATA_LEN, os);
}
}
@Override
protected void writeStart(OutputStream os) throws IOException {
final byte[] startData = new byte[] {(byte) 0x8C,
0x00
};
os.write(startData);
}
@Override
protected void writeEnd(OutputStream os) throws IOException {
final byte[] endData = new byte[] {(byte) 0x8D,
0x00,
};
os.write(endData);
}
public void setPosition(TilePosition tilePosition) {
this.tilePosition = tilePosition;
}
public void setSize(TileSize tileSize) {
this.tileSize = tileSize;
}
public void setImageData(byte[] imageData) {
this.data = imageData.clone();
}
protected static void writeChunksToStream(byte[] data, byte[] dataHeader, int lengthOffset,
int maxChunkLength, OutputStream os) throws IOException {
int dataLength = data.length;
maxChunkLength -= 3;
int numFullChunks = dataLength / maxChunkLength;
int lastChunkLength = dataLength % maxChunkLength;
byte[] len = {(byte) 0x1f, (byte) 0xff};
int off = 0;
if (numFullChunks > 0) {
dataHeader[lengthOffset] = len[0];
dataHeader[lengthOffset + 1] = len[1];
for (int i = 0; i < numFullChunks; i++, off += maxChunkLength) {
os.write(dataHeader);
os.write(data, off, maxChunkLength);
}
}
if (lastChunkLength > 0) {
len = BinaryUtils.convert(3 + lastChunkLength, 2);
dataHeader[lengthOffset] = len[0];
dataHeader[lengthOffset + 1] = len[1];
os.write(dataHeader);
os.write(data, off, lastChunkLength);
}
}
public void setImageEncodingParameter(byte encoding) {
this.encoding = encoding;
}
public void setImageIDESizeParameter(byte ideSize) {
this.ideSize = ideSize;
}
public void setIDEStructureParameter(IDEStructureParameter ideStructureParameter) {
this.ideStructureParameter = ideStructureParameter;
}
private byte[] getImageEncodingParameter() {
final byte[] encodingData = new byte[] {(byte) 0x95,
0x02,
encoding, (byte) (encoding == ImageContent.COMPID_JPEG ? 0xFE : 0x01),
};
return encodingData;
}
private byte[] getImageIDESizeParameter() {
if (ideSize != 1) {
final byte[] ideSizeData = new byte[] {(byte) 0x96,
0x01,
ideSize};
return ideSizeData;
} else {
return new byte[0];
}
}
public void setBandImage(BandImage bandImage) {
this.bandImage = bandImage;
}
}