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.

JPEGReader.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.analyser;
  8. // Java
  9. import java.io.BufferedInputStream;
  10. import java.io.IOException;
  11. import org.apache.fop.fo.FOUserAgent;
  12. /**
  13. * ImageReader object for JPEG image type.
  14. * @author Pankaj Narula
  15. * @version 1.0
  16. */
  17. public class JPEGReader extends AbstractImageReader {
  18. /**
  19. * Only SOFn and APPn markers are defined as SOFn is needed for the height and
  20. * width search. APPn is also defined because if the JPEG contains thumbnails
  21. * the dimensions of the thumnail would also be after the SOFn marker enclosed
  22. * inside the APPn marker. And we don't want to confuse those dimensions with
  23. * the image dimensions.
  24. */
  25. static protected final int MARK = 0xff; // Beginneing of a Marker
  26. static protected final int NULL = 0x00; // Special case for 0xff00
  27. static protected final int SOF1 = 0xc0; // Baseline DCT
  28. static protected final int SOF2 = 0xc1; // Extended Sequential DCT
  29. static protected final int SOF3 = 0xc2; // Progrssive DCT only PDF 1.3
  30. static protected final int SOFA = 0xca; // Progressice DCT only PDF 1.3
  31. static protected final int APP0 = 0xe0; // Application marker, JFIF
  32. static protected final int APPF = 0xef; // Application marker
  33. static protected final int SOS = 0xda; // Start of Scan
  34. static protected final int SOI = 0xd8; // start of Image
  35. static protected final int JPG_SIG_LENGTH = 2;
  36. protected byte[] header;
  37. public boolean verifySignature(String uri, BufferedInputStream fis,
  38. FOUserAgent ua) throws IOException {
  39. this.imageStream = fis;
  40. this.setDefaultHeader();
  41. boolean supported = ((header[0] == (byte) 0xff) &&
  42. (header[1] == (byte) 0xd8));
  43. if (supported) {
  44. setDimension();
  45. return true;
  46. } else
  47. return false;
  48. }
  49. public String getMimeType() {
  50. return "image/jpeg";
  51. }
  52. protected void setDefaultHeader() throws IOException {
  53. this.header = new byte[JPG_SIG_LENGTH];
  54. try {
  55. this.imageStream.mark(JPG_SIG_LENGTH + 1);
  56. this.imageStream.read(header);
  57. this.imageStream.reset();
  58. } catch (IOException ex) {
  59. try {
  60. this.imageStream.reset();
  61. } catch (IOException exbis) {}
  62. throw ex;
  63. }
  64. }
  65. protected void setDimension() throws IOException {
  66. try {
  67. int marker = NULL;
  68. long length, skipped;
  69. outer:
  70. while (imageStream.available() > 0) {
  71. while ((marker = imageStream.read()) != MARK) {
  72. ;
  73. }
  74. do {
  75. marker = imageStream.read();
  76. } while (marker == MARK)
  77. ;
  78. switch (marker) {
  79. case SOI:
  80. break;
  81. case NULL:
  82. break;
  83. case SOF1:
  84. case SOF2:
  85. case SOF3: // SOF3 and SOFA are only supported by PDF 1.3
  86. case SOFA:
  87. this.skip(3);
  88. this.height = this.read2bytes();
  89. this.width = this.read2bytes();
  90. break outer;
  91. default:
  92. length = this.read2bytes();
  93. skipped = this.skip(length - 2);
  94. if (skipped != length - 2)
  95. throw new IOException("Skipping Error");
  96. }
  97. }
  98. } catch (IOException ioe) {
  99. try {
  100. this.imageStream.reset();
  101. } catch (IOException exbis) {}
  102. throw ioe;
  103. }
  104. }
  105. protected int read2bytes() throws IOException {
  106. int byte1 = imageStream.read();
  107. int byte2 = imageStream.read();
  108. return (int)((byte1 << 8) | byte2);
  109. }
  110. protected long skip(long n) throws IOException {
  111. long discarded = 0;
  112. while (discarded != n) {
  113. imageStream.read();
  114. discarded++;
  115. }
  116. return discarded; // scope for exception
  117. }
  118. }