The temporary file

Dependencies:   mbed

Committer:
sjuzyz
Date:
Sat Apr 11 11:21:08 2020 +0000
Revision:
0:db648efbaa56
mide

Who changed what in which revision?

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