/* * Ray++ - Object-oriented ray tracing library * Copyright (C) 1998-2001 Martin Reinecke and others. * See the AUTHORS file for more information. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * See the README file for more information. */ #include "outputs/bmp_output.h" namespace RAYPP { BMP_OUTPUT::BMP_OUTPUT () : xres (320), yres (240), igamma (float4(1.0)), filename ("") {} BMP_OUTPUT::BMP_OUTPUT (uint4 xpix, uint4 ypix, const string &name, float4 gamma) : xres (xpix), yres (ypix), igamma (float4(1.0)/gamma), filename (name) {} //virtual void BMP_OUTPUT::Init () { if (initialized) return; if (filename == "") error ("BMP_OUTPUT: no filename specified"); initialized = true; } namespace { ostream &Put_uint4 (ostream &out, uint4 n) { out << byte( (n % 65536) % 256) << byte( (n % 65536) / 256) << byte( (n / 65536) % 256) << byte( (n / 65536) / 256); return out; } ostream &Put_uint2 (ostream &out, uint2 n) { out << byte (n % 256) << byte (n / 256); return out; } } //virtual void BMP_OUTPUT::Render () const { ci(); byte pixel[3]; float8 du = 1.0/xres, dv = 1.0/yres; COLOUR Col; ofstream outfile (filename.c_str(), ios::out | ios::binary); if (!outfile) error ("Could not open BMP file"); outfile << 'B' << 'M'; Put_uint4 (outfile, xres * yres * 3 + 54); // File size Put_uint4 (outfile, 0); // Reserve Put_uint4 (outfile, 54); // Offset to data Put_uint4 (outfile, 0x28); Put_uint4 (outfile, xres); Put_uint4 (outfile, yres); Put_uint2 (outfile, 1); // Number of planes Put_uint2 (outfile, 24); // BPP Put_uint4 (outfile, 0); // No compression Put_uint4 (outfile, xres * yres * 3); // Size of pixel data Put_uint4 (outfile, 0); // hp/m Put_uint4 (outfile, 0); // vp/m Put_uint4 (outfile, 0); // Number of colors in palette Put_uint4 (outfile, 0); // Important colors Message_Stream << "BMP-Output rendering to " << filename << endl; Message_Stream << "Resolution " << xres << "x" << yres << endl; for (int4 y=yres-1;y>=0;--y) { Message_Stream << "Rendering line " << y << " \r" << flush; for (uint4 x=0;xGet_Pixel (x*du, y*dv, du, dv); Col.Clip(); pixel[0] = byte (pow(Col.b, igamma)*255); pixel[1] = byte (pow(Col.g, igamma)*255); pixel[2] = byte (pow(Col.r, igamma)*255); outfile << pixel; } } Message_Stream << endl; } void BMP_OUTPUT::Set_Resolution (uint4 xpix, uint4 ypix) { cni(); xres = xpix; yres = ypix; } void BMP_OUTPUT::Set_Filename (const string &name) { cni(); filename = name; } } // namespace RAYPP