01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17: package org.apache.poi.hslf.blip;
18:
19: import org.apache.poi.hslf.model.Picture;
20: import org.apache.poi.hslf.model.Shape;
21: import org.apache.poi.hslf.exceptions.HSLFException;
22:
23: import java.io.ByteArrayOutputStream;
24: import java.io.InputStream;
25: import java.io.ByteArrayInputStream;
26: import java.io.IOException;
27: import java.util.zip.InflaterInputStream;
28: import java.util.zip.DeflaterOutputStream;
29:
30: /**
31: * Represents EMF (Windows Enhanced Metafile) picture data.
32: *
33: * @author Yegor Kozlov
34: */
35: public class EMF extends Metafile {
36:
37: /**
38: * Extract compressed EMF data from a ppt
39: */
40: public byte[] getData() {
41: try {
42: byte[] rawdata = getRawData();
43:
44: ByteArrayOutputStream out = new ByteArrayOutputStream();
45: InputStream is = new ByteArrayInputStream(rawdata);
46: Header header = new Header();
47: header.read(rawdata, CHECKSUM_SIZE);
48: is.skip(header.getSize() + CHECKSUM_SIZE);
49:
50: InflaterInputStream inflater = new InflaterInputStream(is);
51: byte[] chunk = new byte[4096];
52: int count;
53: while ((count = inflater.read(chunk)) >= 0) {
54: out.write(chunk, 0, count);
55: }
56: inflater.close();
57: return out.toByteArray();
58: } catch (IOException e) {
59: throw new HSLFException(e);
60: }
61: }
62:
63: public void setData(byte[] data) throws IOException {
64: byte[] compressed = compress(data, 0, data.length);
65:
66: Header header = new Header();
67: header.wmfsize = data.length;
68: //we don't have a EMF reader in java, have to set default image size 200x200
69: header.bounds = new java.awt.Rectangle(0, 0, 200, 200);
70: header.size = new java.awt.Dimension(header.bounds.width
71: * Shape.EMU_PER_POINT, header.bounds.height
72: * Shape.EMU_PER_POINT);
73: header.zipsize = compressed.length;
74:
75: byte[] checksum = getChecksum(data);
76: ByteArrayOutputStream out = new ByteArrayOutputStream();
77: out.write(checksum);
78: header.write(out);
79: out.write(compressed);
80:
81: setRawData(out.toByteArray());
82: }
83:
84: public int getType() {
85: return Picture.EMF;
86: }
87:
88: /**
89: * EMF signature is <code>0x3D40</code>
90: *
91: * @return EMF signature (<code>0x3D40</code>)
92: */
93: public int getSignature() {
94: return 0x3D40;
95: }
96: }
|