Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The C++ code I wrote for this is below: #include <iostream> #include <fstream> #

ID: 3902139 • Letter: T

Question

The C++ code I wrote for this is below:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

void process(int& blue, int& green, int& red) // code not finished
{
blue = .11 + blue;
green = .59 + green;
red = .30 + red;
}

int get_int(fstream& stream, int offset)
{
stream.seekg(offset);
int result = 0;
int base = 1;
for (int i = 0; i < 4; i++)
{
result = result + stream.get() * base;
base = base * 256;
}
return result;
}

int main()
{
cout << "Please enter the file name: ";
string filename;
cin >> filename;

fstream stream;
stream.open(filename, ios::in | ios::out | ios::binary);

int file_size = get_int(stream, 2);
int start = get_int(stream, 10);
int width = get_int(stream, 18);
int height = get_int(stream, 22);
int scanline_size = width * 3;
int padding = 0;

if (scanline_size % 4 != 0)
{
padding = 4 - scanline_size % 4;
}

if (file_size != start + (scanline_size + padding) * height)
{
cout << "Not a 24-bit true color image file." << endl;
return 1;
}

int pos = start;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
stream.seekg(pos);
int blue = stream.get();
int green = stream.get();
int red = stream.get();
process(blue, green, red);
stream.seekp(pos);
stream.put(blue);
stream.put(green);
stream.put(red);
pos = pos + 3;
}
stream.seekg(padding, ios::cur);
pos = pos + padding;
}
return 0;
}

My teacher said the image didn't really change. Can you guys help me, please. Thank you.

Write a program that edits an image file, turning it into grayscale. O Cay Horstmann. Replace each pixel with a pixel that has the same grayness level for the blue, green and red component. The grayness level is computed by adding 30 percent of the red level, 59 percent of the green level, and 11 percent of the blue level. (The color- sensing cone cells in the human eye differ in their sensitivity for red, green, and blue light.)

Explanation / Answer

Your entire program is correct but logic used in process function is wrong. In order to get gray scale image you need to make

red = 0.30*red, blue=blue*0.11, green=green*0.59

void process(int& blue, int& green, int& red) // code not finished

{

blue = .11 * blue;

green = .59 *green;

red = .30 * red;

}

THUMBS UP IF YOU ARE SATISFIED WITH THE ANSWER OTHERWISE REPLY WITH YOUR QUERIES.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote