#include "logic.h"
#include "CAMorphologyExternal.h"
#include "cpp/HalconCpp.h"

using namespace Halcon;


CAMorphologyExternal::CAMorphologyExternal(const QString& sName, int32_t iID)
	: CAMorphology_h(sName, iID)
{
	m_Variants.get_VData(Var_Morphology_Height_ID)->setValue((double)255);
}

CAMorphologyExternal::~CAMorphologyExternal(void)
{
}

void CAMorphologyExternal::binary(CMImage* pmimage)
{

	double paramThres= m_Variants.get_VData(Var_Morphology_Thres_ID)->toDouble();
	double width = m_Variants.get_VData(Var_Morphology_Width_ID)->toDouble();
	double param2 = m_Variants.get_VData(Var_Morphology_Height_ID)->toDouble();
	double minGray = std::max<int>(0, (int)paramThres-width);
	double maxGay = std::min<int>(255, (int)paramThres+param2);

	Hobject region,Image,regionDiff;
	Herror err = gen_image1_extern(&Image, "byte", pmimage->m_width, pmimage->m_height, (uintptr_t)pmimage->m_pRawImage, 0);

	threshold(Image, &region, minGray, maxGay);
	difference(Image, region, &regionDiff);
	overpaint_region(Image, region, 255, "fill");
	overpaint_region(Image, regionDiff, 0, "fill");

	//write_image(Image, "jpg", 0, "__1.jpg");

	//HTuple Type;
	//HTuple imgWidth;
	//HTuple imgHeight;
	//Hlong pt;
	//get_image_pointer1(Image, &pt, &Type, &imgWidth, &imgHeight);
	//pmimage->m_pRawImage = pt;
}
