magick.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package icat
  3. import (
  4. "fmt"
  5. "kitty/tools/tui/graphics"
  6. "kitty/tools/utils/images"
  7. )
  8. var _ = fmt.Print
  9. func Render(path string, ro *images.RenderOptions, frames []images.IdentifyRecord) (ans []*image_frame, err error) {
  10. ro.TempfilenameTemplate = shm_template
  11. image_frames, filenames, err := images.RenderWithMagick(path, ro, frames)
  12. if err == nil {
  13. ans = make([]*image_frame, len(image_frames))
  14. for i, x := range image_frames {
  15. ans[i] = &image_frame{
  16. filename: filenames[x.Number], filename_is_temporary: true,
  17. number: x.Number, width: x.Width, height: x.Height, left: x.Left, top: x.Top,
  18. transmission_format: graphics.GRT_format_rgba, delay_ms: int(x.Delay_ms), compose_onto: x.Compose_onto,
  19. }
  20. if x.Is_opaque {
  21. ans[i].transmission_format = graphics.GRT_format_rgb
  22. }
  23. }
  24. }
  25. return ans, err
  26. }
  27. func render_image_with_magick(imgd *image_data, src *opened_input) (err error) {
  28. err = src.PutOnFilesystem()
  29. if err != nil {
  30. return err
  31. }
  32. frames, err := images.IdentifyWithMagick(src.FileSystemName())
  33. if err != nil {
  34. return err
  35. }
  36. imgd.format_uppercase = frames[0].Fmt_uppercase
  37. imgd.canvas_width, imgd.canvas_height = frames[0].Canvas.Width, frames[0].Canvas.Height
  38. set_basic_metadata(imgd)
  39. if !imgd.needs_conversion {
  40. make_output_from_input(imgd, src)
  41. return nil
  42. }
  43. ro := images.RenderOptions{RemoveAlpha: remove_alpha, Flip: flip, Flop: flop}
  44. if scale_image(imgd) {
  45. ro.ResizeTo.X, ro.ResizeTo.Y = imgd.canvas_width, imgd.canvas_height
  46. }
  47. imgd.frames, err = Render(src.FileSystemName(), &ro, frames)
  48. if err != nil {
  49. return err
  50. }
  51. return nil
  52. }