Toluwalakin Owoso 201231164

Dependencies:   mbed ELEC2645_Project_el18to

Committer:
Tolu__
Date:
Sun May 03 16:15:14 2020 +0000
Revision:
8:37efdea02311
Parent:
1:356eeff7b566
check commit;

Who changed what in which revision?

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