int main(void){
	cli();//disable interrupts
	/* Setup the 8mhz PWM clock 
	 * This will be on pin 11*/
	DDRB|=(1<<3);//pin 11
	ASSR &= ~(_BV(EXCLK) | _BV(AS2));
	TCCR2A=(1<<COM2A0)|(1<<WGM21)|(1<<WGM20);
	TCCR2B=(1<<WGM22)|(1<<CS20);
	OCR2A=0;//(F_CPU)/(2*(X+1))
	DDRC&=~15;//low d0-d3 camera
	DDRD&=~252;//d7-d4 and interrupt pins
	_delay_ms(3000);
	//set up twi for 100khz
	TWSR&=~3;//disable prescaler for TWI
	TWBR=72;//set to 100khz
	//enable serial
	UBRR0H=0;
	UBRR0L=1;//0 = 2M baud rate. 1 = 1M baud. 3 = 0.5M. 7 = 250k 207 is 9600 baud rate.
	UCSR0A|=2;//double speed aysnc
	UCSR0B = (1<<RXEN0)|(1<<TXEN0);//Enable receiver and transmitter
	UCSR0C=6;//async 1 stop bit 8bit char no parity bits
	camInit();
#ifdef useVga
	setRes(VGA);
	setColorSpace(BAYER_RGB);
	wrReg(0x11,25);
#elif defined(useQvga)
	setRes(QVGA);
	setColorSpace(YUV422);
	wrReg(0x11,12);
#else
	setRes(QQVGA);
	setColorSpace(YUV422);
	wrReg(0x11,3);
#endif
	/* If you are not sure what value to use here for the divider (register 0x11)
	 * Values I have found to work raw vga 25 qqvga yuv422 12 qvga yuv422 21
	 * run the commented out test below and pick the smallest value that gets a correct image */
	while (1){
		/* captureImg operates in bytes not pixels in some cases pixels are two bytes per pixel
		 * So for the width (if you were reading 640x480) you would put 1280 if you are reading yuv422 or rgb565 */
		/*uint8_t x=63;//Uncomment this block to test divider settings note the other line you need to uncomment
		  do{
		  wrReg(0x11,x);
		  _delay_ms(1000);*/
#ifdef useVga
		captureImg(640,480);
#elif defined(useQvga)
		captureImg(320*2,240);
#else
		captureImg(160*2,120);
#endif
		//}while(--x);//Uncomment this line to test divider settings
	}
}
// Get potential bounding boxes for all test images
void Objectness::getObjBndBoxesForTests(vector<vector<Vec4i>> &_boxesTests, int numDetPerSize)
{
	const int TestNum = _voc.testSet.size();
	vecM imgs3u(TestNum);
	vector<ValStructVec<float, Vec4i>> boxesTests;
	boxesTests.resize(TestNum);

#pragma omp parallel for
	for (int i = 0; i < TestNum; i++){
		//imgs3u[i] = imread(format(_S(_voc.imgPathW), _S(_voc.testSet[i])));
		imgs3u[i]  =  imread("C:/Users/TerryChen/Desktop/M.jpg");
		boxesTests[i].reserve(10000);
	}

	int scales[3] = {1, 3, 5};
	for (int clr = MAXBGR; clr <= G; clr++){
		setColorSpace(clr);
		trainObjectness(numDetPerSize);
		loadTrainedModel();
		CmTimer tm("Predict");
		tm.Start();

#pragma omp parallel for
		for (int i = 0; i < TestNum; i++){
			ValStructVec<float, Vec4i> boxes;
			getObjBndBoxes(imgs3u[i], boxes, numDetPerSize);
			boxesTests[i].append(boxes, scales[clr]);
		}

		tm.Stop();
		printf("Average time for predicting an image (%s) is %gs\n", _clrName[_Clr], tm.TimeInSeconds()/TestNum);
	}

	_boxesTests.resize(TestNum);
	CmFile::MkDir(_bbResDir);
#pragma omp parallel for
	for (int i = 0; i < TestNum; i++){
		CStr fName = _bbResDir + _voc.testSet[i];
		ValStructVec<float, Vec4i> &boxes = boxesTests[i];
		FILE *f = fopen(_S(fName + ".txt"), "w");
		fprintf(f, "%d\n", boxes.size());
		for (size_t k = 0; k < boxes.size(); k++)
			fprintf(f, "%g, %s\n", boxes(k), _S(strVec4i(boxes[k])));
		fclose(f);

		_boxesTests[i].resize(boxesTests[i].size());
		for (int j = 0; j < boxesTests[i].size(); j++)
			_boxesTests[i][j] = boxesTests[i][j];
	}

	evaluatePerImgRecall(_boxesTests, "PerImgAllNS.m", 5000);

#pragma omp parallel for
	for (int i = 0; i < TestNum; i++){
		boxesTests[i].sort(false);
		for (int j = 0; j < boxesTests[i].size(); j++)
			_boxesTests[i][j] = boxesTests[i][j];
	}
	evaluatePerImgRecall(_boxesTests, "PerImgAllS.m", 5000);
}
void KisColorSelector::loadSettings()
{
    KisConfig cfg;
    setColorSpace(KisColor::Type(cfg.readEntry<qint32>("ArtColorSel.ColorSpace" , KisColor::HSY)));
    
    setNumLightPieces(cfg.readEntry("ArtColorSel.LightPieces", 19));
    
    m_selectedColor.setH(cfg.readEntry<float>("ArtColorSel.SelColorH", 0.0f));
    m_selectedColor.setS(cfg.readEntry<float>("ArtColorSel.SelColorS", 0.0f));
    m_selectedColor.setX(cfg.readEntry<float>("ArtColorSel.SelColorX", 0.0f));
    m_selectedColor.setA(1.0f);
    
    setInverseSaturation(cfg.readEntry<bool>("ArtColorSel.InversedSaturation", false));
    setLight(cfg.readEntry<float>("ArtColorSel.Light", 0.5f), cfg.readEntry<bool>("ArtColorSel.RelativeLight", false));
    
    recalculateRings(
                cfg.readEntry("ArtColorSel.NumRings"  , 11),
                cfg.readEntry("ArtColorSel.RingPieces", 12)
                );
    
    QList<float> angles = cfg.readList<float>("ArtColorSel.RingAngles");

    for (int i = 0; i < m_colorRings.size(); ++i) {
        if (i < angles.size() && i < m_colorRings.size()) {
            m_colorRings[i].angle = angles[i];
        }
    }
    
    selectColor(m_selectedColor);
    update();
}
// base for window size quantization, R orientation channels, and feature window size (_W, _W)
Objectness::Objectness(DataSetVOC &voc, double base, int W, int NSS)
    : _voc(voc)
    , _base(base)
    , _W(W)
    , _NSS(NSS)
    , _logBase(log(_base))
    , _minT(cvCeil(log(10.)/_logBase))
    , _maxT(cvCeil(log(500.)/_logBase))
    , _numT(_maxT - _minT + 1)
    , _Clr(MAXBGR)
{
    setColorSpace(_Clr);
}
int midParseInt(int identifier,int fd, char *data, int len)
{
	int recvlen;
	int cmd = 0;
	int actdata = 0;
	//char logoname[15] = "logo.png";
	int ret = 0;
	int web_ret = SERVER_RET_OK;

	int need_send = 0;


	char senddata[1024] = {0};
	int totallen = 0;

	recvlen = recv(fd, data, len, 0);
	int out = 0;

//	if(recvlen < 0) {
//		return -1;
//	}

	if(recvlen < 0 || len != sizeof(cmd) + sizeof(int)) {
		web_ret = SERVER_RET_INVAID_PARM_LEN;
		need_send = 1;
		goto EXIT;
	}


	memcpy(&cmd, data, sizeof(int));
	memcpy(&actdata, data + sizeof(int), len - sizeof(int));

	PRINTF("cmd = 0x%04x\n", cmd);

	switch(cmd) {
		case MSG_GETINPUTTYPE:
			PRINTF("cmd = 0x%04x is MSG_GETINPUTTYPE.\n", cmd);
			ret = getinputType(actdata, &out);
			need_send = 1;
			break;

		case MSG_SETINPUTTYPE:
			PRINTF("cmd = 0x%04x is MSG_SETINPUTTYPE.\n", cmd);
			ret = setInputType(actdata, &out);
			need_send = 1;
			break;

			//case MSG_STOP_MULT:
			//	stopMultParse(fd, cmd, &actdata);
			//	break;

		case MSG_GETCPULOAD:
			PRINTF("cmd = 0x%04x is MSG_GETCPULOAD.\n", cmd);
			ret = getcpuload(actdata, &out);
			need_send = 1;
			break;

		case MSG_RESTORESET:
			PRINTF("cmd = 0x%04x is MSG_RESTORESET.\n", cmd);
			ret = restoresetparse(actdata, &out);
			need_send = 1;
			break;

		case MSG_GET_CTRL_PROTO:
			PRINTF("cmd = 0x%04x is MSG_GET_CTRL_PROTO.\n", cmd);
			need_send = 1;
			getCtrlProto(actdata, &out);
			break;

		case MSG_SET_CTRL_PROTO:
			PRINTF("cmd = 0x%04x is MSG_SET_CTRL_PROTO.\n", cmd);
			need_send = 1;
			setCtrlProto(actdata, &out);
			break;

			/*
			case MSG_SAVEPARAM:
				saveParams(fd, cmd, &actdata);
				break;
			*/
#if 0

		case MSG_GETDHCPFLAG:
			PRINTF("cmd = 0x%04x is MSG_GETDHCPFLAG.\n", cmd);
			need_send = 1;
			PRINTF("MSG_GETDHCPFLAG actdata = %d\n", actdata);
			webgetDHCPFlag(actdata, &out);
			break;

		case MSG_SETDHCPFLAG:
			PRINTF("cmd = 0x%04x is MSG_SETDHCPFLAG.\n", cmd);
			need_send = 1;
			websetDHCPFlag(actdata, &out);
			break;
#endif

		case MSG_SETCOLORSPACE:
			PRINTF("cmd = 0x%04x is MSG_SETCOLORSPACE.\n", cmd);
			need_send = 1;
			ret = setColorSpace(actdata, &out);
			break;

		case MSG_GETCOLORSPACE:
			PRINTF("cmd = 0x%04x is MSG_GETCOLORSPACE.\n", cmd);
			getColorSpace(&out);
			need_send = 1;
			break;

			//case MSG_GETHDCPVAL:
			//	getHDCPValue(fd, cmd, &actdata);
			//	break;

		case MSG_REBOOTSYS:
			PRINTF("cmd = 0x%04x is MSG_REBOOTSYS.\n", cmd);
			need_send = 1;
			rebootSys(actdata, &out);
			break;

		case MSG_GETHDCPVALUE:
			PRINTF("cmd = 0x%04x is MSG_GETHDCPVALUE.\n", cmd);
			need_send = 1;
			getHDCPValue(actdata, &out);
			break;

		case MSG_LOCK_RESOLUTION:
			PRINTF("cmd = 0x%04x is MSG_LOCK_RESOLUTION.\n", cmd);
			ret = DoLockResolution(actdata, &out);
			need_send = 1;
			break;

		case MSG_GETOUTPUTRESOLUTION:
			PRINTF("cmd = 0x%04x is MSG_GETOUTPUTRESOLUTION.\n", cmd);
			ret = webgetOutputResolution(actdata, &out);
			need_send = 1;
			break;

		case MSG_RESIZE_MODE:
			PRINTF("cmd = 0x%04x is MSG_RESIZE_MODE.\n", cmd);
			//	setResizeMode(fd, cmd, &actdata);
			ret = setResizeMode(actdata, &out);
			need_send = 1;
			break;

		case MSG_GETRESIZEMODE:
			PRINTF("cmd = 0x%04x is MSG_GETRESIZEMODE.\n", cmd);
			//getResizeMode(fd, cmd, &actdata);
			ret = getResizeMode(actdata, &out);
			need_send = 1;
			break;

		case MSG_GETENCODELV:
			PRINTF("cmd = 0x%04x is MSG_GETENCODELV.\n", cmd);
			ret = getEncodelevel(actdata, &out);

			need_send = 1;
			break;

		case MSG_SETENCODELV:
			PRINTF("cmd = 0x%04x is MSG_SETENCODELV.\n", cmd);
			ret = setEncodelevel(actdata, &out);

			need_send = 1;
			break;

		case MSG_GETSCENE:
			PRINTF("cmd = 0x%04x is MSG_GETSCENE.\n", cmd);
			need_send = 1;
			getSceneconfig(actdata, &out);
			break;

		case MSG_SETSCENE:
			PRINTF("cmd = 0x%04x is MSG_SETSCENE.\n", cmd);
			need_send = 1;
			ret = setSceneconfig(actdata, &out);
			break;

		case MSG_GETFRAMERATE:
			PRINTF("cmd = 0x%04x is MSG_GETFRAMERATE.\n", cmd);
			need_send = 1;
			getFrameRate(actdata, &out);
			break;

		case MSG_SETFRAMERATE:
			PRINTF("cmd = 0x%04x is MSG_SETFRAMERATE.\n", cmd);
			need_send = 1;
			ret = setFrameRate(actdata, &out);
			break;

		case MSG_GETIFRAMEINTERVAL:
			PRINTF("cmd = 0x%04x is MSG_GETIFRAMEINTERVAL.\n", cmd);
			need_send = 1;
			getIFrameInterval(actdata, &out);
			break;

			//case MSG_SETIFRAMEINTERVAL:
			//	PRINTF("cmd = 0x%04x is MSG_SETIFRAMEINTERVAL.\n", cmd);
			//	need_send = 1;
			//	ret = setIFrameInterval(actdata, &out);
			//	break;

		case MSG_GETBITRATE:
			PRINTF("cmd = 0x%04x is MSG_GETBITRATE.\n", cmd);
			need_send = 1;
			getBitRate(actdata, &out);
			break;

		case MSG_SETBITRATE:
			PRINTF("cmd = 0x%04x is MSG_SETBITRATE.\n", cmd);
			need_send = 1;
			ret = setBitRate(actdata, &out);
			break;


		case MSG_SETMUTE:
			PRINTF("cmd = 0x%04x is MSG_SETMUTE.\n", cmd);
			need_send = 1;
			ret = webSetMute(actdata, &out);
			break;

		case MSG_GETMUTE:
			PRINTF("cmd = 0x%04x is MSG_GETMUTE.\n", cmd);
			webGetMute(actdata, &out);
			need_send = 1;
			break;

			//stream_ouput
		case MSG_RTSP_GET_USED:
		case MSG_RTSP_DEL_SERVER:
		case MSG_MULT_GET_NUM:
		case MSG_MULT_GET_TS_RATE:
		case MSG_MULT_GET_RTP_RATE:
		case MSG_MULT_GET_RTPTS_RATE:
			app_web_stream_output_process_int(cmd, actdata, &out);
			need_send = 1;
			break;

			//case MSG_GETSHOWTEXTLOGO:
			//	PRINTF("cmd = 0x%04x is MSG_GETSHOWTEXTLOGO.\n", cmd);
			//	webgetShowLogoTextFlag(actdata, &out);
			//	need_send = 1;
			//	break;

			//	case MSG_SETSHOWTEXTLOGO:
			//		PRINTF("cmd = 0x%04x is MSG_SETSHOWTEXTLOGO.\n", cmd);
			//		ret = websetShowLogoTextFlag(actdata, &out);
			//		need_send = 1;
			//		break;
			//		case MSG_GET_SHOWTEXT_FLAG:
			//			out = app_get_textshow_flag();
			//			need_send = 1;
			//			break;
			//		case MSG_SET_SHOWTEXT_FLAG:
			//			out = app_set_textshow_flag(actdata);
			//			need_send = 1;
			//			break;
			//		case MSG_GET_SHOWLOGO_FLAG:
			//			out = app_get_logoshow_flag();
			//			need_send = 1;
			//			break;
			//		case MSG_SET_SHOWLOGO_FLAG:
			//			out = app_set_logoshow_flag(actdata);
			//			need_send = 1;
			//			break;

		default:
			PRINTF("unkonwn cmd = %04x\n", cmd);
			need_send = 1;
			web_ret = SERVER_RET_UNKOWN_CMD;
			break;
			//	case
	}

	if(ret < 0) {
		web_ret = SERVER_RET_INVAID_PARM_VALUE;
	}

EXIT:

	if(need_send == 1) {
		totallen = MSGINFOHEAD + sizeof(cmd) + sizeof(web_ret) + sizeof(out);
		msgPacket(identifier,senddata, INT_TYPE, totallen, cmd, web_ret);
		memcpy(senddata + (totallen - sizeof(out)), &out, sizeof(out));
		PRINTF("the cmd =%04x,the value=%d,the ret=%04x\n", cmd, out, web_ret);
		send(fd, senddata, totallen, 0);
	}

	return 0;
}