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.

DIB.java 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. public 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</code>
  38. *
  39. * @return DIB signature (<code>0x7A80</code>)
  40. */
  41. public int getSignature(){
  42. return 0x7A80;
  43. }
  44. public byte[] getData(){
  45. return addBMPHeader ( super.getData() );
  46. }
  47. public static byte[] addBMPHeader(byte[] data){
  48. // bitmap file-header, corresponds to a
  49. // Windows BITMAPFILEHEADER structure
  50. // (For more information, consult the Windows API Programmer's reference )
  51. byte[] header = new byte[HEADER_SIZE];
  52. //Specifies the file type. It must be set to the signature word BM (0x4D42) to indicate bitmap.
  53. LittleEndian.putInt(header, 0, 0x4D42);
  54. // read the size of the image and calculate the overall file size
  55. // and the offset where the bitmap starts
  56. int imageSize = LittleEndian.getInt(data, 0x22 - HEADER_SIZE);
  57. int fileSize = data.length + HEADER_SIZE;
  58. int offset = fileSize - imageSize;
  59. // specifies the size, in bytes, of the bitmap file - must add the length of the header
  60. LittleEndian.putInt(header, 2, fileSize);
  61. // Reserved; set to zero
  62. LittleEndian.putInt(header, 6, 0);
  63. // the offset, i.e. starting address, of the byte where the bitmap data can be found
  64. LittleEndian.putInt(header, 10, offset);
  65. //DIB data is the header + dib bytes
  66. byte[] dib = new byte[header.length + data.length];
  67. System.arraycopy(header, 0, dib, 0, header.length);
  68. System.arraycopy(data, 0, dib, header.length, data.length);
  69. return dib;
  70. }
  71. public void setData(byte[] data) throws IOException {
  72. //cut off the bitmap file-header
  73. byte[] dib = new byte[data.length-HEADER_SIZE];
  74. System.arraycopy(data, HEADER_SIZE, dib, 0, dib.length);
  75. super.setData(dib);
  76. }
  77. }