<commit_before>/*
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.facebook.imagepipeline.bitmaps;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;

import com.facebook.common.references.CloseableReference;
import com.facebook.common.webp.BitmapCreator;
import com.facebook.imageformat.DefaultImageFormats;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.memory.FlexByteArrayPool;
import com.facebook.imagepipeline.memory.PoolFactory;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import com.facebook.imageutils.JfifUtil;

/**
 * This is the implementation of the BitmapCreator for the Honeycomb
 */
public class HoneycombBitmapCreator implements BitmapCreator {

  protected static final byte[] EOI = new byte[]{
      (byte) JfifUtil.MARKER_FIRST_BYTE, (byte) JfifUtil.MARKER_EOI};

  private final EmptyJpegGenerator mJpegGenerator;
  private final FlexByteArrayPool mFlexByteArrayPool;

  public HoneycombBitmapCreator(PoolFactory poolFactory) {
    mFlexByteArrayPool = poolFactory.getFlexByteArrayPool();
    mJpegGenerator = new EmptyJpegGenerator(poolFactory.getPooledByteBufferFactory());
  }

  @Override
  public Bitmap createNakedBitmap(
      int width, int height, Bitmap.Config bitmapConfig) {
    CloseableReference<PooledByteBuffer> jpgRef = mJpegGenerator.generate(
        (short) width,
        (short) height);
    EncodedImage encodedImage = null;
    CloseableReference<byte[]> encodedBytesArrayRef = null;
    try {
      encodedImage = new EncodedImage(jpgRef);
      encodedImage.setImageFormat(DefaultImageFormats.JPEG);
      BitmapFactory.Options options = getBitmapFactoryOptions(
          encodedImage.getSampleSize(),
          bitmapConfig);
      int length = jpgRef.get().size();
      byte[] suffix = endsWithEOI(jpgRef, length) ? null : EOI;
      final PooledByteBuffer pooledByteBuffer = jpgRef.get();
      encodedBytesArrayRef =
          mFlexByteArrayPool.get(length + 2);
      byte[] encodedBytesArray = encodedBytesArrayRef.get();
      pooledByteBuffer.read(0, encodedBytesArray, 0, length);
      if (suffix != null) {
        putEOI(encodedBytesArray, length);
        length += 2;
      }
      Bitmap bitmap = BitmapFactory.decodeByteArray(
          encodedBytesArray,
          0,
          length,
          options);
      bitmap.eraseColor(Color.TRANSPARENT);
      return bitmap;
    } finally {
      CloseableReference.closeSafely(encodedBytesArrayRef);
      EncodedImage.closeSafely(encodedImage);
      CloseableReference.closeSafely(jpgRef);
    }
  }

  private static BitmapFactory.Options getBitmapFactoryOptions(
      int sampleSize,
      Bitmap.Config bitmapConfig) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = true; // known to improve picture quality at low cost<commit_msg>Fixed alpha for Webp<commit_after>/*
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.facebook.imagepipeline.bitmaps;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;

import com.facebook.common.references.CloseableReference;
import com.facebook.common.webp.BitmapCreator;
import com.facebook.imageformat.DefaultImageFormats;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.memory.FlexByteArrayPool;
import com.facebook.imagepipeline.memory.PoolFactory;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import com.facebook.imageutils.JfifUtil;

/**
 * This is the implementation of the BitmapCreator for the Honeycomb
 */
public class HoneycombBitmapCreator implements BitmapCreator {

  protected static final byte[] EOI = new byte[]{
      (byte) JfifUtil.MARKER_FIRST_BYTE, (byte) JfifUtil.MARKER_EOI};

  private final EmptyJpegGenerator mJpegGenerator;
  private final FlexByteArrayPool mFlexByteArrayPool;

  public HoneycombBitmapCreator(PoolFactory poolFactory) {
    mFlexByteArrayPool = poolFactory.getFlexByteArrayPool();
    mJpegGenerator = new EmptyJpegGenerator(poolFactory.getPooledByteBufferFactory());
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
  @Override
  public Bitmap createNakedBitmap(
      int width, int height, Bitmap.Config bitmapConfig) {
    CloseableReference<PooledByteBuffer> jpgRef = mJpegGenerator.generate(
        (short) width,
        (short) height);
    EncodedImage encodedImage = null;
    CloseableReference<byte[]> encodedBytesArrayRef = null;
    try {
      encodedImage = new EncodedImage(jpgRef);
      encodedImage.setImageFormat(DefaultImageFormats.JPEG);
      BitmapFactory.Options options = getBitmapFactoryOptions(
          encodedImage.getSampleSize(),
          bitmapConfig);
      int length = jpgRef.get().size();
      byte[] suffix = endsWithEOI(jpgRef, length) ? null : EOI;
      final PooledByteBuffer pooledByteBuffer = jpgRef.get();
      encodedBytesArrayRef =
          mFlexByteArrayPool.get(length + 2);
      byte[] encodedBytesArray = encodedBytesArrayRef.get();
      pooledByteBuffer.read(0, encodedBytesArray, 0, length);
      if (suffix != null) {
        putEOI(encodedBytesArray, length);
        length += 2;
      }
      Bitmap bitmap = BitmapFactory.decodeByteArray(
          encodedBytesArray,
          0,
          length,
          options);
      bitmap.setHasAlpha(true);
      bitmap.eraseColor(Color.TRANSPARENT);
      return bitmap;
    } finally {
      CloseableReference.closeSafely(encodedBytesArrayRef);
      EncodedImage.closeSafely(encodedImage);
      CloseableReference.closeSafely(jpgRef);
    }
  }

  private static BitmapFactory.Options getBitmapFactoryOptions(
      int sampleSize,
      Bitmap.Config bitmapConfig) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = true; // known to improve picture quality at low cost
{'fraction_non_alphanumeric': 0.08328211432083589, 'fraction_numerical': 0.0029194837123540257, 'mean_word_length': 3.995395241749808, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5016748', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1795, 'n_words': 436}