Example #1
0
func (b *Board) Draw(m *image.RGBA) {
	r := b.image.Bounds()
	draw.Draw(m, r, b.image, ZP, draw.Src)
	// Vertical lines.
	x := b.xInset + b.squareWidth/2
	y := b.yInset + b.squareHeight/2
	wid := b.lineWidth
	for i := 0; i < b.dim; i++ {
		r := image.Rect(x, y, x+wid, y+(b.dim-1)*b.squareHeight)
		draw.Draw(m, r, image.Black, ZP, draw.Src)
		x += b.squareWidth
	}
	// Horizontal lines.
	x = b.xInset + b.squareWidth/2
	for i := 0; i < b.dim; i++ {
		r := image.Rect(x, y, x+(b.dim-1)*b.squareWidth+wid, y+wid)
		draw.Draw(m, r, image.Black, ZP, draw.Src)
		y += b.squareHeight
	}
	// Points.
	spot := 4
	if b.dim < 13 {
		spot = 3
	}
	points := []IJ{
		{spot, spot},
		{spot, (b.dim + 1) / 2},
		{spot, b.dim + 1 - spot},
		{(b.dim + 1) / 2, spot},
		{(b.dim + 1) / 2, (b.dim + 1) / 2},
		{(b.dim + 1) / 2, b.dim + 1 - spot},
		{b.dim + 1 - spot, spot},
		{b.dim + 1 - spot, (b.dim + 1) / 2},
		{b.dim + 1 - spot, b.dim + 1 - spot},
	}
	for _, ij := range points {
		b.drawPoint(m, ij)
	}
	// Pieces.
	for i := 1; i <= b.dim; i++ {
		for j := 1; j <= b.dim; j++ {
			ij := IJ{i, j}
			if p := b.piece(ij); p != nil {
				b.drawPiece(m, ij, p)
			}
		}
	}
}
Example #2
0
func (b *Board) drawPoint(m *image.RGBA, ij IJ) {
	pt := ij.XYCenter(&b.Dims)
	wid := b.lineWidth
	sz := wid * 3 / 2
	r := image.Rect(pt.x-sz, pt.y-sz, pt.x+wid+sz, pt.y+wid+sz)
	draw.Draw(m, r, image.Black, ZP, draw.Src)
}
Example #3
0
func main() {
	flag.Parse()

	dir := flag.Arg(0)
	if dir == "" {
		fmt.Fprintf(os.Stderr, "Images not specified.")
		flag.Usage()
		os.Exit(1)
	}

	files, err := ioutil.ReadDir(dir)
	check(err)

	w, h := *width, *height
	collage := image.NewRGBA(image.Rect(0, 0, w*2, h*len(files)))
	draw.Draw(collage, collage.Bounds(), image.NewUniform(color.RGBA{0xFF, 0xFF, 0xFF, 0xFF}), image.Point{}, draw.Src)

	for i, file := range files {
		fmt.Println("Processing ", file.Name())
		m, err := ycbcr.FromFile(filepath.Join(dir, file.Name()))
		check(err)

		drawcell(collage, m, 0, i, w, h)
		process(m)
		drawcell(collage, m, 1, i, w, h)
	}

	check(ycbcr.ToFile(*output, collage))
}
Example #4
0
func resize(s string) {
	in, err := os.Open(s)
	if err != nil {
		log.Fatal(err)
	}
	defer in.Close()
	src, _, err := image.Decode(in)
	if err != nil {
		log.Fatal(err)
	}
	name := "../" + s
	out, err := os.Create(name)
	fmt.Println(name)
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()
	dst := image.NewGray(image.Rect(0, 0, 256, 256))
	draw.Draw(dst, dst.Bounds(), image.White, image.ZP, draw.Src)
	dr := image.Rect(*n, *n, 256-*n, 256-*n)
	draw.ApproxBiLinear.Scale(dst, dr, src, src.Bounds(), draw.Src, nil)
	err = jpeg.Encode(out, dst, nil)
	if err != nil {
		log.Fatal(err)
	}
}
Example #5
0
func tToRGBA64(m image.Image) *image.RGBA64 {
	if p, ok := m.(*image.RGBA64); ok {
		return p
	}
	p := image.NewRGBA64(m.Bounds())
	xdraw.Draw(p, p.Bounds(), m, image.Pt(0, 0), xdraw.Src)
	return p
}
Example #6
0
func tToGray16(m image.Image) *image.Gray16 {
	if p, ok := m.(*image.Gray16); ok {
		return p
	}
	p := image.NewGray16(m.Bounds())
	xdraw.Draw(p, p.Bounds(), m, image.Pt(0, 0), xdraw.Src)
	return p
}
Example #7
0
func addBounds(img image.Image) image.Image {
	imgWidth := img.Bounds().Dx() + 16
	imgHeight := img.Bounds().Dy() + 16
	newImg := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight))
	draw.Draw(newImg, newImg.Bounds(), &image.Uniform{color.RGBA{255, 255, 255, 255}}, image.ZP, draw.Src)
	draw.Copy(newImg, image.Pt(8, 8), img, img.Bounds(), draw.Src, nil)
	return newImg

}
Example #8
0
func makeStone(name string, circleMask *image.Alpha) (*image.RGBA, *image.Alpha) {
	stone := get(name, stoneSize0)
	dst := image.NewRGBA(stone.Bounds())
	// Make the whole area black, for the shadow.
	draw.Draw(dst, dst.Bounds(), image.Black, ZP, draw.Src)
	// Lay in the stone within the circle so it shows up inside the shadow.
	draw.DrawMask(dst, dst.Bounds(), stone, ZP, circleMask, ZP, draw.Over)
	return dst, makeShadowMask(stone)
}
Example #9
0
// Clip は、画像の一部部分を矩形で切り抜く。
// 切り抜く領域は、幅x高さ+X座標+Y座標で指定し、
// (X座標, Y座標) - (X座標+幅, Y座標+高さ)の領域が切り抜かれる。
// 幅と高さは、Resizeで指定できるものと同じである。
// XY座標にも"px"や"%"の単位が使える。
// "%"を指定すると、元の画像の幅や高さを基準とする。
// XY座標は省略でき、省略すると0となる。
func (img *Image) Clip(s string) error {
	r, err := img.parseBounds(s)
	if err != nil {
		return err
	}

	dst := newDrawImage(r, img.ColorModel())
	draw.Draw(dst, dst.Bounds(), img, r.Min, draw.Src)

	img.Image = dst

	return nil
}
Example #10
0
func (i *image) cutImg(bit img.Image, rect img.Rectangle) img.Image {
	r_draw := i.imageToDraw(bit.Bounds(), rect)
	start := i.startRect(r_draw, rect).toRect()
	mx := bit.Bounds().Dx()
	my := bit.Bounds().Dy()

	if r_draw.Dx() != 0 && r_draw.Dy() != 0 {
		if (r_draw.Min.X < 0 && r_draw.Min.X+Size < 0 && r_draw.Min.Y < 0 && r_draw.Min.Y+Size < 0) ||
			(r_draw.Min.X > mx && r_draw.Min.Y > my && r_draw.Max.X > mx && r_draw.Max.Y > my) {
			return nil
		}
		ret := img.NewRGBA(img.Rect(0, 0, Size, Size))
		draw.Draw(ret, start, bit, img.Pt(r_draw.Min.X, r_draw.Min.Y), draw.Src)
		return ret
	}
	return nil
}
Example #11
0
func NewBoard(dim, percent int) *Board {
	switch dim {
	case 9, 13, 19, 21:
	default:
		return nil
	}
	boardTexture := get("goboard.jpg", 0)
	b := new(Board)
	b.Dims.Init(dim, 100)
	b.pieces = make([]*Piece, maxBoard*maxBoard)
	b.image = image.NewRGBA(boardTexture.Bounds())
	draw.Draw(b.image, b.image.Bounds(), boardTexture, ZP, draw.Src)
	dir, err := os.Open("asset")
	if err != nil {
		log.Fatal(err)
	}
	defer dir.Close()
	names, err := dir.Readdirnames(0)
	if err != nil {
		log.Fatal(err)
	}
	circleMask := makeCircle()
	// Blackstones go first
	for _, name := range names {
		if strings.HasPrefix(name, "blackstone") {
			s, m := makeStone(name, circleMask)
			b.stone = append(b.stone, Stone{s, m, nil, nil})
			b.numBlackStones++
		}
	}
	for _, name := range names {
		if strings.HasPrefix(name, "whitestone") {
			s, m := makeStone(name, circleMask)
			b.stone = append(b.stone, Stone{s, m, nil, nil})
			b.numWhiteStones++
		}
	}
	b.Resize(percent) // TODO
	return b
}
Example #12
0
// textBox renders t into a tight fitting image
func (ig *ImageGraphics) textBox(t string, font chart.Font, textCol color.Color) image.Image {
	// Initialize the context.
	bg := image.NewUniform(color.Alpha{0})
	fg := image.NewUniform(textCol)
	width := ig.TextLen(t, font)
	size := ig.relFontsizeToPixel(font.Size)

	c := freetype.NewContext()
	c.SetDPI(dpi)
	c.SetFont(ig.font)
	c.SetFontSize(size)
	bb := ig.font.Bounds(c.PointToFixed(float64(size)))
	bbDelta := bb.Max.Sub(bb.Min)

	height := int(bbDelta.Y+32) >> 6
	canvas := image.NewRGBA(image.Rect(0, 0, width, height))
	draw.Draw(canvas, canvas.Bounds(), bg, image.ZP, draw.Src)
	c.SetDst(canvas)
	c.SetSrc(fg)
	c.SetClip(canvas.Bounds())
	// Draw the text.
	extent, err := c.DrawString(t, fixed.Point26_6{X: 0, Y: bb.Max.Y})
	if err != nil {
		log.Println(err)
		return nil
	}

	// Ugly heuristic hack: font bounds are pretty high resulting in white top border: Trim.
	topskip := 1
	if size > 15 {
		topskip = 2
	} else if size > 20 {
		topskip = 3
	}
	return canvas.SubImage(image.Rect(0, topskip, int(extent.X)>>6, height))
}
Example #13
0
// ClearRect fills the current canvas with a default transparent color at the specified rectangle
func (gc *GraphicContext) ClearRect(x1, y1, x2, y2 int) {
	imageColor := image.NewUniform(gc.Current.FillColor)
	draw.Draw(gc.img, image.Rect(x1, y1, x2, y2), imageColor, image.ZP, draw.Over)
}
Example #14
0
func main() {
	input, err := os.Open(inputFile)
	if os.IsNotExist(err) {
		fmt.Printf("Unable to open %s\n", inputFile)
		os.Exit(1)
	} else if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer input.Close()

	inputImage, _, err := image.Decode(input)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	paletteBuilder := vibrant.NewPaletteBuilder(inputImage).
		MaximumColorCount(uint32(maximumColorCount)).
		ResizeImageArea(resizeImageArea)

	validScaler := false
	for name, scaler := range scalerByName {
		if name == scalerName {
			validScaler = true
			paletteBuilder = paletteBuilder.Scaler(scaler)
			break
		}
	}
	if !validScaler {
		fmt.Printf("%s is not a valid scaler\n", scalerName)
		os.Exit(1)
	}

	start := time.Now()
	palette := paletteBuilder.Generate()
	elapsed := time.Since(start)
	if debug {
		fmt.Printf("Palette generation took approximately %s\n\n", elapsed)
	}

	swatches := palette.Swatches()
	sort.Sort(populationSwatchSorter(swatches))
	colorPalette := make(color.Palette, 0, len(swatches))
	for i, swatch := range swatches {
		colorPalette = append(colorPalette, swatch.Color())
		if debug {
			fmt.Printf("Swatch: %s (%d)\n", swatch.RGBAInt(), swatch.Population())
			fmt.Printf("  %v\n", swatch.HSL())
			if i == len(swatches)-1 {
				fmt.Println("")
			}
		}
	}
	fmt.Printf("%s: %06s\n", formatTarget("Vibrant", vibrant.Vibrant), formatPackgedRGB(palette.VibrantColor(0)))
	if palette.VibrantSwatch() != nil {
		fmt.Printf("  %v\n", palette.VibrantSwatch().HSL())
	}
	fmt.Printf("%s: %06s\n", formatTarget("Light Vibrant", vibrant.LightVibrant), formatPackgedRGB(palette.LightVibrantColor(0)))
	if palette.LightVibrantSwatch() != nil {
		fmt.Printf("  %v\n", palette.LightVibrantSwatch().HSL())
	}
	fmt.Printf("%s: %06s\n", formatTarget("Dark Vibrant", vibrant.DarkVibrant), formatPackgedRGB(palette.DarkVibrantColor(0)))
	if palette.DarkVibrantSwatch() != nil {
		fmt.Printf("  %v\n", palette.DarkVibrantSwatch().HSL())
	}
	fmt.Printf("%s: %06s\n", formatTarget("Muted", vibrant.Muted), formatPackgedRGB(palette.MutedColor(0)))
	if palette.MutedSwatch() != nil {
		fmt.Printf("  %v\n", palette.MutedSwatch().HSL())
	}
	fmt.Printf("%s: %06s\n", formatTarget("Light Muted", vibrant.LightMuted), formatPackgedRGB(palette.LightMutedColor(0)))
	if palette.LightMutedSwatch() != nil {
		fmt.Printf("  %v\n", palette.LightMutedSwatch().HSL())
	}
	fmt.Printf("%s: %06s\n", formatTarget("Dark Muted", vibrant.DarkMuted), formatPackgedRGB(palette.DarkMutedColor(0)))
	if palette.DarkMutedSwatch() != nil {
		fmt.Printf("  %v\n", palette.DarkMutedSwatch().HSL())
	}

	if len(outputFile) > 0 {
		if resizeImageArea > 0 {
			inputImage = vibrant.ScaleImageDown(inputImage, resizeImageArea, scalerByName[scalerName])
		}

		outputImageRectangle := inputImage.Bounds()
		var outputImage draw.Image = image.NewPaletted(outputImageRectangle, colorPalette)

		draw.Draw(outputImage, outputImage.Bounds(), inputImage, image.ZP, draw.Src)

		if debug {
			maxPoint := outputImageRectangle.Max
			var palettedStartPoint image.Point

			if maxPoint.X > maxPoint.Y {
				// Landscape, display images stacked.
				palettedStartPoint = image.Pt(0, maxPoint.Y+1)
				maxPoint.Y *= 2
				maxPoint.Y++
			} else {
				// Square or Portrait, display images side by side.
				palettedStartPoint = image.Pt(maxPoint.X+1, 0)
				maxPoint.X *= 2
				maxPoint.X++
			}

			swatchesPerRow := int(maximumColorCount)
			swatchRows := 1
			targetSwatchDimension := int(math.Max(math.Ceil(float64(maxPoint.X)*0.04), 8))
			actualSwatchDimension := maxPoint.X / swatchesPerRow
			for actualSwatchDimension < targetSwatchDimension {
				if swatchesPerRow%2 == 0 {
					swatchesPerRow = swatchesPerRow / 2
				} else {
					swatchesPerRow = (swatchesPerRow + 1) / 2
				}
				actualSwatchDimension = maxPoint.X / swatchesPerRow
				swatchRows = swatchRows << 1
			}

			swatchStartY := maxPoint.Y + 1
			maxPoint.Y = maxPoint.Y + 2 + (actualSwatchDimension * (swatchRows + len(palette.Targets())))

			rgbaImage := image.NewRGBA(image.Rectangle{image.ZP, maxPoint})
			// Add background...
			draw.Draw(rgbaImage, rgbaImage.Bounds(), image.NewUniform(image.Black), image.ZP, draw.Src)
			// Add original...
			draw.Draw(rgbaImage, inputImage.Bounds(), inputImage, image.ZP, draw.Src)
			// Add paletted...
			draw.Draw(rgbaImage, outputImage.Bounds().Add(palettedStartPoint), outputImage, image.ZP, draw.Src)
			// Add swatches...
			swatchRectangle := image.Rect(0, 0, actualSwatchDimension, actualSwatchDimension)
			sort.Sort(hueSwatchSorter(swatches))
			for i, swatch := range swatches {
				swatchX := (i % swatchesPerRow) * actualSwatchDimension
				swatchY := (i / swatchesPerRow) * actualSwatchDimension
				draw.Draw(rgbaImage, swatchRectangle.Add(image.Pt(swatchX, swatchStartY+swatchY)), image.NewUniform(swatch.Color()), image.ZP, draw.Src)
			}
			// Add targets...
			targetStartY := maxPoint.Y - (actualSwatchDimension * len(palette.Targets()))
			targetRectangle := image.Rect(0, 0, maxPoint.X, actualSwatchDimension)
			for i, target := range []*vibrant.Target{vibrant.Vibrant, vibrant.LightVibrant, vibrant.DarkVibrant, vibrant.Muted, vibrant.LightMuted, vibrant.DarkMuted} {
				swatch := palette.SwatchForTarget(target)
				var targetColor color.Color
				if swatch == nil {
					targetColor = color.Black
				} else {
					targetColor = swatch.Color()
				}
				draw.Draw(rgbaImage, targetRectangle.Add(image.Pt(0, targetStartY+(i*actualSwatchDimension))), image.NewUniform(targetColor), image.ZP, draw.Src)
			}

			outputImage = rgbaImage
		}

		output, err := os.Create(outputFile)
		if os.IsExist(err) {
			fmt.Printf("%s already exists\n", inputFile)
			os.Exit(1)
		} else if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		defer output.Close()

		if strings.HasSuffix(strings.ToLower(outputFile), ".png") {
			err = png.Encode(output, outputImage)
		} else if strings.HasSuffix(strings.ToLower(outputFile), ".jpg") {
			err = jpeg.Encode(output, outputImage, &jpeg.Options{90})
		} else {
			err = fmt.Errorf("Unable to find encoder for output file: %s", outputFile)
		}
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	}
}