native.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package icat
  3. import (
  4. "fmt"
  5. "image"
  6. "image/gif"
  7. "kitty/tools/tty"
  8. "kitty/tools/tui/graphics"
  9. "kitty/tools/utils"
  10. "kitty/tools/utils/images"
  11. "kitty/tools/utils/shm"
  12. "github.com/edwvee/exiffix"
  13. "github.com/kovidgoyal/imaging"
  14. )
  15. var _ = fmt.Print
  16. func resize_frame(imgd *image_data, img image.Image) (image.Image, image.Rectangle) {
  17. b := img.Bounds()
  18. left, top, width, height := b.Min.X, b.Min.Y, b.Dx(), b.Dy()
  19. new_width := int(imgd.scaled_frac.x * float64(width))
  20. new_height := int(imgd.scaled_frac.y * float64(height))
  21. img = imaging.Resize(img, new_width, new_height, imaging.Lanczos)
  22. newleft := int(imgd.scaled_frac.x * float64(left))
  23. newtop := int(imgd.scaled_frac.y * float64(top))
  24. return img, image.Rect(newleft, newtop, newleft+new_width, newtop+new_height)
  25. }
  26. const shm_template = "kitty-icat-*"
  27. func add_frame(ctx *images.Context, imgd *image_data, img image.Image) *image_frame {
  28. is_opaque := false
  29. if imgd.format_uppercase == "JPEG" {
  30. // special cased because EXIF orientation could have already changed this image to an NRGBA making IsOpaque() very slow
  31. is_opaque = true
  32. } else {
  33. is_opaque = images.IsOpaque(img)
  34. }
  35. b := img.Bounds()
  36. if imgd.scaled_frac.x != 0 {
  37. img, b = resize_frame(imgd, img)
  38. }
  39. f := image_frame{width: b.Dx(), height: b.Dy(), number: len(imgd.frames) + 1, left: b.Min.X, top: b.Min.Y}
  40. dest_rect := image.Rect(0, 0, f.width, f.height)
  41. var final_img image.Image
  42. bytes_per_pixel := 4
  43. if is_opaque || remove_alpha != nil {
  44. var rgb *images.NRGB
  45. bytes_per_pixel = 3
  46. m, err := shm.CreateTemp(shm_template, uint64(f.width*f.height*bytes_per_pixel))
  47. if err != nil {
  48. rgb = images.NewNRGB(dest_rect)
  49. } else {
  50. rgb = &images.NRGB{Pix: m.Slice(), Stride: bytes_per_pixel * f.width, Rect: dest_rect}
  51. f.shm = m
  52. }
  53. f.transmission_format = graphics.GRT_format_rgb
  54. f.in_memory_bytes = rgb.Pix
  55. final_img = rgb
  56. } else {
  57. var rgba *image.NRGBA
  58. m, err := shm.CreateTemp(shm_template, uint64(f.width*f.height*bytes_per_pixel))
  59. if err != nil {
  60. rgba = image.NewNRGBA(dest_rect)
  61. } else {
  62. rgba = &image.NRGBA{Pix: m.Slice(), Stride: bytes_per_pixel * f.width, Rect: dest_rect}
  63. f.shm = m
  64. }
  65. f.transmission_format = graphics.GRT_format_rgba
  66. f.in_memory_bytes = rgba.Pix
  67. final_img = rgba
  68. }
  69. ctx.PasteCenter(final_img, img, remove_alpha)
  70. imgd.frames = append(imgd.frames, &f)
  71. if flip {
  72. ctx.FlipPixelsV(bytes_per_pixel, f.width, f.height, f.in_memory_bytes)
  73. if f.height < imgd.canvas_height {
  74. f.top = (2*imgd.canvas_height - f.height - f.top) % imgd.canvas_height
  75. }
  76. }
  77. if flop {
  78. ctx.FlipPixelsH(bytes_per_pixel, f.width, f.height, f.in_memory_bytes)
  79. if f.width < imgd.canvas_width {
  80. f.left = (2*imgd.canvas_width - f.width - f.left) % imgd.canvas_width
  81. }
  82. }
  83. return &f
  84. }
  85. func scale_image(imgd *image_data) bool {
  86. if imgd.needs_scaling {
  87. width, height := imgd.canvas_width, imgd.canvas_height
  88. if imgd.canvas_width < imgd.available_width && opts.ScaleUp && place != nil {
  89. r := float64(imgd.available_width) / float64(imgd.canvas_width)
  90. imgd.canvas_width, imgd.canvas_height = imgd.available_width, int(r*float64(imgd.canvas_height))
  91. }
  92. neww, newh := images.FitImage(imgd.canvas_width, imgd.canvas_height, imgd.available_width, imgd.available_height)
  93. imgd.needs_scaling = false
  94. imgd.scaled_frac.x = float64(neww) / float64(width)
  95. imgd.scaled_frac.y = float64(newh) / float64(height)
  96. imgd.canvas_width = int(imgd.scaled_frac.x * float64(width))
  97. imgd.canvas_height = int(imgd.scaled_frac.y * float64(height))
  98. return true
  99. }
  100. return false
  101. }
  102. func load_one_frame_image(imgd *image_data, src *opened_input) (img image.Image, err error) {
  103. img, _, err = exiffix.Decode(src.file)
  104. src.Rewind()
  105. if err != nil {
  106. return
  107. }
  108. // reset the sizes as we read EXIF tags here which could have rotated the image
  109. imgd.canvas_width = img.Bounds().Dx()
  110. imgd.canvas_height = img.Bounds().Dy()
  111. set_basic_metadata(imgd)
  112. scale_image(imgd)
  113. return
  114. }
  115. var debugprintln = tty.DebugPrintln
  116. var _ = debugprintln
  117. func (frame *image_frame) set_disposal(anchor_frame int, disposal byte) int {
  118. anchor_frame, frame.compose_onto = images.SetGIFFrameDisposal(frame.number, anchor_frame, disposal)
  119. return anchor_frame
  120. }
  121. func (frame *image_frame) set_delay(gap, min_gap int) {
  122. frame.delay_ms = utils.Max(min_gap, gap) * 10
  123. if frame.delay_ms == 0 {
  124. frame.delay_ms = -1
  125. }
  126. }
  127. func add_gif_frames(ctx *images.Context, imgd *image_data, gf *gif.GIF) error {
  128. min_gap := images.CalcMinimumGIFGap(gf.Delay)
  129. scale_image(imgd)
  130. anchor_frame := 1
  131. for i, paletted_img := range gf.Image {
  132. frame := add_frame(ctx, imgd, paletted_img)
  133. frame.set_delay(gf.Delay[i], min_gap)
  134. anchor_frame = frame.set_disposal(anchor_frame, gf.Disposal[i])
  135. }
  136. return nil
  137. }
  138. func render_image_with_go(imgd *image_data, src *opened_input) (err error) {
  139. ctx := images.Context{}
  140. switch {
  141. case imgd.format_uppercase == "GIF" && opts.Loop != 0:
  142. gif_frames, err := gif.DecodeAll(src.file)
  143. src.Rewind()
  144. if err != nil {
  145. return fmt.Errorf("Failed to decode GIF file with error: %w", err)
  146. }
  147. err = add_gif_frames(&ctx, imgd, gif_frames)
  148. if err != nil {
  149. return err
  150. }
  151. default:
  152. img, err := load_one_frame_image(imgd, src)
  153. if err != nil {
  154. return err
  155. }
  156. add_frame(&ctx, imgd, img)
  157. }
  158. return nil
  159. }