Commit 8358ac25 authored by Hans Buchmann's avatar Hans Buchmann

first

parent 5d3f03f2
#-----------------------
#Makefile
#(c) H.Buchmann FHNW 2020
#-----------------------
VPATH=../src
CC=g++
CXX=${CC}
CXXFLAGS=-std=c++2a -O2
mandelbrot: mandelbrot.o pbm.o bmp.o
pbm.o: pbm.cc pbm.h
bmp.o: bmp.cc bmp.h
mandelbrot.o: mandelbrot.cc pbm.h bmp.h
//--------------------------
//pbm.cc color image format
//(c) H.Buchmann 2020
//-------------------------
#include "bmp.h"
BMP::BMP(unsigned width,unsigned height,const RGB& c)
:width(width)
,height(height)
,row(new RGB*[height])
{
for(unsigned y=0;y<height;++y)
{
row[y]=new RGB[width];
for(unsigned x=0;x<width;++x) row[y][x]=c;
}
}
BMP::~BMP()
{
for(unsigned y=0;y<height;++y)
{
delete [] row[y];
}
delete [] row;
}
BMP::RGB& BMP::operator()(unsigned x,unsigned y)
{
if ((x>=width) || (y>=height)) return nil;
return row[y][x];
}
std::ostream& BMP::stream(std::ostream& out) const
{
// unsigned size=0;
out<<"BM";
put(out,14+40+3*(width*height)),
put(out,0u);
put(out,54u);
put(out,40u);
put(out,width);
put(out,height);
put<unsigned short>(out,1); //biPlanes
put<unsigned short>(out,24); //biBitCount
put(out,0u); //biCompression
put(out,0); //biSizeImage
put(out,0); //biXPelsPerMeter
put(out,0); //biYPelsPerMeter
put(out,0); //biClrUsed
put(out,0); //biClrImportant
for(unsigned y=0;y<height;++y)
{
auto r=row[y];
for(unsigned x=0;x<width;++x)
{
const auto& p=r[x];
put<unsigned char>(out,p[0]);
put<unsigned char>(out,p[1]);
put<unsigned char>(out,p[2]);
}
}
out.flush();
return out;
}
#if 0
int main(int argc,char** args)
{
BMP img(1023,1025,BMP::WHITE);
unsigned g=0;
for(unsigned x=0;x<img.width;++x)
{
for(unsigned y=0;y<img.height;++y)
{
img(x,y)={g,g,g};
g++;
if (g==256) g=0;
}
}
std::cout<<img;
return 0;
}
#endif
#pragma once
//--------------------------
//bpm.cc color image format
//(c) H.Buchmann 2020
//-------------------------
#include <iostream>
#include <array>
class BMP
{
public:
using RGB=std::array<unsigned,3>;
static const constexpr RGB WHITE ={0xff,0xff,0xff};
static const constexpr RGB BLACK ={0x00,0x00,0x00};
BMP(unsigned width,unsigned height,const RGB& c=WHITE); //8 bit
~BMP();
friend std::ostream& operator<<(std::ostream& out,const BMP& pgm){return pgm.stream(out);}
std::ostream& stream(std::ostream& out) const;
RGB& operator()(unsigned x,unsigned y);
const unsigned width;
const unsigned height;
private:
RGB nil=WHITE;
RGB** row;
template<typename T>
static void put(std::ostream& out,T val)
{
out.write((const char*)&val,sizeof(T));
}
};
//-----------------------
//mandelbrot.cc
//(c) H.Buchmann FHNW 2020
//https://de.wikipedia.org/wiki/Mandelbrot-Menge
//-----------------------
#include "pbm.h"
#include "bmp.h"
#include <complex>
using Complex=std::complex<double>;
using Image=BMP;
static const unsigned R=1024;
static const unsigned HE=2*R;
static const unsigned WI=3*R;
static const Complex LL(-2,-1); //lower left
static const Complex UR( 1, 1); //upper right
static const unsigned N=256;
unsigned iter(const Complex& c)
{
Complex z=0;
for(unsigned n=0;n<N;++n)
{
if (std::abs(z)>2) return n;
z=z*z+c;
}
return N;
}
Image::RGB color(unsigned n)
{
if (n<8)
{
return {0x40,0x40,n*256/8};
}
if (n<16)
{
return {0x40,(n-8)*256/8,0x40};
}
if (n<24)
{
return {(n-16)*256/8,(n-16)*256/8,0x40};
}
return {0xc0,0xc0,0xc0};
}
int main(int argc,char** args)
{
auto dx=(UR.real()-LL.real())/WI;
auto dy=Complex(0,(UR.imag()-LL.imag())/HE);
Image img(WI,HE);
Complex cy=LL;
for(unsigned y=0;y<HE;++y)
{
Complex cx=cy;
for(unsigned x=0;x<WI;++x)
{
img(x,y)=color(iter(cx));
cx+=dx;
}
cy+=dy;
}
std::cout<<img;
return 0;
}
//--------------------------
//pbm.cc color image format
//(c) H.Buchmann 2020
//-------------------------
#include "pbm.h"
PBM::PBM(unsigned width,unsigned height,const RGB& c)
:width(width)
,height(height)
,row(new RGB*[height])
{
for(unsigned y=0;y<height;++y)
{
row[y]=new RGB[width];
for(unsigned x=0;x<width;++x) row[y][x]=c;
}
}
PBM::~PBM()
{
for(unsigned y=0;y<height;++y)
{
delete [] row[y];
}
delete [] row;
}
PBM::RGB& PBM::operator()(unsigned x,unsigned y)
{
if ((x>=width) || (y>=height)) return nil;
return row[y][x];
}
std::ostream& PBM::stream(std::ostream& out) const
{
out<<"P6\n"
<<width<<" "<<height<<"\n"
"255\n";
for(unsigned y=0;y<height;++y)
{
auto r=row[y];
for(unsigned x=0;x<width;++x)
{
const auto& rgb=r[x];
for(auto& c:rgb)
{
out.write((const char*)&c,1);
}
}
}
return out;
}
#if 0
int main(int argc,char** args)
{
PBM img(1023,2047);
unsigned g=0;
for(unsigned x=0;x<img.width;++x)
{
for(unsigned y=0;y<img.height;++y)
{
img(x,y)={g,g,g};
g++;
if (g==256) g=0;
}
}
std::cout<<img;
return 0;
}
#endif
#pragma once
//--------------------------
//pbm.cc color image format
//(c) H.Buchmann 2020
//-------------------------
#include <iostream>
#include <array>
class PBM
{
public:
using RGB=std::array<unsigned,3>;
static const constexpr RGB WHITE ={0xff,0xff,0xff};
static const constexpr RGB BLACK ={0x00,0x00,0x00};
PBM(unsigned width,unsigned height,const RGB& c=WHITE); //8 bit
~PBM();
friend std::ostream& operator<<(std::ostream& out,const PBM& pgm){return pgm.stream(out);}
std::ostream& stream(std::ostream& out) const;
RGB& operator()(unsigned x,unsigned y);
const unsigned width;
const unsigned height;
private:
RGB nil=WHITE;
RGB** row;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment