Skip to content

Commit 557a193

Browse files
authored
Improve correctness of Sixel background fill (#18260)
This is an attempt to improve the correctness of the background fill that the Sixel parser performs when an image is scrolled because it doesn't fit on the screen. This new behavior may not be exactly correct, but does at least match the VT330 and VT340 hardware more closely than it did before. The initial Sixel parser implementation was in PR #17421. When a Sixel image has the background select parameter set to 0 or 2, it fills the background up to the active raster attribute dimensions prior to writing out any actual pixel data. But this fill operation is clamped at the boundaries of the screen, so if the image doesn't fit, and needs to scroll, we have to perform an additional fill at that point to cover the background of the newly revealed rows (this is something we weren't doing before). This later fill uses the width of the most recent raster attributes command, which is not necessarily the same as the initial background fill, and fills the entire height of the new rows, regardless of the height specified in the raster attributes command. ## Validation Steps Performed Thanks to @al20878 and @hackerb9, we've been able to test on both a VT330 and a VT340, and I've confirmed that we're matching the behavior of those terminals as closely as possible. There are some edge cases where they don't agree with each other, so we can't always match both. I've also confirmed that the test case in issue #17946 now matches what the OP was expecting, and the test case in #17887 at least works more consistently now when scrolling (this is not what the OP was expecting though). Closes #17887 Closes #17946
1 parent 4abc041 commit 557a193

File tree

2 files changed

+71
-13
lines changed

2 files changed

+71
-13
lines changed

src/terminal/adapter/SixelParser.cpp

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ void SixelParser::_executeNextLine()
237237
_imageCursor.y += _sixelHeight;
238238
_availablePixelHeight -= _sixelHeight;
239239
_resizeImageBuffer(_sixelHeight);
240+
_fillImageBackgroundWhenScrolled();
240241
}
241242

242243
void SixelParser::_executeMoveToHome()
@@ -341,6 +342,12 @@ void SixelParser::_initRasterAttributes(const VTInt macroParameter, const Dispat
341342

342343
// By default, the filled area will cover the maximum extent allowed.
343344
_backgroundSize = { til::CoordTypeMax, til::CoordTypeMax };
345+
346+
// If the image ends up extending beyond the bottom of the page, we may need
347+
// to perform additional background filling as the screen is scrolled, which
348+
// requires us to track the area filled so far. This will be initialized, if
349+
// necessary, in the _fillImageBackground method below.
350+
_filledBackgroundHeight = std::nullopt;
344351
}
345352

346353
void SixelParser::_updateRasterAttributes(const VTParameters& rasterAttributes)
@@ -373,6 +380,15 @@ void SixelParser::_updateRasterAttributes(const VTParameters& rasterAttributes)
373380
// back to the dimensions from an earlier raster attributes command.
374381
_backgroundSize.width = width > 0 ? width : _backgroundSize.width;
375382
_backgroundSize.height = height > 0 ? height : _backgroundSize.height;
383+
384+
// If the aspect ratio has changed, the image height may increase, and that
385+
// could potentially trigger a scroll requiring the background to be filled.
386+
_fillImageBackgroundWhenScrolled();
387+
388+
// And while not documented, we know from testing on a VT330 that the raster
389+
// attributes command should also trigger a carriage return. This applies
390+
// regardless of whether the requested aspect ratio is valid or not.
391+
_executeCarriageReturn();
376392
}
377393

378394
void SixelParser::_scrollTextBuffer(Page& page, const int scrollAmount)
@@ -656,24 +672,60 @@ void SixelParser::_fillImageBackground()
656672
{
657673
_backgroundFillRequired = false;
658674

659-
const auto backgroundHeight = std::min(_backgroundSize.height, _availablePixelHeight);
660-
const auto backgroundWidth = std::min(_backgroundSize.width, _availablePixelWidth);
661-
_resizeImageBuffer(backgroundHeight);
662-
663675
// When a background fill is requested, we prefill the buffer with the 0
664676
// color index, up to the boundaries set by the raster attributes (or if
665677
// none were given, up to the page boundaries). The actual image output
666678
// isn't limited by the background dimensions though.
667-
static constexpr auto backgroundPixel = IndexedPixel{};
668-
const auto backgroundOffset = _imageCursor.y * _imageMaxWidth;
669-
auto dst = std::next(_imageBuffer.begin(), backgroundOffset);
670-
for (auto i = 0; i < backgroundHeight; i++)
671-
{
672-
std::fill_n(dst, backgroundWidth, backgroundPixel);
673-
std::advance(dst, _imageMaxWidth);
674-
}
679+
const auto backgroundHeight = std::min(_backgroundSize.height, _availablePixelHeight);
680+
_resizeImageBuffer(backgroundHeight);
681+
_fillImageBackground(backgroundHeight);
682+
// When the image extends beyond the page boundaries, and the screen is
683+
// scrolled, we also need to fill the newly exposed lines, so we keep a
684+
// record of the area filled so far. Initially this is considered to be
685+
// the available height, even if it wasn't all filled to start with.
686+
_filledBackgroundHeight = _imageCursor.y + _availablePixelHeight;
687+
_fillImageBackgroundWhenScrolled();
688+
}
689+
}
690+
691+
void SixelParser::_fillImageBackground(const int backgroundHeight)
692+
{
693+
static constexpr auto backgroundPixel = IndexedPixel{};
694+
const auto backgroundWidth = std::min(_backgroundSize.width, _availablePixelWidth);
695+
const auto backgroundOffset = _imageCursor.y * _imageMaxWidth;
696+
auto dst = std::next(_imageBuffer.begin(), backgroundOffset);
697+
for (auto i = 0; i < backgroundHeight; i++)
698+
{
699+
std::fill_n(dst, backgroundWidth, backgroundPixel);
700+
std::advance(dst, _imageMaxWidth);
701+
}
702+
_imageWidth = std::max(_imageWidth, backgroundWidth);
703+
}
675704

676-
_imageWidth = std::max(_imageWidth, backgroundWidth);
705+
void SixelParser::_fillImageBackgroundWhenScrolled()
706+
{
707+
// If _filledBackgroundHeight is set, that means a background fill has been
708+
// requested, and we need to extend that area whenever the image is about to
709+
// overrun it. The newly filled area is a multiple of the cell height (this
710+
// is to match the behavior of the original hardware terminals).
711+
const auto imageHeight = _imageCursor.y + _sixelHeight;
712+
if (_filledBackgroundHeight && imageHeight > _filledBackgroundHeight) [[unlikely]]
713+
{
714+
_filledBackgroundHeight = (imageHeight + _cellSize.height - 1) / _cellSize.height * _cellSize.height;
715+
const auto additionalFillHeight = *_filledBackgroundHeight - _imageCursor.y;
716+
_resizeImageBuffer(additionalFillHeight);
717+
_fillImageBackground(additionalFillHeight);
718+
}
719+
}
720+
721+
void SixelParser::_decreaseFilledBackgroundHeight(const int decreasedHeight) noexcept
722+
{
723+
// Sometimes the top of the image buffer may be clipped (e.g. when the image
724+
// scrolls off the top of a margin area). When that occurs, our record of
725+
// the filled height will need to be decreased to account for the new start.
726+
if (_filledBackgroundHeight) [[unlikely]]
727+
{
728+
_filledBackgroundHeight = *_filledBackgroundHeight - decreasedHeight;
677729
}
678730
}
679731

@@ -717,11 +769,13 @@ void SixelParser::_eraseImageBufferRows(const int rowCount, const til::CoordType
717769
const auto bufferOffsetEnd = bufferOffset + pixelCount * _imageMaxWidth;
718770
if (static_cast<size_t>(bufferOffsetEnd) >= _imageBuffer.size()) [[unlikely]]
719771
{
772+
_decreaseFilledBackgroundHeight(_imageCursor.y);
720773
_imageBuffer.clear();
721774
_imageCursor.y = 0;
722775
}
723776
else
724777
{
778+
_decreaseFilledBackgroundHeight(pixelCount);
725779
_imageBuffer.erase(_imageBuffer.begin() + bufferOffset, _imageBuffer.begin() + bufferOffsetEnd);
726780
_imageCursor.y -= pixelCount;
727781
}

src/terminal/adapter/SixelParser.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ namespace Microsoft::Console::VirtualTerminal
8989
til::CoordType _pendingTextScrollCount = 0;
9090
til::size _backgroundSize;
9191
bool _backgroundFillRequired = false;
92+
std::optional<til::CoordType> _filledBackgroundHeight;
9293

9394
void _initColorMap(const VTParameter backgroundColor);
9495
void _defineColor(const VTParameters& colorParameters);
@@ -109,6 +110,9 @@ namespace Microsoft::Console::VirtualTerminal
109110
void _initImageBuffer();
110111
void _resizeImageBuffer(const til::CoordType requiredHeight);
111112
void _fillImageBackground();
113+
void _fillImageBackground(const int backgroundHeight);
114+
void _fillImageBackgroundWhenScrolled();
115+
void _decreaseFilledBackgroundHeight(const int decreasedHeight) noexcept;
112116
void _writeToImageBuffer(const int sixelValue, const int repeatCount);
113117
void _eraseImageBufferRows(const int rowCount, const til::CoordType startRow = 0) noexcept;
114118
void _maybeFlushImageBuffer(const bool endOfSequence = false);

0 commit comments

Comments
 (0)