Luke Penrose / N5110
Committer:
el16lp
Date:
Mon Apr 23 09:13:51 2018 +0000
Revision:
0:75f561781f6b
N5110;

Who changed what in which revision?

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