Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JpegImage.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * $Id: JpegImage.java,v 1.12 2003/03/06 21:25:44 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.image;
  52. // Java
  53. import java.io.ByteArrayOutputStream;
  54. import java.awt.color.ColorSpace;
  55. import java.awt.color.ICC_Profile;
  56. // FOP
  57. import org.apache.fop.apps.FOUserAgent;
  58. import org.apache.fop.util.CMYKColorSpace;
  59. /**
  60. * FopImage object for JPEG images, Using Java native classes.
  61. * @author Eric Dalquist
  62. * @see AbstractFopImage
  63. * @see FopImage
  64. */
  65. public class JpegImage extends AbstractFopImage {
  66. private ICC_Profile iccProfile = null;
  67. private boolean foundICCProfile = false;
  68. private boolean foundDimensions = false;
  69. /**
  70. * Create a jpeg image with the info.
  71. *
  72. * @param imgInfo the image info for this jpeg
  73. */
  74. public JpegImage(FopImage.ImageInfo imgInfo) {
  75. super(imgInfo);
  76. }
  77. /**
  78. * Load the original jpeg data.
  79. * This loads the original jpeg data and reads the color space,
  80. * and icc profile if any.
  81. *
  82. * @param ua the user agent
  83. * @return true if loaded false for any error
  84. */
  85. protected boolean loadOriginalData(FOUserAgent ua) {
  86. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  87. ByteArrayOutputStream iccStream = new ByteArrayOutputStream();
  88. int index = 0;
  89. boolean cont = true;
  90. try {
  91. byte[] readBuf = new byte[4096];
  92. int bytesRead;
  93. while ((bytesRead = inputStream.read(readBuf)) != -1) {
  94. baos.write(readBuf, 0, bytesRead);
  95. }
  96. inputStream.close();
  97. inputStream = null;
  98. } catch (java.io.IOException ex) {
  99. ua.getLogger().error("Error while loading image "
  100. + " : " + ex.getClass()
  101. + " - " + ex.getMessage(), ex);
  102. return false;
  103. }
  104. this.bitmaps = baos.toByteArray();
  105. this.bitsPerPixel = 8;
  106. this.isTransparent = false;
  107. //Check for SOI (Start of image) marker (FFD8)
  108. if (this.bitmaps.length > (index + 2)
  109. && uByte(this.bitmaps[index]) == 255 /*0xFF*/
  110. && uByte(this.bitmaps[index + 1]) == 216 /*0xD8*/) {
  111. index += 2;
  112. while (index < this.bitmaps.length && cont) {
  113. //check to be sure this is the begining of a header
  114. if (this.bitmaps.length > (index + 2)
  115. && uByte(this.bitmaps[index]) == 255 /*0xFF*/) {
  116. //192 or 194 are the header bytes that contain
  117. // the jpeg width height and color depth.
  118. if (uByte(this.bitmaps[index + 1]) == 192 /*0xC0*/
  119. || uByte(this.bitmaps[index + 1]) == 194 /*0xC2*/) {
  120. this.height = calcBytes(this.bitmaps[index + 5],
  121. this.bitmaps[index + 6]);
  122. this.width = calcBytes(this.bitmaps[index + 7],
  123. this.bitmaps[index + 8]);
  124. if (this.bitmaps[index + 9] == 1) {
  125. this.colorSpace = ColorSpace.getInstance(
  126. ColorSpace.CS_GRAY);
  127. } else if (this.bitmaps[index + 9] == 3) {
  128. this.colorSpace = ColorSpace.getInstance(
  129. ColorSpace.CS_LINEAR_RGB);
  130. } else if (this.bitmaps[index + 9] == 4) {
  131. // howto create CMYK color space
  132. /*
  133. this.colorSpace = ColorSpace.getInstance(
  134. ColorSpace.CS_CIEXYZ);
  135. */
  136. this.colorSpace = CMYKColorSpace.getInstance();
  137. } else {
  138. ua.getLogger().error("Unknown ColorSpace for image: "
  139. + "");
  140. return false;
  141. }
  142. foundDimensions = true;
  143. if (foundICCProfile) {
  144. cont = false;
  145. break;
  146. }
  147. index += calcBytes(this.bitmaps[index + 2],
  148. this.bitmaps[index + 3]) + 2;
  149. } else if (uByte(this.bitmaps[index + 1]) == 226 /*0xE2*/
  150. && this.bitmaps.length > (index + 60)) {
  151. // Check if ICC profile
  152. byte[] iccString = new byte[11];
  153. System.arraycopy(this.bitmaps, index + 4,
  154. iccString, 0, 11);
  155. if ("ICC_PROFILE".equals(new String(iccString))) {
  156. int chunkSize = calcBytes(
  157. this.bitmaps[index + 2],
  158. this.bitmaps[index + 3]) + 2;
  159. iccStream.write(this.bitmaps,
  160. index + 18, chunkSize - 18);
  161. }
  162. index += calcBytes(this.bitmaps[index + 2],
  163. this.bitmaps[index + 3]) + 2;
  164. } else {
  165. index += calcBytes(this.bitmaps[index + 2],
  166. this.bitmaps[index + 3]) + 2;
  167. }
  168. } else {
  169. cont = false;
  170. }
  171. }
  172. } else {
  173. ua.getLogger().error("Error while loading "
  174. + "JpegImage - Invalid JPEG Header.");
  175. return false;
  176. }
  177. if (iccStream.size() > 0) {
  178. byte[] align = new byte[((iccStream.size()) % 8) + 8];
  179. try {
  180. iccStream.write(align);
  181. } catch (Exception e) {
  182. ua.getLogger().error("Error while loading image "
  183. + " : "
  184. + e.getMessage(), e);
  185. return false;
  186. }
  187. try {
  188. iccProfile = ICC_Profile.getInstance(iccStream.toByteArray());
  189. } catch (Exception e) {
  190. ua.getLogger().error("Invalid ICC profile: " + e, e);
  191. return false;
  192. }
  193. } else if (this.colorSpace == null) {
  194. ua.getLogger().error("ColorSpace not specified for JPEG image");
  195. return false;
  196. }
  197. return true;
  198. }
  199. /**
  200. * Get the ICC profile for this Jpeg image.
  201. *
  202. * @return the icc profile or null if not found
  203. */
  204. public ICC_Profile getICCProfile() {
  205. return iccProfile;
  206. }
  207. private int calcBytes(byte bOne, byte bTwo) {
  208. return (uByte(bOne) * 256) + uByte(bTwo);
  209. }
  210. private int uByte(byte bIn) {
  211. if (bIn < 0) {
  212. return 256 + bIn;
  213. } else {
  214. return bIn;
  215. }
  216. }
  217. }