diff --git a/go.mod b/go.mod index 5de00cf16..0515b3d5a 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,14 @@ go 1.19 require ( github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924 + github.com/disintegration/imaging v1.6.2 github.com/google/go-cmp v0.5.8 github.com/google/uuid v1.3.0 github.com/jamesruan/go-rfc1924 v0.0.0-20170108144916-2767ca7c638f github.com/seancfoley/ipaddress-go v1.2.1 golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 golang.org/x/exp v0.0.0-20220921164117-439092de6870 - golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 + golang.org/x/image v0.2.0 golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 ) diff --git a/go.sum b/go.sum index 526c5e2d7..9a5160664 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924 h1:DG4UyTVIujioxwJc8Zj8Nabz1L1wTgQ/xNBSQDfdP3I= github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924/go.mod h1:+NaH2gLeY6RPBPPQf4aRotPPStg+eXc8f9ZaE4vRfD4= +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= @@ -10,13 +12,36 @@ github.com/seancfoley/bintree v1.1.0 h1:6J0rj9hLNLIcWSsfYdZ4ZHkMHokaK/PHkak8qyBO github.com/seancfoley/bintree v1.1.0/go.mod h1:CtE6qO6/n9H3V2CAGEC0lpaYr6/OijhNaMG/dt7P70c= github.com/seancfoley/ipaddress-go v1.2.1 h1:yEZxnyC6NQEDDPflyQm4KkWozffx1vHWsx+knKBr/n0= github.com/seancfoley/ipaddress-go v1.2.1/go.mod h1:/UEVHyrBg1ASVap2ffdY2cq5UMYIX9f3QW3uWSVqpbo= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 h1:WJywXQVIb56P2kAvXeMGTIgQ1ZHQxR60+F9dLsodECc= golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20220921164117-439092de6870 h1:j8b6j9gzSigH28O5SjSpQSSh9lFd6f5D/q0aHjNTulc= golang.org/x/exp v0.0.0-20220921164117-439092de6870/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= -golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 h1:Lj6HJGCSn5AjxRAH2+r35Mir4icalbqku+CLUtjnvXY= -golang.org/x/image v0.0.0-20220902085622-e7cb96979f69/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.2.0 h1:/DcQ0w3VHKCC5p0/P2B0JpAZ9Z++V2KOo2fyU89CXBQ= +golang.org/x/image v0.2.0/go.mod h1:la7oBXb9w3YFjBqaAwtynVioc1ZvOnNteUNrifGNmAI= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc= golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/tools/cmd/icat/main.go b/tools/cmd/icat/main.go index 4d36ebb05..50b0272c9 100644 --- a/tools/cmd/icat/main.go +++ b/tools/cmd/icat/main.go @@ -5,6 +5,7 @@ package icat import ( "errors" "fmt" + "image/color" "os" "runtime" "strconv" @@ -32,7 +33,7 @@ var opts *Options var lp *loop.Loop var place *Place var z_index int32 -var remove_alpha *style.RGBA +var remove_alpha *color.NRGBA var flip, flop bool type transfer_mode int @@ -71,7 +72,7 @@ func parse_background() (err error) { if err != nil { return fmt.Errorf("Invalid value for --background: %w", err) } - remove_alpha = &col + remove_alpha = &color.NRGBA{col.Red, col.Green, col.Blue, 255} return } diff --git a/tools/cmd/icat/native.go b/tools/cmd/icat/native.go index fecec2348..433149e08 100644 --- a/tools/cmd/icat/native.go +++ b/tools/cmd/icat/native.go @@ -5,28 +5,73 @@ package icat import ( "fmt" "image" + "kitty/tools/utils" + "kitty/tools/utils/shm" - "golang.org/x/image/draw" + "github.com/disintegration/imaging" ) var _ = fmt.Print +func add_frame(imgd *image_data, img image.Image) { + if flip { + img = imaging.FlipV(img) + } + if flop { + img = imaging.FlipH(img) + } + b := img.Bounds() + f := image_frame{width: b.Dx(), height: b.Dy()} + has_non_black_background := remove_alpha != nil && (remove_alpha.R != 0 || remove_alpha.G != 0 || remove_alpha.B != 0) + var rgba *image.NRGBA + m, err := shm.CreateTemp("icat-*", uint64(f.width*f.height*4)) + if err != nil { + if has_non_black_background { + rgba = imaging.New(b.Dx(), b.Dy(), remove_alpha) + } else { + rgba = image.NewNRGBA(image.Rect(0, 0, f.width, f.height)) + } + } else { + rgba = &image.NRGBA{ + Pix: m.Slice(), + Stride: 4 * f.width, + Rect: image.Rect(0, 0, f.width, f.height), + } + f.shm = m + if has_non_black_background { + utils.Memset(m.Slice(), remove_alpha.R, remove_alpha.G, remove_alpha.B, remove_alpha.A) + } else { + utils.Memset(m.Slice()) + } + } + imaging.PasteCenter(rgba, img) + imgd.format_uppercase = "RGBA" + f.in_memory_bytes = rgba.Pix + imgd.frames = append(imgd.frames, &f) +} + +func load_one_frame_image(imgd *image_data, src *opened_input) (image.Image, error) { + img, err := imaging.Decode(src.file, imaging.AutoOrientation(true)) + src.Rewind() + if err == nil { + // reset the sizes as we read EXIF tags here which could have rotated the image + imgd.canvas_width = img.Bounds().Dx() + imgd.canvas_height = img.Bounds().Dy() + set_basic_metadata(imgd) + } + return img, err +} + func render_image_with_go(imgd *image_data, src *opened_input) (err error) { switch imgd.format_uppercase { case "GIF": return fmt.Errorf("TODO: implement GIF decoding") default: - img, _, err := image.Decode(src.file) + img, err := load_one_frame_image(imgd, src) if err != nil { return err } - b := img.Bounds() - rgba := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) - draw.Draw(rgba, rgba.Bounds(), img, b.Min, draw.Src) - imgd.format_uppercase = "RGBA" - f := image_frame{width: b.Dx(), height: b.Dy()} - f.in_memory_bytes = rgba.Pix - imgd.frames = append(imgd.frames, &f) + add_frame(imgd, img) } return nil } diff --git a/tools/cmd/icat/process_images.go b/tools/cmd/icat/process_images.go index 40e752df2..c7030b2ea 100644 --- a/tools/cmd/icat/process_images.go +++ b/tools/cmd/icat/process_images.go @@ -16,6 +16,7 @@ import ( "kitty/tools/tty" "kitty/tools/utils" + "kitty/tools/utils/shm" ) var _ = fmt.Print @@ -137,6 +138,7 @@ func (self *opened_input) Release() { type image_frame struct { filename string + shm shm.MMap in_memory_bytes []byte filename_is_temporary bool width, height int @@ -156,15 +158,16 @@ type image_data struct { } func set_basic_metadata(imgd *image_data) { - imgd.frames = make([]*image_frame, 0, 32) + if imgd.frames == nil { + imgd.frames = make([]*image_frame, 0, 32) + } imgd.available_width = int(screen_size.WidthPx) imgd.available_height = 10 * imgd.canvas_height if place != nil { imgd.available_width = place.width * int(screen_size.CellWidth) imgd.available_height = place.height * int(screen_size.CellHeight) } - imgd.needs_scaling = imgd.canvas_width > imgd.available_width || imgd.canvas_height > imgd.available_height - imgd.needs_scaling = imgd.needs_scaling || opts.ScaleUp + imgd.needs_scaling = imgd.canvas_width > imgd.available_width || imgd.canvas_height > imgd.available_height || opts.ScaleUp imgd.needs_conversion = imgd.needs_scaling || remove_alpha != nil || flip || flop || imgd.format_uppercase != "PNG" } diff --git a/tools/cmd/icat/transmit.go b/tools/cmd/icat/transmit.go index c6e0b3132..30490109e 100644 --- a/tools/cmd/icat/transmit.go +++ b/tools/cmd/icat/transmit.go @@ -55,7 +55,6 @@ func transmit_shm(imgd *image_data, frame_num int, frame *image_frame) (err erro if err != nil { return fmt.Errorf("Failed to create a SHM file for transmission: %w", err) } - defer mmap.Close() dest := mmap.Slice() for len(dest) > 0 { n, err := f.Read(dest) @@ -69,18 +68,23 @@ func transmit_shm(imgd *image_data, frame_num int, frame *image_frame) (err erro } } } else { - data_size = int64(len(frame.in_memory_bytes)) - mmap, err = shm.CreateTemp("icat-*", uint64(data_size)) - if err != nil { - return fmt.Errorf("Failed to create a SHM file for transmission: %w", err) + if frame.shm == nil { + data_size = int64(len(frame.in_memory_bytes)) + mmap, err = shm.CreateTemp("icat-*", uint64(data_size)) + if err != nil { + return fmt.Errorf("Failed to create a SHM file for transmission: %w", err) + } + copy(mmap.Slice(), frame.in_memory_bytes) + } else { + mmap = frame.shm + frame.shm = nil } - defer mmap.Close() - copy(mmap.Slice(), frame.in_memory_bytes) } gc := gc_for_image(imgd, frame_num, frame) gc.SetTransmission(graphics.GRT_transmission_sharedmem) gc.SetDataSize(uint64(data_size)) gc.WriteWithPayloadToLoop(lp, utils.UnsafeStringToBytes(mmap.Name())) + mmap.Close() return nil } @@ -97,18 +101,24 @@ func transmit_file(imgd *image_data, frame_num int, frame *image_frame) (err err } frame.filename = "" // so it isnt deleted in cleanup } else { - f, err := graphics.CreateTempInRAM() - if err != nil { - return fmt.Errorf("Failed to create a temp file for image data transmission: %w", err) - } - data_size = len(frame.in_memory_bytes) - _, err = bytes.NewBuffer(frame.in_memory_bytes).WriteTo(f) - f.Close() - if err != nil { - return fmt.Errorf("Failed to write image data to temp file for transmission: %w", err) - } is_temp = true - fname = f.Name() + if frame.shm != nil && frame.shm.FileSystemName() != "" { + fname = frame.shm.FileSystemName() + frame.shm.Close() + frame.shm = nil + } else { + f, err := graphics.CreateTempInRAM() + if err != nil { + return fmt.Errorf("Failed to create a temp file for image data transmission: %w", err) + } + data_size = len(frame.in_memory_bytes) + _, err = bytes.NewBuffer(frame.in_memory_bytes).WriteTo(f) + f.Close() + if err != nil { + return fmt.Errorf("Failed to write image data to temp file for transmission: %w", err) + } + fname = f.Name() + } } gc := gc_for_image(imgd, frame_num, frame) if is_temp { @@ -148,6 +158,11 @@ func transmit_image(imgd *image_data) { os.Remove(frame.filename) frame.filename = "" } + if frame.shm != nil { + frame.shm.Unlink() + frame.shm.Close() + frame.shm = nil + } frame.in_memory_bytes = nil } }() diff --git a/tools/utils/misc.go b/tools/utils/misc.go index 545d2a8b1..5a107cb57 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -110,3 +110,27 @@ func Values[M ~map[K]V, K comparable, V any](m M) []V { } return r } + +func Memset[T any](dest []T, pattern ...T) []T { + if len(pattern) == 0 { + var zero T + switch any(zero).(type) { + case byte: // special case this as the compiler can generate efficient code for memset of a byte slice to zero + bd := any(dest).([]byte) + for i := range bd { + bd[i] = 0 + } + default: + for i := range dest { + dest[i] = zero + } + } + return dest + } + bp := copy(dest, pattern) + for bp < len(dest) { + copy(dest[bp:], dest[:bp]) + bp *= 2 + } + return dest +}