You are viewing a plain text version of this content. The canonical link for it is here.
Posted to sanselan-commits@incubator.apache.org by cm...@apache.org on 2007/11/29 02:27:15 UTC
svn commit: r599250 [9/15] - in /incubator/sanselan/trunk/src/main/java/org:
apache/sanselan/ apache/sanselan/color/ apache/sanselan/common/
apache/sanselan/common/byteSources/ apache/sanselan/common/mylzw/
apache/sanselan/formats/bmp/ apache/sanselan/...
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PsdImageParser.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PsdImageParser.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PsdImageParser.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PsdImageParser.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,805 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd;
+
+import java.awt.Dimension;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.Map;
+import java.util.Vector;
+
+import org.apache.sanselan.ImageFormat;
+import org.apache.sanselan.ImageInfo;
+import org.apache.sanselan.ImageParser;
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.common.IImageMetadata;
+import org.apache.sanselan.common.byteSources.ByteSource;
+import org.apache.sanselan.formats.psd.dataparsers.DataParser;
+import org.apache.sanselan.formats.psd.dataparsers.DataParserBitmap;
+import org.apache.sanselan.formats.psd.dataparsers.DataParserCMYK;
+import org.apache.sanselan.formats.psd.dataparsers.DataParserGrayscale;
+import org.apache.sanselan.formats.psd.dataparsers.DataParserIndexed;
+import org.apache.sanselan.formats.psd.dataparsers.DataParserLab;
+import org.apache.sanselan.formats.psd.dataparsers.DataParserRGB;
+import org.apache.sanselan.formats.psd.datareaders.CompressedDataReader;
+import org.apache.sanselan.formats.psd.datareaders.DataReader;
+import org.apache.sanselan.formats.psd.datareaders.UncompressedDataReader;
+import org.apache.sanselan.util.Debug;
+
+public class PsdImageParser extends ImageParser
+{
+
+ public PsdImageParser()
+ {
+ super.setByteOrder(BYTE_ORDER_MSB);
+ // setDebug(true);
+ }
+
+ public String getName()
+ {
+ return "PSD-Custom";
+ }
+
+ public String getDefaultExtension()
+ {
+ return DEFAULT_EXTENSION;
+ }
+
+ private static final String DEFAULT_EXTENSION = ".psd";
+
+ private static final String ACCEPTED_EXTENSIONS[] = {
+ DEFAULT_EXTENSION,
+ };
+
+ protected String[] getAcceptedExtensions()
+ {
+ return ACCEPTED_EXTENSIONS;
+ }
+
+ protected ImageFormat[] getAcceptedTypes()
+ {
+ return new ImageFormat[]{
+ ImageFormat.IMAGE_FORMAT_PSD, //
+ };
+ }
+
+ private PSDHeaderInfo readHeader(ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ InputStream is = null;
+
+ try
+ {
+ is = byteSource.getInputStream();
+
+ return readHeader(is);
+ }
+ finally
+ {
+ try
+ {
+ is.close();
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+ }
+
+ private PSDHeaderInfo readHeader(InputStream is) throws ImageReadException,
+ IOException
+ {
+ readAndVerifyBytes(is, new byte[]{
+ 56, 66, 80, 83
+ }, "Not a Valid PSD File");
+
+ int Version = read2Bytes("Version", is, "Not a Valid PSD File");
+
+ byte Reserved[] = readByteArray("Reserved", 6, is,
+ "Not a Valid PSD File");
+
+ int Channels = read2Bytes("Channels", is, "Not a Valid PSD File");
+ int Rows = read4Bytes("Rows", is, "Not a Valid PSD File");
+ int Columns = read4Bytes("Columns", is, "Not a Valid PSD File");
+ int Depth = read2Bytes("Depth", is, "Not a Valid PSD File");
+ int Mode = read2Bytes("Mode", is, "Not a Valid PSD File");
+
+ PSDHeaderInfo result = new PSDHeaderInfo(Version, Reserved, Channels,
+ Rows, Columns, Depth, Mode);
+
+ return result;
+ }
+
+ private ImageContents readImageContents(InputStream is)
+ throws ImageReadException, IOException
+ {
+ PSDHeaderInfo header = readHeader(is);
+
+ int ColorModeDataLength = read4Bytes("ColorModeDataLength", is,
+ "Not a Valid PSD File");
+ skipBytes(is, ColorModeDataLength);
+ // is.skip(ColorModeDataLength);
+ // byte ColorModeData[] = readByteArray("ColorModeData",
+ // ColorModeDataLength, is, "Not a Valid PSD File");
+
+ int ImageResourcesLength = read4Bytes("ImageResourcesLength", is,
+ "Not a Valid PSD File");
+ skipBytes(is, ImageResourcesLength);
+ // long skipped = is.skip(ImageResourcesLength);
+ // byte ImageResources[] = readByteArray("ImageResources",
+ // ImageResourcesLength, is, "Not a Valid PSD File");
+
+ int LayerAndMaskDataLength = read4Bytes("LayerAndMaskDataLength", is,
+ "Not a Valid PSD File");
+ skipBytes(is, LayerAndMaskDataLength);
+ // is.skip(LayerAndMaskDataLength);
+ // byte LayerAndMaskData[] = readByteArray("LayerAndMaskData",
+ // LayerAndMaskDataLength, is, "Not a Valid PSD File");
+
+ int Compression = read2Bytes("Compression", is, "Not a Valid PSD File");
+
+ // skip_bytes(is, LayerAndMaskDataLength);
+ // byte ImageData[] = readByteArray("ImageData", LayerAndMaskDataLength,
+ // is, "Not a Valid PSD File");
+
+ // System.out.println("Compression: " + Compression);
+
+ ImageContents result = new ImageContents(header, ColorModeDataLength,
+ // ColorModeData,
+ ImageResourcesLength,
+ // ImageResources,
+ LayerAndMaskDataLength,
+ // LayerAndMaskData,
+ Compression);
+
+ return result;
+ }
+
+ private Vector readImageResourceBlocks(byte bytes[],
+ int fImageResourceIDs[], int max_blocks_to_read)
+ throws ImageReadException, IOException
+ {
+ return readImageResourceBlocks(new ByteArrayInputStream(bytes),
+ fImageResourceIDs, max_blocks_to_read, bytes.length);
+ }
+
+ private boolean keepImageResourceBlock(int ID, int fImageResourceIDs[])
+ {
+ if (fImageResourceIDs == null)
+ return true;
+
+ for (int i = 0; i < fImageResourceIDs.length; i++)
+ if (ID == fImageResourceIDs[i])
+ return true;
+
+ return false;
+ }
+
+ private Vector readImageResourceBlocks(InputStream is,
+ int fImageResourceIDs[], int max_blocks_to_read, int available)
+ throws ImageReadException, IOException
+ {
+ Vector result = new Vector();
+
+ while (available > 0)
+ {
+ // if (debug)
+ // System.out.println("available: " + available);
+
+ readAndVerifyBytes(is, new byte[]{
+ 56, 66, 73, 77
+ }, "Not a Valid PSD File");
+ available -= 4;
+
+ int ID = read2Bytes("ID", is, "Not a Valid PSD File");
+ available -= 2;
+
+ int NameLength = readByte("NameLength", is, "Not a Valid PSD File");
+
+ available -= 1;
+ // int ActualNameLength = (((NameLength + 1) % 2) == 0)
+ // ? NameLength
+ // : NameLength + 1; // pad to make even
+ byte NameData[] = readByteArray("NameData", NameLength, is,
+ "Not a Valid PSD File");
+ available -= NameLength;
+ if (((NameLength + 1) % 2) != 0)
+ {
+ int NameDiscard = readByte("NameDiscard", is,
+ "Not a Valid PSD File");
+ available -= 1;
+ }
+ // String Name = readPString("Name", 6, is, "Not a Valid PSD File");
+ int DataSize = read4Bytes("Size", is, "Not a Valid PSD File");
+ available -= 4;
+ // int ActualDataSize = ((DataSize % 2) == 0)
+ // ? DataSize
+ // : DataSize + 1; // pad to make even
+
+ byte Data[] = readByteArray("Data", DataSize, is,
+ "Not a Valid PSD File");
+ available -= DataSize;
+
+ if ((DataSize % 2) != 0)
+ {
+ int DataDiscard = readByte("DataDiscard", is,
+ "Not a Valid PSD File");
+ available -= 1;
+ }
+
+ if (keepImageResourceBlock(ID, fImageResourceIDs))
+ {
+ result.add(new ImageResourceBlock(ID, NameData, Data));
+
+ if ((max_blocks_to_read >= 0)
+ && (result.size() >= max_blocks_to_read))
+ return result;
+ }
+ // debugNumber("ID", ID, 2);
+
+ }
+
+ return result;
+ }
+
+ private Vector readImageResourceBlocks(ByteSource byteSource,
+ int fImageResourceIDs[], int max_blocks_to_read)
+ throws ImageReadException, IOException
+ {
+ InputStream is = null;
+
+ try
+ {
+ is = byteSource.getInputStream();
+
+ ImageContents imageContents = readImageContents(is);
+
+ is.close();
+
+ is = this.getInputStream(byteSource, PSD_SECTION_IMAGE_RESOURCES);
+ byte ImageResources[] = readByteArray("ImageResources",
+ imageContents.ImageResourcesLength, is,
+ "Not a Valid PSD File");
+
+ return readImageResourceBlocks(ImageResources, fImageResourceIDs,
+ max_blocks_to_read);
+ }
+ // catch (Exception e)
+ // {
+ // // System.out.println("Error: " + file.getAbsolutePath());
+ // Debug.debug(e); }
+ finally
+ {
+ try
+ {
+ is.close();
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+ // is.reset();
+ // if (debug)
+ // System.out.println("");
+ //
+ // return null;
+ }
+
+ // InputStream is = getInputStream(byteSource, kPSD_SECTION_IMAGE_DATA);
+
+ private static final int PSD_SECTION_HEADER = 0;
+ private static final int PSD_SECTION_COLOR_MODE = 1;
+ private static final int PSD_SECTION_IMAGE_RESOURCES = 2;
+ private static final int PSD_SECTION_LAYER_AND_MASK_DATA = 3;
+ private static final int PSD_SECTION_IMAGE_DATA = 4;
+
+ private static final int PSD_HEADER_LENGTH = 26;
+
+ private InputStream getInputStream(ByteSource byteSource, int section)
+ throws ImageReadException, IOException
+ {
+ InputStream is = byteSource.getInputStream();
+
+ if (section == PSD_SECTION_HEADER)
+ return is;
+
+ skipBytes(is, PSD_HEADER_LENGTH);
+ // is.skip(kHeaderLength);
+
+ int ColorModeDataLength = read4Bytes("ColorModeDataLength", is,
+ "Not a Valid PSD File");
+
+ if (section == PSD_SECTION_COLOR_MODE)
+ return is;
+
+ skipBytes(is, ColorModeDataLength);
+ // byte ColorModeData[] = readByteArray("ColorModeData",
+ // ColorModeDataLength, is, "Not a Valid PSD File");
+
+ int ImageResourcesLength = read4Bytes("ImageResourcesLength", is,
+ "Not a Valid PSD File");
+
+ if (section == PSD_SECTION_IMAGE_RESOURCES)
+ return is;
+
+ skipBytes(is, ImageResourcesLength);
+ // byte ImageResources[] = readByteArray("ImageResources",
+ // ImageResourcesLength, is, "Not a Valid PSD File");
+
+ int LayerAndMaskDataLength = read4Bytes("LayerAndMaskDataLength", is,
+ "Not a Valid PSD File");
+
+ if (section == PSD_SECTION_LAYER_AND_MASK_DATA)
+ return is;
+
+ skipBytes(is, LayerAndMaskDataLength);
+ // byte LayerAndMaskData[] = readByteArray("LayerAndMaskData",
+ // LayerAndMaskDataLength, is, "Not a Valid PSD File");
+
+ int Compression = read2Bytes("Compression", is, "Not a Valid PSD File");
+
+ // byte ImageData[] = readByteArray("ImageData",
+ // LayerAndMaskDataLength, is, "Not a Valid PSD File");
+
+ if (section == PSD_SECTION_IMAGE_DATA)
+ return is;
+
+ throw new ImageReadException("getInputStream: Unknown Section: "
+ + section);
+ }
+
+ private byte[] getData(ByteSource byteSource, int section)
+ throws ImageReadException, IOException
+ {
+ InputStream is = null;
+
+ try
+ {
+ is = byteSource.getInputStream();
+
+ // PSDHeaderInfo header = readHeader(is);
+ if (section == PSD_SECTION_HEADER)
+ return readByteArray("Header", PSD_HEADER_LENGTH, is,
+ "Not a Valid PSD File");
+ skipBytes(is, PSD_HEADER_LENGTH);
+
+ int ColorModeDataLength = read4Bytes("ColorModeDataLength", is,
+ "Not a Valid PSD File");
+
+ if (section == PSD_SECTION_COLOR_MODE)
+ return readByteArray("ColorModeData", ColorModeDataLength, is,
+ "Not a Valid PSD File");
+
+ skipBytes(is, ColorModeDataLength);
+ // byte ColorModeData[] = readByteArray("ColorModeData",
+ // ColorModeDataLength, is, "Not a Valid PSD File");
+
+ int ImageResourcesLength = read4Bytes("ImageResourcesLength", is,
+ "Not a Valid PSD File");
+
+ if (section == PSD_SECTION_IMAGE_RESOURCES)
+ return readByteArray("ImageResources", ImageResourcesLength,
+ is, "Not a Valid PSD File");
+
+ skipBytes(is, ImageResourcesLength);
+ // byte ImageResources[] = readByteArray("ImageResources",
+ // ImageResourcesLength, is, "Not a Valid PSD File");
+
+ int LayerAndMaskDataLength = read4Bytes("LayerAndMaskDataLength",
+ is, "Not a Valid PSD File");
+
+ if (section == PSD_SECTION_LAYER_AND_MASK_DATA)
+ return readByteArray("LayerAndMaskData",
+ LayerAndMaskDataLength, is, "Not a Valid PSD File");
+
+ skipBytes(is, LayerAndMaskDataLength);
+ // byte LayerAndMaskData[] = readByteArray("LayerAndMaskData",
+ // LayerAndMaskDataLength, is, "Not a Valid PSD File");
+
+ int Compression = read2Bytes("Compression", is,
+ "Not a Valid PSD File");
+
+ // byte ImageData[] = readByteArray("ImageData",
+ // LayerAndMaskDataLength, is, "Not a Valid PSD File");
+
+ // if (section == kPSD_SECTION_IMAGE_DATA)
+ // return readByteArray("LayerAndMaskData", LayerAndMaskDataLength, is,
+ // "Not a Valid PSD File");
+
+ }
+ finally
+ {
+ try
+ {
+ is.close();
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+ throw new ImageReadException("getInputStream: Unknown Section: "
+ + section);
+ }
+
+ private ImageContents readImageContents(ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ InputStream is = null;
+
+ try
+ {
+ is = byteSource.getInputStream();
+
+ ImageContents imageContents = readImageContents(is);
+ return imageContents;
+ }
+ finally
+ {
+ try
+ {
+ is.close();
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+
+ }
+
+ public final static int IMAGE_RESOURCE_Id_ICC_PROFILE = 0x040F;
+
+ public byte[] getICCProfileBytes(ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ Vector segments = readImageResourceBlocks(byteSource, new int[]{
+ IMAGE_RESOURCE_Id_ICC_PROFILE,
+ }, 1);
+
+ if ((segments == null) || (segments.size() < 1))
+ return null;
+
+ ImageResourceBlock irb = (ImageResourceBlock) segments.get(0);
+ byte bytes[] = irb.Data;
+ if ((bytes == null) || (bytes.length < 1))
+ return null;
+ return bytes;
+ }
+
+ public Dimension getImageSize(ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ PSDHeaderInfo bhi = readHeader(byteSource);
+ if (bhi == null)
+ throw new ImageReadException("PSD: couldn't read header");
+
+ return new Dimension(bhi.Columns, bhi.Rows);
+
+ }
+
+ public byte[] embedICCProfile(byte image[], byte profile[])
+ {
+ return null;
+ }
+
+ public boolean embedICCProfile(File src, File dst, byte profile[])
+ {
+ return false;
+ }
+
+ public IImageMetadata getMetadata(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ return null;
+ }
+
+ private int getChannelsPerMode(int mode)
+ {
+ switch (mode)
+ {
+ case 0 : // Bitmap
+ return 1;
+ case 1 : // Grayscale
+ return 1;
+ case 2 : // Indexed
+ return -1;
+ case 3 : // RGB
+ return 3;
+ case 4 : // CMYK
+ return 4;
+ case 7 : // Multichannel
+ return -1;
+ case 8 : // Duotone
+ return -1;
+ case 9 : // Lab
+ return 4;
+ default :
+ return -1;
+
+ }
+ }
+
+ public ImageInfo getImageInfo(ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ ImageContents imageContents = readImageContents(byteSource);
+ // ImageContents imageContents = readImage(byteSource, false);
+
+ if (imageContents == null)
+ throw new ImageReadException("PSD: Couldn't read blocks");
+
+ PSDHeaderInfo header = imageContents.header;
+ if (header == null)
+ throw new ImageReadException("PSD: Couldn't read Header");
+
+ int Width = header.Columns;
+ int Height = header.Rows;
+
+ Vector Comments = new Vector();
+ // TODO: comments...
+
+ int BitsPerPixel = header.Depth * getChannelsPerMode(header.Mode);
+ // System.out.println("header.Depth: " + header.Depth);
+ // System.out.println("header.Mode: " + header.Mode);
+ // System.out.println("getChannelsPerMode(header.Mode): " + getChannelsPerMode(header.Mode));
+ if (BitsPerPixel < 0)
+ BitsPerPixel = 0;
+ ImageFormat Format = ImageFormat.IMAGE_FORMAT_PSD;
+ String FormatName = "Photoshop";
+ String MimeType = "image/x-photoshop";
+ // we ought to count images, but don't yet.
+ int NumberOfImages = -1;
+ // not accurate ... only reflects first
+ boolean isProgressive = false;
+
+ int PhysicalWidthDpi = 72;
+ float PhysicalWidthInch = (float) ((double) Width / (double) PhysicalWidthDpi);
+ int PhysicalHeightDpi = 72;
+ float PhysicalHeightInch = (float) ((double) Height / (double) PhysicalHeightDpi);
+
+ String FormatDetails = "Psd";
+
+ boolean isTransparent = false; // TODO: inaccurate.
+ boolean usesPalette = header.Mode == COLOR_MODE_INDEXED;
+ int ColorType = ImageInfo.COLOR_TYPE_UNKNOWN;
+
+ String compressionAlgorithm;
+ switch (imageContents.Compression)
+ {
+ case 0 :
+ compressionAlgorithm = ImageInfo.COMPRESSION_ALGORITHM_NONE;
+ break;
+ case 1 :
+ compressionAlgorithm = ImageInfo.COMPRESSION_ALGORITHM_PSD;
+ break;
+ default :
+ compressionAlgorithm = ImageInfo.COMPRESSION_ALGORITHM_UNKNOWN;
+ }
+
+ ImageInfo result = new ImageInfo(FormatDetails, BitsPerPixel, Comments,
+ Format, FormatName, Height, MimeType, NumberOfImages,
+ PhysicalHeightDpi, PhysicalHeightInch, PhysicalWidthDpi,
+ PhysicalWidthInch, Width, isProgressive, isTransparent,
+ usesPalette, ColorType, compressionAlgorithm);
+
+ return result;
+ }
+
+ private ImageResourceBlock findImageResourceBlock(Vector blocks, int ID)
+ throws ImageReadException, IOException
+ {
+ for (int i = 0; i < blocks.size(); i++)
+ {
+ ImageResourceBlock block = (ImageResourceBlock) blocks.get(i);
+
+ if (block.ID == ID)
+ return block;
+ }
+ return null;
+ }
+
+ public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource)
+ throws ImageReadException, IOException
+ {
+ pw.println("gif.dumpImageFile");
+
+ {
+ ImageInfo fImageData = getImageInfo(byteSource);
+ if (fImageData == null)
+ return false;
+
+ fImageData.toString(pw, "");
+ }
+ {
+ ImageContents imageContents = readImageContents(byteSource);
+
+ imageContents.dump(pw);
+ imageContents.header.dump(pw);
+
+ Vector blocks = readImageResourceBlocks(byteSource,
+ // fImageContents.ImageResources,
+ null, -1);
+
+ pw.println("blocks.size(): " + blocks.size());
+
+ // System.out.println("gif.blocks: " + blocks.blocks.size());
+ for (int i = 0; i < blocks.size(); i++)
+ {
+ ImageResourceBlock block = (ImageResourceBlock) blocks.get(i);
+ pw.println("\t" + i + " (" + Integer.toHexString(block.ID)
+ + ", " + "'"
+ + new String(block.NameData)
+ + "' ("
+ + block.NameData.length
+ + "), "
+ // + block.getClass().getName()
+ // + ", "
+ + " data: " + block.Data.length + " type: '"
+ + new PSDConstants().getDescription(block.ID) + "' "
+ + ")");
+
+ }
+
+ }
+
+ pw.println("");
+
+ return true;
+ }
+
+ private static final int COLOR_MODE_INDEXED = 2;
+
+ public BufferedImage getBufferedImage(ByteSource byteSource, Map params)
+ throws ImageReadException, IOException
+ {
+ ImageContents imageContents = readImageContents(byteSource);
+ // ImageContents imageContents = readImage(byteSource, false);
+
+ if (imageContents == null)
+ throw new ImageReadException("PSD: Couldn't read blocks");
+
+ PSDHeaderInfo header = imageContents.header;
+ if (header == null)
+ throw new ImageReadException("PSD: Couldn't read Header");
+
+ // ImageDescriptor id = (ImageDescriptor) findBlock(fImageContents.blocks,
+ // kImageSeperator);
+ // if (id == null)
+ // throw new ImageReadException("PSD: Couldn't read Image Descriptor");
+ // GraphicControlExtension gce = (GraphicControlExtension) findBlock(
+ // fImageContents.blocks, kGraphicControlExtension);
+
+ Vector blocks = readImageResourceBlocks(byteSource,
+ // fImageContents.ImageResources,
+ null, -1);
+
+ int width = header.Columns;
+ int height = header.Rows;
+ // int height = header.Columns;
+
+ // int transfer_type;
+
+ // transfer_type = DataBuffer.TYPE_BYTE;
+
+ boolean hasAlpha = false;
+ BufferedImage result = getBufferedImageFactory(params)
+ .getColorBufferedImage(width, height, hasAlpha);
+
+ DataParser dataParser;
+ switch (imageContents.header.Mode)
+ {
+ case 0 : // bitmap
+ dataParser = new DataParserBitmap();
+ break;
+ case 1 :
+ case 8 : // Duotone=8;
+ dataParser = new DataParserGrayscale();
+ break;
+ case 3 :
+ dataParser = new DataParserRGB();
+ break;
+ case 4 :
+ dataParser = new DataParserCMYK();
+ break;
+ case 9 :
+ dataParser = new DataParserLab();
+ break;
+ case COLOR_MODE_INDEXED :
+ // case 2 : // Indexed=2;
+ {
+
+ byte ColorModeData[] = getData(byteSource,
+ PSD_SECTION_COLOR_MODE);
+
+ // ImageResourceBlock block = findImageResourceBlock(blocks,
+ // 0x03EB);
+ // if (block == null)
+ // throw new ImageReadException(
+ // "Missing: Indexed Color Image Resource Block");
+
+ dataParser = new DataParserIndexed(ColorModeData);
+ break;
+ }
+ case 7 : // Multichannel=7;
+ // fDataParser = new DataParserStub();
+ // break;
+
+ // case 1 :
+ // fDataReader = new CompressedDataReader();
+ // break;
+ default :
+ throw new ImageReadException("Unknown Mode: "
+ + imageContents.header.Mode);
+ }
+ DataReader fDataReader;
+ switch (imageContents.Compression)
+ {
+ case 0 :
+ fDataReader = new UncompressedDataReader(dataParser);
+ break;
+ case 1 :
+ fDataReader = new CompressedDataReader(dataParser);
+ break;
+ default :
+ throw new ImageReadException("Unknown Compression: "
+ + imageContents.Compression);
+ }
+ {
+ InputStream is = null;
+
+ try
+ {
+ is = getInputStream(byteSource, PSD_SECTION_IMAGE_DATA);
+ fDataReader.readData(is, result, imageContents, this);
+
+ fDataReader.dump();
+ //is.
+ // ImageContents imageContents = readImageContents(is);
+ // return imageContents;
+ }
+ finally
+ {
+ try
+ {
+ if (is != null)
+ is.close();
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+
+ }
+
+ return result;
+
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/PsdImageParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParser.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParser.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParser.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParser.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import java.awt.image.BufferedImage;
+import java.awt.image.DataBuffer;
+
+import org.apache.sanselan.formats.psd.ImageContents;
+import org.apache.sanselan.formats.psd.PSDHeaderInfo;
+
+public abstract class DataParser
+{
+ public final void parseData(int data[][][], BufferedImage bi,
+ ImageContents imageContents)
+ {
+ DataBuffer buffer = bi.getRaster().getDataBuffer();
+
+ PSDHeaderInfo header = imageContents.header;
+ int width = header.Columns;
+ int height = header.Rows;
+
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ int rgb = getRGB(data, x, y, imageContents);
+ buffer.setElem(y * width + x, rgb);
+ }
+
+ }
+
+ protected abstract int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents);
+
+ public abstract int getBasicChannelsCount();
+
+ public void dump()
+ {
+
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserBitmap.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserBitmap.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserBitmap.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserBitmap.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import org.apache.sanselan.formats.psd.ImageContents;
+
+public class DataParserBitmap extends DataParser
+{
+
+ protected int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents)
+ {
+ int sample = 0xff & data[0][y][x];
+ if (sample == 0)
+ sample = 255;
+ else
+ sample = 0;
+ // sample = 255- sample;
+ int alpha = 0xff;
+
+ int rgb = ((0xff & alpha) << 24) | ((0xff & sample) << 16)
+ | ((0xff & sample) << 8) | ((0xff & sample) << 0);
+
+ return rgb;
+ }
+
+ public int getBasicChannelsCount()
+ {
+ return 1;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserBitmap.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserCMYK.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserCMYK.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserCMYK.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserCMYK.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import org.apache.sanselan.color.ColorConversions;
+import org.apache.sanselan.formats.psd.ImageContents;
+
+public class DataParserCMYK extends DataParser
+{
+ protected int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents)
+ {
+ int sc = 0xff & data[0][y][x];
+ int sm = 0xff & data[1][y][x];
+ int sy = 0xff & data[2][y][x];
+ int sk = 0xff & data[3][y][x];
+
+ // CRAZY adobe has to store the bytes in reverse form.
+ sc = 255 - sc;
+ sm = 255 - sm;
+ sy = 255 - sy;
+ sk = 255 - sk;
+
+ int rgb = ColorConversions.convertCMYKtoRGB(sc, sm, sy, sk);
+
+ return rgb;
+ }
+
+ public int getBasicChannelsCount()
+ {
+ return 4;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserCMYK.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserGrayscale.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserGrayscale.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserGrayscale.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserGrayscale.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import org.apache.sanselan.formats.psd.ImageContents;
+
+public class DataParserGrayscale extends DataParser
+{
+ protected int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents)
+ {
+ int sample = 0xff & data[0][y][x];
+ int alpha = 0xff;
+
+ int rgb = ((0xff & alpha) << 24) | ((0xff & sample) << 16)
+ | ((0xff & sample) << 8) | ((0xff & sample) << 0);
+
+ return rgb;
+ }
+
+ public int getBasicChannelsCount()
+ {
+ return 1;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserGrayscale.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserIndexed.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserIndexed.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserIndexed.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserIndexed.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import org.apache.sanselan.formats.psd.ImageContents;
+
+public class DataParserIndexed extends DataParser
+{
+ private final int ColorTable[];
+
+ public DataParserIndexed(byte ColorModeData[])
+ {
+ ColorTable = new int[256];
+ for (int i = 0; i < 256; i++)
+ {
+ int red = 0xff & ColorModeData[0 * 256 + i];
+ int green = 0xff & ColorModeData[1 * 256 + i];
+ int blue = 0xff & ColorModeData[2 * 256 + i];
+ int alpha = 0xff;
+
+ int rgb = ((0xff & alpha) << 24) | ((0xff & red) << 16)
+ | ((0xff & green) << 8) | ((0xff & blue) << 0);
+
+ ColorTable[i] = rgb;
+ }
+ }
+
+ protected int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents)
+ {
+ int sample = 0xff & data[0][y][x];
+ int rgb = ColorTable[sample];
+
+ return rgb;
+ }
+
+ public int getBasicChannelsCount()
+ {
+ return 1;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserIndexed.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserLab.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserLab.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserLab.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserLab.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import org.apache.sanselan.color.ColorConversions;
+import org.apache.sanselan.formats.psd.ImageContents;
+
+public class DataParserLab extends DataParser
+{
+
+ public DataParserLab()
+ {
+
+ }
+
+ protected int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents)
+ {
+ int cieL = 0xff & data[0][y][x];
+ int cieA = 0xff & data[1][y][x];
+ int cieB = 0xff & data[2][y][x];
+
+ cieA -= 128;
+ cieB -= 128;
+
+ int rgb = ColorConversions.convertCIELabtoARGBTest(cieL, cieA, cieB);
+
+ return rgb;
+ }
+
+ public int getBasicChannelsCount()
+ {
+ return 3;
+ }
+
+ public void dump()
+ {
+ // for(int i=0;i<3;i++)
+ // {
+ // System.out.println("CIE: " + i + ": min: " + mins[i] + ", max: " + maxs[i]);
+ // }
+ }
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserLab.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserRGB.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserRGB.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserRGB.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserRGB.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import org.apache.sanselan.formats.psd.ImageContents;
+
+public class DataParserRGB extends DataParser
+{
+ protected int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents)
+ {
+ int red = 0xff & data[0][y][x];
+ int green = 0xff & data[1][y][x];
+ int blue = 0xff & data[2][y][x];
+ int alpha = 0xff;
+
+ int rgb = ((0xff & alpha) << 24) | ((0xff & red) << 16)
+ | ((0xff & green) << 8) | ((0xff & blue) << 0);
+
+ return rgb;
+ }
+
+ public int getBasicChannelsCount()
+ {
+ return 3;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserRGB.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserStub.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserStub.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserStub.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserStub.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.dataparsers;
+
+import org.apache.sanselan.formats.psd.ImageContents;
+
+public class DataParserStub extends DataParser
+{
+ protected int getRGB(int data[][][], int x, int y,
+ ImageContents imageContents)
+ {
+ return 0;
+ }
+
+ public int getBasicChannelsCount()
+ {
+ return 1;
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/dataparsers/DataParserStub.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/CompressedDataReader.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/CompressedDataReader.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/CompressedDataReader.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/CompressedDataReader.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.datareaders;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.common.BinaryFileParser;
+import org.apache.sanselan.common.PackBits;
+import org.apache.sanselan.common.mylzw.BitsToByteInputStream;
+import org.apache.sanselan.common.mylzw.MyBitInputStream;
+import org.apache.sanselan.formats.psd.ImageContents;
+import org.apache.sanselan.formats.psd.PSDHeaderInfo;
+import org.apache.sanselan.formats.psd.dataparsers.DataParser;
+
+public class CompressedDataReader extends DataReader
+{
+
+ public CompressedDataReader(DataParser fDataParser)
+ {
+ super(fDataParser);
+ }
+
+ public void readData(InputStream is, BufferedImage bi,
+ ImageContents imageContents, BinaryFileParser bfp)
+ throws ImageReadException, IOException
+ {
+ PSDHeaderInfo header = imageContents.header;
+ int width = header.Columns;
+ int height = header.Rows;
+
+ // this.setDebug(true);
+ int scanline_count = height * header.Channels;
+ int scanline_bytecounts[] = new int[scanline_count];
+ for (int i = 0; i < scanline_count; i++)
+ scanline_bytecounts[i] = bfp.read2Bytes("scanline_bytecount[" + i
+ + "]", is, "PSD: bad Image Data");
+ bfp.setDebug(false);
+ // System.out.println("fImageContents.Compression: "
+ // + imageContents.Compression);
+
+ int depth = header.Depth;
+
+ int channel_count = dataParser.getBasicChannelsCount();
+ int data[][][] = new int[channel_count][height][];
+ // channels[0] =
+ for (int channel = 0; channel < channel_count; channel++)
+ for (int y = 0; y < height; y++)
+ {
+ int index = channel * height + y;
+ byte packed[] = bfp.readByteArray("scanline",
+ scanline_bytecounts[index], is,
+ "PSD: Missing Image Data");
+
+ byte unpacked[] = new PackBits().decompress(packed, width);
+ InputStream bais = new ByteArrayInputStream(unpacked);
+ MyBitInputStream mbis = new MyBitInputStream(bais,
+ BYTE_ORDER_MSB);
+ BitsToByteInputStream bbis = new BitsToByteInputStream(mbis, 8); // we want all samples to be bytes
+ int scanline[] = bbis.readBitsArray(depth, width);
+ data[channel][y] = scanline;
+
+ }
+
+ dataParser.parseData(data, bi, imageContents);
+
+ }
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/CompressedDataReader.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/DataReader.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/DataReader.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/DataReader.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/DataReader.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.datareaders;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.common.BinaryConstants;
+import org.apache.sanselan.common.BinaryFileParser;
+import org.apache.sanselan.formats.psd.ImageContents;
+import org.apache.sanselan.formats.psd.dataparsers.DataParser;
+
+public abstract class DataReader implements BinaryConstants
+{
+ protected final DataParser dataParser;
+
+ public DataReader(DataParser fDataParser)
+ {
+ this.dataParser = fDataParser;
+ }
+
+ public abstract void readData(InputStream is, BufferedImage bi,
+ ImageContents imageContents, BinaryFileParser bfp)
+ throws ImageReadException, IOException;
+
+ public void dump()
+ {
+ dataParser.dump();
+ }
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/DataReader.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/UncompressedDataReader.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/UncompressedDataReader.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/UncompressedDataReader.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/UncompressedDataReader.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.psd.datareaders;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.sanselan.ImageReadException;
+import org.apache.sanselan.common.BinaryFileParser;
+import org.apache.sanselan.common.mylzw.BitsToByteInputStream;
+import org.apache.sanselan.common.mylzw.MyBitInputStream;
+import org.apache.sanselan.formats.psd.ImageContents;
+import org.apache.sanselan.formats.psd.PSDHeaderInfo;
+import org.apache.sanselan.formats.psd.dataparsers.DataParser;
+
+public class UncompressedDataReader extends DataReader
+{
+ public UncompressedDataReader(DataParser fDataParser)
+ {
+ super(fDataParser);
+ }
+
+ public void readData(InputStream is, BufferedImage bi,
+ ImageContents imageContents, BinaryFileParser bfp)
+ throws ImageReadException, IOException
+ {
+ PSDHeaderInfo header = imageContents.header;
+ int width = header.Columns;
+ int height = header.Rows;
+
+ bfp.setDebug(false);
+
+ int channel_count = dataParser.getBasicChannelsCount();
+ int depth = header.Depth;
+ MyBitInputStream mbis = new MyBitInputStream(is, BYTE_ORDER_MSB);
+ BitsToByteInputStream bbis = new BitsToByteInputStream(mbis, 8); // we want all samples to be bytes
+
+ int data[][][] = new int[channel_count][height][width];
+ for (int channel = 0; channel < channel_count; channel++)
+ for (int y = 0; y < height; y++)
+ for (int x = 0; x < width; x++)
+ {
+ int b = bbis.readBits(depth);
+
+ data[channel][y][x] = (byte) b;
+ }
+
+ dataParser.parseData(data, bi, imageContents);
+
+ }
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/psd/datareaders/UncompressedDataReader.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaConstants.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaConstants.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaConstants.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaConstants.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.tga;
+
+public interface TgaConstants
+{
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaConstants.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaImageParser.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaImageParser.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaImageParser.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaImageParser.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,249 @@
+///*
+// * Licensed to the Apache Software Foundation (ASF) under one or more
+// * contributor license agreements. See the NOTICE file distributed with
+// * this work for additional information regarding copyright ownership.
+// * The ASF licenses this file to You under the Apache License, Version 2.0
+// * (the "License"); you may not use this file except in compliance with
+// * the License. You may obtain a copy of the License at
+// *
+// * http://www.apache.org/licenses/LICENSE-2.0
+// *
+// * Unless required by applicable law or agreed to in writing, software
+// * distributed under the License is distributed on an "AS IS" BASIS,
+// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// * See the License for the specific language governing permissions and
+// * limitations under the License.
+// */
+//package org.apache.sanselan.formats.tga;
+//
+//import java.awt.Dimension;
+//import java.awt.image.BufferedImage;
+//import java.io.File;
+//import java.io.IOException;
+//import java.io.InputStream;
+//import java.io.PrintWriter;
+//import java.util.Map;
+//import java.util.Vector;
+//
+//import org.apache.sanselan.ImageFormat;
+//import org.apache.sanselan.ImageInfo;
+//import org.apache.sanselan.ImageParser;
+//import org.apache.sanselan.ImageReadException;
+//import org.apache.sanselan.common.IImageMetadata;
+//import org.apache.sanselan.common.byteSources.ByteSource;
+//import org.apache.sanselan.util.Debug;
+//
+///*
+// * This class is just a placeholder. TGA format is not yet supported.
+// */
+//public class TgaImageParser extends ImageParser implements TgaConstants
+//{
+// public TgaImageParser()
+// {
+// this.setByteOrder(BYTE_ORDER_INTEL);
+// setDebug(true);
+// }
+//
+// public String getName()
+// {
+// return "Tga";
+// }
+//
+// public String getDefaultExtension()
+// {
+// return DEFAULT_EXTENSION;
+// }
+//
+// private static final String DEFAULT_EXTENSION = ".tga";
+//
+// private static final String ACCEPTED_EXTENSIONS[] = {
+// ".tga", ".tpic",
+// };
+//
+// protected String[] getAcceptedExtensions()
+// {
+// return ACCEPTED_EXTENSIONS;
+// }
+//
+// protected ImageFormat[] getAcceptedTypes()
+// {
+// return new ImageFormat[]{
+// ImageFormat.IMAGE_FORMAT_TGA, //
+// };
+// }
+//
+// public IImageMetadata getMetadata(ByteSource byteSource, Map params)
+// throws ImageReadException, IOException
+// {
+// return null;
+// }
+//
+// public byte[] getICCProfileBytes(ByteSource byteSource)
+// throws ImageReadException, IOException
+// {
+// return null;
+// }
+//
+// private static final int TGA_FILE_HEADER_LENGTH = 18;
+//
+// public Dimension getImageSize(ByteSource byteSource)
+// throws ImageReadException, IOException
+// {
+//// int length = (int) byteSource.getLength();
+//// if (length < TGA_FILE_HEADER_LENGTH)
+//// return null;
+//
+// InputStream is = byteSource.getInputStream();
+//
+// is.skip(12);
+//
+// int width = this.read2Bytes("image width", is, "image width");
+// int height = this.read2Bytes("image height", is, "image height");
+//
+// return new Dimension(width, height);
+// }
+//
+// private static final int TGA_FILE_FOOTER_LENGTH = 26;
+// private static final String TGA_FILE_FOOTER_SIGNATURE = "TRUEVISION-XFILE";
+//
+// private final boolean isNewTGAFormat(ByteSource byteSource)
+// throws ImageReadException, IOException
+// {
+// int length = (int) byteSource.getLength();
+// if (length < TGA_FILE_FOOTER_LENGTH)
+// return true;
+//
+// InputStream is = byteSource.getInputStream(length
+// - TGA_FILE_FOOTER_LENGTH);
+//
+// byte bytes[] = this.readByteArray("tga_file_footer",
+// TGA_FILE_FOOTER_LENGTH, is, "tga_file_footer");
+//
+// Debug.debug("bytes", bytes);
+//
+// Debug.debug("kTGA_FILE_FOOTER_SIGNATURE", TGA_FILE_FOOTER_SIGNATURE);
+// Debug.debug("kTGA_FILE_FOOTER_SIGNATURE", TGA_FILE_FOOTER_SIGNATURE
+// .length());
+//
+// return this.compareByteArrays(bytes, 8, TGA_FILE_FOOTER_SIGNATURE
+// .getBytes(), 0, TGA_FILE_FOOTER_SIGNATURE.length());
+// }
+//
+// private static final int TGA_IMAGE_TYPE_NO_IMAGE = 0;
+// private static final int UNCOMPRESSED_COLOR_MAPPED = 1;
+// private static final int UNCOMPRESSED_RGB = 2;
+// private static final int UNCOMPRESSED_BLACK_AND_WHITE = 3;
+// private static final int COMPRESSED_COLOR_MAPPED_RLE = 9;
+// private static final int COMPRESSED_RGB_RLE = 10;
+// private static final int COMPRESSED_BLACK_AND_WHITE = 11;
+// private static final int COMPRESSED_COLOR_MAPPED_DATA_HUFFMAN_DELTA_RLE = 32;
+// private static final int COMPRESSED_COLOR_MAPPED_DATA_RLE = 33;
+//
+// public ImageInfo getImageInfo(ByteSource byteSource)
+// throws ImageReadException, IOException
+// {
+//// int length = (int) byteSource.getLength();
+//// if (length < TGA_FILE_HEADER_LENGTH)
+//// return null;
+//
+// InputStream is = byteSource.getInputStream();
+//
+// int id_string_length = this.readByte("id_string_length", is,
+// "id_string_length");
+// int color_map_type = this.readByte("color_map_type", is,
+// "color_map_type");
+// int image_type = this.readByte("image_type", is, "image_type");
+//
+// int color_map_first_entry_index = this.read2Bytes(
+// "color_map_first_entry_index", is,
+// "color_map_first_entry_index");
+// int color_map_length = this.read2Bytes("color_map_length", is,
+// "color_map_length");
+// int color_map_entry_size = this.readByte("color_map_entry_size", is,
+// "color_map_entry_size");
+//
+// int origin_x = this.read2Bytes("origin_x", is, "origin_x");
+// int origin_y = this.read2Bytes("origin_y", is, "origin_y");
+//
+// int width = this.read2Bytes("image width", is, "image width");
+// int height = this.read2Bytes("image height", is, "image height");
+//
+// int pixel_depth = this.readByte("pixel_depth", is, "pixel_depth");
+// int image_descriptor = this.readByte("image_descriptor", is,
+// "image_descriptor");
+// // charles
+//
+// switch (image_type)
+// {
+// case UNCOMPRESSED_COLOR_MAPPED :
+// break;
+// case UNCOMPRESSED_RGB :
+// break;
+// case UNCOMPRESSED_BLACK_AND_WHITE :
+// break;
+// case COMPRESSED_COLOR_MAPPED_RLE :
+// break;
+// case COMPRESSED_RGB_RLE :
+// break;
+// case COMPRESSED_BLACK_AND_WHITE :
+// break;
+// case COMPRESSED_COLOR_MAPPED_DATA_HUFFMAN_DELTA_RLE :
+// break;
+// case COMPRESSED_COLOR_MAPPED_DATA_RLE :
+// break;
+//
+// default :
+//
+// }
+// String FormatDetails;
+// int BitsPerPixel;
+// Vector Comments;
+// ImageFormat Format = ImageFormat.IMAGE_FORMAT_TGA;
+// String FormatName = Format.name;
+// String MimeType = "image/tga";
+// int NumberOfImages = 1; // charles could have thumbnail(s).
+// int PhysicalHeightDpi;
+// float PhysicalHeightInch;
+// int PhysicalWidthDpi;
+// float PhysicalWidthInch;
+// boolean isProgressive = false;
+// boolean isTransparent = pixel_depth > 24;
+// boolean usesPalette;
+// int ColorType;
+//
+// return null;
+// // return new ImageInfo(FormatDetails, BitsPerPixel, Comments, Format,
+// // FormatName, height, MimeType, NumberOfImages,
+// // PhysicalHeightDpi, PhysicalHeightInch, PhysicalWidthDpi,
+// // PhysicalWidthInch, width, isProgressive, isTransparent,
+// // usesPalette, ColorType);
+//
+// // boolean is_new_tga_format = isNewTGAFormat(byteSource);
+// //
+// // Debug.debug("is_new_tga_format", is_new_tga_format);
+// }
+//
+// public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource)
+// throws ImageReadException, IOException
+// {
+// return false;
+// }
+//
+// public BufferedImage getBufferedImage(ByteSource byteSource, Map params)
+// throws ImageReadException, IOException
+// {
+// return null;
+// }
+//
+// // public void writeImage(BufferedImage src, OutputStream os, Map params)
+// // throws ImageWriteException, IOException
+// // {
+// // return false;
+// // }
+//
+// public boolean embedICCProfile(File src, File dst, byte profile[])
+// {
+// return false;
+// }
+//
+//}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tga/TgaImageParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/RawTiffImageData.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/RawTiffImageData.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/RawTiffImageData.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/RawTiffImageData.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,66 @@
+/**
+ *
+ */
+package org.apache.sanselan.formats.tiff;
+
+public abstract class RawTiffImageData
+{
+ public static class Tiles extends RawTiffImageData
+ {
+ public final byte tiles[][];
+
+ public Tiles(final byte tiles[][])
+ {
+ this.tiles = tiles;
+ }
+
+ public byte[][] getRawImageData()
+ {
+ return tiles;
+ }
+
+ public boolean stripsNotTiles()
+ {
+ return false;
+ }
+ }
+
+ public static class Strips extends RawTiffImageData
+ {
+ public final byte strips[][];
+
+ public Strips(final byte strips[][])
+ {
+ this.strips = strips;
+ }
+
+ public byte[][] getRawImageData()
+ {
+ return strips;
+ }
+
+ public boolean stripsNotTiles()
+ {
+ return true;
+ }
+ }
+
+ // public static class Jpeg extends RawImageData
+ // {
+ // public final byte data[][];
+ //
+ // public Jpeg(final byte data[])
+ // {
+ // this.data = data;
+ // }
+ //
+ // public byte[] getRawImageData()
+ // {
+ // return data;
+ // }
+ // }
+
+ public abstract byte[][] getRawImageData();
+
+ public abstract boolean stripsNotTiles();
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/RawTiffImageData.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TagInfo.java
URL: http://svn.apache.org/viewvc/incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TagInfo.java?rev=599250&view=auto
==============================================================================
--- incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TagInfo.java (added)
+++ incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TagInfo.java Wed Nov 28 18:27:05 2007
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sanselan.formats.tiff;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.apache.sanselan.util.Debug;
+
+public class TagInfo
+{
+ public final int tag;
+ public final String name;
+ public final boolean isDate;
+
+ public TagInfo(int tag, String name)
+ {
+ this(tag, name, false);
+ }
+
+ public TagInfo(int tag, String name, boolean is_date)
+ {
+ this.tag = tag;
+ this.name = name.trim();
+ this.isDate = is_date;
+ }
+
+ private static final DateFormat DATE_FORMAT_1 = new SimpleDateFormat(
+ "yyyy:MM:dd HH:mm:ss");
+ private static final DateFormat DATE_FORMAT_2 = new SimpleDateFormat(
+ "yyyy:MM:dd:HH:mm:ss");
+
+ public Object getValue(TiffField entry)
+ {
+ Object o = entry.fieldType.getSimpleValue(entry);
+
+ if (isDate)
+ {
+ String s = (String) o;
+ try
+ {
+ Date date = DATE_FORMAT_1.parse(s);
+ return date;
+ }
+ catch (Exception e)
+ {
+ // Debug.debug(e);
+ }
+ try
+ {
+ Date date = DATE_FORMAT_2.parse(s);
+ return date;
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+
+ return o;
+ }
+
+ public String toString()
+ {
+ return "[TagInfo. tag: " + tag + ", name: " + name
+ + (isDate ? " (data)" : "") + "]";
+ }
+
+}
\ No newline at end of file
Propchange: incubator/sanselan/trunk/src/main/java/org/apache/sanselan/formats/tiff/TagInfo.java
------------------------------------------------------------------------------
svn:eol-style = native