Load, Modify and Create a BMP file with C++

The Pigeon

Cool Member
Reaction score
0
Hi,

I'm using Microsoft Visual Studio 2010 and I would like to be able to load a BMP file, modify it based on the colors it has and create a new BMP file with the modifications. I've googled around and have seen some code, but didn't understand it all or at least how to modify it to do what I want it to.

Would anyone know how to code this?
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
You'll need a graphics library first and maybe a more detailed question.
From what I can tell, you want to:
+ Load an image
+ Read the RGBA values of a pixel on the image
+ Write the RGBA values of a pixel on the image
+ Save an image

You could use SDL if you just want something quick and dirty.
But do you know how to set up a VS2010 project using libraries?

http://www.libsdl.org/download-1.2.php <--
http://www.libsdl.org/projects/SDL_image/ <--

You'll need those two to get started.
 

The Pigeon

Cool Member
Reaction score
0
Cheers, I'll take a look.

What I want to do is basically what you said;
+ Load and Image
+ Create a new image based off pixel color of the old
+ Save the new image

I'm making an overworld map for an old game called arcanum. The game's world editor generates a simple map which looks like it came from MS paint. Water is one color, forest another, mountains another etc. I want to make mine look a little more like the more fancy version that comes with the game.

So all areas have a random variety of pixel color, being shades of blue, green, brown etc with a few shade of light brown over everything to make it look like the piece of parchment like in game. I also want to draw thin black borders around the landmasses and rivers (say 1 to 3 pixels thick).
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
SDL_Surface is your friend.
A quick example (which may not compile, I haven't tried to run it):
Code:
#include <SDL.h>

void getPixelColor (SDL_Surface* surface, int x, int y, Uint8& r, Uint8& g, Uint8& b, Uint8& a) {
    Uint32* pixels = static_cast<Uint32*>(surface->pixels);
    Uint32  color  = pixels[y*surface->w + x];
    
    SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);
}

void setPixelColor (SDL_Surface* surface, int x, int y, Uint32 color) {
    Uint32* pixels = static_cast<Uint32*>(surface->pixels);
    pixels[y*surface->w + x] = color;
}

Uint32 toUint32Color (SDL_Surface* surface, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
    return SDL_MapRGBA(surface->format, r, g, b, a);
}

int main (int argc, int* argv[]) {
    char const* filename = "Some path to your image";
    //Initialize SDL the lazy-man's way
    SDL_Init(SDL_INIT_EVERYTHING);
    //Create the window
    SDL_Surface* screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    //Load an image
    SDL_Surface* image  = IMG_Load(filename);

    //Clear screen
    //I don't remember if alpha is supposed to be 0 or 255; I always mix alpha up
    static Uint32 const black = toUint32Color(screen, 0, 0, 0, 0);
    SDL_FillRect(screen, nullptr, black);

    //Draw the image to the screen
    SDL_Rect dst = {
        32, //x pos
        32, //y pos
        0, 0
    };
    SDL_BlitSurface(image, nullptr, screen, &dst);

    //"Flip" the screen to show the drawn image
    SDL_Flip(screen);

    //Wait for at least 2000 milliseconds
    SDL_Delay(2000);
    
    //Free the image
    SDL_FreeSurface(image);

    //You don't ever free the screen

    //Quit SDL
    SDL_Quit();
}

Oh, as for saving images..
I think SDL_SaveBMP() will work, never used it myself
 

The Pigeon

Cool Member
Reaction score
0
Cheers, made a new project and put the code in (using visual studio 2010), but it can't find sdl.h I know I figured out how to fix this ages ago, can't remember how though. I think I'd have to find the library of sdl files somewhere, but I haven't found em yet
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
You have to download them?
You should have two (or more) folders, one should be the "bin" and the other the "include".
Inside the "include" folder should be SDL.h

Once you've found it, you may know where it is, but VS2010 doesn't.
You will need to tell it where SDL.h is.

You can do that by going to the project properties.
You can access the project properties by going to VS2010 and then pressing:
01) Alt
02) P

A drop-down menu should appear with the phrase "<insert project name> Properties ...".
Click on that.
Then, go to: Configuration Properties > VC++ Directories
You should see "Include Directories" on the right panel.
Add in the path to the folder that contains SDL.h from there.

Then, there are also Library Directories..
You will need to set that up, it's all those .lib files in the "bin" folder.

Add in the path to the folder that contains the .lib files for SDL under "Library Directories".
Not the path the the .lib files themselves but the folder that contains them.

Then, go to: Configuration Properties > Linker > Input
You should see "Additional dependencies" on the right panel.
Go ahead and add in the name of the .lib files that you need, no need to specify the path.
Like,
"SDLmain.lib;SDL_image.lib;"

Finally, you will need to build the program, fail to run as it says you're missing some DLLs and then transfer the .dll files into your solution folder or debugging folder.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top