예제 #1
0
func (im *Image) writeToPng(
	pixelTypes ImagePixelTypes, outputPath string) (err error) {
	image := image.NewNRGBA(image.Rect(0, 0, im.Width, im.Height))
	xStart := maxInt(im.XStart, 0)
	xEnd := minInt(im.XStart+im.XCount, im.Width)
	yStart := maxInt(im.YStart, 0)
	yEnd := minInt(im.YStart+im.YCount, im.Height)
	for y := yStart; y < yEnd; y++ {
		for x := xStart; x < xEnd; x++ {
			k := im.getIndex(x, y)

			var Ls Spectrum
			if (pixelTypes & IM_SENSOR_PIXELS) != 0 {
				sp := &im.sensorPixels[k]
				if sp.n > 0 {
					Ls.ScaleInv(&sp.sum, float32(sp.n))
				}
			}

			var Lp Spectrum
			if im.lightN > 0 &&
				(pixelTypes&IM_LIGHT_PIXELS) != 0 {
				lp := &im.lightPixels[k]
				Lp.ScaleInv(&lp.sum, float32(im.lightN))
			}

			var L Spectrum
			L.Add(&Ls, &Lp)
			r, g, b := L.ToRGB()
			c := color.NRGBA{
				R: scaleRGB(r),
				G: scaleRGB(g),
				B: scaleRGB(b),
				A: 255,
			}
			image.SetNRGBA(x, y, c)
		}
	}
	f, err := os.Create(outputPath)
	if err != nil {
		return
	}
	defer func() {
		if closeErr := f.Close(); err == nil && closeErr != nil {
			err = closeErr
		}
	}()
	if err = png.Encode(f, image); err != nil {
		return
	}
	return
}
예제 #2
0
func RenderFractal() image.Image {
	// Logical bounds are [-2, 2], but we want lots of pixels.
	minX, maxX, minY, maxY := -2, 2, -2, 2
	image := image.NewNRGBA(image.Rect(*factor*minX, *factor*minY,
		*factor*maxX, *factor*maxY))

	// 1 step per pixel.
	xStepSize := float64(maxX-minX) / numSteps
	yStepSize := float64(maxY-minY) / numSteps

	// Walk through pixel space, compute each value.
	for currX := float64(-2); currX < float64(2); currX += xStepSize {
		for currY := float64(-2); currY < float64(2); currY += yStepSize {
			currColor := GetColor(currX, currY)
			image.SetNRGBA(int(float64(*factor)*currX),
				int(float64(*factor)*currY), currColor)
		}
	}

	return image
}