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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.hslf.blip;
  16. import org.apache.poi.hslf.model.Picture;
  17. import org.apache.poi.util.LittleEndian;
  18. import java.io.IOException;
  19. /**
  20. * Represents a DIB picture data in a PPT file
  21. *
  22. * @author Yegor Kozlov
  23. */
  24. public final class DIB extends Bitmap {
  25. /**
  26. * Size of the BITMAPFILEHEADER structure preceding the actual DIB bytes
  27. */
  28. private static final int HEADER_SIZE = 14;
  29. /**
  30. * @return type of this picture
  31. * @see org.apache.poi.hslf.model.Picture#DIB
  32. */
  33. public int getType(){
  34. return Picture.DIB;
  35. }
  36. /**
  37. * DIB signature is {@code 0x7A80} or {@code 0x7A90}
  38. *
  39. * @return DIB signature ({@code 0x7A80} or {@code 0x7A90})
  40. */
  41. public int getSignature(){
  42. return (uidInstanceCount == 1 ? 0x7A80 : 0x7A90);
  43. }
  44. /**
  45. * Sets the DIB signature - either {@code 0x7A80} or {@code 0x7A90}
  46. */
  47. public void setSignature(int signature) {
  48. switch (signature) {
  49. case 0x7A80:
  50. uidInstanceCount = 1;
  51. break;
  52. case 0x7A90:
  53. uidInstanceCount = 2;
  54. break;
  55. default:
  56. throw new IllegalArgumentException(signature+" is not a valid instance/signature value for DIB");
  57. }
  58. }
  59. public byte[] getData(){
  60. return addBMPHeader ( super.getData() );
  61. }
  62. public static byte[] addBMPHeader(byte[] data){
  63. // bitmap file-header, corresponds to a
  64. // Windows BITMAPFILEHEADER structure
  65. // (For more information, consult the Windows API Programmer's reference )
  66. byte[] header = new byte[HEADER_SIZE];
  67. //Specifies the file type. It must be set to the signature word BM (0x4D42) to indicate bitmap.
  68. LittleEndian.putInt(header, 0, 0x4D42);
  69. // read the size of the image and calculate the overall file size
  70. // and the offset where the bitmap starts
  71. int imageSize = LittleEndian.getInt(data, 0x22 - HEADER_SIZE);
  72. int fileSize = data.length + HEADER_SIZE;
  73. int offset = fileSize - imageSize;
  74. // specifies the size, in bytes, of the bitmap file - must add the length of the header
  75. LittleEndian.putInt(header, 2, fileSize);
  76. // Reserved; set to zero
  77. LittleEndian.putInt(header, 6, 0);
  78. // the offset, i.e. starting address, of the byte where the bitmap data can be found
  79. LittleEndian.putInt(header, 10, offset);
  80. //DIB data is the header + dib bytes
  81. byte[] dib = new byte[header.length + data.length];
  82. System.arraycopy(header, 0, dib, 0, header.length);
  83. System.arraycopy(data, 0, dib, header.length, data.length);
  84. return dib;
  85. }
  86. public void setData(byte[] data) throws IOException {
  87. //cut off the bitmap file-header
  88. byte[] dib = new byte[data.length-HEADER_SIZE];
  89. System.arraycopy(data, HEADER_SIZE, dib, 0, dib.length);
  90. super.setData(dib);
  91. }
  92. }