Template for the ELEC1620 End of year exam

Dependencies:   mbed

Committer:
el16ttb
Date:
Fri Mar 22 13:11:07 2019 +0000
Revision:
0:54721f063ac8
Initial commit

Who changed what in which revision?

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