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