// Convolve applies a convolution matrix (kernel) to an image with the supplied options.
//
// Usage example:
//
//		result := Convolve(img, kernel, &Options{Bias: 0, Wrap: false})
//
502575
func Convolve(img image.Image, k Matrix, o *Options) *image.RGBA {
	// Config the convolution
	bias := 0.0
	wrap := false
	keepAlpha := false
	if o != nil {
		wrap = o.Wrap
		bias = o.Bias
		keepAlpha = o.KeepAlpha
	}

	return execute(img, k, bias, wrap, keepAlpha)
}
64798
func (in *ExecEnvVar) DeepCopy() *ExecEnvVar {
	if in == nil {
		return nil
	}
	out := new(ExecEnvVar)
	in.DeepCopyInto(out)
	return out
}
640992
func (_m *APIClient) ImageInspectWithRaw(ctx context.Context, _a1 string) (types.ImageInspect, []byte, error) {
	ret := _m.Called(ctx, _a1)

	var r0 types.ImageInspect
	if rf, ok := ret.Get(0).(func(context.Context, string) types.ImageInspect); ok {
		r0 = rf(ctx, _a1)
	} else {
		r0 = ret.Get(0).(types.ImageInspect)
	}

	var r1 []byte
	if rf, ok := ret.Get(1).(func(context.Context, string) []byte); ok {
		r1 = rf(ctx, _a1)
	} else {
		if ret.Get(1) != nil {
			r1 = ret.Get(1).([]byte)
		}
	}

	var r2 error
	if rf, ok := ret.Get(2).(func(context.Context, string) error); ok {
		r2 = rf(ctx, _a1)
	} else {
		r2 = ret.Error(2)
	}

	return r0, r1, r2
}
1076689
function conv2(image, kernel) {
    var out = new Float64Array( height * width  )
    var acc = 0
    , row, col, i, j, k
    for ( row = 0; row < height; row++ ) {
      for ( col = 0; col < width; col++ ) {
        for ( i = -1; i <= 1; i++ ) {
          for ( j = -1; j <= 1; j++ ) {
            if( row+i >= 0 && col+j >= 0 &&
                row+i < height && col+j < width) {
              k = image[ (row + i) * width + (col + j)]
              acc += k * kernel[ (1 + i) * 3 + (1 + j)]
            }
          }
        }
        out[ row * width + col] = acc
        acc = 0
      }
    }
    return out
  }
649715
func linear(v0, v1, v2, v3 *uint32) {
	*v0 = ((*v0 << 13) | (*v0 >> (32 - 13)))
	*v2 = ((*v2 << 3) | (*v2 >> (32 - 3)))
	*v1 ^= *v0 ^ *v2
	*v3 ^= *v2 ^ (*v0 << 3)
	*v1 = (*v1 << 1) | (*v1 >> (32 - 1))
	*v3 = (*v3 << 7) | (*v3 >> (32 - 7))
	*v0 ^= *v1 ^ *v3
	*v2 ^= *v3 ^ (*v1 << 7)
	*v0 = (*v0 << 5) | (*v0 >> (32 - 5))
	*v2 = (*v2 << 22) | (*v2 >> (32 - 22))
}
94944
func Convert_image_ImageLayer_To_v1_ImageLayer(in *image.ImageLayer, out *v1.ImageLayer, s conversion.Scope) error {
	return autoConvert_image_ImageLayer_To_v1_ImageLayer(in, out, s)
}
458447
func (i *Image) Convert(t ImageType) ([]byte, error) {
	options := Options{Type: t}
	return i.Process(options)
}
1293711
func (k *Kernel) Normalized() Matrix {
	sum := k.Absum()
	w := k.Width
	h := k.Height
	nk := NewKernel(w, h)

	// avoid division by 0
	if sum == 0 {
		sum = 1
	}

	for i := 0; i < w*h; i++ {
		nk.Matrix[i] = k.Matrix[i] / sum
	}

	return nk
}
1196387
function Convolution(kernels, size, count, stride, bias, margin){
	var N, K;

	if(type(bias) === "Tensor"){
		this.bias = bias;
		console.assert(this.bias.shape[0] === 1, "Bias Tensor must have first dimension of length one.");
		K = this.bias.shape[1];
	} else {
		K = bias.length;
		this.bias = new Tensor([1, K], bias);
	}


	// transpose kernels are required
	if(type(kernels) === "Tensor"){
		console.assert(this.kernels.shape[0] === K, "Kernel Tensor first dimension must equal Bias Tensor second dimension.");
		this.kernels = kernels;
	} else {
		N = (kernels.length / K) | 0;
		console.assert(K * N === kernels.length, "Kernel Array must have length equal to a multiple of bias array length.");

		this.kernels = new Tensor([K, N], kernels);
	}

	// kernel patch size
	this.size = size;
	// kernel count
	this.count = count;

	// stride between kernel patches
	this.stride = stride;
	// margin to add
	this.margin = margin;
}
1116804
func ShearH(img image.Image, angle float64) *image.RGBA {
	src := clone.AsRGBA(img)
	srcW, srcH := src.Bounds().Dx(), src.Bounds().Dy()

	// Supersample, currently hard set to 2x
	srcW, srcH = srcW*2, srcH*2
	src = Resize(src, srcW, srcH, NearestNeighbor)

	// Calculate shear factor
	kx := math.Tan(angle * (math.Pi / 180))

	dstW, dstH := srcW+int(float64(srcH)*math.Abs(kx)), srcH
	dst := image.NewRGBA(image.Rect(0, 0, dstW, dstH))

	pivotX := float64(dstW) / 2
	pivotY := float64(dstH) / 2

	// Calculate offset since we are resizing the bounds to
	// fit the sheared image.
	dx := (dstW - srcW) / 2
	dy := (dstH - srcH) / 2

	parallel.Line(dstH, func(start, end int) {
		for y := start; y < end; y++ {
			for x := 0; x < dstW; x++ {
				// Move positions to revolve around pivot
				ix := x - int(pivotX) - dx
				iy := y - int(pivotY) - dy

				// Apply linear transformation
				ix = ix + int(float64(iy)*kx)

				// Move positions back to image coordinates
				ix += int(pivotX)
				iy += int(pivotY)

				if ix < 0 || ix >= srcW || iy < 0 || iy >= srcH {
					continue
				}

				srcPos := iy*src.Stride + ix*4
				dstPos := y*dst.Stride + x*4

				dst.Pix[dstPos+0] = src.Pix[srcPos+0]
				dst.Pix[dstPos+1] = src.Pix[srcPos+1]
				dst.Pix[dstPos+2] = src.Pix[srcPos+2]
				dst.Pix[dstPos+3] = src.Pix[srcPos+3]
			}
		}
	})

	// Downsample to original bounds as part of the Supersampling
	dst = Resize(dst, dstW/2, dstH/2, Linear)

	return dst
}
358249
func Add(bg image.Image, fg image.Image) *image.RGBA {
	dst := blend(bg, fg, func(c0, c1 fcolor.RGBAF64) fcolor.RGBAF64 {
		r := c0.R + c1.R
		g := c0.G + c1.G
		b := c0.B + c1.B

		c2 := fcolor.RGBAF64{R: r, G: g, B: b, A: c1.A}
		return alphaComp(c0, c2)
	})

	return dst
}
1019903
func Blur(dst draw.Image, src image.Image, opt *BlurOptions) error {
	if dst == nil {
		return errors.New("graphics: dst is nil")
	}
	if src == nil {
		return errors.New("graphics: src is nil")
	}

	sd := DefaultStdDev
	size := 0

	if opt != nil {
		sd = opt.StdDev
		size = opt.Size
	}

	if size < 1 {
		size = int(math.Ceil(sd * 6))
	}

	kernel := make([]float64, 2*size+1)
	for i := 0; i <= size; i++ {
		x := float64(i) / sd
		x = math.Pow(1/math.SqrtE, x*x)
		kernel[size-i] = x
		kernel[size+i] = x
	}

	// Normalize the weights to sum to 1.0.
	kSum := 0.0
	for _, k := range kernel {
		kSum += k
	}
	for i, k := range kernel {
		kernel[i] = k / kSum
	}

	return convolve.Convolve(dst, src, &convolve.SeparableKernel{
		X: kernel,
		Y: kernel,
	})
}
660002
func (prc *Processor) Process(nim image.Image, params imageserver.Params) (image.Image, error) {
	out := prc.newDrawable(nim)
	bd := nim.Bounds().Intersect(out.Bounds())
	at := imageutil.NewAtFunc(nim)
	set := imageutil.NewSetFunc(out)
	imageutil.Parallel1D(bd, func(bd image.Rectangle) {
		for y := bd.Min.Y; y < bd.Max.Y; y++ {
			for x := bd.Min.X; x < bd.Max.X; x++ {
				r, g, b, a := at(x, y)
				r, g, b, a = imageutil.RGBAToNRGBA(r, g, b, a)
				r = uint32(prc.vals[uint16(r)])
				g = uint32(prc.vals[uint16(g)])
				b = uint32(prc.vals[uint16(b)])
				r, g, b, a = imageutil.NRGBAToRGBA(r, g, b, a)
				set(x, y, r, g, b, a)
			}
		}
	})
	return out, nil
}
150641
func compute(X, Y mat.Matrix) *mat.Dense {
	var ret mat.Dense
	ret.Mul(X, Y)
	return &ret
}
539438
public static void convolve(Kernel2D_S32 kernel, InterleavedS32 src, InterleavedS32 dst ) {
		InputSanityCheck.checkSameShapeB(src, dst);

		boolean processed = BOverrideConvolveImageNormalized.invokeNativeConvolve(kernel,src,dst);
		
		if( !processed ) {
			if( kernel.width >= src.width || kernel.width >= src.height ) {
				ConvolveNormalizedNaive_IL.convolve(kernel, src, dst);
			} else {
				ConvolveImageNoBorder.convolve(kernel, src, dst, kernel.computeSum());
				ConvolveNormalized_JustBorder_IL.convolve(kernel, src, dst);
			}
		}
	}
2273
func Gamma(src image.Image, gamma float64) *image.RGBA {
	gamma = math.Max(0.00001, gamma)

	lookup := make([]uint8, 256)

	for i := 0; i < 256; i++ {
		lookup[i] = uint8(f64.Clamp(math.Pow(float64(i)/255, 1.0/gamma)*255, 0, 255))
	}

	fn := func(c color.RGBA) color.RGBA {
		return color.RGBA{lookup[c.R], lookup[c.G], lookup[c.B], c.A}
	}

	img := Apply(src, fn)

	return img
}
1323718
func Normalize(v Vect) Vect {
	f := 1.0 / Length(v)
	return Vect{v.X * f, v.Y * f}
}