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.psd.datareaders; 018 019import java.awt.image.BufferedImage; 020import java.io.ByteArrayInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023import java.nio.ByteOrder; 024 025import org.apache.commons.imaging.ImagingException; 026import org.apache.commons.imaging.common.Allocator; 027import org.apache.commons.imaging.common.BinaryFileParser; 028import org.apache.commons.imaging.common.BinaryFunctions; 029import org.apache.commons.imaging.common.PackBits; 030import org.apache.commons.imaging.formats.psd.PsdHeaderInfo; 031import org.apache.commons.imaging.formats.psd.PsdImageContents; 032import org.apache.commons.imaging.formats.psd.dataparsers.AbstractDataParser; 033import org.apache.commons.imaging.mylzw.BitsToByteInputStream; 034import org.apache.commons.imaging.mylzw.MyBitInputStream; 035 036public class CompressedDataReader implements DataReader { 037 038 private final AbstractDataParser dataParser; 039 040 public CompressedDataReader(final AbstractDataParser dataParser) { 041 this.dataParser = dataParser; 042 } 043 044 @Override 045 public void readData(final InputStream is, final BufferedImage bi, final PsdImageContents imageContents, final BinaryFileParser bfp) 046 throws ImagingException, IOException { 047 final PsdHeaderInfo header = imageContents.header; 048 final int width = header.columns; 049 final int height = header.rows; 050 051 // this.setDebug(true); 052 final int scanlineCount = height * header.channels; 053 final int[] scanlineByteCounts = Allocator.intArray(scanlineCount); 054 for (int i = 0; i < scanlineCount; i++) { 055 scanlineByteCounts[i] = BinaryFunctions.read2Bytes("scanlineByteCounts[" + i + "]", is, "PSD: bad Image Data", bfp.getByteOrder()); 056 } 057 // System.out.println("fImageContents.Compression: " 058 // + imageContents.Compression); 059 060 final int depth = header.depth; 061 062 final int channelCount = dataParser.getBasicChannelsCount(); 063 final int[][][] data = new int[Allocator.check(channelCount)][Allocator.check(height)][]; 064 // channels[0] = 065 for (int channel = 0; channel < channelCount; channel++) { 066 for (int y = 0; y < height; y++) { 067 final int index = channel * height + y; 068 final byte[] packed = BinaryFunctions.readBytes("scanline", is, scanlineByteCounts[index], "PSD: Missing Image Data"); 069 070 final byte[] unpacked = PackBits.decompress(packed, width); 071 try (InputStream bais = new ByteArrayInputStream(unpacked); 072 MyBitInputStream mbis = new MyBitInputStream(bais, ByteOrder.BIG_ENDIAN, false)) { 073 // we want all samples to be bytes 074 try (BitsToByteInputStream bbis = new BitsToByteInputStream(mbis, 8)) { 075 final int[] scanline = bbis.readBitsArray(depth, width); 076 data[channel][y] = scanline; 077 } 078 } 079 } 080 } 081 dataParser.parseData(data, bi, imageContents); 082 } 083 084}