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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Java
  20. import java.awt.image.ImageProducer;
  21. import java.awt.image.ColorModel;
  22. import java.awt.image.IndexColorModel;
  23. import java.awt.color.ColorSpace;
  24. import java.awt.Color;
  25. import org.apache.commons.io.IOUtils;
  26. // Jimi
  27. import com.sun.jimi.core.Jimi;
  28. /**
  29. * FopImage object for several images types, using Jimi.
  30. * See Jimi documentation for supported image types.
  31. *
  32. * @see AbstractFopImage
  33. * @see FopImage
  34. */
  35. public class JimiImage extends AbstractFopImage {
  36. /**
  37. * Create a new Jimi image.
  38. *
  39. * @param imgInfo the image info for this Jimi image
  40. */
  41. public JimiImage(FopImage.ImageInfo imgInfo) {
  42. super(imgInfo);
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. protected boolean loadDimensions() {
  48. if (this.bitmaps == null) {
  49. loadImage();
  50. }
  51. return this.bitmaps != null;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. protected boolean loadBitmap() {
  57. if (this.bitmaps == null) {
  58. loadImage();
  59. }
  60. return this.bitmaps != null;
  61. }
  62. /**
  63. * Loads the image from the inputstream
  64. */
  65. protected void loadImage() {
  66. int[] tmpMap = null;
  67. try {
  68. ImageProducer ip = Jimi.getImageProducer(inputStream,
  69. Jimi.SYNCHRONOUS | Jimi.IN_MEMORY);
  70. FopImageConsumer consumer = new FopImageConsumer(ip);
  71. ip.startProduction(consumer);
  72. while (!consumer.isImageReady()) {
  73. Thread.sleep(500);
  74. }
  75. this.height = consumer.getHeight();
  76. this.width = consumer.getWidth();
  77. try {
  78. tmpMap = consumer.getImage();
  79. } catch (Exception ex) {
  80. log.error("Image grabbing interrupted", ex);
  81. return;
  82. }
  83. ColorModel cm = consumer.getColorModel();
  84. this.bitsPerPixel = 8;
  85. // this.bitsPerPixel = cm.getPixelSize();
  86. this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
  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. */
  132. // use special API...
  133. this.isTransparent = false;
  134. }
  135. } else {
  136. this.isTransparent = false;
  137. }
  138. } else {
  139. this.isTransparent = false;
  140. }
  141. } catch (Throwable ex) {
  142. log.error("Error while loading image (Jimi): " + ex.getMessage(), ex);
  143. return;
  144. } finally {
  145. IOUtils.closeQuietly(inputStream);
  146. inputStream = null;
  147. }
  148. // Should take care of the ColorSpace and bitsPerPixel
  149. this.bitmaps = new byte[this.width * this.height * 3];
  150. for (int i = 0; i < this.height; i++) {
  151. for (int j = 0; j < this.width; j++) {
  152. int p = tmpMap[i * this.width + j];
  153. int r = (p >> 16) & 0xFF;
  154. int g = (p >> 8) & 0xFF;
  155. int b = (p) & 0xFF;
  156. this.bitmaps[3 * (i * this.width + j)] = (byte)(r & 0xFF);
  157. this.bitmaps[3 * (i * this.width + j) + 1] = (byte)(g & 0xFF);
  158. this.bitmaps[3 * (i * this.width + j) + 2] = (byte)(b & 0xFF);
  159. }
  160. }
  161. }
  162. /** {@inheritDoc} */
  163. protected boolean loadOriginalData() {
  164. return loadDefaultOriginalData();
  165. }
  166. }