Example #1
0
c_haship_pubkey::c_haship_pubkey( const string_as_bin & input ) {
	_mark("Loadig pubkey from: " << ::to_debug(input) );
	this->load_from_bin(input.bytes);
	_mark("After loading: " << (*this) );
	//for(size_t i=0; i<input.bytes.size(); ++i) at(i) = input.bytes.at(i);
//	for(auto v : input.bytes) at(
}
Example #2
0
void example_memcheck_2() { // to test valgrind detection of errors
	_mark("Valgrind memcheck test.");
	vector<int> vec(100);
	vec[2000] = 42;
	volatile auto * ptr = & vec[3000];
	*ptr = 42;
	_mark("Valgrdin memcheck test done (program not aborted), result");
}
Example #3
0
void example_ubsan_1() { // to test UBSAN / ub sanitize
	_mark("UBSAN test.");
	// TODO make it work for "signed char" as well
	signed int i;
	i = std::numeric_limits<decltype(i)>::max();
	i += static_cast<decltype(i)>(5); // overflow of signed
	_mark("UBSAN test done (program not aborted)");
}
Example #4
0
void example_memcheck_1() { // to test valgrind detection of errors
	_mark("Valgrind memcheck test.");
	vector<int> vec(100);
	int* ptr = & vec.at(0);
	ptr += 200;
	volatile int read = *ptr;
	volatile int result = (read==0) ? 0 : 1; // to silence "unused var" and to cause e.g. cause "jump depends on uninitialized"
	_mark("Valgrdin memcheck test done (program not aborted), result"<<result);
}
Example #5
0
void example_tsan_1() { // to test STAN / thread sanitizer
	_mark("TSAN test.");
	int data=10;
	// run concurent update of data:
	auto thread1 = std::thread( [&](){ data=20; } );
	auto thread2 = std::thread( [&](){ data=30; } );
	thread1.join();
	thread2.join();
	_mark("TSAN test done (program not aborted)");
}
Example #6
0
double ApplyCurve(FIBITMAP *src, FIBITMAP *dst, std::vector<cp> ctpts, int threadcount)
{
    _mark();
    unsigned spitch = FreeImage_GetPitch(src);
    unsigned dpitch = FreeImage_GetPitch(dst);
    unsigned w = FreeImage_GetWidth(src);
    unsigned h = FreeImage_GetHeight(src);
    BYTE * srcbits = FreeImage_GetBits(src);
    BYTE * dstbits = FreeImage_GetBits(dst);

    Curve c;
    BYTE LUT8[256];
    WORD LUT16[65536];
    c.setControlPoints(ctpts);
    int bpp = FreeImage_GetBPP(src);
    if (bpp == 24) {
        c.clampto(0.0,255.0);
        for (int x=0; x<256; x++) {
            LUT8[x] = (BYTE)floor(c.getpoint(x) + 0.5);
        }
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < h; y++) {
            for(unsigned x = 0; x < w; x++) {
                BYTE * bdstpix = (BYTE *) dstbits + dpitch*y + 3*x;
                BYTE *pixel = (BYTE *) (srcbits + spitch*y + 3*x);
                bdstpix[FI_RGBA_RED]   = LUT8[pixel[FI_RGBA_RED]];
                bdstpix[FI_RGBA_GREEN] = LUT8[pixel[FI_RGBA_GREEN]];
                bdstpix[FI_RGBA_BLUE]  = LUT8[pixel[FI_RGBA_BLUE]];

                //bdstpix[FI_RGBA_RED]   = ((BYTE *) LUT8)[pixel[FI_RGBA_RED]];
                //bdstpix[FI_RGBA_GREEN] = ((BYTE *) LUT8)[pixel[FI_RGBA_GREEN]];
                //bdstpix[FI_RGBA_BLUE]  = ((BYTE *) LUT8)[pixel[FI_RGBA_BLUE]];
            }
        }
    }
    if (bpp == 48) {
        c.scalepoints(256.0);
        c.clampto(0.0,65535.0);
        for (int x=0; x<65536; x++) {
            LUT16[x] = (WORD)floor(c.getpoint(x) + 0.5);
        }
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < h; y++) {
            for(unsigned x = 0; x < w; x++) {
                FIRGB16 * wdstpix = (FIRGB16 *) (dstbits + dpitch*y + 6*x);
                FIRGB16 * pixel   = (FIRGB16 *) (srcbits + spitch*y + 6*x);
                wdstpix->red   = LUT16[pixel->red];
                wdstpix->green = LUT16[pixel->green];
                wdstpix->blue  = LUT16[pixel->blue];

                //wdstpix->red   = ((WORD *) LUT16)[pixel->red];
                //wdstpix->green = ((WORD *) LUT16)[pixel->green];
                //wdstpix->blue  = ((WORD *) LUT16)[pixel->blue];
            }
        }
    }
    return _duration();
}
Example #7
0
double ApplyLUT(FIBITMAP *src, FIBITMAP *dst, char * LUT, int threadcount)
{
    _mark();
    unsigned spitch = FreeImage_GetPitch(src);
    unsigned dpitch = FreeImage_GetPitch(dst);
    unsigned w = FreeImage_GetWidth(src);
    unsigned h = FreeImage_GetHeight(src);
    BYTE * srcbits = FreeImage_GetBits(src);
    BYTE * dstbits = FreeImage_GetBits(dst);

    BYTE LUT8[256];
    WORD LUT16[65536];
    int bpp = FreeImage_GetBPP(src);
    if (bpp == 24) {
        for (int x=0; x<256; x++) {
            LUT8[x] = ((BYTE *) LUT)[x];;
        }
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < h; y++) {
            for(unsigned x = 0; x < w; x++) {
                BYTE * bdstpix = (BYTE *) (dstbits + dpitch*y + 3*x);
                BYTE * pixel   = (BYTE *) (srcbits + spitch*y + 3*x);
                bdstpix[FI_RGBA_RED]   = LUT8[pixel[FI_RGBA_RED]];
                bdstpix[FI_RGBA_GREEN] = LUT8[pixel[FI_RGBA_GREEN]];
                bdstpix[FI_RGBA_BLUE]  = LUT8[pixel[FI_RGBA_BLUE]];

                //bdstpix[FI_RGBA_RED]   = ((BYTE *) LUT8)[pixel[FI_RGBA_RED]];
                //bdstpix[FI_RGBA_GREEN] = ((BYTE *) LUT8)[pixel[FI_RGBA_GREEN]];
                //bdstpix[FI_RGBA_BLUE]  = ((BYTE *) LUT8)[pixel[FI_RGBA_BLUE]];


            }
        }
    }
    if (bpp == 48) {
        for (int x=0; x<65536; x++) {
            LUT16[x] = ((WORD *)LUT)[x];
        }
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < h-1; y++) {
            for(unsigned x = 0; x < w-1; x++) {
                FIRGB16 * wdstpix = (FIRGB16 *) (dstbits + dpitch*y + 6*x);
                FIRGB16 * pixel   = (FIRGB16 *) (srcbits + spitch*y + 6*x);
                wdstpix->red   = LUT16[pixel->red];
                wdstpix->green = LUT16[pixel->green];
                wdstpix->blue  = LUT16[pixel->blue];

                //wdstpix->red   = ((WORD *) LUT16)[pixel->red];
                //wdstpix->green = ((WORD *) LUT16)[pixel->green];
                //wdstpix->blue  = ((WORD *) LUT16)[pixel->blue];


            }
        }
    }
    return _duration();
}
Example #8
0
double ApplyLUT2LUMA(FIBITMAP *src, FIBITMAP *dst, char * LUT, int threadcount)
{
    _mark();
    unsigned spitch = FreeImage_GetPitch(src);
    unsigned dpitch = FreeImage_GetPitch(dst);
    unsigned w = FreeImage_GetWidth(src);
    unsigned h = FreeImage_GetHeight(src);
    BYTE * srcbits = FreeImage_GetBits(src);
    BYTE * dstbits = FreeImage_GetBits(dst);

    BYTE LUT8[256];
    WORD LUT16[65536];
    int bpp = FreeImage_GetBPP(src);
    if (bpp == 24) {
        for (int x=0; x<256; x++) {
            LUT8[x] = ((BYTE *) LUT)[x];
        }
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < h; y++) {
            for(unsigned x = 0; x < w; x++) {
                BYTE * bdstpix = (BYTE *) (dstbits + dpitch*y + 3*x);
                BYTE * pixel   = (BYTE *) (srcbits + spitch*y + 3*x);

                int lumaorig = (int) LUMA(pixel[FI_RGBA_RED],pixel[FI_RGBA_GREEN],pixel[FI_RGBA_BLUE]);
                int diff = lumaorig - LUT8[lumaorig];

                bdstpix[FI_RGBA_RED]   = std::min(std::max(bdstpix[FI_RGBA_RED] + diff,0),255);
                bdstpix[FI_RGBA_GREEN] = std::min(std::max(bdstpix[FI_RGBA_GREEN] + diff,0),255);
                bdstpix[FI_RGBA_BLUE]  = std::min(std::max(bdstpix[FI_RGBA_BLUE]  + diff,0),255);

            }
        }
    }
    if (bpp == 48) {
        for (int x=0; x<65536; x++) {
            LUT16[x] = ((WORD *)LUT)[x];
        }
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < h-1; y++) {
            for(unsigned x = 0; x < w-1; x++) {
                FIRGB16 * wdstpix = (FIRGB16 *) (dstbits + dpitch*y + 6*x);
                FIRGB16 * pixel   = (FIRGB16 *) (srcbits + spitch*y + 6*x);

                int lumaorig = (int) LUMA(pixel->red,pixel->green,pixel->blue);
                int diff = lumaorig - LUT16[lumaorig];

                wdstpix->red   = std::min(std::max(pixel->red + diff,0),65535);
                wdstpix->green = std::min(std::max(pixel->green + diff,0),65535);
                wdstpix->blue  = std::min(std::max(pixel->blue + diff,0),65535);

            }
        }
    }
    return _duration();
}
Example #9
0
double ApplyGray(FIBITMAP *src, FIBITMAP *dst, double redpct, double greenpct, double bluepct, int threadcount)
{
    _mark();

    int bpp = FreeImage_GetBPP(src);

    unsigned spitch = FreeImage_GetPitch(src);
    unsigned dpitch = FreeImage_GetPitch(dst);
    BYTE * srcbits = FreeImage_GetBits(src);
    BYTE * dstbits = FreeImage_GetBits(dst);


    switch(bpp) {
    case 48:
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < FreeImage_GetHeight(src); y++) {
            for(unsigned x = 0; x < FreeImage_GetWidth(src); x++) {
                FIRGB16 * wdstpix = (FIRGB16 *) (dstbits + dpitch*y + 6*x);
                FIRGB16 * pixel   = (FIRGB16 *) (srcbits + spitch*y + 6*x);

                double G = floor((double) pixel->red*redpct + (double) pixel->green*greenpct + (double) pixel->blue*bluepct)+0.5;
                if (G>65535.0) G=65535.0;
                if (G<0.0) G=0.0;

                wdstpix->red = int(G);
                wdstpix->green = int(G);
                wdstpix->blue = int(G);
            }
        }
        break;
    case 24 :
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < FreeImage_GetHeight(src); y++) {
            for(unsigned x = 0; x < FreeImage_GetWidth(src); x++) {
                BYTE * bdstpix = (BYTE *) dstbits + dpitch*y + 3*x;
                BYTE *pixel = (BYTE *) (srcbits + spitch*y + 3*x);

                double G = floor((double) pixel[FI_RGBA_RED]*redpct + (double) pixel[FI_RGBA_GREEN]*greenpct + (double) pixel[FI_RGBA_BLUE]*bluepct)+0.5;
                if (G>255.0) G=255.0;
                if (G<0.0) G=0.0;

                bdstpix[FI_RGBA_RED] = int(G);
                bdstpix[FI_RGBA_GREEN] = int(G);
                bdstpix[FI_RGBA_BLUE]= int(G);

            }
        }
        break;
    }
    return _duration();

}
Example #10
0
File: plsub.c Project: EQ4/SPTK
int mark(int mrk, double ax[], double ay[], int n, double f, int m)
{
   int i;

   if (mrk < 0 || mrk > 15)
      return (1);
   m = (n < 0) ? m : 1;
   if ((n = abs(n)) < 1 || m < 0)
      return (1);
   ascale(_plnorm(f * 1.75));
   arotate(0);
   for (i = 0; i < n; i += m) {
      plot(ax[i], ay[i], 3);
      _mark(mrk);
   }
   return (0);
}
Example #11
0
size_t draft_net2_testsend(t_clock use_tdelta, t_netspeed_bps use_speed, size_t use_size, t_clock sim_length, int dbg) { 
	_mark("Test with use_tdelta="<<use_tdelta<<" use_speed="<<use_speed<<" use_size="<<use_size);
	
	t_netspeed_bps m_net_speed = use_speed;
	t_netsize_fraction m_net_reminder = 0; // bytes that remain partially accumulated from previous iterations
	
	size_t totall_sent=0;
	t_clock clock = 0;	 // world clock in simulation
	
	size_t c=0; // cycle number
	while (true) { // simulation
		++c;
		if (clock>=sim_length) break; // end
		
		t_clock tdelta = use_tdelta;
		clock += tdelta;
		size_t packet_size = use_size;
		
		t_netsize_fraction bytes_now_part = tdelta * m_net_speed ; // bytes we transfer just now 
		t_netsize_fraction bytes_now_all = bytes_now_part + m_net_reminder; // bytes we can now transfer with reminder
		
		t_netsize_fraction send_bytes = bytes_now_all;
		
		if (dbg) _info("=== clock="<<clock<<", cycle="<<c<<", m_net_reminder=" << m_net_reminder
		               << " bytes_now_all=" << bytes_now_all );
		while (send_bytes > packet_size) { // send a packet
			if (dbg) _info("*** SENDING at clock="<<clock<<", cycle="<<c<<", m_net_reminder=" << m_net_reminder);
			totall_sent += packet_size;
			send_bytes -= packet_size;
		}
		
		if (1) { // there is any more data waiting to be sent
			if (dbg) _info("Will send reminder to send_bytes="<<send_bytes);
			m_net_reminder = send_bytes; // "start" sending remaining data - in future
		}
		
		// TODO account for in progress the reminder of time in this cycle
	}
	return totall_sent;
}
Example #12
0
bool draft_net2_testsend_speeds(
		vector<t_clock> tab_tdelta, vector<t_netspeed_bps> tab_speed, vector<size_t> tab_size, int dbg
		,t_clock sim_length
	)
{ 
	_mark("Starting test " <<__FUNCTION__);
	
	vector<string> error_tab;
	
	std::ostringstream oss_main;
	for(auto use_tdelta : tab_tdelta) {
		for(auto use_speed : tab_speed) {
			for(auto use_size : tab_size) {
				size_t total = draft_net2_testsend(use_tdelta, use_speed, use_size, sim_length, dbg>=2);
				std::ostringstream oss;
				oss << "tdelta="<<use_tdelta<<" use_speed="<<use_speed<<" use_size="<<use_size<<" : ";
				double avg_speed = total / sim_length;
				double error = std::fabs( (avg_speed - use_speed) / use_speed );
				oss << " avg="<<avg_speed<<" bps, error="<<error;
				string msg = oss.str();
				
				if (error > 0.03) error_tab.push_back(msg);
				
				oss_main << msg << "\n";
				if (dbg >= 2) _info( msg );
			}
		}
	}
	_info("Sim results:\n" << oss_main.str());
	
	for (const auto & e : error_tab) _info("Error was: " << e);
	if (error_tab.size()) {
		_info("Found " << error_tab.size() << " errors."); 
		return false;
	} else _info("All in norm :)");
	return true;
}
Example #13
0
/// @brief This function tests the code from this file.
unique_ptr<c_world> draft_net2() { // the main function for test
/*	
	if (! draft_net2_testsend_alltests()) {
		_erro("Unit test failed!");
		return 0;
	}
	return 0; // !
*/	
    _mark("Starting test " <<__FUNCTION__);

    t_drawtarget_type drawtarget_type = e_drawtarget_type_allegro;
    c_simulation simulation(drawtarget_type);

    unique_ptr<c_world> world_ptr = make_unique<c_world>(simulation);
    c_world & world = * world_ptr;
	
	
	world.add_node("nodeA",100,100); // ***
	world.add_node("nodeB",150,200);
	world.add_node("nodeC1",300,300);
	world.add_node("nodeC2",300,350);
	world.add_node("nodeC3",300,400);
	world.add_node("nodeC4",300,450);
	world.add_node("nodeD", 50,200);
	world.add_node("nodeE",400,100); // ***

	{ int i=0, a=80, x0=500, y0=120;
	world.add_node("nodeF1",x0,y0+(a*i++));
	world.add_node("nodeF2",x0,y0+(a*i++));
	world.add_node("nodeF3",x0,y0+(a*i++));
	world.add_node("nodeF4",x0,y0+(a*i++));
	world.add_node("nodeF5",x0,y0+(a*i++));
	world.add_node("nodeF6",x0,y0+(a*i++));
	world.add_node("nodeF7",x0,y0+(a*i++));
	}

	world.add_node("nodeG1",650,100);
	world.add_node("nodeG2",650,300);

	world.add_node("nodeP",750,500);
	world.add_node("nodeX",800,500);
	
	world.add_osi2_switch("swA", 200,100);
	world.add_osi2_switch("swB", 300,100);
	world.add_osi2_switch("swC", 200,300);
	world.add_osi2_switch("swD", 300,200);


	
	_mark("Connecting devices");
	world.connect_network_devices("nodeA","swA", 1);
	world.connect_network_devices("nodeB","swA", 1);
	world.connect_network_devices("swA","swB", 1);
	world.connect_network_devices("swB","swD", 1);
	world.connect_network_devices("nodeA","nodeD", 1);
	world.connect_network_devices("swA","swC", 1);
	world.connect_network_devices("swC","nodeC1", 1);
	world.connect_network_devices("swC","nodeC2", 1);
	world.connect_network_devices("swC","nodeC3", 1);
	world.connect_network_devices("swC","nodeC4", 1);
	world.connect_network_devices("swD","nodeE", 1); // ***

	world.connect_network_devices("swD","nodeF1", 100);
	world.connect_network_devices("swD","nodeF2", 2);
	world.connect_network_devices("swD","nodeF3", 100);

	world.connect_network_devices("nodeF1","nodeF2", 1);
	world.connect_network_devices("nodeF2","nodeF3", 1);
	world.connect_network_devices("nodeF3","nodeF4", 1);
	world.connect_network_devices("nodeF4","nodeF5", 1);

	world.connect_network_devices("nodeF5","nodeG2", 1);
	world.connect_network_devices("nodeG2","nodeG1", 1);

	world.connect_network_devices("nodeG1","nodeP", 1);
	world.connect_network_devices("swD","nodeP", 50);
	world.connect_network_devices("nodeP","nodeX", 2);

	
	_mark("Testing - show object:");
	_info( world.find_object_by_name_as_switch("swA") );
	
	_mark("Testing - show object:");
	_info( world.find_object_by_name_as_switch("nodeA") );
	
	_mark("Find route:");
	t_osi2_route_result route = world.route_find_route_between(
		world.find_object_by_name_as_switch("nodeA"),
		world.find_object_by_name_as_switch("nodeE")
	);
	

	t_osi2_data data( std::string("HELLOWORLD") );
	/*
	_mark("Testing - send data:");
	world.find_object_by_name_as_switch("nodeA").send_data(
		world.find_object_by_name_as_switch("nodeE").get_uuid_any(), 
		data);
	*/
	
	_mark("Testing - show object:");
	_info( world.find_object_by_name_as_switch("swA") );

	c_dijkstry01 ( world.find_object_by_name_as_switch("nodeA") ,
			  world.find_object_by_name_as_switch("nodeX") );

#if 0
	world.connect_network_devices("","");
#endif
	
//	world.serialize(std::cout);
	
	
	/***
	 * 
	 * (outdated now - more elements are added)
	 * 
	 *  NodeA           SwitchA         SwitchB 
	 *   nic#0 -------> nic#0 --------> nic#0 ----------------> SwitchD 
	 *   nic#1 ---,     nic#1 --,       nic#1 ---------> NodeB  nic#0 -----> NodeE
	 *            |             |
	 *            |             |
	 *            |             |       SwitchC 
	 *            '--> NodeD    `-----> nic#0 -----> NodeC1
	 *                                  nic#1 -----> NodeC2
	 *                                  nic#2 -----> NodeC3
	 *                                  nic#3 -----> NodeC4
	 * 
	 * 
	 */
	
	
	return std::move(world_ptr);
}
Example #14
0
double ApplyNLMeans(FIBITMAP *src, FIBITMAP *dst, double sigma, int local, int patch, int threadcount)
{
    _mark();
    int bpp = FreeImage_GetBPP(src);

    unsigned spitch = FreeImage_GetPitch(src);
    unsigned dpitch = FreeImage_GetPitch(dst);
    BYTE * srcbits = FreeImage_GetBits(src);
    BYTE * dstbits = FreeImage_GetBits(dst);
    unsigned iw = FreeImage_GetWidth(src);
    unsigned ih = FreeImage_GetHeight(src);

    double sigma2 = pow(2*sigma,2);

    //y|x upper|lower bound computations, used to offset patch to avoid out-of-image references
    unsigned yplb = patch+local+1;
    unsigned ypub = ih-yplb;
    unsigned xplb = yplb;
    unsigned xpub = iw-xplb;

    switch(bpp) {
    case 48:
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = local; y < ih-local; y++) {
            unsigned py = y;
            if (py<yplb) py = yplb;
            if (py>ypub) py = ypub;
            for(unsigned x = local; x < iw-local; x++) {
                unsigned px = x;
                if (px<xplb) px = xplb;
                if (px>xpub) px = xpub;
                FIRGB16 *wdstpix = (FIRGB16 *) (dstbits + dpitch*y + 6*x);

                double valueR = 0.0;
                double valueG = 0.0;
                double valueB = 0.0;
                double sum_weightsR = 0.0;
                double sum_weightsG = 0.0;
                double sum_weightsB = 0.0;
                for (int q = -local; q<=local; ++q) {
                    for (int p = -local; p<=local; ++p) {
                        double diffR = 0.0;
                        double diffG = 0.0;
                        double diffB = 0.0;
                        for (int s = -patch; s<=patch; ++s) {
                            for (int r = -patch; r<=patch; ++r) {
                                FIRGB16 *ppix = (FIRGB16 *) (srcbits + spitch*(py+q+s) + 6*(px+p+r));
                                FIRGB16 *lpix = (FIRGB16 *) (srcbits + spitch*(py+s) + 6*(px+r));
                                diffR += pow((float) (ppix->red/256 - lpix->red/256),2);
                                diffG += pow((float) (ppix->green/256 - lpix->green/256),2);
                                diffB += pow((float) (ppix->blue/256 - lpix->blue/256),2);
                                //gmic: diff += pow(i[x+p+r,y+q+s] - i[x+r,y+s],2);

                            }
                        }
                        double weightR = exp(-diffR/sigma2);
                        double weightG = exp(-diffG/sigma2);
                        double weightB = exp(-diffB/sigma2);
                        FIRGB16 *localpix = (FIRGB16 *) (srcbits + spitch*(y+q) + 6*(x+p));
                        valueR += weightR*localpix->red/256;
                        valueG += weightG*localpix->green/256;
                        valueB += weightB*localpix->blue/256;
                        //gmic: value += weight*i(x+p,y+q);
                        sum_weightsR += weightR;
                        sum_weightsG += weightG;
                        sum_weightsB += weightB;

                    }
                }
                wdstpix->red   = int((valueR/(1e-5 + sum_weightsR))*256);
                wdstpix->green = int((valueG/(1e-5 + sum_weightsG))*256);
                wdstpix->blue  = int((valueB/(1e-5 + sum_weightsB))*256);
            }
        }
        break;
    case 24 :
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = local; y < ih-local; y++) {
            unsigned py = y;
            if (py<yplb) py = yplb;
            if (py>ypub) py = ypub;
            for(unsigned x = local; x < iw-local; x++) {
                unsigned px = x;
                if (px<xplb) px = xplb;
                if (px>xpub) px = xpub;
                BYTE *bdstpix = (BYTE *) (dstbits + dpitch*y + 3*x);

                double valueR = 0.0;
                double valueG = 0.0;
                double valueB = 0.0;
                double sum_weightsR = 0.0;
                double sum_weightsG = 0.0;
                double sum_weightsB = 0.0;

                for (int q = -local; q<=local; ++q) {
                    for (int p = -local; p<=local; ++p) {
                        double diffR = 0;
                        double diffG = 0;
                        double diffB = 0;
                        for (int s = -patch; s<=patch; ++s) {
                            for (int r = -patch; r<=patch; ++r) {
                                BYTE *ppix = (BYTE *) (srcbits + spitch*(py+q+s) + 3*(px+p+r));
                                BYTE *lpix = (BYTE *) (srcbits + spitch*(py+s) + 3*(px+r));
                                diffR += pow((float) (ppix[FI_RGBA_RED] - lpix[FI_RGBA_RED]),2);
                                diffG += pow((float) (ppix[FI_RGBA_GREEN] - lpix[FI_RGBA_GREEN]),2);
                                diffB += pow((float) (ppix[FI_RGBA_BLUE] - lpix[FI_RGBA_BLUE]),2);
                                //gmic: diff += pow(i[x+p+r,y+q+s] - i[x+r,y+s],2);
                            }
                        }
                        double weightR = exp(-diffR/sigma2);
                        double weightG = exp(-diffG/sigma2);
                        double weightB = exp(-diffB/sigma2);
                        BYTE *localpix = (BYTE *) (srcbits + spitch*(y+q) + 3*(x+p));
                        valueR += weightR*localpix[FI_RGBA_RED];
                        valueG += weightG*localpix[FI_RGBA_GREEN];
                        valueB += weightB*localpix[FI_RGBA_BLUE];
                        //gmic: value += weight*i(x+p,y+q);
                        sum_weightsR += weightR;
                        sum_weightsG += weightG;
                        sum_weightsB += weightB;
                    }
                }
                bdstpix[FI_RGBA_RED]   = (BYTE) (valueR/(1e-5 + sum_weightsR));
                bdstpix[FI_RGBA_GREEN] = (BYTE) (valueG/(1e-5 + sum_weightsG));
                bdstpix[FI_RGBA_BLUE]  = (BYTE) (valueB/(1e-5 + sum_weightsB));
            }
        }
        break;
    }
    return _duration();

}
Example #15
0
double ApplySaturation(FIBITMAP *src, FIBITMAP *dst, double saturate, int threadcount)
{
    _mark();

    int bpp = FreeImage_GetBPP(src);

    unsigned spitch = FreeImage_GetPitch(src);
    unsigned dpitch = FreeImage_GetPitch(dst);
    BYTE * srcbits = FreeImage_GetBits(src);
    BYTE * dstbits = FreeImage_GetBits(dst);


    switch(bpp) {
    case 48:
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < FreeImage_GetHeight(src); y++) {
            for(unsigned x = 0; x < FreeImage_GetWidth(src); x++) {
                FIRGB16 * wdstpix = (FIRGB16 *) (dstbits + dpitch*y + 6*x);
                FIRGB16 * pixel   = (FIRGB16 *) (srcbits + spitch*y + 6*x);

                double R = (double) pixel->red;
                double G = (double) pixel->green;
                double B = (double) pixel->blue;

                double  P=sqrt(
                              R*R*Pr+
                              G*G*Pg+
                              B*B*Pb ) ;

                R=P+(R-P)*saturate;
                G=P+(G-P)*saturate;
                B=P+(B-P)*saturate;

                if (R>65535.0) R=65535.0;
                if (G>65535.0) G=65535.0;
                if (B>65535.0) B=65535.0;
                if (R<0.0) R=0.0;
                if (G<0.0) G=0.0;
                if (B<0.0) B=0.0;
                wdstpix->red = int(R);
                wdstpix->green = int(G);
                wdstpix->blue = int(B);
            }
        }
        break;
    case 24 :
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 0; y < FreeImage_GetHeight(src); y++) {
            for(unsigned x = 0; x < FreeImage_GetWidth(src); x++) {
                BYTE * bdstpix = (BYTE *) dstbits + dpitch*y + 3*x;
                BYTE *pixel = (BYTE *) (srcbits + spitch*y + 3*x);

                double R = (double) pixel[FI_RGBA_RED];
                double G = (double) pixel[FI_RGBA_GREEN];
                double B = (double) pixel[FI_RGBA_BLUE];

                double  P=sqrt(
                              R*R*Pr+
                              G*G*Pg+
                              B*B*Pb ) ;

                R=P+(R-P)*saturate;
                G=P+(G-P)*saturate;
                B=P+(B-P)*saturate;

                if (R>255.0) R=255.0;
                if (G>255.0) G=255.0;
                if (B>255.0) B=255.0;
                if (R<0.0) R=0.0;
                if (G<0.0) G=0.0;
                if (B<0.0) B=0.0;
                bdstpix[FI_RGBA_RED] = int(R);
                bdstpix[FI_RGBA_GREEN] = int(G);
                bdstpix[FI_RGBA_BLUE]= int(B);
            }
        }
        break;
    }
    return _duration();

}
Example #16
0
// we assume we are already locked when inside here
// so we DO not call ENTER / LEAVE
int
DBFmalloc_mark(const char *func, const char *file, int line,
               ulong_t top, int todo)
{
    int			  rtn = 0;
    mse_t 		 *roots;
    int			  nroots;
    Block 		* bp;
    int			  nb;

    int i = 0;
    unsigned int start, end;
    int count=0;
    int cache = 0;

    extern int		  _malloc_scan_start();
    extern int		  _malloc_scan_finish();

    MALLOC_INIT();

    /*
     * Mark all the nodes as unreferenced at the start.
     * Make sure the malloc chain is valid or we will be in for trouble
     */
    if ((rtn = DBFmalloc_mark_all("DBFmalloc_mark", file, line, todo, 0)) != 0) {
        return rtn;
    }

    if ((roots = alloca(MAX_ROOTS * sizeof *roots)) == NULL) {
        return 0;
    }
    mc_cache_clear();
    cache = mc_get_cache_size();
    mc_set_cache(0);
    nroots = _malloc_scan_start(roots, MAX_ROOTS, top);

    _mark_stack_create();

    /*
     * We go through each root of the root set.
     * We never mark more than one page of a root at a time to avoid
     * having its unmarked children overflowing the mark stack.
     */

    start = roots[i].mse_start;
    end = roots[i].mse_end;
    if (end > start + __pagesize) end = start + __pagesize;

    while (i < nroots) {
        _mark_stack_push(start, end);

        count++;
        /* _mark will find every heap node reachable from the root
         * that was pushed onto the mark stack.
         */
        rtn += _mark(func, file, line, todo);
        start += __pagesize;
        if (start >= roots[i].mse_end) {
            i++;
            start = roots[i].mse_start;
            end = roots[i].mse_end;
            if (end > start + __pagesize) end = start + __pagesize;
        } else {
            end += __pagesize;
            if (end > roots[i].mse_end) end = roots[i].mse_end;
        }
    }
    _mark_stack_destroy();

//    /*
//     * Go through every Block in every Band and mark it
//     *  - this is because it's an internal structure and shouldn't be
//     *    reported as a leak
//     */
//    for (nb = 0; nb < *__pnband; nb++)
//    {
//	Band  *band;
//
//	band = __pBands[nb];
//
//	/*
//	 * For each Block on the allocated list
//	 */
//        for (bp = band->alist; bp; bp = bp->next)
//        {
//	    Dhead *dh = (Dhead *)bp - 1;
//            dh->d_debug.flag |= M_REFERENCED;
//        }
//
//	/*
//	 * For each Block on the depleted list
//	 */
//        for (bp = band->dlist; bp; bp = bp->next)
//        {
//	    Dhead *dh = (Dhead *)bp - 1;
//            dh->d_debug.flag |= M_REFERENCED;
//        }
//    }

    _malloc_scan_finish();
    mc_set_cache(cache);
    return rtn;
} /* malloc_mark(... */
Example #17
0
double ApplyKernel(FIBITMAP *src, FIBITMAP *dst, double kernel[3][3], int threadcount)
{
    _mark();

    int bpp = FreeImage_GetBPP(src);

    unsigned spitch = FreeImage_GetPitch(src);
    unsigned dpitch = FreeImage_GetPitch(dst);
    BYTE * srcbits = FreeImage_GetBits(src);
    BYTE * dstbits = FreeImage_GetBits(dst);

    switch(bpp) {
    case 48:
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 1; y < FreeImage_GetHeight(src)-1; y++) {
            for(unsigned x = 1; x < FreeImage_GetWidth(src)-1; x++) {
                FIRGB16 *wdstpix = (FIRGB16 *) (dstbits + dpitch*y + 6*x);
                double R=0.0;
                double G=0.0;
                double B=0.0;
                for (unsigned kx=0; kx<3; kx++) {
                    int ix=kx*3;
                    for (int ky=0; ky<3; ky++) {
                        int i = ix+ky;
                        FIRGB16 *pixel = (FIRGB16 *) (srcbits + spitch*(y-1+ky) + 6*(x-1+kx));
                        R += pixel->red   * kernel[kx][ky];
                        G += pixel->green * kernel[kx][ky];
                        B += pixel->blue  * kernel[kx][ky];
                    }
                    wdstpix->red   = std::min(std::max(int(R), 0), 65535);
                    wdstpix->green = std::min(std::max(int(G), 0), 65535);
                    wdstpix->blue  = std::min(std::max(int(B), 0), 65535);
                }

            }
        }
        break;
    case 24 :
        #pragma omp parallel for num_threads(threadcount)
        for(unsigned y = 1; y < FreeImage_GetHeight(src)-1; y++) {
            for(unsigned x = 1; x < FreeImage_GetWidth(src)-1; x++) {
                BYTE *bdstpix = (BYTE *) dstbits + dpitch*y + 3*x;
                double R=0.0;
                double G=0.0;
                double B=0.0;
                for (unsigned kx=0; kx<3; kx++) {
                    int ix=kx*3;
                    for (int ky=0; ky<3; ky++) {
                        int i = ix+ky;
                        BYTE *pixel = (BYTE *) srcbits + spitch*(y-1+ky) + 3*(x-1+kx);
                        R += pixel[FI_RGBA_RED]   * kernel[kx][ky];
                        G += pixel[FI_RGBA_GREEN] * kernel[kx][ky];
                        B += pixel[FI_RGBA_BLUE]  * kernel[kx][ky];
                    }
                    bdstpix[FI_RGBA_RED]   = std::min(std::max(int(R), 0), 255);
                    bdstpix[FI_RGBA_GREEN] = std::min(std::max(int(G), 0), 255);
                    bdstpix[FI_RGBA_BLUE]  = std::min(std::max(int(B), 0), 255);
                }
            }
        }
        break;
    }
    return _duration();
}