Solved

change a bmp file in VC++

Posted on 2004-10-15
6
465 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

735 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