FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
0:7d4d2023ed9c
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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