Zikang Qian / Mbed 2 deprecated el17z2q

Dependencies:   mbed

Fork of el17z2q by ELEC2645 (2017/18)

Committer:
yzjdxl
Date:
Tue Apr 17 08:43:38 2018 +0000
Revision:
0:f707ce1010fa
initial commmit

Who changed what in which revision?

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