Skip to content

Commit

Permalink
C++ Bitmap Image Reader Writer Library http://partow.net/programming/…
Browse files Browse the repository at this point in the history
  • Loading branch information
ArashPartow committed Sep 3, 2019
1 parent 7b671cb commit 624fb94
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: cpp

sudo: required

dist: trusty
dist: xenial

compiler:
- gcc
Expand Down
67 changes: 43 additions & 24 deletions bitmap_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class bitmap_image
red_plane = 2
};

struct rgb_t
{
unsigned char red;
unsigned char green;
unsigned char blue;
};

bitmap_image()
: file_name_(""),
width_ (0),
Expand Down Expand Up @@ -164,10 +171,11 @@ class bitmap_image
{
const unsigned int y_offset = y * row_increment_;
const unsigned int x_offset = x * bytes_per_pixel_;
const unsigned int offset = y_offset + x_offset;

blue = data_[y_offset + x_offset + 0];
green = data_[y_offset + x_offset + 1];
red = data_[y_offset + x_offset + 2];
blue = data_[offset + 0];
green = data_[offset + 1];
red = data_[offset + 2];
}

template <typename RGB>
Expand All @@ -176,17 +184,25 @@ class bitmap_image
get_pixel(x, y, colour.red, colour.green, colour.blue);
}

inline rgb_t get_pixel(const unsigned int x, const unsigned int y) const
{
rgb_t colour;
get_pixel(x, y, colour.red, colour.green, colour.blue);
return colour;
}

inline void set_pixel(const unsigned int x, const unsigned int y,
const unsigned char red,
const unsigned char green,
const unsigned char blue)
{
const unsigned int y_offset = y * row_increment_;
const unsigned int x_offset = x * bytes_per_pixel_;
const unsigned int offset = y_offset + x_offset;

data_[y_offset + x_offset + 0] = blue;
data_[y_offset + x_offset + 1] = green;
data_[y_offset + x_offset + 2] = red;
data_[offset + 0] = blue;
data_[offset + 1] = green;
data_[offset + 2] = red;
}

template <typename RGB>
Expand Down Expand Up @@ -418,7 +434,8 @@ class bitmap_image

if (!stream)
{
std::cerr << "bitmap_image::save_image(): Error - Could not open file " << file_name << " for writing!" << std::endl;
std::cerr << "bitmap_image::save_image(): Error - Could not open file "
<< file_name << " for writing!" << std::endl;
return;
}

Expand Down Expand Up @@ -1495,7 +1512,8 @@ class bitmap_image

if (!stream)
{
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - file " << file_name_ << " not found!" << std::endl;
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - "
<< "file " << file_name_ << " not found!" << std::endl;
return;
}

Expand All @@ -1513,36 +1531,41 @@ class bitmap_image

if (bfh.type != 19778)
{
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - "
<< "Invalid type value " << bfh.type << " expected 19778." << std::endl;

bfh.clear();
bih.clear();

stream.close();

std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - Invalid type value " << bfh.type << " expected 19778." << std::endl;
return;
}

if (bih.bit_count != 24)
{
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - "
<< "Invalid bit depth " << bih.bit_count << " expected 24." << std::endl;

bfh.clear();
bih.clear();

stream.close();

std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - Invalid bit depth " << bih.bit_count << " expected 24." << std::endl;

return;
}

if (bih.size != bih.struct_size())
{
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - "
<< "Invalid BIH size " << bih.size
<< " expected " << bih.struct_size() << std::endl;

bfh.clear();
bih.clear();

stream.close();

std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - Invalid BIH size " << bih.size << " expected " << bih.struct_size() << std::endl;

return;
}

Expand All @@ -1552,7 +1575,7 @@ class bitmap_image
bytes_per_pixel_ = bih.bit_count >> 3;

unsigned int padding = (4 - ((3 * width_) % 4)) % 4;
char padding_data[4] = {0,0,0,0};
char padding_data[4] = { 0x00, 0x00, 0x00, 0x00 };

std::size_t bitmap_file_size = file_size(file_name_);

Expand All @@ -1563,15 +1586,16 @@ class bitmap_image

if (bitmap_file_size != bitmap_logical_size)
{
std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - "
<< "Mismatch between logical and physical sizes of bitmap. "
<< "Logical: " << bitmap_logical_size << " "
<< "Physical: " << bitmap_file_size << std::endl;

bfh.clear();
bih.clear();

stream.close();

std::cerr << "bitmap_image::load_bitmap() ERROR: bitmap_image - Mismatch between logical and physical sizes of bitmap. " <<
"Logical: " << bitmap_logical_size << " " <<
"Physical: " << bitmap_file_size << std::endl;

return;
}

Expand Down Expand Up @@ -1606,12 +1630,7 @@ class bitmap_image
std::vector<unsigned char> data_;
};

struct rgb_t
{
unsigned char red;
unsigned char green;
unsigned char blue;
};
typedef bitmap_image::rgb_t rgb_t;

inline bool operator==(const rgb_t& c0, const rgb_t& c1)
{
Expand Down
Loading

0 comments on commit 624fb94

Please sign in to comment.