Question
C++ Coding Assignment 2 Overview In this assignment you will turn the BitMap struct created into a class with constructor, copy constructor, destructor, assignment operator
C++ Coding Assignment 2
Overview
In this assignment you will turn the BitMap struct created into a class with constructor, copy constructor, destructor, assignment operator and other functions.
The Program
In main you will write code to fully test every function in your class. Your testing should test corner cases such as self assignment, deleting an object with unallocated array...etc. Classes and methods to Implement:
You will NOT modify the BitMapHeader struct at all. You will modify the BitMap struct to become a class. You should determine the appropriate functions to implement. Note that some of the functions we wrote in the struct will go away (for example the Free function will be replaced by a destructor).
struct BitMap {
uint32_t width, height;
uint8_t *image;
};
typedef BitMap* BitMapPointer;
/* CreateBitMapPic
* Create a new Bit Map of the desired height and width. Both the height and
* width should be positive.
*/
BitMap *CreateBitMapPic(int width, int height);
/* Free
* Free the memory associated with a bit map and set the pointer to 0.
*/
void Free(BitMapPointer &bm);
/* Load
* Load a bitmap from the specified file.
* Code should support 24- and 32-bit bitmaps. Internally, the code should use
* 32-bit bitmaps.
* If the height is negative, the file has the rows reversed; they should be reversed
* upon loading, so that all bitmaps will be stored in the same way.
* Returns 0 if the loading failed.
*/
BitMap *Load(const char *file);
/* Save
* Write a bitmap to the specified file.
* Code should write 32-bit bitmaps with positive height.
*/
void Save(BitMap *bm, const char *file);
/* GetPixel
* Load the RGB values from a given pixel. (0, 0) is the bottom left of the image.
*/
void GetPixel(BitMap *bm, int x, int y, uint8_t &redByte, uint8_t &greenByte, uint8_t &blueByte, uint8_t &alphaByte);
/* SetPixel
* Write the RGB values to a given pixel. (0, 0) is the bottom left of the image.
*/
void SetPixel(BitMap *bm, int x, int y, uint8_t redByte, uint8_t greenByte, uint8_t blueByte, uint8_t alphaByte );
#endif
Main.cpp
int main(int argc, const char * argv[])
{
/* This code tests your bitmap implementation on 24-bit and 32-bit files. The desired output
* files are included for comparison purposes.
*/
/** Test #1: Create a new file with a gradient from yellow to green to cyan (bottom to top) */
BitMap *bm = CreateBitMapPic(255, 255);
for (int x = 0; x < 255; x++)
{
for (int y = 0; y < 255; y++)
{
SetPixel(bm, x, y, 255-y, 255, y, 255);
}
}
std::cout << "Processing test.bmp ";
Save(bm, "test.bmp");
Free(bm);
/** Test #2: A 24-bit file with reversed height; add a 40x40 red square in the lower right corner */
std::cout << "Processing picture.bmp ";
bm = Load("picture.bmp");
if (bm == 0)
{
std::cout << "Load of 'picture.bmp' failed ";
}
else {
for (int x = 0; x < 40; x++)
{
for (int y = 0; y < 40; y++)
{
SetPixel(bm, bm->width-x-1, y, 255, 0, 0, 255);
}
}
Save(bm, "picture new.bmp");
free(bm);
}
/** Test #3: A 24-bit file; add a 40x40 blue square in the upper right corner */
std::cout << "Processing train.bmp ";
bm = Load("train.bmp");
if (bm == 0)
{
std::cout << "Load of 'train.bmp' failed ";
}
else {
for (int x = 0; x < 40; x++)
{
for (int y = 0; y < 40; y++)
{
SetPixel(bm, bm->width-x-1, bm->height-y-1, 0, 0, 255, 255);
}
}
Save(bm, "train new.bmp");
free(bm);
}
/** Test #4: A 24-bit file with padding; add a 40x40 blue square in the upper right corner */
std::cout << "Processing mountain.bmp ";
bm = Load("mountain.bmp");
if (bm == 0)
{
std::cout << "Load of 'mountain.bmp' failed ";
}
else {
// Convert to grayscale
for (int x = 0; x < bm->width; x++)
{
for (int y = 0; y < bm->height; y++)
{
uint8_t r, g, b, a;
GetPixel(bm, x, y, r, g, b, a);
uint8_t average = (r+g+b)/3;
SetPixel(bm, x, y, average, average, average, a);
}
}
Save(bm, "mountain new.bmp");
free(bm);
}
/** Test #4: A 32-bit file with reversed height; extract the green channel */
std::cout << "Processing canmore.bmp ";
bm = Load("canmore.bmp");
if (bm == 0)
{
std::cout << "Load of 'canmore.bmp' failed ";
}
else {
for (int x = 0; x < bm->width; x++)
{
for (int y = 0; y < bm->height; y++)
{
uint8_t r, g, b, a;
GetPixel(bm, x, y, r, g, b, a);
SetPixel(bm, x, y, 0, g, 0, a);
}
}
Save(bm, "canmore new.bmp");
free(bm);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started