ELEC2645 (2017/18) / Mbed 2 deprecated el16a2t

Dependencies:   mbed

Committer:
el16a2t
Date:
Tue Apr 17 08:33:39 2018 +0000
Revision:
0:1a1863a687d3
initial commit

Who changed what in which revision?

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