Example #1
0
GPU_PERF_TEST(Min, cv::gpu::DeviceInfo, cv::Size, perf::MatType)
{
    cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
    cv::Size size = GET_PARAM(1);
    int type = GET_PARAM(2);

    cv::gpu::setDevice(devInfo.deviceID());

    cv::Mat src1_host(size, type);
    cv::Mat src2_host(size, type);

    declare.in(src1_host, src2_host, WARMUP_RNG);

    cv::gpu::GpuMat src1(src1_host);
    cv::gpu::GpuMat src2(src2_host);
    cv::gpu::GpuMat dst(size, type);

    TEST_CYCLE()
    {
        cv::gpu::min(src1, src2, dst);
    }
}
Example #2
0
 void AddWeightedTest::testAllocate0()
 {
     m_operator->setParameter(AddWeighted::DATA_FLOW, runtime::Enum(AddWeighted::ALLOCATE));
     m_operator->initialize();
     m_operator->activate();
     
     runtime::DataContainer src1(new cvsupport::Image("lenna.jpg", cvsupport::Image::DEPTH_16));
     runtime::Float64 alpha(-1.0);
     runtime::DataContainer src2(new cvsupport::Image("barbara.jpg", cvsupport::Image::DEPTH_16));
     runtime::Float64 beta(10.0);
     runtime::Float64 gamma(2.0);
     
     m_operator->setInputData(AddWeighted::SRC_1, src1);
     m_operator->setParameter(AddWeighted::ALPHA, alpha);
     m_operator->setInputData(AddWeighted::SRC_2, src2);
     m_operator->setParameter(AddWeighted::BETA, beta);
     m_operator->setParameter(AddWeighted::GAMMA, gamma);
     
     runtime::DataContainer dstResult = m_operator->getOutputData(AddWeighted::DST);
     
     runtime::ReadAccess dstAccess(dstResult);
     cvsupport::Image::save("AddWeightedTest_testAllocate0_dst.png", dstAccess.get<runtime::Image>());
 }
Example #3
0
GPU_PERF_TEST(AddMat, cv::gpu::DeviceInfo, cv::Size, perf::MatType)
{
    cv::gpu::DeviceInfo devInfo = GET_PARAM(0);
    cv::Size size = GET_PARAM(1);
    int type = GET_PARAM(2);

    cv::gpu::setDevice(devInfo.deviceID());

    cv::Mat src1_host(size, type);
    cv::Mat src2_host(size, type);

    fill(src1_host, 0.0, 100.0);
    fill(src2_host, 0.0, 100.0);

    cv::gpu::GpuMat src1(src1_host);
    cv::gpu::GpuMat src2(src2_host);
    cv::gpu::GpuMat dst;

    TEST_CYCLE()
    {
        cv::gpu::add(src1, src2, dst);
    }
}
Example #4
0
static void
testMask2()
{
  CFile *ifile1 = new CFile("data/transparent.png");
  CFile *ifile2 = new CFile("data/png_mask.png");

  CFile *ofile = new CFile("transparent1.png");

  CImageFileSrc src1(*ifile1);

  CImagePtr image1 = CImageMgrInst->createImage(src1);

  CImageFileSrc src2(*ifile2);

  CImagePtr image2 = CImageMgrInst->createImage(src2);

  image1->alphaMask(image2);

  image1->writePNG(ofile);

  delete ifile1;
  delete ifile2;
  delete ofile;
}
Example #5
0
void copy_channels_node_t::do_process( const render::render_context_t& context)
{
    Imath::Box2i src1_area( intersect( input(0)->defined(), defined()));
    image::const_image_view_t src1( input(0)->const_subimage_view( src1_area));

    Imath::Box2i src2_area( intersect( input(1)->defined(), defined()));
    image::const_image_view_t src2( input(1)->const_subimage_view( src2_area));

    image::image_view_t dst1( subimage_view( src1_area));
    image::image_view_t dst2( subimage_view( src2_area));

    int ch;

    ch = get_value<int>( param( "red"));
    switch( ch)
    {
    case copy_source:
	copy_channel( src1, 0, dst1, 0);
    break;

    default:
	copy_channel( src2, ch-1, dst2, 0);
    }

    ch = get_value<int>( param( "green"));
    switch( ch)
    {
    case copy_source:
	copy_channel( src1, 1, dst1, 1);
    break;

    default:
	copy_channel( src2, ch-1, dst2, 1);
    }

    ch = get_value<int>( param( "blue"));
    switch( ch)
    {
    case copy_source:
	copy_channel( src1, 2, dst1, 2);
    break;

    default:
	copy_channel( src2, ch-1, dst2, 2);
    }

    ch = get_value<int>( param( "alpha"));
    switch( ch)
    {
    case copy_source:
	copy_channel( src1, 3, dst1, 3);
    break;

    case set_one:
    case set_zero:
    {
	Imath::Box2i area( intersect( defined(), domain()));
	copy_channel( src1, ch-1, subimage_view( area), 3);
    }
    break;

    default:
	copy_channel( src2, ch-1, dst2, 3);
    }
}
Example #6
0
int main(int argc, char** argv)
{
	bool srcbin = 0;
	bool invbk = 0;
	if(argc < 3){
		printf("Not enough args!\narg1: target image\narg2: source image\narg3: do source image adaptive threshold or not\narg4: invert back ground or not\n");
		getchar();
		return 1;
	}
	if(argc >= 4){
		if(!strcmp(argv[3], "1"))
			srcbin = 1;
	}
	if(argc >= 5){
		if(!strcmp(argv[4], "1"))
			invbk = 1;
	}

	IplImage* srcimg= 0, *srcimgb= 0, *srcimgb2 = 0, *bimg = 0, *b2img = 0,*bugimg = 0, *alg2dst = 0;
	srcimg= cvLoadImage(argv[2], 1);
	if (!srcimg)
	{
		printf("src img %s load failed!\n", argv[2]);
		getchar();
		return 1;
	}
	
	//choosing the parameters for our ccl
	int bn = 8; //how many partitions
	int nwidth = 512;
	if(srcimg->width > 512){
		nwidth = 1024;
		bn = 6;
	}
	if(srcimg->width > 1024){
		nwidth = 2048;
		bn = 3;
	}
	if(srcimg->width > 2048){
		printf("warning, image too wide, max support 2048. image is truncated.\n");
		getchar();
		return 1;
	}
	
	//start selection gpu devices
	int devCount;
	int smCnt = 0;
    cudaGetDeviceCount(&devCount);
 
    // Iterate through devices
	int devChosen = 0;
    for (int i = 0; i < devCount; ++i)
    {
        cudaDeviceProp devProp;
        cudaGetDeviceProperties(&devProp, i);
		if(devProp.major >= 2){//only one device supported
			smCnt = max(smCnt, devProp.multiProcessorCount);
			if(devProp.multiProcessorCount == smCnt)
				devChosen = i;
		}
    }
	
	if(smCnt == 0){
		//our ccl require CUDA cap 2.0 or above, but the Ostava's ccl can be run on any CUDA gpu
		printf("Error, no device with cap 2.x found. Only cpu alg will be run.\n");
		getchar();
		return 1;
	}
	
	if(smCnt != 0){
		cudaSetDevice(devChosen);
		bn = bn * smCnt;
	}

	int nheight = (cvGetSize(srcimg).height-2) / (2*bn);
	if((nheight*2*bn+2) < cvGetSize(srcimg).height)
		nheight++;
	nheight = nheight*2*bn+2;

	if(smCnt != 0)
		printf("gpu ccl for image width 512, 1024, 2048.\nchoosing device %d, width %d, height %d, blocks %d\n", devChosen, nwidth, nheight, bn);

	srcimgb= cvCreateImage(cvSize(nwidth, cvGetSize(srcimg).height),IPL_DEPTH_8U,1);
	srcimgb2= cvCreateImage(cvSize(nwidth, cvGetSize(srcimg).height),IPL_DEPTH_8U,1);
	cvSetImageROI(srcimg, cvRect(0, 0, min(cvGetSize(srcimg).width, nwidth), cvGetSize(srcimg).height));
	cvSetImageROI(srcimgb2, cvRect(0, 0, min(cvGetSize(srcimg).width, nwidth), cvGetSize(srcimg).height));
	cvSet(srcimgb2, cvScalar(0,0,0));
	cvCvtColor(srcimg, srcimgb2, CV_BGRA2GRAY);
	cvResetImageROI(srcimgb2);
	cvReleaseImage(&srcimg);
	if(srcbin)
		cvAdaptiveThreshold(srcimgb2, srcimgb, 1.0, CV_ADAPTIVE_THRESH_MEAN_C, invbk ? CV_THRESH_BINARY_INV :  CV_THRESH_BINARY);
	else
		cvThreshold(srcimgb2, srcimgb, 0.0, 1.0, invbk ? CV_THRESH_BINARY_INV :  CV_THRESH_BINARY);
	boundCheck(srcimgb);

	cvScale(srcimgb, srcimgb2, 255);
	//the source binary image to be labeled is saved as bsrc.bmp
	cvSaveImage("bsrc.bmp", srcimgb2);
	cvSet(srcimgb2, cvScalar(0,0,0));
	
	float elapsedMilliSeconds1;
	{//begin cpu labeling algorithm, the SBLA proposed by Zhao
		LABELDATATYPE *data=(LABELDATATYPE *)malloc(srcimgb->width * srcimgb->height * sizeof(LABELDATATYPE));
	
		for(int j = 0; j<srcimgb->height; j++)
			for(int i = 0; i<srcimgb->width; i++)
				data[i + j*srcimgb->width] = (srcimgb->imageData[i + j*srcimgb->widthStep]) ? 1 : 0;

		int iNumLabels;
		CPerformanceCounter perf;
		perf.Start();
    	iNumLabels = LabelSBLA(data, srcimgb->width, srcimgb->height);
		elapsedMilliSeconds1 = (float)perf.GetElapsedMilliSeconds();
		printf("cpu SBLA used %f ms, total labels %u\n", elapsedMilliSeconds1, iNumLabels);
		free(data);
	}
	
	IplImage *src2(0),*dst2(0);
	int iNumLabels;
	float elapsedMilliSeconds2;
	{//begin cpu labeling algorithm, the BBDT proposed by C. Grana, D. Borghesani, R. Cucchiara
		CPerformanceCounter perf;
		src2 = cvCreateImage( cvGetSize(srcimgb), IPL_DEPTH_8U, 1 );
		cvCopyImage(srcimgb,src2);
		dst2 = cvCreateImage( cvGetSize(srcimgb), IPL_DEPTH_32S, 1 );
		perf.Start();
		cvLabelingImageLab(src2, dst2, 1, &iNumLabels);
		elapsedMilliSeconds2 = (float)perf.GetElapsedMilliSeconds();
		printf("cpu BBDT used %f ms, total labels %u\n", elapsedMilliSeconds2, iNumLabels);
		cvSaveImage("bbdt.bmp", dst2);
//		cvReleaseImage(&src2);
//		cvReleaseImage(&dst2);
	}

	if(smCnt != 0){

	bugimg = cvCreateImage(cvSize(nwidth, 9*bn),IPL_DEPTH_8U,1);
	bimg = cvCreateImage(cvSize(nwidth, 2*bn),IPL_DEPTH_8U,1);
	b2img = cvCreateImage(cvSize(nwidth, 2*bn),IPL_DEPTH_8U,1);

//    cvNamedWindow("src",CV_WINDOW_AUTOSIZE);
//	cvShowImage("src",srcimg);

	//prepare buffers for our gpu algorithm
	CudaBuffer srcBuf, dstBuf, dstBuf2, bBuf, b2Buf, errBuf, glabel;
	srcBuf.Create2D(nwidth, nheight);		//the binary image to be processed
	dstBuf.Create2D(nwidth, (nheight-2)/2); //the label result, only about 1/4 the size of source image contains the final labels
	dstBuf2.Create2D(nwidth,(nheight-2)/2);	//a copy of the pass1 temp result, for debug purpose
	glabel.Create2D(4, 1);					//a int size global buffer for unique final label
	errBuf.Create2D(nwidth, 9*bn);			//a buffer for debug info
	bBuf.Create2D(nwidth, 2 * bn);			//the intersection info used by pass2
	b2Buf.Create2D(nwidth, 2 * bn);			//a copy of bBuf for debug purpose

	srcBuf.SetZeroData();
	srcBuf.CopyFrom(srcimgb->imageData, srcimgb->widthStep, nwidth, cvGetSize(srcimgb).height);

	float elapsedTimeInMs = 0.0f;
    
	//-------------------gpu part----------------------------
    cudaEvent_t start, stop;
    cutilSafeCall  ( cudaEventCreate( &start ) );
    cutilSafeCall  ( cudaEventCreate( &stop ) );
    cutilSafeCall( cudaEventRecord( start, 0 ) );  
	
	if(nwidth == 512)
		label_512(&dstBuf, &dstBuf2, &srcBuf, &bBuf, &b2Buf, &glabel, nheight, bn, &errBuf);
	else if(nwidth == 1024)
		label_1024(&dstBuf, &dstBuf2, &srcBuf, &bBuf, &b2Buf, &glabel, nheight, bn, &errBuf);
	else if(nwidth == 2048)
		label_2048(&dstBuf, &dstBuf2, &srcBuf, &bBuf, &b2Buf, &glabel, nheight, bn, &errBuf);

    cutilSafeCall( cudaEventRecord( stop, 0 ) );
//	cutilCheckMsg("kernel launch failure");
	cudaEventSynchronize(stop);
	cutilSafeCall( cudaEventElapsedTime( &elapsedTimeInMs, start, stop ) );
	uint tlabel = 0;

	cudaMemcpy(&tlabel, glabel.GetData(), 4, cudaMemcpyDeviceToHost);
	printf("gpu alg 1 used %f ms, total labels %u\n", elapsedTimeInMs, tlabel);

	dstBuf.CopyToHost(srcimgb->imageData, srcimgb->widthStep, nwidth, (nheight-2)/2);
	dstBuf2.CopyToHost(srcimgb2->imageData, srcimgb->widthStep, nwidth, (nheight-2)/2);
	errBuf.CopyToHost(bugimg->imageData, bugimg->widthStep, nwidth, 9*bn);
	bBuf.CopyToHost(bimg->imageData, bimg->widthStep, nwidth, 2*bn);
	b2Buf.CopyToHost(b2img->imageData, bimg->widthStep, nwidth, 2*bn);

//	cvNamedWindow("gpu",CV_WINDOW_AUTOSIZE);
//	cvShowImage("gpu",srcimgb);
	cvSaveImage(argv[1], srcimgb);
	cvSaveImage("gpu2.bmp", srcimgb2);	//the final labels of our algorithm
	cvSaveImage("bug.bmp", bugimg);
	cvSaveImage("b.bmp", bimg);
	cvSaveImage("b2.bmp", b2img);
	
	//now start the gpu ccl implemented by Ostava
	alg2dst= cvCreateImage(cvSize(nwidth*4, cvGetSize(srcimgb).height),IPL_DEPTH_8U,1);
	CCLBase* m_ccl;
	m_ccl = new CCL();	

	m_ccl->FindRegions(nwidth, cvGetSize(srcimgb).height, &srcBuf);
	m_ccl->GetConnectedRegionsBuffer()->CopyToHost(alg2dst->imageData, alg2dst->widthStep, nwidth*4, cvGetSize(srcimgb).height);
	delete m_ccl;
	cvSaveImage("alg2.bmp", alg2dst);

	cvReleaseImage(&bugimg);
	cvReleaseImage(&bimg);
	cvReleaseImage(&b2img);
	cvReleaseImage(&alg2dst);
//	}
	//cvWaitKey(0);
	
	//now start cross compare label results of our ccl and the BBDT, to check the correctness
//	if(smCnt != 0){
		ushort *gpures, *cpures;
		uint sz = nwidth * (cvGetSize(srcimgb).height/2);
		gpures = (ushort*)malloc(sz);
		cpures = (ushort*)malloc(sz);
		dstBuf.CopyToHost(gpures, nwidth, nwidth, (cvGetSize(srcimgb).height/2));
		
		//first, reduce cpu labels from one label for each pixel to one label for a 2x2 block, assuming 8-connectivity
		for(int j = 0; j < (cvGetSize(srcimgb).height/2); j++)
			for(int i = 0; i < (nwidth/2); i++){
				uint* cpup;
				ushort res = LBMAX;
				uint y = j*2, x = i*2;
				cpup = (uint*)(dst2->imageData + y*dst2->widthStep);
//				if(y < cvGetSize(srcimgb).height){
					if(cpup[x] != 0)
						res = cpup[x]-1;
					if(cpup[x+1] != 0)
						res = cpup[x+1]-1;
//				}
				y++;
				cpup = (uint*)(dst2->imageData + y*dst2->widthStep);
//				if(y < cvGetSize(srcimgb).height){
					if(cpup[x] != 0)
						res = cpup[x]-1;
					if(cpup[x+1] != 0)
						res = cpup[x+1]-1;
//				}
				cpures[i + j*(nwidth/2)] = res;
			}
		
		//our algo use unsigned short to represent a label, the first label starts a 0, and maximun labels is LBMAX
		if(iNumLabels > LBMAX)
			printf("too much cc, compare abort.\n");
		else{
			//create a error
			//cpures[5] = 12;
			//cpures[15] = 18;
			printf("Checking correctness of gpu alg1\nChecking gpu ref by cpu.\n");
			checkLabels(cpures, gpures, nwidth/2, cvGetSize(srcimgb).height/2, iNumLabels);

			printf("Checking cpu ref by gpu.\n");
			checkLabels(gpures, cpures, nwidth/2, cvGetSize(srcimgb).height/2, tlabel);
		}

		free(gpures);
		free(cpures);
		printf("speedup is %f, %f, %f\n", gpu2time/elapsedTimeInMs, elapsedMilliSeconds1/elapsedTimeInMs, elapsedMilliSeconds2/elapsedTimeInMs);
	}

	cvReleaseImage(&srcimgb);
	cvReleaseImage(&srcimgb2);
	cvReleaseImage(&dst2);
	cvReleaseImage(&src2);

    cutilSafeCall( cudaThreadExit() );
	return 0;

}
Example #7
0
int LibVision::calcCorrelationCoefficient(cv::Mat src, cv::Mat img, int *rot) {
  unsigned int j;
  int i;
  double tempsim;
  int warpSize = 56;
  double N = (double)(warpSize * warpSize / 4);
  double nom, den;
  float confThreshold = 0.9;
  cv::Scalar mean_ext, std_ext, mean_int, std_int;
  std::vector<cv::Mat> loadedPatterns;
  int msize = warpSize;

  cv::Mat src2(msize, msize, CV_8UC1);
  cv::Point2f center((msize - 1) / 2.0f, (msize - 1) / 2.0f);
  cv::Mat rot_mat(2, 3, CV_32F);
  cv::resize(img, src2, cv::Size(msize, msize));
  cv::Mat subImg = src2(cv::Range(msize / 4, 3 * msize / 4),
                        cv::Range(msize / 4, 3 * msize / 4));
  loadedPatterns.push_back(subImg);
  rot_mat = cv::getRotationMatrix2D(center, 90, 1.0);

  for (int i = 1; i < 4; i++) {
    cv::Mat dst = cv::Mat(msize, msize, CV_8UC1);
    rot_mat = cv::getRotationMatrix2D(center, -i * 90, 1.0);
    cv::warpAffine(src2, dst, rot_mat, cv::Size(msize, msize));
    cv::Mat subImg = dst(cv::Range(msize / 4, 3 * msize / 4),
                         cv::Range(msize / 4, 3 * msize / 4));
    loadedPatterns.push_back(subImg);
  }

  cv::Mat normROI = cv::Mat(warpSize, warpSize, CV_8UC1); // normalized ROI
  // Masks for exterior(black) and interior area inside the pattern
  cv::Mat patMask = cv::Mat::ones(warpSize, warpSize, CV_8UC1);
  cv::Mat submat = patMask(cv::Range(warpSize / 4, 3 * warpSize / 4),
                           cv::Range(warpSize / 4, 3 * warpSize / 4));
  cv::Mat patMaskInt = cv::Mat::zeros(warpSize, warpSize, CV_8UC1);
  submat = patMaskInt(cv::Range(warpSize / 4, 3 * warpSize / 4),
                      cv::Range(warpSize / 4, 3 * warpSize / 4));
  submat = cv::Scalar(1);
  cv::meanStdDev(src, mean_ext, std_ext, patMask);
  cv::meanStdDev(src, mean_int, std_int, patMaskInt);
  if ((mean_ext.val[0] > mean_int.val[0]))
    return -1;

  cv::Mat inter = src(cv::Range(warpSize / 4, 3 * warpSize / 4),
                      cv::Range(warpSize / 4, 3 * warpSize / 4));
  double normSrcSq = pow(norm(inter), 2);
  int zero_mean_mode = 1;
  float maxCor = -1.0;
  // int index = 0;
  int ori = 0;
  for (j = 0; j < (loadedPatterns.size() / 4); j++) {
    for (i = 0; i < 4; i++) {
      double const nnn = pow(norm(loadedPatterns.at(j * 4 + i)), 2);
      if (zero_mean_mode == 1) {
        double const mmm = mean(loadedPatterns.at(j * 4 + i)).val[0];
        nom = inter.dot(loadedPatterns.at(j * 4 + i)) -
              (N * mean_int.val[0] * mmm);
        den = sqrt((normSrcSq - (N * mean_int.val[0] * mean_int.val[0])) *
                   (nnn - (N * mmm * mmm)));
        tempsim = nom / den;
      } else {
        tempsim =
            inter.dot(loadedPatterns.at(j * 4 + i)) / (sqrt(normSrcSq * nnn));
      }
      if (tempsim > maxCor) {
        maxCor = tempsim;
        ori = i;
      }
    }
  }
  *rot = ori;
  if (maxCor > confThreshold)
    return 1;
  else
    return 0;
}
Example #8
0
TEST(SAVE, SaveGraph)
{
	std::string expect_pbfile = testdir + "/graph.pb";
	std::string got_pbfile = "got_graph.pb";
	cortenn::Graph graph;
	std::vector<ade::TensptrT> roots;

	pbm::PathedMapT labels;
	// subtree one
	ade::Shape shape({3, 7});
	ade::TensptrT osrc(new MockTensor(shape));

	ade::Shape shape2({7, 3});
	ade::TensptrT osrc2(new MockTensor(shape2));

	labels[osrc] = {"global", "osrc"};
	labels[osrc2] = {"global", "osrc2"};

	{
		ade::TensptrT src(new MockTensor(shape));

		ade::Shape shape3({3, 1, 7});
		ade::TensptrT src2(new MockTensor(shape3));

		ade::TensptrT dest(ade::Functor::get(ade::Opcode{"-", 0}, {
			{src2, ade::identity},
			{ade::TensptrT(ade::Functor::get(ade::Opcode{"@", 1}, {
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"/", 2}, {
					{ade::TensptrT(ade::Functor::get(ade::Opcode{"neg", 3}, {
						{osrc, ade::identity},
					})), ade::identity},
					{ade::TensptrT(ade::Functor::get(ade::Opcode{"+", 4}, {
						{ade::TensptrT(
							ade::Functor::get(ade::Opcode{"sin", 5}, {
							{src, ade::identity}})), ade::identity},
						{src, ade::identity},
					})), ade::identity}
				})), ade::permute({1, 0})},
				{osrc2, ade::identity}
			})), ade::permute({1, 2, 0})},
		}));
		roots.push_back(dest);

		labels[src] = {"subtree", "src"};
		labels[src2] = {"subtree", "src2"};
		labels[dest] = {"subtree", "dest"};
	}

	// subtree two
	{
		ade::Shape mshape({3, 3});
		ade::TensptrT src(new MockTensor(mshape));

		ade::TensptrT src2(new MockTensor(mshape));

		ade::TensptrT src3(new MockTensor(mshape));

		ade::TensptrT dest(ade::Functor::get(ade::Opcode{"-", 0}, {
			{src, ade::identity},
			{ade::TensptrT(ade::Functor::get(ade::Opcode{"*", 6}, {
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"abs", 7}, {
					{src, ade::identity},
				})), ade::identity},
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"exp", 8}, {
					{src2, ade::identity},
				})), ade::identity},
				{ade::TensptrT(ade::Functor::get(ade::Opcode{"neg", 3}, {
					{src3, ade::identity},
				})), ade::identity},
			})), ade::identity},
		}));
		roots.push_back(dest);

		labels[src] = {"subtree2", "src"};
		labels[src2] = {"subtree2", "src2"};
		labels[src3] = {"subtree2", "src3"};
		labels[dest] = {"subtree2", "dest"};
	}

	pbm::GraphSaver<TestSaver> saver;
	for (auto& root : roots)
	{
		root->accept(saver);
	}

	saver.save(graph, labels);

	{
		std::fstream gotstr(got_pbfile,
			std::ios::out | std::ios::trunc | std::ios::binary);
		ASSERT_TRUE(gotstr.is_open());
		ASSERT_TRUE(graph.SerializeToOstream(&gotstr));
	}

	std::fstream expect_ifs(expect_pbfile, std::ios::in | std::ios::binary);
	std::fstream got_ifs(got_pbfile, std::ios::in | std::ios::binary);
	ASSERT_TRUE(expect_ifs.is_open());
	ASSERT_TRUE(got_ifs.is_open());

	std::string expect;
	std::string got;
	// skip the first line (it contains timestamp)
	expect_ifs >> expect;
	got_ifs >> got;
	for (size_t lineno = 1; expect_ifs && got_ifs; ++lineno)
	{
		expect_ifs >> expect;
		got_ifs >> got;
		EXPECT_STREQ(expect.c_str(), got.c_str()) << "line number " << lineno;
	}
}
Example #9
0
/// GYU Decoding
///
/// @param archive Archive
///
bool CRetouch::DecodeGYU(CArcFile* archive)
{
	const SFileInfo* file_info = archive->GetOpenFileInfo();

	if (file_info->format != _T("GYU"))
		return false;

	// Read header
	GYUHeader gyu_header;
	archive->Read(&gyu_header, sizeof(gyu_header));
	if (memcmp(gyu_header.identifier, "GYU\x1A", 4) != 0)
	{
		// File does not matter
		archive->SeekCur(-static_cast<s64>(sizeof(gyu_header)));
		return false;
	}

	// Read pallet
	u8 palette[1024] = {};
	const u32 palette_size = gyu_header.pallets * 4;
	if (palette_size > 0)
	{
		// Palette exists
		u8 black_palette[1024] = {};
		archive->Read(palette, palette_size);

		if (memcmp(palette, black_palette, palette_size) == 0)
		{
			// All pallets set to zero
			for (size_t i = 0, j = 0; i < palette_size; i += sizeof(u32), j++)
			{
				palette[i + 0] = static_cast<u8>(j);
				palette[i + 1] = static_cast<u8>(j);
				palette[i + 2] = static_cast<u8>(j);
				palette[i + 3] = 0;
			}
		}
	}

	// Read
	const u32 src_size = gyu_header.compressed_sizes[0];
	std::vector<u8> src(src_size);
	archive->Read(src.data(), src.size());

	// Ensure output buffers exist
	const u32 dst_size = ((gyu_header.width * (gyu_header.bpp >> 3) + 3) & 0xFFFFFFFC) * gyu_header.height;
	std::vector<u8> dst(dst_size);

	// Decrypt GYU
	DecryptGYU(src.data(), src.size(), gyu_header.key);

	// Decompress GYU
	DecompGYU(dst.data(), dst.size(), src.data(), src.size());

	// Output
	if (gyu_header.compressed_sizes[1] != 0)
	{
		// Alpha value exists

		// Read
		const u32 src_size2 = gyu_header.compressed_sizes[1];
		std::vector<u8> src2(src_size2);
		archive->Read(src2.data(), src2.size());

		// Ensure output buffer exists
		const u32 dst_size2 = ((gyu_header.width * (gyu_header.bpp >> 3) + 3) & 0xFFFFFFFC) * gyu_header.height;
		std::vector<u8> dst2(dst_size2);

		// Decompress GYU
		DecompGYU(dst2.data(), dst2.size(), src2.data(), src2.size());

		// Ensure output buffer exists
		const u32 dst_32bit_size = gyu_header.width * gyu_header.height * 4;
		std::vector<u8> dst_32bit(dst_32bit_size);

		// Into 32-bit by adding the alpha value
		for (long i = 0, y = 0; i < dst_32bit_size && y < gyu_header.height; y++)
		{
			const long present_height = ((gyu_header.width + 3) & 0xFFFFFFFC) * y;

			for (long x = 0; x < gyu_header.width; x++)
			{
				dst_32bit[i++] = palette[dst[present_height + x] * 4 + 0];
				dst_32bit[i++] = palette[dst[present_height + x] * 4 + 1];
				dst_32bit[i++] = palette[dst[present_height + x] * 4 + 2];

				if (dst2[present_height + x] < 0x10)
				{
					dst_32bit[i++] = dst2[present_height + x] * 16;
				}
				else
				{
					dst_32bit[i++] = 0xFF;
				}
			}
		}

		// Output
		CImage image;
		image.Init(archive, gyu_header.width, gyu_header.height, 32);
		image.Write(dst_32bit.data(), dst_32bit.size());
	}
	else
	{
Example #10
0
bool test_feature(const vector<accelerator>& devices)
{
	int edata[_rank];
	for (int i = 0; i < _rank; i++)
		edata[i] = 3;
	
	printf("Found %d devices\n", devices.size());

	for (size_t i = 0; i < devices.size()-1; i++)
	{
		accelerator device1 = devices[i];
		accelerator device2 = devices[i+1];

		if (_rank > 0)
		{
			const int rank = 1;
			Concurrency::array<_type, rank> src1(edata[0], device1.get_default_view());
			Concurrency::array<_type, rank> src2(edata[0], device2.get_default_view());

			// let the kernel initialize data;
			extent<1> e1(edata);
			parallel_for_each(e1, [&](index<rank> idx) __GPU_ONLY
			{
				src1[idx] = _rank;
				src2[idx] = _rank;
			});

			// Copy data to CPU
			vector<_type> opt1(e1.size());
			opt1 = src1;
			vector<_type> opt2(e1.size());
			opt2 = src2;

			for (unsigned int i = 0; i < e1.size(); i++)
			{
				if ((opt1[i] != _rank) || (opt2[i] != _rank))
					return false;
			}
			printf ("Passed for rank %d\n", rank);
			rank_step = rank;
		}
		if (_rank > 1)
		{
			const int rank = 2;
			rank_step = rank;
			Concurrency::array<_type, rank> src1(edata[0], edata[1], device1.get_default_view());
			Concurrency::array<_type, rank> src2(edata[0], edata[1], device2.get_default_view());

			// let the kernel initialize data;
			extent<rank> e1(edata);
			parallel_for_each(e1, [&](index<rank> idx) __GPU_ONLY
			{
				src1[idx] = _rank;
				src2[idx] = _rank;
			});

			// Copy data to CPU
			vector<_type> opt1(e1.size());
			opt1 = src1;
			vector<_type> opt2(e1.size());
			opt2 = src2;

			for (unsigned int i = 0; i < e1.size(); i++)
			{
				if ((opt1[i] != _rank) || (opt2[i] != _rank))
					return false;
			}
			printf ("Passed for rank %d\n", rank);
			rank_step = rank;
		}
Example #11
0
void Hud::renderRadar(const float dt, sdlx::Surface &window, const std::vector<v3<int> > &specials, const std::vector<v3<int> > &flags, const sdlx::Rect &viewport) {
    if (!Map->loaded()) {
        _radar.free();
        _radar_bg.free();
        return;
    }

    if (_map_mode == MapNone || !_enable_radar)
        return;

    if (!_radar.isNull() && !_update_radar.tick(dt)) {
        const int x = window.get_width() - _radar.get_width(), y = _background->get_height();
        window.blit(_radar, x, y);
        return;
    }

    if (_radar_bg.isNull())
        generateRadarBG(viewport); //needed for destructable layers.

    v2<int> radar_size;

    if (_map_mode == MapSmall) {
        radar_size.x = math::min(window.get_width() / 8, _radar_bg.get_width());
        radar_size.y = math::min(window.get_height() / 8, _radar_bg.get_height());
    } else {
        radar_size.x = _radar_bg.get_width();
        radar_size.y = _radar_bg.get_height();
    }

    if (_radar.isNull()) {
        _radar.create_rgb(radar_size.x, radar_size.y, 32);
        _radar.display_format_alpha();
    }

    const int x = window.get_width() - _radar.get_width(), y = _background->get_height();
    v2<int> msize = Map->get_size();

    v2<int> radar_shift;
    if (_map_mode == MapSmall || Map->torus()) {
        radar_shift.x = viewport.x + viewport.w / 2 - msize.x / 2 - msize.x * (_radar.get_width() - _radar_bg.get_width()) / 2 / _radar_bg.get_width();
        radar_shift.y = viewport.y + viewport.h / 2 - msize.y / 2 - msize.y * (_radar.get_height() - _radar_bg.get_height()) / 2 / _radar_bg.get_height();
        Map->validate(radar_shift);
    }

    if (Map->torus()) {
        /* 2x2 split
        [12]
        [34]
        */
        v2<int> split = radar_shift;
        //LOG_DEBUG(("split: %d %d %d %d", split.x, split.y, viewport.x, viewport.y));
        split *= v2<int>(_radar_bg.get_width(), _radar_bg.get_height());
        split /= msize;
        //int split_x = (viewport.w - viewport.x) * _radar_bg.get_width() / msize.x, split_y = (viewport.h - viewport.y) * _radar_bg.get_width() / msize.x;

        _radar.fill(_radar.map_rgba(0,0,0,255));
        sdlx::Rect src1(split.x - _radar_bg.get_width(), split.y - _radar_bg.get_height(), _radar_bg.get_width(), _radar_bg.get_height());
        sdlx::Rect src2(split.x, split.y - _radar_bg.get_height(), _radar_bg.get_width(), _radar_bg.get_height());
        sdlx::Rect src3(split.x - _radar_bg.get_width(), split.y, _radar_bg.get_width(), _radar_bg.get_height());
        sdlx::Rect src4(split.x, split.y, _radar_bg.get_width(), _radar_bg.get_height());
        _radar.blit(_radar_bg, src1, 0, 0);
        _radar.blit(_radar_bg, src2, 0, 0);
        _radar.blit(_radar_bg, src3, 0, 0);
        _radar.blit(_radar_bg, src4, 0, 0);
    } else {
        if (radar_shift.x < 0)
            radar_shift.x = 0;
        if (radar_shift.y < 0)
            radar_shift.y = 0;
        v2<int> radar_map_size = radar_size * msize / v2<int>(_radar_bg.get_width(), _radar_bg.get_height());

        if (radar_shift.x + radar_map_size.x > msize.x)
            radar_shift.x = msize.x - radar_map_size.x;

        if (radar_shift.y + radar_map_size.y > msize.y)
            radar_shift.y = msize.y - radar_map_size.y;

        v2<int> shift = radar_shift * v2<int>(_radar_bg.get_width(), _radar_bg.get_height()) / msize;
        sdlx::Rect src(shift.x, shift.y, _radar.get_width(), _radar.get_height());
        _radar.blit(_radar_bg, src, 0, 0);
    }

    //LOG_DEBUG(("radar shift: %d %d", radar_shift.x, radar_shift.y));

    _radar.lock();

    size_t n = PlayerManager->get_slots_count();
    for(size_t i = 0; i < n; ++i) {
        PlayerSlot &slot = PlayerManager->get_slot(i);
        const Object *obj = slot.getObject();
        if (obj == NULL)
            continue;

        v2<int> pos;
        obj->get_center_position(pos);
        pos -= radar_shift;
        Map->validate(pos);

        _radar.put_pixel(pos.x * _radar_bg.get_width() / msize.x, pos.y * _radar_bg.get_height() / msize.y, index2color(_radar, i + 1, 255));
        _radar.put_pixel(pos.x * _radar_bg.get_width() / msize.x, pos.y * _radar_bg.get_height() / msize.y + 1, index2color(_radar, i + 1, 200));
        _radar.put_pixel(pos.x * _radar_bg.get_width() / msize.x, pos.y * _radar_bg.get_height() / msize.y - 1, index2color(_radar, i + 1, 200));
        _radar.put_pixel(pos.x * _radar_bg.get_width() / msize.x + 1, pos.y * _radar_bg.get_height() / msize.y, index2color(_radar, i + 1, 200));
        _radar.put_pixel(pos.x * _radar_bg.get_width() / msize.x - 1, pos.y * _radar_bg.get_height() / msize.y, index2color(_radar, i + 1, 200));
    }

    static bool blink;

    blink = !blink;
    if (blink) {
        //format me
        n = specials.size();
        for(size_t i = 0; i < n; ++i) {
            v3<int> pos = specials[i];
            {
                v2<int> p(pos.x, pos.y);
                p -= radar_shift;
                Map->validate(p);
                pos.x = p.x;
                pos.y = p.y;
            }

            Uint32 color[2];
            color[0] = index2color(_radar, i + 1, 255);
            color[1] = index2color(_radar, i + 1, 200);
            for(int b = 0; b < 2; ++b) {
                _radar.put_pixel(b + pos.x * _radar_bg.get_width() / msize.x, pos.y * _radar_bg.get_height() / msize.y, color[b]);
                for(int l = 1; l <= 2; ++l) {
                    _radar.put_pixel(b + pos.x * _radar_bg.get_width() / msize.x + l, pos.y * _radar_bg.get_height() / msize.y + l, color[b]);
                    _radar.put_pixel(b + pos.x * _radar_bg.get_width() / msize.x - l, pos.y * _radar_bg.get_height() / msize.y - l, color[b]);
                    _radar.put_pixel(b + pos.x * _radar_bg.get_width() / msize.x + l, pos.y * _radar_bg.get_height() / msize.y - l, color[b]);
                    _radar.put_pixel(b + pos.x * _radar_bg.get_width() / msize.x - l, pos.y * _radar_bg.get_height() / msize.y + l, color[b]);
                }
            }
        }


        n = flags.size();
        if (n > 2)
            n = 2;

        for(size_t i = 0; i < n; ++i) {
            v3<int> pos = flags[i];
            {
                v2<int> p(pos.x, pos.y);
                p -= radar_shift;
                Map->validate(p);
                pos.x = p.x;
                pos.y = p.y;
            }

            Uint32 color[2] = { _radar.map_rgb(255, 0, 0), _radar.map_rgb(0, 255, 0), };
            _radar.put_pixel(0 + pos.x * _radar_bg.get_width() / msize.x, 0 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(1 + pos.x * _radar_bg.get_width() / msize.x, 0 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(2 + pos.x * _radar_bg.get_width() / msize.x, 0 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(0 + pos.x * _radar_bg.get_width() / msize.x, 1 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(1 + pos.x * _radar_bg.get_width() / msize.x, 1 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(0 + pos.x * _radar_bg.get_width() / msize.x, 2 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(1 + pos.x * _radar_bg.get_width() / msize.x, 2 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(2 + pos.x * _radar_bg.get_width() / msize.x, 2 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
            _radar.put_pixel(0 + pos.x * _radar_bg.get_width() / msize.x, 3 + pos.y * _radar_bg.get_height() / msize.y, color[i]);
        }

    } //blink

    _radar.unlock();

    window.blit(_radar, x, y);
}