Sometimes I need to generate some images with simple shapes for a little project, but i don’t want to install some heavy libraries, and this is why I decided to creaty my own single file library for simple image generation in C++: EasyBMP.
Full example: A 512x512 image with two Metaballs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "EasyBMP.hpp"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
struct point
{
double x, y;
point(double _x, double _y) : x(_x), y(_y) { }
double distanceTo(const point& o) {
double dx = (x - o.x);
double dy = (y - o.y);
return sqrt(dx * dx + dy * dy);
}
};
int main()
{
vector< point > centers = {
{256, 256},
{192, 192}
};
vector< double > charges = {1024.0, 2048.0};
// R, G, B [0, 255]
EasyBMP::RGBColor black(0, 0, 0);
// sizeX, sizeY, FileName, BackgroundColor
EasyBMP::Image img(512, 512, "sample.bmp", black);
for (int y = 0; y < 512; ++y) {
for (int x = 0; x < 512; ++x) {
double intensity = 0.0;
for (int i = 0; i < 2; ++i) {
double dist = max(centers[i].distanceTo(point(x, y)), 1.0);
intensity += charges[i] / (dist * dist);
}
int final_color = min(255, int(255. * intensity));
// PositionX, PisitionY, Color
img.SetPixel(x, y, EasyBMP::RGBColor(final_color, final_color, 0));
}
}
img.Write();
return 0;
}
Result:
EasyBMP
EasyBMP is an easy to use library to generate BMP images with a simple structure to prototype any project with image generation requirement.
The API it’s really simple.
The namespace it’s EasyBMP
.
This little tutorial won’t use “using namespace”.
You have 2 classes (EasyBMP::Image
, EasyBMP::RGBColor
).
EasyBMP::RGBColor
EasyBMP::RGBColor
just have 2 constructors:
1
2
EasyBMP::RGBColor(uint8_t r, uint8_t g, uint8_t b); // Just r,g,b
EasyBMP::RGBColor(); // Empty constructor, you need to assign values manually
This means that you have two ways to create an instance of EasyBMP::RGBColor
:
1
2
3
4
5
6
// Using the rgb constructor:
EasyBMP::RGBColor yellow1 = EasyBMP::RGBColor(255, 255, 0);
// Or using the void constructor and set values manually using SetColor:
EasyBMP::RGBColor yellow2;
yellow2.SetColor(255, 255, 0);
EasyBMP::SetColor
is defined as:
1
RGBColor::SetColor(uint8_t r, uint8_t g, uint8_t b);
I personally recommend to use the first option because code may have some errors if you forget to set color.
You can also acces variables using .r
, .g
or .b
:
1
2
EasyBMP::RGBColor yellow1 = EasyBMP::RGBColor(255, 255, 0);
uint8_t yellow1_green = yellow1.g;
EasyBMP::Image
You have some constructors to create an instance of an EasyBMP::Image
from scratch:
1
2
3
4
EasyBMP::Image(int64_t width, int64_t height);
EasyBMP::Image(int64_t width, int64_t height, string fileName);
EasyBMP::Image(int64_t width, int64_t height, RGBColor backgroundColor);
EasyBMP::Image(int64_t width, int64_t height, string fileName, RGBColor backgroundColor);
For example:
1
2
3
4
5
/*
This will create an image "img" with size 512x512,
on a file "sample.bmp", and the background will be yellow.
*/
EasyBMP::Image img(512, 512, "sample.bmp", EasyBMP::RGBColor(255, 255, 0));
And some others to create an instance of an EasyBMP::Image
from an already existing bitmap:
1
2
EasyBMP::Image(const string& _inFileName);
EasyBMP::Image(const string& _inFileName, const string& _outFileName);
For example:
1
2
3
4
5
/*
This will create an image "img" by loading "input.bmp".
"img" will be written to a new file named "output.bmp".
*/
EasyBMP::Image img("input.bmp", "output.bmp");
To modify a single pixel, you can use EasyBMP::Image::SetPixel
:
1
img.SetPixel(x, y, EasyBMP::RGBColor(r, g, b));
Defined as:
1
EasyBMP::Image::SetPixel(int64_t x, int64_t y, RGBColor color);
And to get a pixel value, EasyBMP::Image::GetPixel
:
1
img.GetPixel(x, y);
Defined as:
1
const RGBColor& EasyBMP::Image::GetPixel(int64_t x, int64_t y) const;