    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.IOException;
    021import java.io.InputStream;
    022import java.nio.ByteOrder;
    023
    024import org.apache.commons.imaging.ImagingException;
    025import org.apache.commons.imaging.common.Allocator;
    026import org.apache.commons.imaging.common.BinaryFileParser;
    027import org.apache.commons.imaging.formats.psd.PsdHeaderInfo;
    028import org.apache.commons.imaging.formats.psd.PsdImageContents;
    029import org.apache.commons.imaging.formats.psd.dataparsers.AbstractDataParser;
    030import org.apache.commons.imaging.mylzw.BitsToByteInputStream;
    031import org.apache.commons.imaging.mylzw.MyBitInputStream;
    032
    033public class UncompressedDataReader implements DataReader {
    034
    035    private final AbstractDataParser dataParser;
    036
    037    public UncompressedDataReader(final AbstractDataParser dataParser) {
    038        this.dataParser = dataParser;
    039    }
    040
    041    @Override
    042    public void readData(final InputStream is, final BufferedImage bi, final PsdImageContents imageContents, final BinaryFileParser bfp)
    043            throws ImagingException, IOException {
    044        final PsdHeaderInfo header = imageContents.header;
    045        final int width = header.columns;
    046        final int height = header.rows;
    047
    048        final int channelCount = dataParser.getBasicChannelsCount();
    049        final int depth = header.depth;
    050        final MyBitInputStream mbis = new MyBitInputStream(is, ByteOrder.BIG_ENDIAN, false);
    051        // we want all samples to be bytes
    052        try (BitsToByteInputStream bbis = new BitsToByteInputStream(mbis, 8)) {
    053            final int[][][] data = new int[Allocator.check(channelCount)][Allocator.check(height)][Allocator.check(width)];
    054            for (int channel = 0; channel < channelCount; channel++) {
    055                for (int y = 0; y < height; y++) {
    056                    for (int x = 0; x < width; x++) {
    057                        final int b = bbis.readBits(depth);
    058
    059                        data[channel][y][x] = (byte) b;
    060                    }
    061                }
    062            }
    063
    064            dataParser.parseData(data, bi, imageContents);
    065        }
    066    }
    067}