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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.image;
  18. // Java
  19. import java.awt.image.ImageProducer;
  20. import java.awt.image.ColorModel;
  21. import java.awt.image.IndexColorModel;
  22. import java.awt.color.ColorSpace;
  23. import java.awt.Color;
  24. // Jimi
  25. import com.sun.jimi.core.Jimi;
  26. // Avalon
  27. import org.apache.avalon.framework.logger.Logger;
  28. /**
  29. * FopImage object for several images types, using Jimi.
  30. * See Jimi documentation for supported image types.
  31. * @author Eric SCHAEFFER
  32. * @see AbstractFopImage
  33. * @see FopImage
  34. */
  35. public class JimiImage extends AbstractFopImage {
  36. public JimiImage(FopImage.ImageInfo imgReader) {
  37. super(imgReader);
  38. try {
  39. Class c = Class.forName("com.sun.jimi.core.Jimi");
  40. } catch (ClassNotFoundException e) {
  41. //throw new FopImageException("Jimi image library not available");
  42. }
  43. }
  44. protected boolean loadDimensions(Logger logger) {
  45. if (this.bitmaps == null) {
  46. loadImage(logger);
  47. }
  48. return this.bitmaps != null;
  49. }
  50. /**
  51. * @see org.apache.fop.image.AbstractFopImage#loadBitmap(Logger)
  52. */
  53. protected boolean loadBitmap(Logger logger) {
  54. if (this.bitmaps == null) {
  55. loadImage(logger);
  56. }
  57. return this.bitmaps != null;
  58. }
  59. protected void loadImage(Logger log) {
  60. int[] tmpMap = null;
  61. try {
  62. ImageProducer ip =
  63. Jimi.getImageProducer(inputStream,
  64. Jimi.SYNCHRONOUS | Jimi.IN_MEMORY);
  65. FopImageConsumer consumer = new FopImageConsumer(ip);
  66. ip.startProduction(consumer);
  67. while (!consumer.isImageReady()) {
  68. Thread.sleep(500);
  69. }
  70. this.height = consumer.getHeight();
  71. this.width = consumer.getWidth();
  72. inputStream.close();
  73. inputStream = null;
  74. try {
  75. tmpMap = consumer.getImage();
  76. } catch (Exception ex) {
  77. log.error("Image grabbing interrupted", ex);
  78. return;
  79. }
  80. ColorModel cm = consumer.getColorModel();
  81. this.bitsPerPixel = 8;
  82. // this.bitsPerPixel = cm.getPixelSize();
  83. this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
  84. if (cm.hasAlpha()) {
  85. // java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENT
  86. int transparencyType = cm.getTransparency();
  87. if (transparencyType == java.awt.Transparency.OPAQUE) {
  88. this.isTransparent = false;
  89. } else if (transparencyType == java.awt.Transparency.BITMASK) {
  90. if (cm instanceof IndexColorModel) {
  91. this.isTransparent = false;
  92. byte[] alphas = new byte[
  93. ((IndexColorModel) cm).getMapSize()];
  94. byte[] reds = new byte[
  95. ((IndexColorModel) cm).getMapSize()];
  96. byte[] greens = new byte[
  97. ((IndexColorModel) cm).getMapSize()];
  98. byte[] blues = new byte[
  99. ((IndexColorModel) cm).getMapSize()];
  100. ((IndexColorModel) cm).getAlphas(alphas);
  101. ((IndexColorModel) cm).getReds(reds);
  102. ((IndexColorModel) cm).getGreens(greens);
  103. ((IndexColorModel) cm).getBlues(blues);
  104. for (int i = 0;
  105. i < ((IndexColorModel) cm).getMapSize();
  106. i++) {
  107. if ((alphas[i] & 0xFF) == 0) {
  108. this.isTransparent = true;
  109. this.transparentColor = new Color(
  110. (int)(reds[i] & 0xFF),
  111. (int)(greens[i] & 0xFF),
  112. (int)(blues[i] & 0xFF));
  113. break;
  114. }
  115. }
  116. } else {
  117. // TRANSLUCENT
  118. /*
  119. * this.isTransparent = false;
  120. * for (int i = 0; i < this.width * this.height; i++) {
  121. * if (cm.getAlpha(tmpMap[i]) == 0) {
  122. * this.isTransparent = true;
  123. * this.transparentColor = new PDFColor(cm.getRed(tmpMap[i]),
  124. * cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
  125. * break;
  126. * }
  127. * }
  128. */
  129. // use special API...
  130. this.isTransparent = false;
  131. }
  132. } else {
  133. this.isTransparent = false;
  134. }
  135. } else {
  136. this.isTransparent = false;
  137. }
  138. } catch (Throwable ex) {
  139. log.error("Error while loading image "
  140. + "", ex);
  141. return;
  142. }
  143. // Should take care of the ColorSpace and bitsPerPixel
  144. this.bitmapsSize = this.width * this.height * 3;
  145. this.bitmaps = new byte[this.bitmapsSize];
  146. for (int i = 0; i < this.height; i++) {
  147. for (int j = 0; j < this.width; j++) {
  148. int p = tmpMap[i * this.width + j];
  149. int r = (p >> 16) & 0xFF;
  150. int g = (p >> 8) & 0xFF;
  151. int b = (p) & 0xFF;
  152. this.bitmaps[3 * (i * this.width + j)] =
  153. (byte)(r & 0xFF);
  154. this.bitmaps[3 * (i * this.width + j) + 1] =
  155. (byte)(g & 0xFF);
  156. this.bitmaps[3 * (i * this.width + j) + 2] =
  157. (byte)(b & 0xFF);
  158. }
  159. }
  160. }
  161. }