Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement write_image()/read_image() functions to write/read bmp images to/from STL streams for flexibility #8

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
81 changes: 49 additions & 32 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 @@ -422,6 +438,13 @@ class bitmap_image
return;
}

write_image(stream);

stream.close();
}

void write_image(std::ostream& stream) const
{
bitmap_information_header bih;

bih.width = width_;
Expand Down Expand Up @@ -457,8 +480,6 @@ class bitmap_image
stream.write(reinterpret_cast<const char*>(data_ptr), sizeof(unsigned char) * bytes_per_pixel_ * width_);
stream.write(padding_data,padding);
}

stream.close();
}

inline void set_all_ith_bits_low(const unsigned int bitr_index)
Expand Down Expand Up @@ -1364,18 +1385,18 @@ class bitmap_image
}

template <typename T>
inline void read_from_stream(std::ifstream& stream,T& t)
inline void read_from_stream(std::istream& stream,T& t)
{
stream.read(reinterpret_cast<char*>(&t),sizeof(T));
}

template <typename T>
inline void write_to_stream(std::ofstream& stream,const T& t) const
inline void write_to_stream(std::ostream& stream,const T& t) const
{
stream.write(reinterpret_cast<const char*>(&t),sizeof(T));
}

inline void read_bfh(std::ifstream& stream, bitmap_file_header& bfh)
inline void read_bfh(std::istream& stream, bitmap_file_header& bfh)
{
read_from_stream(stream,bfh.type );
read_from_stream(stream,bfh.size );
Expand All @@ -1393,7 +1414,7 @@ class bitmap_image
}
}

inline void write_bfh(std::ofstream& stream, const bitmap_file_header& bfh) const
inline void write_bfh(std::ostream& stream, const bitmap_file_header& bfh) const
{
if (big_endian())
{
Expand All @@ -1413,7 +1434,7 @@ class bitmap_image
}
}

inline void read_bih(std::ifstream& stream,bitmap_information_header& bih)
inline void read_bih(std::istream& stream,bitmap_information_header& bih)
{
read_from_stream(stream,bih.size );
read_from_stream(stream,bih.width );
Expand Down Expand Up @@ -1443,7 +1464,7 @@ class bitmap_image
}
}

inline void write_bih(std::ofstream& stream, const bitmap_information_header& bih) const
inline void write_bih(std::ostream& stream, const bitmap_information_header& bih) const
{
if (big_endian())
{
Expand Down Expand Up @@ -1499,6 +1520,13 @@ class bitmap_image
return;
}

read_bitmap(stream);

stream.close();
}

bool read_bitmap(std::istream& stream)
{
width_ = 0;
height_ = 0;

Expand All @@ -1516,34 +1544,28 @@ class bitmap_image
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;
return false;
}

if (bih.bit_count != 24)
{
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;
return false;
}

if (bih.size != bih.struct_size())
{
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;
return false;
}

width_ = bih.width;
Expand All @@ -1566,13 +1588,11 @@ class bitmap_image
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;
return false;
}

create_bitmap();
Expand All @@ -1584,6 +1604,8 @@ class bitmap_image
stream.read(reinterpret_cast<char*>(data_ptr), sizeof(char) * bytes_per_pixel_ * width_);
stream.read(padding_data,padding);
}

return true;
}

template <typename T>
Expand All @@ -1606,12 +1628,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