//: image.h : Grafik-Bibliothek ohne Grafiktreiber - R.Richter 2011-01-15 ///////////////////////////////////////////////////////////////////////// #ifndef IMAGE_H #define IMAGE_H #include #include #include "color.h" class Image { public: Image(unsigned int width, unsigned int height, Color background = 0) : width_(width), height_(height), pixels_(width * height, background) { } unsigned int width() const { return width_; } unsigned int height() const { return height_; } Color& pixel(unsigned int x, unsigned int y) { return x>=width() || y>=height() ? outside() : pixels_[y*width() + x]; } Color const& pixel(unsigned int x, unsigned int y) const { return x>=width() || y>=height() ? outside() : pixels_[y*width() + x]; } private: unsigned int width_, height_; std::vector pixels_; // width_ x height_ pixels static Color& outside() { static Color c; return c = Color::NONE; } }; // ===[ Speichern als BMP 24bit unkomprimiert, ohne Transparenz ]=========== #include #include #include #include // Achtung, nicht portabel: Intel Byte-order vorausgesetzt inline // a bit too long for ... void saveBMP(std::string filename, Image const& image) { unsigned long const linesize = 3*image.width(); // 24 bit per pixel unsigned long const fillbytes = (4-linesize%4)%4; // at end of line to quad boundary unsigned long Image constsize = (linesize+fillbytes)*image.height(); // byte alignment, no padding // #if defined( _MSC_VER ) || defined( __MINGW32__ ) #pragma pack(1) // #endif struct BMPheader { // file info char ID[2]; unsigned long filesize, reserved, offset; // BMP info unsigned long infosize, width, height; unsigned short planes, bitsperpixel; unsigned long compression, imagesize, xPixelPerMeter, yPixelPerMeter, colorsUsed, colorsImportant; } header = { // file info { 'B', 'M' }, unsigned(sizeof(header)) + imagesize, // 24 bits per pixel 0, sizeof(header), // 0x36 // BMP info 40, image.width(), image.height(), 1, 24, 0, imagesize, 1000, 1000, 0, 0 }; // #if defined( _MSC_VER ) || defined( __MINGW32__ ) #pragma pack() // #endif std::ofstream file(filename.c_str(), std::ios::out|std::ios::binary); file.write( (char*)&header, sizeof(header)); // Intel byte order for (unsigned y = 0; y < image.height(); ++y) { for (unsigned x = 0; x < image.width(); ++x) { Color c= image.pixel(x,y); unsigned char b = c.blue(), g = c.green(), r = c.red(); file.write( (char*) &b, 1); file.write( (char*) &g, 1); file.write( (char*) &r, 1); } if (fillbytes) { int zero = 0; file.write( (char*) &zero, fillbytes); } } } #endif // IMAGE_H //~