I'm sorry if this has been posted before but...

I used this example code from my book to understand image processing better. The image is of the queen mary ship, in a .bmp extension file. Anyways I basically take the picture and save a negative image of it onto the same or different file. The example says to show colours red blue and green at some set values, so I did and the problem is, it completely turns to different colours like black, light blue, and etc depending on the number of times i try to do this. I don't understand why this is at all. I figured that an example would be a great way to understand the code but it doesn't seem to work.

If someone needs me to post the code, I'll post it.

I'm sorry if this has been posted before but...

I used this example code from my book to understand image processing better. The image is of the queen mary ship, in a .bmp extension file. Anyways I basically take the picture and save a negative image of it onto the same or different file. The example says to show colours red blue and green at some set values, so I did and the problem is, it completely turns to different colours like black, light blue, and etc depending on the number of times i try to do this. I don't understand why this is at all. I figured that an example would be a great way to understand the code but it doesn't seem to work.

If someone needs me to post the code, I'll post it.

#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
*/
/**
   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;
}
void process(int& blue, int& green, int& red)
{
   blue = 255-blue;
   green = 255-green;
   red = 255-red;
}
int main()
{
   fstream stream;
   // Open as a binary file
   stream.open(file, ios::in | ios::out | ios::binary);
   int file_size = get_int(stream, 2); // Get the image dimensions
   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;
   }
   
   stream.seekg(start); // Go to the start of the pixels

   for (int i = 0; i < height; i++) // For each scan line
   {
      for (int j = 0; j < width; j++) // For each pixel
      {
	 int pos = stream.tellg(); // Go to the start of the pixel

         int blue = stream.get(); // Read the pixel
         int green = stream.get();
         int red = stream.get();

         process(blue, green, red); // Process the pixel

         stream.seekp(pos); // Go back to the start of the pixel

         stream.put(blue); // Write the pixel
         stream.put(green);
         stream.put(red);
      }

      stream.seekg(padding, ios::cur); // Skip the padding
   }

   return 0;
}

Here is the code. Basically I'm to turn this image attached into a negative type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.