HaoZhang SID: 201199702

Dependencies:   mbed

Committer:
zh870524589
Date:
Thu May 14 17:45:05 2020 +0000
Revision:
2:867fdea920c1
Parent:
0:45ce241d316b
final

Who changed what in which revision?

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