[RESOLVED] Loading and altering a BMP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] Loading and altering a BMP

  1. #1
    Join Date
    Aug 2010
    Posts
    47

    [RESOLVED] Loading and altering a BMP

    Hi there, I'm a reasonably experienced programmer but I'm new to C++. I currently have a fairly simple task in front of me, but I keep getting bogged down in fiddly details. I've done numerous searches on the web, which turn up all sorts of promising code... which never quite works, and being new to C++ I have difficulty figuring out why.

    I'm currently using the trial version of Professional Visual Studio 2010 (we'll buy it, assuming I can get this working). In a nutshell what I need to do is load a bitmap from a file, display it, copy it, alter the copy somehow, then display the copy. For altering the copy I need to be able to get and set arbitrary pixels. I've got some of this kind of working, but really I'd rather just throw it out and use something better programmed.

    What I've got working is a bit ugly, but it loads the bmp from a file, puts it into a picture control, copies it, makes some edits, then puts it into another picture control. The real problem is that I don't understand the format I'm being given... there seems to be unused portions at the end of each scanline (padding for some structure I guess) but I can't seem to predict how much there will be from one bmp to the next.

    Today I tried to install ImageMagick, but it wouldn't work... just using the InitializeMagick function would give me unresolved symbol errors, and I've tried telling it where to look for header and library files. I've also tried copying the libraries into various directories in my visual studio install. So that isn't really working for me.

    I would think someone would have come up with a decent and simple interface to this sort of thing by now, but so far my searches have only turned up incomplete code that usually needs modifications to even compile (and I'm not confident my modifications are correct, being a newbie). What I really need is a simple class that implements an interface that would flow something like:
    bmp.LoadFromFile("somefile.bmp");
    pixelwidth = bmp.getpixelwidth();
    pixelheight = bmp.getpixelheight();
    rgbvalues = bmp.getpixel(x,y);
    bmp.setpixel(x,y,rgbvalues);

    You know, a simple straight forward interface that a newb like me can grok. Most importantly though, I need something that will compile on visual studio 2010 without me having to make major modifications.

    Would anyone know where I could find some code that would fit the bill for me?

    I'd appreciate any help people could give me.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Loading and altering a BMP

    Quote Originally Posted by Ankheg View Post
    The real problem is that I don't understand the format I'm being given... there seems to be unused portions at the end of each scanline (padding for some structure I guess) but I can't seem to predict how much there will be from one bmp to the next.
    Scanlines are supposed to be word-aligned to be properly handled by Windows -- that's why the extra space is there.

    Seriously, this topic is just too large for a single forum message. Just the fact that you didn't know about the scanline's in bitmaps being aligned as a requirement for it to be displayed properly is an indication you have a large learning curve.
    Today I tried to install ImageMagick, but it wouldn't work... just using the InitializeMagick function would give me unresolved symbol errors, and I've tried telling it where to look for header and library files. I've also tried copying the libraries into various directories in my visual studio install. So that isn't really working for me.
    I'm sure others do have ImageMagick working, so it's something you are doing wrong, or something you think you're doing correctly, but is still wrong.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2008
    Posts
    118

    Re: Loading and altering a BMP

    If all you want to do is grab and and alter bmp files, take a look at EasyBMP, over on

    http://easybmp.sourceforge.net/

    Version 1.0 is a single *.h file, containing everything you need. All you need do is include it in your build (which as I recall, under Visual Studio is really easy - something along the lines of "project->add file to project", or the like), and you've got access to all its classes, structures and functions. Versions after 1.0 split the code a little, but it's still pretty easy. Just copy the source files, put them in your project directory, ensure that Visual Studio has added them to the project, however it does that, and you're done.

    Sounds like you're trying to learn a new programming environment AND learn BMP files at the same time. I think that having a single header file to deal with will enable you to put aside the new environment and get on with BMP manipulation.

    It doesn't handle all possible kinds of BMP, but with any luck the ones you need to handle are covered, and whilst having the entire thing in a single *.h file isn't great coding practice, it does make including its functionality very easy.


    Edit: Just because I think EasyBMP might be exactly what you're looking for, I think your code above becomes something like this (not compiled or anything, just me playing around):

    Code:
    // bmp.LoadFromFile("somefile.bmp");
    BMP theBMPImage;
    theBMPImage.ReadFromFile("somefile.bmp");
    
    
    //pixelwidth = bmp.getpixelwidth();
    // Presumably you want the width of the image in pixels, not the width of a single pixel :)
    pixelwidth = theBMPImage.TellWidth();
    
    //pixelheight = bmp.getpixelheight();
    pixelheight = theBMPImage.TellHeight();
    
    //rgbvalues = bmp.getpixel(x,y);
    RGBApixel thePixelValues = theBMPImage.GetPixel(x,y);
    
    //bmp.setpixel(x,y,rgbvalues);
    theBMPImage.SetPixel(x,y,rgbvalues);

    My God, it's spooky how neatly your code maps into easyBMP!

    "I would think someone would have come up with a decent and simple interface to this sort of thing by now"

    Paul Macklin has, and there it is.
    Last edited by Moschops; August 19th, 2010 at 09:26 AM.

  4. #4
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Loading and altering a BMP

    Welcome to C++ , if your are new persist , Once you have mastered C/ C++ you ill not be able to go back to any other language you will find all of them to very limiting , to your question , BMP image format is well documented in wingdi.h , he is a simple C++ code I have written to help you get started work with VS 2010
    Code:
    // bitmapped.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <Windows.h>
    #include <string>
    #include <stdio.h>	
    #include <WinGDI.h>
    #include <Windows.h>
    
    
    using namespace std;
    #pragma pack(push,1)
     typedef struct  _Fheader
    {
    	unsigned  short a;
    	unsigned  int imagesizeb;
    	unsigned  short resc;
    	unsigned  short  resd;
    	unsigned  int offset;
    
    } Fheader;
    #pragma pack(pop)
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Fheader fheader;
    	BITMAPINFOHEADER mapinfoheader;
    	 cout << sizeof(Fheader) << endl ;
    	 cout << sizeof(BITMAPINFOHEADER) << endl;
    	string filename("c:\\aaa.bmp");
    	
    	fstream file;
    	file.open(filename.data(),std::ios::binary| std::ios::in);
    	if(file.is_open() == false)
    	{
    		return 1;
    	}
    	if(!file.read((char *)&fheader,sizeof(Fheader)))
    	{
    		return 1;
    	}
    	file.read((char *) &mapinfoheader,sizeof(BITMAPINFOHEADER));
    
    
    	cout << fheader.a  << endl;
    	cout << fheader.imagesizeb << endl;
    	cout << fheader.resc << endl;
    	cout << fheader.resd << endl;
    	cout << fheader.offset << endl;
    	cout << mapinfoheader.biSize << endl;
    	cout << mapinfoheader.biWidth << endl;
    	cout << mapinfoheader.biHeight << endl;
    	cout << mapinfoheader.biSizeImage << endl;
    	unsigned char * imagedata1;
    	imagedata1  = new byte [mapinfoheader.biSizeImage];
    	cout << sizeof(imagedata1) << endl;
    	file.read((char *) imagedata1,mapinfoheader.biSizeImage);
    	for(int i = 0;i < mapinfoheader.biSizeImage;i++)
    	{
    
    		cout << (int)imagedata1[i]  << endl;
    		
    	}
    	delete imagedata1;
    	
    	 
    	Sleep(10000);
        file.close();
    	return 0;
    }

  5. #5
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Loading and altering a BMP

    Quote Originally Posted by aamir121a View Post
    he is a simple C++ code I have written to help you get started work with VS 2010
    Code:
    //...
    delete imagedata1;
    Correction
    Code:
    delete [] imagedata1;
    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 2008
    Posts
    118

    Re: Loading and altering a BMP

    As an aside, there is no need to typedef a struct in C++. One can simply declare it as

    Code:
    struct someStructure{
       int a;
       int b;
    };
    and then just use someStructure like any other data type - no need to say struct someStructure when creating an instance.

  7. #7
    Join Date
    Aug 2010
    Posts
    47

    Re: Loading and altering a BMP

    Thanks for the replies. I found your posts helpful, and have had it working for a few days now... just thought I'd drop you guys a line saying thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center