ELEC2645 (2018/19) / Mbed 2 deprecated el16y2m

Dependencies:   mbed

Committer:
MYY
Date:
Thu May 09 05:08:02 2019 +0000
Revision:
17:9f7ff626210b
Parent:
12:777558372c67
Final submission, i guarantee there is no plagiarism in the project.

Who changed what in which revision?

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