Urgent-reading bitmap images in c++, kindly help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gagansingh
    New Member
    • Dec 2006
    • 2

    Urgent-reading bitmap images in c++, kindly help

    guys,

    I really want to know how i can process images in c++ (.bmp)
    Last edited by gagansingh; Dec 11 '06, 06:24 AM. Reason: none
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Originally posted by gagansingh
    guys,

    I really want to know how i can process images in c++ (.bmp)
    Hi,

    What is the process you want to do with the bmp images?
    For eg., display the bmp image

    Also check this link.

    Read BMP Images


    Regards,
    M.Sivadhas.

    Comment

    • macklin01
      New Member
      • Aug 2005
      • 145

      #3
      Originally posted by sivadhas2006
      Hi,

      What is the process you want to do with the bmp images?
      For eg., display the bmp image

      Also check this link.

      Read BMP Images


      Regards,
      M.Sivadhas.
      You can use my open source C++ bitmap library (EasyBMP) to read, modify, and write bitmap images. Here's a sample (to invert the colors, as in a photograph negative):

      Code:
      #include <cstdio>
      #include <cstdlib>
      #include <iostream>
      
      using namespace std;
      
      #include "EasyBMP.h"
      
      int main( int argc, char* argv[] )
      {
       if( argc < 3 )
       {
        cout << "Correct usage: InvertColors <input.bmp> <output.bmp>" << endl;
        return -1;
       }
      
       // declare and read the image
      
       BMP Image;
       Image.ReadFromFile( argv[1] );
      
       // do operations on the pixels
      
       for( int j=0; j < Image.TellHeight() ; j++ )
       {
        for( int i=0; i < Image.TellWidth() ; i++ )
        {
         Image(i,j)->Red   = 255 - Image(i,j)->Red;
         Image(i,j)->Green = 255 - Image(i,j)->Green;
         Image(i,j)->Blue  = 255 - Image(i,j)->Blue;
        }
       }
      
       // write the modified image
      
       Image.WriteToFile( argv[2] );
      
       return 0;
      }
      1, 4, 8, 16, 24, and 32 bits per pixel are supported. It's also a cross-platform/cross-compiler library.

      Please post here if you have additional questions. Thanks -- Paul

      Comment

      • bryantkob
        New Member
        • Feb 2010
        • 3

        #4
        flipping an image vertically and adjusting brigthness

        Hi,

        I've been trying to flip a bmp image vertically through the center of the image but i dont think im doing it right.

        i have so far

        BMP img;

        img.ReadToFile( "in.bmp");

        int width = img.TellWidth() ;
        int height = img.TellHeight( );

        for(int i=0; i<width; i++){
        for(int j=0; j<height; j++){
        img(i,j)->Red = img(j,i) ->Red;
        img(i,j)->Green = img(j,i) ->Green;
        img(i,j)->Blue = img(j,i) ->Blue;
        }
        }

        img.WriteToFile ("out.bmp");

        I dont have any idea on how to adjust the brigthness of an image, any help will be greatly appreciated.

        Thanks in advance for your help.

        Comment

        • macklin01
          New Member
          • Aug 2005
          • 145

          #5
          Hi, Bryant.

          In the code above, you are overwriting the pixels of img as you go, which means that you will lose half the image before you finish.

          Instead, make a second, blank image, say:

          BMP blank;
          blank.SetSize( img.TellWidth() , img.TellHeight( ) );

          Then, use your same code, only with "img" as the source and "blank" as the destination.

          As for brightness balance, that's something you would need to code yourself, because EasyBMP is solely for reading and writing pixels -- it's up to you to decide what to do to them. :-)

          However, I have done the following simple "normalization" :

          ebmpBYTE max_Red = 0;
          ebmpBYTE max_Green = 0;
          ebmpBYTE max_Blue = 0;

          for( int i = 0 ; i < img.TellWidth() ; i++ )
          {
          for( int j=0 ; j < img.TellHeight( ) ; j++ )
          {
          if( img(i,j)->Red > max_Red )
          { max_Red = img(i,j)->Red; }
          // similar for red and green.
          }
          }

          ebmpBYTE red_adjust = 255 - max_Red;
          // similar for green_adjust, blue_adjust

          for( int i=0; i < img.TellWidth() ; i++ )
          {
          for( int j = 0 ; j < img.TellHeight( ) ; j++ )
          {
          img(i,j)->Red += red_adjust;
          // similar for green, blue.
          }
          }

          Depending upon the image, that may or may not do the trick, but is a nice start. More advanced ways would be to convert the image to a HSL or HSV color space and do computations on L (lightness) or V (value), but that's a rather bit more complex.

          Another alternative:

          Just go through and calculate "max_lightn ess" like this:

          double max_lightness = 0;
          // loop over all pixels

          double temp_lightness = 0.3 * img(i,j)->Red + 0.59 * img(i,j)->Green + 0.11 * img(i,j)->Blue;
          if( temp_lightness > max_lightness )
          { max_lightness = temp_lightness; }

          // then loop over all pixels again and rescale the image such that
          // the max lightness is 255.0

          double temp_lightness = 0.3 * img(i,j)->Red + 0.59 * img(i,j)->Green + 0.11 * img(i,j)->Blue;

          double temp_red = img(i,j)-Red * ( 255.0 / temp_lightness );
          img(i,j)-> = (ebmpBYTE) ( temp_red );
          // similar for green and blue

          The effect should be to change the maximum lightness to 255 while maintaining the same relative difference between all the pixels. Both methods are probably worth examining.

          You might want to add a "rounding" or "floor" type of operation prior to the last (ebmpBYTE) conversion.

          I hope that helps. -- Paul

          Comment

          • bryantkob
            New Member
            • Feb 2010
            • 3

            #6
            hi Paul,

            Thanks so much for the detailed response.
            One of the major requirement is that i should not declare a blank image. the requirement is as follow:

            "flip a image about a vertical line through its center by swapping pixels

            You must implement this function with space efficiency. That is, you should not declare a new empty Image into which you copy pixels. Rather, you should just use a temporary variable to swap pixels within the original Image"

            I am to alter an already existing image. i cant create a blank image and copy an image to it pixel by pixel.

            than you so much for your help. I hope you have the time to help me more
            bryant

            Comment

            • macklin01
              New Member
              • Aug 2005
              • 145

              #7
              Ah, I see. Good point.

              Something like this, then:

              // loop over all pixels
              RGBApixel temp = *img(i1,j1);
              *img(i1,j1) = *img(i2,j2);
              *img(i2,j2) = temp;

              // or like this
              // loop over all pixels

              BYTE temp = img(i1,j1)->Red;
              img(i1,j1)->Red = img(i2,j2)->Red;
              img(i2,j2)->Red = temp;
              // repeat for green, blue, alpha

              The latter is more tedious but takes 3 bytes less memory (:rolleyes:).

              Comment

              • bryantkob
                New Member
                • Feb 2010
                • 3

                #8
                You are the best. Thanks a lot. I'll implement this and will let you know if I'm having more problems. Please be online tomorrow also, if you have the time.

                Thanks Paul.

                Bryant.

                Comment

                Working...