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