Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- /**
- Processes a pixel by forming the negative.
- @param blue the blue value of the pixel
- @param green the green value of the pixel
- @param red the red value of the pixel
- */
- void process(int& blue, int& green, int& red)
- {
- blue = 255 - blue;
- green = 255 - green;
- red = 255 - red;
- }
- /**
- Gets an integer from a binary stream.
- @param stream the stream
- @param offset the offset at which to read the integer
- @return the integer starting at the given offset
- */
- 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;
- // Open as a binary file
- stream.open(filename.c_str(),
- ios::in|ios::out|ios::binary);
- // Get the image dimensions
- 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);
- // Scan lines must occupy multiples of four bytes
- 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;
- }
- // Go to the start of the pixels
- stream.seekg(start);
- // For each scan line
- for (int i = 0; i < height; i++)
- {
- // For each pixel
- for (int j = 0; j < width; j++)
- {
- // Go to the start of the pixel
- int pos = stream.tellg();
- // Read the pixel
- int blue = stream.get();
- int green = stream.get();
- int red = stream.get();
- // Process the pixel
- process(blue, green, red);
- // Go back to the start of the pixel
- stream.seekp(pos);
- stream.put(blue);
- // Write the pixel
- stream.put(green);
- stream.put(red);
- }
- // Skip the padding
- stream.seekg(padding, ios::cur);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment