// // bmp.hpp // AwesomeAttract0r // // Created by Joshua Moerman on 10/24/11. // Copyright 2011 Vadovas. All rights reserved. // #ifndef AwesomeAttract0r_bmp_hpp #define AwesomeAttract0r_bmp_hpp #include #include #include #include namespace bmp { struct bitmap_file_header { uint32_t filesize; uint16_t creator1; uint16_t creator2; uint32_t bmp_offset; template bitmap_file_header(DIBT dib_header): filesize(dib_header.bytes() + dib_header.header_sz + 12 + 2), creator1(0), creator2(0), bmp_offset(dib_header.header_sz + 12 + 2){} void write(std::ostream& out) const { out << "BM"; out.write(reinterpret_cast(this), 12); } }; struct bitmapcoreheader { uint32_t header_sz; uint16_t width; uint16_t height; uint16_t nplanes; uint16_t bitspp; bitmapcoreheader(int width, int height): header_sz(sizeof(bitmapcoreheader)), width(width), height(height), nplanes(1), bitspp(24){} void write(std::ostream& out) const { out.write(reinterpret_cast(this), header_sz); } unsigned int bytes(){ return width*height*bitspp/8; } }; template struct bitmap { DIBT dib_header; bitmap_file_header header; T const * data; bitmap(int width, int height) : dib_header(width, height) , header(dib_header) , data(0) {} void write(std::string const & filename){ std::ofstream file(filename.c_str()); write(file); } void write(std::ostream& out){ header.write(out); dib_header.write(out); //std::copy_n((char const *)data, dib_header.bytes(), std::ostream_iterator(out)); std::copy((char const *)data, (char const *)data + dib_header.bytes(), std::ostream_iterator(out)); } }; } #endif