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.

ColorSpaceCache.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.util;
  19. import java.awt.color.ColorSpace;
  20. import java.awt.color.ICC_Profile;
  21. import java.util.Collections;
  22. import java.util.Map;
  23. import javax.xml.transform.Source;
  24. import javax.xml.transform.URIResolver;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.xmlgraphics.java2d.color.ICCColorSpaceExt;
  29. /**
  30. * Map with cached ICC based ColorSpace objects.
  31. */
  32. public class ColorSpaceCache {
  33. /** logger instance */
  34. private static Log log = LogFactory.getLog(ColorSpaceCache.class);
  35. private URIResolver resolver;
  36. private Map colorSpaceMap = Collections.synchronizedMap(new java.util.HashMap());
  37. /**
  38. * Default constructor
  39. * @param resolver uri resolver
  40. */
  41. public ColorSpaceCache(URIResolver resolver) {
  42. this.resolver = resolver;
  43. }
  44. /**
  45. * Create (if needed) and return an ICC ColorSpace instance.
  46. *
  47. * The ICC profile source is taken from the src attribute of the color-profile FO element.
  48. * If the ICC ColorSpace is not yet in the cache a new one is created and stored in the cache.
  49. *
  50. * The FOP URI resolver is used to try and locate the ICC file.
  51. * If that fails null is returned.
  52. *
  53. * @param profileName the profile name
  54. * @param base a base URI to resolve relative URIs
  55. * @param iccProfileSrc ICC Profile source to return a ColorSpace for
  56. * @param renderingIntent overriding rendering intent (see {@link ICCColorSpaceExt}.*)
  57. * @return ICC ColorSpace object or null if ColorSpace could not be created
  58. */
  59. public ColorSpace get(String profileName, String base, String iccProfileSrc,
  60. int renderingIntent) {
  61. String key = profileName + ":" + base + iccProfileSrc;
  62. ColorSpace colorSpace = null;
  63. if (!colorSpaceMap.containsKey(key)) {
  64. try {
  65. ICC_Profile iccProfile = null;
  66. // First attempt to use the FOP URI resolver to locate the ICC
  67. // profile
  68. Source src = resolver.resolve(iccProfileSrc, base);
  69. if (src != null && src instanceof StreamSource) {
  70. // FOP URI resolver found ICC profile - create ICC profile
  71. // from the Source
  72. iccProfile = ICC_Profile.getInstance(((StreamSource) src)
  73. .getInputStream());
  74. } else {
  75. // TODO - Would it make sense to fall back on VM ICC
  76. // resolution
  77. // Problem is the cache might be more difficult to maintain
  78. //
  79. // FOP URI resolver did not find ICC profile - perhaps the
  80. // Java VM can find it?
  81. // iccProfile = ICC_Profile.getInstance(iccProfileSrc);
  82. }
  83. if (iccProfile != null) {
  84. colorSpace = new ICCColorSpaceExt(iccProfile, renderingIntent,
  85. profileName, iccProfileSrc);
  86. }
  87. } catch (Exception e) {
  88. // Ignore exception - will be logged a bit further down
  89. // (colorSpace == null case)
  90. }
  91. if (colorSpace != null) {
  92. // Put in cache (not when VM resolved it as we can't control
  93. colorSpaceMap.put(key, colorSpace);
  94. } else {
  95. // TODO To avoid an excessive amount of warnings perhaps
  96. // register a null ColorMap in the colorSpaceMap
  97. log.warn("Color profile '" + iccProfileSrc + "' not found.");
  98. }
  99. } else {
  100. colorSpace = (ColorSpace)colorSpaceMap.get(key);
  101. }
  102. return colorSpace;
  103. }
  104. }