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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.ICCColorSpaceWithIntent;
  29. import org.apache.xmlgraphics.java2d.color.RenderingIntent;
  30. /**
  31. * Map with cached ICC based ColorSpace objects.
  32. */
  33. public class ColorSpaceCache {
  34. /** logger instance */
  35. private static Log log = LogFactory.getLog(ColorSpaceCache.class);
  36. private URIResolver resolver;
  37. private Map<String, ColorSpace> colorSpaceMap
  38. = Collections.synchronizedMap(new java.util.HashMap<String, ColorSpace>());
  39. /**
  40. * Default constructor
  41. * @param resolver uri resolver
  42. */
  43. public ColorSpaceCache(URIResolver resolver) {
  44. this.resolver = resolver;
  45. }
  46. /**
  47. * Create (if needed) and return an ICC ColorSpace instance.
  48. *
  49. * The ICC profile source is taken from the src attribute of the color-profile FO element.
  50. * If the ICC ColorSpace is not yet in the cache a new one is created and stored in the cache.
  51. *
  52. * The FOP URI resolver is used to try and locate the ICC file.
  53. * If that fails null is returned.
  54. *
  55. * @param profileName the profile name
  56. * @param base a base URI to resolve relative URIs
  57. * @param iccProfileSrc ICC Profile source to return a ColorSpace for
  58. * @param renderingIntent overriding rendering intent
  59. * @return ICC ColorSpace object or null if ColorSpace could not be created
  60. */
  61. public ColorSpace get(String profileName, String base, String iccProfileSrc,
  62. RenderingIntent renderingIntent) {
  63. String key = profileName + ":" + base + iccProfileSrc;
  64. ColorSpace colorSpace = null;
  65. if (!colorSpaceMap.containsKey(key)) {
  66. try {
  67. ICC_Profile iccProfile = null;
  68. // First attempt to use the FOP URI resolver to locate the ICC
  69. // profile
  70. Source src = resolver.resolve(iccProfileSrc, base);
  71. if (src != null && src instanceof StreamSource) {
  72. // FOP URI resolver found ICC profile - create ICC profile
  73. // from the Source
  74. iccProfile = ICC_Profile.getInstance(((StreamSource) src)
  75. .getInputStream());
  76. } else {
  77. // TODO - Would it make sense to fall back on VM ICC
  78. // resolution
  79. // Problem is the cache might be more difficult to maintain
  80. //
  81. // FOP URI resolver did not find ICC profile - perhaps the
  82. // Java VM can find it?
  83. // iccProfile = ICC_Profile.getInstance(iccProfileSrc);
  84. }
  85. if (iccProfile != null) {
  86. colorSpace = new ICCColorSpaceWithIntent(iccProfile, renderingIntent,
  87. profileName, iccProfileSrc);
  88. }
  89. } catch (Exception e) {
  90. // Ignore exception - will be logged a bit further down
  91. // (colorSpace == null case)
  92. }
  93. if (colorSpace != null) {
  94. // Put in cache (not when VM resolved it as we can't control
  95. colorSpaceMap.put(key, colorSpace);
  96. } else {
  97. // TODO To avoid an excessive amount of warnings perhaps
  98. // register a null ColorMap in the colorSpaceMap
  99. log.warn("Color profile '" + iccProfileSrc + "' not found.");
  100. }
  101. } else {
  102. colorSpace = colorSpaceMap.get(key);
  103. }
  104. return colorSpace;
  105. }
  106. }