diff options
author | Yegor Kozlov <yegor@apache.org> | 2012-08-04 05:30:19 +0000 |
---|---|---|
committer | Yegor Kozlov <yegor@apache.org> | 2012-08-04 05:30:19 +0000 |
commit | 63f3987cb0dac320bb51c7418181f55272311190 (patch) | |
tree | 664514eb9666c72bbe81e6e824ec952d9c37574b /src/java/org/apache | |
parent | 27f82281eaf649731c46eaed835d29d9394a1dde (diff) | |
download | poi-63f3987cb0dac320bb51c7418181f55272311190.tar.gz poi-63f3987cb0dac320bb51c7418181f55272311190.zip |
Bugzilla 53446 - Fixed some problems extracting PNGs
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1369263 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache')
-rw-r--r-- | src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java | 14 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/PngUtils.java | 57 |
2 files changed, 70 insertions, 1 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java index 0516cccf6b..69bdae1521 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java @@ -22,6 +22,7 @@ import org.apache.poi.ddf.EscherBitmapBlip; import org.apache.poi.ddf.EscherBlipRecord; import org.apache.poi.ddf.EscherMetafileBlip; import org.apache.poi.ss.usermodel.PictureData; +import org.apache.poi.util.PngUtils; /** * Represents binary data stored in the file. Eg. A GIF, JPEG etc... @@ -60,7 +61,18 @@ public class HSSFPictureData implements PictureData */ public byte[] getData() { - return blip.getPicturedata(); + byte[] pictureData = blip.getPicturedata(); + + //PNG created on MAC may have a 16-byte prefix which prevents successful reading. + //Just cut it off!. + if (PngUtils.matchesPngHeader(pictureData, 16)) + { + byte[] png = new byte[pictureData.length-16]; + System.arraycopy(pictureData, 16, png, 0, png.length); + pictureData = png; + } + + return pictureData; } /** diff --git a/src/java/org/apache/poi/util/PngUtils.java b/src/java/org/apache/poi/util/PngUtils.java new file mode 100644 index 0000000000..785675830b --- /dev/null +++ b/src/java/org/apache/poi/util/PngUtils.java @@ -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.poi.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public final class PngUtils {
+
+ /**
+ * File header for PNG format.
+ */
+ private static final byte[] PNG_FILE_HEADER =
+ new byte[] { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
+
+ private PngUtils() {
+ // no instances of this class
+ }
+
+ /**
+ * Checks if the offset matches the PNG header.
+ *
+ * @param data the data to check.
+ * @param offset the offset to check at.
+ * @return {@code true} if the offset matches.
+ */
+ public static boolean matchesPngHeader(byte[] data, int offset) {
+ if (data == null || data.length - offset < PNG_FILE_HEADER.length) {
+ return false;
+ }
+
+ for (int i = 0; i < PNG_FILE_HEADER.length; i++) {
+ if (PNG_FILE_HEADER[i] != data[i + offset]) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
|