    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.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.ImagingException;
    027import org.apache.commons.imaging.common.AbstractBinaryOutputStream;
    028
    029public class TiffImageWriterLossy extends AbstractTiffImageWriter {
    030
    031    public TiffImageWriterLossy() {
    032        // with default byte order
    033    }
    034
    035    public TiffImageWriterLossy(final ByteOrder byteOrder) {
    036        super(byteOrder);
    037    }
    038
    039    private void updateOffsetsStep(final List<AbstractTiffOutputItem> outputItems) {
    040        int offset = HEADER_SIZE;
    041
    042        for (final AbstractTiffOutputItem outputItem : outputItems) {
    043            outputItem.setOffset(offset);
    044            final int itemLength = outputItem.getItemLength();
    045            offset += itemLength;
    046
    047            final int remainder = imageDataPaddingLength(itemLength);
    048            offset += remainder;
    049        }
    050    }
    051
    052    @Override
    053    public void write(final OutputStream os, final TiffOutputSet outputSet) throws IOException, ImagingException {
    054        final TiffOutputSummary outputSummary = validateDirectories(outputSet);
    055
    056        final List<AbstractTiffOutputItem> outputItems = outputSet.getOutputItems(outputSummary);
    057
    058        updateOffsetsStep(outputItems);
    059
    060        outputSummary.updateOffsets(byteOrder);
    061
    062        final AbstractBinaryOutputStream bos = AbstractBinaryOutputStream.create(os, byteOrder);
    063
    064        // NB: resource is intentionally left open
    065        writeStep(bos, outputItems);
    066    }
    067
    068    private void writeStep(final AbstractBinaryOutputStream bos, final List<AbstractTiffOutputItem> outputItems) throws IOException, ImagingException {
    069        writeImageFileHeader(bos);
    070
    071        for (final AbstractTiffOutputItem outputItem : outputItems) {
    072            outputItem.writeItem(bos);
    073
    074            final int length = outputItem.getItemLength();
    075
    076            final int remainder = imageDataPaddingLength(length);
    077            for (int j = 0; j < remainder; j++) {
    078                bos.write(0);
    079            }
    080        }
    081
    082    }
    083}