All there but errors need fixing

Dependencies:   mbed

Committer:
el18rs
Date:
Fri May 22 10:36:01 2020 +0000
Revision:
1:35dba0833c7d
Start

Who changed what in which revision?

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