Skip to content

Commit 1e16a5e

Browse files
committed
Improve correctness of Sixel background fill
1 parent 06f736b commit 1e16a5e

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/terminal/adapter/SixelParser.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ void SixelParser::_initRasterAttributes(const VTInt macroParameter, const Dispat
341341

342342
// By default, the filled area will cover the maximum extent allowed.
343343
_backgroundSize = { til::CoordTypeMax, til::CoordTypeMax };
344+
345+
// If the requested background area can not initially be filled, we'll
346+
// handle the rest of it on demand when the image is resized. But this
347+
// will be determined later in the _fillImageBackground method.
348+
_resizeFillRequired = false;
344349
}
345350

346351
void SixelParser::_updateRasterAttributes(const VTParameters& rasterAttributes)
@@ -647,6 +652,10 @@ void SixelParser::_resizeImageBuffer(const til::CoordType requiredHeight)
647652
{
648653
static constexpr auto transparentPixel = IndexedPixel{ .transparent = true };
649654
_imageBuffer.resize(requiredSize, transparentPixel);
655+
if (_resizeFillRequired)
656+
{
657+
_fillImageBackground(requiredHeight);
658+
}
650659
}
651660
}
652661

@@ -656,25 +665,30 @@ void SixelParser::_fillImageBackground()
656665
{
657666
_backgroundFillRequired = false;
658667

659-
const auto backgroundHeight = std::min(_backgroundSize.height, _availablePixelHeight);
660-
const auto backgroundWidth = std::min(_backgroundSize.width, _availablePixelWidth);
661-
_resizeImageBuffer(backgroundHeight);
662-
663668
// When a background fill is requested, we prefill the buffer with the 0
664669
// color index, up to the boundaries set by the raster attributes (or if
665-
// none were given, up to the page boundaries). The actual image output
666-
// 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-
}
670+
// none were given, up to the page boundaries). If the requested height
671+
// is more than the available height, we'll continue filling the rest of
672+
// it on demand when the image is resized (see above).
673+
const auto backgroundHeight = std::min(_backgroundSize.height, _availablePixelHeight);
674+
_resizeImageBuffer(backgroundHeight);
675+
_fillImageBackground(backgroundHeight);
676+
_resizeFillRequired = _backgroundSize.height > _availablePixelHeight;
677+
}
678+
}
675679

676-
_imageWidth = std::max(_imageWidth, backgroundWidth);
680+
void SixelParser::_fillImageBackground(const int backgroundHeight)
681+
{
682+
static constexpr auto backgroundPixel = IndexedPixel{};
683+
const auto backgroundWidth = std::min(_backgroundSize.width, _availablePixelWidth);
684+
const auto backgroundOffset = _imageCursor.y * _imageMaxWidth;
685+
auto dst = std::next(_imageBuffer.begin(), backgroundOffset);
686+
for (auto i = 0; i < backgroundHeight; i++)
687+
{
688+
std::fill_n(dst, backgroundWidth, backgroundPixel);
689+
std::advance(dst, _imageMaxWidth);
677690
}
691+
_imageWidth = std::max(_imageWidth, backgroundWidth);
678692
}
679693

680694
void SixelParser::_writeToImageBuffer(int sixelValue, int repeatCount)

src/terminal/adapter/SixelParser.hpp

Lines changed: 2 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+
bool _resizeFillRequired = false;
9293

9394
void _initColorMap(const VTParameter backgroundColor);
9495
void _defineColor(const VTParameters& colorParameters);
@@ -109,6 +110,7 @@ namespace Microsoft::Console::VirtualTerminal
109110
void _initImageBuffer();
110111
void _resizeImageBuffer(const til::CoordType requiredHeight);
111112
void _fillImageBackground();
113+
void _fillImageBackground(const int backgroundHeight);
112114
void _writeToImageBuffer(const int sixelValue, const int repeatCount);
113115
void _eraseImageBufferRows(const int rowCount, const til::CoordType startRow = 0) noexcept;
114116
void _maybeFlushImageBuffer(const bool endOfSequence = false);

0 commit comments

Comments
 (0)