I am writing a program to make pictures into their negative. But I do not know how to open the bitmap file, I don't know if I should open it as a binary, when I try to my security coding kicks in, and when I open as ios::out my security coding doesn't kick in and it shows me allocated garbage. Please help.

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


using namespace std;

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


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

int main()
{
    Image bmp;
    
    cout << setfill(' ') << setw(28) << " " << "Negative Picture Creator" << endl; 
    cout << setfill('-') << setw(80) << "-" << endl;  
    endl(cout);
    
    cout << "Please enter the name of the file: ";
    cin.getline(bmp.imageName, 50);
    
    endl(cout);
    
    fstream outFile;
    outFile.open(bmp.imageName, ios::binary);
    if(outFile.is_open())
    {
        read4mFile(outFile, bmp);
        displayFileInfo(bmp);  
        
    outFile.close();
    }
    else
    { 
        cout << "The file named " <<  bmp.imageName << " was not found! Program will shutdown." << endl;
        Sleep(1500);
        return 0;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

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));
}

void displayFileInfo(const Image &dInfo)
{
    cout << "File Name: " << dInfo.imageName <<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; 
}

I am trying to get all my information from a 24 bit True color bmp.

Recommended Answers

All 4 Replies

Are you opposed to using a library? ImageMagick, ITK, VTK, VIL (from VXL), CImg, and OpenCV should all do the trick :)

I don't have enough knowledge to use another library, once I get the file to actually open; the rest of the coding is elementary lol. I just don't know how get the bmp to open for my program, how to call it from my coding when the user inputs name... And if I should open it in binary.

I think something like this should work :

#include <fstream>
#include <iostream>
#include <string>
#include "CommonFunctions.h"
using namespace std;

struct BitmapInfo{
	unsigned char id[2];
	unsigned char size[4];
	unsigned char unUsed[4];
	unsigned char pixelOffset[4];
	unsigned char bytesInDIBHeader[4];
	unsigned char width[4];
	unsigned char height[4];
	BitmapInfo() : id(), size(),unUsed(),pixelOffset(),bytesInDIBHeader(),width(),height() {}
};
int main() {  
 ifstream fileIn("test.bmp", ios::binary);
 BitmapInfo bmpInfo;
 fileIn.read(reinterpret_cast<char*>(&bmpInfo),sizeof(bmpInfo));
 //...bmpInfo should have correct results, print and check   
 return 0;
}

I'm something of a fan of the easyBMP library.

Here's some incomplete pseudo-code that should demonstrate the ease of fiddling with bmp images using it.

#include "EasyBMP.h
BMP theBMPImage;
theBMPImage.ReadFromFile("somefile.bmp");
width = theBMPImage.TellWidth();
height = theBMPImage.TellHeight();

// Over all pixels, using height and width in a loop..
  // Readpixel
  RGBApixel thePixelValues = theBMPImage.GetPixel(x,y);

  // Carry out your negative function on thePixelValues object...

  // Write pixel back into bmp object
  theBMPImage.SetPixel(x,y,rgbvalues);

  // Alternatively, something like this, where 14 and 18 are just example pixel locations:
   theBMPImage(14,18)->Red = negativiseFunctionOnRed(theBMPImage(14,18)->Red);
   theBMPImage(14,18)->Green = negativiseFunctionOnGreen(theBMPImage(14,18)->Green);
   theBMPImage(14,18)->Blue = negativiseFunctionOnBlue(theBMPImage(14,18)->Blue);
   theBMPImage(14,18)->Alpha = negativiseFunctionOnAlpha(theBMPImage(14,18)->Alpha);

//

theBMPImage.WriteToFile("outputFilename.bmp");

There's nothing wrong, of course, with reading the data directly from the file into a structure you define yourself, but the easyBMP library does make it awfully easy. If you get version 1.0, it's literally a single header file to include - nothing else whatsoever and no compiled library to link to.

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.