// 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) } }
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) } } }
func Render(surfaces []Surface, camera Camera, width, height int) image.Image { view := View{camera, width, height} bounds := image.Rect(0, 0, width, height) img := image.NewNRGBA64(bounds) halfwidth := width / 2 halfheight := height / 2 // render for x := bounds.Min.X; x < bounds.Max.X; x++ { for y := bounds.Min.Y; y < bounds.Max.Y; y++ { // compensate for image y direction ray := view.Ray(x-halfwidth, -y+halfheight) hit, color := RayTraceRecursive(surfaces, ray, 1) if hit { img.Set(x, y, vecToNRGBA(color.Scale(0.7))) } } } return img }
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) } }) } }
func (self *AverageSmooth) Process(img image.Image) image.Image { ret := image.NewNRGBA64(img.Bounds()) for x := 0; x < img.Bounds().Dx(); x++ { for y := 0; y < img.Bounds().Dy(); y++ { rs := 0 gs := 0 bs := 0 as := 0 n := 0 for i := Max(0, x-1); i <= Min(img.Bounds().Dx()-1, x+1); i++ { for j := Max(0, y-1); j <= Min(img.Bounds().Dy()-1, y+1); j++ { c := 1 if i == x && j == y { c = 8 } r, g, b, a := img.At(i, j).RGBA() rs += int(r) * c gs += int(g) * c bs += int(b) * c as += int(a) * c n += c } } ret.Set(x, y, color.RGBA64{R: uint16(rs / n), G: uint16(gs / n), B: uint16(bs / n), A: uint16(as / n)}) } } return ret }
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") }
/* Create a new StegoImgWriter. orig_img is a reader which should be the file of the image to encode the data into. new_img is the file that the encoded image will be written to. format can be one of "png", "jpeg", or "gif" The orig_img reader need not remain open after the call returns. The writer must remain open until after the call to Close returns. */ func NewStegoImgWriter(orig_img io.Reader, new_img io.Writer) (img *StegoImgWriter, e error) { // create the image writer tmp_img := new(StegoImgWriter) // attempt to decode the original image tmp_img.orig_img, tmp_img.format, e = image.Decode(orig_img) // BUG(Andrew): only PNG format currently works tmp_img.format = "png" if e != nil { return } // create a new image tmp_img.new_img = image.NewNRGBA64(tmp_img.orig_img.Bounds()) // make the data array to store all potential data size := (tmp_img.orig_img.Bounds().Max.X - 1) * (tmp_img.orig_img.Bounds().Max.Y - 1) * 3 tmp_img.data = make([]byte, 0, size) // block out space for the size argument tmp_img.data = append(tmp_img.data, 0, 0, 0, 0) // save output tmp_img.output = new_img // mark image as open tmp_img.is_open = true // all was successful, return the new pointer img = tmp_img return }
func (self *MeanShift) Process(img image.Image) image.Image { for k := 0; k < self.K; k++ { next := image.NewNRGBA64(img.Bounds()) for x := 0; x < img.Bounds().Dx(); x++ { for y := 0; y < img.Bounds().Dy(); y++ { rs := []uint32{} gs := []uint32{} bs := []uint32{} as := []uint32{} for i := Max(0, x-1); i <= Min(img.Bounds().Dx()-1, x+1); i++ { for j := Max(0, y-1); j <= Min(img.Bounds().Dy()-1, y+1); j++ { r, g, b, a := img.At(i, j).RGBA() rs = append(rs, r) gs = append(gs, g) bs = append(bs, b) as = append(as, a) } } next.Set(x, y, color.RGBA64{R: MiddleValue(rs), G: MiddleValue(gs), B: MiddleValue(bs), A: MiddleValue(as)}) } } img = next } return img }
func main() { srcPath := os.Args[1] dstPath := os.Args[2] reader, err := os.Open(srcPath) check(err) src, srcFmt, err := image.Decode(reader) check(err) fmt.Println("Decoded source", srcPath, "as", srcFmt) scale := 8 boundsMax := src.Bounds().Max smallMax := boundsMax.Div(scale) flat := image.NewNRGBA64(image.Rectangle{image.ZP, smallMax}) samplePix := func(x, y int) (r, g, b []int) { var rs, gs, bs []int for i := x - 25; i <= x+25; i += 5 { if i < 0 || i >= boundsMax.X { continue } for j := y - 25; j <= y+25; j += 5 { if j < 0 || j >= boundsMax.Y { continue } r, g, b, _ := src.At(i, j).RGBA() rs = append(rs, int(r)) gs = append(gs, int(g)) bs = append(bs, int(b)) } } return rs, gs, bs } var wg sync.WaitGroup for i := 0; i < smallMax.X; i++ { for j := 0; j < smallMax.Y; j++ { wg.Add(1) go func(x, y int) { defer wg.Done() r, g, b := samplePix(x*scale+scale/2, y*scale+scale/2) sort.Ints(r) sort.Ints(g) sort.Ints(b) idx := len(r) / 8 flat.SetNRGBA64(x, y, color.NRGBA64{ uint16(r[idx]), uint16(g[idx]), uint16(b[idx]), 0xFFFF}) }(i, j) } } wg.Wait() writer, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, 0600) check(err) //jpeg.Encode(writer, proxy, nil) png.Encode(writer, flat) }
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) }
// ConvertToNRGBA64 returns an *image.NRGBA64 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.NRGBA64 instance with the same // bounds. func ConvertToNRGBA64(src ImageReader) *image.NRGBA64 { if dst, ok := src.(*image.NRGBA64); ok { return dst } dst := image.NewNRGBA64(src.Bounds()) Copy(dst, src) return dst }
func BenchmarkEncodeNRGBA64(b *testing.B) { img := image.NewNRGBA64(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) } }
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) } } } }
// Decode reads a farbfeld from r and returns it as image.NRGBA64. func Decode(r io.Reader) (image.Image, error) { cfg, err := DecodeConfig(r) if err != nil { return nil, err } img := image.NewNRGBA64(image.Rect(0, 0, cfg.Width, cfg.Height)) // image.NRGBA64 is big endian, so is farbfeld → just copy bytes _, err = io.ReadFull(r, img.Pix) return img, err }
// Decode reads a Farbfeld image from r and returns it as an image.Image. func Decode(r io.Reader) (image.Image, error) { cfg, err := DecodeConfig(r) if err != nil { return nil, err } img := image.NewNRGBA64(image.Rect(0, 0, cfg.Width, cfg.Height)) _, err = io.ReadFull(r, img.Pix) return img, err }
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) } } }
// Interpolate uint64/pixel images. func interpolate1x64(src *image.NRGBA64, dstW, dstH int) image.Image { srcRect := src.Bounds() srcW := srcRect.Dx() srcH := srcRect.Dy() ww, hh := uint64(dstW), uint64(dstH) dx, dy := uint64(srcW), uint64(srcH) n, sum := dx*dy, make([]uint64, dstW*dstH) for y := 0; y < srcH; y++ { pixOffset := src.PixOffset(0, y) for x := 0; x < srcW; x++ { // Get the source pixel. val64 := binary.BigEndian.Uint64([]byte(src.Pix[pixOffset+0 : pixOffset+8])) pixOffset += 8 // Spread the source pixel over 1 or more destination rows. py := uint64(y) * hh for remy := hh; remy > 0; { qy := dy - (py % dy) if qy > remy { qy = remy } // Spread the source pixel over 1 or more destination columns. px := uint64(x) * ww index := (py/dy)*ww + (px / dx) for remx := ww; remx > 0; { qx := dx - (px % dx) if qx > remx { qx = remx } qxy := qx * qy sum[index] += val64 * qxy index++ px += qx remx -= qx } py += qy remy -= qy } } } dst := image.NewNRGBA64(image.Rect(0, 0, dstW, dstH)) index := 0 for y := 0; y < dstH; y++ { pixOffset := dst.PixOffset(0, y) for x := 0; x < dstW; x++ { binary.BigEndian.PutUint64(dst.Pix[pixOffset+0:pixOffset+8], sum[index]/n) pixOffset += 8 index++ } } return dst }
func TestEncodeSmallImage(t *testing.T) { img := image.NewNRGBA64(image.Rect(0, 0, 1, 1)) img.Pix = []byte("aAbBcCdD") 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\001\000\000\000\001aAbBcCdD")) { t.Errorf("encoded image differs") } }
func (c *Converter) Image() image.Image { c.Lock() defer c.Unlock() // copy the image copy := image.NewNRGBA64(c.lastimg.Rect) for k, v := range c.lastimg.Pix { copy.Pix[k] = v } return copy }
func BenchmarkDecode(b *testing.B) { img := image.NewNRGBA64(image.Rect(0, 0, 256, 256)) io.ReadFull(rand.Reader, img.Pix) var buf bytes.Buffer Encode(&buf, img) b.ResetTimer() var r io.Reader for i := 0; i < b.N; i++ { r = bytes.NewReader(buf.Bytes()) Decode(r) } }
// Combine the three color channel and write them // to the out file func renderImage(red [][]uint64, green [][]uint64, blue [][]uint64) { im := image.NewNRGBA64(image.Rect(0, 0, int(*width), int(*height))) var ( minr uint64 = 1<<64 - 1 ming uint64 = minr minb uint64 = minr maxr, maxg, maxb uint64 = 0, 0, 0 ) f := func(count uint64, min, max *uint64) { if count < *min { *min = count } if count > *max { *max = count } } // pick the min/max for each channel // to be able to do the colouring for y := 0; y < int(*height); y++ { for x := 0; x < int(*width); x++ { r, g, b := red[y][x], green[y][x], blue[y][x] f(r, &minr, &maxr) f(g, &ming, &maxg) f(b, &minb, &maxb) } } // set each pixel to its color for y := 0; y < int(*height); y++ { for x := 0; x < int(*width); x++ { r, g, b := red[y][x], green[y][x], blue[y][x] var m float64 = 1<<16 - 1 im.SetNRGBA64(x, y, color.NRGBA64{ uint16(m * math.Sqrt(float64(r)/float64(maxr-minr))), uint16(m * math.Sqrt(float64(g)/float64(maxg-ming))), uint16(m * math.Sqrt(float64(b)/float64(maxb-minb))), 1<<16 - 1}) } } w, _ := os.OpenFile(*out, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) defer w.Close() // write the png png.Encode(w, im) }
func (g zoomedImageGetter) Get(ctx groupcache.Context, key string, dest groupcache.Sink) error { bfz := &BuildingFloorZoom{} if err := json.Unmarshal([]byte(key), bfz); err != nil { return err } floor := g.s.GetBuildingFloor(bfz.Building, bfz.Floor) var err error floor.ImageOnce.Do(func() { var img, origImg draw.Image floor.ImageWG.Add(1) defer floor.ImageWG.Done() origImg, err = floor.LoadImage() if err != nil { return } img = origImg if floor.Rotation != 0 { rotatedWidth, rotatedHeight := newImageDimentions(origImg, floor.Rotation) img = image.NewNRGBA64(image.Rect(0, 0, rotatedWidth, rotatedHeight)) if err = graphics.Rotate(img, origImg, &graphics.RotateOptions{Angle: floor.Rotation}); err != nil { return } } floor.RotatedImage = img }) floor.ImageWG.Wait() if err != nil { return err } img := floor.RotatedImage coords := ctx.(*models.Coords) pixelsPerLongitude := TileSize / coords.DLng() pixelsPerLatitude := TileSize / coords.DLat() newWidth := floor.Coords.DLng() * pixelsPerLongitude newHeight := floor.Coords.DLat() * pixelsPerLatitude log.Printf("Generating resized image %f %f", newWidth, newHeight) resizedImg := resize.Resize(uint(newWidth), uint(newHeight), img, resize.NearestNeighbor) var buf bytes.Buffer if err := tileEncoder.Encode(&buf, resizedImg); err != nil { return err } return dest.SetBytes(buf.Bytes()) }
func randomNRGBA64(rect image.Rectangle) image.Image { img := image.NewNRGBA64(rect) QuickRP( AllPointsRP(func(pt image.Point) { img.SetNRGBA64(pt.X, pt.Y, color.NRGBA64{ R: uint16(rand.Intn(int(math.MaxUint16 + 1))), G: uint16(rand.Intn(int(math.MaxUint16 + 1))), B: uint16(rand.Intn(int(math.MaxUint16 + 1))), A: uint16(rand.Intn(int(math.MaxUint16 + 1))), }) }), )(rect) return img }
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)") } }
// NewProcessor creates a Processor. func NewProcessor(gamma float64, highQuality bool) *Processor { prc := new(Processor) gammaInv := 1 / gamma for i := range prc.vals { prc.vals[i] = uint16(math.Pow(float64(i)/65535, gammaInv)*65535 + 0.5) } if highQuality { prc.newDrawable = func(p image.Image) draw.Image { return image.NewNRGBA64(image.Rect(0, 0, p.Bounds().Dx(), p.Bounds().Dy())) } } else { prc.newDrawable = imageserver_image_internal.NewDrawable } return prc }
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), } }
func Test_SameColorWithNRGBA64(t *testing.T) { img := image.NewNRGBA64(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.SetNRGBA64(x, y, color.NRGBA64{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) } } } }
func TestNewPixelSetter(t *testing.T) { var img draw.Image var pg *pixelSetter img = image.NewNRGBA(image.Rect(0, 0, 1, 1)) pg = newPixelSetter(img) if pg.imgType != itNRGBA || pg.imgNRGBA == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter NRGBA") } img = image.NewNRGBA64(image.Rect(0, 0, 1, 1)) pg = newPixelSetter(img) if pg.imgType != itNRGBA64 || pg.imgNRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter NRGBA64") } img = image.NewRGBA(image.Rect(0, 0, 1, 1)) pg = newPixelSetter(img) if pg.imgType != itRGBA || pg.imgRGBA == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter RGBA") } img = image.NewRGBA64(image.Rect(0, 0, 1, 1)) pg = newPixelSetter(img) if pg.imgType != itRGBA64 || pg.imgRGBA64 == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter RGBA64") } img = image.NewGray(image.Rect(0, 0, 1, 1)) pg = newPixelSetter(img) if pg.imgType != itGray || pg.imgGray == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter Gray") } img = image.NewGray16(image.Rect(0, 0, 1, 1)) pg = newPixelSetter(img) if pg.imgType != itGray16 || pg.imgGray16 == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter Gray16") } img = image.NewPaletted(image.Rect(0, 0, 1, 1), color.Palette{}) pg = newPixelSetter(img) if pg.imgType != itPaletted || pg.imgPaletted == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter Paletted") } img = image.NewAlpha(image.Rect(0, 0, 1, 1)) pg = newPixelSetter(img) if pg.imgType != itGeneric || pg.imgGeneric == nil || !img.Bounds().Eq(pg.imgBounds) { t.Error("newPixelSetter Generic(Alpha)") } }
func TestEncodeSubImage(t *testing.T) { img := image.NewNRGBA64(image.Rect(0, 0, 4, 4)) for y := 0; y < 4; y++ { for x := 0; x < 4; x++ { c := uint16(y*4*4 + x*4) img.SetNRGBA64(x, y, color.NRGBA64{c, c + 1, c + 2, c + 3}) } } w := new(bytes.Buffer) err := Encode(w, img.SubImage(image.Rect(1, 1, 3, 3))) if err != nil { t.Errorf("err is not nil: %v", err) } if 0 != bytes.Compare(w.Bytes(), []byte("farbfeld\000\000\000\002\000\000\000\002"+ "\x00\x14\x00\x15\x00\x16\x00\x17\x00\x18\x00\x19\x00\x1a\x00\x1b"+ "\x00\x24\x00\x25\x00\x26\x00\x27\x00\x28\x00\x29\x00\x2a\x00\x2b")) { t.Errorf("encoded image differs") } }
func newDrawImage(r image.Rectangle, m color.Model) draw.Image { switch m { case color.RGBA64Model: return image.NewRGBA64(r) case color.NRGBAModel: return image.NewNRGBA(r) case color.NRGBA64Model: return image.NewNRGBA64(r) case color.AlphaModel: return image.NewAlpha(r) case color.Alpha16Model: return image.NewAlpha16(r) case color.GrayModel: return image.NewGray(r) case color.Gray16Model: return image.NewGray16(r) default: return image.NewRGBA(r) } }