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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.image;
  8. // Java
  9. import java.net.URL;
  10. import java.io.InputStream;
  11. import java.io.BufferedInputStream;
  12. // AWT
  13. import java.awt.image.ColorModel;
  14. import java.awt.image.IndexColorModel;
  15. import java.awt.image.BufferedImage;
  16. // JAI
  17. import javax.media.jai.JAI;
  18. import javax.media.jai.RenderedOp;
  19. // Sun codec
  20. import com.sun.media.jai.codec.FileCacheSeekableStream;
  21. // FOP
  22. import org.apache.fop.datatypes.ColorSpace;
  23. import org.apache.fop.pdf.PDFColor;
  24. import org.apache.fop.image.analyser.ImageReader;
  25. /**
  26. * FopImage object using JAI.
  27. * @author Eric SCHAEFFER
  28. * @see AbstractFopImage
  29. * @see FopImage
  30. */
  31. public class JAIImage extends AbstractFopImage {
  32. public JAIImage(URL href) {
  33. super(href);
  34. }
  35. public JAIImage(URL href, ImageReader imgReader) {
  36. super(href, imgReader);
  37. }
  38. protected void loadImage() {
  39. try {
  40. InputStream inputStream = this.m_href.openStream();
  41. /*
  42. * BufferedInputStream inputStream = this.m_imageReader.getInputStream();
  43. * inputStream.reset();
  44. */
  45. com.sun.media.jai.codec.FileCacheSeekableStream seekableInput =
  46. new FileCacheSeekableStream(inputStream);
  47. RenderedOp imageOp = JAI.create("stream", seekableInput);
  48. this.m_height = imageOp.getHeight();
  49. this.m_width = imageOp.getWidth();
  50. ColorModel cm = imageOp.getColorModel();
  51. this.m_bitsPerPixel = 8;
  52. // this.m_bitsPerPixel = cm.getPixelSize();
  53. this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
  54. BufferedImage imageData = imageOp.getAsBufferedImage();
  55. int[] tmpMap = imageData.getRGB(0, 0, this.m_width,
  56. this.m_height, null, 0, this.m_width);
  57. if (cm.hasAlpha()) {
  58. int transparencyType = cm.getTransparency(); // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
  59. if (transparencyType == java.awt.Transparency.OPAQUE) {
  60. this.m_isTransparent = false;
  61. } else if (transparencyType ==
  62. java.awt.Transparency.BITMASK) {
  63. if (cm instanceof IndexColorModel) {
  64. this.m_isTransparent = false;
  65. byte[] alphas = new byte[
  66. ((IndexColorModel) cm).getMapSize()];
  67. byte[] reds = new byte[
  68. ((IndexColorModel) cm).getMapSize()];
  69. byte[] greens = new byte[
  70. ((IndexColorModel) cm).getMapSize()];
  71. byte[] blues = new byte[
  72. ((IndexColorModel) cm).getMapSize()];
  73. ((IndexColorModel) cm).getAlphas(alphas);
  74. ((IndexColorModel) cm).getReds(reds);
  75. ((IndexColorModel) cm).getGreens(greens);
  76. ((IndexColorModel) cm).getBlues(blues);
  77. for (int i = 0;
  78. i < ((IndexColorModel) cm).getMapSize();
  79. i++) {
  80. if ((alphas[i] & 0xFF) == 0) {
  81. this.m_isTransparent = true;
  82. this.m_transparentColor = new PDFColor(
  83. (int)(reds[i] & 0xFF),
  84. (int)(greens[i] & 0xFF),
  85. (int)(blues[i] & 0xFF));
  86. break;
  87. }
  88. }
  89. } else {
  90. // TRANSLUCENT
  91. /*
  92. * this.m_isTransparent = false;
  93. * for (int i = 0; i < this.m_width * this.m_height; i++) {
  94. * if (cm.getAlpha(tmpMap[i]) == 0) {
  95. * this.m_isTransparent = true;
  96. * this.m_transparentColor = new PDFColor(cm.getRed(tmpMap[i]), cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
  97. * break;
  98. * }
  99. * }
  100. * // or use special API...
  101. */
  102. this.m_isTransparent = false;
  103. }
  104. } else {
  105. this.m_isTransparent = false;
  106. }
  107. } else {
  108. this.m_isTransparent = false;
  109. }
  110. // Should take care of the ColorSpace and bitsPerPixel
  111. this.m_bitmapsSize = this.m_width * this.m_height * 3;
  112. this.m_bitmaps = new byte[this.m_bitmapsSize];
  113. for (int i = 0; i < this.m_height; i++) {
  114. for (int j = 0; j < this.m_width; j++) {
  115. int p = tmpMap[i * this.m_width + j];
  116. int r = (p > > 16) & 0xFF;
  117. int g = (p > > 8) & 0xFF;
  118. int b = (p) & 0xFF;
  119. this.m_bitmaps[3 * (i * this.m_width + j)] =
  120. (byte)(r & 0xFF);
  121. this.m_bitmaps[3 * (i * this.m_width + j) + 1] =
  122. (byte)(g & 0xFF);
  123. this.m_bitmaps[3 * (i * this.m_width + j) + 2] =
  124. (byte)(b & 0xFF);
  125. }
  126. }
  127. }
  128. catch (Exception ex) {
  129. /*throw new FopImageException("Error while loading image "
  130. + this.m_href.toString() + " : "
  131. + ex.getClass() + " - "
  132. + ex.getMessage());
  133. */}
  134. }
  135. }