1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }