Okay, I was given a picture in bitmap format and a very brief explanation on seekp and seekg. And was told to write a program. I am supposed to make overwrite the image as its own negative and output in binary.

I don't know where to even start, I know basic file operations for text files, but this is a bit out of my league. Can I get a logical guide please, or an explanation to get me started? On the following list I astericked what I sorta have coded until know and might be doing correctly.

Ask the user for an input file name *
read the input file, *
determine the file size *
determine the offset (where the pixels start) *
determine the width of pixels
determine the height of pixels
For each scan line, for each pixel in that line, read the pixel (the red, green, and blue values) and convert it to its negative and replace the original pixel

*

1. is easy.
2. I know I have to loop, but don't know how to get my numbers for the loop. should I open file in binary mode?
3. I am supposing that with sizeof() I can do it.
4. Supposing that with ios::beg I can figure that out.
7. using seek I just have to get and put 255 - color

Help please.

Check out wiki_bitmap. It has good explanation of how bitmaps are formatted. From there you can determine how to read the width/height, and the starting offset of where the actual pixels starts from.

Now I sort of have an idea of how a bmp works.

#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

const int bitsPerPix = 24;

struct Image
{
    char imgName[50];
    int sizeFile,
        offSet,
        imageWidth,
        imageHeight;   
};

//void change2Negative();
void read4mFile(fstream &, Image &);
void displayFile(const Image &);

int main()
{
    Image bmp;
    
    cout << "Please enter the name of the file: " << endl;
    cin.getline(bmp.imgName, 50);
    
    fstream outFile;
    outFile.open(bmp.imgName, ios::out);
    if(outFile.is_open())
    {
        read4mFile(outFile, bmp);
        displayFile(bmp);  
        outFile.close();
    }
    else
    { 
        cout << "Error opening file!" << endl;
        return 0;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void read4mFile(fstream &dFile, Image &dInfo)
{
    string temp;
    
    for(int i = 0; i < bitsPerPix; i++)
    {
        if (i == 2)
            dFile >> dInfo.sizeFile;
        else if(i == 10)
            dFile >> dInfo.offSet;
        else if(i == 18)
            dFile >> dInfo.imageWidth;
        else if(i == 22)
            dFile >> dInfo.imageHeight;
        else
            dFile >> temp;
    }
}

/*void change2Negative()
{
     
}*/

void displayFile(const Image &dInfo)
{
    cout << "File Name: " << dInfo.imgName <<endl; 
    cout << "File Size: " << dInfo.sizeFile <<endl; 
    cout << "File Offset: " << dInfo.offSet <<endl; 
    cout << "File Width: " << dInfo.imageWidth <<endl; 
    cout << "File Height: " << dInfo.imageHeight <<endl; 
}

This is What I have until now, the if statements in the reading are for where I was told the information was, I'm still getting gibberish. Any Advice?

From wiki :

- First two bytes are identifier( type of image )
- Next 4 bytes are Size of BMP
- Next 4 bytes are unused
- Next 4 bytes are offset where Pixel array is
- Next 4 bytes are number of bytes in DIB header
- Next 4 bytes is the width of the bitmap
- Next 4 bytes is the height of the bitmap

The highlighted part is what you need. So the other, just read it into a temporary variable. I suggest you read it into a char. A char in C++ is 1 byte usually. So think a little and you should get it.

void read4mFile(fstream &dFile, Image &dInfo)
{
     dFile.seekg(0, ios::beg);
     dFile.seekg(2);
     dFile.read(reinterpret_cast<char *>(dInfo.sizeFile), sizeof(int));
     dFile.seekg(10);
     dFile.read(reinterpret_cast<char *>(dInfo.offSet), sizeof(int));
     dFile.seekg(18);
     dFile.read(reinterpret_cast<char *>(dInfo.imageWidth), sizeof(int));
     dFile.seekg(22);
     dFile.read(reinterpret_cast<char *>(dInfo.imageHeight), sizeof(int));
}

So I try outFile.open(bmp.imgName, ios::binary);

With the code from above which to me sounds logical according to the information you gave me. Now the file does not want to open, No compiler errors though.

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.