Example #1
1
func main() {
	var outFile *os.File
	var err error

	if outFile, err = os.Create("create.png"); err != nil {
		println("Error", err)
		return
	}

	defer outFile.Close()

	rect := image.Rect(0, 0, 100, 100)
	rgba := image.NewRGBA64(rect)

	// #golangとか書きたいけど、とりあえず#だけ
	for i := 0; i < 10; i++ {
		rgba.Set(60, (10 + i), image.Black.At(0, 0))
		rgba.Set(65, (10 + i), image.Black.At(0, 0))
		rgba.Set((58 + i), 13, image.Black.At(0, 0))
		rgba.Set((58 + i), 16, image.Black.At(0, 0))
	}

	outImage := rgba.SubImage(rect)

	if err = png.Encode(outFile, outImage); err != nil {
		println("Error", err)
		return
	}

}
Example #2
0
// Resize an image to new width and height using the interpolation function interp.
// A new image with the given dimensions will be returned.
// If one of the parameters width or height is set to 0, its size will be calculated so that
// the aspect ratio is that of the originating image.
// The resizing algorithm uses channels for parallel computation.
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
	oldBounds := img.Bounds()
	oldWidth := float32(oldBounds.Dx())
	oldHeight := float32(oldBounds.Dy())
	scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)

	tempImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
	b := tempImg.Bounds()
	adjust := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))

	n := numJobs(b.Dy())
	c := make(chan int, n)
	for i := 0; i < n; i++ {
		slice := image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n)
		go resizeSlice(img, tempImg, interp, scaleX, adjust, float32(oldBounds.Min.X), slice, c)
	}
	for i := 0; i < n; i++ {
		<-c
	}

	resultImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
	b = resultImg.Bounds()
	adjust = 0.5 * ((oldHeight-1.0)/scaleY - float32(b.Dy()-1))

	for i := 0; i < n; i++ {
		slice := image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n)
		go resizeSlice(tempImg, resultImg, interp, scaleY, adjust, float32(oldBounds.Min.Y), slice, c)
	}
	for i := 0; i < n; i++ {
		<-c
	}

	return resultImg
}
Example #3
0
func NewImageOfTypeRect(src image.Image, bounds image.Rectangle) image.Image {
	switch i := src.(type) {
	case *image.Alpha:
		return image.NewAlpha(bounds)
	case *image.Alpha16:
		return image.NewAlpha16(bounds)
	case *image.Gray:
		return image.NewGray(bounds)
	case *image.Gray16:
		return image.NewGray16(bounds)
	case *image.NRGBA:
		return image.NewNRGBA(bounds)
	case *image.NRGBA64:
		return image.NewNRGBA64(bounds)
	case *image.Paletted:
		return image.NewPaletted(bounds, i.Palette)
	case *image.RGBA:
		return image.NewRGBA(bounds)
	case *image.RGBA64:
		return image.NewRGBA64(bounds)
	case *image.YCbCr:
		return image.NewYCbCr(bounds, i.SubsampleRatio)
	}
	panic("Unknown image type")
}
Example #4
0
func orient(r io.Reader, i image.Image) (image.Image, error) {
	t := log.Start()
	defer log.End(t)

	e, err := exif.Decode(r)
	if err != nil {
		log.Printf("fail to decode exif: %v", err)
		return nil, err
	}
	tag, err := e.Get(exif.Orientation)
	// Orientationタグが存在しない場合、処理を完了する
	if err != nil {
		log.Println("oritentation tag doesn't exist")
		return nil, err
	}
	o, err := tag.Int(0)
	if err != nil {
		log.Println("oritentation tag is't int")
		return nil, err
	}

	rect := i.Bounds()
	// orientation=5~8 なら画像サイズの縦横を入れ替える
	if o >= 5 && o <= 8 {
		rect = RotateRect(rect)
	}
	d := image.NewRGBA64(rect)
	a := affines[o]
	a.TransformCenter(d, i, interp.Bilinear)

	return d, nil
}
Example #5
0
// NewDrawableSize returns a new draw.Image with the same type as p and the given bounds.
// If p is not a draw.Image, another type is used.
func NewDrawableSize(p image.Image, r image.Rectangle) draw.Image {
	switch p := p.(type) {
	case *image.RGBA:
		return image.NewRGBA(r)
	case *image.RGBA64:
		return image.NewRGBA64(r)
	case *image.NRGBA:
		return image.NewNRGBA(r)
	case *image.NRGBA64:
		return image.NewNRGBA64(r)
	case *image.Alpha:
		return image.NewAlpha(r)
	case *image.Alpha16:
		return image.NewAlpha16(r)
	case *image.Gray:
		return image.NewGray(r)
	case *image.Gray16:
		return image.NewGray16(r)
	case *image.Paletted:
		pl := make(color.Palette, len(p.Palette))
		copy(pl, p.Palette)
		return image.NewPaletted(r, pl)
	case *image.CMYK:
		return image.NewCMYK(r)
	default:
		return image.NewRGBA(r)
	}
}
Example #6
0
// Resample returns a resampled copy of the image slice r of m.
// The returned image has width w and height h.
func Resample(m image.Image, r image.Rectangle, w, h int) image.Image {
	if w < 0 || h < 0 {
		return nil
	}
	if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
		return image.NewRGBA64(image.Rect(0, 0, w, h))
	}
	img := image.NewRGBA(image.Rect(0, 0, w, h))
	xStep := float64(r.Dx()) / float64(w)
	yStep := float64(r.Dy()) / float64(h)
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			xSrc := int(float64(r.Min.X) + float64(x)*xStep)
			ySrc := int(float64(r.Min.Y) + float64(y)*yStep)
			r, g, b, a := m.At(xSrc, ySrc).RGBA()
			img.SetRGBA(x, y, color.RGBA{
				R: uint8(r >> 8),
				G: uint8(g >> 8),
				B: uint8(b >> 8),
				A: uint8(a >> 8),
			})
		}
	}
	return img
}
Example #7
0
func Crop(m image.Image, r image.Rectangle, w, h int) image.Image {
	if w < 0 || h < 0 {
		return nil
	}
	if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 {
		return image.NewRGBA64(image.Rect(0, 0, w, h))
	}
	curw, curh := r.Min.X, r.Min.Y

	img := image.NewRGBA(image.Rect(0, 0, w, h))

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			// Get a source pixel.
			subx := curw + x
			suby := curh + y

			r32, g32, b32, a32 := m.At(subx, suby).RGBA()
			r := uint8(r32 >> 8)
			g := uint8(g32 >> 8)
			b := uint8(b32 >> 8)
			a := uint8(a32 >> 8)

			img.SetRGBA(x, y, color.RGBA{r, g, b, a})
		}
	}

	return img
}
Example #8
0
func TestIsHighQuality(t *testing.T) {
	r := image.Rect(0, 0, 1, 1)
	for _, tc := range []struct {
		name     string
		p        image.Image
		expected bool
	}{
		{
			name:     "RGBA64",
			p:        image.NewRGBA64(r),
			expected: true,
		},
		{
			name:     "NRGBA64",
			p:        image.NewNRGBA64(r),
			expected: true,
		},
		{
			name:     "RGBA",
			p:        image.NewRGBA(r),
			expected: false,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			res := isHighQuality(tc.p)
			if res != tc.expected {
				t.Fatalf("unexpected result for %T: got %t, want %t", tc.p, res, tc.expected)
			}
		})
	}
}
Example #9
0
func TestIsHighQuality(t *testing.T) {
	r := image.Rect(0, 0, 1, 1)
	type TC struct {
		p        image.Image
		expected bool
	}
	for _, tc := range []TC{
		{
			p:        image.NewRGBA64(r),
			expected: true,
		},
		{
			p:        image.NewNRGBA64(r),
			expected: true,
		},
		{
			p:        image.NewRGBA(r),
			expected: false,
		},
	} {
		res := isHighQuality(tc.p)
		if res != tc.expected {
			t.Fatalf("unexpected result for %T: got %t, want %t", tc.p, res, tc.expected)
		}
	}
}
Example #10
0
func createRandomImage(maxPaletteSize int) image.Image {
	r := image.Rectangle{Min: image.Point{0, 0}, Max: image.Point{gen.Rand(32) + 1, gen.Rand(32) + 1}}
	var img Image
	switch gen.Rand(10) {
	case 0:
		img = image.NewAlpha(r)
	case 1:
		img = image.NewAlpha16(r)
	case 2:
		img = image.NewCMYK(r)
	case 3:
		img = image.NewGray(r)
	case 4:
		img = image.NewGray16(r)
	case 5:
		img = image.NewNRGBA(r)
	case 6:
		img = image.NewNRGBA64(r)
	case 7:
		img = image.NewPaletted(r, randPalette(maxPaletteSize))
	case 8:
		img = image.NewRGBA(r)
	case 9:
		img = image.NewRGBA64(r)
	default:
		panic("bad")
	}
	fill := gen.Rand(19)
	var palette []color.Color
	if fill == 17 {
		palette = randPalette(maxPaletteSize)
	}
	for y := 0; y < r.Max.Y; y++ {
		for x := 0; x < r.Max.X; x++ {
			switch {
			case fill <= 15:
				img.Set(x, y, color.RGBA64{
					^uint16(0) * uint16((fill>>0)&1),
					^uint16(0) * uint16((fill>>1)&1),
					^uint16(0) * uint16((fill>>2)&1),
					^uint16(0) * uint16((fill>>3)&1),
				})
			case fill == 16:
				img.Set(x, y, randColor())
			case fill == 17:
				img.Set(x, y, palette[gen.Rand(len(palette))])
			case fill == 18:
				if gen.Rand(3) != 0 {
					img.Set(x, y, color.RGBA64{})
				} else {
					img.Set(x, y, randColor())
				}
			default:
				panic("bad")
			}
		}
	}
	return img.(image.Image)
}
Example #11
0
func main() {
	var cpus *int
	if runtime.GOMAXPROCS(0) == 1 {
		cpus = flag.Int("cpus", runtime.NumCPU(), "the number of processor cores to use at any given time")
	} else {
		cpus = flag.Int("cpus", runtime.GOMAXPROCS(0), "the number of processor cores to use at any given time")
	}

	flag.Parse()

	if *cpuprof != "" {
		f, err := os.Create(*cpuprof)
		if err != nil {
			panic(err)
		}
		defer f.Close()
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	runtime.GOMAXPROCS(*cpus)

	noise0 = NewNoiseGen(0, *oct)
	noise1 = NewNoiseGen(1, *oct)
	noise2 = NewNoiseGen(2, *oct)

	var wg sync.WaitGroup

	dim := *ppi * *ppp

	img := image.NewRGBA64(image.Rect(0, 0, dim, dim))

	ch := make(chan [2]int, 16)
	for i := 0; i < *cpus; i++ {
		go Worker(ch, img, &wg)
	}

	for x := 0; x < dim; x++ {
		for y := 0; y < dim; y++ {
			wg.Add(1)
			ch <- [2]int{x, y}
		}
	}

	close(ch)

	wg.Wait()

	f, err := os.Create(*output)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	err = png.Encode(f, img)
	if err != nil {
		panic(err)
	}
}
Example #12
0
// ConvertToRGBA64 returns an *image.RGBA64 instance by asserting the given
// ImageReader has that type or, if it does not, using Copy to concurrently
// set the color.Color values of a new *image.RGBA64 instance with the same
// bounds.
func ConvertToRGBA64(src ImageReader) *image.RGBA64 {
	if dst, ok := src.(*image.RGBA64); ok {
		return dst
	}
	dst := image.NewRGBA64(src.Bounds())
	Copy(dst, src)
	return dst
}
Example #13
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 #14
0
func BenchmarkEncodeRGBA64(b *testing.B) {
	img := image.NewRGBA64(image.Rect(0, 0, 1<<10, 1<<10))
	io.ReadFull(rand.Reader, img.Pix)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Encode(ioutil.Discard, img)
	}
}
Example #15
0
func TestSubImage(t *testing.T) {
	testData := []struct {
		desc string
		img  draw.Image
		ok   bool
	}{
		{
			"sub image (Gray)",
			image.NewGray(image.Rect(0, 0, 10, 10)),
			true,
		},
		{
			"sub image (Gray16)",
			image.NewGray16(image.Rect(0, 0, 10, 10)),
			true,
		},
		{
			"sub image (RGBA)",
			image.NewRGBA(image.Rect(0, 0, 10, 10)),
			true,
		},
		{
			"sub image (RGBA64)",
			image.NewRGBA64(image.Rect(0, 0, 10, 10)),
			true,
		},
		{
			"sub image (NRGBA)",
			image.NewNRGBA(image.Rect(0, 0, 10, 10)),
			true,
		},
		{
			"sub image (NRGBA64)",
			image.NewNRGBA64(image.Rect(0, 0, 10, 10)),
			true,
		},
		{
			"sub image (fake)",
			fakeDrawImage{image.Rect(0, 0, 10, 10)},
			false,
		},
	}

	for _, d := range testData {
		simg, ok := getSubImage(d.img, image.Pt(3, 3))
		if ok != d.ok {
			t.Errorf("test [%s] failed: expected %#v, got %#v", d.desc, d.ok, ok)
		} else if ok {
			simg.Set(5, 5, color.NRGBA{255, 255, 255, 255})
			r, g, b, a := d.img.At(5, 5).RGBA()
			if r != 0xffff || g != 0xffff || b != 0xffff || a != 0xffff {
				t.Errorf("test [%s] failed: expected (0xffff, 0xffff, 0xffff, 0xffff), got (%d, %d, %d, %d)", d.desc, r, g, b, a)
			}
		}
	}

}
Example #16
0
func TestIsOpaque(t *testing.T) {
	type opqt struct {
		img    image.Image
		opaque bool
	}
	var testData []opqt

	testData = append(testData, opqt{image.NewNRGBA(image.Rect(0, 0, 1, 1)), false})
	testData = append(testData, opqt{image.NewNRGBA64(image.Rect(0, 0, 1, 1)), false})
	testData = append(testData, opqt{image.NewRGBA(image.Rect(0, 0, 1, 1)), false})
	testData = append(testData, opqt{image.NewRGBA64(image.Rect(0, 0, 1, 1)), false})
	testData = append(testData, opqt{image.NewGray(image.Rect(0, 0, 1, 1)), true})
	testData = append(testData, opqt{image.NewGray16(image.Rect(0, 0, 1, 1)), true})
	testData = append(testData, opqt{image.NewYCbCr(image.Rect(0, 0, 1, 1), image.YCbCrSubsampleRatio444), true})
	testData = append(testData, opqt{image.NewAlpha(image.Rect(0, 0, 1, 1)), false})

	img1 := image.NewNRGBA(image.Rect(0, 0, 1, 1))
	img1.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
	testData = append(testData, opqt{img1, true})
	img2 := image.NewNRGBA64(image.Rect(0, 0, 1, 1))
	img2.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
	testData = append(testData, opqt{img2, true})
	img3 := image.NewRGBA(image.Rect(0, 0, 1, 1))
	img3.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
	testData = append(testData, opqt{img3, true})
	img4 := image.NewRGBA64(image.Rect(0, 0, 1, 1))
	img4.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff})
	testData = append(testData, opqt{img4, true})
	imgp1 := image.NewPaletted(image.Rect(0, 0, 1, 1), []color.Color{color.NRGBA{0x00, 0x00, 0x00, 0xff}})
	imgp1.SetColorIndex(0, 0, 0)
	testData = append(testData, opqt{imgp1, true})
	imgp2 := image.NewPaletted(image.Rect(0, 0, 1, 1), []color.Color{color.NRGBA{0x00, 0x00, 0x00, 0xfe}})
	imgp2.SetColorIndex(0, 0, 0)
	testData = append(testData, opqt{imgp2, false})

	for _, d := range testData {
		isop := isOpaque(d.img)
		if isop != d.opaque {
			t.Errorf("isOpaque failed %#v, %v", d.img, isop)
		}
	}
}
Example #17
0
func TestEncodeEmptyImage(t *testing.T) {
	img := image.NewRGBA64(image.Rect(0, 0, 0, 0))
	w := new(bytes.Buffer)
	err := Encode(w, img)
	if err != nil {
		t.Errorf("err is not nil: %v", err)
	}
	if 0 != bytes.Compare(w.Bytes(), []byte("farbfeld\000\000\000\000\000\000\000\000")) {
		t.Errorf("encoded image differs")
	}
}
Example #18
0
func Rect(src image.Image, r image.Rectangle, at At) image.Image {
	dst := image.NewRGBA64(image.Rectangle{image.ZP, r.Size()})
	b := dst.Bounds()
	for x := r.Min.X; x < r.Max.X; x++ {
		for y := r.Min.Y; y < r.Max.Y; y++ {
			p := image.Pt(x, y).Sub(r.Min).Add(b.Min)
			dst.Set(p.X, p.Y, at(src, image.Pt(x, y)))
		}
	}
	return dst
}
Example #19
0
File: gl.go Project: extrame/gl
// Add the interface to export image
func ReadPixels(x int, y int, w int, h int) image.Image {
	data := make([]uint16, 4*w*h)
	p := unsafe.Pointer(&data[0])
	C.glReadPixels(C.GLint(x), C.GLint(y), C.GLsizei(w), C.GLsizei(h), C.GLenum(RGBA), C.GLenum(UNSIGNED_SHORT), p)
	rec := image.Rect(x, y, x+w, y+h)
	rgba := image.NewRGBA64(rec)
	for i := 0; i < w*h; i++ {
		c := color.RGBA64{data[4*i], data[4*i+1], data[4*i+2], data[4*i+3]}
		rgba.Set(i%w, h-i/w-1, c)
	}
	return rgba
}
Example #20
0
// Returns the Image to be used for output operations. If patch is
// true and `out' describes an existing image, we will load and use
// that image; note that if the image is a different size than the
// `src' image we return an error. If patch is true and `out' does
// not describe a valid image, we return an error. Otherwise, we
// return a blank image.
func outputImage(src image.Image, out string, patch bool) (*image.RGBA64, error) {
	if patch {
		if out == "" {
			return nil, errors.New("Cannot patch without an explicit output file")
		}

		if img, _, err := readImage(out); err != nil {
			return nil, err
		} else {
			src_bounds := src.Bounds()
			img_bounds := img.Bounds()
			// Check the size of the image
			if img_bounds.Size() != src_bounds.Size() {
				img_w := img_bounds.Size().X
				img_h := img_bounds.Size().Y
				src_w := src_bounds.Size().X
				src_h := src_bounds.Size().Y
				unf_msg := "Output image's size (%d x %d) does not match " +
					"the input image's size (%d x %d)"
				msg := fmt.Sprintf(unf_msg, img_w, img_h, src_w, src_h)
				return nil, errors.New(msg)
			}

			// We now need to coerce the image into the image.RGBA64
			// format. We use src.Bounds() so the coordinate systems are
			// consistent.
			ret := image.NewRGBA64(src.Bounds())
			for x := src_bounds.Min.X; x < src_bounds.Max.X; x++ {
				for y := src_bounds.Min.Y; y < src_bounds.Max.Y; y++ {
					ret.Set(x, y, img.At(x, y))
				}
			}

			return ret, nil
		}
	} else {
		// We use src.Bounds() so the coordinate systems are consistent.
		return image.NewRGBA64(src.Bounds()), nil
	}
}
Example #21
0
File: img.go Project: grisme/go_imp
func (this JpegImg) SaveToFile(path string) {
	img := image.NewRGBA64(this.bounds)
	pos := 0
	for y := this.bounds.Min.Y; y < this.bounds.Max.Y; y++ {
		for x := this.bounds.Min.X; x < this.bounds.Max.X; x++ {
			img.Set(x, y, this.Pixels[pos])
			pos++
		}
	}
	outfile, _ := os.Create(path)
	defer outfile.Close()
	jpeg.Encode(outfile, img, nil)
}
Example #22
0
func TestNewPixelGetter(t *testing.T) {
	var img image.Image
	var pg *pixelGetter
	img = image.NewNRGBA(image.Rect(0, 0, 1, 1))
	pg = newPixelGetter(img)
	if pg.imgType != itNRGBA || pg.imgNRGBA == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter NRGBA")
	}
	img = image.NewNRGBA64(image.Rect(0, 0, 1, 1))
	pg = newPixelGetter(img)
	if pg.imgType != itNRGBA64 || pg.imgNRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter NRGBA64")
	}
	img = image.NewRGBA(image.Rect(0, 0, 1, 1))
	pg = newPixelGetter(img)
	if pg.imgType != itRGBA || pg.imgRGBA == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter RGBA")
	}
	img = image.NewRGBA64(image.Rect(0, 0, 1, 1))
	pg = newPixelGetter(img)
	if pg.imgType != itRGBA64 || pg.imgRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter RGBA64")
	}
	img = image.NewGray(image.Rect(0, 0, 1, 1))
	pg = newPixelGetter(img)
	if pg.imgType != itGray || pg.imgGray == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter Gray")
	}
	img = image.NewGray16(image.Rect(0, 0, 1, 1))
	pg = newPixelGetter(img)
	if pg.imgType != itGray16 || pg.imgGray16 == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter Gray16")
	}
	img = image.NewYCbCr(image.Rect(0, 0, 1, 1), image.YCbCrSubsampleRatio422)
	pg = newPixelGetter(img)
	if pg.imgType != itYCbCr || pg.imgYCbCr == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter YCbCr")
	}
	img = image.NewUniform(color.NRGBA64{0, 0, 0, 0})
	pg = newPixelGetter(img)
	if pg.imgType != itGeneric || pg.imgGeneric == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter Generic(Uniform)")
	}
	img = image.NewAlpha(image.Rect(0, 0, 1, 1))
	pg = newPixelGetter(img)
	if pg.imgType != itGeneric || pg.imgGeneric == nil || !img.Bounds().Eq(pg.imgBounds) {
		t.Error("newPixelGetter Generic(Alpha)")
	}
}
Example #23
0
func makeImages(r image.Rectangle) []image.Image {
	return []image.Image{
		image.NewGray(r),
		image.NewGray16(r),
		image.NewNRGBA(r),
		image.NewNRGBA64(r),
		image.NewPaletted(r, somePalette),
		image.NewRGBA(r),
		image.NewRGBA64(r),
		image.NewYCbCr(r, image.YCbCrSubsampleRatio444),
		image.NewYCbCr(r, image.YCbCrSubsampleRatio422),
		image.NewYCbCr(r, image.YCbCrSubsampleRatio420),
		image.NewYCbCr(r, image.YCbCrSubsampleRatio440),
	}
}
Example #24
0
func imgType(im image.Image, w, h int) imageng {
	var scaled imageng
	switch im.ColorModel() {
	case image.RGBAColorModel:
		scaled = image.NewRGBA(w, h)
	case image.NRGBAColorModel:
		scaled = image.NewNRGBA(w, h)
	case image.GrayColorModel:
		scaled = image.NewGray(w, h)
	case image.Gray16ColorModel:
		scaled = image.NewGray16(w, h)
	default:
		scaled = image.NewRGBA64(w, h)
	}
	return scaled
}
Example #25
0
func Test_SameColorWithRGBA64(t *testing.T) {
	img := image.NewRGBA64(image.Rect(0, 0, 20, 20))
	for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
		for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
			img.SetRGBA64(x, y, color.RGBA64{0x8000, 0x8000, 0x8000, 0xFFFF})
		}
	}
	out := Resize(10, 10, img, Lanczos3)
	for y := out.Bounds().Min.Y; y < out.Bounds().Max.Y; y++ {
		for x := out.Bounds().Min.X; x < out.Bounds().Max.X; x++ {
			color := out.At(x, y).(color.RGBA64)
			if color.R != 0x8000 || color.G != 0x8000 || color.B != 0x8000 || color.A != 0xFFFF {
				t.Errorf("%+v", color)
			}
		}
	}
}
Example #26
0
// Resize an image to new width and height using the interpolation function interp.
// A new image with the given dimensions will be returned.
// If one of the parameters width or height is set to 0, its size will be calculated so that
// the aspect ratio is that of the originating image.
// The resizing algorithm uses channels for parallel computation.
func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
	oldBounds := img.Bounds()
	oldWidth := float32(oldBounds.Dx())
	oldHeight := float32(oldBounds.Dy())

	scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)
	t := Trans2{scaleX, 0, float32(oldBounds.Min.X), 0, scaleY, float32(oldBounds.Min.Y)}

	resizedImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
	b := resizedImg.Bounds()
	adjustX := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dx()-1))
	adjustY := 0.5 * ((oldHeight-1.0)/scaleY - float32(b.Dy()-1))

	n := numJobs(b.Dy())
	c := make(chan int, n)
	for i := 0; i < n; i++ {
		go func(b image.Rectangle, c chan int) {
			filter := interp(img, [2]float32{clampFactor(scaleX), clampFactor(scaleY)})
			var u, v float32
			var color color.RGBA64
			for y := b.Min.Y; y < b.Max.Y; y++ {
				for x := b.Min.X; x < b.Max.X; x++ {
					u, v = t.Eval(float32(x)+adjustX, float32(y)+adjustY)
					color = filter.Interpolate(u, v)

					i := resizedImg.PixOffset(x, y)
					resizedImg.Pix[i+0] = uint8(color.R >> 8)
					resizedImg.Pix[i+1] = uint8(color.R)
					resizedImg.Pix[i+2] = uint8(color.G >> 8)
					resizedImg.Pix[i+3] = uint8(color.G)
					resizedImg.Pix[i+4] = uint8(color.B >> 8)
					resizedImg.Pix[i+5] = uint8(color.B)
					resizedImg.Pix[i+6] = uint8(color.A >> 8)
					resizedImg.Pix[i+7] = uint8(color.A)
				}
			}
			c <- 1
		}(image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n), c)
	}

	for i := 0; i < n; i++ {
		<-c
	}

	return resizedImg
}
Example #27
0
func (p *RadianceImage) ToRGBA64(iterations int) *image.RGBA64 {

	scalefactor := p.calculateScaleFactor(iterations)

	m := image.NewRGBA64(image.Rect(0, 0, p.Width, p.Height))
	for x := 0; x < p.Width; x++ {
		for y := 0; y < p.Height; y++ {
			radiance := p.Get(x, y)
			r := uint16(65535 * math.Pow(math.Max(radiance.R*scalefactor/float64(iterations), 0), GAMMA_ENCODE))
			g := uint16(65535 * math.Pow(math.Max(radiance.G*scalefactor/float64(iterations), 0), GAMMA_ENCODE))
			b := uint16(65535 * math.Pow(math.Max(radiance.B*scalefactor/float64(iterations), 0), GAMMA_ENCODE))
			a := uint16(65535)
			m.Set(x, y, color.RGBA64{r, g, b, a})
		}
	}
	return m
}
Example #28
0
func (pyr *Pyramid) Visualize() image.Image {
	var width, height int
	for _, level := range pyr.Levels {
		if level.Bounds().Dx() > width {
			width = level.Bounds().Dx()
		}
		height += level.Bounds().Dy()
	}

	im := image.NewRGBA64(image.Rect(0, 0, width, height))
	var p image.Point
	for _, level := range pyr.Levels {
		draw.Draw(im, level.Bounds().Add(p), level, image.ZP, draw.Src)
		p.Y += level.Bounds().Dy()
	}
	return im
}
// purpleJPEG takes in an image and makes each of pixels more purple by
// incrementing its red and blue hex values by 50.
func purpleJPEG(img image.Image) {
	pixels := image.NewRGBA64(img.Bounds())
	for x := pixels.Bounds().Min.X; x < pixels.Bounds().Max.X; x++ {
		for y := pixels.Bounds().Min.Y; y < pixels.Bounds().Max.Y; y++ {
			r, g, b, a := img.At(x, y).RGBA()
			r = r>>8 + 50
			b = b>>8 + 50
			if r > 256 {
				r = 256
			}
			if b > 256 {
				b = 256
			}
			pixels.Set(x, y, color.RGBA64{R: uint16(r << 8), G: uint16(g), B: uint16(b << 8), A: uint16(a)})
		}
	}
	jpeg.Encode(os.Stdout, pixels, nil)
}
Example #30
0
func TestEncode64(t *testing.T) {
	img := image.NewRGBA64(image.Rect(0, 0, 3, 3))
	for x := 0; x < 3; x++ {
		for y := 0; y < 3; y++ {
			img.SetRGBA64(x, y, imagePixels64[y][x])
		}
	}

	var buf bytes.Buffer
	err := Encode(&buf, img)
	if err != nil {
		t.Fatal(err)
	}

	if !bytes.Equal(imageData, buf.Bytes()) {
		t.Fatal("encoding error")
	}
}