001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.formats.tiff.write; 018 019import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_HEADER_SIZE; 020 021import java.io.IOException; 022import java.io.OutputStream; 023import java.nio.ByteOrder; 024import java.util.List; 025 026import org.apache.commons.imaging.ImageWriteException; 027import org.apache.commons.imaging.common.BinaryOutputStream; 028 029public class TiffImageWriterLossy extends TiffImageWriterBase { 030 031 public TiffImageWriterLossy() { 032 // with default byte order 033 } 034 035 public TiffImageWriterLossy(final ByteOrder byteOrder) { 036 super(byteOrder); 037 } 038 039 @Override 040 public void write(final OutputStream os, final TiffOutputSet outputSet) 041 throws IOException, ImageWriteException { 042 final TiffOutputSummary outputSummary = validateDirectories(outputSet); 043 044 final List<TiffOutputItem> outputItems = outputSet.getOutputItems(outputSummary); 045 046 updateOffsetsStep(outputItems); 047 048 outputSummary.updateOffsets(byteOrder); 049 050 final BinaryOutputStream bos = new BinaryOutputStream(os, byteOrder); 051 052 writeStep(bos, outputItems); 053 } 054 055 private void updateOffsetsStep(final List<TiffOutputItem> outputItems) { 056 int offset = TIFF_HEADER_SIZE; 057 058 for (final TiffOutputItem outputItem : outputItems) { 059 outputItem.setOffset(offset); 060 final int itemLength = outputItem.getItemLength(); 061 offset += itemLength; 062 063 final int remainder = imageDataPaddingLength(itemLength); 064 offset += remainder; 065 } 066 } 067 068 private void writeStep(final BinaryOutputStream bos, 069 final List<TiffOutputItem> outputItems) throws IOException, 070 ImageWriteException { 071 writeImageFileHeader(bos); 072 073 for (final TiffOutputItem outputItem : outputItems) { 074 outputItem.writeItem(bos); 075 076 final int length = outputItem.getItemLength(); 077 078 final int remainder = imageDataPaddingLength(length); 079 for (int j = 0; j < remainder; j++) { 080 bos.write(0); 081 } 082 } 083 084 } 085}