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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top