Beispiel #1
0
int main(void)
{
	init_MCU();
	LED_R_ON;

	speed(0,0);
	while(1)
	{
		ctrrobot();
	}
}
Beispiel #2
0
//
// Function : MAIN                                               
//
int main(void) { 

     init_MCU();

     // Main Loop

     for(;;) {
	     
     // Computer Starts
     
     initgame();
         
         for(;;) { 
	    
         getcomputurn();
         
		 // Wait to give the impression it is taking some time to think.
		 wait(150); 
		 
		 // Load display buffer
         display_red=play_red; 
         display_rg_ex = play_rg_ex;
     
         if (check()) break;
		 
         gethumanturn();
         
         if (check()) break; 
         } 

     // Human Starts
     
     initgame();

         for(;;) { 
	      
         gethumanturn();
         
         if (check()) break;
	     
	     getcomputurn();
	     
	     // Wait to give the impression it is taking some time to think.
		 wait(150); 
		 
         display_red=play_red; // Load display buffer
         display_rg_ex = play_rg_ex;
         
         if (check()) break;
	     
		}
     }
}
int main()
{
	init_MCU();
	sel_mode();
	
	while (1)
	{
		switch (pattern)
		{
			case 1:
			if ( check_crossline() ) //Be 90
			{
				pattern = 21;
				break;
			}
			if ( check_rightline() ) // Chuyen lan phai
			{
				pattern = 51;
				break;
			}
			if ( check_leftline() ) // Chuyen lan trai
			{
				pattern = 61;
				break;
			}
			led7(10);
			set_encoder(19);
			switch (doc_cam_bien(0b01111110))
			{
				case 0b01111110:
				toc_do(0, 0);
				goc(0);
				break;
				case 0b00011000:	// Chay thang
				goc( 0 );
				toc_do( 100 , 100 );
				break;

				//lech phai
				case 0b00011100:
				case 0b00001000:
				toc_do(90, 80);
				goc(8);
				break;
				case 0b00001100:
				toc_do(90, 70);
				goc(10);
				break;
				case 0b00001110:
				case 0b00000100:
				toc_do(90, 60);
				goc(12);
				break;
				case 0b00000110:
				toc_do(90, 50);
				goc(15);
				break;
				case 0b00000010:
				toc_do(90, 30);
				goc(20);
				pattern = 11;	//Lệch phải góc lớn
				break;

				//////////////////////////////////////////

				//lech trai
				case 0b00111000:
				case 0b00010000:
				toc_do(80, 90);
				goc(-8);
				break;
				case 0b00110000:
				toc_do(70, 90);
				goc(-10);
				break;
				case 0b01110000:
				case 0b00100000:
				toc_do(60, 90);
				goc(-12);
				break;
				case 0b01100000:
				toc_do(50, 90);
				goc(-15);
				break;
				case 0b01000000:
				toc_do(30, 90);
				goc(-20);
				pattern = 12; //Lệch trái góc lớn
				break;
				default:
				break;
			}
			break;

			case 11: 	//Lệch phải góc lớn
			led7(11);
			set_encoder(23);
			switch (doc_cam_bien(0b11001100))
			{
				case 0b11000000:
				toc_do(95, 15);
				goc(75);
				break;
				case 0b10000000:
				toc_do(95, 20);
				goc(51);
				break;
				case 0b00000000:
				toc_do(95, 25);
				goc(48);
				break;
				case 0b00000100:
				toc_do(95, 25);
				goc(37);
				break;
				case 0b00001100:
				toc_do(95, 25);
				goc(29);
				pattern = 1;
				break;
				default: pattern = 11;
			}
			break;
			case 12: //Lệch trái góc lớn
			led7(12);
			set_encoder(23);
			switch (doc_cam_bien(0b00110011))
			{
				case 0b00000011:
				toc_do(15, 95);
				goc(-73);
				break;
				case 0b00000001:
				toc_do(20, 95);
				goc(-50);
				break;
				case 0b00000000:
				toc_do(25, 95);
				goc(-47);
				break;
				case 0b00100000:
				toc_do(25, 95);
				goc(-36);
				break;
				case 0b00110000:
				toc_do(25, 95);
				goc(-28);
				pattern = 1;
				break;
				default: pattern = 12;
			}
			break;

			case 21:		//Xử lý khi gặp vạch tín hiệu cua vuông đầu tiên
			led7(18);
			set_encoder(20);
			goc( 0 );
			toc_do( 60 , 60);
			pattern = 22;
			cnt_1ms = 0;
			xung = 0;
			break;

			case 22:		// Delay hết vạch
			led7(22);
			set_encoder(20);
			goc( 0 );
			toc_do( 60 , 60);
			if ( cnt_1ms > 150 || xung > 10 )
			{
				pattern = 23;
				cnt_1ms = 0;
				xung = 0;
			}
			break;

			case 23:
			led7(23);
			//cua trái
			if ( (xung > 100) && ((doc_cam_bien(0b11111111) == 0b11111000)  || (doc_cam_bien(0b11111111) == 0b11110000) || (doc_cam_bien(0b11111111) == 0b11100000) || (doc_cam_bien(0b11111111) == 0b11111100)))	// Neu gap tin hieu nay la goc cua 90 trai thi be
			{
				pattern = 24;
				xung = 0;
				cnt_1ms = 0;
				break;
			}
			//cua phải
			if (  (xung > 100) &&   ((doc_cam_bien(0b11111111) == 0b00011111 ) || (doc_cam_bien(0b11111111) == 0b00000111) || (doc_cam_bien(0b11111111) == 0b00001111) || (doc_cam_bien(0b11111111) == 0b00111111))) // Neu gap tin hieu nay la goc cua 90 phai thi be
			{
				pattern = 25;
				xung = 0;
				cnt_1ms = 0;
				break;
			}
			if ( (xung > 30) && (xung < 100) && check_crossline() == 1)
			{
				pattern = 71;
				xung = 0;
				cnt_1ms = 0;
				break;
			}
			// Nguoc lai thi chinh thang cho xe
			switch (doc_cam_bien(0b01111110))
			{
				case 0b00011000:
				goc( 0 );
				toc_do(30, 30 );
				break;
				//lech phai
				case 0b00001000:
				toc_do(23, 3);
				goc(8);
				break;
				case 0b00001100:
				toc_do(23, -3);
				goc(10);
				break;
				case 0b00000100:
				toc_do(23, -8);
				goc(13);
				break;
				case 0b00000110:
				case 0b00000010:
				toc_do(23, -13);
				goc(18);
				break;
				////////////////////////////////
				case 0b00010000:
				toc_do(5, 20);
				goc(-8);
				break;
				case 0b00110000:
				toc_do(0, 20);
				goc(-10);
				break;
				case 0b00100000:
				toc_do(-5, 20);
				goc(-13);
				break;
				case 0b01100000:
				case 0b01000000:
				toc_do(10, 20);
				goc(-18);
				break;
				default:
				break;
			}
			break;

			case 24://delay het vach line TRAI
			set_encoder(10);
			toc_do(50, 50);
			if ( (xung > 100) && ((doc_cam_bien(0b00000111) == 0b00000111) || (doc_cam_bien(0b00001111) == 0b00001111)))
			{
				pattern = 23 ;
				break;
			}
			if (cnt_1ms > 100 || xung > 1)
			{
				pattern = 26;
				break;
			}
			break;

			case 25:	//delay het vach line PHAI
			set_encoder(10);
			toc_do(50, 50);
			if ( (xung > 100 ) && ((doc_cam_bien(0b11100000) == 0b11100000) || (doc_cam_bien(0b11110000) == 0b11110000)))
			{
				pattern = 23 ;
				break;
			}
			if (cnt_1ms > 100 || xung > 1)
			{
				pattern = 27;
				break;
			}
			break;

			case 26: //vuông trái
			led7(24);
			goc( - 140 );
			set_encoder(8);
			toc_do( -20 , 90 );
			led7(1);
			pattern = 31;
			cnt_1ms = 0;
			break;

			case 27://vuông phải
			led7(25);
			goc( 140 );
			set_encoder(8);
			toc_do( 90 , -20 );
			led7(2);
			pattern = 41;
			cnt_1ms = 0;
			break;


			case 31:	//delay cho xe kịp bẻ vuông trái
			led7(31);
			if ( cnt_1ms > 50 )
			{
				pattern = 32;
				cnt_1ms = 0;
			}
			break;

			case 32:	//chờ tín hiệu để về trường hợp chạy thẳng
			led7(32);
			if ( doc_cam_bien(0b11100111) == 0b00100000 )
			{
				pattern = 1;
				cnt_1ms = 0;
			}
			break;

			case 41: //delay cho xe kịp bẻ vuông phải
			led7(41);
			if ( cnt_1ms > 50 )
			{
				pattern = 42;
				cnt_1ms = 0;
			}
			break;

			case 42: //chờ tín hiệu để về trường hợp chạy thẳng
			led7(42);
			if ( doc_cam_bien(0b11100111) == 0b00000100 )
			{
				pattern = 1;
				cnt_1ms = 0;
			}
			break;

			case 51: //xử lý khi gặp vạch tín hiệu chuyển làn PHẢI
			set_encoder(20);
			if ((doc_cam_bien(0b11100000) == 0b11100000) || (doc_cam_bien(0b11110000) == 0b11110000))
			{
				pattern = 21 ;
				break;
			}
			led7(51);
			goc( 0 );
			toc_do( 70 , 70 );
			pattern = 52;
			cnt_1ms = 0;
			xung = 0;
			break;

			case 52: //delay hết vạch
			if ( cnt_1ms > 100 || xung > 10)
			{
				pattern = 53;
				cnt_1ms = 0;
			}
			if ((doc_cam_bien(0b11100000) == 0b11100000) || (doc_cam_bien(0b11110000) == 0b11110000))
			{
				pattern = 21 ;
				break;
			}
			break;

			case 53: //hết vạch trắng
			if ( doc_cam_bien(0b11111111) == 0b00000000 )
			{
				goc( 75 );
				toc_do( 60 , 50 );
				pattern = 54;
				cnt_1ms = 0;
				break;
			}
			switch ( doc_cam_bien(0b11100111) )
			{
				case 0x00:
				goc( 0 );
				toc_do( 30 , 30 );
				break;
				case 0x04:
				case 0x06:
				case 0x07:
				case 0x03:
				goc( 15 );
				toc_do( 30 , 20 );
				break;
				case 0x20:
				case 0x60:
				case 0xe0:
				case 0xc0:
				goc( -15 );
				toc_do( 20 , 30 );
				break;
				default:
				break;
			}
			break;

			case 54: //bắt được line, chuyển về chạy thẳng
			if ( doc_cam_bien( 0b00001100 ) == 0b00001100 )
			{
				pattern = 1;
				cnt_1ms = 0;
				led7(0);
			}
			break;

			case 61:	//xử lý khi gặp vạch tín hiệu chuyển làn TRÁI
			set_encoder(20);
			if ( (doc_cam_bien(0b00000111) == 0b00000111) || (doc_cam_bien(0b00001111) == 0b00001111) )
			{
				pattern = 21 ;
				break;
			}
			led7(61);
			goc( 0 );
			toc_do( 90 , 90 );
			pattern = 62;
			cnt_1ms = 0;
			xung = 0;
			break;

			case 62: //delay hết vạch
			if ( cnt_1ms > 100 || xung > 10)
			{
				pattern = 63;
				cnt_1ms = 0;
			}
			if ((doc_cam_bien(0b00000111) == 0b00000111) || (doc_cam_bien(0b00001111) == 0b00001111))
			{
				pattern = 21 ;
				break;
			}
			break;

			case 63: //hết vạch trắng
			if ( doc_cam_bien(0b11111111) == 0x00 )
			{
				goc( -75 );
				toc_do( 50 , 60 );
				pattern = 64;
				cnt_1ms = 0;
				break;
			}
			switch ( doc_cam_bien(0b11100111) )
			{
				case 0x00:
				goc( 0 );
				toc_do( 30 , 30 );
				break;
				case 0x04:
				case 0x06:
				case 0x07:
				case 0x03:
				goc( 15 );
				toc_do( 30 , 20 );
				break;
				case 0x20:
				case 0x60:
				case 0xe0:
				case 0xc0:
				goc( -15 );
				toc_do( 20 , 30 );
				break;
				default:
				break;
			}
			break;

			case 64: //bắt được line, chuyển về chạy thẳng
			if ( doc_cam_bien( 0b00110000 ) == 0b00110000 )
			{
				pattern = 1;
				cnt_1ms = 0;
				led7(0);
			}
			break;

			case 71: // Delay het vach
			set_encoder(30);
			toc_do(100, 100);
			led7(71);
			if ( cnt_1ms > 100 || xung > 10)
			{
				pattern = 72;
				cnt_1ms = 0;
			}
			break;

			case 72:
			if (doc_cam_bien(0xff) == 0b00000000)
			{
				goc(0);
				toc_do(100, 100);
				pattern = 73;
				break;
			}

			switch (doc_cam_bien(0b01111110))
			{
				case 0b00011000:
				goc( 0 );
				toc_do(100, 100 );
				break;
				//lech phai
				case 0b00001000:
				toc_do(90, 17);
				goc(8);
				break;
				case 0b00001100:
				toc_do(90, 70);
				goc(13);
				break;
				case 0b00000100:
				toc_do(90, 65);
				goc(18);
				break;
				case 0b00000110:
				case 0b00000010:
				toc_do(90, 60);
				goc(26);
				break;
				//////////////////////////////////////////////////////////////////////////
				case 0b00010000:
				toc_do(75, 90);
				goc(-8);
				break;
				case 0b00110000:
				toc_do(70, 90);
				goc(-13);
				break;
				case 0b00100000:
				toc_do(65, 90);
				goc(-18);
				break;
				case 0b01100000:
				case 0b01000000:
				toc_do(60, 90);
				goc(-26);
				break;
				default:
				break;
			}
			break;
			case 73:
			goc(0);
			toc_do(100, 100);
			if (doc_cam_bien(0b11111111) > 0)
			{
				pattern = 1;
			}
			break;

			default:
			pattern = 1;
			break;
		}
	}

}
Beispiel #4
0
int JpegToBmp()
{
	unsigned int aux, mark;
	int n_restarts, restart_interval, leftover;	/* RST check */
	int i, j;
	int turn;
	int temp;
	int xmsize;
	/* First find the SOI marker: */
	//mk_mon_debug_info(9999);
	aux = get_next_MK();
	if (aux != SOI_MK)
		aborted_stream(0);
		

	//if (verbose)
		//fprintf(stderr, "%ld:\tINFO:\tFound the SOI marker!\n", ftell(fi));
		//;
	in_frame = 0;
	restart_interval = 0;
	for (i = 0; i < 4; i++)
		QTvalid[i] = 0;

	/* Now process segments as they appear: */
	do {
		mark = get_next_MK();

		switch (mark) {
		case SOF_MK:  //ffc0 start of the frame
			//if (verbose);
				//fprintf(stderr, "%ld:\tINFO:\tFound the SOF marker!\n", ftell(fi));
			in_frame = 1;
			//get_size(fi);	/* header size, don't care */
			get_size();   //0011 17

			/* load basic image parameters */
			//fgetc(fi);	/* precision, 8bit, don't care */
			FGETC();   //8
			
			y_size = get_size();//FGETC() twice
			x_size = get_size();
			mk_mon_debug_info(x_size);
			mk_mon_debug_info(y_size);
			//if (verbose);
				//fprintf(stderr, "\tINFO:\tImage size is %d by %d\n", x_size, y_size);

			//n_comp = fgetc(fi);	/* # of components */
			n_comp = FGETC();
			mk_mon_debug_info(123456);
			mk_mon_debug_info(n_comp);
			mk_mon_debug_info(654321);
			//if (1) {
				//fprintf(stderr, "\tINFO:\t");
				////switch (n_comp) {
				//case 1:
					//printf( "Monochrome\n");
					//break;
				//case 3:
					//printf( "Color\n");
				//	break;
				////default:
				//	printf( "Not a picture!\n");
					//break;
				//}
				//fprintf(stderr, " JPEG image!\n");
			//}

			for (i = 0; i < n_comp; i++) {
				/* component specifiers */
				//comp[i].CID = fgetc(fi);
				comp[i].CID = FGETC();
				//aux = fgetc(fi);
				aux = FGETC();
				comp[i].HS = first_quad(aux); //0x11 >> 4   1
				comp[i].VS = second_quad(aux);  //&15       1
				//comp[i].QT = fgetc(fi);
				comp[i].QT = FGETC();
			}
			//if ((n_comp > 1) && verbose);
				/*fprintf(stderr,
					"\tINFO:\tColor format is %d:%d:%d, H=%d\n",
					comp[0].HS * comp[0].VS, comp[1].HS * comp[1].VS, comp[2].HS * comp[2].VS,
					comp[1].HS);*/
					

			if (init_MCU() == -1)
				aborted_stream(1);
				

			/* dimension scan buffer for YUV->RGB conversion */
			//FrameBuffer = (unsigned char *)mk_malloc((size_t) x_size * y_size * n_comp);
			ColorBuffer = (unsigned char *)mk_malloc((size_t) MCU_sx * MCU_sy * n_comp);
			FBuff = (FBlock *) mk_malloc(sizeof(FBlock));
			PBuff = (PBlock *) mk_malloc(sizeof(PBlock));

			if (  (ColorBuffer == NULL) || (FBuff == NULL) || (PBuff == NULL)) {
				//fprintf(stderr, "\tERROR:\tCould not allocate pixel storage!\n");
				aborted_stream(2);
			}
			break;

		case DHT_MK:
			//if (verbose)
				//fprintf(stderr, "%ld:\tINFO:\tDefining Huffman Tables\n", ftell(fi));
				
			if (load_huff_tables() == -1)
				aborted_stream(2);
				
			break;

		case DQT_MK:  //FFDB, the following 0084 shows the table length 132 
			//if (verbose)
				//fprintf(stderr, "%ld:\tINFO:\tDefining Quantization Tables\n", ftell(fi));
				
			if (load_quant_tables() == -1)
				aborted_stream(3);
				
			break;

		case DRI_MK:
			get_size();	/* skip size */
			restart_interval = get_size();
			mk_mon_debug_info(00000000);
			mk_mon_debug_info(restart_interval);
			mk_mon_debug_info(00000000);
			//if (verbose)
				//fprintf(stderr, "%ld:\tINFO:\tDefining Restart Interval %d\n", ftell(fi),
					//restart_interval);
					//;
			break;

		case SOS_MK:	/* lots of things to do here */ //ffda
			//if (verbose);
				//fprintf(stderr, "%ld:\tINFO:\tFound the SOS marker!\n", ftell(fi));
			get_size();	/* don't care */
			//aux = fgetc(fi);
			aux = FGETC();  //03
			if (aux != (unsigned int)n_comp) {
				//fprintf(stderr, "\tERROR:\tBad component interleaving!\n");
				aborted_stream(4);
			
			}

			for (i = 0; i < n_comp; i++) {
				//aux = fgetc(fi);
				aux = FGETC();
				if (aux != comp[i].CID) {
					//fprintf(stderr, "\tERROR:\tBad Component Order!\n");
					aborted_stream(5);
					
				}
				//aux = fgetc(fi);
				aux = FGETC();
				comp[i].DC_HT = first_quad(aux);
				comp[i].AC_HT = second_quad(aux);
			}
			get_size();
			//fgetc(fi);	/* skip things */
			FGETC();
			MCU_column = 0;
			MCU_row = 0;
			clear_bits();
			reset_prediction();

			/* main MCU processing loop here */
			if (restart_interval) {
				n_restarts = ceil_div(mx_size * my_size, restart_interval) - 1;
				leftover = mx_size * my_size - n_restarts * restart_interval;
				/* final interval may be incomplete */

				for (i = 0; i < n_restarts; i++) {
					//temp = restart_interval*i;
					for (j = 0; j < restart_interval; j++){
						//turn = (temp+j) & 0x3;
						process_MCU();}
					/* proc till all EOB met */

					aux = get_next_MK();
					if (!RST_MK(aux)) {
						//fprintf(stderr, "%ld:\tERROR:\tLost Sync after interval!\n", ftell(fi));
						aborted_stream(6);
						
					} else if (verbose);
						//fprintf(stderr, "%ld:\tINFO:\tFound Restart Marker\n", ftell(fi));

					reset_prediction();
					clear_bits();
				}	/* intra-interval loop */
			} else
			leftover = mx_size * my_size; //picture size in units of MCUs 

			/* process till end of row without restarts */
			for (i = 0; i < leftover; i++){
				//turn = i & 0x3;
				process_MCU();
			}

			in_frame = 0;
			break;

		case EOI_MK:
			//if (verbose);
				//fprintf(stderr, "%ld:\tINFO:\tFound the EOI marker!\n", ftell(fi));
			if (in_frame)
				aborted_stream(7);

			//if (verbose);
				/*fprintf(stderr, "\tINFO:\tTotal skipped bytes %d, total stuffers %d\n", passed,
					stuffers);*/
			//fclose(fi);
			
	//mk_mon_debug_info(8888);
			//write_bmp();
		//	mk_mon_debug_info(6666);
			//printf("%ld,%ld", x_size * y_size * n_comp,MCU_sx * MCU_sy * n_comp);
			
			//free_structures();
			return 0;
			break;

		case COM_MK: //ffee
			//if (verbose);
				//fprintf(stderr, "%ld:\tINFO:\tSkipping comments\n", ftell(fi));
			skip_segment();
			break;

		case EOF:
			//if (verbose);
				//fprintf(stderr, "%ld:\tERROR:\tRan out of input data!\n", ftell(fi));
			aborted_stream(8);

		default:
			if ((mark & MK_MSK) == APP_MK) {//when read from FFEC, this will hold again
				//if (verbose);
					//fprintf(stderr, "%ld:\tINFO:\tSkipping application data\n", ftell(fi));
				skip_segment();
				break;
			}
			if (RST_MK(mark)) {
				reset_prediction();
				break;
			}
			/* if all else has failed ... */
			//fprintf(stderr, "%ld:\tWARNING:\tLost Sync outside scan, %d!\n", ftell(fi), mark);
			aborted_stream(9);
			break;
		}		/* end switch */
	} while (1);

	return 0;
}
Beispiel #5
0
int JpegToBmp()
{
	unsigned int aux, mark;
	int n_restarts, restart_interval, leftover;	/* RST check */
	int i, j;
	
	/* First find the SOI marker: */
	aux = get_next_MK();
	if (aux != SOI_MK)
		aborted_stream(0);
		
	in_frame = 0;
	restart_interval = 0;
	for (i = 0; i < 4; i++)
		QTvalid[i] = 0;

	/* Now process segments as they appear: */
	do {
		mark = get_next_MK();

		switch (mark) {
		case SOF_MK:  //ffc0 start of the frame
			in_frame = 1;
			get_size();   //0011 17

			/* load basic image parameters */
			FGETC();   //8
			
			y_size = get_size();//FGETC() twice
			x_size = get_size();

			n_comp = FGETC();

			for (i = 0; i < n_comp; i++) {
				comp[i].CID = FGETC();
				aux = FGETC();
				comp[i].HS = first_quad(aux); //0x11 >> 4   1
				comp[i].VS = second_quad(aux);  //&15       1
				comp[i].QT = FGETC();
			}

			if (init_MCU() == -1)
				aborted_stream(1);

			/* dimension scan buffer for YUV->RGB conversion */
			ColorBuffer = (unsigned char *)mk_malloc((size_t) MCU_sx * MCU_sy * n_comp);
			// ColorBuffer = (unsigned int*)mk_malloc(sizeof(unsigned int) * MCU_sy * x_size);
			FBuff = (FBlock *) mk_malloc(sizeof(FBlock));

			if (  (ColorBuffer == NULL) || (FBuff == NULL)) {
				aborted_stream(2);
			}
			break;

		case DHT_MK:
			if (load_huff_tables() == -1)
				aborted_stream(2);	
			break;

		case DQT_MK:  //FFDB, the following 0084 shows the table length 132 
			if (load_quant_tables() == -1)
				aborted_stream(3);
			break;

		case DRI_MK:
			get_size();	/* skip size */
			restart_interval = get_size();
			break;

		case SOS_MK:	/* lots of things to do here */ //ffda
			get_size();	/* don't care */
			aux = FGETC();  //03
			if (aux != (unsigned int)n_comp) {
				aborted_stream(4);
			}

			for (i = 0; i < n_comp; i++) {
				aux = FGETC();
				if (aux != comp[i].CID) {
					aborted_stream(5);
				}
				aux = FGETC();
				comp[i].DC_HT = first_quad(aux);
				comp[i].AC_HT = second_quad(aux);
			}
			get_size();
			FGETC();
			MCU_column = 0;
			MCU_row = 0;
			clear_bits();
			reset_prediction();
			
			/* main MCU processing loop here */
			if (restart_interval) {
				n_restarts = ceil_div(mx_size * my_size, restart_interval) - 1;
				leftover = mx_size * my_size - n_restarts * restart_interval;
				/* final interval may be incomplete */

				for (i = 0; i < n_restarts; i++) {
					for (j = 0; j < restart_interval; j++)
					{
						process_MCU();
					/* proc till all EOB met */
					}
					aux = get_next_MK();
					if (!RST_MK(aux)) {
						aborted_stream(6);
					} else if (verbose);
						reset_prediction();
						clear_bits();
				}	/* intra-interval loop */
			} else
			leftover = mx_size * my_size; //picture size in units of MCUs 

			/* process till end of row without restarts */
			for (i = 0; i < leftover; i++){
				process_MCU();
			}
			
			in_frame = 0;
			break;

		case EOI_MK:
			if (in_frame)
				aborted_stream(7);
			return 0;
			break;

		case COM_MK: //ffee
			skip_segment();
			break;

		case EOF:
			aborted_stream(8);

		default:
			if ((mark & MK_MSK) == APP_MK) {//when read from FFEC, this will hold again
				skip_segment();
				break;
			}
			if (RST_MK(mark)) {
				reset_prediction();
				break;
			}
			/* if all else has failed ... */
			aborted_stream(9);
			break;
		}		/* end switch */
	} while (1);

	return 0;
}
int JpegToBmp()
{
    unsigned int aux, mark;
    int n_restarts, restart_interval, leftover;	/* RST check */
    int i, j;

    /*No Need to do the file operation operation*/
#ifdef FILE_IO
    fi = fopen(file1, "rb");
    if (fi == NULL) 
    {
        return 0;
    }
#else 


#ifdef INPUT_DMA
    // wait for input data to arrive
    //mk_mon_debug_info(0xFF);
    // read input file DRAM (via my cmem-out)
	ddr_input = (unsigned int*)(shared_pt_REMOTEADDR + 1024*1024*4);
	cmem_input_circ_buff = (unsigned int*) (mb1_cmemout0_BASEADDR);

	hw_dma_receive_addr((int*)(cmem_input_circ_buff + buff_sel * INPUT_READ_SIZE_INT), (void*)(&ddr_input[ddr_input_chunck_offset*INPUT_READ_SIZE_INT]), INPUT_READ_SIZE_INT, (void*)mb1_dma0_BASEADDR);
	ddr_input_chunck_offset++;
	buff_sel = CHANGE_BUFFER(buff_sel);
	for (i = 0 ; i < NUM_OF_INIT_BUFF_LOAD-1; i++)
    {
	    while(hw_dma_status_addr( (void *) mb1_dma0_BASEADDR));
		hw_dma_receive_addr((unsigned int*)(cmem_input_circ_buff + buff_sel * INPUT_READ_SIZE_INT), (void*)(&ddr_input[ddr_input_chunck_offset*INPUT_READ_SIZE_INT]), INPUT_READ_SIZE_INT, (void*)mb1_dma0_BASEADDR);
		ddr_input_chunck_offset++;

        buff_sel = CHANGE_BUFFER(buff_sel);
    }

    fi = (unsigned char *)(cmem_input_circ_buff);
#else
    fi = (volatile unsigned int *)(shared_pt_REMOTEADDR+1024*1024*4);
#endif

#endif

    /* First find the SOI marker: */
    aux = get_next_MK(fi);


    if (aux != SOI_MK)
        aborted_stream(fi);

    if (verbose)
    {
#ifdef FILE_IO
        fprintf(stderr, "%ld:\tINFO:\tFound the SOI marker!\n", ftell(fi));
#else
        //printf("%d:\tINFO:\tFound the SOI marker!\n", FTELL());
#endif
    }
    in_frame = 0;
    restart_interval = 0;
    for (i = 0; i < 4; i++)
        QTvalid[i] = 0;


    /* Now process segments as they appear: */
    do {
        mark = get_next_MK(fi);

        //mk_mon_debug_info(0XFFF);
        //mk_mon_debug_info(mark);
        //mk_mon_debug_info(bit_counter);
        //mk_mon_debug_info(0XFFF);

        switch (mark) {
            case SOF_MK:
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tINFO:\tFound the SOF marker!\n", ftell(fi));
#else
                    //printf("%d:\tINFO:\tFound the SOF marker!\n", FTELL());
#endif
                }
                in_frame = 1;
                get_size(fi);	/* header size, don't care */

                /* load basic image parameters */
#ifdef FILE_IO
                fgetc(fi);	/* precision, 8bit, don't care */
#else
                FGETC(fi);	/* precision, 8bit, don't care */
#endif
                y_size = get_size(fi);
                x_size = get_size(fi);



                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "\tINFO:\tImage size is %d by %d\n", x_size, y_size);
#else
                    //printf("\tINFO:\tImage size is %d by %d\n", x_size, y_size);
#endif
                }

#ifdef FILE_IO
                n_comp = fgetc(fi);	/* # of components */
#else
                n_comp = FGETC(fi);	/* # of components */
#endif
                if (verbose) 
                {
#ifdef FILE_IO
                    fprintf(stderr, "\tINFO:\t");
#else
                    //printf("\tINFO:\t");
#endif
                    switch (n_comp) 
                    {
#ifdef FILE_IO
                        case 1:
                            fprintf(stderr, "Monochrome");
                            break;
                        case 3:
                            fprintf(stderr, "Color");
                            break;
                        default:
                            fprintf(stderr, "Not a");
                            break;
#else
                        case 1:
                            //printf("Monochrome");
                            break;
                        case 3:
                            //printf("Color");
                            break;
                        default:
                            //printf("Not a");
                            break;

#endif
                    }
#ifdef FILE_IO
                    fprintf(stderr, " JPEG image!\n");
#else
                    //printf(" JPEG image!\n");
#endif
                }

                for (i = 0; i < n_comp; i++) 
                {
#ifdef FILE_IO
                    /* component specifiers */
                    comp[i].CID = fgetc(fi);
                    aux = fgetc(fi);
                    comp[i].HS = first_quad(aux);
                    comp[i].VS = second_quad(aux);
                    comp[i].QT = fgetc(fi);
#else
                    /* component specifiers */
                    comp[i].CID = FGETC(fi);
                    aux = FGETC(fi);
                    comp[i].HS = first_quad(aux);
                    comp[i].VS = second_quad(aux);
                    comp[i].QT = FGETC(fi);
#endif
                }

                if ((n_comp > 1) && verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr,
                            "\tINFO:\tColor format is %d:%d:%d, H=%d\n",
                            comp[0].HS * comp[0].VS, comp[1].HS * comp[1].VS, comp[2].HS * comp[2].VS,
                            comp[1].HS);
#else
#if 0
                    //printf("\tINFO:\tColor format is %d:%d:%d, H=%d\n",
                    comp[0].HS * comp[0].VS, comp[1].HS * comp[1].VS, comp[2].HS * comp[2].VS,
                        comp[1].HS);
#endif
#endif
                }

                if (init_MCU() == -1)
                    aborted_stream(fi);

                /* dimension scan buffer for YUV->RGB conversion */
                /* TODO */			
#if 0
                FrameBuffer = (volatile unsigned char *)mk_malloc((size_t) x_size * y_size * n_comp);
#else
                FrameBuffer = (unsigned int *) mb1_cmemout1_BASEADDR;
#endif		
                //ColorBuffer = (volatile unsigned char *)mk_malloc((size_t) MCU_sx * MCU_sy * n_comp);
#if 0
                FBuff = (FBlock *) mk_malloc(sizeof(FBlock));
#else
                MY_MK_MALLOC(FBuff,FBlock,1);
#endif
#if 0
                PBuff = (PBlock *) mk_malloc(sizeof(PBlock));
#else
                MY_MK_MALLOC(PBuff,PBlock,1);
#endif

                if ((FrameBuffer == NULL) /*|| (ColorBuffer == NULL)*/ || (FBuff == NULL) || (PBuff == NULL)) 
                {
#ifdef FILE_IO
                    fprintf(stderr, "\tERROR:\tCould not allocate pixel storage!\n");
#else
                    //printf("\tERROR:\tCould not allocate pixel storage!\n");
#endif
                }
                break;

            case DHT_MK:
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tINFO:\tDefining Huffman Tables\n", ftell(fi));
#else
                    //printf("%d:\tINFO:\tDefining Huffman Tables\n", FTELL());
#endif
                }
                if (load_huff_tables(fi) == -1)
                    aborted_stream(fi);
                break;

            case DQT_MK:
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tINFO:\tDefining Quantization Tables\n", ftell(fi));
#else
                    //printf("%d:\tINFO:\tDefining Quantization Tables\n", FTELL());
#endif
                }
                if (load_quant_tables(fi) == -1)
                    aborted_stream(fi);
                break;

            case DRI_MK:
                get_size(fi);	/* skip size */
                restart_interval = get_size(fi);
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tINFO:\tDefining Restart Interval %d\n", ftell(fi),restart_interval);
#else
                    //printf("%d:\tINFO:\tDefining Restart Interval %d\n", FTELL(), restart_interval);
#endif
                }
                break;

            case SOS_MK:	/* lots of things to do here */
                //mk_mon_debug_info(01);
                //mk_mon_debug_info(bit_counter);
                //mk_mon_debug_info(02);
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tINFO:\tFound the SOS marker!\n", ftell(fi));
#else
                    //printf("%d:\tINFO:\tFound the SOS marker!\n", FTELL(fi));
#endif
                }
                get_size(fi);	/* don't care */
#ifdef FILE_IO
                aux = fgetc(fi);
#else
                aux = FGETC(fi);
#endif
                if (aux != (unsigned int)n_comp) 
                {
#ifdef FILE_IO
                    fprintf(stderr, "\tERROR:\tBad component interleaving!\n");
#else
                    //printf("\tERROR:\tBad component interleaving!\n");
#endif
                    aborted_stream(fi);
                }

                for (i = 0; i < n_comp; i++) 
                {
#ifdef FILE_IO
                    aux = fgetc(fi);
#else
                    aux = FGETC(fi);
#endif
                    if (aux != comp[i].CID) 
                    {
#ifdef FILE_IO
                        fprintf(stderr, "\tERROR:\tBad Component Order!\n");
#else
                        //printf("\tERROR:\tBad Component Order!\n");
#endif
                        aborted_stream(fi);
                    }
#ifdef FILE_IO
                    aux = fgetc(fi);
#else
                    aux = FGETC(fi);
#endif
                    comp[i].DC_HT = first_quad(aux);
                    comp[i].AC_HT = second_quad(aux);
                }
                get_size(fi);
#ifdef FILE_IO
                fgetc(fi);	/* skip things */
#else
                FGETC(fi);	/* skip things */
#endif

                MCU_column = 0;
                MCU_row = 0;
                clear_bits();
                reset_prediction();

                /* main MCU processing loop here */
                if (restart_interval) 
                {
                    n_restarts = ceil_div(mx_size * my_size, restart_interval) - 1;
                    leftover = mx_size * my_size - n_restarts * restart_interval;
                    /* final interval may be incomplete */

                    for (i = 0; i < n_restarts; i++) 
                    {
                        for (j = 0; j < restart_interval; j++)
                        {
                            process_MCU(fi);
                        }
                        /* proc till all EOB met */

                        aux = get_next_MK(fi);
                        if (!RST_MK(aux)) 
                        {
#ifdef FILE_IO
                            fprintf(stderr, "%ld:\tERROR:\tLost Sync after interval!\n", ftell(fi));
#else
                            //printf("%d:\tERROR:\tLost Sync after interval!\n", FTELL());
#endif
                            aborted_stream(fi);
                        } 
                        else if (verbose)
                        {
                            //printf("%d:\tINFO:\tFound Restart Marker\n", FTELL());
                        }

                        reset_prediction();
                        clear_bits();
                    }	/* intra-interval loop */
                } 
                else
                {
                    leftover = mx_size * my_size;
                }
                /* process till end of row without restarts */
                for (i = 0; i < leftover; i++)
                {
                    process_MCU(fi);
                }
                in_frame = 0;
                //mk_mon_debug_info(0XFEFE);

                //mk_mon_debug_info(0XFEFE);
                break;

            case EOI_MK:
                //mk_mon_debug_info(0XDEADBEE2);
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tINFO:\tFound the EOI marker!\n", ftell(fi));
#else
                    //printf("%d:\tINFO:\tFound the EOI marker!\n", FTELL());
#endif
                }
                if (in_frame)
                {
                    aborted_stream(fi);
                }
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "\tINFO:\tTotal skipped bytes %d, total stuffers %d\n", passed, stuffers);
#else
                    //printf("\tINFO:\tTotal skipped bytes %d, total stuffers %d\n", passed, stuffers);
#endif
                }
#ifdef FILE_IO
                fclose(fi);
#else
                /*Check if something has to be done!!*/
#endif

#ifdef FILE_IO
                //	write_bmp(file2);
#else
                /*Need to implement the function to write in DDR*/	
                //	write_bmp_to_ddr_1();
                //printf_frame_buffer();
#endif
#ifdef FILE_IO
                free_structures();
#else
                /*No Need to do anything as structures are static*/
                //mk_mon_debug_info(0XDEADBEE1);

                //free_structures();
                //mk_mon_debug_info(0XDEADBEE2);
#endif
                /* signal to core 2 that the FIFO is initialized and can be read from. */
                while(cheap_is_empty(producer) != 1)
                {
#if 0

                    mk_mon_debug_info(producer->readc);
                    mk_mon_debug_info(producer->writec);
#endif
                }

                *fifo_sync_data = 0;
                DMA_SEND_BLOCKING((void *)fifo_sync, (int*)fifo_sync_data, sizeof(int),(void *)mb1_dma0_BASEADDR,DMA_flag);
                return 0;
                break;

            case COM_MK:
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tINFO:\tSkipping comments\n", ftell(fi));
#else
                    //printf("%d:\tINFO:\tSkipping comments\n", FTELL());
#endif
                }
                skip_segment(fi);
                break;

            case 0XD9:
                //case 0XD9:
                if (verbose)
                {
#ifdef FILE_IO
                    fprintf(stderr, "%ld:\tERROR:\tRan out of input data!\n", ftell(fi));
#else
                    //printf("%d:\tERROR:\tRan out of input data!\n", FTELL());
#endif
                }

                aborted_stream(fi);

            default:
                if ((mark & MK_MSK) == APP_MK) 
                {
                    if (verbose)
                    {
#ifdef FILE_IO
                        fprintf(stderr, "%ld:\tINFO:\tSkipping application data\n", ftell(fi));
#else
                        //printf("%d:\tINFO:\tSkipping application data\n", FTELL());
#endif
                    }
                    skip_segment(fi);
                    break;
                }
                if (RST_MK(mark)) 
                {
                    reset_prediction();
                    break;
                }
                /* if all else has failed ... */
#ifdef FILE_IO
                fprintf(stderr, "%ld:\tWARNING:\tLost Sync outside scan, %d!\n", ftell(fi), mark);
#else
                //printf("%d:\tWARNING:\tLost Sync outside scan, %d!\n", FTELL(), mark);
#endif
                aborted_stream(fi);
                break;
        }		/* end switch */
    } while (1);

    return 0;
}