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.

PictureType.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.common.usermodel;
  16. import org.apache.poi.poifs.filesystem.FileMagic;
  17. import java.util.HashMap;
  18. /**
  19. * General enum class to define a picture format/type
  20. *
  21. * @since POI 5.0
  22. */
  23. public enum PictureType {
  24. /** Extended windows meta file */
  25. EMF("image/x-emf", ".emf", 2),
  26. /** Windows Meta File */
  27. WMF("image/x-wmf", ".wmf", 3),
  28. /** Mac PICT format */
  29. PICT("image/x-pict", ".pict", 4),
  30. /** JPEG format */
  31. JPEG("image/jpeg", ".jpg", 5),
  32. /** PNG format */
  33. PNG("image/png", ".png", 6),
  34. /** Device independent bitmap */
  35. DIB("image/dib", ".dib", 7),
  36. /** GIF image format */
  37. GIF("image/gif", ".gif", 8),
  38. /** Tag Image File (.tiff) */
  39. TIFF("image/tiff", ".tif", 9),
  40. /** Encapsulated Postscript (.eps) */
  41. EPS("image/x-eps", ".eps", 10),
  42. /** Windows Bitmap (.bmp) */
  43. BMP("image/x-ms-bmp", ".bmp", 11),
  44. /** WordPerfect graphics (.wpg) */
  45. WPG("image/x-wpg", ".wpg", 12),
  46. /** Microsoft Windows Media Photo image (.wdp) */
  47. WDP("image/vnd.ms-photo", ".wdp", 13),
  48. /** Scalable vector graphics (.svg) - supported by Office 2016 and higher */
  49. SVG("image/svg+xml", ".svg", 14),
  50. /** Unknown picture type - specific to escher bse record */
  51. UNKNOWN("", ".dat", -1),
  52. /** Picture type error - specific to escher bse record */
  53. ERROR("", ".dat", -1),
  54. /** JPEG in the YCCK or CMYK color space. */
  55. CMYKJPEG("image/jpeg", ".jpg", -1),
  56. /** client defined blip type - native-id 32 to 255 */
  57. CLIENT("", ".dat", -1)
  58. ;
  59. private static final HashMap<Integer, PictureType> PICTURE_TYPE_BY_OOXML_ID;
  60. static {
  61. PICTURE_TYPE_BY_OOXML_ID = new HashMap<>();
  62. for (PictureType pictureType : values()) {
  63. if (pictureType.ooxmlId >= -1) {
  64. PICTURE_TYPE_BY_OOXML_ID.put(pictureType.ooxmlId, pictureType);
  65. }
  66. }
  67. }
  68. public final String contentType, extension;
  69. public final int ooxmlId;
  70. PictureType(String contentType, String extension, int ooxmlId) {
  71. this.contentType = contentType;
  72. this.extension = extension;
  73. this.ooxmlId = ooxmlId;
  74. }
  75. public String getContentType() {
  76. return contentType;
  77. }
  78. public String getExtension() {
  79. return extension;
  80. }
  81. public int getOoxmlId() {
  82. return ooxmlId;
  83. }
  84. public static PictureType valueOf(FileMagic fm) {
  85. switch (fm) {
  86. case BMP:
  87. return PictureType.BMP;
  88. case GIF:
  89. return PictureType.GIF;
  90. case JPEG:
  91. return PictureType.JPEG;
  92. case PNG:
  93. return PictureType.PNG;
  94. case XML:
  95. // this is quite fuzzy, to suppose all XMLs are SVGs when handling pictures ...
  96. return PictureType.SVG;
  97. case WMF:
  98. return PictureType.WMF;
  99. case EMF:
  100. return PictureType.EMF;
  101. case TIFF:
  102. return PictureType.TIFF;
  103. default:
  104. case UNKNOWN:
  105. return PictureType.UNKNOWN;
  106. }
  107. }
  108. /**
  109. * @param ooxmlId for PictureType
  110. * @return PictureType, null if ooxmlId does not match any PictureTypes
  111. */
  112. public static PictureType findByOoxmlId(int ooxmlId) {
  113. return PICTURE_TYPE_BY_OOXML_ID.get(ooxmlId);
  114. }
  115. }