ZIWEI LIU / Mbed 2 deprecated ELEC2645_Project_el19z2l

Dependencies:   mbed

Committer:
alexliu0812
Date:
Thu May 14 06:54:58 2020 +0000
Revision:
1:11854f815cc8
welcome

Who changed what in which revision?

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