ELEC2645 (2017/18) / Mbed OS el16ajm
Committer:
Andrew_M
Date:
Mon Apr 16 08:42:04 2018 +0000
Revision:
0:66e5b37c127e
Initial commit

Who changed what in which revision?

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