Haoyan Zhang / Mbed 2 deprecated demmo

Dependencies:   mbed

Committer:
haoyan
Date:
Tue Apr 28 14:38:02 2020 +0000
Revision:
0:08b2bdddf22e
deemo

Who changed what in which revision?

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