Lib BitMaplusplus
Developed by Christian Visintin
Version 1.1.1 (07/09/2019)
libBmpp is a library for working with Bitmaps C++11 (or greater), which allows you to manipulate bitmaps images.
libBmpp build is achievable through autotools, issuing the following commands:
./autogen.sh
./configure
make
make install
typedef struct Header {
uint16_t bmpId;
uint32_t fileSize;
uint32_t unused = 0;
uint32_t dataOffset = 54;
//DIB
uint32_t dibSize = 40;
uint32_t width;
uint32_t height;
uint16_t colorPlanes = 1;
uint16_t bitsPerPixel;
uint32_t biRgb = 0;
uint32_t dataSize;
uint32_t printSizeW;
uint32_t printSizeH;
uint32_t paletteSize;
uint32_t importantColors;
} Header;
Header is the bitmap header storage structure and contains all the data which are usually stored into the bitmap header.
Bmp class is the main class of libBmpp. It is the superclass of every kind of Bitmap and takes care of handling pixels independently from the bits per pixel which is currently used. Bmp class also encodes and decodes the header and prepare the buffer for the raster data. Every kind of Bmp (Bmp24 etc) must inherits from Bmp class.
bmp::Header* header;
header represents the bitmap header, which is stored into a Header structure.
Bmp();
Bmp constructor initializes header to nullptr
Bmp(std::vector<bmp::Pixel*> pixelArray, size_t width, size_t height);
It is possible to create a new Bmp object passing a pixel array (of any kind), width and height of the image to create a new bitmap.
There are also 2 copy constructors which copies both the header and the pixels
Bmp(const bmp::Bmp& bmp);
Bmp(Bmp* bmp);
~Bmp();
Bmp destructor deletes header structure.
bool decodeBmp(uint8_t* bmpData, size_t dataSize);
Given the buffer read from bitmap file, this method decodes the bitmap header and store its attributes in the struct. The raster data parsing is not provided by this method and must be implemented by the classes which extends Bmp.
If was unable to parse the bmpData or was invalid returns false.
uint8_t* encodeBmp(size_t& dataSize);
Starting from the header struct, this method creates the image buffer and returns a pointer to the buffer allocated. This method takes care only of allocating enough space to put the raster data and filling the first 54 bytes with the header. The encoding of the raster data is a task for the class which extends Bmp.
If was unable to allocate the buffer, returns nullptr
Reads a bmp file and decodes it. The decoded bitmap's header and pixels become attributes of the object.
bool readBmp(const std::string& bmpFile);
Encodes the bitmap stored in the object and writes a bmp file.
bool writeBmp(const std::string& bmpFile);
bool rotate(int degrees);
Rotates the BMP image of 'degrees', which must be multiple of 90 (90/180/270/360).
If was unable to rotate the image, returns false
bool flipVertical();
Flip the image vertically
bool flipHorizontal();
Flip the image vertically
Resize the image area, an offset can be specified.
bool resizeArea(size_t width, size_t height, size_t xOffset = 0, size_t yOffset = 0);
Resize the image
bool resizeImage(size_t width, size_t height);
size_t getWidth();
Returns the image width in pixels
size_t getHeight();
Returns the image height in pixels
Bmp8 is a class which extends Bmp class and describes a 8 bits for pixel Bitmap.
In addition to Bmp methods, it provides the following methods:
bool setPixelAt(size_t row, size_t column, uint8_t value);
bool setPixelAt(size_t index, uint8_t value);
set the 255 value of the pixel in index position.
Returns false if the requested pixel, does not exist.
bmp::BytePixel* getPixelAt(size_t row, size_t column);
bmp::BytePixel* getPixel(size_t index);
Returns the pointer to the BytePixel in provided position. If the requested pixel does not exist, returns nullptr
Bmp8 is a class which extends Bmp class and describes a 16 bits for pixel Bitmap.
In addition to Bmp methods, it provides the following methods:
bool setPixelAt(size_t row, size_t column, uint16_t value);
bool setPixelAt(size_t index, uint16_t value);
set the 255 value of the pixel in index position.
Returns false if the requested pixel, does not exist.
bmp::WordPixel* getPixel(size_t row, size_t column);
bmp::WordPixel* getPixel(size_t index);
Returns the pointer to the BytePixel in provided position. If the requested pixel does not exist, returns nullptr
Bmp24 is a class which extends Bmp class and describes a 24 bits for pixel Bitmap.
In addition to Bmp methods, it provides the following methods:
bool setPixelAt(size_t row, size_t column, uint8_t red, uint8_t green, uint8_t blue);
bool setPixelAt(size_t index, uint8_t red, uint8_t green, uint8_t blue);
set the RGB value of the pixel in index position.
Returns false if the requested pixel, does not exist.
bool toGreyScale(int greyLevels = 255);
Converts a color image to greyScale. Is it possible to specify the amount of grey levels (2 - 255).
bool toSepiaTone();
Converts image to sepia tone.
bool invert();
Invert image colors.
bmp::RGBPixel* getPixel(size_t row, size_t column);
bmp::RGBPixel* getPixel(size_t index);
Returns the pointer to the RGBPixel in provided position. If the requested pixel does not exist, returns nullptr
Bmp32 is a class which extends Bmp class and describes a 32 bits for pixel Bitmap. (RGBa)
In addition to Bmp methods, it provides the following methods:
bool setPixelAt(size_t row, size_t column, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
bool setPixelAt(size_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha);
set the RGBa value of the pixel in index position.
Returns false if the requested pixel, does not exist.
bool toGreyScale(int greyLevels = 255);
Converts a color image to greyScale. Is it possible to specify the amount of grey levels (2 - 255).
bool toSepiaTone();
Converts image to sepia tone.
bool invert();
Invert image colors.
bmp::RGBAPixel* getPixel(size_t row, size_t column);
bmp::RGBAPixel* getPixel(size_t index);
Returns the pointer to the RGBAPixel in provided position. If the requested pixel does not exist, returns nullptr
Bmpmonochome is a class which extends Bmp class and describes a 1 bits for pixel Bitmap. (BW)
In addition to Bmp methods, it provides the following methods:
bool setPixelAt(size_t row, size_t column, uint8_t value);
bool setPixelAt(size_t index, uint8_t value);
bmp::BWPixel* getPixel(size_t row, size_t column);
bmp::BWPixel* getPixel(size_t index);
Returns the pointer to the BWPixel in provided position. If the requested pixel does not exist, returns nullptr
Parses a bmp and returns a pointer to a Bmp type (e.g. bmp8, bmp24 ...).
bmp::Bmp* getBmp(uint8_t* data, size_t dataSize, size_t& bitsPerPixel);
- Added missing
#include <string>
in bmp.hpp
- Added copy constructors for bitmaps
- Added new constructor for creating an empty bmp
- Removed pixel array constructor due to possible memory corruption; use setPixelAt instead
- Changed int params to size_t
- Added method to write BMP to file
- Use reference instead of pointer
- Added const type when necessary
- Added getPixelAt with vector index as argument
MIT License
Copyright (c) 2019 Christian Visintin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.