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.

GifImage.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. import java.io.InputStream;
  25. import java.io.IOException;
  26. import java.net.URLConnection;
  27. // FOP
  28. import org.apache.avalon.framework.logger.Logger;
  29. /**
  30. * FopImage object for GIF images, using Java native classes.
  31. * @author Eric SCHAEFFER
  32. * @author Modified by Eric Dalquist - 9/14/2001 - ebdalqui@mtu.edu
  33. * @see AbstractFopImage
  34. * @see FopImage
  35. */
  36. public class GifImage extends AbstractFopImage {
  37. /**
  38. * Create a new gif image.
  39. *
  40. * @param imgInfo the image info for this gif image
  41. */
  42. public GifImage(FopImage.ImageInfo imgInfo) {
  43. super(imgInfo);
  44. }
  45. /**
  46. * Load the bitmap for this gif image.
  47. * This loads the data and creates a bitmap byte array
  48. * of the image data.
  49. * To decode the image a dummy URLConnection is used that
  50. * will do the conversion.
  51. *
  52. * @param ua the user agent for loading
  53. * @return True if the load process succeeded
  54. */
  55. protected boolean loadBitmap(Logger logger) {
  56. int[] tmpMap = null;
  57. try {
  58. URLConnection con = new DummyConnection(inputStream);
  59. ImageProducer ip = (ImageProducer) con.getContent();
  60. if (ip == null) {
  61. return false;
  62. }
  63. FopImageConsumer consumer = new FopImageConsumer(ip);
  64. ip.startProduction(consumer);
  65. //Load the image into memory
  66. while (!consumer.isImageReady()) {
  67. Thread.sleep(500);
  68. }
  69. this.height = consumer.getHeight();
  70. this.width = consumer.getWidth();
  71. try {
  72. tmpMap = consumer.getImage();
  73. } catch (Exception ex) {
  74. logger.error("Image grabbing interrupted : "
  75. + ex.getMessage(), ex);
  76. return false;
  77. }
  78. inputStream.close();
  79. inputStream = null;
  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. IndexColorModel indexcm = (IndexColorModel) cm;
  92. this.isTransparent = false;
  93. byte[] alphas = new byte[indexcm.getMapSize()];
  94. byte[] reds = new byte[indexcm.getMapSize()];
  95. byte[] greens = new byte[indexcm.getMapSize()];
  96. byte[] blues = new byte[indexcm.getMapSize()];
  97. indexcm.getAlphas(alphas);
  98. indexcm.getReds(reds);
  99. indexcm.getGreens(greens);
  100. indexcm.getBlues(blues);
  101. for (int i = 0;
  102. i < indexcm.getMapSize();
  103. i++) {
  104. if ((alphas[i] & 0xFF) == 0) {
  105. this.isTransparent = true;
  106. this.transparentColor = new Color(
  107. (int)(reds[i] & 0xFF),
  108. (int)(greens[i] & 0xFF),
  109. (int)(blues[i] & 0xFF));
  110. break;
  111. }
  112. }
  113. } else {
  114. // TRANSLUCENT
  115. /*
  116. * this.isTransparent = false;
  117. * for (int i = 0; i < this.width * this.height; i++) {
  118. * if (cm.getAlpha(tmpMap[i]) == 0) {
  119. * this.isTransparent = true;
  120. * this.transparentColor = new PDFColor(cm.getRed(tmpMap[i]),
  121. * cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i]));
  122. * break;
  123. * }
  124. * }
  125. */
  126. // use special API...
  127. this.isTransparent = false;
  128. }
  129. } else {
  130. this.isTransparent = false;
  131. }
  132. } else {
  133. this.isTransparent = false;
  134. }
  135. } catch (Exception ex) {
  136. logger.error("Error while loading image "
  137. + "" + " : "
  138. + ex.getClass() + " - "
  139. + ex.getMessage(), ex);
  140. return false;
  141. }
  142. // Should take care of the ColorSpace and bitsPerPixel
  143. this.bitmapsSize = this.width * this.height * 3;
  144. this.bitmaps = new byte[this.bitmapsSize];
  145. for (int i = 0; i < this.height; i++) {
  146. for (int j = 0; j < this.width; j++) {
  147. int p = tmpMap[i * this.width + j];
  148. int r = (p >> 16) & 0xFF;
  149. int g = (p >> 8) & 0xFF;
  150. int b = (p) & 0xFF;
  151. this.bitmaps[3 * (i * this.width + j)] =
  152. (byte)(r & 0xFF);
  153. this.bitmaps[3 * (i * this.width + j) + 1] =
  154. (byte)(g & 0xFF);
  155. this.bitmaps[3 * (i * this.width + j) + 2] =
  156. (byte)(b & 0xFF);
  157. }
  158. }
  159. return true;
  160. }
  161. /**
  162. * A dummy url connection for a gif image in an input stream.
  163. */
  164. protected static class DummyConnection extends URLConnection {
  165. private InputStream inputStream;
  166. DummyConnection(InputStream is) {
  167. super(null);
  168. inputStream = is;
  169. }
  170. public InputStream getInputStream() throws IOException {
  171. return inputStream;
  172. }
  173. public void connect() throws IOException {
  174. // do nothing
  175. }
  176. public String getContentType() {
  177. return "image/gif";
  178. }
  179. public int getContentLength() {
  180. try {
  181. return inputStream.available();
  182. } catch (IOException e) {
  183. return -1;
  184. }
  185. }
  186. }
  187. }