1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-18 09:43:47 +01:00

Image bit dephts (#1241)

This commit is contained in:
Guillermo Ruffino
2020-08-11 10:28:30 -03:00
committed by GitHub
parent ccb6fc3010
commit a9d75ca4f4
3 changed files with 79 additions and 70 deletions

View File

@@ -204,31 +204,33 @@ void DisplayBuffer::vprintf_(int x, int y, Font *font, Color color, TextAlign al
if (ret > 0)
this->print(x, y, font, color, align, buffer);
}
void DisplayBuffer::image(int x, int y, Image *image) { this->image(x, y, COLOR_ON, image); }
void DisplayBuffer::image(int x, int y, Color color, Image *image, bool invert) {
if (image->get_type() == BINARY) {
for (int img_x = 0; img_x < image->get_width(); img_x++) {
for (int img_y = 0; img_y < image->get_height(); img_y++) {
if (invert)
this->draw_pixel_at(x + img_x, y + img_y, image->get_pixel(img_x, img_y) ? COLOR_OFF : color);
else
this->draw_pixel_at(x + img_x, y + img_y, image->get_pixel(img_x, img_y) ? color : COLOR_OFF);
void DisplayBuffer::image(int x, int y, Image *image, Color color_on, Color color_off) {
switch (image->get_type()) {
case IMAGE_TYPE_BINARY:
for (int img_x = 0; img_x < image->get_width(); img_x++) {
for (int img_y = 0; img_y < image->get_height(); img_y++) {
this->draw_pixel_at(x + img_x, y + img_y, image->get_pixel(img_x, img_y) ? color_on : color_off);
}
}
}
} else if (image->get_type() == GRAYSCALE) {
for (int img_x = 0; img_x < image->get_width(); img_x++) {
for (int img_y = 0; img_y < image->get_height(); img_y++) {
this->draw_pixel_at(x + img_x, y + img_y, image->get_grayscale_pixel(img_x, img_y));
break;
case IMAGE_TYPE_GRAYSCALE:
for (int img_x = 0; img_x < image->get_width(); img_x++) {
for (int img_y = 0; img_y < image->get_height(); img_y++) {
this->draw_pixel_at(x + img_x, y + img_y, image->get_grayscale_pixel(img_x, img_y));
}
}
}
} else if (image->get_type() == RGB) {
for (int img_x = 0; img_x < image->get_width(); img_x++) {
for (int img_y = 0; img_y < image->get_height(); img_y++) {
this->draw_pixel_at(x + img_x, y + img_y, image->get_color_pixel(img_x, img_y));
break;
case IMAGE_TYPE_RGB24:
for (int img_x = 0; img_x < image->get_width(); img_x++) {
for (int img_y = 0; img_y < image->get_height(); img_y++) {
this->draw_pixel_at(x + img_x, y + img_y, image->get_color_pixel(img_x, img_y));
}
}
}
break;
}
}
void DisplayBuffer::get_text_bounds(int x, int y, const char *text, Font *font, TextAlign align, int *x1, int *y1,
int *width, int *height) {
int x_offset, baseline;
@@ -463,15 +465,14 @@ Color Image::get_grayscale_pixel(int x, int y) const {
if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
return 0;
const uint32_t pos = (x + y * this->width_);
return Color(pgm_read_byte(this->data_start_ + pos) << 24);
const uint8_t gray = pgm_read_byte(this->data_start_ + pos);
return Color(gray | gray << 8 | gray << 16 | gray << 24);
}
int Image::get_width() const { return this->width_; }
int Image::get_height() const { return this->height_; }
ImageType Image::get_type() const { return this->type_; }
Image::Image(const uint8_t *data_start, int width, int height)
: width_(width), height_(height), data_start_(data_start) {}
Image::Image(const uint8_t *data_start, int width, int height, int type)
: width_(width), height_(height), type_((ImageType) type), data_start_(data_start) {}
Image::Image(const uint8_t *data_start, int width, int height, ImageType type)
: width_(width), height_(height), type_(type), data_start_(data_start) {}
DisplayPage::DisplayPage(const display_writer_t &writer) : writer_(writer) {}
void DisplayPage::show() { this->parent_->show_page(this); }

View File

@@ -68,7 +68,7 @@ extern const Color COLOR_OFF;
/// Turn the pixel ON.
extern const Color COLOR_ON;
enum ImageType { BINARY = 0, GRAYSCALE = 1, RGB = 2 };
enum ImageType { IMAGE_TYPE_BINARY = 0, IMAGE_TYPE_GRAYSCALE = 1, IMAGE_TYPE_RGB24 = 2 };
enum DisplayRotation {
DISPLAY_ROTATION_0_DEGREES = 0,
@@ -262,9 +262,15 @@ class DisplayBuffer {
__attribute__((format(strftime, 5, 0)));
#endif
/// Draw the `image` with the top-left corner at [x,y] to the screen.
void image(int x, int y, Image *image);
void image(int x, int y, Color color, Image *image, bool invert = false);
/** Draw the `image` with the top-left corner at [x,y] to the screen.
*
* @param x The x coordinate of the upper left corner.
* @param y The y coordinate of the upper left corner.
* @param image The image to draw
* @param color_on The color to replace in binary images for the on bits.
* @param color_off The color to replace in binary images for the off bits.
*/
void image(int x, int y, Image *image, Color color_on = COLOR_ON, Color color_off = COLOR_OFF);
/** Get the text bounds of the given string.
*
@@ -381,8 +387,7 @@ class Font {
class Image {
public:
Image(const uint8_t *data_start, int width, int height);
Image(const uint8_t *data_start, int width, int height, int type);
Image(const uint8_t *data_start, int width, int height, ImageType type);
bool get_pixel(int x, int y) const;
Color get_color_pixel(int x, int y) const;
Color get_grayscale_pixel(int x, int y) const;
@@ -393,7 +398,7 @@ class Image {
protected:
int width_;
int height_;
ImageType type_{BINARY};
ImageType type_;
const uint8_t *data_start_;
};