com.facebook.imagepipeline.image.QualityInfo Java Examples

The following examples show how to use com.facebook.imagepipeline.image.QualityInfo. 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: DefaultImageDecoder.java    From fresco with MIT License 6 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  ImageFormat imageFormat = encodedImage.getImageFormat();
  if (imageFormat == DefaultImageFormats.JPEG) {
    return decodeJpeg(encodedImage, length, qualityInfo, options);
  } else if (imageFormat == DefaultImageFormats.GIF) {
    return decodeGif(encodedImage, length, qualityInfo, options);
  } else if (imageFormat == DefaultImageFormats.WEBP_ANIMATED) {
    return decodeAnimatedWebp(encodedImage, length, qualityInfo, options);
  } else if (imageFormat == ImageFormat.UNKNOWN) {
    throw new DecodeException("unknown image format", encodedImage);
  }
  return decodeStaticImage(encodedImage, options);
}
 
Example #2
Source File: ImageFormatProgressiveJpegFragment.java    From fresco with MIT License 6 votes vote down vote up
private void logScan(QualityInfo qualityInfo, boolean isFinalImage) {
  mDebugOutput.append(
      String.format(
          Locale.getDefault(),
          "%s: %s, goodEnough=%b, fullQuality=%b, quality=%d\n\n",
          mDateFormat.format(new Date(System.currentTimeMillis())),
          isFinalImage ? "final" : "intermediate",
          qualityInfo.isOfGoodEnoughQuality(),
          qualityInfo.isOfFullQuality(),
          qualityInfo.getQuality()));
  // Scroll to the bottom
  mDebugOutputScrollView.post(
      new Runnable() {
        @Override
        public void run() {
          mDebugOutputScrollView.scrollTo(0, mDebugOutputScrollView.getBottom());
        }
      });
}
 
Example #3
Source File: DefaultImageDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes image.
 *
 * @param encodedImage input image (encoded bytes plus meta data)
 * @param length if image type supports decoding incomplete image then determines where the image
 *     data should be cut for decoding.
 * @param qualityInfo quality information for the image
 * @param options options that can change decode behavior
 */
@Override
public CloseableImage decode(
    final EncodedImage encodedImage,
    final int length,
    final QualityInfo qualityInfo,
    final ImageDecodeOptions options) {
  if (options.customImageDecoder != null) {
    return options.customImageDecoder.decode(encodedImage, length, qualityInfo, options);
  }
  ImageFormat imageFormat = encodedImage.getImageFormat();
  if (imageFormat == null || imageFormat == ImageFormat.UNKNOWN) {
    imageFormat =
        ImageFormatChecker.getImageFormat_WrapIOException(encodedImage.getInputStream());
    encodedImage.setImageFormat(imageFormat);
  }
  if (mCustomDecoders != null) {
    ImageDecoder decoder = mCustomDecoders.get(imageFormat);
    if (decoder != null) {
      return decoder.decode(encodedImage, length, qualityInfo, options);
    }
  }
  return mDefaultDecoder.decode(encodedImage, length, qualityInfo, options);
}
 
Example #4
Source File: FrescoVitoRegionDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes a partial jpeg.
 *
 * @param encodedImage input image (encoded bytes plus meta data)
 * @param length if image type supports decoding incomplete image then determines where the image
 *     data should be cut for decoding.
 * @param qualityInfo quality information for the image
 * @param options options that can change decode behavior
 */
@Override
public CloseableImage decode(
    EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) {

  Rect regionToDecode = computeRegionToDecode(encodedImage, options);

  CloseableReference<Bitmap> decodedBitmapReference =
      mPlatformDecoder.decodeJPEGFromEncodedImageWithColorSpace(
          encodedImage, options.bitmapConfig, regionToDecode, length, options.colorSpace);
  try {
    maybeApplyTransformation(options.bitmapTransformation, decodedBitmapReference);
    return new CloseableStaticBitmap(
        decodedBitmapReference,
        ImmutableQualityInfo.FULL_QUALITY,
        encodedImage.getRotationAngle(),
        encodedImage.getExifOrientation());
  } finally {
    CloseableReference.closeSafely(decodedBitmapReference);
  }
}
 
Example #5
Source File: KeyframesDecoderExample.java    From fresco with MIT License 6 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  InputStream encodedInputStream = null;
  try {
    encodedInputStream = encodedImage.getInputStream();
    return new CloseableKeyframesImage(KFImageDeserializer.deserialize(encodedInputStream));
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  } finally {
    Closeables.closeQuietly(encodedInputStream);
  }
}
 
Example #6
Source File: DefaultImageDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes a partial jpeg.
 *
 * @param encodedImage input image (encoded bytes plus meta data)
 * @param length amount of currently available data in bytes
 * @param qualityInfo quality info for the image
 * @return a CloseableStaticBitmap
 */
public CloseableStaticBitmap decodeJpeg(
    final EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  CloseableReference<Bitmap> bitmapReference =
      mPlatformDecoder.decodeJPEGFromEncodedImageWithColorSpace(
          encodedImage, options.bitmapConfig, null, length, options.colorSpace);
  try {
    maybeApplyTransformation(options.bitmapTransformation, bitmapReference);
    return new CloseableStaticBitmap(
        bitmapReference,
        qualityInfo,
        encodedImage.getRotationAngle(),
        encodedImage.getExifOrientation());
  } finally {
    bitmapReference.close();
  }
}
 
Example #7
Source File: ImagePipelineRegionDecodingFragment.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  CloseableReference<Bitmap> decodedBitmapReference =
      mPlatformDecoder.decodeJPEGFromEncodedImageWithColorSpace(
          encodedImage, options.bitmapConfig, mRegion, length, options.colorSpace);
  try {
    return new CloseableStaticBitmap(decodedBitmapReference, qualityInfo, 0);
  } finally {
    CloseableReference.closeSafely(decodedBitmapReference);
  }
}
 
Example #8
Source File: DefaultImageDecoder.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Decodes gif into CloseableImage.
 *
 * @param encodedImage input image (encoded bytes plus meta data)
 * @return a CloseableImage
 */
public CloseableImage decodeGif(
    final EncodedImage encodedImage,
    final int length,
    final QualityInfo qualityInfo,
    final ImageDecodeOptions options) {
  if (encodedImage.getWidth() == EncodedImage.UNKNOWN_WIDTH
      || encodedImage.getHeight() == EncodedImage.UNKNOWN_HEIGHT) {
    throw new DecodeException("image width or height is incorrect", encodedImage);
  }
  if (!options.forceStaticImage && mAnimatedGifDecoder != null) {
    return mAnimatedGifDecoder.decode(encodedImage, length, qualityInfo, options);
  }
  return decodeStaticImage(encodedImage, options);
}
 
Example #9
Source File: AnimatedFactoryV2Impl.java    From fresco with MIT License 5 votes vote down vote up
@Override
public ImageDecoder getWebPDecoder(final Bitmap.Config bitmapConfig) {
  return new ImageDecoder() {
    @Override
    public CloseableImage decode(
        EncodedImage encodedImage,
        int length,
        QualityInfo qualityInfo,
        ImageDecodeOptions options) {
      return getAnimatedImageFactory().decodeWebP(encodedImage, options, bitmapConfig);
    }
  };
}
 
Example #10
Source File: AnimatedFactoryV2Impl.java    From fresco with MIT License 5 votes vote down vote up
@Override
public ImageDecoder getGifDecoder(final Bitmap.Config bitmapConfig) {
  return new ImageDecoder() {
    @Override
    public CloseableImage decode(
        EncodedImage encodedImage,
        int length,
        QualityInfo qualityInfo,
        ImageDecodeOptions options) {
      return getAnimatedImageFactory().decodeGif(encodedImage, options, bitmapConfig);
    }
  };
}
 
Example #11
Source File: ImageDecoder.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decodes a partial jpeg.
 *
 * @param pooledByteBufferRef
 * @param length amount of currently available data in bytes
 * @param qualityInfo quality info for the image
 * @return a CloseableStaticBitmap
 */
public synchronized CloseableStaticBitmap decodeJpeg(
    final CloseableReference<PooledByteBuffer> pooledByteBufferRef,
    int length,
    QualityInfo qualityInfo) {
  CloseableReference<Bitmap> bitmapReference =
      mBitmapFactoryWithPool.decodeJPEGFromPooledByteBuffer(pooledByteBufferRef, length);
  try {
    return new CloseableStaticBitmap(bitmapReference, qualityInfo);
  } finally {
    bitmapReference.close();
  }
}
 
Example #12
Source File: ImageDecoder.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decodes image.
 *
 * @param pooledByteBufferRef buffer containing image data
 * @param imageFormat if not null and not UNKNOWN, then format check is skipped and this one is
 *   assumed.
 * @param length if image type supports decoding incomplete image then determines where
 *   the image data should be cut for decoding.
 * @param qualityInfo quality information for the image
 * @param options options that cange decode behavior
 */
public CloseableImage decodeImage(
    final CloseableReference<PooledByteBuffer> pooledByteBufferRef,
    @Nullable ImageFormat imageFormat,
    final int length,
    final QualityInfo qualityInfo,
    final ImageDecodeOptions options) {
  if (imageFormat == null || imageFormat == ImageFormat.UNKNOWN) {
    imageFormat = ImageFormatChecker.getImageFormat_WrapIOException(
        pooledByteBufferRef.get().getStream());
  }

  switch (imageFormat) {
    case UNKNOWN:
      throw new IllegalArgumentException("unknown image format");

    case JPEG:
      return decodeJpeg(pooledByteBufferRef, length, qualityInfo);

    case GIF:
      return decodeAnimatedGif(pooledByteBufferRef, options);

    case WEBP_ANIMATED:
      return decodeAnimatedWebp(pooledByteBufferRef, options);

    default:
      return decodeStaticImage(pooledByteBufferRef);
  }
}
 
Example #13
Source File: SimpleProgressiveJpegConfig.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public QualityInfo getQualityInfo(int scanNumber) {
  return ImmutableQualityInfo.of(
      scanNumber,
      /* isOfGoodEnoughQuality */ scanNumber >= mGoodEnoughScanNumber,
      /* isOfFullQuality */ false);
}
 
Example #14
Source File: BitmapMemoryCacheProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
private boolean shouldCacheIntermediateResult(
    CloseableReference<CloseableImage> newResult,
    BitmapMemoryCacheKey cacheKey) {
  CloseableReference<CloseableImage> currentCachedResult = mMemoryCache.get(cacheKey, null);
  if (currentCachedResult == null) {
    return true;
  }
  try {
    QualityInfo currentQualityInfo = currentCachedResult.get().getQualityInfo();
    return !currentQualityInfo.isOfFullQuality() &&
        newResult.get().getQualityInfo().getQuality() > currentQualityInfo.getQuality();
  } finally {
    CloseableReference.closeSafely(currentCachedResult);
  }
}
 
Example #15
Source File: SimpleProgressiveJpegConfig.java    From fresco with MIT License 5 votes vote down vote up
@Override
public QualityInfo getQualityInfo(int scanNumber) {
  return ImmutableQualityInfo.of(
      scanNumber,
      /* isOfGoodEnoughQuality */ scanNumber >= mDynamicValueConfig.getGoodEnoughScanNumber(),
      /* isOfFullQuality */ false);
}
 
Example #16
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
private Map<String, String> getExtraMap(
    final long queueTime,
    final QualityInfo qualityInfo,
    final boolean isFinal) {
  if (!mProducerListener.requiresExtraMap(mProducerContext.getId())) {
    return null;
  }
  return ImmutableMap.of(
      QUEUE_TIME_KEY,
      String.valueOf(queueTime),
      HAS_GOOD_QUALITY_KEY,
      String.valueOf(qualityInfo.isOfGoodEnoughQuality()),
      IS_FINAL_KEY,
      String.valueOf(isFinal));
}
 
Example #17
Source File: SvgDecoderExample.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  try {
    SVG svg = SVG.getFromInputStream(encodedImage.getInputStream());
    return new CloseableSvgImage(svg);
  } catch (SVGParseException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example #18
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
protected void maybeDecodeIntermediateImage(
    CloseableReference<PooledByteBuffer> imageBytesRef) {
  Preconditions.checkNotNull(imageBytesRef);
  final int bestScanEnd = getIntermediateImageEndOffset(imageBytesRef);
  final ImageFormat imageFormat = getImageFormat(imageBytesRef);
  final QualityInfo qualityInfo = getQualityInfo(imageBytesRef);

  synchronized (ProgressiveDecoder.this) {
    if (!mProducerContext.isIntermediateResultExpected()) {
      // do not schedule decode as the result is not expected.
      // however, keep the result in case the client should need it in the future.
      updateStoredIntermediateImage(
          imageBytesRef,
          bestScanEnd,
          imageFormat,
          qualityInfo);
    } else {
      closeStoredIntermediateImageBytes();
      scheduleImageDecode(
          imageBytesRef,
          bestScanEnd,
          imageFormat,
          qualityInfo,
          false);
    }
  }
}
 
Example #19
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
protected synchronized void updateStoredIntermediateImage(
    CloseableReference<PooledByteBuffer> intermediateImageBytesRef,
    int intermediateImageBestScanEnd,
    ImageFormat intermediateImageFormat,
    QualityInfo intermediateImageQualityInfo) {
  closeStoredIntermediateImageBytes();
  mStoredIntermediateImageBytesRef = intermediateImageBytesRef.clone();
  mStoredIntermediateImageBestScanEnd = intermediateImageBestScanEnd;
  mStoredIntermediateImageFormat = intermediateImageFormat;
  mStoredIntermediateImageQualityInfo = intermediateImageQualityInfo;
}
 
Example #20
Source File: ColorImageExample.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  try {
    // Read the file as a string
    String text = new String(ByteStreams.toByteArray(encodedImage.getInputStream()));

    // Check if the string matches "<color>#"
    if (!text.startsWith(COLOR_TAG + "#")) {
      return null;
    }

    // Parse the int value between # and <
    int startIndex = COLOR_TAG.length() + 1;
    int endIndex = text.lastIndexOf('<');
    int color = Integer.parseInt(text.substring(startIndex, endIndex), 16);

    // Add the alpha component so that we actually see the color
    color = ColorUtils.setAlphaComponent(color, 255);

    // Return the CloseableImage
    return new CloseableColorImage(color);
  } catch (IOException e) {
    e.printStackTrace();
  }
  // Return nothing if an error occurred
  return null;
}
 
Example #21
Source File: ProgressiveJpegConfig.java    From fresco with MIT License 4 votes vote down vote up
/** Gets the quality information for the given scan-number. */
QualityInfo getQualityInfo(int scanNumber);
 
Example #22
Source File: ImageDecoder.java    From fresco with MIT License 4 votes vote down vote up
CloseableImage decode(
@Nonnull EncodedImage encodedImage,
int length,
@Nonnull QualityInfo qualityInfo,
@Nonnull ImageDecodeOptions options);
 
Example #23
Source File: BitmapMemoryCacheProducer.java    From fresco with MIT License 4 votes vote down vote up
protected Consumer<CloseableReference<CloseableImage>> wrapConsumer(
    final Consumer<CloseableReference<CloseableImage>> consumer,
    final CacheKey cacheKey,
    final boolean isMemoryCacheEnabled) {
  return new DelegatingConsumer<
      CloseableReference<CloseableImage>, CloseableReference<CloseableImage>>(consumer) {
    @Override
    public void onNewResultImpl(
        CloseableReference<CloseableImage> newResult, @Status int status) {
      try {
        if (FrescoSystrace.isTracing()) {
          FrescoSystrace.beginSection("BitmapMemoryCacheProducer#onNewResultImpl");
        }
        final boolean isLast = isLast(status);
        // ignore invalid intermediate results and forward the null result if last
        if (newResult == null) {
          if (isLast) {
            getConsumer().onNewResult(null, status);
          }
          return;
        }
        // stateful and partial results cannot be cached and are just forwarded
        if (newResult.get().isStateful() || statusHasFlag(status, IS_PARTIAL_RESULT)) {
          getConsumer().onNewResult(newResult, status);
          return;
        }
        // if the intermediate result is not of a better quality than the cached result,
        // forward the already cached result and don't cache the new result.
        if (!isLast) {
          CloseableReference<CloseableImage> currentCachedResult = mMemoryCache.get(cacheKey);
          if (currentCachedResult != null) {
            try {
              QualityInfo newInfo = newResult.get().getQualityInfo();
              QualityInfo cachedInfo = currentCachedResult.get().getQualityInfo();
              if (cachedInfo.isOfFullQuality()
                  || cachedInfo.getQuality() >= newInfo.getQuality()) {
                getConsumer().onNewResult(currentCachedResult, status);
                return;
              }
            } finally {
              CloseableReference.closeSafely(currentCachedResult);
            }
          }
        }
        // cache, if needed, and forward the new result
        CloseableReference<CloseableImage> newCachedResult = null;
        if (isMemoryCacheEnabled) {
          newCachedResult = mMemoryCache.cache(cacheKey, newResult);
        }
        try {
          if (isLast) {
            getConsumer().onProgressUpdate(1f);
          }
          getConsumer()
              .onNewResult((newCachedResult != null) ? newCachedResult : newResult, status);
        } finally {
          CloseableReference.closeSafely(newCachedResult);
        }
      } finally {
        if (FrescoSystrace.isTracing()) {
          FrescoSystrace.endSection();
        }
      }
    }
  };
}
 
Example #24
Source File: DecodeProducer.java    From fresco with MIT License 4 votes vote down vote up
@Override
protected QualityInfo getQualityInfo() {
  return mProgressiveJpegConfig.getQualityInfo(mProgressiveJpegParser.getBestScanNumber());
}
 
Example #25
Source File: DecodeProducer.java    From fresco with MIT License 4 votes vote down vote up
@Override
protected QualityInfo getQualityInfo() {
  return ImmutableQualityInfo.of(0, false, false);
}
 
Example #26
Source File: ReactToolbar.java    From react-native-GPay with MIT License 4 votes vote down vote up
@Override
public QualityInfo getQualityInfo() {
  return null;
}
 
Example #27
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected QualityInfo getQualityInfo(CloseableReference<PooledByteBuffer> imageBytesRef) {
  return mProgressiveJpegConfig.getQualityInfo(mProgressiveJpegParser.getBestScanNumber());
}
 
Example #28
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected QualityInfo getQualityInfo(CloseableReference<PooledByteBuffer> imageBytesRef) {
  return ImmutableQualityInfo.of(0, false, false);
}
 
Example #29
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
protected abstract QualityInfo getQualityInfo(
CloseableReference<PooledByteBuffer> imageBytesRef);
 
Example #30
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
protected void scheduleImageDecode(
    final CloseableReference<PooledByteBuffer> imageBytesRef,
    final int length,
    @Nullable final ImageFormat imageFormat,
    final QualityInfo qualityInfo,
    final boolean isFinal) {
  final CloseableReference<PooledByteBuffer> imageBytesRefCopy = imageBytesRef.clone();
  final long startTime = SystemClock.elapsedRealtime();
  mExecutor.execute(
      new Runnable() {
        @Override
        public void run() {
          final long queueTime = SystemClock.elapsedRealtime() - startTime;
          try {
            if (isFinished()) {
              return;
            }
            mProducerListener.onProducerStart(mProducerContext.getId(), PRODUCER_NAME);
            CloseableImage decodedImage = mImageDecoder.decodeImage(
                imageBytesRefCopy,
                imageFormat,
                length,
                qualityInfo,
                mImageDecodeOptions);
            mProducerListener.onProducerFinishWithSuccess(
                mProducerContext.getId(),
                PRODUCER_NAME,
                getExtraMap(queueTime, qualityInfo, isFinal));
            handleResult(decodedImage, isFinal);
          } catch (Exception e) {
            mProducerListener.onProducerFinishWithFailure(
                mProducerContext.getId(),
                PRODUCER_NAME,
                e,
                getExtraMap(queueTime, qualityInfo, isFinal));
            handleError(e);
          } finally {
            imageBytesRefCopy.close();
          }
        }
      });
}