Java Code Examples for android.graphics.Bitmap#setPixel()

The following examples show how to use android.graphics.Bitmap#setPixel() . 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: PortraitBlurService.java    From CameraBlur with Apache License 2.0 6 votes vote down vote up
public Bitmap getBitmap(int mOutputs[],Bitmap bitmap,int bRad,int eRad){
    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Bitmap blur=ImageUtils.RenderBlur(PortraitBlurService.this,bitmap,bRad);
    Bitmap softBlur=ImageUtils.RenderBlur(PortraitBlurService.this, bitmap,eRad);

    int imgMatrixEroded[][]=new int[w][h];
    int imgMatrixDilated[][]=new int[w][h];
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            imgMatrixEroded[x][y]=imgMatrixDilated[x][y]=mOutputs[y * w + x];
        }
    }
    imgMatrixDilated=ImageUtils.dilate(imgMatrixDilated,1);
    imgMatrixEroded=ImageUtils.erode(imgMatrixEroded,2);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            output.setPixel(x, y,imgMatrixDilated[x][y]==1 ?  (imgMatrixEroded[x][y]==1?bitmap.getPixel(x,y):softBlur.getPixel(x,y)):blur.getPixel(x,y));
        }
    }
    return output;
}
 
Example 2
Source File: GreyScaleCalculus.java    From Alexei with Apache License 2.0 6 votes vote down vote up
@Override
protected Bitmap theCalculation(Bitmap image) throws Exception {

    Bitmap bitmap = image.copy(image.getConfig(), true);

    for(int i = 0; i < bitmap.getWidth(); i++)
        for(int j = 0; j < bitmap.getHeight(); j++) {
            int pixel = image.getPixel(i,j);

            int alpha = Color.alpha(pixel);
            int red = Color.red(pixel);
            int green = Color.green(pixel);
            int blue = Color.blue(pixel);

            red = green = blue = (int) (RED_FACTOR * red + GREEN_FACTOR * green + BLUE_FACTOR * blue);

            bitmap.setPixel(i,j,Color.argb(alpha,red,green,blue));
        }

    return bitmap;
}
 
Example 3
Source File: BitmapFromImage.java    From rosjava_android_template with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap call(sensor_msgs.Image message) {
  Preconditions.checkArgument(message.getEncoding().equals("rgb8"));
  Bitmap bitmap =
          Bitmap.createBitmap(message.getWidth(), message.getHeight(),
                  Bitmap.Config.ARGB_8888);
  for (int x = 0; x < message.getWidth(); x++) {
    for (int y = 0; y < message.getHeight(); y++) {
      ChannelBuffer data = message.getData();
      byte red = data.getByte(y * message.getStep() + 3 * x);
      byte green = data.getByte(y * message.getStep() + 3 * x + 1);
      byte blue = data.getByte(y * message.getStep() + 3 * x + 2);
      bitmap.setPixel(x, y, Color.argb(255, red & 0xFF, green & 0xFF, blue & 0xFF));
    }
  }
  return bitmap;
}
 
Example 4
Source File: DarkenPostprocessor.java    From oneHookLibraryAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Bitmap bitmap) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();
    int pixel;

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            pixel = bitmap.getPixel(x, y);

            final int red = Color.red(pixel);
            final int green = Color.green(pixel);
            final int blue = Color.blue(pixel);

            pixel = Color.rgb(
                    applyDegree(red),
                    applyDegree(green),
                    applyDegree(blue));

            bitmap.setPixel(x, y, pixel);
        }
    }
}
 
Example 5
Source File: MainActivity.java    From android-apps with MIT License 5 votes vote down vote up
/**
 * This method used for converting BitMatrix to BitMap
 * @param matrix
 * @return bitmap
 */
public static Bitmap toBitmap(BitMatrix bitMatrix){
    int height = bitMatrix.getHeight();
    int width = bitMatrix.getWidth();
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int x = 0; x < width; x++){
        for (int y = 0; y < height; y++){
            bmp.setPixel(x, y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE);
        }
    }
    return bmp;
}
 
Example 6
Source File: GroupDetailActivity.java    From Rumble with GNU General Public License v3.0 5 votes vote down vote up
public void invite() {
    String buffer = group.getGroupBase64ID();
    int size = 200;
    Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
    hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    try {
        BitMatrix bitMatrix = qrCodeWriter.encode(buffer, BarcodeFormat.QR_CODE, size, size, hintMap);
        Bitmap image = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);

        if(image != null) {
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    image.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK : Color.WHITE);
                }
            }
            Intent intent = new Intent(this, DisplayQRCode.class);
            intent.putExtra("EXTRA_GROUP_NAME", groupName);
            intent.putExtra("EXTRA_BUFFER", buffer);
            intent.putExtra("EXTRA_QRCODE", image);
            startActivity(intent);
            overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
        }
    }catch(WriterException ignore) {
    }
    //}
}
 
Example 7
Source File: ImageUtils.java    From ScreenCapture with MIT License 5 votes vote down vote up
public static Bitmap image_ARGB8888_2_bitmap(DisplayMetrics metrics, Image image) {
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer buffer = planes[0].getBuffer();

        int width = image.getWidth();
//        Log.d("WOW", "image w = " + width);
        int height = image.getHeight();
//        Log.d("WOW", "image h = " + height);

        int pixelStride = planes[0].getPixelStride();
//        Log.d("WOW", "pixelStride is " + pixelStride);
        int rowStride = planes[0].getRowStride();
//        Log.d("WOW", "row Stride is " + rowStride);
        int rowPadding = rowStride - pixelStride * width;
//        Log.d("WOW", "rowPadding is " + rowPadding);

        int offset = 0;
        Bitmap bitmap;
        bitmap = Bitmap.createBitmap(metrics, width, height, Bitmap.Config.ARGB_8888);
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                int pixel = 0;
                pixel |= (buffer.get(offset) & 0xff) << 16;     // R
                pixel |= (buffer.get(offset + 1) & 0xff) << 8;  // G
                pixel |= (buffer.get(offset + 2) & 0xff);       // B
                pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
                bitmap.setPixel(j, i, pixel);
                offset += pixelStride;
            }
            offset += rowPadding;
        }
        return bitmap;
    }
 
Example 8
Source File: ImageDsicern.java    From VMLibrary with Apache License 2.0 5 votes vote down vote up
/**
 * 修改 Bitmap 像素点颜色
 *
 * @param x      像素点x坐标
 * @param y      像素点y坐标
 * @param bitmap bitmap 图片数据
 * @param color  修改的颜色
 */
private static void changeBitmapPixel(int x, int y, Bitmap bitmap, int color) {
    int pixel = bitmap.getPixel(x, y);
    final int alpha = (pixel >> 24) & 0xff;
    if (alpha > 0 && alpha != 0xff) {
        color = (alpha << 24) | (color & 0xffffff);
    }
    bitmap.setPixel(x, y, (alpha << 24) | color);
}
 
Example 9
Source File: ConstraintLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void dispatchDraw(@NonNull Canvas canvas) {
    boolean r = revealAnimator != null && revealAnimator.isRunning();
    boolean c = !Carbon.isShapeRect(shapeModel, boundsRect);

    if (Carbon.IS_PIE_OR_HIGHER) {
        if (spotShadowColor != null)
            super.setOutlineSpotShadowColor(spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()));
        if (ambientShadowColor != null)
            super.setOutlineAmbientShadowColor(ambientShadowColor.getColorForState(getDrawableState(), ambientShadowColor.getDefaultColor()));
    }

    // draw not called, we have to handle corners here
    if (isInEditMode() && !drawCalled && (r || c) && getWidth() > 0 && getHeight() > 0) {
        Bitmap layer = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas layerCanvas = new Canvas(layer);
        dispatchDrawInternal(layerCanvas);

        Bitmap mask = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas maskCanvas = new Canvas(mask);
        Paint maskPaint = new Paint(0xffffffff);
        maskCanvas.drawPath(cornersMask, maskPaint);

        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                int maskPixel = mask.getPixel(x, y);
                layer.setPixel(x, y, Color.alpha(maskPixel) > 0 ? layer.getPixel(x, y) : 0);
            }
        }
        canvas.drawBitmap(layer, 0, 0, paint);
    } else if (!drawCalled && (r || c) && getWidth() > 0 && getHeight() > 0 && !Carbon.IS_LOLLIPOP_OR_HIGHER) {
        int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        if (r) {
            int saveCount2 = canvas.save();
            canvas.clipRect(revealAnimator.x - revealAnimator.radius, revealAnimator.y - revealAnimator.radius, revealAnimator.x + revealAnimator.radius, revealAnimator.y + revealAnimator.radius);
            dispatchDrawInternal(canvas);
            canvas.restoreToCount(saveCount2);
        } else {
            dispatchDrawInternal(canvas);
        }

        paint.setXfermode(Carbon.CLEAR_MODE);
        if (c) {
            cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
            canvas.drawPath(cornersMask, paint);
        }
        if (r)
            canvas.drawPath(revealAnimator.mask, paint);
        paint.setXfermode(null);

        canvas.restoreToCount(saveCount);
    } else {
        dispatchDrawInternal(canvas);
    }
    drawCalled = false;
}
 
Example 10
Source File: SatelliteRenderer.java    From blocktopograph with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Render a single chunk to provided bitmap (bm)
 * @param cm ChunkManager, provides chunks, which provide chunk-data
 * @param bm Bitmap to render to
 * @param dimension Mapped dimension
 * @param chunkX X chunk coordinate (x-block coord / Chunk.WIDTH)
 * @param chunkZ Z chunk coordinate (z-block coord / Chunk.LENGTH)
 * @param bX begin block X coordinate, relative to chunk edge
 * @param bZ begin block Z coordinate, relative to chunk edge
 * @param eX end block X coordinate, relative to chunk edge
 * @param eZ end block Z coordinate, relative to chunk edge
 * @param pX texture X pixel coord to start rendering to
 * @param pY texture Y pixel coord to start rendering to
 * @param pW width (X) of one block in pixels
 * @param pL length (Z) of one block in pixels
 * @return bm is returned back
 *
 * @throws Version.VersionException when the version of the chunk is unsupported.
 */
public Bitmap renderToBitmap(ChunkManager cm, Bitmap bm, Dimension dimension, int chunkX, int chunkZ, int bX, int bZ, int eX, int eZ, int pX, int pY, int pW, int pL) throws Version.VersionException {

    Chunk chunk = cm.getChunk(chunkX, chunkZ);
    Version cVersion = chunk.getVersion();

    if(cVersion == Version.ERROR) return MapType.ERROR.renderer.renderToBitmap(cm, bm, dimension, chunkX, chunkZ, bX, bZ, eX, eZ, pX, pY, pW, pL);
    if(cVersion == Version.NULL) return MapType.CHESS.renderer.renderToBitmap(cm, bm, dimension, chunkX, chunkZ, bX, bZ, eX, eZ, pX, pY, pW, pL);

    //the bottom sub-chunk is sufficient to get heightmap data.
    TerrainChunkData data = chunk.getTerrain((byte) 0);
    if(data == null || !data.load2DData()) return MapType.CHESS.renderer.renderToBitmap(cm, bm, dimension, chunkX, chunkZ, bX, bZ, eX, eZ, pX, pY, pW, pL);


    TerrainChunkData dataW = cm.getChunk(chunkX - 1, chunkZ).getTerrain((byte) 0);
    TerrainChunkData dataN = cm.getChunk(chunkX, chunkZ-1).getTerrain((byte) 0);

    boolean west = dataW != null && dataW.load2DData(),
            north = dataN != null && dataN.load2DData();

    int x, y, z, color, i, j, tX, tY;
    for (z = bZ, tY = pY ; z < eZ; z++, tY += pL) {
        for (x = bX, tX = pX; x < eX; x++, tX += pW) {

            y = data.getHeightMapValue(x, z);

            color = getColumnColour(chunk, data, x, y, z,
                    (x == 0) ? (west ? dataW.getHeightMapValue(dimension.chunkW - 1, z) : y)//chunk edge
                             : data.getHeightMapValue(x - 1, z),//within chunk
                    (z == 0) ? (north ? dataN.getHeightMapValue(x, dimension.chunkL - 1) : y)//chunk edge
                             : data.getHeightMapValue(x, z - 1)//within chunk
            );

            for(i = 0; i < pL; i++){
                for(j = 0; j < pW; j++){
                    bm.setPixel(tX + j, tY + i, color);
                }
            }


        }
    }

    return bm;
}
 
Example 11
Source File: DrawerLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@SuppressLint("MissingSuperCall")
@Override
public void draw(@NonNull Canvas canvas) {
    drawCalled = true;
    boolean r = revealAnimator != null;
    boolean c = !Carbon.isShapeRect(shapeModel, boundsRect);

    if (Carbon.IS_PIE_OR_HIGHER) {
        if (spotShadowColor != null)
            super.setOutlineSpotShadowColor(spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()));
        if (ambientShadowColor != null)
            super.setOutlineAmbientShadowColor(ambientShadowColor.getColorForState(getDrawableState(), ambientShadowColor.getDefaultColor()));
    }

    if (isInEditMode() && (r || c) && getWidth() > 0 && getHeight() > 0) {
        Bitmap layer = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas layerCanvas = new Canvas(layer);
        drawInternal(layerCanvas);

        Bitmap mask = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas maskCanvas = new Canvas(mask);
        Paint maskPaint = new Paint(0xffffffff);
        maskCanvas.drawPath(cornersMask, maskPaint);

        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                int maskPixel = mask.getPixel(x, y);
                layer.setPixel(x, y, Color.alpha(maskPixel) > 0 ? layer.getPixel(x, y) : 0);
            }
        }
        canvas.drawBitmap(layer, 0, 0, paint);
    } else if (getWidth() > 0 && getHeight() > 0 && (((r || c) && !Carbon.IS_LOLLIPOP_OR_HIGHER) || !shapeModel.isRoundRect(boundsRect))) {
        int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        if (r) {
            int saveCount2 = canvas.save();
            canvas.clipRect(revealAnimator.x - revealAnimator.radius, revealAnimator.y - revealAnimator.radius, revealAnimator.x + revealAnimator.radius, revealAnimator.y + revealAnimator.radius);
            drawInternal(canvas);
            canvas.restoreToCount(saveCount2);
        } else {
            drawInternal(canvas);
        }

        paint.setXfermode(Carbon.CLEAR_MODE);
        if (c) {
            cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
            canvas.drawPath(cornersMask, paint);
        }
        if (r)
            canvas.drawPath(revealAnimator.mask, paint);
        paint.setXfermode(null);

        canvas.restoreToCount(saveCount);
        paint.setXfermode(null);
    } else {
        drawInternal(canvas);
    }
}
 
Example 12
Source File: FlowLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@SuppressLint("MissingSuperCall")
@Override
public void draw(@NonNull Canvas canvas) {
    drawCalled = true;
    boolean r = revealAnimator != null;
    boolean c = !Carbon.isShapeRect(shapeModel, boundsRect);

    if (Carbon.IS_PIE_OR_HIGHER) {
        if (spotShadowColor != null)
            super.setOutlineSpotShadowColor(spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()));
        if (ambientShadowColor != null)
            super.setOutlineAmbientShadowColor(ambientShadowColor.getColorForState(getDrawableState(), ambientShadowColor.getDefaultColor()));
    }

    if (isInEditMode() && (r || c) && getWidth() > 0 && getHeight() > 0) {
        Bitmap layer = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas layerCanvas = new Canvas(layer);
        drawInternal(layerCanvas);

        Bitmap mask = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas maskCanvas = new Canvas(mask);
        Paint maskPaint = new Paint(0xffffffff);
        maskCanvas.drawPath(cornersMask, maskPaint);

        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                int maskPixel = mask.getPixel(x, y);
                layer.setPixel(x, y, Color.alpha(maskPixel) > 0 ? layer.getPixel(x, y) : 0);
            }
        }
        canvas.drawBitmap(layer, 0, 0, paint);
    } else if (getWidth() > 0 && getHeight() > 0 && (((r || c) && !Carbon.IS_LOLLIPOP_OR_HIGHER) || !shapeModel.isRoundRect(boundsRect))) {
        int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        if (r) {
            int saveCount2 = canvas.save();
            canvas.clipRect(revealAnimator.x - revealAnimator.radius, revealAnimator.y - revealAnimator.radius, revealAnimator.x + revealAnimator.radius, revealAnimator.y + revealAnimator.radius);
            drawInternal(canvas);
            canvas.restoreToCount(saveCount2);
        } else {
            drawInternal(canvas);
        }

        paint.setXfermode(Carbon.CLEAR_MODE);
        if (c) {
            cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
            canvas.drawPath(cornersMask, paint);
        }
        if (r)
            canvas.drawPath(revealAnimator.mask, paint);
        paint.setXfermode(null);

        canvas.restoreToCount(saveCount);
        paint.setXfermode(null);
    } else {
        drawInternal(canvas);
    }
}
 
Example 13
Source File: BitmapProcessing.java    From Effects-Pro with MIT License 4 votes vote down vote up
public static final Bitmap sketch(Bitmap src) {
	int type = 6;
	int threshold = 130;
	
	int width = src.getWidth();
	int height = src.getHeight();
	Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());

	int A, R, G, B;
	int sumR, sumG, sumB;
	int[][] pixels = new int[3][3];
	for(int y = 0; y < height - 2; ++y) {
		for(int x = 0; x < width - 2; ++x) {
			//      get pixel matrix
			for(int i = 0; i < 3; ++i) {
				for(int j = 0; j < 3; ++j) {
					pixels[i][j] = src.getPixel(x + i, y + j);
				}
			}
			// get alpha of center pixel
			A = Color.alpha(pixels[1][1]);
			// init color sum
			sumR = sumG = sumB = 0;
			sumR = (type*Color.red(pixels[1][1])) - Color.red(pixels[0][0]) - Color.red(pixels[0][2]) - Color.red(pixels[2][0]) - Color.red(pixels[2][2]);
			sumG = (type*Color.green(pixels[1][1])) - Color.green(pixels[0][0]) - Color.green(pixels[0][2]) - Color.green(pixels[2][0]) - Color.green(pixels[2][2]);
			sumB = (type*Color.blue(pixels[1][1])) - Color.blue(pixels[0][0]) - Color.blue(pixels[0][2]) - Color.blue(pixels[2][0]) - Color.blue(pixels[2][2]);
			// get final Red
			R = (int)(sumR  + threshold);
			if(R < 0) { R = 0; }
			else if(R > 255) { R = 255; }
			// get final Green
			G = (int)(sumG  + threshold);
			if(G < 0) { G = 0; }
			else if(G > 255) { G = 255; }
			// get final Blue
			B = (int)(sumB  + threshold);
			if(B < 0) { B = 0; }
			else if(B > 255) { B = 255; }

			result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
		}
	}

	src.recycle();
	src = null;

	return result;
}
 
Example 14
Source File: BitmapProcessing.java    From Effects-Pro with MIT License 4 votes vote down vote up
public static Bitmap sepia(Bitmap src) {
	// image size
	int width = src.getWidth();
	int height = src.getHeight();
	// create output bitmap
	Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
	// constant grayscale
	final double GS_RED = 0.3;
	final double GS_GREEN = 0.59;
	final double GS_BLUE = 0.11;
	// color information
	int A, R, G, B;
	int pixel;

	// scan through all pixels
	for(int x = 0; x < width; ++x) {
		for(int y = 0; y < height; ++y) {
			// get pixel color
			pixel = src.getPixel(x, y);
			// get color on each channel
			A = Color.alpha(pixel);
			R = Color.red(pixel);
			G = Color.green(pixel);
			B = Color.blue(pixel);
			// apply grayscale sample
			B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);

			// apply intensity level for sepid-toning on each channel
			R += 110;
			if(R > 255) { R = 255; }

			G += 65;
			if(G > 255) { G = 255; }

			B += 20;
			if(B > 255) { B = 255; }

			// set new pixel color to output image
			bmOut.setPixel(x, y, Color.argb(A, R, G, B));
		}
	}

	src.recycle();
	src = null;

	return bmOut;
}
 
Example 15
Source File: DeeplabMobile.java    From ml with Apache License 2.0 4 votes vote down vote up
@Override
    public Bitmap segment(final Bitmap bitmap) {
        if (sTFInterface == null) {
            Logger.warn("tf model is NOT initialized.");
            return null;
        }

        if (bitmap == null) {
            return null;
        }

        final int w = bitmap.getWidth();
        final int h = bitmap.getHeight();
        Logger.debug("bitmap: %d x %d,", w, h);

        if (w > INPUT_SIZE || h > INPUT_SIZE) {
            Logger.warn("invalid bitmap size: %d x %d [should be: %d x %d]",
                    w, h,
                    INPUT_SIZE, INPUT_SIZE);

            return null;
        }

        int[] mIntValues = new int[w * h];
        byte[] mFlatIntValues = new byte[w * h * 3];
        int[] mOutputs = new int[w * h];

        bitmap.getPixels(mIntValues, 0, w, 0, 0, w, h);
        for (int i = 0; i < mIntValues.length; ++i) {
            final int val = mIntValues[i];
            mFlatIntValues[i * 3 + 0] = (byte)((val >> 16) & 0xFF);
            mFlatIntValues[i * 3 + 1] = (byte)((val >> 8) & 0xFF);
            mFlatIntValues[i * 3 + 2] = (byte)(val & 0xFF);
        }

        final long start = System.currentTimeMillis();
        sTFInterface.feed(INPUT_NAME, mFlatIntValues, 1, h, w, 3 );

        sTFInterface.run(new String[] { OUTPUT_NAME }, true);

        sTFInterface.fetch(OUTPUT_NAME, mOutputs);
        final long end = System.currentTimeMillis();
        Logger.debug("%d millis per core segment call.", (end - start));

//        Logger.debug("outputs = %s", ArrayUtils.intArrayToString(mOutputs));

        Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                output.setPixel(x, y, mOutputs[y * w + x] == 0 ? Color.TRANSPARENT : Color.BLACK);
            }
        }

        return output;
    }
 
Example 16
Source File: EditText.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@SuppressLint("MissingSuperCall")
@Override
public void draw(@NonNull Canvas canvas) {
    boolean r = revealAnimator != null;
    boolean c = !Carbon.isShapeRect(shapeModel, boundsRect);

    if (Carbon.IS_PIE_OR_HIGHER) {
        if (spotShadowColor != null)
            super.setOutlineSpotShadowColor(spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()));
        if (ambientShadowColor != null)
            super.setOutlineAmbientShadowColor(ambientShadowColor.getColorForState(getDrawableState(), ambientShadowColor.getDefaultColor()));
    }

    if (isInEditMode()) {
        if ((r || c) && getWidth() > 0 && getHeight() > 0) {
            Bitmap layer = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas layerCanvas = new Canvas(layer);
            drawInternal(layerCanvas);

            Bitmap mask = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas maskCanvas = new Canvas(mask);
            Paint maskPaint = new Paint(0xffffffff);
            maskCanvas.drawPath(cornersMask, maskPaint);

            for (int x = 0; x < getWidth(); x++) {
                for (int y = 0; y < getHeight(); y++) {
                    int maskPixel = mask.getPixel(x, y);
                    layer.setPixel(x, y, Color.alpha(maskPixel) > 0 ? layer.getPixel(x, y) : 0);
                }
            }
            canvas.drawBitmap(layer, 0, 0, paint);
        } else {
            drawInternal(canvas);
        }
    } else if (getWidth() > 0 && getHeight() > 0 && (((r || c) && !Carbon.IS_LOLLIPOP_OR_HIGHER) || !shapeModel.isRoundRect(boundsRect))) {
        int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        if (r) {
            int saveCount2 = canvas.save();
            canvas.clipRect(revealAnimator.x - revealAnimator.radius, revealAnimator.y - revealAnimator.radius, revealAnimator.x + revealAnimator.radius, revealAnimator.y + revealAnimator.radius);
            drawInternal(canvas);
            canvas.restoreToCount(saveCount2);
        } else {
            drawInternal(canvas);
        }

        paint.setXfermode(Carbon.CLEAR_MODE);
        if (c) {
            cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
            canvas.drawPath(cornersMask, paint);
        }
        if (r)
            canvas.drawPath(revealAnimator.mask, paint);
        paint.setXfermode(null);    // TODO check if this is needed

        canvas.restoreToCount(saveCount);
        paint.setXfermode(null);
    } else {
        drawInternal(canvas);
    }
}
 
Example 17
Source File: BitmapProcessing.java    From Effects-Pro with MIT License 4 votes vote down vote up
public static Bitmap gamma(Bitmap src, double red, double green, double blue) {
	red = (double) (red + 2) / 10.0;
	green = (double) (green + 2) / 10.0;
	blue = (double) (blue + 2) / 10.0;
	// create output image
	Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
	// get image size
	int width = src.getWidth();
	int height = src.getHeight();
	// color information
	int A, R, G, B;
	int pixel;
	// constant value curve
	final int    MAX_SIZE = 256;
	final double MAX_VALUE_DBL = 255.0;
	final int    MAX_VALUE_INT = 255;
	final double REVERSE = 1.0;

	// gamma arrays
	int[] gammaR = new int[MAX_SIZE];
	int[] gammaG = new int[MAX_SIZE];
	int[] gammaB = new int[MAX_SIZE];

	// setting values for every gamma channels
	for(int i = 0; i < MAX_SIZE; ++i) {
		gammaR[i] = (int)Math.min(MAX_VALUE_INT,
				(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / red)) + 0.5));
		gammaG[i] = (int)Math.min(MAX_VALUE_INT,
				(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / green)) + 0.5));
		gammaB[i] = (int)Math.min(MAX_VALUE_INT,
				(int)((MAX_VALUE_DBL * Math.pow(i / MAX_VALUE_DBL, REVERSE / blue)) + 0.5));
	}

	// apply gamma table
	for(int x = 0; x < width; ++x) {
		for(int y = 0; y < height; ++y) {
			// get pixel color
			pixel = src.getPixel(x, y);
			A = Color.alpha(pixel);
			// look up gamma
			R = gammaR[Color.red(pixel)];
			G = gammaG[Color.green(pixel)];
			B = gammaB[Color.blue(pixel)];
			// set new color to output bitmap
			bmOut.setPixel(x, y, Color.argb(A, R, G, B));
		}
	}

	src.recycle();
	src = null;

	// return final image
	return bmOut;
}
 
Example 18
Source File: MotionLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@SuppressLint("MissingSuperCall")
@Override
public void draw(@NonNull Canvas canvas) {
    drawCalled = true;
    boolean r = revealAnimator != null;
    boolean c = !Carbon.isShapeRect(shapeModel, boundsRect);

    if (Carbon.IS_PIE_OR_HIGHER) {
        if (spotShadowColor != null)
            super.setOutlineSpotShadowColor(spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()));
        if (ambientShadowColor != null)
            super.setOutlineAmbientShadowColor(ambientShadowColor.getColorForState(getDrawableState(), ambientShadowColor.getDefaultColor()));
    }

    if (isInEditMode() && (r || c) && getWidth() > 0 && getHeight() > 0) {
        Bitmap layer = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas layerCanvas = new Canvas(layer);
        drawInternal(layerCanvas);

        Bitmap mask = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas maskCanvas = new Canvas(mask);
        Paint maskPaint = new Paint(0xffffffff);
        maskCanvas.drawPath(cornersMask, maskPaint);

        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                int maskPixel = mask.getPixel(x, y);
                layer.setPixel(x, y, Color.alpha(maskPixel) > 0 ? layer.getPixel(x, y) : 0);
            }
        }
        canvas.drawBitmap(layer, 0, 0, paint);
    } else if (getWidth() > 0 && getHeight() > 0 && (((r || c) && !Carbon.IS_LOLLIPOP_OR_HIGHER) || !shapeModel.isRoundRect(boundsRect))) {
        int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        if (r) {
            int saveCount2 = canvas.save();
            canvas.clipRect(revealAnimator.x - revealAnimator.radius, revealAnimator.y - revealAnimator.radius, revealAnimator.x + revealAnimator.radius, revealAnimator.y + revealAnimator.radius);
            drawInternal(canvas);
            canvas.restoreToCount(saveCount2);
        } else {
            drawInternal(canvas);
        }

        paint.setXfermode(Carbon.CLEAR_MODE);
        if (c) {
            cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
            canvas.drawPath(cornersMask, paint);
        }
        if (r)
            canvas.drawPath(revealAnimator.mask, paint);
        paint.setXfermode(null);

        canvas.restoreToCount(saveCount);
        paint.setXfermode(null);
    } else {
        drawInternal(canvas);
    }
}
 
Example 19
Source File: BiomeRenderer.java    From blocktopograph with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Render a single chunk to provided bitmap (bm)
 * @param cm ChunkManager, provides chunks, which provide chunk-data
 * @param bm Bitmap to render to
 * @param dimension Mapped dimension
 * @param chunkX X chunk coordinate (x-block coord / Chunk.WIDTH)
 * @param chunkZ Z chunk coordinate (z-block coord / Chunk.LENGTH)
 * @param bX begin block X coordinate, relative to chunk edge
 * @param bZ begin block Z coordinate, relative to chunk edge
 * @param eX end block X coordinate, relative to chunk edge
 * @param eZ end block Z coordinate, relative to chunk edge
 * @param pX texture X pixel coord to start rendering to
 * @param pY texture Y pixel coord to start rendering to
 * @param pW width (X) of one block in pixels
 * @param pL length (Z) of one block in pixels
 * @return bm is returned back
 *
 * @throws Version.VersionException when the version of the chunk is unsupported.
 */
public Bitmap renderToBitmap(ChunkManager cm, Bitmap bm, Dimension dimension, int chunkX, int chunkZ, int bX, int bZ, int eX, int eZ, int pX, int pY, int pW, int pL) throws Version.VersionException {

    Chunk chunk = cm.getChunk(chunkX, chunkZ);
    Version cVersion = chunk.getVersion();

    if(cVersion == Version.ERROR) return MapType.ERROR.renderer.renderToBitmap(cm, bm, dimension, chunkX, chunkZ, bX, bZ, eX, eZ, pX, pY, pW, pL);
    if(cVersion == Version.NULL) return MapType.CHESS.renderer.renderToBitmap(cm, bm, dimension, chunkX, chunkZ, bX, bZ, eX, eZ, pX, pY, pW, pL);

    //the bottom sub-chunk is sufficient to get biome data.
    TerrainChunkData data = chunk.getTerrain((byte) 0);
    if(data == null || !data.load2DData()) return MapType.CHESS.renderer.renderToBitmap(cm, bm, dimension, chunkX, chunkZ, bX, bZ, eX, eZ, pX, pY, pW, pL);


    int x, z, biomeID, color, i, j, tX, tY;
    Biome biome;

    for (z = bZ, tY = pY ; z < eZ; z++, tY += pL) {
        for (x = bX, tX = pX; x < eX; x++, tX += pW) {


            biomeID = data.getBiome(x, z) & 0xff;
            biome = Biome.getBiome(biomeID);

            color = biome == null ? 0xff000000 : (biome.color.red << 16) | (biome.color.green << 8) | (biome.color.blue) | 0xff000000;

            for(i = 0; i < pL; i++){
                for(j = 0; j < pW; j++){
                    bm.setPixel(tX + j, tY + i, color);
                }
            }


        }
    }

    return bm;
}
 
Example 20
Source File: ImageProcessing.java    From android-motion-detector with Apache License 2.0 3 votes vote down vote up
/**
 * Convert an Luma image into Greyscale.
 * 
 * @param lum
 *            Integer array representing an Luma image.
 * @param width
 *            Width of the image.
 * @param height
 *            Height of the image.
 * @return Bitmap of the Luma image.
 * @throws NullPointerException
 *             if RGB integer array is NULL.
 */
public static Bitmap lumaToGreyscale(int[] lum, int width, int height) {
    if (lum == null) throw new NullPointerException();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    for (int y = 0, xy = 0; y < bitmap.getHeight(); y++) {
        for (int x = 0; x < bitmap.getWidth(); x++, xy++) {
            int luma = lum[xy];
            bitmap.setPixel(x, y, Color.argb(1, luma, luma, luma));
        }
    }
    return bitmap;
}