ELEC2645 (2016/17) / Mbed 2 deprecated 2645_Project_el15as

Dependencies:   mbed

Committer:
el15as
Date:
Fri May 05 12:14:48 2017 +0000
Revision:
15:e3fc18eb519e
Final version

Who changed what in which revision?

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