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.

JAIImage.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.image;
  19. // AWT
  20. import java.awt.image.ColorModel;
  21. import java.awt.image.IndexColorModel;
  22. import java.awt.image.BufferedImage;
  23. import java.awt.Color;
  24. // JAI
  25. import javax.media.jai.JAI;
  26. import javax.media.jai.RenderedOp;
  27. import org.apache.commons.io.IOUtils;
  28. // Sun codec
  29. import com.sun.media.jai.codec.FileCacheSeekableStream;
  30. /**
  31. * FopImage object using JAI.
  32. *
  33. * @see AbstractFopImage
  34. * @see FopImage
  35. */
  36. public class JAIImage extends AbstractFopImage {
  37. /**
  38. * Create a new JAI image.
  39. *
  40. * @param imgInfo the image info for this JAI image
  41. */
  42. public JAIImage(FopImage.ImageInfo imgInfo) {
  43. super(imgInfo);
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. protected boolean loadDimensions() {
  49. if (this.bitmaps == null) {
  50. loadImage();
  51. }
  52. return this.bitmaps != null;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. protected boolean loadBitmap() {
  58. if (this.bitmaps == null) {
  59. loadImage();
  60. }
  61. return this.bitmaps != null;
  62. }
  63. /**
  64. * Loads the image from the inputstream
  65. */
  66. protected void loadImage() {
  67. com.sun.media.jai.codec.FileCacheSeekableStream seekableInput = null;
  68. RenderedOp imageOp = null;
  69. try {
  70. seekableInput = new FileCacheSeekableStream(inputStream);
  71. imageOp = JAI.create("stream", seekableInput);
  72. this.height = imageOp.getHeight();
  73. this.width = imageOp.getWidth();
  74. ColorModel cm = imageOp.getColorModel();
  75. //this.bitsPerPixel = 8;
  76. this.bitsPerPixel = cm.getPixelSize();
  77. // TODO: the getRGB() function converts the image into the RGB
  78. // colorspace. However, here we assume the image colorspace is kept.
  79. // It should be either one of them, but not both. Unfortunately
  80. // there are other hacks for images in the CMYK colorspace (e.g. in
  81. // the PDF output) that would need to be changed as well.
  82. //this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
  83. this.colorSpace = cm.getColorSpace();
  84. BufferedImage imageData = imageOp.getAsBufferedImage();
  85. int[] tmpMap = imageData.getRGB(0, 0, this.width,
  86. this.height, null, 0, this.width);
  87. if (cm.hasAlpha()) {
  88. // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
  89. int transparencyType = cm.getTransparency();
  90. if (transparencyType == java.awt.Transparency.OPAQUE) {
  91. this.isTransparent = false;
  92. } else if (transparencyType == java.awt.Transparency.BITMASK) {
  93. if (cm instanceof IndexColorModel) {
  94. this.isTransparent = false;
  95. byte[] alphas = new byte[
  96. ((IndexColorModel) cm).getMapSize()];
  97. byte[] reds = new byte[
  98. ((IndexColorModel) cm).getMapSize()];
  99. byte[] greens = new byte[
  100. ((IndexColorModel) cm).getMapSize()];
  101. byte[] blues = new byte[
  102. ((IndexColorModel) cm).getMapSize()];
  103. ((IndexColorModel) cm).getAlphas(alphas);
  104. ((IndexColorModel) cm).getReds(reds);
  105. ((IndexColorModel) cm).getGreens(greens);
  106. ((IndexColorModel) cm).getBlues(blues);
  107. for (int i = 0;
  108. i < ((IndexColorModel) cm).getMapSize();
  109. i++) {
  110. if ((alphas[i] & 0xFF) == 0) {
  111. this.isTransparent = true;
  112. this.transparentColor = new Color(
  113. (int)(reds[i] & 0xFF),
  114. (int)(greens[i] & 0xFF),
  115. (int)(blues[i] & 0xFF));
  116. break;
  117. }
  118. }
  119. } else {
  120. // TRANSLUCENT
  121. /*
  122. * this.isTransparent = false;
  123. * for (int i = 0; i < this.width * this.height; i++) {
  124. * if (cm.getAlpha(tmpMap[i]) == 0) {
  125. * this.isTransparent = true;
  126. * this.transparentColor = new PDFColor(cm.getRed(tmpMap[i]),
  127. * cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
  128. * break;
  129. * }
  130. * }
  131. * // or use special API...
  132. */
  133. this.isTransparent = false;
  134. }
  135. } else {
  136. this.isTransparent = false;
  137. }
  138. } else {
  139. this.isTransparent = false;
  140. }
  141. // Should take care of the ColorSpace and bitsPerPixel
  142. this.bitmaps = new byte[this.width * this.height * 3];
  143. for (int i = 0; i < this.height; i++) {
  144. for (int j = 0; j < this.width; j++) {
  145. int p = tmpMap[i * this.width + j];
  146. int r = (p >> 16) & 0xFF;
  147. int g = (p >> 8) & 0xFF;
  148. int b = (p) & 0xFF;
  149. this.bitmaps[3 * (i * this.width + j)] = (byte)(r & 0xFF);
  150. this.bitmaps[3 * (i * this.width + j) + 1] = (byte)(g & 0xFF);
  151. this.bitmaps[3 * (i * this.width + j) + 2] = (byte)(b & 0xFF);
  152. }
  153. }
  154. } catch (Exception ex) {
  155. log.error("Error while loading image (JAI): " + ex.getMessage(), ex);
  156. } finally {
  157. IOUtils.closeQuietly(inputStream);
  158. inputStream = null;
  159. if (imageOp != null) {
  160. imageOp.dispose();
  161. }
  162. if (seekableInput != null) {
  163. IOUtils.closeQuietly(seekableInput);
  164. }
  165. }
  166. }
  167. /** {@inheritDoc} */
  168. protected boolean loadOriginalData() {
  169. return loadDefaultOriginalData();
  170. }
  171. }