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,

//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;  
    cout << "Please enter the name of the file: ";
    cin.getline(bmp.imageName, 50);
    fstream outFile;
    outFile.open(bmp.imageName, ios::binary);
        read4mFile(outFile, bmp);
        cout << "The file named " <<  bmp.imageName << " was not found! Program will shutdown." << endl;
        return 0;
    return EXIT_SUCCESS;

void read4mFile(fstream &dFile, Image &dInfo)
     dFile.seekg(0, ios::beg);
     dFile.read(reinterpret_cast<char *>(&dInfo.sizeFile), sizeof(int));
     dFile.read(reinterpret_cast<char *>(&dInfo.offSet), sizeof(int));
     dFile.read(reinterpret_cast<char *>(&dInfo.imageWidth), sizeof(int));
     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.

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;
 //...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;
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

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



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.

Edited 5 Years Ago by Moschops: n/a

This article has been dead for over six months. Start a new discussion instead.