Yang Hongxiao 201199678

Dependencies:   mbed

Committer:
YHX
Date:
Thu May 14 17:00:34 2020 +0000
Revision:
1:a6ead8050c23
Parent:
0:4b02786450c0
Yang Hongxiao; el17hy; 201199678

Who changed what in which revision?

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