![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
legionella detector
Dependencies: mbed
N5110/Bitmap.cpp@0:089b4c4f2b6f, 23 months ago (annotated)
- Committer:
- lewiscameron
- Date:
- Tue Aug 23 08:47:11 2022 +0000
- Revision:
- 0:089b4c4f2b6f
h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lewiscameron | 0:089b4c4f2b6f | 1 | #include "Bitmap.h" |
lewiscameron | 0:089b4c4f2b6f | 2 | |
lewiscameron | 0:089b4c4f2b6f | 3 | #include <iostream> |
lewiscameron | 0:089b4c4f2b6f | 4 | |
lewiscameron | 0:089b4c4f2b6f | 5 | #include "N5110.h" |
lewiscameron | 0:089b4c4f2b6f | 6 | |
lewiscameron | 0:089b4c4f2b6f | 7 | Bitmap::Bitmap(int const *contents, |
lewiscameron | 0:089b4c4f2b6f | 8 | unsigned int const height, |
lewiscameron | 0:089b4c4f2b6f | 9 | unsigned int const width) |
lewiscameron | 0:089b4c4f2b6f | 10 | : |
lewiscameron | 0:089b4c4f2b6f | 11 | _contents(std::vector<int>(height*width)), |
lewiscameron | 0:089b4c4f2b6f | 12 | _height(height), |
lewiscameron | 0:089b4c4f2b6f | 13 | _width(width) |
lewiscameron | 0:089b4c4f2b6f | 14 | { |
lewiscameron | 0:089b4c4f2b6f | 15 | // Perform a quick sanity check of the dimensions |
lewiscameron | 0:089b4c4f2b6f | 16 | if (_contents.size() != height * width) { |
lewiscameron | 0:089b4c4f2b6f | 17 | std::cerr << "Contents of bitmap has size " << _contents.size() |
lewiscameron | 0:089b4c4f2b6f | 18 | << " pixels, but its dimensions were specified as " |
lewiscameron | 0:089b4c4f2b6f | 19 | << width << " * " << height << " = " << width * height << std::endl; |
lewiscameron | 0:089b4c4f2b6f | 20 | } |
lewiscameron | 0:089b4c4f2b6f | 21 | |
lewiscameron | 0:089b4c4f2b6f | 22 | for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i]; |
lewiscameron | 0:089b4c4f2b6f | 23 | } |
lewiscameron | 0:089b4c4f2b6f | 24 | |
lewiscameron | 0:089b4c4f2b6f | 25 | /** |
lewiscameron | 0:089b4c4f2b6f | 26 | * @returns the value of the pixel at the given position |
lewiscameron | 0:089b4c4f2b6f | 27 | */ |
lewiscameron | 0:089b4c4f2b6f | 28 | int Bitmap::get_pixel(unsigned int const row, |
lewiscameron | 0:089b4c4f2b6f | 29 | unsigned int const column) const |
lewiscameron | 0:089b4c4f2b6f | 30 | { |
lewiscameron | 0:089b4c4f2b6f | 31 | // First check that row and column indices are within bounds |
lewiscameron | 0:089b4c4f2b6f | 32 | if(column >= _width || row >= _height) |
lewiscameron | 0:089b4c4f2b6f | 33 | { |
lewiscameron | 0:089b4c4f2b6f | 34 | std::cerr << "The requested pixel with index " << row << "," << column |
lewiscameron | 0:089b4c4f2b6f | 35 | << "is outside the bitmap dimensions: " << _width << "," |
lewiscameron | 0:089b4c4f2b6f | 36 | << _height << std::endl; |
lewiscameron | 0:089b4c4f2b6f | 37 | } |
lewiscameron | 0:089b4c4f2b6f | 38 | |
lewiscameron | 0:089b4c4f2b6f | 39 | // Now return the pixel value, using row-major indexing |
lewiscameron | 0:089b4c4f2b6f | 40 | return _contents[row * _width + column]; |
lewiscameron | 0:089b4c4f2b6f | 41 | } |
lewiscameron | 0:089b4c4f2b6f | 42 | |
lewiscameron | 0:089b4c4f2b6f | 43 | /** |
lewiscameron | 0:089b4c4f2b6f | 44 | * @brief Prints the contents of the bitmap to the terminal |
lewiscameron | 0:089b4c4f2b6f | 45 | */ |
lewiscameron | 0:089b4c4f2b6f | 46 | void Bitmap::print() const |
lewiscameron | 0:089b4c4f2b6f | 47 | { |
lewiscameron | 0:089b4c4f2b6f | 48 | for (unsigned int row = 0; row < _height; ++row) |
lewiscameron | 0:089b4c4f2b6f | 49 | { |
lewiscameron | 0:089b4c4f2b6f | 50 | // Print each element of the row |
lewiscameron | 0:089b4c4f2b6f | 51 | for (unsigned int column = 0; column < _width; ++column) |
lewiscameron | 0:089b4c4f2b6f | 52 | { |
lewiscameron | 0:089b4c4f2b6f | 53 | int pixel = get_pixel(row, column); |
lewiscameron | 0:089b4c4f2b6f | 54 | std::cout << pixel; |
lewiscameron | 0:089b4c4f2b6f | 55 | } |
lewiscameron | 0:089b4c4f2b6f | 56 | |
lewiscameron | 0:089b4c4f2b6f | 57 | // And then terminate with a new-line character |
lewiscameron | 0:089b4c4f2b6f | 58 | std::cout << std::endl; |
lewiscameron | 0:089b4c4f2b6f | 59 | } |
lewiscameron | 0:089b4c4f2b6f | 60 | } |
lewiscameron | 0:089b4c4f2b6f | 61 | |
lewiscameron | 0:089b4c4f2b6f | 62 | /** |
lewiscameron | 0:089b4c4f2b6f | 63 | * @brief Renders the contents of the bitmap onto an N5110 screen |
lewiscameron | 0:089b4c4f2b6f | 64 | * |
lewiscameron | 0:089b4c4f2b6f | 65 | * @param[in] lcd The screen to use for rendering |
lewiscameron | 0:089b4c4f2b6f | 66 | * @param[in] x0 The horizontal position in pixels at which to render the bitmap |
lewiscameron | 0:089b4c4f2b6f | 67 | * @param[in] y0 The vertical position in pixels at which to render the bitmap |
lewiscameron | 0:089b4c4f2b6f | 68 | * |
lewiscameron | 0:089b4c4f2b6f | 69 | * @details Note that x0, y0 gives the location of the top-left of the bitmap on |
lewiscameron | 0:089b4c4f2b6f | 70 | * the screen. |
lewiscameron | 0:089b4c4f2b6f | 71 | * This function only updates the buffer on the screen. You still need |
lewiscameron | 0:089b4c4f2b6f | 72 | * to refresh the screen in order to actually see the bitmap. |
lewiscameron | 0:089b4c4f2b6f | 73 | */ |
lewiscameron | 0:089b4c4f2b6f | 74 | void Bitmap::render(N5110 &lcd, |
lewiscameron | 0:089b4c4f2b6f | 75 | unsigned int const x0, |
lewiscameron | 0:089b4c4f2b6f | 76 | unsigned int const y0) const |
lewiscameron | 0:089b4c4f2b6f | 77 | { |
lewiscameron | 0:089b4c4f2b6f | 78 | // Loop through each row of the bitmap image |
lewiscameron | 0:089b4c4f2b6f | 79 | for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row) |
lewiscameron | 0:089b4c4f2b6f | 80 | { |
lewiscameron | 0:089b4c4f2b6f | 81 | // Row index on the screen for rendering the row of pixels |
lewiscameron | 0:089b4c4f2b6f | 82 | unsigned int screen_row = y0 + bitmap_row; |
lewiscameron | 0:089b4c4f2b6f | 83 | |
lewiscameron | 0:089b4c4f2b6f | 84 | // Render each pixel in the row |
lewiscameron | 0:089b4c4f2b6f | 85 | for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col) |
lewiscameron | 0:089b4c4f2b6f | 86 | { |
lewiscameron | 0:089b4c4f2b6f | 87 | // Column index on the screen for rendering this pixel |
lewiscameron | 0:089b4c4f2b6f | 88 | int screen_col = x0 + bitmap_col; |
lewiscameron | 0:089b4c4f2b6f | 89 | |
lewiscameron | 0:089b4c4f2b6f | 90 | // Find the required value of the pixel at the given location within |
lewiscameron | 0:089b4c4f2b6f | 91 | // the bitmap data and then write it to the LCD screen |
lewiscameron | 0:089b4c4f2b6f | 92 | int pixel = get_pixel(bitmap_row, bitmap_col); |
lewiscameron | 0:089b4c4f2b6f | 93 | lcd.setPixel(screen_col, screen_row, pixel); |
lewiscameron | 0:089b4c4f2b6f | 94 | } |
lewiscameron | 0:089b4c4f2b6f | 95 | } |
lewiscameron | 0:089b4c4f2b6f | 96 | } |