You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSSFPictureData.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import org.apache.poi.ddf.EscherBitmapBlip;
  17. import org.apache.poi.ddf.EscherBlipRecord;
  18. import org.apache.poi.ddf.EscherMetafileBlip;
  19. import org.apache.poi.ss.usermodel.PictureData;
  20. import org.apache.poi.util.PngUtils;
  21. /**
  22. * Represents binary data stored in the file. Eg. A GIF, JPEG etc...
  23. *
  24. * @author Daniel Noll
  25. */
  26. public class HSSFPictureData implements PictureData
  27. {
  28. // MSOBI constants for various formats.
  29. public static final short MSOBI_WMF = 0x2160;
  30. public static final short MSOBI_EMF = 0x3D40;
  31. public static final short MSOBI_PICT = 0x5420;
  32. public static final short MSOBI_PNG = 0x6E00;
  33. public static final short MSOBI_JPEG = 0x46A0;
  34. public static final short MSOBI_DIB = 0x7A80;
  35. // Mask of the bits in the options used to store the image format.
  36. public static final short FORMAT_MASK = (short) 0xFFF0;
  37. /**
  38. * Underlying escher blip record containing the bitmap data.
  39. */
  40. private EscherBlipRecord blip;
  41. /**
  42. * Constructs a picture object.
  43. *
  44. * @param blip the underlying blip record containing the bitmap data.
  45. */
  46. public HSSFPictureData( EscherBlipRecord blip )
  47. {
  48. this.blip = blip;
  49. }
  50. /* (non-Javadoc)
  51. * @see org.apache.poi.hssf.usermodel.PictureData#getData()
  52. */
  53. public byte[] getData()
  54. {
  55. byte[] pictureData = blip.getPicturedata();
  56. //PNG created on MAC may have a 16-byte prefix which prevents successful reading.
  57. //Just cut it off!.
  58. if (PngUtils.matchesPngHeader(pictureData, 16))
  59. {
  60. byte[] png = new byte[pictureData.length-16];
  61. System.arraycopy(pictureData, 16, png, 0, png.length);
  62. pictureData = png;
  63. }
  64. return pictureData;
  65. }
  66. /**
  67. *
  68. * @return format of the picture.
  69. * @see HSSFWorkbook#PICTURE_TYPE_DIB
  70. * @see HSSFWorkbook#PICTURE_TYPE_WMF
  71. * @see HSSFWorkbook#PICTURE_TYPE_EMF
  72. * @see HSSFWorkbook#PICTURE_TYPE_PNG
  73. * @see HSSFWorkbook#PICTURE_TYPE_JPEG
  74. * @see HSSFWorkbook#PICTURE_TYPE_PICT
  75. */
  76. public int getFormat(){
  77. return blip.getRecordId() - (short)0xF018;
  78. }
  79. /**
  80. * @see #getFormat
  81. * @return 'wmf', 'jpeg' etc depending on the format. never <code>null</code>
  82. */
  83. public String suggestFileExtension() {
  84. switch (blip.getRecordId()) {
  85. case EscherMetafileBlip.RECORD_ID_WMF:
  86. return "wmf";
  87. case EscherMetafileBlip.RECORD_ID_EMF:
  88. return "emf";
  89. case EscherMetafileBlip.RECORD_ID_PICT:
  90. return "pict";
  91. case EscherBitmapBlip.RECORD_ID_PNG:
  92. return "png";
  93. case EscherBitmapBlip.RECORD_ID_JPEG:
  94. return "jpeg";
  95. case EscherBitmapBlip.RECORD_ID_DIB:
  96. return "dib";
  97. default:
  98. return "";
  99. }
  100. }
  101. /**
  102. * Returns the mime type for the image
  103. */
  104. public String getMimeType() {
  105. switch (blip.getRecordId()) {
  106. case EscherMetafileBlip.RECORD_ID_WMF:
  107. return "image/x-wmf";
  108. case EscherMetafileBlip.RECORD_ID_EMF:
  109. return "image/x-emf";
  110. case EscherMetafileBlip.RECORD_ID_PICT:
  111. return "image/x-pict";
  112. case EscherBitmapBlip.RECORD_ID_PNG:
  113. return "image/png";
  114. case EscherBitmapBlip.RECORD_ID_JPEG:
  115. return "image/jpeg";
  116. case EscherBitmapBlip.RECORD_ID_DIB:
  117. return "image/bmp";
  118. default:
  119. return "image/unknown";
  120. }
  121. }
  122. }