1. /*
  2. * @(#)JPEGMetadataFormat.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.imageio.plugins.jpeg;
  8. import javax.imageio.metadata.IIOMetadataFormat;
  9. import javax.imageio.metadata.IIOMetadataFormatImpl;
  10. import javax.imageio.ImageTypeSpecifier;
  11. import javax.imageio.plugins.jpeg.JPEGQTable;
  12. import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
  13. import java.util.List;
  14. import java.util.ArrayList;
  15. abstract class JPEGMetadataFormat extends IIOMetadataFormatImpl {
  16. // 2-byte length reduces max to 65533
  17. private static final int MAX_JPEG_DATA_SIZE = 65533;
  18. String resourceBaseName = this.getClass().getName() + "Resources";
  19. JPEGMetadataFormat(String formatName, int childPolicy) {
  20. super(formatName, childPolicy);
  21. setResourceBaseName(resourceBaseName);
  22. }
  23. // Format shared between image and stream formats
  24. void addStreamElements(String parentName) {
  25. addElement("dqt", parentName, 1, 4);
  26. addElement("dqtable", "dqt", CHILD_POLICY_EMPTY);
  27. addAttribute("dqtable",
  28. "elementPrecision",
  29. DATATYPE_INTEGER,
  30. false,
  31. "0");
  32. List tabids = new ArrayList();
  33. tabids.add("0");
  34. tabids.add("1");
  35. tabids.add("2");
  36. tabids.add("3");
  37. addAttribute("dqtable",
  38. "qtableId",
  39. DATATYPE_INTEGER,
  40. true,
  41. null,
  42. tabids);
  43. addObjectValue("dqtable",
  44. JPEGQTable.class,
  45. true,
  46. null);
  47. addElement("dht", parentName, 1, 4);
  48. addElement("dhtable", "dht", CHILD_POLICY_EMPTY);
  49. List classes = new ArrayList();
  50. classes.add("0");
  51. classes.add("1");
  52. addAttribute("dhtable",
  53. "class",
  54. DATATYPE_INTEGER,
  55. true,
  56. null,
  57. classes);
  58. addAttribute("dhtable",
  59. "htableId",
  60. DATATYPE_INTEGER,
  61. true,
  62. null,
  63. tabids);
  64. addObjectValue("dhtable",
  65. JPEGHuffmanTable.class,
  66. true,
  67. null);
  68. addElement("dri", parentName, CHILD_POLICY_EMPTY);
  69. addAttribute("dri",
  70. "interval",
  71. DATATYPE_INTEGER,
  72. true,
  73. null,
  74. "0", "65535",
  75. true, true);
  76. addElement("com", parentName, CHILD_POLICY_EMPTY);
  77. addAttribute("com",
  78. "comment",
  79. DATATYPE_STRING,
  80. false,
  81. null);
  82. addObjectValue("com", byte[].class, 1, MAX_JPEG_DATA_SIZE);
  83. addElement("unknown", parentName, CHILD_POLICY_EMPTY);
  84. addAttribute("unknown",
  85. "MarkerTag",
  86. DATATYPE_INTEGER,
  87. true,
  88. null,
  89. "0", "255",
  90. true, true);
  91. addObjectValue("unknown", byte[].class, 1, MAX_JPEG_DATA_SIZE);
  92. }
  93. public boolean canNodeAppear(String elementName,
  94. ImageTypeSpecifier imageType) {
  95. // Just check if it appears in the format
  96. if (isInSubtree(elementName, getRootName())){
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Returns <code>true</code> if the named element occurs in the
  103. * subtree of the format starting with the node named by
  104. * <code>subtreeName</code>, including the node
  105. * itself. <code>subtreeName</code> may be any node in
  106. * the format. If it is not, an
  107. * <code>IllegalArgumentException</code> is thrown.
  108. */
  109. protected boolean isInSubtree(String elementName,
  110. String subtreeName) {
  111. if (elementName.equals(subtreeName)) {
  112. return true;
  113. }
  114. String [] children = getChildNames(elementName);
  115. for (int i=0; i < children.length; i++) {
  116. if (isInSubtree(elementName, children[i])) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. }