Bump version of imaging

This commit is contained in:
Kovid Goyal
2025-10-11 13:17:15 +05:30
parent 3ba6c3f4b9
commit 4c0c5c01f2
8 changed files with 36 additions and 186 deletions

2
go.mod
View File

@@ -13,7 +13,7 @@ require (
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/kovidgoyal/dbus v0.0.0-20250519011319-e811c41c0bc1 github.com/kovidgoyal/dbus v0.0.0-20250519011319-e811c41c0bc1
github.com/kovidgoyal/exiffix v0.0.0-20250919160812-dbef770c2032 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/seancfoley/ipaddress-go v1.7.1
github.com/shirou/gopsutil/v3 v3.24.5 github.com/shirou/gopsutil/v3 v3.24.5
github.com/zeebo/xxh3 v1.0.2 github.com/zeebo/xxh3 v1.0.2

4
go.sum
View File

@@ -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/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 h1:TEV9lpo2a6fP1byiDsoJe2fXpvrj2itae41xMM+bEAg=
github.com/kovidgoyal/exiffix v0.0.0-20250919160812-dbef770c2032/go.mod h1:VU38Nlbvb0lbyS5YkopCZMS5HuJ5QLVJBxRWyzq79q4= 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.7.0 h1:SFWQPJdEuaRY7WhL+MJyoN5IvslgqU+z0iPaoGl3yuM=
github.com/kovidgoyal/imaging v1.6.5/go.mod h1:mBprO214rATK/6OaPAUXmHbSMelPSFEmoBAt/IJdmno= 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 h1:N9zuLhTvBSRt0gWSiJswwQ2HqDmtX/ZCDJURnKUt1Ik=
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a/go.mod h1:JKx41uQRwqlTZabZc+kILPrO/3jlKnQ2Z8b7YiVw5cE= 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= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@@ -11,12 +11,12 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/kovidgoyal/imaging"
"github.com/kovidgoyal/kitty/tools/cli" "github.com/kovidgoyal/kitty/tools/cli"
"github.com/kovidgoyal/kitty/tools/tty" "github.com/kovidgoyal/kitty/tools/tty"
"github.com/kovidgoyal/kitty/tools/tui" "github.com/kovidgoyal/kitty/tools/tui"
"github.com/kovidgoyal/kitty/tools/tui/graphics" "github.com/kovidgoyal/kitty/tools/tui/graphics"
"github.com/kovidgoyal/kitty/tools/utils" "github.com/kovidgoyal/kitty/tools/utils"
"github.com/kovidgoyal/kitty/tools/utils/images"
"github.com/kovidgoyal/kitty/tools/utils/style" "github.com/kovidgoyal/kitty/tools/utils/style"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@@ -31,7 +31,7 @@ type Place struct {
var opts *Options var opts *Options
var place *Place var place *Place
var z_index int32 var z_index int32
var remove_alpha *images.NRGBColor var remove_alpha *imaging.NRGBColor
var flip, flop bool var flip, flop bool
type transfer_mode int type transfer_mode int
@@ -68,7 +68,7 @@ func parse_background() (err error) {
if err != nil { if err != nil {
return fmt.Errorf("Invalid value for --background: %w", err) 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 return
} }

View File

@@ -49,13 +49,13 @@ func add_frame(ctx *images.Context, imgd *image_data, img image.Image) *image_fr
bytes_per_pixel := 4 bytes_per_pixel := 4
if is_opaque || remove_alpha != nil { if is_opaque || remove_alpha != nil {
var rgb *images.NRGB var rgb *imaging.NRGB
bytes_per_pixel = 3 bytes_per_pixel = 3
m, err := shm.CreateTemp(shm_template, uint64(f.width*f.height*bytes_per_pixel)) m, err := shm.CreateTemp(shm_template, uint64(f.width*f.height*bytes_per_pixel))
if err != nil { if err != nil {
rgb = images.NewNRGB(dest_rect) rgb = imaging.NewNRGB(dest_rect)
} else { } 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.shm = m
} }
f.transmission_format = graphics.GRT_format_rgb f.transmission_format = graphics.GRT_format_rgb

View File

@@ -85,7 +85,7 @@ func (self *ImageFrame) DataAsSHM(pattern string) (ans shm.MMap, err error) {
return nil, err return nil, err
} }
switch img := self.Img.(type) { switch img := self.Img.(type) {
case *NRGB: case *imaging.NRGB:
if bytes_per_pixel == 3 { if bytes_per_pixel == 3 {
copy(ans.Slice(), img.Pix) copy(ans.Slice(), img.Pix)
return return
@@ -100,7 +100,7 @@ func (self *ImageFrame) DataAsSHM(pattern string) (ans shm.MMap, err error) {
var final_img image.Image var final_img image.Image
switch bytes_per_pixel { switch bytes_per_pixel {
case 3: 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 final_img = rgb
case 4: case 4:
rgba := &image.NRGBA{Pix: ans.Slice(), Stride: bytes_per_pixel * self.Width, Rect: dest_rect} 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 bytes_per_pixel = 3
} }
switch img := self.Img.(type) { switch img := self.Img.(type) {
case *NRGB: case *imaging.NRGB:
if bytes_per_pixel == 3 { if bytes_per_pixel == 3 {
return img.Pix return img.Pix
} }
@@ -131,7 +131,7 @@ func (self *ImageFrame) Data() (ans []byte) {
var final_img image.Image var final_img image.Image
switch bytes_per_pixel { switch bytes_per_pixel {
case 3: case 3:
rgb := NewNRGB(dest_rect) rgb := imaging.NewNRGB(dest_rect)
final_img = rgb final_img = rgb
ans = rgb.Pix ans = rgb.Pix
case 4: 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) return nil, fmt.Errorf("serialized image data has size: %d != %d", len(data), expected)
} }
if s.Is_opaque { 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 { } else {
ans.Img, err = NewNRGBAWithContiguousRGBAPixels(data, s.Left, s.Top, s.Width, s.Height) 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 { type RenderOptions struct {
RemoveAlpha *NRGBColor RemoveAlpha *imaging.NRGBColor
Flip, Flop bool Flip, Flop bool
ResizeTo image.Point ResizeTo image.Point
OnlyFirstFrame bool OnlyFirstFrame bool
@@ -695,7 +695,7 @@ func OpenImageFromPathWithMagick(path string) (ans *ImageData, err error) {
} }
dest_rect := image.Rect(0, 0, frame.Width, frame.Height) dest_rect := image.Rect(0, 0, frame.Width, frame.Height)
if frame.Is_opaque { 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 { } else {
frame.Img = &image.NRGBA{Pix: data, Stride: frame.Width * 4, Rect: dest_rect} frame.Img = &image.NRGBA{Pix: data, Stride: frame.Width * 4, Rect: dest_rect}
} }

View File

@@ -5,6 +5,8 @@ package images
import ( import (
"fmt" "fmt"
"image" "image"
"github.com/kovidgoyal/imaging"
) )
var _ = fmt.Print var _ = fmt.Print
@@ -64,7 +66,7 @@ func IsOpaque(img image.Image) bool {
return i.Opaque() return i.Opaque()
case *image.YCbCr: case *image.YCbCr:
return i.Opaque() return i.Opaque()
case *NRGB: case *imaging.NRGB:
return i.Opaque() return i.Opaque()
} }
return false return false

View File

@@ -5,150 +5,16 @@ package images
import ( import (
"fmt" "fmt"
"image" "image"
"image/color"
"github.com/kovidgoyal/imaging"
) )
var _ = fmt.Print 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 { type scanner_rgb struct {
image image.Image image image.Image
w, h int w, h int
palette []NRGBColor palette []imaging.NRGBColor
opaque_base []float64 opaque_base []float64
opaque_base_uint []uint8 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]) 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{ s := &scanner_rgb{
image: img, w: img.Bounds().Dx(), h: img.Bounds().Dy(), 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: []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], opaque_base_uint: []uint8{opaque_base.R, opaque_base.G, opaque_base.B}[0:3:3],
} }
if img, ok := img.(*image.Paletted); ok { 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} d := [3]uint8{0, 0, 0}
ds := d[:] ds := d[:]
for i := 0; i < len(img.Palette); i++ { for i := 0; i < len(img.Palette); i++ {
@@ -179,10 +45,10 @@ func newScannerRGB(img image.Image, opaque_base NRGBColor) *scanner_rgb {
case 0: case 0:
s.palette[i] = opaque_base s.palette[i] = opaque_base
case 0xffff: 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: default:
blend(ds, s.opaque_base, uint8((r*0xffff/a)>>8), uint8((g*0xffff/a)>>8), uint8((b*0xffff/a)>>8), uint8(a>>8)) 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) { func (self *Context) paste_nrgb_onto_opaque(background *imaging.NRGB, img image.Image, pos image.Point, bgcol *imaging.NRGBColor) {
bg := NRGBColor{} bg := imaging.NRGBColor{}
if bgcol != nil { if bgcol != nil {
bg = *bgcol bg = *bgcol
@@ -421,23 +287,3 @@ func (self *Context) paste_nrgb_onto_opaque(background *NRGB, img image.Image, p
src := newScannerRGB(img, bg) src := newScannerRGB(img, bg)
self.run_paste(src, background, pos, func(dst []byte) {}) 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
}

View File

@@ -7,6 +7,8 @@ import (
"image" "image"
"image/color" "image/color"
"math" "math"
"github.com/kovidgoyal/imaging"
) )
var _ = fmt.Print var _ = fmt.Print
@@ -314,8 +316,8 @@ func (self *Context) run_paste(src Scanner, background image.Image, pos image.Po
i := background.(*image.NRGBA) i := background.(*image.NRGBA)
stride = i.Stride stride = i.Stride
pix = i.Pix pix = i.Pix
case *NRGB: case *imaging.NRGB:
i := background.(*NRGB) i := background.(*imaging.NRGB)
stride = i.Stride stride = i.Stride
pix = i.Pix pix = i.Pix
default: 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) src := newScanner(img)
if bgcol == nil { if bgcol == nil {
self.run_paste(src, background, pos, func([]byte) {}) 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. // 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) { switch b := background.(type) {
case *image.NRGBA: case *image.NRGBA:
self.paste_nrgba_onto_opaque(b, img, pos, opaque_bg) 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) self.paste_nrgb_onto_opaque(b, img, pos, opaque_bg)
default: default:
panic("Unsupported background image type") 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. // 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() bgBounds := background.Bounds()
bgW := bgBounds.Dx() bgW := bgBounds.Dx()
bgH := bgBounds.Dy() bgH := bgBounds.Dy()