Example #1
0
 // region Public methods
 public Bitmap getBitmap(File file, int type) {
   BitmapFactory.Options options = new BitmapFactory.Options();
   switch (type) {
     case BACKGROUND:
       options.inBitmap = background;
       break;
     case SUBJECT:
       options.inBitmap = subject;
       break;
     case RAW:
       options.inBitmap = raw;
       break;
   }
   return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 }
Example #2
0
  public DessertCaseView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    final Resources res = getResources();

    mStarted = false;

    mCellSize = res.getDimensionPixelSize(R.dimen.dessert_case_cell_size);
    final BitmapFactory.Options opts = new BitmapFactory.Options();
    if (mCellSize < 512) { // assuming 512x512 images
      opts.inSampleSize = 2;
    }
    opts.inMutable = true;
    Bitmap loaded = null;
    for (int[] list : new int[][] {PASTRIES, RARE_PASTRIES, XRARE_PASTRIES, XXRARE_PASTRIES}) {
      for (int resid : list) {
        opts.inBitmap = loaded;
        loaded = BitmapFactory.decodeResource(res, resid, opts);
        final BitmapDrawable d = new BitmapDrawable(res, convertToAlphaMask(loaded));
        d.setColorFilter(new ColorMatrixColorFilter(ALPHA_MASK));
        d.setBounds(0, 0, mCellSize, mCellSize);
        mDrawables.append(resid, d);
      }
    }
    loaded = null;
    if (DEBUG) setWillNotDraw(false);
  }
  private CloseableReference<Bitmap> decodeStaticImageFromStream(
      InputStream inputStream, BitmapFactory.Options options) {
    Preconditions.checkNotNull(inputStream);
    int sizeInBytes =
        BitmapUtil.getSizeInByteForBitmap(
            options.outWidth, options.outHeight, options.inPreferredConfig);
    final Bitmap bitmapToReuse = mBitmapPool.get(sizeInBytes);
    if (bitmapToReuse == null) {
      throw new NullPointerException("BitmapPool.get returned null");
    }
    options.inBitmap = bitmapToReuse;

    Bitmap decodedBitmap;
    ByteBuffer byteBuffer = mDecodeBuffers.acquire();
    if (byteBuffer == null) {
      byteBuffer = ByteBuffer.allocate(DECODE_BUFFER_SIZE);
    }
    try {
      options.inTempStorage = byteBuffer.array();
      decodedBitmap = BitmapFactory.decodeStream(inputStream, null, options);
    } catch (RuntimeException re) {
      mBitmapPool.release(bitmapToReuse);
      throw re;
    } finally {
      mDecodeBuffers.release(byteBuffer);
    }

    if (bitmapToReuse != decodedBitmap) {
      mBitmapPool.release(bitmapToReuse);
      decodedBitmap.recycle();
      throw new IllegalStateException();
    }

    return CloseableReference.of(decodedBitmap, mBitmapPool);
  }
 private Bitmap readFromDb(String name, Bitmap b) {
   if (mCachedSelectQuery == null) {
     mCachedSelectQuery = CacheDb.COLUMN_NAME + " = ? AND " + CacheDb.COLUMN_SIZE + " = ?";
   }
   SQLiteDatabase db = mDb.getReadableDatabase();
   Cursor result =
       db.query(
           CacheDb.TABLE_NAME,
           new String[] {CacheDb.COLUMN_PREVIEW_BITMAP}, // cols to return
           mCachedSelectQuery, // select query
           new String[] {name, mSize}, // args to select query
           null,
           null,
           null,
           null);
   if (result.getCount() > 0) {
     result.moveToFirst();
     byte[] blob = result.getBlob(0);
     result.close();
     final BitmapFactory.Options opts = mCachedBitmapFactoryOptions.get();
     opts.inBitmap = b;
     opts.inSampleSize = 1;
     try {
       return BitmapFactory.decodeByteArray(blob, 0, blob.length, opts);
     } catch (IllegalArgumentException e) {
       removeItemFromDb(mDb, name);
       return null;
     }
   } else {
     result.close();
     return null;
   }
 }
Example #5
0
 /**
  * ponerOpcionInBitmap: Checa si es posible reutilizar un bitmap desechado de acuerdo a los
  * requerimientos de esta imagen en particular, de ser asi activa la opcion inBitmap.
  *
  * @param opciones BitmapFactory.Options de la imagen nueva.
  */
 private void ponerOpcionInBitmap(BitmapFactory.Options opciones) {
   // Tiene que ser mutable
   opciones.inMutable = true;
   Bitmap bitmap_reutilizable = obtenerBitmapDesechado(opciones);
   // Activa inBitmap en las opciones con el bitmap encontrado
   if (bitmap_reutilizable != null) {
     opciones.inBitmap = bitmap_reutilizable;
   }
 }
Example #6
0
  public GLPicture(BitmapRegionLoader bitmapRegionLoader, int maxHeight) {
    if (bitmapRegionLoader == null || maxHeight == 0) {
      return;
    }

    mHasContent = true;
    mVertexBuffer = GLUtil.newFloatBuffer(mVertices.length);
    mTextureCoordsBuffer = GLUtil.asFloatBuffer(SQUARE_TEXTURE_VERTICES);

    int originalWidth = bitmapRegionLoader.getWidth();
    int originalHeight = bitmapRegionLoader.getHeight();
    int sampleSize = 1;
    while (originalHeight / (sampleSize << 1) > maxHeight) {
      sampleSize <<= 1;
    }

    mWidth = originalWidth / sampleSize;
    mHeight = originalHeight / sampleSize;

    mTileSize = Math.min(512, sMaxTextureSize);
    int unsampledTileSize = mTileSize * sampleSize;
    int leftoverHeight = originalHeight % unsampledTileSize;

    // Load m x n textures
    mCols = mWidth / (mTileSize + 1) + 1;
    mRows = mHeight / (mTileSize + 1) + 1;

    mTextureHandles = new int[mCols * mRows];

    Bitmap tileBitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
    Rect rect = new Rect();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;
    options.inBitmap = tileBitmap;
    for (int y = 0; y < mRows; y++) {
      for (int x = 0; x < mCols; x++) {
        rect.set(
            x * unsampledTileSize,
            (mRows - y - 1) * unsampledTileSize,
            (x + 1) * unsampledTileSize,
            (mRows - y) * unsampledTileSize);
        // The bottom tiles must be full tiles for drawing, so only allow edge tiles
        // at the top
        if (leftoverHeight > 0) {
          rect.offset(0, -unsampledTileSize + leftoverHeight);
        }
        rect.intersect(0, 0, originalWidth, originalHeight);
        Bitmap useBitmap = bitmapRegionLoader.decodeRegion(rect, options);
        mTextureHandles[y * mCols + x] = GLUtil.loadTexture(useBitmap);
        if (useBitmap != tileBitmap) {
          useBitmap.recycle();
        }
      }
    }
  }
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private void addInBitmapOptions(BitmapFactory.Options options, ImageCache cache) {
    options.inMutable = true;

    if (cache != null) {
      Bitmap inBitmap = cache.getBitmapFromReusableSet(options);

      if (inBitmap != null) {
        options.inBitmap = inBitmap;
      }
    }
  }
Example #8
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public static Bitmap decodeUsingPool(
      JobContext jc, byte[] data, int offset, int length, BitmapFactory.Options options) {
    if (options == null) options = new BitmapFactory.Options();
    if (options.inSampleSize < 1) options.inSampleSize = 1;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inBitmap =
        (options.inSampleSize == 1) ? findCachedBitmap(jc, data, offset, length, options) : null;
    try {
      Bitmap bitmap = decode(jc, data, offset, length, options);
      if (options.inBitmap != null && options.inBitmap != bitmap) {
        GalleryBitmapPool.getInstance().put(options.inBitmap);
        options.inBitmap = null;
      }
      return bitmap;
    } catch (IllegalArgumentException e) {
      if (options.inBitmap == null) throw e;

      Log.w(TAG, "decode fail with a given bitmap, try decode to a new bitmap");
      GalleryBitmapPool.getInstance().put(options.inBitmap);
      options.inBitmap = null;
      return decode(jc, data, offset, length, options);
    }
  }
Example #9
0
  private static void addInBitmapOptions(
      BitmapFactory.Options options, Set<SoftReference<Bitmap>> reuseableBitmaps) {
    // inBitmap only works with mutable bitmaps, so force the decoder to
    // return mutable bitmaps.
    options.inMutable = true;

    // Try to find a bitmap to use for inBitmap.
    Bitmap inBitmap = getBitmapFromReusableSet(reuseableBitmaps, options);

    if (inBitmap != null) {
      // If a suitable bitmap has been found, set it as the value of
      // inBitmap.
      options.inBitmap = inBitmap;
    }
  }
Example #10
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private static void addInBitmapOptions(BitmapFactory.Options options, ImageCache cache) {
    // BEGIN_INCLUDE(add_bitmap_options)
    // inBitmap only works with mutable bitmaps so force the decoder to
    // return mutable bitmaps.
    options.inMutable = true;

    if (cache != null) {
      // Try and find a bitmap to use for inBitmap
      Bitmap inBitmap = cache.getBitmapFromReusableSet(options);

      if (inBitmap != null) {
        options.inBitmap = inBitmap;
      }
    }
    // END_INCLUDE(add_bitmap_options)
  }
Example #11
0
  public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inMutable = true;
    options.inBitmap = BitmapFactory.decodeFile(path, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inScaled = true;
    options.inDensity = options.outWidth;
    options.inTargetDensity = reqWidth * options.inSampleSize;

    options.inJustDecodeBounds = false;
    options.inPurgeable = true;
    options.inInputShareable = true;

    return BitmapFactory.decodeFile(path, options);
  }
Example #12
0
  /** Decode and sample down a bitmap from a byte stream */
  public static Bitmap decodeSampledBitmapFromByte(Context context, byte[] bitmapBytes) {
    Display display =
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    int reqWidth, reqHeight;
    Point point = new Point();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
      display.getSize(point);
      reqWidth = point.x;
      reqHeight = point.y;
    } else {
      reqWidth = display.getWidth();
      reqHeight = display.getHeight();
    }

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inMutable = true;
    options.inBitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Load & resize the image to be 1/inSampleSize dimensions
    // Use when you do not want to scale the image with a inSampleSize that is a power of 2
    options.inScaled = true;
    options.inDensity = options.outWidth;
    options.inTargetDensity = reqWidth * options.inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds =
        false; // If set to true, the decoder will return null (no bitmap), but the out... fields
               // will still be set, allowing the caller to query the bitmap without having to
               // allocate the memory for its pixels.
    options.inPurgeable =
        true; // Tell to gc that whether it needs free memory, the Bitmap can be cleared
    options.inInputShareable =
        true; // Which kind of reference will be used to recover the Bitmap data after being clear,
              // when it will be used in the future

    return BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options);
  }
Example #13
0
 @TargetApi(11)
 private BitmapFactory.Options createTileBitmapFactoryOptions(
     int tileSize, boolean isTransparent) {
   BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
   if (isTransparent) {
     bitmapFactoryOptions.inPreferredConfig = AndroidGraphicFactory.TRANSPARENT_BITMAP;
   } else {
     bitmapFactoryOptions.inPreferredConfig = AndroidGraphicFactory.NON_TRANSPARENT_BITMAP;
   }
   if (org.mapsforge.map.android.util.AndroidUtil.HONEYCOMB_PLUS) {
     android.graphics.Bitmap reusableBitmap =
         getTileBitmapFromReusableSet(tileSize, isTransparent);
     if (reusableBitmap != null) {
       bitmapFactoryOptions.inMutable = true;
       bitmapFactoryOptions.inSampleSize =
           1; // not really sure why this is required, but otherwise decoding
       // fails
       bitmapFactoryOptions.inBitmap = getTileBitmapFromReusableSet(tileSize, isTransparent);
     }
   }
   return bitmapFactoryOptions;
 }
Example #14
0
 public Bitmap getRawBitmap(byte[] data) {
   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inMutable = true;
   options.inBitmap = raw;
   return BitmapFactory.decodeByteArray(data, 0, data.length, options);
 }
Example #15
0
 @TargetApi(11)
 private static void setInBitmap(BitmapFactory.Options options, Bitmap recycled) {
   if (Build.VERSION.SDK_INT >= 11) {
     options.inBitmap = recycled;
   }
 }