Benutzer:AlterVista/DRI Source

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Quellcode für Dynamic Range Increase-Programm. Benötigt CxImage von Davide Pizzolato. [1]

#include <stdlib.h>
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <io.h>
#include <ios>
#include <math.h>
#include <string>
#include <direct.h>
//#include <process.h>

#include <ximage.h>

#include <ximagif.h>
#include <ximatif.h>
#include <xfile.h>

using namespace std;

const double version=1.0;

struct list{ 
	 list*next;
	 _finddata_t c_file;
	 string ext;
	 float G;
 };


string zahlen[100]={ "0","1","2","3","4","5","6","7","8","9",
					"10","11","12","13","14","15","16","17","18","19",
					"20","21","22","23","24","25","26","27","28","29",
					"30","31","32","33","34","35","36","37","38","39",
					"40","41","42","43","44","45","46","47","48","49",
					"50","51","52","53","54","55","56","57","58","59",
					"60","61","62","63","64","65","66","67","68","69",
					"70","71","72","73","74","75","76","77","78","79",
					"80","81","82","83","84","85","86","87","88","89",
					"90","91","92","93","94","95","96","97","98","99",};
string Farbstrings[8]={"000000","FF0000","00FF00","0000FF","FFFF00","FF00FF","00FFFF","FFFFFF"};
char*cwd;
list*jpghier,*jpganfang;
CxImage*Bilder;
int jpgn=0;

float mean(CxImage*img)
{
	int x,y;
	float sum=0;
	for(x=0;x<((int) (img->GetWidth()));x++)
		for(y=0;y<((int) (img->GetHeight()));y++)
		{
			sum+=img->GetPixelGray(x,y);
		}
	sum/=(img->GetWidth()*img->GetHeight());
	return(sum);
}

string combinePath (string path1, string path2)
{
    if (path1.find_last_of("\\") != path1.length()-1 && path2 != "")
        path1 += "\\";
    path1 += path2;

    return path1;
}



void find_images()
{
	struct _finddata_t c_file;
	int jpgc=0;
	long hFile;
	string filter;
	filter = "*.jpg";
	if( (hFile = _findfirst( combinePath(cwd,filter).c_str(), &c_file )) != -1L )
	{
		jpgn=1;
		jpghier=new list;
		jpganfang=jpghier;
		jpghier->c_file=c_file;
		jpghier->ext="JPG";
		while( _findnext( hFile, &c_file ) == 0 )
		{ 
			jpgn++;
			jpghier->next=new list;
			jpghier=jpghier->next;
			jpghier->c_file=c_file;
			jpghier->ext="JPG";
		}
	}
	cout << "Found " << jpgn << " images\n";
}

void sort_and_load_images()
{
	int i,j,k,w,h;
	CxImage*aktuell;
	list*jpghier2;
	jpghier=jpganfang;
	float minG=256.0,G;
	aktuell=new CxImage;
	for(i=0;i<jpgn;i++)
	{
		aktuell->Load(jpghier->c_file.name);
		jpghier->G=mean(aktuell);
		minG=min(minG,jpghier->G);
		aktuell->Destroy();
		if (i<jpgn-1) jpghier=jpghier->next;
	}
	G=minG;
	Bilder = new CxImage [jpgn];
	for (j=0;j<jpgn;j++)
	{
		jpghier=jpganfang;
		for(i=0;i<jpgn-j;i++)
		{
			if (jpghier->G==minG)
			{
				
				if (Bilder[j].Load(jpghier->c_file.name)) cout << "Loaded " << jpghier->c_file.name << "\n";
				if (j==0) {w=Bilder[0].GetWidth(); h=Bilder[0].GetHeight();}
				else if ((w!=Bilder[0].GetWidth())||(h!=Bilder[0].GetHeight())) {cout << "Images do not have equal size.\n"; exit(0);}
				i=jpgn;
			}
			else jpghier=jpghier->next;
		}
		if (jpghier==jpganfang) jpganfang=jpghier->next;
		else 
		{
			jpghier2=jpganfang;
			while (jpghier2->next!=jpghier) jpghier2=jpghier2->next;
			jpghier2->next=jpghier2->next->next;
		}
		minG=256.0;
		jpghier2=jpganfang;
		for(k=0;k<jpgn-j-1;k++)
		{
			minG=min(minG,jpghier2->G);
		}
	}
}

void method0()//Nimm alle Pixel heller als der Durchschnitt des dunklen Bildes vom dunklen Bild und alle Pixel dunkler als der Durchschnitt des hellen Bildes vom hellen Bild. Interpoliere dazwischen: Falls der Wert des Pixels im hellen Bild nahe am Mittel des hellen Bildes liegt, wird der Wert des hellen Bildes wichtig. Gleiches gilt für das dunkle Bild. D.h. 50:50, wenn der Grauwert des Pixels in beiden Bildern genau dem Bilddurchschnitt entspricht.
{
	int i,x,y;
	RGBQUAD Csmall, Clarge;
	float smallG,largeG,f1,f2;
	CxImage*target;
	cout << "Applying method 0.\n";
	target = new CxImage;
	target->Create(Bilder[0].GetWidth(),Bilder[0].GetHeight(),24);
	for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	for(i=1;i<jpgn;i++)
	{
		smallG=mean(target);
		largeG=mean(&Bilder[i]);
 		for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
			for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
			{
				if (target->GetPixelGray(x,y)<=smallG) target->SetPixelColor(x,y,Bilder[i].GetPixelColor(x,y));
				else if (Bilder[i].GetPixelGray(x,y)>largeG) {}
				else 
				{
					f1=1-(target->GetPixelGray(x,y)-smallG)/255;
					f2=1-(largeG-Bilder[i].GetPixelGray(x,y))/255;
					f1/=(f1+f2);
					f2/=(1-(target->GetPixelGray(x,y)-smallG)/255+f2);
					Csmall=target->GetPixelColor(x,y);
					Clarge=Bilder[i].GetPixelColor(x,y);
					Clarge.rgbRed=BYTE (f1*Csmall.rgbRed+f2*Clarge.rgbRed);
					Clarge.rgbGreen=BYTE (f1*Csmall.rgbGreen+f2*Clarge.rgbGreen);		
					Clarge.rgbBlue=BYTE (f1*Csmall.rgbBlue+f2*Clarge.rgbBlue);
					target->SetPixelColor(x,y,Clarge);
				}

			}
	}
	string filename=combinePath(cwd,"DRI_method0.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
}

void method1()//Der Wert des Pixels ist exakt der Durchschnitt der entsprechenden Pixel aller Eingabebilder.
{
	long int sumr,sumg,sumb;
	int x,y,i;
	RGBQUAD col;
	CxImage*target;
	cout << "Applying method 1.\n";
	target = new CxImage;
	target->Create(Bilder[0].GetWidth(),Bilder[0].GetHeight(),24);
	for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	for(x=0;x<((int) (Bilder[jpgn-1].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[jpgn-1].GetHeight()));y++)
		{
			sumr=sumg=sumb=0;
			for (i=0;i<jpgn;i++) 
			{
				col=Bilder[i].GetPixelColor(x,y);
				sumr+=col.rgbRed;
				sumg+=col.rgbGreen;
				sumb+=col.rgbBlue;
			}
			col.rgbRed=BYTE (sumr/(1.0*jpgn));
			col.rgbGreen=BYTE (sumg/(1.0*jpgn));
			col.rgbBlue=BYTE (sumb/(1.0*jpgn));
			target->SetPixelColor(x,y,col);
		}
	string filename=combinePath(cwd,"DRI_method1.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
}

void method2()//Addiere alle Pixelwerte, mache im Summenbild eine Integral-Tonwertkorrektur, Stauche auf Bereich 0..255
{
	long int** sumr,**sumg,**sumb;
	long int**sumgray,*hist;
	long int width=Bilder[0].GetWidth();
	long int height=Bilder[0].GetHeight();
	long int size=width*height;
	int x,y,i;
	RGBQUAD col;
	CxImage*target;
	cout << "Applying method 2.\n";
	target = new CxImage;
	target->Create(width,height,24);
	for(x=0;x<width;x++)
		for(y=0;y<height;y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	sumr=new long int* [width]; for (i=0;i<width;i++) sumr[i]=new long int [height];
	sumg=new long int* [width]; for (i=0;i<width;i++) sumg[i]=new long int [height];
	sumb=new long int* [width]; for (i=0;i<width;i++) sumb[i]=new long int [height];
	sumgray=new long int* [width]; for (x=0;x<width;x++) sumgray[x]=new long int [height];
	for(x=0;x<width;x++)
		for(y=0;y<height;y++)
		{
			sumr[x][y]=sumg[x][y]=sumb[x][y]=sumgray[x][y]=0;
			for (i=0;i<jpgn;i++) 
			{
				col=Bilder[i].GetPixelColor(x,y);
				sumr[x][y]+=col.rgbRed;
				sumg[x][y]+=col.rgbGreen;
				sumb[x][y]+=col.rgbBlue;
				sumgray[x][y]+=Bilder[i].GetPixelGray(x,y); 
			}
		}
	hist=new long int [256*jpgn]; for(x=0;x<256*jpgn;x++) hist[x]=0;
	for(x=0;x<width;x++) for(y=0;y<height;y++) hist[sumgray[x][y]]++;
	double*integral;
	integral=new double [256*jpgn];
	for (i=0;i<jpgn*256;i++) integral[i]=0.0;
	integral[0]=hist[0]/(1.0*size);
	for(x=1;x<256*jpgn;x++) integral[x]=integral[x-1]+hist[x]/(1.0*size);
	for(x=1;x<256*jpgn;x++) integral[x]=(integral[x]+integral[x-1])/2.0;
	integral[0]/=2.0;
	double norm=(1.0/integral[256*jpgn-1]);
	for(x=0;x<256*jpgn;x++) integral[x]*=norm;

	for(x=0;x<width;x++) 
		for(y=0;y<height;y++) 
		{
			col.rgbRed  =(BYTE) (255*integral[sumr[x][y]]+0.49);
			col.rgbGreen=(BYTE) (255*integral[sumg[x][y]]+0.49);
			col.rgbBlue =(BYTE) (255*integral[sumb[x][y]]+0.49);
			target->SetPixelColor(x,y,col);
		}
	string filename=combinePath(cwd,"DRI_method2.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
	for (i=0;i<width;i++) delete [] sumr[i];
	for (i=0;i<width;i++) delete [] sumg[i];
	for (i=0;i<width;i++) delete [] sumb[i];
	for (i=0;i<width;i++) delete [] sumgray[i];
	delete [] sumr; delete [] sumg; delete [] sumb; delete []sumgray;
	delete [] hist;
	delete [] integral;
}

void method3()//Es wird aus allen Eingabepixeln derjenige gewählt, dessen Grauwert am nächsten an 128 liegt.
{
	int x,y,i,ming,mingi,gray;
	CxImage*target;
	cout << "Applying method 3.\n";
	target = new CxImage;
	target->Create(Bilder[0].GetWidth(),Bilder[0].GetHeight(),24);
	for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	for(x=0;x<((int) (Bilder[jpgn-1].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[jpgn-1].GetHeight()));y++)
		{
			ming=128; mingi=0;
			for (i=0;i<jpgn;i++) 
			{
				gray=Bilder[i].GetPixelGray(x,y);
				if (abs(128-gray)<ming) {ming=abs(128-gray); mingi=i;}

			}
			target->SetPixelColor(x,y,Bilder[mingi].GetPixelColor(x,y));
		}
	string filename=combinePath(cwd,"DRI_method3.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
}

void method4()//Immer mit Bildpaaren, bei den dunklen beginnend sortiert zu den hellen hin: Je heller ein Pixel (Mittelwert aus Grauwerten des dunklen und des hellen Bildes) ist, umso mehr geht der Wert des Pixels im dunklen Bild in den Pixel des Ergebnisbildes ein. Das Ergebnisbild ist das neue dunkle Bild für die nächste Paarung.
{
	int i,x,y;
	RGBQUAD Csmall, Clarge;
	double smallG,largeG,f1,f2;
	CxImage*target;
	cout << "Applying method 4.\n";
	target = new CxImage;
	target->Create(Bilder[0].GetWidth(),Bilder[0].GetHeight(),24);
	for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	for(i=1;i<jpgn;i++)
 		for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
			for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
			{
				smallG=target->GetPixelGray(x,y);
				largeG=Bilder[i].GetPixelGray(x,y);
				f1=(smallG+largeG)/2.0/255.0;
				f2=1-f1;
				Csmall=target->GetPixelColor(x,y);
				Clarge=Bilder[i].GetPixelColor(x,y);
				Csmall.rgbRed=BYTE(f1*Csmall.rgbRed+f2*Clarge.rgbRed+0.49);
				Csmall.rgbGreen=BYTE(f1*Csmall.rgbGreen+f2*Clarge.rgbGreen+0.49);
				Csmall.rgbBlue=BYTE(f1*Csmall.rgbBlue+f2*Clarge.rgbBlue+0.49);
				target->SetPixelColor(x,y,Csmall);
			}
	string filename=combinePath(cwd,"DRI_method4.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
}

void method5()//Berechne Durchschnitt der durchschnittlichen Grauwerte aller Bilder. Werte einen Pixel umso stärker, je näher der Graumittelwert seines Bildes an diesem Mittelwert liegt.
{
	int i,x,y;
	RGBQUAD Csmall;
	double *f,fsum,red,green,blue;
	int *G;
	double Gav;
	CxImage*target;
	cout << "Applying method 5.\n";
	target = new CxImage;
	G=new int[jpgn];
	f=new double[jpgn];
	target->Create(Bilder[0].GetWidth(),Bilder[0].GetHeight(),24);
	for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	for(x=0;x<((int) (Bilder[0].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[0].GetHeight()));y++)
		{
			Gav=0.0;
			for(i=0;i<jpgn;i++) {G[i]=Bilder[i].GetPixelGray(x,y); Gav+=G[i];}
			Gav/=jpgn;
			fsum=0.0;
			for(i=0;i<jpgn;i++) {f[i]=fabs(G[i]-Gav); fsum+=f[i];}
			if (fsum==0.0) target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
			else
			{
				for(i=0;i<jpgn;i++) f[i]/=fsum;
				red=green=blue=0.0;
				for(i=0;i<jpgn;i++) 
				{
					Csmall=Bilder[i].GetPixelColor(x,y);
					red+=f[i]*Csmall.rgbRed;
					green+=f[i]*Csmall.rgbGreen;
					blue+=f[i]*Csmall.rgbBlue;
				}
				Csmall.rgbRed=BYTE(red+0.49);
				Csmall.rgbGreen=BYTE(green+0.49);
				Csmall.rgbBlue=BYTE(blue+0.49);
				target->SetPixelColor(x,y,Csmall);
			}
		}
	string filename=combinePath(cwd,"DRI_method5.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
	delete [] G;
	delete [] f;
}

void method6()//Immer paarweise, bei den dunklen beginnend: Verschiebe dunkles Bild um DIfferenz der mittleren Grauwerte beider Bilder, wenn dann der Pixel des helleren Bildes dunkler ist, wird dieser genommen und so normiert, dass der Farbraum nicht überschritten, aber komplett ausgenutzt wird und umgekehrt.
{
	double** r,**g,**b;
	double Glarge,Gsmall,Gdif;
	long int width=Bilder[0].GetWidth();
	long int height=Bilder[0].GetHeight();
	int x,y,i;
	double factor,maxval,minval;
	RGBQUAD col;
	CxImage*target;
	cout << "Applying method 6.\n";
	target = new CxImage;
	target->Create(width,height,24);
	for(x=0;x<width;x++)
		for(y=0;y<height;y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	r=new double* [width]; for (i=0;i<width;i++) r[i]=new double [height];
	g=new double* [width]; for (i=0;i<width;i++) g[i]=new double [height];
	b=new double* [width]; for (i=0;i<width;i++) b[i]=new double [height];
	for (i=1;i<jpgn;i++) 
	{
		Gsmall=target->Mean();
		Glarge=Bilder[i].Mean();
		Gdif=Glarge-Gsmall;
		maxval=0;
		minval=255;
        for(x=0;x<width;x++)
			for(y=0;y<height;y++)
			{
				col=target->GetPixelColor(x,y);
				r[x][y]=col.rgbRed+Gdif; ((maxval)>(r[x][y]))?(maxval=maxval):(maxval=r[x][y]);
				g[x][y]=col.rgbGreen+Gdif; ((maxval)>(g[x][y]))?(maxval=maxval):(maxval=g[x][y]);
				b[x][y]=col.rgbBlue+Gdif; ((maxval)>(b[x][y]))?(maxval=maxval):(maxval=b[x][y]);
				col=Bilder[i].GetPixelColor(x,y);
				((minval)<(col.rgbRed))?(minval=minval):(minval=col.rgbRed);
				((minval)<(col.rgbGreen))?(minval=minval):(minval=col.rgbGreen);
				((minval)<(col.rgbBlue))?(minval=minval):(minval=col.rgbBlue);
			}
		factor=255.0/(maxval-minval);
		if (factor>1.0) factor=1.0;
        for(x=0;x<width;x++)
			for(y=0;y<height;y++)
			{
				if (Bilder[i].GetPixelGray(x,y)<target->GetPixelGray(x,y)+Gdif)
				{
					col=Bilder[i].GetPixelColor(x,y);
					col.rgbRed=BYTE((col.rgbRed-minval)*factor+0.49);
					col.rgbGreen=BYTE((col.rgbGreen-minval)*factor+0.49);
					col.rgbBlue=BYTE((col.rgbBlue-minval)*factor+0.49);
					target->SetPixelColor(x,y,col);
				}
				else
				{
					col.rgbRed=BYTE((r[x][y]-minval)*factor+0.49);
					col.rgbGreen=BYTE((g[x][y]-minval)*factor+0.49);
					col.rgbBlue=BYTE((b[x][y]-minval)*factor+0.49);
					target->SetPixelColor(x,y,col);
				}				
			}
	}
	string filename=combinePath(cwd,"DRI_method6.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
	for (i=0;i<width;i++) delete [] r[i];
	for (i=0;i<width;i++) delete [] g[i];
	for (i=0;i<width;i++) delete [] b[i];
	delete [] r; delete [] g; delete [] b;
}

void method7()//Wie 6, aber Stauchung im hellen Bereich quadratisch statt linear.
{
	double** r,**g,**b;
	double Glarge,Gsmall,Gdif;
	long int width=Bilder[0].GetWidth();
	long int height=Bilder[0].GetHeight();
	int x,y,i;
	double a,maxval,minval;
	RGBQUAD col;
	CxImage*target;
	cout << "Applying method 7.\n";
	target = new CxImage;
	target->Create(width,height,24);
	for(x=0;x<width;x++)
		for(y=0;y<height;y++)
			target->SetPixelColor(x,y,Bilder[0].GetPixelColor(x,y));
	r=new double* [width]; for (i=0;i<width;i++) r[i]=new double [height];
	g=new double* [width]; for (i=0;i<width;i++) g[i]=new double [height];
	b=new double* [width]; for (i=0;i<width;i++) b[i]=new double [height];
	for (i=1;i<jpgn;i++) 
	{
		Gsmall=target->Mean();
		Glarge=Bilder[i].Mean();
		Gdif=Glarge-Gsmall;
		maxval=0;
		minval=255;
        for(x=0;x<width;x++)
			for(y=0;y<height;y++)
			{
				col=target->GetPixelColor(x,y);
				r[x][y]=col.rgbRed+Gdif; ((maxval)>(r[x][y]))?(maxval=maxval):(maxval=r[x][y]);
				g[x][y]=col.rgbGreen+Gdif; ((maxval)>(g[x][y]))?(maxval=maxval):(maxval=g[x][y]);
				b[x][y]=col.rgbBlue+Gdif; ((maxval)>(b[x][y]))?(maxval=maxval):(maxval=b[x][y]);
				col=Bilder[i].GetPixelColor(x,y);
				((minval)<(col.rgbRed))?(minval=minval):(minval=col.rgbRed);
				((minval)<(col.rgbGreen))?(minval=minval):(minval=col.rgbGreen);
				((minval)<(col.rgbBlue))?(minval=minval):(minval=col.rgbBlue);
			}
		a=255.0/(maxval-minval)/(maxval-minval)-1.0/(maxval-minval);
        for(x=0;x<width;x++)
			for(y=0;y<height;y++)
			{
				if (Bilder[i].GetPixelGray(x,y)<target->GetPixelGray(x,y)+Gdif)
				{
					col=Bilder[i].GetPixelColor(x,y);
					col.rgbRed=BYTE(a*(col.rgbRed-minval)*(col.rgbRed-minval)+1.0*(col.rgbRed-minval)+0.49);
					col.rgbGreen=BYTE(a*(col.rgbGreen-minval)*(col.rgbGreen-minval)+1.0*(col.rgbGreen-minval)+0.49);
					col.rgbBlue=BYTE(a*(col.rgbBlue-minval)*(col.rgbBlue-minval)+1.0*(col.rgbBlue-minval)+0.49);
					target->SetPixelColor(x,y,col);
				}
				else
				{
					col.rgbRed=BYTE(a*(r[x][y]-minval)*(r[x][y]-minval)+1.0*(r[x][y]-minval)+0.49);
					col.rgbGreen=BYTE(a*(g[x][y]-minval)*(g[x][y]-minval)+1.0*(g[x][y]-minval)+0.49);
					col.rgbBlue=BYTE(a*(b[x][y]-minval)*(b[x][y]-minval)+1.0*(b[x][y]-minval)+0.49);
					target->SetPixelColor(x,y,col);
				}				
			}
	}
	string filename=combinePath(cwd,"DRI_method7.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
	for (i=0;i<width;i++) delete [] r[i];
	for (i=0;i<width;i++) delete [] g[i];
	for (i=0;i<width;i++) delete [] b[i];
	delete [] r; delete [] g; delete [] b;
}

void method8()//Wie 4, nur von hell nach dunkel
{
	int i,x,y;
	RGBQUAD Csmall, Clarge;
	double smallG,largeG,f1,f2;
	CxImage*target;
	cout << "Applying method 8.\n";
	target = new CxImage;
	target->Create(Bilder[jpgn-1].GetWidth(),Bilder[jpgn-1].GetHeight(),24);
	for(x=0;x<((int) (Bilder[jpgn-1].GetWidth()));x++)
		for(y=0;y<((int) (Bilder[jpgn-1].GetHeight()));y++)
			target->SetPixelColor(x,y,Bilder[jpgn-1].GetPixelColor(x,y));
	for(i=jpgn-2;i>0;i--)
 		for(x=0;x<((int) (Bilder[jpgn-1].GetWidth()));x++)
			for(y=0;y<((int) (Bilder[jpgn-1].GetHeight()));y++)
			{
				largeG=target->GetPixelGray(x,y);
				smallG=Bilder[i].GetPixelGray(x,y);
				f1=(smallG+largeG)/2.0/255.0;
				f2=1-f1;
				Clarge=target->GetPixelColor(x,y);
				Csmall=Bilder[i].GetPixelColor(x,y);
				Csmall.rgbRed=BYTE(f1*Csmall.rgbRed+f2*Clarge.rgbRed+0.49);
				Csmall.rgbGreen=BYTE(f1*Csmall.rgbGreen+f2*Clarge.rgbGreen+0.49);
				Csmall.rgbBlue=BYTE(f1*Csmall.rgbBlue+f2*Clarge.rgbBlue+0.49);
				target->SetPixelColor(x,y,Csmall);
			}
	string filename=combinePath(cwd,"DRI_method8.jpg");
	target->SetJpegQuality(98);
	target->Save(filename.c_str(),CXIMAGE_FORMAT_JPG);
	target->Destroy();
}


int main(int noa, char*argch[])
{
	string*args;
	char*du;
	cwd = new char [511];
	du=_getcwd(cwd,511);
	cout << du<< "\n";
	if (noa>1) 
	{
		args=new string[noa];
		for (int ii=0;ii<noa;ii++) args[ii]=argch[ii];
		find_images();
		sort_and_load_images();
		for (int i=0; i<noa;i++){
			if ((args[i]=="-0")||(args[i]=="/0")) {method0();}
			else if ((args[i]=="-1")||(args[i]=="/1")) {method1();}
			else if ((args[i]=="-2")||(args[i]=="/2")) {method2();}
			else if ((args[i]=="-3")||(args[i]=="/3")) {method3();}
			else if ((args[i]=="-4")||(args[i]=="/4")) {method4();}
			else if ((args[i]=="-5")||(args[i]=="/5")) {method5();}
			else if ((args[i]=="-6")||(args[i]=="/6")) {method6();}
			else if ((args[i]=="-7")||(args[i]=="/7")) {method7();}
			else if ((args[i]=="-8")||(args[i]=="/8")) {method8();}


			else if ((args[i]=="-all")||(args[i]=="/all")) 
			{method0(); method1(); method2(); method3(); method4(); method5(); method6(); method7(); method8();};
		}
	}
	else
	{
		cout << "DRI - Dynamic Range Increase.\n";
		cout << "A Freeware program by Wikipedia [[:de:Benutzer:AlterVista]].\n";
		cout << "Use freely but at own risk.\n\n";
		cout << "Usage:\n";
		cout << "DRI -m\n";
		cout << "m can be 0, 1, 2, 3, 4, 5, 6, 7 or 8\n";
		cout << "Multiple options are possible. For all type DRI -all\n";
	}
	return 0;
}