mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-18 22:44:50 +02:00
Work on adding support for rendering transforms
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user