diff --git a/esphome/components/mipi_dsi/mipi_dsi.cpp b/esphome/components/mipi_dsi/mipi_dsi.cpp index 2aa7029ff6c..9753b6db609 100644 --- a/esphome/components/mipi_dsi/mipi_dsi.cpp +++ b/esphome/components/mipi_dsi/mipi_dsi.cpp @@ -302,10 +302,22 @@ void MipiDsi::mark_dirty_(int x_start, int y_start, int w, int h) { this->rotate_(x2, y2); const int right = static_cast(this->width_) - 1; const int bottom = static_cast(this->height_) - 1; - this->x_low_ = std::min(this->x_low_, std::clamp(std::min(x1, x2), 0, right)); - this->x_high_ = std::max(this->x_high_, std::clamp(std::max(x1, x2), 0, right)); - this->y_low_ = std::min(this->y_low_, std::clamp(std::min(y1, y2), 0, bottom)); - this->y_high_ = std::max(this->y_high_, std::clamp(std::max(y1, y2), 0, bottom)); + const int left = std::clamp(std::min(x1, x2), 0, right); + const int top = std::clamp(std::min(y1, y2), 0, bottom); + x2 = std::clamp(std::max(x1, x2), 0, right); + y2 = std::clamp(std::max(y1, y2), 0, bottom); + // An empty box is low above high, so it is replaced rather than widened + if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_) { + this->x_low_ = left; + this->x_high_ = x2; + this->y_low_ = top; + this->y_high_ = y2; + return; + } + this->x_low_ = std::min(this->x_low_, left); + this->x_high_ = std::max(this->x_high_, x2); + this->y_low_ = std::min(this->y_low_, top); + this->y_high_ = std::max(this->y_high_, y2); } void MipiDsi::draw_pixel_at(int x, int y, Color color) { diff --git a/esphome/components/mipi_rgb/mipi_rgb.cpp b/esphome/components/mipi_rgb/mipi_rgb.cpp index 4494f98ed33..08bbf31282c 100644 --- a/esphome/components/mipi_rgb/mipi_rgb.cpp +++ b/esphome/components/mipi_rgb/mipi_rgb.cpp @@ -297,10 +297,22 @@ void MipiRgb::mark_dirty_(int x_start, int y_start, int w, int h) { this->rotate_(x2, y2); const int right = static_cast(this->width_) - 1; const int bottom = static_cast(this->height_) - 1; - this->x_low_ = std::min(this->x_low_, std::clamp(std::min(x1, x2), 0, right)); - this->x_high_ = std::max(this->x_high_, std::clamp(std::max(x1, x2), 0, right)); - this->y_low_ = std::min(this->y_low_, std::clamp(std::min(y1, y2), 0, bottom)); - this->y_high_ = std::max(this->y_high_, std::clamp(std::max(y1, y2), 0, bottom)); + const int left = std::clamp(std::min(x1, x2), 0, right); + const int top = std::clamp(std::min(y1, y2), 0, bottom); + x2 = std::clamp(std::max(x1, x2), 0, right); + y2 = std::clamp(std::max(y1, y2), 0, bottom); + // An empty box is low above high, so it is replaced rather than widened + if (this->x_low_ > this->x_high_ || this->y_low_ > this->y_high_) { + this->x_low_ = left; + this->x_high_ = x2; + this->y_low_ = top; + this->y_high_ = y2; + return; + } + this->x_low_ = std::min(this->x_low_, left); + this->x_high_ = std::max(this->x_high_, x2); + this->y_low_ = std::min(this->y_low_, top); + this->y_high_ = std::max(this->y_high_, y2); } void MipiRgb::draw_pixel_at(int x, int y, Color color) {