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: ==================================================================== */
017: package org.apache.poi.hslf.blip;
018:
019: import org.apache.poi.hslf.model.Picture;
020: import org.apache.poi.hslf.model.Shape;
021: import org.apache.poi.hslf.exceptions.HSLFException;
022: import org.apache.poi.util.LittleEndian;
023:
024: import java.io.*;
025: import java.util.zip.InflaterInputStream;
026: import java.util.zip.DeflaterOutputStream;
027:
028: /**
029: * Represents Macintosh PICT picture data.
030: *
031: * @author Yegor Kozlov
032: */
033: public class PICT extends Metafile {
034:
035: public PICT() {
036: super ();
037: }
038:
039: /**
040: * Extract compressed PICT data from a ppt
041: */
042: public byte[] getData() {
043: byte[] rawdata = getRawData();
044: try {
045: byte[] macheader = new byte[512];
046: ByteArrayOutputStream out = new ByteArrayOutputStream();
047: out.write(macheader);
048: int pos = CHECKSUM_SIZE;
049: byte[] pict;
050: try {
051: pict = read(rawdata, pos);
052: } catch (IOException e) {
053: //weird MAC behaviour.
054: //if failed to read right after the checksum - skip 16 bytes and try again
055: pict = read(rawdata, pos + 16);
056: }
057: out.write(pict);
058: return out.toByteArray();
059: } catch (IOException e) {
060: throw new HSLFException(e);
061: }
062: }
063:
064: private byte[] read(byte[] data, int pos) throws IOException {
065: ByteArrayOutputStream out = new ByteArrayOutputStream();
066: ByteArrayInputStream bis = new ByteArrayInputStream(data);
067: Header header = new Header();
068: header.read(data, pos);
069: bis.skip(pos + header.getSize());
070: InflaterInputStream inflater = new InflaterInputStream(bis);
071: byte[] chunk = new byte[4096];
072: int count;
073: while ((count = inflater.read(chunk)) >= 0) {
074: out.write(chunk, 0, count);
075: }
076: inflater.close();
077: return out.toByteArray();
078: }
079:
080: public void setData(byte[] data) throws IOException {
081: int pos = 512; //skip the first 512 bytes - they are MAC specific crap
082: byte[] compressed = compress(data, pos, data.length - pos);
083:
084: Header header = new Header();
085: header.wmfsize = data.length - 512;
086: //we don't have a PICT reader in java, have to set default image size 200x200
087: header.bounds = new java.awt.Rectangle(0, 0, 200, 200);
088: header.size = new java.awt.Dimension(header.bounds.width
089: * Shape.EMU_PER_POINT, header.bounds.height
090: * Shape.EMU_PER_POINT);
091: header.zipsize = compressed.length;
092:
093: byte[] checksum = getChecksum(data);
094: ByteArrayOutputStream out = new ByteArrayOutputStream();
095: out.write(checksum);
096:
097: out.write(new byte[16]); //16-byte prefix which is safe to ignore
098: header.write(out);
099: out.write(compressed);
100:
101: setRawData(out.toByteArray());
102: }
103:
104: /**
105: * @see org.apache.poi.hslf.model.Picture#PICT
106: */
107: public int getType() {
108: return Picture.PICT;
109: }
110:
111: /**
112: * PICT signature is <code>0x5430</code>
113: *
114: * @return PICT signature (<code>0x5430</code>)
115: */
116: public int getSignature() {
117: return 0x5430;
118: }
119:
120: }
|