ELEC2645 (2018/19) / Mbed 2 deprecated el17kz

Dependencies:   mbed

Committer:
kamtas
Date:
Tue May 07 19:09:03 2019 +0000
Revision:
0:1e61ade20a23
initial commit

Who changed what in which revision?

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