Samuel Lashmar 201170334

Dependencies:   mbed

Committer:
sdlashmar
Date:
Wed Mar 04 12:04:43 2020 +0000
Revision:
1:5aba5e5a748b
inital commit

Who changed what in which revision?

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