Zeyu Feng 201377605

Dependencies:   mbed

On Minerva

Committer:
el19zf
Date:
Fri May 22 16:07:02 2020 +0000
Revision:
22:cded0cd8e1c9
Parent:
1:b133934e0d45
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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