ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Committer:
el17cd
Date:
Fri Feb 22 12:26:52 2019 +0000
Revision:
0:efb5eec6b8ea
Initial Commit

Who changed what in which revision?

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