diff --git a/go.mod b/go.mod index 332d44736..1187f4c07 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/google/uuid v1.6.0 github.com/kovidgoyal/dbus v0.0.0-20250519011319-e811c41c0bc1 github.com/kovidgoyal/exiffix v0.0.0-20250919160812-dbef770c2032 - github.com/kovidgoyal/imaging v1.6.5 + github.com/kovidgoyal/imaging v1.7.0 github.com/seancfoley/ipaddress-go v1.7.1 github.com/shirou/gopsutil/v3 v3.24.5 github.com/zeebo/xxh3 v1.0.2 diff --git a/go.sum b/go.sum index 9c214b82f..036b68fee 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,8 @@ github.com/kovidgoyal/dbus v0.0.0-20250519011319-e811c41c0bc1 h1:rMY/hWfcVzBm6BL github.com/kovidgoyal/dbus v0.0.0-20250519011319-e811c41c0bc1/go.mod h1:RbNG3Q1g6GUy1/WzWVx+S24m7VKyvl57vV+cr2hpt50= github.com/kovidgoyal/exiffix v0.0.0-20250919160812-dbef770c2032 h1:TEV9lpo2a6fP1byiDsoJe2fXpvrj2itae41xMM+bEAg= github.com/kovidgoyal/exiffix v0.0.0-20250919160812-dbef770c2032/go.mod h1:VU38Nlbvb0lbyS5YkopCZMS5HuJ5QLVJBxRWyzq79q4= -github.com/kovidgoyal/imaging v1.6.5 h1:Id9DKlz/ydl5Vxt9QG5IjGSiIcHcszSKXxDubdO49PQ= -github.com/kovidgoyal/imaging v1.6.5/go.mod h1:mBprO214rATK/6OaPAUXmHbSMelPSFEmoBAt/IJdmno= +github.com/kovidgoyal/imaging v1.7.0 h1:SFWQPJdEuaRY7WhL+MJyoN5IvslgqU+z0iPaoGl3yuM= +github.com/kovidgoyal/imaging v1.7.0/go.mod h1:mBprO214rATK/6OaPAUXmHbSMelPSFEmoBAt/IJdmno= github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a h1:N9zuLhTvBSRt0gWSiJswwQ2HqDmtX/ZCDJURnKUt1Ik= github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a/go.mod h1:JKx41uQRwqlTZabZc+kILPrO/3jlKnQ2Z8b7YiVw5cE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/kittens/icat/main.go b/kittens/icat/main.go index b9780a11b..64d8bef52 100644 --- a/kittens/icat/main.go +++ b/kittens/icat/main.go @@ -11,12 +11,12 @@ import ( "sync/atomic" "time" + "github.com/kovidgoyal/imaging" "github.com/kovidgoyal/kitty/tools/cli" "github.com/kovidgoyal/kitty/tools/tty" "github.com/kovidgoyal/kitty/tools/tui" "github.com/kovidgoyal/kitty/tools/tui/graphics" "github.com/kovidgoyal/kitty/tools/utils" - "github.com/kovidgoyal/kitty/tools/utils/images" "github.com/kovidgoyal/kitty/tools/utils/style" "golang.org/x/sys/unix" @@ -31,7 +31,7 @@ type Place struct { var opts *Options var place *Place var z_index int32 -var remove_alpha *images.NRGBColor +var remove_alpha *imaging.NRGBColor var flip, flop bool type transfer_mode int @@ -68,7 +68,7 @@ func parse_background() (err error) { if err != nil { return fmt.Errorf("Invalid value for --background: %w", err) } - remove_alpha = &images.NRGBColor{R: col.Red, G: col.Green, B: col.Blue} + remove_alpha = &imaging.NRGBColor{R: col.Red, G: col.Green, B: col.Blue} return } diff --git a/kittens/icat/native.go b/kittens/icat/native.go index 1a83de8ee..1a07ac835 100644 --- a/kittens/icat/native.go +++ b/kittens/icat/native.go @@ -49,13 +49,13 @@ func add_frame(ctx *images.Context, imgd *image_data, img image.Image) *image_fr bytes_per_pixel := 4 if is_opaque || remove_alpha != nil { - var rgb *images.NRGB + var rgb *imaging.NRGB bytes_per_pixel = 3 m, err := shm.CreateTemp(shm_template, uint64(f.width*f.height*bytes_per_pixel)) if err != nil { - rgb = images.NewNRGB(dest_rect) + rgb = imaging.NewNRGB(dest_rect) } else { - rgb = &images.NRGB{Pix: m.Slice(), Stride: bytes_per_pixel * f.width, Rect: dest_rect} + rgb = &imaging.NRGB{Pix: m.Slice(), Stride: bytes_per_pixel * f.width, Rect: dest_rect} f.shm = m } f.transmission_format = graphics.GRT_format_rgb diff --git a/tools/utils/images/loading.go b/tools/utils/images/loading.go index e46e98f6c..448306cc7 100644 --- a/tools/utils/images/loading.go +++ b/tools/utils/images/loading.go @@ -85,7 +85,7 @@ func (self *ImageFrame) DataAsSHM(pattern string) (ans shm.MMap, err error) { return nil, err } switch img := self.Img.(type) { - case *NRGB: + case *imaging.NRGB: if bytes_per_pixel == 3 { copy(ans.Slice(), img.Pix) return @@ -100,7 +100,7 @@ func (self *ImageFrame) DataAsSHM(pattern string) (ans shm.MMap, err error) { var final_img image.Image switch bytes_per_pixel { case 3: - rgb := &NRGB{Pix: ans.Slice(), Stride: bytes_per_pixel * self.Width, Rect: dest_rect} + rgb := &imaging.NRGB{Pix: ans.Slice(), Stride: bytes_per_pixel * self.Width, Rect: dest_rect} final_img = rgb case 4: rgba := &image.NRGBA{Pix: ans.Slice(), Stride: bytes_per_pixel * self.Width, Rect: dest_rect} @@ -118,7 +118,7 @@ func (self *ImageFrame) Data() (ans []byte) { bytes_per_pixel = 3 } switch img := self.Img.(type) { - case *NRGB: + case *imaging.NRGB: if bytes_per_pixel == 3 { return img.Pix } @@ -131,7 +131,7 @@ func (self *ImageFrame) Data() (ans []byte) { var final_img image.Image switch bytes_per_pixel { case 3: - rgb := NewNRGB(dest_rect) + rgb := imaging.NewNRGB(dest_rect) final_img = rgb ans = rgb.Pix case 4: @@ -155,7 +155,7 @@ func ImageFrameFromSerialized(s SerializableImageFrame, data []byte) (aa *ImageF return nil, fmt.Errorf("serialized image data has size: %d != %d", len(data), expected) } if s.Is_opaque { - ans.Img, err = NewNRGBWithContiguousRGBPixels(data, s.Left, s.Top, s.Width, s.Height) + ans.Img, err = imaging.NewNRGBWithContiguousRGBPixels(data, s.Left, s.Top, s.Width, s.Height) } else { ans.Img, err = NewNRGBAWithContiguousRGBAPixels(data, s.Left, s.Top, s.Width, s.Height) } @@ -524,7 +524,7 @@ func IdentifyWithMagick(path string) (ans []IdentifyRecord, err error) { } type RenderOptions struct { - RemoveAlpha *NRGBColor + RemoveAlpha *imaging.NRGBColor Flip, Flop bool ResizeTo image.Point OnlyFirstFrame bool @@ -695,7 +695,7 @@ func OpenImageFromPathWithMagick(path string) (ans *ImageData, err error) { } dest_rect := image.Rect(0, 0, frame.Width, frame.Height) if frame.Is_opaque { - frame.Img = &NRGB{Pix: data, Stride: frame.Width * 3, Rect: dest_rect} + frame.Img = &imaging.NRGB{Pix: data, Stride: frame.Width * 3, Rect: dest_rect} } else { frame.Img = &image.NRGBA{Pix: data, Stride: frame.Width * 4, Rect: dest_rect} } diff --git a/tools/utils/images/opaque.go b/tools/utils/images/opaque.go index 6d62181dd..6ba1f10d9 100644 --- a/tools/utils/images/opaque.go +++ b/tools/utils/images/opaque.go @@ -5,6 +5,8 @@ package images import ( "fmt" "image" + + "github.com/kovidgoyal/imaging" ) var _ = fmt.Print @@ -64,7 +66,7 @@ func IsOpaque(img image.Image) bool { return i.Opaque() case *image.YCbCr: return i.Opaque() - case *NRGB: + case *imaging.NRGB: return i.Opaque() } return false diff --git a/tools/utils/images/to_rgb.go b/tools/utils/images/to_rgb.go index 4d4229e01..411bbe478 100644 --- a/tools/utils/images/to_rgb.go +++ b/tools/utils/images/to_rgb.go @@ -5,150 +5,16 @@ package images import ( "fmt" "image" - "image/color" + + "github.com/kovidgoyal/imaging" ) var _ = fmt.Print -type NRGBColor struct { - R, G, B uint8 -} - -func (c NRGBColor) AsSharp() string { - return fmt.Sprintf("#%02X%02X%02X", c.R, c.G, c.B) -} - -func (c NRGBColor) RGBA() (r, g, b, a uint32) { - r = uint32(c.R) - r |= r << 8 - g = uint32(c.G) - g |= g << 8 - b = uint32(c.B) - b |= b << 8 - a = 65535 // (255 << 8 | 255) - return -} - -// NRGB is an in-memory image whose At method returns NRGBColor values. -type NRGB struct { - // Pix holds the image's pixels, in R, G, B order. The pixel at - // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*3]. - Pix []uint8 - // Stride is the Pix stride (in bytes) between vertically adjacent pixels. - Stride int - // Rect is the image's bounds. - Rect image.Rectangle -} - -func nrgbModel(c color.Color) color.Color { - if _, ok := c.(NRGBColor); ok { - return c - } - r, g, b, a := c.RGBA() - switch a { - case 0xffff: - return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} - case 0: - return NRGBColor{0, 0, 0} - default: - // Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a. - r = (r * 0xffff) / a - g = (g * 0xffff) / a - b = (b * 0xffff) / a - return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} - } -} - -var NRGBModel color.Model = color.ModelFunc(nrgbModel) - -func (p *NRGB) ColorModel() color.Model { return NRGBModel } - -func (p *NRGB) Bounds() image.Rectangle { return p.Rect } - -func (p *NRGB) At(x, y int) color.Color { - return p.NRGBAt(x, y) -} - -func (p *NRGB) NRGBAt(x, y int) NRGBColor { - if !(image.Point{x, y}.In(p.Rect)) { - return NRGBColor{} - } - i := p.PixOffset(x, y) - s := p.Pix[i : i+3 : i+3] // Small cap improves performance, see https://golang.org/issue/27857 - return NRGBColor{s[0], s[1], s[2]} -} - -// PixOffset returns the index of the first element of Pix that corresponds to -// the pixel at (x, y). -func (p *NRGB) PixOffset(x, y int) int { - return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3 -} - -func (p *NRGB) Set(x, y int, c color.Color) { - if !(image.Point{x, y}.In(p.Rect)) { - return - } - i := p.PixOffset(x, y) - c1 := NRGBModel.Convert(c).(NRGBColor) - s := p.Pix[i : i+3 : i+3] // Small cap improves performance, see https://golang.org/issue/27857 - s[0] = c1.R - s[1] = c1.G - s[2] = c1.B -} - -func (p *NRGB) SetRGBA64(x, y int, c color.RGBA64) { - if !(image.Point{x, y}.In(p.Rect)) { - return - } - r, g, b, a := uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A) - if (a != 0) && (a != 0xffff) { - r = (r * 0xffff) / a - g = (g * 0xffff) / a - b = (b * 0xffff) / a - } - i := p.PixOffset(x, y) - s := p.Pix[i : i+3 : i+3] // Small cap improves performance, see https://golang.org/issue/27857 - s[0] = uint8(r >> 8) - s[1] = uint8(g >> 8) - s[2] = uint8(b >> 8) -} - -func (p *NRGB) SetNRGBA(x, y int, c color.NRGBA) { - if !(image.Point{x, y}.In(p.Rect)) { - return - } - i := p.PixOffset(x, y) - s := p.Pix[i : i+3 : i+3] // Small cap improves performance, see https://golang.org/issue/27857 - s[0] = c.R - s[1] = c.G - s[2] = c.B -} - -// SubImage returns an image representing the portion of the image p visible -// through r. The returned value shares pixels with the original image. -func (p *NRGB) SubImage(r image.Rectangle) image.Image { - r = r.Intersect(p.Rect) - // If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside - // either r1 or r2 if the intersection is empty. Without explicitly checking for - // this, the Pix[i:] expression below can panic. - if r.Empty() { - return &NRGB{} - } - i := p.PixOffset(r.Min.X, r.Min.Y) - return &NRGB{ - Pix: p.Pix[i:], - Stride: p.Stride, - Rect: r, - } -} - -// Opaque scans the entire image and reports whether it is fully opaque. -func (p *NRGB) Opaque() bool { return true } - type scanner_rgb struct { image image.Image w, h int - palette []NRGBColor + palette []imaging.NRGBColor opaque_base []float64 opaque_base_uint []uint8 } @@ -163,14 +29,14 @@ func blend(dest []uint8, base []float64, r, g, b, a uint8) { dest[2] = uint8(alpha*float64(b) + (1.0-alpha)*base[2]) } -func newScannerRGB(img image.Image, opaque_base NRGBColor) *scanner_rgb { +func newScannerRGB(img image.Image, opaque_base imaging.NRGBColor) *scanner_rgb { s := &scanner_rgb{ image: img, w: img.Bounds().Dx(), h: img.Bounds().Dy(), opaque_base: []float64{float64(opaque_base.R), float64(opaque_base.G), float64(opaque_base.B)}[0:3:3], opaque_base_uint: []uint8{opaque_base.R, opaque_base.G, opaque_base.B}[0:3:3], } if img, ok := img.(*image.Paletted); ok { - s.palette = make([]NRGBColor, max(256, len(img.Palette))) + s.palette = make([]imaging.NRGBColor, max(256, len(img.Palette))) d := [3]uint8{0, 0, 0} ds := d[:] for i := 0; i < len(img.Palette); i++ { @@ -179,10 +45,10 @@ func newScannerRGB(img image.Image, opaque_base NRGBColor) *scanner_rgb { case 0: s.palette[i] = opaque_base case 0xffff: - s.palette[i] = NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} + s.palette[i] = imaging.NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)} default: blend(ds, s.opaque_base, uint8((r*0xffff/a)>>8), uint8((g*0xffff/a)>>8), uint8((b*0xffff/a)>>8), uint8(a>>8)) - s.palette[i] = NRGBColor{d[0], d[1], d[2]} + s.palette[i] = imaging.NRGBColor{d[0], d[1], d[2]} } } } @@ -412,8 +278,8 @@ func (s *scanner_rgb) scan(x1, y1, x2, y2 int, dst []uint8) { } } -func (self *Context) paste_nrgb_onto_opaque(background *NRGB, img image.Image, pos image.Point, bgcol *NRGBColor) { - bg := NRGBColor{} +func (self *Context) paste_nrgb_onto_opaque(background *imaging.NRGB, img image.Image, pos image.Point, bgcol *imaging.NRGBColor) { + bg := imaging.NRGBColor{} if bgcol != nil { bg = *bgcol @@ -421,23 +287,3 @@ func (self *Context) paste_nrgb_onto_opaque(background *NRGB, img image.Image, p src := newScannerRGB(img, bg) self.run_paste(src, background, pos, func(dst []byte) {}) } - -func NewNRGB(r image.Rectangle) *NRGB { - return &NRGB{ - Pix: make([]uint8, 3*r.Dx()*r.Dy()), - Stride: 3 * r.Dx(), - Rect: r, - } -} - -func NewNRGBWithContiguousRGBPixels(p []byte, left, top, width, height int) (*NRGB, error) { - const bpp = 3 - if expected := bpp * width * height; expected != len(p) { - return nil, fmt.Errorf("the image width and height dont match the size of the specified pixel data: width=%d height=%d sz=%d != %d", width, height, len(p), expected) - } - return &NRGB{ - Pix: p, - Stride: bpp * width, - Rect: image.Rectangle{image.Point{left, top}, image.Point{left + width, top + height}}, - }, nil -} diff --git a/tools/utils/images/to_rgba.go b/tools/utils/images/to_rgba.go index eb4a635b9..512a584eb 100644 --- a/tools/utils/images/to_rgba.go +++ b/tools/utils/images/to_rgba.go @@ -7,6 +7,8 @@ import ( "image" "image/color" "math" + + "github.com/kovidgoyal/imaging" ) var _ = fmt.Print @@ -314,8 +316,8 @@ func (self *Context) run_paste(src Scanner, background image.Image, pos image.Po i := background.(*image.NRGBA) stride = i.Stride pix = i.Pix - case *NRGB: - i := background.(*NRGB) + case *imaging.NRGB: + i := background.(*imaging.NRGB) stride = i.Stride pix = i.Pix default: @@ -339,7 +341,7 @@ func (self *Context) run_paste(src Scanner, background image.Image, pos image.Po } -func (self *Context) paste_nrgba_onto_opaque(background *image.NRGBA, img image.Image, pos image.Point, bgcol *NRGBColor) { +func (self *Context) paste_nrgba_onto_opaque(background *image.NRGBA, img image.Image, pos image.Point, bgcol *imaging.NRGBColor) { src := newScanner(img) if bgcol == nil { self.run_paste(src, background, pos, func([]byte) {}) @@ -361,11 +363,11 @@ func (self *Context) paste_nrgba_onto_opaque(background *image.NRGBA, img image. } // Paste pastes the img image to the background image at the specified position. Optionally composing onto the specified opaque color. -func (self *Context) Paste(background image.Image, img image.Image, pos image.Point, opaque_bg *NRGBColor) { +func (self *Context) Paste(background image.Image, img image.Image, pos image.Point, opaque_bg *imaging.NRGBColor) { switch b := background.(type) { case *image.NRGBA: self.paste_nrgba_onto_opaque(b, img, pos, opaque_bg) - case *NRGB: + case *imaging.NRGB: self.paste_nrgb_onto_opaque(b, img, pos, opaque_bg) default: panic("Unsupported background image type") @@ -373,7 +375,7 @@ func (self *Context) Paste(background image.Image, img image.Image, pos image.Po } // PasteCenter pastes the img image to the center of the background image. Optionally composing onto the specified opaque color. -func (self *Context) PasteCenter(background image.Image, img image.Image, opaque_bg *NRGBColor) { +func (self *Context) PasteCenter(background image.Image, img image.Image, opaque_bg *imaging.NRGBColor) { bgBounds := background.Bounds() bgW := bgBounds.Dx() bgH := bgBounds.Dy()