Helios Lyons 201239214

Dependencies:   mbed

Committer:
helioslyons
Date:
Tue Apr 14 13:22:35 2020 +0000
Revision:
5:72f59786b695
Parent:
1:a3f43007030e
Import to Mbed Studio

Who changed what in which revision?

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