Example #1
0
// Polygon.
func (avatar *Avatar) polygon(img *image.RGBA, points []image.Point, c color.RGBA) {
	// For each row
	for j := 0; j <= avatar.Y; j++ {

		// Build the list of Xs at which the row crosses a polygon edge
		intersect := make([]int, 0, len(points))
		adj := len(points) - 1
		for i, p := range points {
			q := points[adj]

			if (j > p.Y && j <= q.Y) || (j > q.Y && j <= p.Y) {
				x := int(float64(p.X) + (float64(j)-float64(p.Y))/(float64(q.Y)-float64(p.Y))*(float64(q.X)-float64(p.X)))
				intersect = append(intersect, x)
			}

			adj = i
		}

		// Sort the list f Xs
		sort.Ints(intersect)

		// Fill the pixels between node pairs
		for i := 0; i < len(intersect); i += 2 {
			for k := intersect[i]; k < intersect[i+1]; k++ {
				img.SetRGBA(k, j, c)
			}
		}
	}
}
Example #2
0
func PaintBG(avatar *image.RGBA, bgColor color.RGBA) {
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			avatar.SetRGBA(x, y, bgColor)
		}
	}
}
Example #3
0
// Rectangle.
func rectangle(img *image.RGBA, rect image.Rectangle, c color.RGBA) {
	for i := rect.Min.X; i <= rect.Max.X; i++ {
		for j := rect.Min.Y; j <= rect.Max.Y; j++ {
			img.SetRGBA(i, j, c)
		}
	}
}
Example #4
0
func fillPixels(avatar *image.RGBA, x, y int, pixelColor color.RGBA) {
	for i := x; i < x+PixelSize; i++ {
		for j := y; j < y+PixelSize; j++ {
			avatar.SetRGBA(i, j, pixelColor)
		}
	}
}
Example #5
0
// Gradient.
func gradient(img *image.RGBA, from, to color.RGBA, x, y int, horizontal bool) {
	s := [3]float32{
		(float32(to.R) - float32(from.R)) / float32(x),
		(float32(to.G) - float32(from.G)) / float32(x),
		(float32(to.B) - float32(from.B)) / float32(x),
	}

	for i := 0; i < x; i++ {
		for j := 0; j < y; j++ {
			a, b := i, j
			if horizontal {
				a, b = j, i
			}
			img.SetRGBA(
				a,
				b,
				color.RGBA{
					uint8(float32(from.R) + float32(i)*s[0]),
					uint8(float32(from.G) + float32(i)*s[1]),
					uint8(float32(from.B) + float32(i)*s[2]),
					255,
				})
		}
	}
}
Example #6
0
File: main.go Project: morero/exp
func drawGradient(m *image.RGBA) {
	b := m.Bounds()
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			if x%64 == 0 || y%64 == 0 {
				m.SetRGBA(x, y, color.RGBA{0xff, 0xff, 0xff, 0xff})
			} else if x%64 == 63 || y%64 == 63 {
				m.SetRGBA(x, y, color.RGBA{0x00, 0x00, 0xff, 0xff})
			} else {
				m.SetRGBA(x, y, color.RGBA{uint8(x), uint8(y), 0x00, 0xff})
			}
		}
	}

	// Round off the corners.
	const radius = 64
	lox := b.Min.X + radius - 1
	loy := b.Min.Y + radius - 1
	hix := b.Max.X - radius
	hiy := b.Max.Y - radius
	for y := 0; y < radius; y++ {
		for x := 0; x < radius; x++ {
			if x*x+y*y <= radius*radius {
				continue
			}
			m.SetRGBA(lox-x, loy-y, color.RGBA{})
			m.SetRGBA(hix+x, loy-y, color.RGBA{})
			m.SetRGBA(lox-x, hiy+y, color.RGBA{})
			m.SetRGBA(hix+x, hiy+y, color.RGBA{})
		}
	}
}
Example #7
0
/*
draws pixels to connect a line
*/
func (line Line) connect(img *image.RGBA) {
	points := line.generatePoints()
	black := color.RGBA{0, 0, 0, 255}
	for _, val := range points {
		img.SetRGBA(val.X, val.Y, black)
	}
}
Example #8
0
//Taken from https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
func DrawBasicCircle(myImage *image.RGBA, x_0, y_0, radius float64, colorGradient ColorGradient, isEdge bool) {
	if radius < 0 {
		return
	}
	x := radius
	y := float64(0)
	decisionOver2 := 1 - x
	for y <= x {
		for xM := float64(-1); xM < 2; xM += 2 {
			for yM := float64(-1); yM < 2; yM += 2 {
				if isEdge {
					myImage.SetRGBA(int(x_0+x*xM), int(y_0+y*yM), colorGradient.BorderColor())
					myImage.SetRGBA(int(x_0+y*xM), int(y_0+x*yM), colorGradient.BorderColor())
				} else {
					myImage.SetRGBA(int(x_0+x*xM), int(y_0+y*yM), colorGradient.GetColor(uint16(x_0+x*xM), uint16(y_0+y*yM)))
					myImage.SetRGBA(int(x_0+y*xM), int(y_0+x*yM), colorGradient.GetColor(uint16(x_0+y*xM), uint16(y_0+x*yM)))
				}
			}
		}
		y++
		if decisionOver2 <= 0 {
			decisionOver2 += 2*y + 1
		} else {
			x--
			decisionOver2 += 2*(y-x) + 1
		}
	}
}
Example #9
0
func drawGradient(m *image.RGBA) {
	b := m.Bounds()
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			m.SetRGBA(x, y, color.RGBA{uint8(x), uint8(y), 0x00, 0xff})
		}
	}
}
Example #10
0
// Ellipse.
func ellipse(img *image.RGBA, center, r image.Point, c color.RGBA) {
	for i := center.X - r.X; i <= center.X+r.X; i++ {
		for j := center.Y - r.Y; j <= center.Y+r.Y; j++ {
			if math.Pow((float64(i)-float64(center.X))/float64(r.X), 2)+math.Pow((float64(j)-float64(center.Y))/float64(r.Y), 2) <= 1.1 {
				img.SetRGBA(i, j, c)
			}
		}
	}
}
Example #11
0
// Circle.
func circle(img *image.RGBA, center image.Point, r int, c color.RGBA) {
	for i := center.X - r; i <= center.X+r; i++ {
		for j := center.Y - r; j <= center.Y+r; j++ {
			if math.Pow(float64(i)-float64(center.X), 2)+math.Pow(float64(j)-float64(center.Y), 2) <= math.Pow(float64(r), 2)+float64(r)*0.8 {
				img.SetRGBA(i, j, c)
			}
		}
	}
}
Example #12
0
func drawCross(m *image.RGBA, x, y int) {
	c := color.RGBA{0xff, 0, 0, 0xff} // red
	m.SetRGBA(x+0, y-2, c)
	m.SetRGBA(x+0, y-1, c)
	m.SetRGBA(x-2, y+0, c)
	m.SetRGBA(x-1, y+0, c)
	m.SetRGBA(x+0, y+0, c)
	m.SetRGBA(x+1, y+0, c)
	m.SetRGBA(x+2, y+0, c)
	m.SetRGBA(x+0, y+1, c)
	m.SetRGBA(x+0, y+2, c)
}
Example #13
0
File: win.go Project: s2607/mbrot
func drawGradient(m *image.RGBA) {
	b := m.Bounds()
	var scale = (b.Min.X - b.Max.X) / 2
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			if mandelbrot.Mbi(float64(-x-scale), float64(y+scale), float64(scale)) {
				m.SetRGBA(x, y, color.RGBA{0xff, 0xff, 0xff, 0xff})
			} else {
				m.SetRGBA(x, y, color.RGBA{0x00, 0x00, 0xff, 0xff})
			}
		}
	}
}
Example #14
0
File: pic.go Project: amrhassan/rsc
func line(m *image.RGBA, p, q image.Point, mode int) {
	x := 0
	y := 0
	dx := q.X - p.X
	dy := q.Y - p.Y
	xsign := +1
	ysign := +1
	if dx < 0 {
		xsign = -1
		dx = -dx
	}
	if dy < 0 {
		ysign = -1
		dy = -dy
	}
	pt := func() {
		switch mode {
		case 0:
			for dx := -2; dx <= 2; dx++ {
				for dy := -2; dy <= 2; dy++ {
					if dy*dx <= -4 || dy*dx >= 4 {
						continue
					}
					m.SetRGBA(p.X+x*xsign+dx, p.Y+y*ysign+dy, color.RGBA{255, 192, 192, 255})
				}
			}

		case 1:
			m.SetRGBA(p.X+x*xsign, p.Y+y*ysign, color.RGBA{128, 0, 0, 255})
		}
	}
	if dx > dy {
		for x < dx || y < dy {
			pt()
			x++
			if float64(x)*float64(dy)/float64(dx)-float64(y) > 0.5 {
				y++
			}
		}
	} else {
		for x < dx || y < dy {
			pt()
			y++
			if float64(y)*float64(dx)/float64(dy)-float64(x) > 0.5 {
				x++
			}
		}
	}
	pt()
}
Example #15
0
func PaintItBlack(m *image.RGBA) *image.RGBA {
	pt := m.Rect.Size()
	black := color.RGBA{
		R: 0,
		G: 0,
		B: 0,
		A: 255,
	}
	for i := 0; i < pt.X; i++ {
		for j := 0; j < pt.Y; j++ {
			m.SetRGBA(i, j, black)
		}
	}
	return m
}
Example #16
0
func drawRect(rgba *image.RGBA, bounds image.Rectangle, color color.RGBA) {
	for x := bounds.Min.X; x < bounds.Max.X; x++ {
		rgba.SetRGBA(x, bounds.Min.Y, color)
		rgba.SetRGBA(x, bounds.Max.Y-1, color)
	}
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		rgba.SetRGBA(bounds.Min.X, y, color)
		rgba.SetRGBA(bounds.Max.X-1, y, color)
	}
}
Example #17
0
File: main.go Project: xunatai/ft
func (ft *FT) encodeSpectrumColor(im *image.RGBA) {
	xo, yo := (ft.w-1)/2, (ft.h-1)/2
	if cornered {
		xo, yo = 0, 0
	}
	for x := 0; x < ft.w; x++ {
		for y := 0; y < ft.h; y++ {
			f := func(c complex128) float64 {
				r := cmplx.Abs(c)
				if r > 1.0 {
					return math.Log(r) / math.Log(float64(ft.w*ft.h))
				}
				return 0.0
			}
			im.SetRGBA((x+xo)%ft.w, (y+yo)%ft.h, ft.channels.LoadColor(x, y, f))
		}
	}
}
Example #18
0
File: main.go Project: xunatai/ft
func (ft *FT) encodeSpectrumComponent(i int, im *image.RGBA) {
	xo, yo := (ft.w-1)/2, (ft.h-1)/2
	for x := 0; x < ft.w; x++ {
		if cornered {
			xo, yo = 0, 0
		}
		for y := 0; y < ft.h; y++ {
			f := func(c complex128) (float64, float64, float64) {
				r, theta := cmplx.Polar(c)
				if r > 1.0 {
					r = math.Log(r) / math.Log(float64(ft.w*ft.h))
				} else {
					r = 0.0
				}
				theta = math.Mod(theta/(2.0*math.Pi)+1.0, 1.0)
				return HSLToRGB(theta, 1.0, r)
			}
			im.SetRGBA((x+xo)%ft.w+ft.w*i, (y+yo)%ft.h, ft.channels.LoadComplex(i, x, y, f))
		}
	}
}
Example #19
0
func Splatter(avatar *image.RGBA, nameBytes []byte, pixelColor color.RGBA) {

	// A somewhat random number based on the username.
	var nameSum int64
	for i := range nameBytes {
		nameSum += int64(nameBytes[i])
	}

	// Use said number to keep random-ness deterministic for a given name
	rand.Seed(nameSum)

	// Make the "splatter"
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			if ((x + y) % 2) == 0 {
				if rand.Intn(2) == 1 {
					avatar.SetRGBA(x, y, pixelColor)
				}
			}
		}
	}

	// Mirror left half to right half
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			if x < AvatarSize/2 {
				avatar.Set(AvatarSize-x-1, y, avatar.At(x, y))
			}
		}
	}

	// Mirror top to bottom
	for y := 0; y < AvatarSize; y++ {
		for x := 0; x < AvatarSize; x++ {
			if y < AvatarSize/2 {
				avatar.Set(x, AvatarSize-y-1, avatar.At(x, y))
			}
		}
	}
}
Example #20
0
func drawGradient(m *image.RGBA) {
	b := m.Bounds()
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			if x%64 == 0 || y%64 == 0 {
				m.SetRGBA(x, y, color.RGBA{0xff, 0xff, 0xff, 0xff})
			} else if x%64 == 63 || y%64 == 63 {
				m.SetRGBA(x, y, color.RGBA{0x00, 0x00, 0xff, 0xff})
			} else {
				m.SetRGBA(x, y, color.RGBA{uint8(x), uint8(y), 0x00, 0xff})
			}
		}
	}
}
Example #21
0
func drawVert(m *image.RGBA, x, y, d int, col color.RGBA) {
	for i := 0; i < d; i++ {
		m.SetRGBA(x, y+i, col)
	}
}
Example #22
0
func drawHoriz(m *image.RGBA, x, y, d int, col color.RGBA) {
	for i := 0; i < d; i++ {
		m.SetRGBA(x+i, y, col)
	}
}
Example #23
0
func window() {
	var wg sync.WaitGroup

	size := 500

	wg.Add(1)

	go func() {
		dw, err := wde.NewWindow(size, size)
		if err != nil {
			fmt.Println(err)
			return
		}
		dw.SetTitle("xkcd-zoom")
		dw.SetSize(size, size)
		dw.Show()

		events := dw.EventChan()

		const (
			Draw         = 1
			Redraw       = 2
			ScaleInPlace = 3
		)

		redraw := make(chan int, 1)

		redraw <- Draw

		done := make(chan bool)

		go func() {
			for {
				time.Sleep(time.Second)
				redraw <- Redraw
			}
		}()

		go func() {
		loop:
			for ei := range events {
				redrawType := 0
				runtime.Gosched()
				switch e := ei.(type) {
				case wde.MouseDownEvent:
				case wde.MouseDraggedEvent:
					switch e.Which {
					case wde.LeftButton:
						changedx := e.Where.X - e.From.X
						changedy := e.Where.Y - e.From.Y
						drawx += float64(changedx) * scale
						drawy += float64(-changedy) * scale
						redrawType = Draw
					case wde.RightButton:
						changedy := e.From.Y - e.Where.Y
						mouseScale := float64(changedy) / 100
						scale *= math.Pow(2, mouseScale)
						redrawType = ScaleInPlace
					}
				case wde.MouseUpEvent:
					if e.Which == wde.RightButton {
						scaledTiles = map[float64]map[int]map[int]chan image.Image{}
						redrawType = Draw
					}
				case wde.KeyTypedEvent:
					if e.Key == wde.KeyEscape {
						break loop
					}
				case wde.CloseEvent:
					break loop
				case wde.ResizeEvent:
					redrawType = Draw
				}
				if redrawType != 0 {
					select {
					case redraw <- redrawType:
					default:
					}
				}
			}
			dw.Close()
			done <- true
		}()

		var greyBack, screenBuffer *image.RGBA
		var grey = color.RGBA{155, 155, 155, 255}
		var bufferScale = scale
		var lastRedraw int
		for {
			select {
			case redrawType := <-redraw:
				if lastRedraw == ScaleInPlace && redrawType == Redraw {
					continue
				}
				lastRedraw = redrawType
				s := dw.Screen()
				if redrawType == Draw || redrawType == Redraw {
					width, height := dw.Size()

					tilesh := int(float64(width)/(float64(imageWidth)/scale) + 1)
					tilesv := int(float64(height)/(float64(imageHeight)/scale) + 1)

					tilecx := drawx / imageWidth
					tilecy := drawy / imageHeight

					tileMinX := int(-tilecx) - tilesh - 1
					tileMinY := int(-tilecy) - tilesv - 1
					tileMaxX := int(-tilecx) + tilesh + 1
					tileMaxY := int(-tilecy) + tilesv + 1

					if greyBack == nil || s.Bounds() != greyBack.Bounds() {
						bounds := s.Bounds()
						greyBack = image.NewRGBA(bounds)
						for x := bounds.Min.X; x <= bounds.Max.X; x++ {
							for y := bounds.Min.Y; y <= bounds.Max.Y; y++ {
								greyBack.SetRGBA(x, y, grey)
							}
						}
						screenBuffer = image.NewRGBA(bounds)
					}

					draw.Draw(screenBuffer, screenBuffer.Bounds(), greyBack, image.Point{0, 0}, draw.Src)

					for tilex := tileMinX; tilex <= tileMaxX; tilex++ {
						for tiley := tileMinY; tiley <= tileMaxY; tiley++ {
							scaledTile, ok := getScaledTile(tilex, tiley, scale)
							if !ok || scaledTile == nil {
								continue
							}

							dx, dy := mapToScreen(width, height, float64(tilex)*imageWidth, float64(tiley)*imageHeight)

							drawRect := scaledTile.Bounds()
							drawRect.Min.X += dx
							drawRect.Min.Y -= dy
							drawRect.Max.X += dx
							drawRect.Max.Y -= dy

							draw.Draw(screenBuffer, drawRect, scaledTile, image.Point{0, 0}, draw.Src)
						}
					}

					bufferScale = scale
					if xs, ok := s.(*xgraphics.Image); ok {
						copyToXGraphicsImage(xs, screenBuffer)
					} else {
						draw.Draw(s, s.Bounds(), screenBuffer, image.Point{0, 0}, draw.Src)
					}
				} else if redrawType == ScaleInPlace {
					scaleFactor := scale / bufferScale
					bufBounds := screenBuffer.Bounds()
					width := bufBounds.Max.X - bufBounds.Min.X
					height := bufBounds.Max.Y - bufBounds.Min.Y
					if scaleFactor < 1 {
						widthDiff := width - int(float64(width)*scaleFactor)
						heightDiff := height - int(float64(height)*scaleFactor)
						//bufBounds.Min.X += widthDiff / 2
						bufBounds.Max.X -= widthDiff
						//bufBounds.Min.Y += heightDiff / 2
						bufBounds.Max.Y -= heightDiff
						subBuffer := screenBuffer.SubImage(bufBounds)
						scaledBuffer := resize.Resize(subBuffer, subBuffer.Bounds(), width, height).(*image.RGBA)

						if xs, ok := s.(*xgraphics.Image); ok {
							copyToXGraphicsImage(xs, scaledBuffer)
						} else {
							draw.Draw(s, s.Bounds(), scaledBuffer, image.Point{0, 0}, draw.Src)
						}
					}
					if scaleFactor > 1 {
						scaleFactor = 1 / scaleFactor
						newWidth := int(float64(width) * scaleFactor)
						newHeight := int(float64(height) * scaleFactor)
						scaledBuffer := resize.Resize(screenBuffer, screenBuffer.Bounds(), newWidth, newHeight).(*image.RGBA)

						if xs, ok := s.(*xgraphics.Image); ok {
							copyToXGraphicsImage(xs, scaledBuffer)
						} else {
							draw.Draw(s, s.Bounds(), scaledBuffer, image.Point{0, 0}, draw.Src)
						}
					}
				}
				dw.FlushImage()
			case <-done:
				wg.Done()
				return
			}
		}
	}()

	wg.Wait()
	wde.Stop()
}
Example #24
0
// decode decodes the IDAT data into an image.
func (d *decoder) decode() (image.Image, os.Error) {
	r, err := zlib.NewReader(d)
	if err != nil {
		return nil, err
	}
	defer r.Close()
	bitsPerPixel := 0
	maxPalette := uint8(0)
	var (
		gray     *image.Gray
		rgba     *image.RGBA
		paletted *image.Paletted
		nrgba    *image.NRGBA
		gray16   *image.Gray16
		rgba64   *image.RGBA64
		nrgba64  *image.NRGBA64
		img      image.Image
	)
	switch d.cb {
	case cbG1, cbG2, cbG4, cbG8:
		bitsPerPixel = d.depth
		gray = image.NewGray(image.Rect(0, 0, d.width, d.height))
		img = gray
	case cbGA8:
		bitsPerPixel = 16
		nrgba = image.NewNRGBA(image.Rect(0, 0, d.width, d.height))
		img = nrgba
	case cbTC8:
		bitsPerPixel = 24
		rgba = image.NewRGBA(image.Rect(0, 0, d.width, d.height))
		img = rgba
	case cbP1, cbP2, cbP4, cbP8:
		bitsPerPixel = d.depth
		paletted = image.NewPaletted(image.Rect(0, 0, d.width, d.height), d.palette)
		img = paletted
		maxPalette = uint8(len(d.palette) - 1)
	case cbTCA8:
		bitsPerPixel = 32
		nrgba = image.NewNRGBA(image.Rect(0, 0, d.width, d.height))
		img = nrgba
	case cbG16:
		bitsPerPixel = 16
		gray16 = image.NewGray16(image.Rect(0, 0, d.width, d.height))
		img = gray16
	case cbGA16:
		bitsPerPixel = 32
		nrgba64 = image.NewNRGBA64(image.Rect(0, 0, d.width, d.height))
		img = nrgba64
	case cbTC16:
		bitsPerPixel = 48
		rgba64 = image.NewRGBA64(image.Rect(0, 0, d.width, d.height))
		img = rgba64
	case cbTCA16:
		bitsPerPixel = 64
		nrgba64 = image.NewNRGBA64(image.Rect(0, 0, d.width, d.height))
		img = nrgba64
	}
	bytesPerPixel := (bitsPerPixel + 7) / 8

	// cr and pr are the bytes for the current and previous row.
	// The +1 is for the per-row filter type, which is at cr[0].
	cr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8)
	pr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8)

	for y := 0; y < d.height; y++ {
		// Read the decompressed bytes.
		_, err := io.ReadFull(r, cr)
		if err != nil {
			return nil, err
		}

		// Apply the filter.
		cdat := cr[1:]
		pdat := pr[1:]
		switch cr[0] {
		case ftNone:
			// No-op.
		case ftSub:
			for i := bytesPerPixel; i < len(cdat); i++ {
				cdat[i] += cdat[i-bytesPerPixel]
			}
		case ftUp:
			for i := 0; i < len(cdat); i++ {
				cdat[i] += pdat[i]
			}
		case ftAverage:
			for i := 0; i < bytesPerPixel; i++ {
				cdat[i] += pdat[i] / 2
			}
			for i := bytesPerPixel; i < len(cdat); i++ {
				cdat[i] += uint8((int(cdat[i-bytesPerPixel]) + int(pdat[i])) / 2)
			}
		case ftPaeth:
			for i := 0; i < bytesPerPixel; i++ {
				cdat[i] += paeth(0, pdat[i], 0)
			}
			for i := bytesPerPixel; i < len(cdat); i++ {
				cdat[i] += paeth(cdat[i-bytesPerPixel], pdat[i], pdat[i-bytesPerPixel])
			}
		default:
			return nil, FormatError("bad filter type")
		}

		// Convert from bytes to colors.
		switch d.cb {
		case cbG1:
			for x := 0; x < d.width; x += 8 {
				b := cdat[x/8]
				for x2 := 0; x2 < 8 && x+x2 < d.width; x2++ {
					gray.SetGray(x+x2, y, color.Gray{(b >> 7) * 0xff})
					b <<= 1
				}
			}
		case cbG2:
			for x := 0; x < d.width; x += 4 {
				b := cdat[x/4]
				for x2 := 0; x2 < 4 && x+x2 < d.width; x2++ {
					gray.SetGray(x+x2, y, color.Gray{(b >> 6) * 0x55})
					b <<= 2
				}
			}
		case cbG4:
			for x := 0; x < d.width; x += 2 {
				b := cdat[x/2]
				for x2 := 0; x2 < 2 && x+x2 < d.width; x2++ {
					gray.SetGray(x+x2, y, color.Gray{(b >> 4) * 0x11})
					b <<= 4
				}
			}
		case cbG8:
			for x := 0; x < d.width; x++ {
				gray.SetGray(x, y, color.Gray{cdat[x]})
			}
		case cbGA8:
			for x := 0; x < d.width; x++ {
				ycol := cdat[2*x+0]
				nrgba.SetNRGBA(x, y, color.NRGBA{ycol, ycol, ycol, cdat[2*x+1]})
			}
		case cbTC8:
			for x := 0; x < d.width; x++ {
				rgba.SetRGBA(x, y, color.RGBA{cdat[3*x+0], cdat[3*x+1], cdat[3*x+2], 0xff})
			}
		case cbP1:
			for x := 0; x < d.width; x += 8 {
				b := cdat[x/8]
				for x2 := 0; x2 < 8 && x+x2 < d.width; x2++ {
					idx := b >> 7
					if idx > maxPalette {
						return nil, FormatError("palette index out of range")
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 1
				}
			}
		case cbP2:
			for x := 0; x < d.width; x += 4 {
				b := cdat[x/4]
				for x2 := 0; x2 < 4 && x+x2 < d.width; x2++ {
					idx := b >> 6
					if idx > maxPalette {
						return nil, FormatError("palette index out of range")
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 2
				}
			}
		case cbP4:
			for x := 0; x < d.width; x += 2 {
				b := cdat[x/2]
				for x2 := 0; x2 < 2 && x+x2 < d.width; x2++ {
					idx := b >> 4
					if idx > maxPalette {
						return nil, FormatError("palette index out of range")
					}
					paletted.SetColorIndex(x+x2, y, idx)
					b <<= 4
				}
			}
		case cbP8:
			for x := 0; x < d.width; x++ {
				if cdat[x] > maxPalette {
					return nil, FormatError("palette index out of range")
				}
				paletted.SetColorIndex(x, y, cdat[x])
			}
		case cbTCA8:
			for x := 0; x < d.width; x++ {
				nrgba.SetNRGBA(x, y, color.NRGBA{cdat[4*x+0], cdat[4*x+1], cdat[4*x+2], cdat[4*x+3]})
			}
		case cbG16:
			for x := 0; x < d.width; x++ {
				ycol := uint16(cdat[2*x+0])<<8 | uint16(cdat[2*x+1])
				gray16.SetGray16(x, y, color.Gray16{ycol})
			}
		case cbGA16:
			for x := 0; x < d.width; x++ {
				ycol := uint16(cdat[4*x+0])<<8 | uint16(cdat[4*x+1])
				acol := uint16(cdat[4*x+2])<<8 | uint16(cdat[4*x+3])
				nrgba64.SetNRGBA64(x, y, color.NRGBA64{ycol, ycol, ycol, acol})
			}
		case cbTC16:
			for x := 0; x < d.width; x++ {
				rcol := uint16(cdat[6*x+0])<<8 | uint16(cdat[6*x+1])
				gcol := uint16(cdat[6*x+2])<<8 | uint16(cdat[6*x+3])
				bcol := uint16(cdat[6*x+4])<<8 | uint16(cdat[6*x+5])
				rgba64.SetRGBA64(x, y, color.RGBA64{rcol, gcol, bcol, 0xffff})
			}
		case cbTCA16:
			for x := 0; x < d.width; x++ {
				rcol := uint16(cdat[8*x+0])<<8 | uint16(cdat[8*x+1])
				gcol := uint16(cdat[8*x+2])<<8 | uint16(cdat[8*x+3])
				bcol := uint16(cdat[8*x+4])<<8 | uint16(cdat[8*x+5])
				acol := uint16(cdat[8*x+6])<<8 | uint16(cdat[8*x+7])
				nrgba64.SetNRGBA64(x, y, color.NRGBA64{rcol, gcol, bcol, acol})
			}
		}

		// The current row for y is the previous row for y+1.
		pr, cr = cr, pr
	}

	// Check for EOF, to verify the zlib checksum.
	n, err := r.Read(pr[:1])
	if err != os.EOF {
		return nil, FormatError(err.String())
	}
	if n != 0 || d.idatLength != 0 {
		return nil, FormatError("too much pixel data")
	}

	return img, nil
}
Example #25
-1
func drawRGBA(m *image.RGBA, tp image.Point) {
	draw.Draw(m, m.Bounds(), image.White, image.Point{}, draw.Src)
	for _, p := range crossPoints {
		m.SetRGBA(p.X, p.Y, crossColor)
	}
	d := font.Drawer{
		Dst:  m,
		Src:  image.Black,
		Face: basicfont.Face7x13,
		Dot:  fixed.P(0, 12),
	}
	d.DrawString(fmt.Sprint(tp))
}