el15mh 200929957

Dependencies:   mbed

Committer:
el15mh
Date:
Thu May 04 14:43:29 2017 +0000
Revision:
9:960dfc71c224
Documented using doxygen

Who changed what in which revision?

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