MCPcopy Create free account
hub / github.com/cogentcore/core / SetGoImage

Method SetGoImage

vgpu/image.go:532–584  ·  view source on GitHub ↗

SetGoImage sets staging image data from a standard Go image at given layer. This is most efficiently done using an image.RGBA, but other formats will be converted as necessary. If flipY is true then the Image Y axis is flipped when copying into the image data, so that images will appear upright in t

(img image.Image, layer int, flipY bool)

Source from the content-addressed store, hash-verified

530// Must still call AllocImage to have image allocated on the device,
531// and copy from this host staging data to the device.
532func (im *Image) SetGoImage(img image.Image, layer int, flipY bool) error {
533 if !im.IsHostActive() {
534 return fmt.Errorf("vgpu.Image.SetGoImage: image cannot be set because Host not active: %s", im.Name)
535 }
536 if !im.Format.IsStdRGBA() {
537 return fmt.Errorf("vgpu.Image: Format is not standard RGBA format: %s", im.Name)
538 }
539 if img == nil {
540 return fmt.Errorf("vgpu.Image: input image is nil: %s", im.Name)
541 }
542 rimg := ImageToRGBA(img)
543 sz := rimg.Rect.Size()
544 dpix := im.HostPixels(layer)
545 sti := rimg.Rect.Min.Y*rimg.Stride + rimg.Rect.Min.X*4
546 spix := rimg.Pix[sti:]
547 ssz := len(spix)
548 dsz := len(dpix)
549 mx := min(ssz, dsz)
550 str := im.Format.Stride()
551 if rimg.Stride == str && !flipY {
552 copy(dpix[:mx], spix[:mx])
553 return nil
554 }
555 rows := min(sz.Y, im.Format.Size.Y)
556 rsz := min(rimg.Stride, str)
557 dmax := str * rows
558 if dmax > dsz {
559 return fmt.Errorf("vgpu.Image: image named: %s, format size: %d doesn't fit in actual destination size: %d", im.Name, dmax, dsz)
560 }
561 sidx := 0
562 if flipY {
563 didx := (rows - 1) * str
564 for rw := 0; rw < rows; rw++ {
565 copy(dpix[didx:didx+rsz], spix[sidx:sidx+rsz])
566 for ii := didx + rsz; ii < didx+str; ii++ { // zero out = transparent any extra mem
567 dpix[ii] = 0
568 }
569 sidx += rimg.Stride
570 didx -= str
571 }
572 } else {
573 didx := 0
574 for rw := 0; rw < rows; rw++ {
575 copy(dpix[didx:didx+rsz], spix[sidx:sidx+rsz])
576 for ii := didx + rsz; ii < didx+str; ii++ { // zero out = transparent any extra mem
577 dpix[ii] = 0
578 }
579 sidx += rimg.Stride
580 didx += str
581 }
582 }
583 return nil
584}
585
586// SetVkImage sets a Vk Image and configures a default 2D view
587// based on existing format information (which must be set properly).

Callers

nothing calls this directly

Calls 7

IsHostActiveMethod · 0.95
HostPixelsMethod · 0.95
ImageToRGBAFunction · 0.85
IsStdRGBAMethod · 0.80
StrideMethod · 0.80
ErrorfMethod · 0.65
SizeMethod · 0.65

Tested by

no test coverage detected