Dears,

Here is a patch rerolling my previous (2013) attempt ¹ at displaying images 
*inside* urxvt, hopefully addressing all previous comments:
A. minimal impact to the codebase
B. no gdk pixbuf dependency
C. OSC definition (and layout semantics) on Perl's side


About layout (positioning/padding/inline/...):
- All the format-parsing are definitely better in Perl.

- IMG_EOL_FLAG (IMG_EOL Perl counterpart) defines whether an image sticks to 
the right most
border. It's the only layout/details flag that calls for screen.C consideration.
Other aspects (padding/size/ratio/wrap/pos/flow) are handled by the extension.

About Perl/core separation and trade-off:

- I went through various iterations and even made one reaching 90% Perl (only 
exposing
rxvt_term::scr_expose() and rxvt_term::scr_touch() + a 10 lines core-change) 
**but** it was
simply impossible to avoid glitches:

Resizing, rewrapping, relationship to cursor, ... imply many sequences and 
states deeply
nested inside src_*() functions: like copy_line(), scr_scroll_text() and even 
more
scr_refresh().

In one way or another tracking images across refreshes at the cell level or 
clearing areas
of images' previous position implied trespassing screen.C

That why this v2 only went from 780 LoC to "only" 500 LoC (+ 60 LoC for 3 xs 
bindings)


Other notes:
- _InTermImage was an initial attempt to make the feature more modular and 
self-contained.
But a better solution was to store images as attribute of a line_t were all the 
flags/col/width/height
could live and benefit from the existing line manipulation/display/refresh 
logic.

- The overlay code ended up not being a good fit because its purpose and 
mechanism are
entirely different (fixed area vs cell-based/tracking logic)

- Two semi-generic helpers added into rxvt_img (no specific `#if HAVE_IMAGES`)


Main limitation: interactivity:

The implementation assigns no ID to pixmaps.
C maintains persistence when row numbering changes but Perl barely knows the 
filename and
can't tells whether an image exists in the viewport or under a mouse click.

Having unique IDs to bridge both sides would efficiently provide this 
information, allowing
the Perl extension to add click handlers/action/file name copy/... features

Given the extensibility potential, it could be worth for the price of another 
`int` inside
line_image_t? (maybe the file's inode could be used)
(It would also have made the patch a bit more complex for another "extra" 
feature so I
preferred to ignore it for now)


New bindings:

rxvt_term::has_line_image (int row_number)
rxvt_term::set_line_image (int row_number, rxvt_img *img, int col = 0, int 
flags = 0)
rxvt_term::clear_line_image (int row_number)



¹ https://lists.schmorp.de/pipermail/rxvt-unicode/2013q1/001736.html
commit f99cadd9d8d89b641b47ad4a67fd66db36bf505e
Author: Raphaël Droz <[email protected]>
Date:   Fri Apr 24 20:50:22 2026 -0300

    balanced implementation: minimally intrusive C core, clean Perl interface

diff --git a/config.h.in b/config.h.in
index 18e6d566..dfd984b7 100644
--- a/config.h.in
+++ b/config.h.in
@@ -60,6 +60,9 @@
 /* Define to 1 if you have the <fontconfig/fontconfig.h> header file. */
 #undef HAVE_FONTCONFIG_FONTCONFIG_H
 
+/* Define if you want to display images inside urxvt */
+#undef HAVE_IMAGES
+
 /* Define to 1 if you have the `inotify_init' function. */
 #undef HAVE_INOTIFY_INIT
 
diff --git a/configure.ac b/configure.ac
index 7fc362bf..c8c35ecc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -236,6 +236,13 @@ AC_ARG_ENABLE(pixbuf,
     support_pixbuf=$enableval
   fi])
 
+AC_ARG_ENABLE(images,
+  [AS_HELP_STRING([--enable-images],
+     [enable the display of images inside the terminal])],
+  [if test x$enableval = xyes -o x$enableval = xno; then
+    AC_DEFINE(HAVE_IMAGES, 1, Define if you want to display images inside urxvt)
+  fi])
+
 AC_ARG_ENABLE(startup-notification,
   [AS_HELP_STRING([--enable-startup-notification],
      [enable freedesktop startup notification support])],
diff --git a/src/init.C b/src/init.C
index 907cdabe..f59983cc 100644
--- a/src/init.C
+++ b/src/init.C
@@ -598,6 +598,10 @@ rxvt_term::init_vars ()
   set_option (Opt_iso14755);
   set_option (Opt_iso14755_52);
   set_option (Opt_buffered);
+
+#if HAVE_IMAGES
+  line_images_need_expose = 0;
+#endif
 }
 
 #if ENABLE_PERL
diff --git a/src/rxvt.h b/src/rxvt.h
index 37aba23a..e8d2e5e1 100644
--- a/src/rxvt.h
+++ b/src/rxvt.h
@@ -717,6 +717,55 @@ typedef callback<int (int)> getfd_callback;
 #define LINE_COMPRESSED 0x0004 // line has been compressed (NYI)
 #define LINE_FILTER     0x0008 // line needs to be filtered before display (NYI)
 #define LINE_BIDI       0x0010 // line needs bidi (NYI)
+#define LINE_HAS_IMAGE  0x0020 // line has an attached image
+
+/*
+ * Line image attachment — stores an image as an attribute of a line_t.
+ * This is the key to surviving terminal resize: when lines rewrap,
+ * the image moves with them because it's part of the line structure,
+ * not stored at an absolute buffer position.
+ *
+ * Allocated on the heap (not in the line chunk) because most lines
+ * won't have images and we don't want to bloat every line_t.
+ */
+#if HAVE_IMAGES
+// IMG_EOL flag value for EOL images (must match Perl plugin constant)
+#define IMG_EOL_FLAG  0x0200
+
+struct line_image_t
+{
+   rxvt_img *img;         // XRender-backed image (refcounted via pixref)
+   line_image_t *next;    // next image in this line's linked list (NULL if last)
+   int16_t   col;         // column offset within the line (line-relative, not absolute)
+   uint16_t  flags;       // image display flags (IMG_EOL, etc.)
+   uint16_t  width;       // pixel width of the image
+   uint16_t  height;      // pixel height of the image
+
+   line_image_t ()
+   : img(0), next(0), col(0), flags(0), width(0), height(0)
+   {
+   }
+
+   ~line_image_t ()
+   {
+     // Only deletes this node; does NOT cascade to next.
+     // The caller must walk the list if freeing the whole chain.
+     if (img)
+       delete img;
+   }
+
+   // Free the entire linked list starting from this node
+   static void free_chain (line_image_t *head)
+   {
+     while (head)
+       {
+         line_image_t *n = head->next;
+         delete head;
+         head = n;
+       }
+   }
+};
+#endif
 
 struct line_t
 {
@@ -724,6 +773,9 @@ struct line_t
    rend_t *r; // rendition, uses RS_ flags
    tlen_t_ l; // length of each text line
    uint32_t f; // flags
+#if HAVE_IMAGES
+   line_image_t *line_img; // attached image (NULL if none) — survives rewrap
+#endif
 
    bool valid ()
    {
@@ -748,8 +800,63 @@ struct line_t
        f &= ~LINE_LONGER;
    }
 
+#if HAVE_IMAGES
+   bool has_image () const
+   {
+     return f & LINE_HAS_IMAGE;
+   }
+
+   // Replace the entire image chain with a single image. Takes ownership of li.
+   void set_image (line_image_t *li)
+   {
+     if (line_img)
+       line_image_t::free_chain (line_img);
+     line_img = li;
+     if (li)
+       f |= LINE_HAS_IMAGE;
+     else
+       f &= ~LINE_HAS_IMAGE;
+   }
+
+   // Append an image to the end of this line's image chain. Takes ownership of li.
+   void append_image (line_image_t *li)
+   {
+     if (!line_img)
+       {
+         line_img = li;
+       }
+     else
+       {
+         line_image_t *tail = line_img;
+         while (tail->next)
+           tail = tail->next;
+         tail->next = li;
+       }
+     f |= LINE_HAS_IMAGE;
+   }
+
+   // Detach and free all images from this line
+   void clear_image ()
+   {
+     if (line_img)
+       {
+         line_image_t::free_chain (line_img);
+         line_img = 0;
+       }
+     f &= ~LINE_HAS_IMAGE;
+   }
+#endif
+
    void clear ()
    {
+#if HAVE_IMAGES
+      // Free the entire image chain before clearing the line
+     if (line_img)
+       {
+         line_image_t::free_chain (line_img);
+         line_img = 0;
+       }
+#endif
      t = 0;
      r = 0;
      l = 0;
@@ -1209,6 +1316,33 @@ struct rxvt_term : zero_initialized, rxvt_vars, rxvt_screen
   void scr_overlay_set (int x, int y, const wchar_t *s) noexcept;
 #endif
 
+#if HAVE_IMAGES
+  /*
+   * Line-attribute image support.
+   * Images are attached to line_t structures via line_image_t pointers.
+   * This ensures images survive terminal resize/rewrap because they
+   * move with their parent line — no absolute row tracking needed.
+   *
+   * The expose system handles clearing old image pixels when scrolling
+   * causes images to move to new screen positions.
+   */
+
+  // Expose state for image areas that need redrawing after scroll/resize.
+  // 0 = no expose needed, 1 = partial expose, 2 = full screen expose.
+  uint16_t  line_images_need_expose;
+
+  // Render all line images that are currently visible on screen.
+  // Called at the end of scr_refresh() after text is drawn.
+  void render_line_images () noexcept;
+
+  // Set up expose regions before refresh when images have scrolled.
+  // Called from scr_changeview(), scr_scroll_text(), and ZERO_SCROLLBACK.
+  void line_images_set_expose (int old_view_start, int new_view_start) noexcept;
+
+  // Return true if any line in the visible range has an attached image.
+  bool has_visible_line_images () const noexcept;
+#endif
+
   vector<void *> allocated;           // free these memory blocks with free()
 
   int            parent_x, parent_y; // parent window position relative to root, only updated on demand
diff --git a/src/rxvtimg.C b/src/rxvtimg.C
index 971c6394..4e8c4d0b 100644
--- a/src/rxvtimg.C
+++ b/src/rxvtimg.C
@@ -982,5 +982,62 @@ rxvt_img::filter (const char *name, int nparams, nv *params)
   return cc;
 }
 
+void
+rxvt_img::composite_onto (Picture dst_pic, int src_x, int src_y,
+                           int dst_x, int dst_y, int cw, int ch, int op)
+{
+  Display *dpy = d->dpy;
+
+  XRenderPictureAttributes pa;
+  pa.repeat = RepeatNone;
+  Picture src_pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
+
+  XRenderComposite (dpy, op,
+                    src_pic, None, dst_pic,
+                    src_x, src_y,
+                    0, 0,
+                    dst_x, dst_y,
+                    cw, ch);
+
+  XRenderFreePicture (dpy, src_pic);
+}
+
+void
+rxvt_img::clipped_composite_onto (Picture dst_pic, int dst_x, int dst_y,
+                                   int clip_w, int clip_h, int op)
+{
+  int src_x = 0, src_y = 0;
+  int draw_w = w, draw_h = h;
+
+  // Clip top edge
+  if (dst_y < 0)
+    {
+      src_y = -dst_y;
+      draw_h += dst_y;
+      dst_y = 0;
+    }
+
+  // Clip left edge
+  if (dst_x < 0)
+    {
+      src_x = -dst_x;
+      draw_w += dst_x;
+      dst_x = 0;
+    }
+
+  // Clip bottom edge
+  if (dst_y + draw_h > clip_h)
+    draw_h = clip_h - dst_y;
+
+  // Clip right edge
+  if (dst_x + draw_w > clip_w)
+    draw_w = clip_w - dst_x;
+
+  if (draw_w <= 0 || draw_h <= 0)
+    return;
+
+  composite_onto (dst_pic, src_x, src_y, dst_x, dst_y, draw_w, draw_h, op);
+}
+
 #endif
 
diff --git a/src/rxvtimg.h b/src/rxvtimg.h
index d4c7eea3..e23853c0 100644
--- a/src/rxvtimg.h
+++ b/src/rxvtimg.h
@@ -112,6 +112,12 @@ struct rxvt_img
   }
 
   void draw (rxvt_img *img, int op = PictOpOver, nv mask = 1.);
+  void composite_onto (Picture dst, int src_x, int src_y,
+                       int dst_x, int dst_y, int w, int h,
+                       int op = PictOpOver);
+  void clipped_composite_onto (Picture dst, int dst_x, int dst_y,
+                               int clip_w, int clip_h,
+                               int op = PictOpOver);
 #if 0
   void draw (rxvt_img *img, int op = PictOpOver, nv mask = 1.,
              nv px, nv py, nv qx, nv qy, nv rx, nv ry, nv sx, nv sy);
diff --git a/src/rxvtperl.xs b/src/rxvtperl.xs
index 5303fa8e..124cf342 100644
--- a/src/rxvtperl.xs
+++ b/src/rxvtperl.xs
@@ -1796,6 +1796,65 @@ rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1)
         OUTPUT:
         RETVAL
 
+#if HAVE_IMAGES
+
+void
+rxvt_term::set_line_image (int row_number, rxvt_img *img, int col = 0, int flags = 0)
+	CODE:
+{
+        if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
+          croak ("set_line_image: row %d out of range (%d..%d)", row_number, THIS->top_row, THIS->nrow - 1);
+
+        line_t &l = ROW(row_number);
+
+        // Create a new line_image_t and clone the rxvt_img (we take ownership of the clone)
+        line_image_t *li = new line_image_t;
+        li->img    = img->clone ();
+        li->col    = col;
+        li->flags  = flags;
+        li->width  = img->w;
+        li->height = img->h;
+        li->next   = 0;
+
+        // Append to the line's image chain (supports multiple images per line)
+        l.append_image (li);
+
+        // Schedule expose so the image gets painted on next refresh
+        THIS->line_images_need_expose = 2;
+        THIS->want_refresh = 1;
+        THIS->refresh_check ();
+}
+
+void
+rxvt_term::clear_line_image (int row_number)
+	CODE:
+{
+        if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
+          croak ("clear_line_image: row %d out of range", row_number);
+
+        line_t &l = ROW(row_number);
+        l.clear_image ();
+
+        THIS->line_images_need_expose = 2;
+        THIS->want_refresh = 1;
+        THIS->refresh_check ();
+}
+
+bool
+rxvt_term::has_line_image (int row_number)
+	CODE:
+{
+        if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
+          XSRETURN_EMPTY;
+
+        line_t &l = ROW(row_number);
+        RETVAL = l.has_image ();
+}
+	OUTPUT:
+        RETVAL
+
+#endif
+
 SV *
 rxvt_term::special_encode (SV *string)
 	CODE:
diff --git a/src/screen.C b/src/screen.C
index 8fdfad9d..df674274 100644
--- a/src/screen.C
+++ b/src/screen.C
@@ -44,9 +44,20 @@ fill_text (text_t *start, text_t value, int len)
 /* ------------------------------------------------------------------------- *
  *             GENERAL SCREEN AND SELECTION UPDATE ROUTINES                  *
  * ------------------------------------------------------------------------- */
+#if HAVE_IMAGES
+// When line images are visible, a scroll-to-bottom must trigger an expose
+// to clear old image pixels before text is redrawn at new positions.
+#define ZERO_SCROLLBACK()                                              \
+    if (option (Opt_scrollTtyOutput) && view_start) {                  \
+        line_images_set_expose (view_start, 0);                        \
+        view_start = 0;                                                \
+    }
+#else
 #define ZERO_SCROLLBACK()                                              \
     if (option (Opt_scrollTtyOutput))                                  \
         view_start = 0
+#endif
+
 #define CLEAR_SELECTION()                                              \
     selection.beg.row = selection.beg.col                              \
         = selection.end.row = selection.end.col = 0
@@ -129,7 +140,18 @@ rxvt_term::scr_blank_screen_mem (line_t &l, rend_t efs) const noexcept
   scr_blank_line (l, 0, ncol, efs);
 
   l.l = 0;
+#if HAVE_IMAGES
+  // Preserve the LINE_HAS_IMAGE flag if line_img is set.
+  // scr_blank_screen_mem clears the TEXT content but must NOT destroy (yet)
+  // image attachments. images are managed by the Perl plugin and
+  // the C++ copy_line/rewrap code. Setting f=0 would orphan line_img.
+  if (l.line_img)
+    l.f = LINE_HAS_IMAGE; // preserve flag, clear everything else
+  else
+    l.f = 0;
+#else
   l.f = 0;
+#endif
 }
 
 // nuke a single wide character at the given column
@@ -194,6 +216,9 @@ rxvt_term::scr_alloc () noexcept
       l.r = (rend_t *)base; base += rsize;
       l.l = -1;
       l.f = 0;
+#if HAVE_IMAGES
+      l.line_img = 0; // no image attached to newly allocated lines
+#endif
     }
 
   drawn_buf = (line_t *)chunk;
@@ -209,6 +234,17 @@ rxvt_term::copy_line (line_t &dst, line_t &src)
   memcpy (dst.t, src.t, sizeof (text_t) * dst.l);
   memcpy (dst.r, src.r, sizeof (rend_t) * dst.l);
   dst.f = src.f;
+
+#if HAVE_IMAGES
+  // Transfer the image chain from src to dst via pointer move.
+  // This is safe because the old buffer (prev_row_buf) is freed with
+  // chunk_free() which only frees the line_t array memory, it does NOT
+  // call destructors or free heap-allocated line_image_t nodes.
+  // Moving the pointer avoids expensive rxvt_img::clone() which would
+  // create new X server pixmaps for each image on every resize.
+  dst.line_img = src.line_img;
+  src.line_img = 0;  // prevent double-free; old buffer no longer owns it
+#endif
 }
 
 void ecb_cold
@@ -289,6 +325,9 @@ rxvt_term::scr_reset ()
     {
       /*
        * add or delete rows as appropriate
+       * Note: line images are automatically preserved during resize because
+       * copy_line() transfers line_img pointers. No explicit image_recompute_pos
+       * is needed
        */
 
       int common_col = min (prev_ncol, ncol);
@@ -387,6 +426,44 @@ rxvt_term::scr_reset ()
               qline->l = llen ? MOD (llen - 1, ncol) + 1 : 0;
               qline->is_longer (0);
               scr_blank_line (*qline, qline->l, ncol - qline->l, DEFAULT_RSTYLE);
+
+#if HAVE_IMAGES
+              // Transfer image attachments during rewrap.
+              // Scan all source rows of this logical line and move any image
+              // found to the first destination row (q).
+              // This is critical for resize survival: the rewrap loop copies
+              // characters one-by-one with memcpy but never touches line_img.
+              // We must explicitly move it to the new row_buf.
+              // Transfer images from ALL source rows of this logical line
+              // to the first destination row (q) via pointer move.
+              // Safe because chunk_free() doesn't free heap line_image_t nodes.
+              {
+                int psrc = p;
+                for (int i = 0; i < plines; i++)
+                  {
+                    line_t &pline = prev_row_buf [MOD (psrc + i, prev_total_rows)];
+                    if (pline.line_img)
+                      {
+                        // Move the chain pointer, no clone needed
+                        line_image_t *chain = pline.line_img;
+                        pline.line_img = 0;  // old buffer no longer owns it
+                        // Append to destination row's existing chain
+                        if (!row_buf[q].line_img)
+                          {
+                            row_buf[q].line_img = chain;
+                            row_buf[q].f |= LINE_HAS_IMAGE;
+                          }
+                        else
+                          {
+                            line_image_t *tail = row_buf[q].line_img;
+                            while (tail->next)
+                              tail = tail->next;
+                            tail->next = chain;
+                          }
+                      }
+                  }
+              }
+#endif
             }
           while (p != pend && q > 0);
         }
@@ -425,6 +502,25 @@ rxvt_term::scr_reset ()
       if (!drawn_buf [row].valid ()) scr_blank_screen_mem (drawn_buf [row], DEFAULT_RSTYLE);
     }
 
+#if HAVE_IMAGES
+  // Free any orphaned line_image_t chains before releasing the old chunk.
+  // copy_line/rewrap moved images from visited rows (nulling the src pointer),
+  // but rows NOT visited (e.g., dropped scrollback on shrink) still have
+  // live line_img pointers.  chunk_free() only frees the flat line_t array;
+  // heap-allocated line_image_t nodes would be leaked without this sweep.
+  if (prev_chunk)
+  {
+    int prev_all_rows = prev_total_rows + prev_nrow + prev_nrow;
+    line_t *prev_lines = (line_t *)prev_chunk;
+    for (int i = 0; i < prev_all_rows; i++)
+      if (prev_lines[i].line_img)
+        {
+          line_image_t::free_chain (prev_lines[i].line_img);
+          prev_lines[i].line_img = 0;
+        }
+  }
+#endif
+
   chunk_free (prev_chunk, prev_chunk_size);
 
   free (tabs);
@@ -440,12 +536,42 @@ rxvt_term::scr_reset ()
 
   tt_winch ();
 
+#if HAVE_IMAGES
+  // After resize, EOL images reposition dynamically at render time.
+  // However, the old pixel area (where the image was before resize) still
+  // has stale content.  Schedule a full-screen repaint so that the old
+  // pixels get cleared before the image is drawn at its new position.
+  if (has_visible_line_images ())
+    {
+      line_images_need_expose = 2;
+      want_refresh = 1;
+    }
+
+#endif
+
   HOOK_INVOKE ((this, HOOK_RESET, DT_END));
 }
 
 void ecb_cold
 rxvt_term::scr_release () noexcept
 {
+#if HAVE_IMAGES
+  // Free all line_image_t chains before releasing the chunk.
+  // chunk_free() only frees the flat line_t array memory; without this,
+  // heap-allocated line_image_t nodes (and their X pixmaps) would leak.
+  if (chunk)
+    {
+      int all_rows = total_rows + nrow + nrow;
+      line_t *lines = (line_t *)chunk;
+      for (int i = 0; i < all_rows; i++)
+        if (lines[i].line_img)
+          {
+            line_image_t::free_chain (lines[i].line_img);
+            lines[i].line_img = 0;
+          }
+    }
+#endif
+
   chunk_free (chunk, chunk_size);
   chunk = 0;
   row_buf = 0;
@@ -654,6 +780,12 @@ rxvt_term::scr_scroll_text (int row1, int row2, int count) noexcept
   if (count == 0 || (row1 > row2))
     return 0;
 
+#if HAVE_IMAGES
+  // When text scrolls, images painted on-screen leave stale pixels.
+  // Schedule an expose to clear those areas before the next refresh.
+  line_images_set_expose (view_start, view_start + count);
+#endif
+
   want_refresh = 1;
   num_scr += count;
 
@@ -1403,6 +1535,12 @@ rxvt_term::scr_erase_screen (int mode) noexcept
 
   for (; num--; row++)
     {
+#if HAVE_IMAGES
+      // Screen erase must also remove images from affected lines.
+      // scr_blank_screen_mem preserves LINE_HAS_IMAGE by design (for resize),
+      // but an explicit screen `clear` should destroy images.
+      ROW(row).clear_image ();
+#endif
       scr_blank_screen_mem (ROW(row), rstyle);
 
       if (row - view_start < nrow)
@@ -1887,6 +2025,13 @@ rxvt_term::scr_expose (int x, int y, int ewidth, int eheight, bool refresh) noex
 
   num_scr_allow = 0;
 
+#if HAVE_IMAGES
+  // Avoid infinite loop: scr_expose() calls scr_refresh() which checks
+  // line_images_need_expose. Clear the flag here before the recursive call.
+  if (line_images_need_expose == 1)
+    line_images_need_expose = 0;
+#endif
+
   if (refresh)
     scr_refresh ();
 }
@@ -1932,6 +2077,12 @@ rxvt_term::scr_changeview (int new_view_start) noexcept
   if (new_view_start == view_start)
     return false;
 
+#if HAVE_IMAGES
+  // When scrolling, images on screen leave stale pixels behind.
+  // Schedule an expose to clear old image areas before refresh.
+  line_images_set_expose (view_start, new_view_start);
+#endif
+
   num_scr += new_view_start - view_start;
   view_start = new_view_start;
   want_refresh = 1;
@@ -2055,6 +2206,27 @@ rxvt_term::scr_refresh () noexcept
   int cur_col;
   int cursorwidth;
 
+#if HAVE_IMAGES
+  // Handle line image expose: when images have scrolled, we need to
+  // clear stale pixels before drawing new content.
+  if (line_images_need_expose == 2)
+    {
+      // Full screen expose needed (e.g. after resize or large scroll).
+      // Clear the flag BEFORE calling scr_touch to prevent infinite recursion.
+      line_images_need_expose = 0;
+      scr_touch (true);
+      return;
+    }
+  else if (line_images_need_expose == 1)
+    {
+      // Partial expose: do a full-screen expose for simplicity.
+      // The cost is negligible and avoids complex partial-region tracking.
+      line_images_need_expose = 0;
+      scr_touch (true);
+      return;
+    }
+#endif
+
   want_refresh = 0;        /* screen is current */
 
   if (refresh_type == NO_REFRESH || !mapped)
@@ -2493,6 +2665,12 @@ rxvt_term::scr_refresh () noexcept
 #endif
   HOOK_INVOKE ((this, HOOK_REFRESH_END, DT_END));
 
+#if HAVE_IMAGES
+  // Render all visible line images AFTER text has been drawn.
+  // Images are composited on top of the text using XRender.
+  render_line_images ();
+#endif
+
   scr_reverse_selection ();
 
   screen.flags = old_screen_flags;
@@ -3698,5 +3876,134 @@ rxvt_term::scr_swap_overlay () noexcept
 }
 
 #endif
+
+#if HAVE_IMAGES
+/*
+ * Line image expose system.
+ *
+ * When images scroll (due to scr_changeview, scr_scroll_text, or
+ * ZERO_SCROLLBACK), the old pixel area where an image was rendered
+ * becomes stale. We schedule an expose to clear those areas before
+ * the next scr_refresh() paints new content.
+ *
+ * Because images are line attributes (not absolute positions),
+ * we don't need to track which specific regions changed, we just
+ * need to know that *some* visible line has an image and the view
+ * moved.
+ *
+ * Values of line_images_need_expose:
+ *   0 = no expose needed
+ *   1 = partial expose (we simplify to full for robustness)
+ *   2 = full screen expose needed
+ */
+void
+rxvt_term::line_images_set_expose (int old_view_start, int new_view_start) noexcept
+{
+  // Already queued for full refresh, nothing more to do
+  if (line_images_need_expose == 2)
+    return;
+
+  // Only schedule expose if there are visible line images
+  if (!has_visible_line_images ())
+    return;
+
+  // Any scroll distance triggers a full expose for simplicity
+  if (old_view_start != new_view_start)
+    line_images_need_expose = 2;
+}
+
+/*
+ * Check if any line in the visible viewport has an attached image.
+ */
+bool
+rxvt_term::has_visible_line_images () const noexcept
+{
+  for (int row = top_row; row < nrow; row++)
+    {
+      // Use LINENO to map view-relative row to buffer index
+      int lineno = MOD (term_start + row, total_rows);
+      const line_t &l = row_buf[lineno];
+      if (l.f & LINE_HAS_IMAGE)
+        return true;
+    }
+  return false;
+}
+
+/*
+ * Render all visible line images.
+ *
+ * Called at the end of scr_refresh() after text has been drawn.
+ * For each visible line that has an image, composite the image
+ * at the correct screen position using XRender (via rxvt_img).
+ *
+ * Position calculation:
+ *   screen_row = (buffer_row - view_start) where buffer_row is view-relative
+ *   pixel_x = line_img->col * fwidth
+ *   pixel_y = screen_row * fheight
+ *
+ * The image is composited using XRenderComposite with PictOpOver
+ * so it properly handles alpha transparency.
+ */
+void
+rxvt_term::render_line_images () noexcept
+{
+  // Don't render images when on the alternate (secondary) screen,
+  // e.g. when ncurses/vim/htop is running.  Images are attached to
+  // primary-screen / scrollback rows only.
+  if (current_screen != PRIMARY)
+    return;
+
+  // We need to scan rows above the visible area because a tall image
+  // anchored to a row that has scrolled up may still be partially visible.
+  // Calculate the maximum number of extra rows to scan based on a
+  // reasonable max image height. We scan up to 200 rows above view_start,
+  // which covers images up to ~200*fheight pixels tall.
+  int scan_above = min (200, view_start - top_row);
+  int scan_start = view_start - scan_above;
+  int scan_end   = view_start + nrow;
+
+  // Create the destination Picture once for all images
+  XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, visual);
+  Picture dst_pic = XRenderCreatePicture (dpy, vt, dst_format, 0, 0);
+
+  for (int row = scan_start; row < scan_end; row++)
+    {
+      line_t &l = ROW(row);
+
+      if (!(l.f & LINE_HAS_IMAGE) || !l.line_img)
+        continue;
+
+      // Iterate the linked list of images attached to this line
+      for (line_image_t *li = l.line_img; li; li = li->next)
+        {
+          if (!li->img)
+            continue;
+
+          // EOL images dynamically reposition to the right edge.
+          // This happens at render time so it adapts to resize without
+          // needing to store the old ncol or pixel width in the line_image_t.
+          int img_col = li->col;
+          if (li->flags & IMG_EOL_FLAG)
+            {
+              int img_width_cols = ((int)li->width + fwidth - 1) / fwidth;
+              img_col = ncol - img_width_cols;
+              if (img_col < 0)
+                img_col = 0;
+            }
+
+          // Calculate screen position: row relative to top of visible area
+          int pixel_x = img_col * fwidth;
+          int pixel_y = (row - view_start) * fheight;
+
+          // Composite with automatic 4-edge clipping against the viewport
+          li->img->clipped_composite_onto (dst_pic, pixel_x, pixel_y,
+                                           vt_width, vt_height);
+        }
+    }
+
+  XRenderFreePicture (dpy, dst_pic);
+}
+#endif
+
 /* ------------------------------------------------------------------------- */
 
_______________________________________________
rxvt-unicode mailing list
[email protected]
http://lists.schmorp.de/mailman/listinfo/rxvt-unicode

Reply via email to