1. /*
  2. * @(#)PNGMetadataFormat.java 1.9 04/03/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.imageio.plugins.png;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. import java.util.ListResourceBundle;
  12. import javax.imageio.ImageTypeSpecifier;
  13. import javax.imageio.metadata.IIOMetadataFormat;
  14. import javax.imageio.metadata.IIOMetadataFormatImpl;
  15. public class PNGMetadataFormat extends IIOMetadataFormatImpl {
  16. private static IIOMetadataFormat instance = null;
  17. private static String VALUE_0 = "0";
  18. private static String VALUE_1 = "1";
  19. private static String VALUE_12 = "12";
  20. private static String VALUE_23 = "23";
  21. private static String VALUE_31 = "31";
  22. private static String VALUE_59 = "59";
  23. private static String VALUE_60 = "60";
  24. private static String VALUE_255 = "255";
  25. private static String VALUE_MAX_16 = "65535"; // 2^16 - 1
  26. private static String VALUE_MAX_32 = "2147483647"; // 2^32 - 1
  27. private PNGMetadataFormat() {
  28. super(PNGMetadata.nativeMetadataFormatName,
  29. CHILD_POLICY_SOME);
  30. // root -> IHDR
  31. addElement("IHDR", PNGMetadata.nativeMetadataFormatName,
  32. CHILD_POLICY_EMPTY);
  33. addAttribute("IHDR", "width",
  34. DATATYPE_INTEGER, true, null,
  35. VALUE_1, VALUE_MAX_32, true, true);
  36. addAttribute("IHDR", "height",
  37. DATATYPE_INTEGER, true, null,
  38. VALUE_1, VALUE_MAX_32, true, true);
  39. addAttribute("IHDR", "bitDepth",
  40. DATATYPE_INTEGER, true, null,
  41. Arrays.asList(PNGMetadata.IHDR_bitDepths));
  42. String[] colorTypes = {
  43. "Grayscale", "RGB", "Palette", "GrayAlpha", "RGBAlpha"
  44. };
  45. addAttribute("IHDR", "colorType",
  46. DATATYPE_STRING, true, null,
  47. Arrays.asList(colorTypes));
  48. addAttribute("IHDR", "compressionMethod",
  49. DATATYPE_STRING, true, null,
  50. Arrays.asList(PNGMetadata.IHDR_compressionMethodNames));
  51. addAttribute("IHDR", "filterMethod",
  52. DATATYPE_STRING, true, null,
  53. Arrays.asList(PNGMetadata.IHDR_filterMethodNames));
  54. addAttribute("IHDR", "interlaceMethod",
  55. DATATYPE_STRING, true, null,
  56. Arrays.asList(PNGMetadata.IHDR_interlaceMethodNames));
  57. // root -> PLTE
  58. addElement("PLTE", PNGMetadata.nativeMetadataFormatName,
  59. 1, 256);
  60. // root -> PLTE -> PLTEEntry
  61. addElement("PLTEEntry", "PLTE",
  62. CHILD_POLICY_EMPTY);
  63. addAttribute("PLTEEntry", "index",
  64. DATATYPE_INTEGER, true, null,
  65. VALUE_0, VALUE_255, true, true);
  66. addAttribute("PLTEEntry", "red",
  67. DATATYPE_INTEGER, true, null,
  68. VALUE_0, VALUE_255, true, true);
  69. addAttribute("PLTEEntry", "green",
  70. DATATYPE_INTEGER, true, null,
  71. VALUE_0, VALUE_255, true, true);
  72. addAttribute("PLTEEntry", "blue",
  73. DATATYPE_INTEGER, true, null,
  74. VALUE_0, VALUE_255, true, true);
  75. // root -> bKGD
  76. addElement("bKGD", PNGMetadata.nativeMetadataFormatName,
  77. CHILD_POLICY_CHOICE);
  78. // root -> bKGD -> bKGD_Grayscale
  79. addElement("bKGD_Grayscale", "bKGD",
  80. CHILD_POLICY_EMPTY);
  81. addAttribute("bKGD_Grayscale", "gray",
  82. DATATYPE_INTEGER, true, null,
  83. VALUE_0, VALUE_MAX_16, true, true);
  84. // root -> bKGD -> bKGD_RGB
  85. addElement("bKGD_RGB", "bKGD",
  86. CHILD_POLICY_EMPTY);
  87. addAttribute("bKGD_RGB", "red",
  88. DATATYPE_INTEGER, true, null,
  89. VALUE_0, VALUE_MAX_16, true, true);
  90. addAttribute("bKGD_RGB", "green",
  91. DATATYPE_INTEGER, true, null,
  92. VALUE_0, VALUE_MAX_16, true, true);
  93. addAttribute("bKGD_RGB", "blue",
  94. DATATYPE_INTEGER, true, null,
  95. VALUE_0, VALUE_MAX_16, true, true);
  96. // root -> bKGD -> bKGD_Palette
  97. addElement("bKGD_Palette", "bKGD",
  98. CHILD_POLICY_EMPTY);
  99. addAttribute("bKGD_Palette", "index",
  100. DATATYPE_INTEGER, true, null,
  101. VALUE_0, VALUE_255, true, true);
  102. // root -> cHRM
  103. addElement("cHRM", PNGMetadata.nativeMetadataFormatName,
  104. CHILD_POLICY_EMPTY);
  105. addAttribute("cHRM", "whitePointX",
  106. DATATYPE_INTEGER, true, null,
  107. VALUE_0, VALUE_MAX_16, true, true);
  108. addAttribute("cHRM", "whitePointY",
  109. DATATYPE_INTEGER, true, null,
  110. VALUE_0, VALUE_MAX_16, true, true);
  111. addAttribute("cHRM", "redX",
  112. DATATYPE_INTEGER, true, null,
  113. VALUE_0, VALUE_MAX_16, true, true);
  114. addAttribute("cHRM", "redY",
  115. DATATYPE_INTEGER, true, null,
  116. VALUE_0, VALUE_MAX_16, true, true);
  117. addAttribute("cHRM", "greenX",
  118. DATATYPE_INTEGER, true, null,
  119. VALUE_0, VALUE_MAX_16, true, true);
  120. addAttribute("cHRM", "greenY",
  121. DATATYPE_INTEGER, true, null,
  122. VALUE_0, VALUE_MAX_16, true, true);
  123. addAttribute("cHRM", "blueX",
  124. DATATYPE_INTEGER, true, null,
  125. VALUE_0, VALUE_MAX_16, true, true);
  126. addAttribute("cHRM", "blueY",
  127. DATATYPE_INTEGER, true, null,
  128. VALUE_0, VALUE_MAX_16, true, true);
  129. // root -> gAMA
  130. addElement("gAMA", PNGMetadata.nativeMetadataFormatName,
  131. CHILD_POLICY_EMPTY);
  132. addAttribute("gAMA", "value",
  133. DATATYPE_INTEGER, true, null,
  134. VALUE_0, VALUE_MAX_32, true, true);
  135. // root -> hIST
  136. addElement("hIST", PNGMetadata.nativeMetadataFormatName,
  137. 1, 256);
  138. // root -> hISTEntry
  139. addElement("hISTEntry", "hIST",
  140. CHILD_POLICY_EMPTY);
  141. addAttribute("hISTEntry", "index",
  142. DATATYPE_INTEGER, true, null,
  143. VALUE_0, VALUE_255, true, true);
  144. addAttribute("hISTEntry", "value",
  145. DATATYPE_INTEGER, true, null,
  146. VALUE_0, VALUE_MAX_16, true, true);
  147. // root -> iCCP
  148. addElement("iCCP", PNGMetadata.nativeMetadataFormatName,
  149. CHILD_POLICY_EMPTY);
  150. addAttribute("iCCP", "profileName",
  151. DATATYPE_STRING, true, null);
  152. addAttribute("iCCP", "compressionMethod",
  153. DATATYPE_STRING, true, null,
  154. Arrays.asList(PNGMetadata.iCCP_compressionMethodNames));
  155. addObjectValue("iCCP", byte.class, 0, Integer.MAX_VALUE);
  156. // root -> iTXt
  157. addElement("iTXt", PNGMetadata.nativeMetadataFormatName,
  158. 1, Integer.MAX_VALUE);
  159. // root -> iTXt -> iTXtEntry
  160. addElement("iTXtEntry", "iTXt",
  161. CHILD_POLICY_EMPTY);
  162. addAttribute("iTXtEntry", "keyword",
  163. DATATYPE_STRING, true, null);
  164. addBooleanAttribute("iTXtEntry", "compressionFlag",
  165. false, false);
  166. addAttribute("iTXtEntry", "compressionMethod",
  167. DATATYPE_STRING, true, null);
  168. addAttribute("iTXtEntry", "languageTag",
  169. DATATYPE_STRING, true, null);
  170. addAttribute("iTXtEntry", "translatedKeyword",
  171. DATATYPE_STRING, true, null);
  172. addAttribute("iTXtEntry", "text",
  173. DATATYPE_STRING, true, null);
  174. // root -> pHYS
  175. addElement("pHYS", PNGMetadata.nativeMetadataFormatName,
  176. CHILD_POLICY_EMPTY);
  177. addAttribute("pHYS", "pixelsPerUnitXAxis",
  178. DATATYPE_INTEGER, true, null,
  179. VALUE_0, VALUE_MAX_32, true, true);
  180. addAttribute("pHYS", "pixelsPerUnitYAxis",
  181. DATATYPE_INTEGER, true, null,
  182. VALUE_0, VALUE_MAX_32, true, true);
  183. addAttribute("pHYS", "unitSpecifier",
  184. DATATYPE_STRING, true, null,
  185. Arrays.asList(PNGMetadata.unitSpecifierNames));
  186. // root -> sBIT
  187. addElement("sBIT", PNGMetadata.nativeMetadataFormatName,
  188. CHILD_POLICY_CHOICE);
  189. // root -> sBIT -> sBIT_Grayscale
  190. addElement("sBIT_Grayscale", "sBIT",
  191. CHILD_POLICY_EMPTY);
  192. addAttribute("sBIT_Grayscale", "gray",
  193. DATATYPE_INTEGER, true, null,
  194. VALUE_0, VALUE_255, true, true);
  195. // root -> sBIT -> sBIT_GrayAlpha
  196. addElement("sBIT_GrayAlpha", "sBIT",
  197. CHILD_POLICY_EMPTY);
  198. addAttribute("sBIT_GrayAlpha", "gray",
  199. DATATYPE_INTEGER, true, null,
  200. VALUE_0, VALUE_255, true, true);
  201. addAttribute("sBIT_GrayAlpha", "alpha",
  202. DATATYPE_INTEGER, true, null,
  203. VALUE_0, VALUE_255, true, true);
  204. // root -> sBIT -> sBIT_RGB
  205. addElement("sBIT_RGB", "sBIT",
  206. CHILD_POLICY_EMPTY);
  207. addAttribute("sBIT_RGB", "red",
  208. DATATYPE_INTEGER, true, null,
  209. VALUE_0, VALUE_255, true, true);
  210. addAttribute("sBIT_RGB", "green",
  211. DATATYPE_INTEGER, true, null,
  212. VALUE_0, VALUE_255, true, true);
  213. addAttribute("sBIT_RGB", "blue",
  214. DATATYPE_INTEGER, true, null,
  215. VALUE_0, VALUE_255, true, true);
  216. // root -> sBIT -> sBIT_RGBAlpha
  217. addElement("sBIT_RGBAlpha", "sBIT",
  218. CHILD_POLICY_EMPTY);
  219. addAttribute("sBIT_RGBAlpha", "red",
  220. DATATYPE_INTEGER, true, null,
  221. VALUE_0, VALUE_255, true, true);
  222. addAttribute("sBIT_RGBAlpha", "green",
  223. DATATYPE_INTEGER, true, null,
  224. VALUE_0, VALUE_255, true, true);
  225. addAttribute("sBIT_RGBAlpha", "blue",
  226. DATATYPE_INTEGER, true, null,
  227. VALUE_0, VALUE_255, true, true);
  228. addAttribute("sBIT_RGBAlpha", "alpha",
  229. DATATYPE_INTEGER, true, null,
  230. VALUE_0, VALUE_255, true, true);
  231. // root -> sBIT -> sBIT_Palette
  232. addElement("sBIT_Palette", "sBIT",
  233. CHILD_POLICY_EMPTY);
  234. addAttribute("sBIT_Palette", "red",
  235. DATATYPE_INTEGER, true, null,
  236. VALUE_0, VALUE_255, true, true);
  237. addAttribute("sBIT_Palette", "green",
  238. DATATYPE_INTEGER, true, null,
  239. VALUE_0, VALUE_255, true, true);
  240. addAttribute("sBIT_Palette", "blue",
  241. DATATYPE_INTEGER, true, null,
  242. VALUE_0, VALUE_255, true, true);
  243. // root -> sPLT
  244. addElement("sPLT", PNGMetadata.nativeMetadataFormatName,
  245. 1, 256);
  246. // root -> sPLT -> sPLTEntry
  247. addElement("sPLTEntry", "sPLT",
  248. CHILD_POLICY_EMPTY);
  249. addAttribute("sPLTEntry", "index",
  250. DATATYPE_INTEGER, true, null,
  251. VALUE_0, VALUE_255, true, true);
  252. addAttribute("sPLTEntry", "red",
  253. DATATYPE_INTEGER, true, null,
  254. VALUE_0, VALUE_255, true, true);
  255. addAttribute("sPLTEntry", "green",
  256. DATATYPE_INTEGER, true, null,
  257. VALUE_0, VALUE_255, true, true);
  258. addAttribute("sPLTEntry", "blue",
  259. DATATYPE_INTEGER, true, null,
  260. VALUE_0, VALUE_255, true, true);
  261. addAttribute("sPLTEntry", "alpha",
  262. DATATYPE_INTEGER, true, null,
  263. VALUE_0, VALUE_255, true, true);
  264. // root -> sRGB
  265. addElement("sRGB", PNGMetadata.nativeMetadataFormatName,
  266. CHILD_POLICY_EMPTY);
  267. addAttribute("sRGB", "renderingIntent",
  268. DATATYPE_STRING, true, null,
  269. Arrays.asList(PNGMetadata.renderingIntentNames));
  270. // root -> tEXt
  271. addElement("tEXt", PNGMetadata.nativeMetadataFormatName,
  272. 1, Integer.MAX_VALUE);
  273. // root -> tEXt -> tEXtEntry
  274. addElement("tEXtEntry", "tEXt",
  275. CHILD_POLICY_EMPTY);
  276. addAttribute("tEXtEntry", "keyword",
  277. DATATYPE_STRING, true, null);
  278. addAttribute("tEXtEntry", "value",
  279. DATATYPE_STRING, true, null);
  280. // root -> tIME
  281. addElement("tIME", PNGMetadata.nativeMetadataFormatName,
  282. CHILD_POLICY_EMPTY);
  283. addAttribute("tIME", "year",
  284. DATATYPE_INTEGER, true, null,
  285. VALUE_0, VALUE_MAX_16, true, true);
  286. addAttribute("tIME", "month",
  287. DATATYPE_INTEGER, true, null,
  288. VALUE_1, VALUE_12, true, true);
  289. addAttribute("tIME", "day",
  290. DATATYPE_INTEGER, true, null,
  291. VALUE_1, VALUE_31, true, true);
  292. addAttribute("tIME", "hour",
  293. DATATYPE_INTEGER, true, null,
  294. VALUE_0, VALUE_23, true, true);
  295. addAttribute("tIME", "minute",
  296. DATATYPE_INTEGER, true, null,
  297. VALUE_0, VALUE_59, true, true);
  298. addAttribute("tIME", "second",
  299. DATATYPE_INTEGER, true, null,
  300. VALUE_0, VALUE_60, true, true);
  301. // root -> tRNS
  302. addElement("tRNS", PNGMetadata.nativeMetadataFormatName,
  303. CHILD_POLICY_CHOICE);
  304. // root -> tRNS -> tRNS_Grayscale
  305. addElement("tRNS_Grayscale", "tRNS",
  306. CHILD_POLICY_EMPTY);
  307. addAttribute("tRNS_Grayscale", "gray",
  308. DATATYPE_INTEGER, true, null,
  309. VALUE_0, VALUE_MAX_16, true, true);
  310. // root -> tRNS -> tRNS_RGB
  311. addElement("tRNS_RGB", "tRNS",
  312. CHILD_POLICY_EMPTY);
  313. addAttribute("tRNS_RGB", "red",
  314. DATATYPE_INTEGER, true, null,
  315. VALUE_0, VALUE_MAX_16, true, true);
  316. addAttribute("tRNS_RGB", "green",
  317. DATATYPE_INTEGER, true, null,
  318. VALUE_0, VALUE_MAX_16, true, true);
  319. addAttribute("tRNS_RGB", "blue",
  320. DATATYPE_INTEGER, true, null,
  321. VALUE_0, VALUE_MAX_16, true, true);
  322. // root -> tRNS -> tRNS_Palette
  323. addElement("tRNS_Palette", "tRNS",
  324. CHILD_POLICY_EMPTY);
  325. addAttribute("tRNS_Palette", "index",
  326. DATATYPE_INTEGER, true, null,
  327. VALUE_0, VALUE_255, true, true);
  328. addAttribute("tRNS_Palette", "alpha",
  329. DATATYPE_INTEGER, true, null,
  330. VALUE_0, VALUE_255, true, true);
  331. // root -> zTXt
  332. addElement("zTXt", PNGMetadata.nativeMetadataFormatName,
  333. 1, Integer.MAX_VALUE);
  334. // root -> zTXt -> zTXtEntry
  335. addElement("zTXtEntry", "zTXt",
  336. CHILD_POLICY_EMPTY);
  337. addAttribute("zTXtEntry", "keyword",
  338. DATATYPE_STRING, true, null);
  339. addAttribute("zTXtEntry", "compressionMethod",
  340. DATATYPE_STRING, true, null,
  341. Arrays.asList(PNGMetadata.zTXt_compressionMethodNames));
  342. addAttribute("zTXtEntry", "text",
  343. DATATYPE_STRING, true, null);
  344. // root -> UnknownChunks
  345. addElement("UnknownChunks", PNGMetadata.nativeMetadataFormatName,
  346. 1, Integer.MAX_VALUE);
  347. // root -> UnknownChunks -> UnknownChunk
  348. addElement("UnknownChunk", "UnknownChunks",
  349. CHILD_POLICY_EMPTY);
  350. addAttribute("UnknownChunk", "type",
  351. DATATYPE_STRING, true, null);
  352. addObjectValue("UnknownChunk", byte.class, 0, Integer.MAX_VALUE);
  353. }
  354. public boolean canNodeAppear(String elementName,
  355. ImageTypeSpecifier imageType) {
  356. return true;
  357. }
  358. public static synchronized IIOMetadataFormat getInstance() {
  359. if (instance == null) {
  360. instance = new PNGMetadataFormat();
  361. }
  362. return instance;
  363. }
  364. }