Project Submission (late)

Dependencies:   mbed

Committer:
el17tc
Date:
Fri May 10 14:52:28 2019 +0000
Revision:
3:83e79d31930c
Parent:
0:72f372170a73
final commit, API is added.; I'm not sure if there is a specific statement of academic integrity wanted but- I declare that this work is 100% my own, I have not plagiarised any work or attempted to fabricate any part of it for extra marks.

Who changed what in which revision?

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