Solved

change a bmp file in VC++

Posted on 2004-10-15
6
462 Views
Last Modified: 2012-05-05
Hi experts,

   In VC++, is that possible to load a bmp file, change some pixels (e.g. change all the colored pixels to black", and then output to another bmp file ???

  many thanks !!!
0
Comment
Question by:meow00
  • 3
  • 2
6 Comments
 
LVL 13

Assisted Solution

by:SteH
SteH earned 50 total points
ID: 12317850
Why not?
Have look a CImage or CBitmap classes. They can read their content from a file and write it back. And you can modify pixels.
0
 
LVL 1

Author Comment

by:meow00
ID: 12318380
thanks very much ....

so could anyone please give me some more example or reference about this ???
I want to change all non-white colors to black, and the black color to white .....

please help ..s many thanks !!!
0
 
LVL 1

Expert Comment

by:Feldspar
ID: 12318404
Heres some code for reading a bitmap file into an array, modifying values, and writing it to another file:

BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;

ifstream fin(inputfilename, ifstream::binary);

fin.read((char*)&bmfh, sizeof(BITMAPFILEHEADER));
fin.read((char*)&bmih, sizeof(BITMAPINFOHEADER));

// width, height, and bytesperpixel can be read from the bitmap info header if needed

char pixels[width][height][bytesperpixel]; // if you want the data in a pixel array

for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
            fin.read(pixels[width][height], bytesperpixel);
      }                        
}

// modify data here

// output to file

ofstream fout(outputfilename, ifstream::binary);

fout.write((char*)&bmfh, sizeof(BITMAPFILEHEADER));
fout.write((char*)&bmih, sizeof(BITMAPINFOHEADER))

for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
            fout.write(pixels[width][height], bytesperpixel);
      }                        
}




0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
Feldspar earned 450 total points
ID: 12318466
changing the colors will depend on the bit depth of the bitmap file.
for 24-bit color, you will read 3 bytes for each pixel, as red green and blue components, each form 0-255
so white would be (255, 255, 255)

if (pixels[width][height][0] == 255 && 
    pixels[width][height][1] == 255 && 
    pixels[width][height][2] == 255) {

    // white - do nothing

} else if if (pixels[width][height][0] == 0 && 
    pixels[width][height][1] == 0 && 
    pixels[width][height][2] == 0) {

      // its black so set to white
   pixels[width][height][0] == 255 ;
   pixels[width][height][1] == 255 ;
   pixels[width][height][2] == 255 ;

} else {
      // set it to black
   pixels[width][height][0] == 0;
   pixels[width][height][1] == 0;
   pixels[width][height][2] == 0;

}
0
 
LVL 1

Author Comment

by:meow00
ID: 12325723
thanks very much .... I tried the above method, but could not even read the bmp files ..... did I miss anything somewhere ??? many thanks !!!

----------------
#include "stdio.h"
#include "iostream.h"
#include "fstream.h"
#include "conio.h"
#include "windows.h"

#define inputfilename "werewolf3.bmp"
#define outputfilename "output.bmp"

#define width 80
#define height 110
#define bytesperpixel 3



int main(){

BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;

ofstream output("bug.txt") ;
ifstream fin(inputfilename, ifstream::binary);

fin.read((char*)&bmfh, sizeof(BITMAPFILEHEADER));
fin.read((char*)&bmih, sizeof(BITMAPINFOHEADER));

// width, height, and bytesperpixel can be read from the bitmap info header if needed

char pixels[width][height][bytesperpixel]; // if you want the data in a pixel array

output << " before read files " << endl ;

for (int y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
          fin.read(pixels[width][height], bytesperpixel);
     }                    
}

output << " after read files" << endl ;
// modify data here
  if (pixels[width][height][0] == 255 && 
    pixels[width][height][1] == 255 && 
    pixels[width][height][2] == 255) {

    // white - do nothing

} else if(pixels[width][height][0] == 0 && 
    pixels[width][height][1] == 0 && 
    pixels[width][height][2] == 0) {

      // its black so set to white
   pixels[width][height][0] = (char)255 ;
   pixels[width][height][1] =  (char)255 ;
   pixels[width][height][2] = (char)255 ;

} else {
      // set it to black
   pixels[width][height][0] = (char)0;
   pixels[width][height][1] = (char)0;
   pixels[width][height][2] = (char)0;

}
// output to file
output << "after change colors " << endl ;

ofstream fout(outputfilename, ifstream::binary);

fout.write((char*)&bmfh, sizeof(BITMAPFILEHEADER));
fout.write((char*)&bmih, sizeof(BITMAPINFOHEADER)) ;

for ( y = 0; y < height; y++) {
     for (int x = 0; x < width; x++) {
          fout.write(pixels[width][height], bytesperpixel);
     }                    
}

output << "write files done !" << endl ;
 return 0 ;
}//main
//-----------------------------
and here is the bug.txt :

 before read files
0
 
LVL 1

Author Comment

by:meow00
ID: 12325761
sorry .... pixels[width][height] --> pixels[x][y],

stupid mistake .... got the file input and output ... but the output isn't right yet ..... :(
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now