Java Code Examples for com.bumptech.glide.load.resource.bitmap.BitmapResource#obtain()

The following examples show how to use com.bumptech.glide.load.resource.bitmap.BitmapResource#obtain() . 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: RawFileDecoder.java    From glide-support with The Unlicense 6 votes vote down vote up
@Override public @Nullable Resource<Bitmap> decode(File file, int w, int h, Options options) throws IOException {
	ByteBuffer buffer = ByteBuffer.allocate(w * h * 4);
	FileInputStream stream = new FileInputStream(file);
	try {
		stream.getChannel().read(buffer);
	} finally {
		stream.close();
	}
	Bitmap result = Bitmap.createBitmap(w, h, Config.ARGB_8888);
	try {
		buffer.rewind();
		result.copyPixelsFromBuffer(buffer);
		return BitmapResource.obtain(result, pool);
	} catch (RuntimeException ex) {
		result.recycle();
		throw ex;
	}
}
 
Example 2
Source File: MaskTransformation.java    From Android with MIT License 6 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();
    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }

    Drawable mask = getMaskDrawable(mContext, mMaskId);

    Canvas canvas = new Canvas(result);
    mask.setBounds(0, 0, width, height);
    mask.draw(canvas);
    canvas.drawBitmap(source, 0, 0, sMaskingPaint);
    return BitmapResource.obtain(result, mBitmapPool);
}
 
Example 3
Source File: GrayscaleTransformation.java    From giffun with Apache License 2.0 6 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(width, height, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, config);
  }

  Canvas canvas = new Canvas(bitmap);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(source, 0, 0, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 4
Source File: MaskTransformation.java    From giffun with Apache License 2.0 6 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
  if (result == null) {
    result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  }

  Drawable mask = Utils.getMaskDrawable(mContext, mMaskId);

  Canvas canvas = new Canvas(result);
  mask.setBounds(0, 0, width, height);
  mask.draw(canvas);
  canvas.drawBitmap(source, 0, 0, sMaskingPaint);

  return BitmapResource.obtain(result, mBitmapPool);
}
 
Example 5
Source File: GrayscaleTransformation.java    From AccountBook with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(width, height, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, config);
  }

  Canvas canvas = new Canvas(bitmap);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(source, 0, 0, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 6
Source File: CropSquareTransformation.java    From giffun with Apache License 2.0 6 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  int size = Math.min(source.getWidth(), source.getHeight());

  mWidth = (source.getWidth() - size) / 2;
  mHeight = (source.getHeight() - size) / 2;

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 7
Source File: BitmapTransformation.java    From AcgClub with MIT License 6 votes vote down vote up
@Override
public final Resource<Bitmap> transform(Context context, Resource<Bitmap> resource, int outWidth,
    int outHeight) {
  if (!Util.isValidDimensions(outWidth, outHeight)) {
    throw new IllegalArgumentException(
        "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight
            + " less than or equal to zero and not Target.SIZE_ORIGINAL");
  }
  BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
  Bitmap toTransform = resource.get();
  int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth;
  int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight;
  Bitmap transformed = transform(context.getApplicationContext(), bitmapPool, toTransform,
      targetWidth, targetHeight);

  final Resource<Bitmap> result;
  if (toTransform.equals(transformed)) {
    result = resource;
  } else {
    result = BitmapResource.obtain(transformed, bitmapPool);
  }
  return result;
}
 
Example 8
Source File: MaskTransformation.java    From AccountBook with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
  if (result == null) {
    result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  }

  Drawable mask = Utils.getMaskDrawable(mContext, mMaskId);

  Canvas canvas = new Canvas(result);
  mask.setBounds(0, 0, width, height);
  mask.draw(canvas);
  canvas.drawBitmap(source, 0, 0, sMaskingPaint);

  return BitmapResource.obtain(result, mBitmapPool);
}
 
Example 9
Source File: CropSquareTransformation.java    From AccountBook with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  int size = Math.min(source.getWidth(), source.getHeight());

  mWidth = (source.getWidth() - size) / 2;
  mHeight = (source.getHeight() - size) / 2;

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 10
Source File: BlurTransformation.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();
  int scaledWidth = width / mSampling;
  int scaledHeight = height / mSampling;

  Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(bitmap);
  canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
  Paint paint = new Paint();
  paint.setFlags(Paint.FILTER_BITMAP_FLAG);
  canvas.drawBitmap(source, 0, 0, paint);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    try {
      bitmap = RSBlur.blur(mContext, bitmap, mRadius);
    } catch (RSRuntimeException e) {
      bitmap = FastBlur.blur(bitmap, mRadius, true);
    }
  } else {
    bitmap = FastBlur.blur(bitmap, mRadius, true);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 11
Source File: BlurTransformation.java    From AccountBook with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();
  int scaledWidth = width / mSampling;
  int scaledHeight = height / mSampling;

  Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(bitmap);
  canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
  Paint paint = new Paint();
  paint.setFlags(Paint.FILTER_BITMAP_FLAG);
  canvas.drawBitmap(source, 0, 0, paint);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    try {
      bitmap = RSBlur.blur(mContext, bitmap, mRadius);
    } catch (RSRuntimeException e) {
      bitmap = FastBlur.blur(bitmap, mRadius, true);
    }
  } else {
    bitmap = FastBlur.blur(bitmap, mRadius, true);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 12
Source File: BlurTransformation.java    From Dota2Helper with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();
    int scaledWidth = width / mSampling;
    int scaledHeight = height / mSampling;

    Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(source, 0, 0, paint);

    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(mRadius);
    blur.forEach(output);
    output.copyTo(bitmap);

    rs.destroy();

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 13
Source File: BlurTransform.java    From ImageLoader with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();

    int samplesize = MyUtil.calculateScaleRatio(width,height,outWidth,outHeight,subsamplingRatio);



    int scaledWidth = width / samplesize;
    int scaledHeight = height / samplesize;

    Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) samplesize, 1 / (float) samplesize);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(source, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        try {
            bitmap = RSBlur.blur(mContext, bitmap, mRadius);
        } catch (RSRuntimeException e) {
            bitmap = FastBlur.blur(bitmap, mRadius, true);
        }
    } else {
        bitmap = FastBlur.blur(bitmap, mRadius, true);
    }

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 14
Source File: RegionResourceDecoder.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override public Resource<Bitmap> decode(T source, int width, int height) throws IOException {
	BitmapRegionDecoder decoder = createDecoder(source, width, height);
	Options opts = new Options();
	// Algorithm from Glide's Downsampler.getRoundedSampleSize
	int sampleSize = (int)Math.ceil((double)region.width() / (double)width);
	sampleSize = sampleSize == 0? 0 : Integer.highestOneBit(sampleSize);
	sampleSize = Math.max(1, sampleSize);
	opts.inSampleSize = sampleSize;

	// Although functionally equivalent to 0 for BitmapFactory, 1 is a safer default for our code than 0.
	Bitmap bitmap = decoder.decodeRegion(region, opts);
	return BitmapResource.obtain(bitmap, bitmapPool);
}
 
Example 15
Source File: AFGlideUtil.java    From AFBaseLibrary with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();
    int scaledWidth = width / mSampling;
    int scaledHeight = height / mSampling;

    Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(source, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        try {
            bitmap = blur(mContext, bitmap, mRadius);
        } catch (RSRuntimeException e) {
            bitmap = blur(bitmap, mRadius, true);
        }
    } else {
        bitmap = blur(bitmap, mRadius, true);
    }

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 16
Source File: CropCircleTransformation.java    From AccountBook with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  int size = Math.min(source.getWidth(), source.getHeight());

  int width = (source.getWidth() - size) / 2;
  int height = (source.getHeight() - size) / 2;

  Bitmap bitmap = mBitmapPool.get(size, size, Bitmap.Config.ARGB_8888);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(bitmap);
  Paint paint = new Paint();
  BitmapShader shader =
      new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
  if (width != 0 || height != 0) {
    // source isn't square, move viewport to center
    Matrix matrix = new Matrix();
    matrix.setTranslate(-width, -height);
    shader.setLocalMatrix(matrix);
  }
  paint.setShader(shader);
  paint.setAntiAlias(true);

  float r = size / 2f;
  canvas.drawCircle(r, r, r, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 17
Source File: CropTransformation.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  mWidth = mWidth == 0 ? source.getWidth() : mWidth;
  mHeight = mHeight == 0 ? source.getHeight() : mHeight;

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(mWidth, mHeight, config);
  }

  float scaleX = (float) mWidth / source.getWidth();
  float scaleY = (float) mHeight / source.getHeight();
  float scale = Math.max(scaleX, scaleY);

  float scaledWidth = scale * source.getWidth();
  float scaledHeight = scale * source.getHeight();
  float left = (mWidth - scaledWidth) / 2;
  float top = getTop(scaledHeight);
  RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

  Canvas canvas = new Canvas(bitmap);
  canvas.drawBitmap(source, null, targetRect, null);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 18
Source File: BlurTransformation.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();
  int scaledWidth = width / mSampling;
  int scaledHeight = height / mSampling;

  Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(bitmap);
  canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
  Paint paint = new Paint();
  paint.setFlags(Paint.FILTER_BITMAP_FLAG);
  canvas.drawBitmap(source, 0, 0, paint);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    try {
      bitmap = RSBlur.blur(mContext, bitmap, mRadius);
    } catch (RSRuntimeException e) {
      bitmap = FastBlur.blur(bitmap, mRadius, true);
    }
  } else {
    bitmap = FastBlur.blur(bitmap, mRadius, true);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 19
Source File: BitmapBitmapResourceDecoder.java    From glide-support with The Unlicense 4 votes vote down vote up
@Override public Resource<Bitmap> decode(Bitmap source, int width, int height) throws IOException {
	return BitmapResource.obtain(source, pool);
}
 
Example 20
Source File: GifFrameResourceDecoder.java    From giffun with Apache License 2.0 4 votes vote down vote up
@Override
public Resource<Bitmap> decode(GifDecoder source, int width, int height) {
    Bitmap bitmap = source.getNextFrame();
    return BitmapResource.obtain(bitmap, bitmapPool);
}