    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        // NB: resource is intentionally left open
    053        writeStep(bos, outputItems);
    054    }
    055
    056    private void updateOffsetsStep(final List<TiffOutputItem> outputItems) {
    057        int offset = TIFF_HEADER_SIZE;
    058
    059        for (final TiffOutputItem outputItem : outputItems) {
    060            outputItem.setOffset(offset);
    061            final int itemLength = outputItem.getItemLength();
    062            offset += itemLength;
    063
    064            final int remainder = imageDataPaddingLength(itemLength);
    065            offset += remainder;
    066        }
    067    }
    068
    069    private void writeStep(final BinaryOutputStream bos,
    070            final List<TiffOutputItem> outputItems) throws IOException,
    071            ImageWriteException {
    072        writeImageFileHeader(bos);
    073
    074        for (final TiffOutputItem outputItem : outputItems) {
    075            outputItem.writeItem(bos);
    076
    077            final int length = outputItem.getItemLength();
    078
    079            final int remainder = imageDataPaddingLength(length);
    080            for (int j = 0; j < remainder; j++) {
    081                bos.write(0);
    082            }
    083        }
    084
    085    }
    086}