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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.ddf.EscherBSERecord;
  17. import org.apache.poi.ddf.EscherContainerRecord;
  18. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  19. import org.apache.poi.util.IOUtils;
  20. import org.apache.poi.util.Internal;
  21. import org.apache.poi.util.LittleEndian;
  22. import org.apache.poi.util.Removal;
  23. /**
  24. * Represents a DIB picture data in a PPT file
  25. */
  26. public final class DIB extends Bitmap {
  27. //arbitrarily selected; may need to increase
  28. private static final int MAX_RECORD_LENGTH = 1_000_000;
  29. /**
  30. * Size of the BITMAPFILEHEADER structure preceding the actual DIB bytes
  31. */
  32. private static final int HEADER_SIZE = 14;
  33. /**
  34. * @deprecated Use {@link HSLFSlideShow#addPicture(byte[], PictureType)} or one of it's overloads to create new
  35. * {@link DIB}. This API led to detached {@link DIB} instances (See Bugzilla
  36. * 46122) and prevented adding additional functionality.
  37. */
  38. @Deprecated
  39. @Removal(version = "5.3")
  40. public DIB() {
  41. this(new EscherContainerRecord(), new EscherBSERecord());
  42. }
  43. /**
  44. * Creates a new instance.
  45. *
  46. * @param recordContainer Record tracking all pictures. Should be attached to the slideshow that this picture is
  47. * linked to.
  48. * @param bse Record referencing this picture. Should be attached to the slideshow that this picture is linked to.
  49. */
  50. @Internal
  51. public DIB(EscherContainerRecord recordContainer, EscherBSERecord bse) {
  52. super(recordContainer, bse);
  53. }
  54. @Override
  55. public PictureType getType(){
  56. return PictureType.DIB;
  57. }
  58. /**
  59. * DIB signature is {@code 0x7A80} or {@code 0x7A90}
  60. *
  61. * @return DIB signature ({@code 0x7A80} or {@code 0x7A90})
  62. */
  63. public int getSignature(){
  64. return (getUIDInstanceCount() == 1 ? 0x7A80 : 0x7A90);
  65. }
  66. /**
  67. * Sets the DIB signature - either {@code 0x7A80} or {@code 0x7A90}
  68. */
  69. public void setSignature(int signature) {
  70. switch (signature) {
  71. case 0x7A80:
  72. setUIDInstanceCount(1);
  73. break;
  74. case 0x7A90:
  75. setUIDInstanceCount(2);
  76. break;
  77. default:
  78. throw new IllegalArgumentException(signature+" is not a valid instance/signature value for DIB");
  79. }
  80. }
  81. @Override
  82. public byte[] getData(){
  83. return addBMPHeader ( super.getData() );
  84. }
  85. public static byte[] addBMPHeader(byte[] data){
  86. // bitmap file-header, corresponds to a
  87. // Windows BITMAPFILEHEADER structure
  88. // (For more information, consult the Windows API Programmer's reference )
  89. byte[] header = new byte[HEADER_SIZE];
  90. //Specifies the file type. It must be set to the signature word BM (0x4D42) to indicate bitmap.
  91. LittleEndian.putInt(header, 0, 0x4D42);
  92. // read the size of the image and calculate the overall file size
  93. // and the offset where the bitmap starts
  94. int imageSize = LittleEndian.getInt(data, 0x22 - HEADER_SIZE);
  95. int fileSize = data.length + HEADER_SIZE;
  96. int offset = fileSize - imageSize;
  97. // specifies the size, in bytes, of the bitmap file - must add the length of the header
  98. LittleEndian.putInt(header, 2, fileSize);
  99. // Reserved; set to zero
  100. LittleEndian.putInt(header, 6, 0);
  101. // the offset, i.e. starting address, of the byte where the bitmap data can be found
  102. LittleEndian.putInt(header, 10, offset);
  103. //DIB data is the header + dib bytes
  104. byte[] dib = IOUtils.safelyAllocate(header.length + (long)data.length, MAX_RECORD_LENGTH);
  105. System.arraycopy(header, 0, dib, 0, header.length);
  106. System.arraycopy(data, 0, dib, header.length, data.length);
  107. return dib;
  108. }
  109. @Override
  110. protected byte[] formatImageForSlideshow(byte[] data) {
  111. //cut off the bitmap file-header
  112. byte[] dib = IOUtils.safelyClone(data, HEADER_SIZE, data.length-HEADER_SIZE, data.length);
  113. return super.formatImageForSlideshow(dib);
  114. }
  115. }