Li Ruofan 201199450

Dependencies:   mbed

Committer:
DannyLee
Date:
Sun May 24 08:16:35 2020 +0000
Revision:
1:bd7c99a5bd10
Parent:
0:80a59a49d504
Li Ruofan 201199450

Who changed what in which revision?

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