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.

JimiImage.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.awt.image.ImageProducer;
  11. import java.awt.image.ColorModel;
  12. import java.awt.image.IndexColorModel;
  13. // Jimi
  14. import com.sun.jimi.core.*;
  15. // FOP
  16. import org.apache.fop.datatypes.ColorSpace;
  17. import org.apache.fop.pdf.PDFColor;
  18. import org.apache.fop.image.analyser.ImageReader;
  19. /**
  20. * FopImage object for several images types, using Jimi.
  21. * See Jimi documentation for supported image types.
  22. * @author Eric SCHAEFFER
  23. * @see AbstractFopImage
  24. * @see FopImage
  25. */
  26. public class JimiImage extends AbstractFopImage {
  27. public JimiImage(URL href, ImageReader imgReader) {
  28. super(href, imgReader);
  29. try {
  30. Class c = Class.forName("com.sun.jimi.core.Jimi");
  31. } catch (ClassNotFoundException e) {
  32. //throw new FopImageException("Jimi image library not available");
  33. }
  34. }
  35. protected void loadImage() {
  36. int[] tmpMap = null;
  37. try {
  38. ImageProducer ip =
  39. Jimi.getImageProducer(this.m_href.openStream(),
  40. Jimi.SYNCHRONOUS | Jimi.IN_MEMORY);
  41. FopImageConsumer consumer = new FopImageConsumer(ip);
  42. ip.startProduction(consumer);
  43. while (!consumer.isImageReady()) {
  44. Thread.sleep(500);
  45. }
  46. this.m_height = consumer.getHeight();
  47. this.m_width = consumer.getWidth();
  48. try {
  49. tmpMap = consumer.getImage();
  50. } catch (Exception ex) {
  51. /*throw new FopImageException("Image grabbing interrupted : "
  52. + ex.getMessage());
  53. */}
  54. ColorModel cm = consumer.getColorModel();
  55. this.m_bitsPerPixel = 8;
  56. // this.m_bitsPerPixel = cm.getPixelSize();
  57. this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
  58. if (cm.hasAlpha()) {
  59. int transparencyType = cm.getTransparency(); // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
  60. if (transparencyType == java.awt.Transparency.OPAQUE) {
  61. this.m_isTransparent = false;
  62. } else if (transparencyType ==
  63. java.awt.Transparency.BITMASK) {
  64. if (cm instanceof IndexColorModel) {
  65. this.m_isTransparent = false;
  66. byte[] alphas = new byte[
  67. ((IndexColorModel) cm).getMapSize()];
  68. byte[] reds = new byte[
  69. ((IndexColorModel) cm).getMapSize()];
  70. byte[] greens = new byte[
  71. ((IndexColorModel) cm).getMapSize()];
  72. byte[] blues = new byte[
  73. ((IndexColorModel) cm).getMapSize()];
  74. ((IndexColorModel) cm).getAlphas(alphas);
  75. ((IndexColorModel) cm).getReds(reds);
  76. ((IndexColorModel) cm).getGreens(greens);
  77. ((IndexColorModel) cm).getBlues(blues);
  78. for (int i = 0;
  79. i < ((IndexColorModel) cm).getMapSize();
  80. i++) {
  81. if ((alphas[i] & 0xFF) == 0) {
  82. this.m_isTransparent = true;
  83. this.m_transparentColor = new PDFColor(
  84. (int)(reds[i] & 0xFF),
  85. (int)(greens[i] & 0xFF),
  86. (int)(blues[i] & 0xFF));
  87. break;
  88. }
  89. }
  90. } else {
  91. // TRANSLUCENT
  92. /*
  93. * this.m_isTransparent = false;
  94. * for (int i = 0; i < this.m_width * this.m_height; i++) {
  95. * if (cm.getAlpha(tmpMap[i]) == 0) {
  96. * this.m_isTransparent = true;
  97. * this.m_transparentColor = new PDFColor(cm.getRed(tmpMap[i]), cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
  98. * break;
  99. * }
  100. * }
  101. */
  102. // use special API...
  103. this.m_isTransparent = false;
  104. }
  105. } else {
  106. this.m_isTransparent = false;
  107. }
  108. } else {
  109. this.m_isTransparent = false;
  110. }
  111. } catch (Exception ex) {
  112. /*throw new FopImageException("Error while loading image "
  113. + this.m_href.toString() + " : "
  114. + ex.getClass() + " - "
  115. + ex.getMessage());
  116. */}
  117. // Should take care of the ColorSpace and bitsPerPixel
  118. this.m_bitmapsSize = this.m_width * this.m_height * 3;
  119. this.m_bitmaps = new byte[this.m_bitmapsSize];
  120. for (int i = 0; i < this.m_height; i++) {
  121. for (int j = 0; j < this.m_width; j++) {
  122. int p = tmpMap[i * this.m_width + j];
  123. int r = (p > > 16) & 0xFF;
  124. int g = (p > > 8) & 0xFF;
  125. int b = (p) & 0xFF;
  126. this.m_bitmaps[3 * (i * this.m_width + j)] =
  127. (byte)(r & 0xFF);
  128. this.m_bitmaps[3 * (i * this.m_width + j) + 1] =
  129. (byte)(g & 0xFF);
  130. this.m_bitmaps[3 * (i * this.m_width + j) + 2] =
  131. (byte)(b & 0xFF);
  132. }
  133. }
  134. }
  135. }