I really don't know how to capture the information from the bitmap file when talking about the pixelArray, I know what I have to do it when I capture it to turn it negative, but each time I run my code it crashes when I run the code I think I have to use, please help.

// This program is meant to open a bitmap file of the user's choice 
// and read its content to the screen. It also takes pixel by pixel 
// converting each to its negative.

// Javier Falcon

// Directories
#include <iostream>
#include <cstdlib>
#include <windows.h> // Sleep
#include <fstream>   // fstream
#include <istream>   // seekg, seekp, read, write
#include <iomanip>   // setfill, setw, setprecision


using namespace std;

// Structure named Image
struct Image
{
    char imageName[80];
    int sizeFile,
        offSet,
        imageWidth,
        imageHeight;  
};

// Prototypes
void change2Negative(fstream &, Image &);
void read4mFile(fstream &, Image &);
void displayFileInfo(const Image &);
void displayErrorFileInfo(const Image &);

int main()
{
// Declare and Inizialize struct Image with variable bmp
    Image bmp;

// Title
    cout << setfill(' ') << setw(28) << " " << "Negative Picture Creator" << endl; 
    cout << setfill('-') << setw(80) << "-" << endl;  
    endl(cout);
// Ask use for input
    cout << "Please enter the name of the file: ";
    cin.getline(bmp.imageName, 50);
    
    endl(cout);
// Open file using input, open as binary.
    fstream theFile;
    theFile.open(bmp.imageName, ios::in | ios:: out | ios::binary);
    if(theFile.is_open())
    {
// Send to read4mFile to get information in file
        read4mFile(theFile, bmp);
// Send to displayFileInfo to display information
        displayFileInfo(bmp);  
// Send to change2Negative to convert values
        change2Negative(theFile, bmp);
// Close file
    theFile.close();
    }
    else
    { 
// Send to displayErrorFileInfo if file doesn't open
        displayErrorFileInfo(bmp);
        return 0;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
//****************************************************************************//
//
//
//
//
//
//
//****************************************************************************//
void read4mFile(fstream &dFile, Image &dInfo)
{
// Read information
     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));
}
//****************************************************************************//
//
//
//
//
//
//
//****************************************************************************//
void displayFileInfo(const Image &dInfo)
{
// Dynamic allocation to make sizeFile in struct usable in equations
    float *sizeFile;
    sizeFile = new float;
    *sizeFile = dInfo.sizeFile;
// Display information
    cout << "File Name: " << dInfo.imageName <<endl; 
    cout << "File Size: "; 
// Flags
    if(*sizeFile > 1000000)
    {
        cout << fixed << setprecision(2) << *sizeFile / 1000000 << " Mb" << endl;
    }
    else if(*sizeFile > 1000)
    {
        cout << fixed << setprecision(2) << *sizeFile / 1000 << " Kb" << endl;
    }
    else
    {
        cout << dInfo.sizeFile << " bytes" << endl;
    }
    cout << "File Offset: " << dInfo.offSet << "th byte" <<endl; 
    cout << "File Width: " << dInfo.imageWidth << " pixels" <<endl; 
    cout << "File Height: " << dInfo.imageHeight << " pixels" <<endl; 
// Delete borrowed memory
    delete sizeFile;
}
//****************************************************************************//
//
//
//
//
//
//
//****************************************************************************//
void change2Negative(fstream &dFile, Image &dInfo)
{
    int pixel[dInfo.imageWidth][dInfo.imageHeight];
    for(int i = 0; i < dInfo.imageWidth; i++)
    {
         for(int j = 0; j < dInfo.imageHeight; j++)
         {
             dFile.seekg(dInfo.offSet + i);
             dFile.read(reinterpret_cast<char *>(&pixel[i][j]), sizeof(pixel));
         }
    }
    
    for(int i = 0; i < dInfo.imageWidth; i++)
    {
         for(int j = 0; j < dInfo.imageHeight; j++)
         {
             dFile.seekg(dInfo.offSet + i);
             pixel[i][j] = 255 - pixel[i][j];
             dFile.write(reinterpret_cast<char *>(&pixel[i][j]), sizeof(pixel));
         }
    }
    
} 
//****************************************************************************//
//
//
//
//
//
//
//****************************************************************************//
void displayErrorFileInfo(const Image &dInfo)
{ 
// Error Message
    cout << "The file named " <<  dInfo.imageName << " was not found! Program will shutdown." << endl;
    endl(cout);
    endl(cout);
    cout << setfill(' ') << setw(35) << " ";
    for(int i = 0; i < 10; i++)
    {
        cout << ".";
        Sleep(300);
    }   
}

Recommended Answers

All 4 Replies

If you run this under a debugger, it'll tell you which line of code is causing the crash.

My IDE doesn't have a debugger, but I will try that thanks.

Is there any clue? An unhandled exception? A segfault?

I figured it out ^.^

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.