If cropping area is rounded we have to use off-screen buffer to punch a hole
176783
@SuppressWarnings("deprecation")
    private void drawRoundedHole(Canvas canvas) {
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(backColor);

        // Punching hole in background color requires offscreen drawing
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null);
        } else {
            canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, 0);
        }

        canvas.drawPaint(paint);

        final float rx = rounding * 0.5f * areaRect.width();
        final float ry = rounding * 0.5f * areaRect.height();
        canvas.drawRoundRect(areaRect, rx, ry, paintClear);

        canvas.restore();
    }
867981
def crop_to_seg_extents(img, seg, padding):
    """"""

    beg_coords, end_coords = crop_coords(seg, padding)

    img = crop_3dimage(img, beg_coords, end_coords)
    seg = crop_3dimage(seg, beg_coords, end_coords)

    return img, seg
578752
def build_screen_image(edit_buffer)
      working_region = edit_buffer[left_margin..right_margin]

      if (mask = @options[:secret_mask])
        mask[0] * working_region.length
      else
        working_region
      end.ljust(active_width)
    end
670915
T clipInput(T input, T output) {
		double ratioInput = input.width/(double)input.height;
		double ratioOutput = output.width/(double)output.height;

		T a = input;

		if( ratioInput > ratioOutput ) { // clip the width
			int width = input.height*output.width/output.height;
			int x0 = (input.width-width)/2;
			int x1 = x0 + width;

			clipped = input.subimage(x0,0,x1,input.height, clipped);
			a = clipped;
		} else if( ratioInput < ratioOutput ) { // clip the height
			int height = input.width*output.height/output.width;
			int y0 = (input.height-height)/2;
			int y1 = y0 + height;

			clipped = input.subimage(0,y0,input.width,y1, clipped);
			a = clipped;
		}

		return a;
	}
1106562
private function getCropPlacing($optimalWidth, $optimalHeight, $newWidth, $newHeight, $pos = 'm')
		#
		# Author:   Jarrod Oberto
		# Date:   July 11
		# Purpose:  Set the cropping area.
		# Params in:
		# Params out: (array) the crop x and y co-ordinates.
		# Notes:    When specifying the exact pixel crop position (eg 10x15), be
		#       very careful as it's easy to crop out of the image leaving
		#       black borders.
		#
	{
		$pos = fix_strtolower($pos);

		// *** If co-ords have been entered
		if (strstr($pos, 'x'))
		{
			$pos = str_replace(' ', '', $pos);

			$xyArray = explode('x', $pos);
			list($cropStartX, $cropStartY) = $xyArray;

		}
		else
		{

			switch ($pos)
			{
				case 'tl':
					$cropStartX = 0;
					$cropStartY = 0;
					break;

				case 't':
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = 0;
					break;

				case 'tr':
					$cropStartX = $optimalWidth - $newWidth;
					$cropStartY = 0;
					break;

				case 'l':
					$cropStartX = 0;
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;

				case 'm':
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;

				case 'r':
					$cropStartX = $optimalWidth - $newWidth;
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;

				case 'bl':
					$cropStartX = 0;
					$cropStartY = $optimalHeight - $newHeight;
					break;

				case 'b':
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = $optimalHeight - $newHeight;
					break;

				case 'br':
					$cropStartX = $optimalWidth - $newWidth;
					$cropStartY = $optimalHeight - $newHeight;
					break;

				case 'auto':
					// *** If image is a portrait crop from top, not center. v1.5
					if ($optimalHeight > $optimalWidth)
					{
						$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
						$cropStartY = ($this->cropFromTopPercent / 100) * $optimalHeight;
					}
					else
					{

						// *** Else crop from the center
						$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
						$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					}
					break;

				default:
					// *** Default to center
					$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
					$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
					break;
			}
		}

		return array( 'x' => $cropStartX, 'y' => $cropStartY );
	}
84680
def rounded_rectangle_region(width, height, radius):
  
  bmp = wx.Bitmap.FromRGBA(width, height) # Mask color is #000000
  dc = wx.MemoryDC(bmp)
  dc.Brush = wx.Brush((255,) * 3) # Any non-black would do
  dc.DrawRoundedRectangle(0, 0, width, height, radius)
  dc.SelectObject(wx.NullBitmap)
  bmp.SetMaskColour((0,) * 3)
  return wx.Region(bmp)
1170353
private void calculateCropping(CmsPositionBean position) {

        m_croppingParam.setCropHeight((int)Math.round(m_heightRatio * position.getHeight()));
        m_croppingParam.setCropWidth((int)Math.round(m_widthRatio * position.getWidth()));
        m_croppingParam.setCropY((int)Math.round(m_heightRatio * position.getTop()));
        m_croppingParam.setCropX((int)Math.round(m_widthRatio * position.getLeft()));
    }
1072158
def crop_to_extents(img1, img2, padding):
    """"""

    beg_coords1, end_coords1 = crop_coords(img1, padding)
    beg_coords2, end_coords2 = crop_coords(img2, padding)

    beg_coords = np.fmin(beg_coords1, beg_coords2)
    end_coords = np.fmax(end_coords1, end_coords2)

    img1 = crop_3dimage(img1, beg_coords, end_coords)
    img2 = crop_3dimage(img2, beg_coords, end_coords)

    return img1, img2
378328
def crop_square(im, r, c, sz):
    '''
    
    '''
    return im[r:r+sz, c:c+sz]
988920
function() {
            if (this._image instanceof IMSoftcrop.Image == false || !this._image.ready) {
                return;
            }

            // Always make sure all crops are added
            for(var n = 0; n < this._crops.length; n++) {
                var crop = this._image.getSoftcrop(this._crops[n].id);

                if (crop == null) {
                    var crop = this._image.addSoftcrop(
                        this._crops[n].id,
                        this._crops[n].setAsCurrent,
                        this._crops[n].hRatio,
                        this._crops[n].vRatio,
                        this._crops[n].x,
                        this._crops[n].y,
                        this._crops[n].exact,
                        this._crops[n].usable
                    );

                    if (this._autocrop) {
                        this._image.autocrop();
                    }

                    this._renderNewCropPreview(crop, this._crops[n].setAsCurrent);
                }


                if (this._crops[n].setAsCurrent) {
                    this.setActiveCrop(crop);
                }
            }
        }
927437
private CmsRectangle getNativeCropRegion() {

        CmsCroppingParamBean crop = m_croppingProvider.get();
        CmsImageInfoBean info = m_imageInfoProvider.get();
        if ((crop == null) || !crop.isCropped()) {
            return CmsRectangle.fromLeftTopWidthHeight(0, 0, info.getWidth(), info.getHeight());
        } else {
            return CmsRectangle.fromLeftTopWidthHeight(
                crop.getCropX(),
                crop.getCropY(),
                crop.getCropWidth(),
                crop.getCropHeight());
        }
    }
321753
def crop(self, cropping):
        
        srcRect = self._add_srcRect()
        srcRect.l, srcRect.t, srcRect.r, srcRect.b = cropping
761072
function (clip) {
    var width = this.getUnscaledWidth();
    var height = this.getUnscaledHeight();
    this.draw(clip);
    this.lastClip = clip;
    this.invalid = false;
  }
751731
function cut(buffer, size) {
  var chunks = [];
  var cursor = 0;
  do {
    var chunkSize = Math.min(size, buffer.length - cursor);
    chunks.push(buffer.slice(cursor, cursor + chunkSize));
    cursor += chunkSize;
  } while(cursor < buffer.length);
  return chunks;
}
1223910
def square_crop(gravity = 'Center')
      gravity(gravity) unless gravity.nil?
      d = [width, height].min
      crop("#{d}x#{d}+0+0!")
    end
2453
private void fitImageToView() {
        Drawable drawable = getDrawable();
        if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
            return;
        }
        if (matrix == null || prevMatrix == null) {
            return;
        }

        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();

        //
        // Scale image for view
        //
        float scaleX = (float) viewWidth / drawableWidth;
        float scaleY = (float) viewHeight / drawableHeight;

        switch (mScaleType) {
            case CENTER:
                scaleX = scaleY = 1;
                break;

            case CENTER_CROP:
                scaleX = scaleY = Math.max(scaleX, scaleY);
                break;

            case CENTER_INSIDE:
                scaleX = scaleY = Math.min(1, Math.min(scaleX, scaleY));

            case FIT_CENTER:
                scaleX = scaleY = Math.min(scaleX, scaleY);
                break;

            case FIT_XY:
                break;

            default:
                //
                // FIT_START and FIT_END not supported
                //
                throw new UnsupportedOperationException("TouchImageView does not support FIT_START or FIT_END");

        }

        //
        // Center the image
        //
        float redundantXSpace = viewWidth - (scaleX * drawableWidth);
        float redundantYSpace = viewHeight - (scaleY * drawableHeight);
        matchViewWidth = viewWidth - redundantXSpace;
        matchViewHeight = viewHeight - redundantYSpace;
        if (!isZoomed() && !imageRenderedAtLeastOnce) {
            //
            // Stretch and center image to fit view
            //
            matrix.setScale(scaleX, scaleY);
            matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2);
            normalizedScale = 1;

        } else {
            //
            // These values should never be 0 or we will set viewWidth and viewHeight
            // to NaN in translateMatrixAfterRotate. To avoid this, call savePreviousImageValues
            // to set them equal to the current values.
            //
            if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) {
                savePreviousImageValues();
            }

            prevMatrix.getValues(m);

            //
            // Rescale Matrix after rotation
            //
            m[Matrix.MSCALE_X] = matchViewWidth / drawableWidth * normalizedScale;
            m[Matrix.MSCALE_Y] = matchViewHeight / drawableHeight * normalizedScale;

            //
            // TransX and TransY from previous matrix
            //
            float transX = m[Matrix.MTRANS_X];
            float transY = m[Matrix.MTRANS_Y];

            //
            // Width
            //
            float prevActualWidth = prevMatchViewWidth * normalizedScale;
            float actualWidth = getImageWidth();
            translateMatrixAfterRotate(Matrix.MTRANS_X, transX, prevActualWidth, actualWidth, prevViewWidth, viewWidth, drawableWidth);

            //
            // Height
            //
            float prevActualHeight = prevMatchViewHeight * normalizedScale;
            float actualHeight = getImageHeight();
            translateMatrixAfterRotate(Matrix.MTRANS_Y, transY, prevActualHeight, actualHeight, prevViewHeight, viewHeight, drawableHeight);

            //
            // Set the matrix to the adjusted scale and translate values.
            //
            matrix.setValues(m);
        }
        fixTrans();
        setImageMatrix(matrix);
    }
407780
def _adjust_crop_box(box, crop):
        

        if crop and box:
            # Both boxes are the same size; just line them up.
            return (box[0] + crop[0], box[1] + crop[1],
                    box[2] + crop[0], box[3] + crop[1])

        if crop:
            # We don't have a fit box, so just convert the crop box
            return (crop[0], crop[1], crop[0] + crop[2], crop[1] + crop[3])

        # We don't have a crop box, so return the fit box (even if it's None)
        return box