Example #1
0
func (c *Cut) SplitTo(img image.Image, outfile string) error {
	if img, ok := img.(subImager); ok {
		sub := img.SubImage(c.AsRect())
		file, err := os.Create(outfile)
		if err != nil {
			return err
		}
		defer file.Close()
		return png.Encode(file, sub)
	} else {
		return fmt.Errorf("Cannot take a SubImage")
	}
}
Example #2
0
func Histogram(img image.Image, rect image.Rectangle) ([]int, string) {
	var hist []int
	var pix []uint8
	var colormodel string
	var bounds image.Rectangle
	stride := 0
	n := 0
	switch img := img.(type) {
	case *image.RGBA:
		colormodel = "rgba"
		img = img.SubImage(rect).(*image.RGBA)
		pix = img.Pix
		n = 4
		stride = img.Stride
		bounds = img.Bounds()
	case *image.NRGBA:
		colormodel = "rgba"
		img = img.SubImage(rect).(*image.NRGBA)
		pix = img.Pix
		n = 4
		stride = img.Stride
		bounds = img.Bounds()
	// case *image.RGBA64: -- 16-bit values
	case *image.Gray:
		colormodel = "gray"
		img = img.SubImage(rect).(*image.Gray)
		pix = img.Pix
		n = 1
		stride = img.Stride
		bounds = img.Bounds()
	// case *image.Gray16: -- 16-bit values
	case *image.YCbCr:
		colormodel = "ycbcr"
		hist = HistogramYCbCr(img, rect, false)
		if hist != nil {
			return hist, colormodel
		}
	default:
		colormodel = "other"
	}
	if n > 0 {
		hist = make([]int, n*256)
		for y, o := bounds.Min.Y, 0; y < bounds.Max.Y; y, o = y+1, o+stride {
			for x := 0; x < n*bounds.Dx(); x += n {
				for i := 0; i < n; i++ {
					hist[int(pix[o+x+i])+i*256] += 1
				}
			}
		}
	} else {
		colormodel = "rgb"
		hist = make([]int, 3*256)
		for y := rect.Min.Y; y < rect.Max.Y; y += 1 {
			for x := rect.Min.X; x < rect.Max.X; x += 1 {
				color := img.At(x, y)
				r, g, b, _ := color.RGBA()
				hist[r>>8] += 1
				hist[(g>>8)+256] += 1
				hist[(b>>8)+512] += 1
			}
		}
	}
	return hist, colormodel
}