for (TiffDirectory directory : directories) {
elements.add(directory);
final List<TiffField> fields = directory.getDirectoryEntries();
for (TiffField field : fields) {
final TiffElement oversizeValue = field.getOversizeValueElement();
if (oversizeValue != null) {
final TiffOutputField frozenField = frozenFields.get(field.getTag());
if (frozenField != null
&& frozenField.getSeperateValue() != null
&& frozenField.bytesEqual(field.getByteArrayValue())) {
frozenField.getSeperateValue().setOffset(field.getOffset());
} else {
elements.add(oversizeValue);
}
}
}
final JpegImageData jpegImageData = directory.getJpegImageData();
if (jpegImageData != null) {
elements.add(jpegImageData);
}
final TiffImageData tiffImageData = directory.getTiffImageData();
if (tiffImageData != null) {
final DataElement[] data = tiffImageData.getImageData();
Collections.addAll(elements, data);
}
}
Collections.sort(elements, TiffElement.COMPARATOR);
// dumpElements(byteSource, elements);
final List<TiffElement> rewritableElements = new ArrayList<TiffElement>();
{
final int TOLERANCE = 3;
// int last = TIFF_HEADER_SIZE;
TiffElement start = null;
long index = -1;
for (TiffElement element : elements) {
final long lastElementByte = element.offset + element.length;
if (start == null) {
start = element;