Josh Davy / Mbed 2 deprecated Flip

Dependencies:   mbed el17jd

Committer:
joshdavy
Date:
Tue Mar 12 12:38:34 2019 +0000
Revision:
0:4916a63a6cbf
initial commit

Who changed what in which revision?

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