Solved

change a bmp file in VC++

Posted on 2004-10-15
6
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

737 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