Class used to interface with the Nokia N5110 LCD.

Fork of N5110 by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Bitmap.cpp Source File

Bitmap.cpp

00001 #include "Bitmap.h"
00002 
00003 #include <iostream>
00004 
00005 #include "N5110.h"
00006 
00007 Bitmap::Bitmap(int const               *contents,
00008                unsigned int const       height,
00009                unsigned int const       width)
00010     :
00011     _contents(std::vector<int>(height*width)),
00012     _height(height),
00013     _width(width)
00014 {
00015     // Perform a quick sanity check of the dimensions
00016     if (_contents.size() != height * width) {
00017         std::cerr << "Contents of bitmap has size " << _contents.size()
00018                   << " pixels, but its dimensions were specified as "
00019                   << width << " * " << height << " = " << width * height << std::endl;
00020     }
00021 
00022     for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i];
00023 }
00024 
00025 /**
00026  * @returns the value of the pixel at the given position
00027  */
00028 int Bitmap::get_pixel (unsigned int const row,
00029                       unsigned int const column) const
00030 {
00031     // First check that row and column indices are within bounds
00032     if(column >= _width || row >= _height)
00033     {
00034         std::cerr << "The requested pixel with index " << row << "," << column
00035                   << "is outside the bitmap dimensions: " << _width << ","
00036                   << _height << std::endl;
00037     }
00038 
00039     // Now return the pixel value, using row-major indexing
00040     return _contents[row * _width + column];
00041 }
00042 
00043 /**
00044  * @brief Prints the contents of the bitmap to the terminal
00045  */
00046 void Bitmap::print() const
00047 {
00048     for (unsigned int row = 0; row < _height; ++row)
00049     {
00050         // Print each element of the row
00051         for (unsigned int column = 0; column < _width; ++column)
00052         {
00053             int pixel = get_pixel (row, column);
00054             std::cout << pixel;
00055         }
00056 
00057         // And then terminate with a new-line character
00058         std::cout << std::endl;
00059     }
00060 }
00061 
00062 /**
00063  * @brief Renders the contents of the bitmap onto an N5110 screen
00064  *
00065  * @param[in] lcd The screen to use for rendering
00066  * @param[in] x0  The horizontal position in pixels at which to render the bitmap
00067  * @param[in] y0  The vertical position in pixels at which to render the bitmap
00068  *
00069  * @details Note that x0, y0 gives the location of the top-left of the bitmap on
00070  *          the screen.
00071  *          This function only updates the buffer on the screen.  You still need
00072  *          to refresh the screen in order to actually see the bitmap.
00073  */
00074 void Bitmap::render(N5110 &lcd,
00075                     unsigned int const x0,
00076                     unsigned int const y0) const
00077 {
00078     // Loop through each row of the bitmap image
00079     for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row)
00080     {
00081         // Row index on the screen for rendering the row of pixels
00082         unsigned int screen_row = y0 + bitmap_row;
00083                 
00084         // Render each pixel in the row
00085         for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col)
00086         {
00087             // Column index on the screen for rendering this pixel
00088             int screen_col = x0 + bitmap_col;
00089 
00090             // Find the required value of the pixel at the given location within
00091             // the bitmap data and then write it to the LCD screen
00092             int pixel = get_pixel (bitmap_row, bitmap_col);
00093             lcd.setPixel(screen_col, screen_row, pixel);
00094         }
00095     }
00096 }