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.psd.datareaders;
18  
19  import java.awt.image.BufferedImage;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.nio.ByteOrder;
23  
24  import org.apache.commons.imaging.ImagingException;
25  import org.apache.commons.imaging.common.Allocator;
26  import org.apache.commons.imaging.common.BinaryFileParser;
27  import org.apache.commons.imaging.formats.psd.PsdHeaderInfo;
28  import org.apache.commons.imaging.formats.psd.PsdImageContents;
29  import org.apache.commons.imaging.formats.psd.dataparsers.DataParser;
30  import org.apache.commons.imaging.mylzw.BitsToByteInputStream;
31  import org.apache.commons.imaging.mylzw.MyBitInputStream;
32  
33  public class UncompressedDataReader implements DataReader {
34  
35      private final DataParser dataParser;
36  
37      public UncompressedDataReader(final DataParser dataParser) {
38          this.dataParser = dataParser;
39      }
40  
41      @Override
42      public void readData(final InputStream is, final BufferedImage bi, final PsdImageContents imageContents, final BinaryFileParser bfp)
43              throws ImagingException, IOException {
44          final PsdHeaderInfo header = imageContents.header;
45          final int width = header.columns;
46          final int height = header.rows;
47  
48          final int channelCount = dataParser.getBasicChannelsCount();
49          final int depth = header.depth;
50          final MyBitInputStream mbis = new MyBitInputStream(is, ByteOrder.BIG_ENDIAN, false);
51          // we want all samples to be bytes
52          try (BitsToByteInputStream bbis = new BitsToByteInputStream(mbis, 8)) {
53              final int[][][] data = new int[Allocator.check(channelCount)][Allocator.check(height)][Allocator.check(width)];
54              for (int channel = 0; channel < channelCount; channel++) {
55                  for (int y = 0; y < height; y++) {
56                      for (int x = 0; x < width; x++) {
57                          final int b = bbis.readBits(depth);
58  
59                          data[channel][y][x] = (byte) b;
60                      }
61                  }
62              }
63  
64              dataParser.parseData(data, bi, imageContents);
65          }
66      }
67  }