View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.tiff.write;
18  
19  import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TIFF_HEADER_SIZE;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.nio.ByteOrder;
24  import java.util.List;
25  
26  import org.apache.commons.imaging.ImagingException;
27  import org.apache.commons.imaging.common.BinaryOutputStream;
28  
29  public class TiffImageWriterLossy extends AbstractTiffImageWriter {
30  
31      public TiffImageWriterLossy() {
32          // with default byte order
33      }
34  
35      public TiffImageWriterLossy(final ByteOrder byteOrder) {
36          super(byteOrder);
37      }
38  
39      private void updateOffsetsStep(final List<AbstractTiffOutputItem> outputItems) {
40          int offset = TIFF_HEADER_SIZE;
41  
42          for (final AbstractTiffOutputItem outputItem : outputItems) {
43              outputItem.setOffset(offset);
44              final int itemLength = outputItem.getItemLength();
45              offset += itemLength;
46  
47              final int remainder = imageDataPaddingLength(itemLength);
48              offset += remainder;
49          }
50      }
51  
52      @Override
53      public void write(final OutputStream os, final TiffOutputSet outputSet) throws IOException, ImagingException {
54          final TiffOutputSummary outputSummary = validateDirectories(outputSet);
55  
56          final List<AbstractTiffOutputItem> outputItems = outputSet.getOutputItems(outputSummary);
57  
58          updateOffsetsStep(outputItems);
59  
60          outputSummary.updateOffsets(byteOrder);
61  
62          final BinaryOutputStream bos = BinaryOutputStream.create(os, byteOrder);
63  
64          // NB: resource is intentionally left open
65          writeStep(bos, outputItems);
66      }
67  
68      private void writeStep(final BinaryOutputStream bos, final List<AbstractTiffOutputItem> outputItems) throws IOException, ImagingException {
69          writeImageFileHeader(bos);
70  
71          for (final AbstractTiffOutputItem outputItem : outputItems) {
72              outputItem.writeItem(bos);
73  
74              final int length = outputItem.getItemLength();
75  
76              final int remainder = imageDataPaddingLength(length);
77              for (int j = 0; j < remainder; j++) {
78                  bos.write(0);
79              }
80          }
81  
82      }
83  }