The Easiest Way to Save and Share Code Snippets on the web

Untitled

cpp

posted: Jul, 26th 2012 | jump to bottom

//---------------------------------------------------------------------------
 
#include <vcl.h>
#include <fstream>
#include <iterator>
using namespace std;
#pragma hdrstop
#include "Unit1.h"
#include "math.h"
 
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
 
#define TBitmap Graphics::TBitmap
 
TBitmap *Src1 = new TBitmap();
TBitmap *Src2 = new TBitmap();
 
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	Src1->LoadFromFile("1.bmp");
	Src2->LoadFromFile("2.bmp");
 
	int *fbSrc1 = (int*)malloc(Src1->Width*Src1->Height*4);
	int *fbSrc2 = (int*)malloc(Src1->Width*Src1->Height*4);
 
	int byteCount = Src1->Width*Src1->Height;
 
	int r=0, g=0, b=0, r1=0, g1=0, b1=0, r2=0, g2=0, b2=0;
 
	for(int i=0; i<Src1->Height; i++)
	{
		for(int n=0; n<Src1->Width; n++)
		{
			fbSrc1[i*Src1->Width+n] = ((uint8_t*)Src1->ScanLine[i])[n*3+0]<<16;
			fbSrc1[i*Src1->Width+n]|= ((uint8_t*)Src1->ScanLine[i])[n*3+1]<<8;
			fbSrc1[i*Src1->Width+n]|= ((uint8_t*)Src1->ScanLine[i])[n*3+2];
		}
	}
 
	for(int i=0; i<Src1->Height; i++)
	{
		for(int n=0; n<Src1->Width; n++)
		{
			fbSrc2[i*Src1->Width+n] = ((uint8_t*)Src2->ScanLine[i])[n*3+0]<<16;
			fbSrc2[i*Src1->Width+n]|= ((uint8_t*)Src2->ScanLine[i])[n*3+1]<<8;
			fbSrc2[i*Src1->Width+n]|= ((uint8_t*)Src2->ScanLine[i])[n*3+2];
		}
	}
 
	int rtrhs = StrToInt(Edit1->Text),
		gtrhs = StrToInt(Edit2->Text),
		btrhs = StrToInt(Edit3->Text);
 
	for (int i = 0; i < byteCount; i++)
	{
		r = (fbSrc1[i] >> 16) & 0xFF;
		g = (fbSrc1[i] >> 8) & 0xFF;
		b = (fbSrc1[i] >> 0) & 0xFF;
 
		r1 = (fbSrc2[i] >> 16) & 0xFF;
		g1 = (fbSrc2[i] >> 8) & 0xFF;
		b1 = (fbSrc2[i] >> 0) & 0xFF;
 
		r2 = abs(r - r1);
		g2 = abs(g - g1);
		b2 = abs(b - b1);
 
		if ((r2 >= rtrhs) || (g2 >= gtrhs) || (b2 >= btrhs))
			Image1->Canvas->Pixels[i%Src1->Width][i/Src1->Width] = fbSrc2[i];
	}
}
//---------------------------------------------------------------------------
 
221 views