Joshua O'hara 201291390

Dependencies:   mbed

Committer:
josh_ohara
Date:
Tue May 26 15:15:46 2020 +0000
Revision:
44:3b904d25ee12
Parent:
1:9b659b3c092b
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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