Coursework

Committer:
sesa514652
Date:
Fri Feb 04 16:44:14 2022 +0000
Revision:
45:f1db729741f7
Parent:
0:1f799c7cce2b
Submission

Who changed what in which revision?

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