b

Committer:
ciarankane123
Date:
Fri Feb 04 23:40:27 2022 +0000
Revision:
0:f2d1f9532b62
d

Who changed what in which revision?

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