Solved

change a bmp file in VC++

Posted on 2004-10-15
6
461 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Accepted Solution

by:
Feldspar earned 450 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 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

10 Experts available now in Live!

Get 1:1 Help Now