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

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