ELEC2645 (2017/18) / Mbed 2 deprecated ll14zs

Dependencies:   mbed

Fork of ll14zs by Zeshaan Saeed

Committer:
ll14zs
Date:
Thu Apr 26 11:21:48 2018 +0000
Revision:
0:2211174dee1c
initial commit

Who changed what in which revision?

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