[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

change a bmp file in VC++

Posted on 2004-10-15
6
Medium Priority
?
469 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Accepted Solution

by:
Feldspar earned 1800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

656 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