Java Code Examples for com.facebook.imageutils.JfifUtil#MARKER_EOI

The following examples show how to use com.facebook.imageutils.JfifUtil#MARKER_EOI . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArtBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image.
 *
 * @param pooledByteBufferRef the reference to the encoded bytes
 * @param length the number of encoded bytes in the buffer
 * @return the bitmap
 * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
CloseableReference<Bitmap> decodeJPEGFromPooledByteBuffer(
    CloseableReference<PooledByteBuffer> pooledByteBufferRef,
    int length) {
  final PooledByteBuffer pooledByteBuffer = pooledByteBufferRef.get();
  final InputStream jpegBufferInputStream = pooledByteBuffer.getStream();
  jpegBufferInputStream.mark(Integer.MAX_VALUE);

  boolean isJpegComplete;
  try {
    jpegBufferInputStream.skip(length - 2);
    isJpegComplete = (jpegBufferInputStream.read() == JfifUtil.MARKER_FIRST_BYTE) &&
        (jpegBufferInputStream.read() == JfifUtil.MARKER_EOI);
    jpegBufferInputStream.reset();
  } catch (IOException ioe) {
    throw new RuntimeException(ioe);
  }

  InputStream jpegDataStream = jpegBufferInputStream;
  if (pooledByteBuffer.size() > length) {
    jpegDataStream = new LimitedInputStream(jpegDataStream, length);
  }
  if (!isJpegComplete) {
    jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL);
  }
  return doDecodeStaticImage(jpegDataStream);
}
 
Example 2
Source File: ProgressiveJpegParser.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Not every marker is followed by associated segment
 */
private static boolean doesMarkerStartSegment(int markerSecondByte) {
  if (markerSecondByte == JfifUtil.MARKER_TEM) {
    return false;
  }

  if (markerSecondByte >= JfifUtil.MARKER_RST0 && markerSecondByte <= JfifUtil.MARKER_RST7) {
    return false;
  }

  return markerSecondByte != JfifUtil.MARKER_EOI && markerSecondByte != JfifUtil.MARKER_SOI;
}
 
Example 3
Source File: EncodedImage.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Returns true if the image is a JPEG or DNG and its data is already complete at the specified
 * length, false otherwise.
 */
public boolean isCompleteAt(int length) {
  if (mImageFormat != DefaultImageFormats.JPEG && mImageFormat != DefaultImageFormats.DNG) {
    return true;
  }
  // If the image is backed by FileInputStreams return true since they will always be complete.
  if (mInputStreamSupplier != null) {
    return true;
  }
  // The image should be backed by a ByteBuffer
  Preconditions.checkNotNull(mPooledByteBufferRef);
  PooledByteBuffer buf = mPooledByteBufferRef.get();
  return (buf.read(length - 2) == (byte) JfifUtil.MARKER_FIRST_BYTE)
      && (buf.read(length - 1) == (byte) JfifUtil.MARKER_EOI);
}
 
Example 4
Source File: EncodedImageTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testIsJpegCompleteAt_Complete() {
  byte[] encodedBytes = new byte[ENCODED_BYTES_LENGTH];
  encodedBytes[ENCODED_BYTES_LENGTH - 2] = (byte) JfifUtil.MARKER_FIRST_BYTE;
  encodedBytes[ENCODED_BYTES_LENGTH - 1] = (byte) JfifUtil.MARKER_EOI;
  PooledByteBuffer buf = new TrivialPooledByteBuffer(encodedBytes);
  EncodedImage encodedImage = new EncodedImage(CloseableReference.of(buf));
  encodedImage.setImageFormat(DefaultImageFormats.JPEG);
  assertTrue(encodedImage.isCompleteAt(ENCODED_BYTES_LENGTH));
}
 
Example 5
Source File: ProgressiveJpegParser.java    From fresco with MIT License 5 votes vote down vote up
/** Not every marker is followed by associated segment */
private static boolean doesMarkerStartSegment(int markerSecondByte) {
  if (markerSecondByte == JfifUtil.MARKER_TEM) {
    return false;
  }

  if (markerSecondByte >= JfifUtil.MARKER_RST0 && markerSecondByte <= JfifUtil.MARKER_RST7) {
    return false;
  }

  return markerSecondByte != JfifUtil.MARKER_EOI && markerSecondByte != JfifUtil.MARKER_SOI;
}
 
Example 6
Source File: ArtDecoderTest.java    From fresco with MIT License 5 votes vote down vote up
private void jpegTestCase(boolean complete, int dataLength) {
  if (complete) {
    mEncodedBytes[dataLength - 2] = (byte) JfifUtil.MARKER_FIRST_BYTE;
    mEncodedBytes[dataLength - 1] = (byte) JfifUtil.MARKER_EOI;
  }
  CloseableReference<Bitmap> result =
      mArtDecoder.decodeJPEGFromEncodedImage(
          mEncodedImage, DEFAULT_BITMAP_CONFIG, null, dataLength);
  verifyDecodedFromStream();
  verifyNoLeaks();
  verifyDecodedBytes(complete, dataLength);
  closeAndVerifyClosed(result);
}
 
Example 7
Source File: DalvikPurgeableDecoder.java    From fresco with MIT License 5 votes vote down vote up
@VisibleForTesting
public static boolean endsWithEOI(CloseableReference<PooledByteBuffer> bytesRef, int length) {
  PooledByteBuffer buffer = bytesRef.get();
  return length >= 2
      && buffer.read(length - 2) == (byte) JfifUtil.MARKER_FIRST_BYTE
      && buffer.read(length - 1) == (byte) JfifUtil.MARKER_EOI;
}
 
Example 8
Source File: DalvikBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
private static void putEOI(byte[] imageBytes, int offset) {
  // TODO 5884402: remove dependency on JfifUtil
  imageBytes[offset] = (byte) JfifUtil.MARKER_FIRST_BYTE;
  imageBytes[offset + 1] = (byte) JfifUtil.MARKER_EOI;
}
 
Example 9
Source File: DalvikBitmapFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
private static boolean endsWithEOI(final byte[] imageBytes, int length) {
  // TODO 5884402: remove dependency on JfifUtil
  return length >= 2 &&
      imageBytes[length - 2] == (byte) JfifUtil.MARKER_FIRST_BYTE &&
      imageBytes[length - 1] == (byte) JfifUtil.MARKER_EOI;
}
 
Example 10
Source File: ProgressiveJpegParser.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses more data from inputStream.
 *
 * @param inputStream instance of buffered pooled byte buffer input stream
 */
private boolean doParseMoreData(final InputStream inputStream) {
  final int oldBestScanNumber = mBestScanNumber;
  try {
    int nextByte;
    while (mParserState != NOT_A_JPEG && (nextByte = inputStream.read()) != -1) {
      mBytesParsed++;

      switch (mParserState) {
        case READ_FIRST_JPEG_BYTE:
          if (nextByte == JfifUtil.MARKER_FIRST_BYTE) {
            mParserState = READ_SECOND_JPEG_BYTE;
          } else {
            mParserState = NOT_A_JPEG;
          }
          break;

        case READ_SECOND_JPEG_BYTE:
          if (nextByte == JfifUtil.MARKER_SOI) {
            mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
          } else {
            mParserState = NOT_A_JPEG;
          }
          break;

        case READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA:
          if (nextByte == JfifUtil.MARKER_FIRST_BYTE) {
            mParserState = READ_MARKER_SECOND_BYTE;
          }
          break;

        case READ_MARKER_SECOND_BYTE:
          if (nextByte == JfifUtil.MARKER_FIRST_BYTE) {
            mParserState = READ_MARKER_SECOND_BYTE;
          } else if (nextByte == JfifUtil.MARKER_ESCAPE_BYTE) {
            mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
          } else {
            if (nextByte == JfifUtil.MARKER_SOS || nextByte == JfifUtil.MARKER_EOI) {
              newScanOrImageEndFound(mBytesParsed - 2);
            }

            if (doesMarkerStartSegment(nextByte)) {
              mParserState = READ_SIZE_FIRST_BYTE;
            } else {
              mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
            }
          }
          break;

        case READ_SIZE_FIRST_BYTE:
          mParserState = READ_SIZE_SECOND_BYTE;
          break;

        case READ_SIZE_SECOND_BYTE:
          final int size = (mLastByteRead << 8) + nextByte;
          // We need to jump after the end of the segment - skip size-2 next bytes.
          // We might want to skip more data than is available to read, in which case we will
          // consume entire data in inputStream and exit this function before entering another
          // iteration of the loop.
          final int bytesToSkip = size - 2;
          StreamUtil.skip(inputStream, bytesToSkip);
          mBytesParsed += bytesToSkip;
          mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
          break;

        case NOT_A_JPEG:
        default:
          Preconditions.checkState(false);
      }

      mLastByteRead = nextByte;
    }
  } catch (IOException ioe) {
    // does not happen, input stream returned by pooled byte buffer does not throw IOExceptions
    Throwables.propagate(ioe);
  }
  return mParserState != NOT_A_JPEG && mBestScanNumber != oldBestScanNumber;
}
 
Example 11
Source File: ProgressiveJpegParser.java    From fresco with MIT License 4 votes vote down vote up
/**
 * Parses more data from inputStream.
 *
 * @param inputStream instance of buffered pooled byte buffer input stream
 */
private boolean doParseMoreData(final InputStream inputStream) {
  final int oldBestScanNumber = mBestScanNumber;
  try {
    int nextByte;
    while (mParserState != NOT_A_JPEG && (nextByte = inputStream.read()) != -1) {
      mBytesParsed++;
      if (mEndMarkerRead) {
        // There should be no more data after the EOI marker, just in case there is lets
        // bail out instead of trying to parse the unknown data
        mParserState = NOT_A_JPEG;
        mEndMarkerRead = false;
        return false;
      }
      switch (mParserState) {
        case READ_FIRST_JPEG_BYTE:
          if (nextByte == JfifUtil.MARKER_FIRST_BYTE) {
            mParserState = READ_SECOND_JPEG_BYTE;
          } else {
            mParserState = NOT_A_JPEG;
          }
          break;

        case READ_SECOND_JPEG_BYTE:
          if (nextByte == JfifUtil.MARKER_SOI) {
            mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
          } else {
            mParserState = NOT_A_JPEG;
          }
          break;

        case READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA:
          if (nextByte == JfifUtil.MARKER_FIRST_BYTE) {
            mParserState = READ_MARKER_SECOND_BYTE;
          }
          break;

        case READ_MARKER_SECOND_BYTE:
          if (nextByte == JfifUtil.MARKER_FIRST_BYTE) {
            mParserState = READ_MARKER_SECOND_BYTE;
          } else if (nextByte == JfifUtil.MARKER_ESCAPE_BYTE) {
            mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
          } else if (nextByte == JfifUtil.MARKER_EOI) {
            mEndMarkerRead = true;
            newScanOrImageEndFound(mBytesParsed - 2);
            // There should be no data after the EOI marker, but in case there is, let's process
            // the next byte as a first marker byte.
            mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
          } else {
            if (nextByte == JfifUtil.MARKER_SOS) {
              newScanOrImageEndFound(mBytesParsed - 2);
            }

            if (doesMarkerStartSegment(nextByte)) {
              mParserState = READ_SIZE_FIRST_BYTE;
            } else {
              mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
            }
          }
          break;

        case READ_SIZE_FIRST_BYTE:
          mParserState = READ_SIZE_SECOND_BYTE;
          break;

        case READ_SIZE_SECOND_BYTE:
          final int size = (mLastByteRead << 8) + nextByte;
          // We need to jump after the end of the segment - skip size-2 next bytes.
          // We might want to skip more data than is available to read, in which case we will
          // consume entire data in inputStream and exit this function before entering another
          // iteration of the loop.
          final int bytesToSkip = size - 2;
          StreamUtil.skip(inputStream, bytesToSkip);
          mBytesParsed += bytesToSkip;
          mParserState = READ_MARKER_FIRST_BYTE_OR_ENTROPY_DATA;
          break;

        case NOT_A_JPEG:
        default:
          Preconditions.checkState(false);
      }

      mLastByteRead = nextByte;
    }
  } catch (IOException ioe) {
    // does not happen, input stream returned by pooled byte buffer does not throw IOExceptions
    Throwables.propagate(ioe);
  }
  return mParserState != NOT_A_JPEG && mBestScanNumber != oldBestScanNumber;
}
 
Example 12
Source File: KitKatPurgeableDecoder.java    From fresco with MIT License 4 votes vote down vote up
private static void putEOI(byte[] imageBytes, int offset) {
  // TODO 5884402: remove dependency on JfifUtil
  imageBytes[offset] = (byte) JfifUtil.MARKER_FIRST_BYTE;
  imageBytes[offset + 1] = (byte) JfifUtil.MARKER_EOI;
}