Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Committer:
Noximilien
Date:
Tue May 07 15:22:35 2019 +0000
Revision:
40:e3bbda7444fa
Parent:
1:5aa2312d3e94
The Final, Submission Version. I have read and agreed to the academic integrity. SID:201160286

Who changed what in which revision?

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