Пример #1
0
int
main (int argc, char *argv[])
{
  char *devname = "/dev/dsp";
  short buf[32 * BUFSZ];
  char line[1024];
  int i, parm;
  int l, speed, wpm = 12;

  fd_set readfds, writefds;

  if (argc > 1)
    devname = argv[1];

  if (argc > 2)
    {
      wpm = atoi (argv[2]);
      if (wpm == 0)
	wpm = 12;
    }

  ncodes = strlen (Chars);

  srandom (time (0));

  if (argc > 3)
    {
      for (i = 3; i < argc; i++)
	parse_charlist (argv[i]);
    }
  else
    {
      strcpy (randomlist, Chars);
      nrandom = strlen (randomlist);
    }

  if (nrandom < 2)
    {
      printf ("Bad character list\n");
      exit (-1);
    }

  randomlist[nrandom] = 0;

  printf ("Practicing codes: %s\n", randomlist);
  for (i = 0; i <= nrandom; i += 4)
    {
      int j, k;
      char line[256], tmp[20];
      memset (line, ' ', 80), line[78] = 0;

      for (j = 0; j < 4; j++)
	if (i + j <= nrandom)
	  {
	    int ix;

	    ix = findcode (randomlist[i + j]);

	    sprintf (tmp, "%c %s", randomlist[i + j], Codes[ix]);
	    for (k = 0; k < strlen (tmp); k++)
	      line[j * 20 + k] = tmp[k];
	  }

      printf ("%s\n", line);
    }

  speed = wpm;

  printf ("Words per minute %d. Characters per minute %d\n", wpm, wpm * 5);

  dotsize = SRATE / speed;

  if ((audiofd = open (devname, O_WRONLY, 0)) == -1)
    {
      perror (devname);
      exit (-1);
    }

  parm = 0x0003000a;
  ioctl (audiofd, SNDCTL_DSP_SETFRAGMENT, &parm);

  parm = AFMT_S16_LE;
  if (ioctl (audiofd, SNDCTL_DSP_SETFMT, &parm) == -1)
    {
      perror ("SETFMT");
      close (audiofd);
      exit (-1);
    }

  if (parm != AFMT_S16_LE)
    {
      printf
	("Error: 32/24 bit sample format is not supported by the device\n");
      printf ("%08x/%08x\n", parm, AFMT_S16_LE);
      close (audiofd);
      exit (-1);
    }

  parm = SRATE;
  if (ioctl (audiofd, SNDCTL_DSP_SPEED, &parm) == -1)
    {
      perror ("SPEED");
      close (audiofd);
      exit (-1);
    }

  if (parm != SRATE)
    {
      printf
	("Error: %d Hz sampling rate is not supported by the device (%d)\n",
	 SRATE, parm);
      close (audiofd);
      exit (-1);
    }

  if (tcgetattr (terminal_fd, &saved_ti) == -1)
    {
      perror ("tcgetattr");
      exit (-1);
    }

  signal (SIGINT, terminate);
/*
 * Set up the terminal (stdin) for single character input.
 */

  if (tcgetattr (terminal_fd, &ti) == -1)
    {
      perror ("tcgetattr");
      exit (-1);
    }


  ti.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  ti.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  ti.c_cflag &= ~(CSIZE | PARENB);
  ti.c_cflag |= CS8;
  ti.c_oflag &= ~(OPOST);

  ti.c_cc[VMIN] = 1;
  ti.c_cc[VTIME] = 1;

  if (tcsetattr (terminal_fd, TCSAFLUSH, &ti) == -1)
    {
      perror ("tcgetattr");
      exit (-1);
    }

  a = 0.0;

/*
 * Play some "extra" audio data to delay the startup. This just gives
 * some extra time to the user to prepare before we start.
 */

  step = 360.0 * 600.0 / parm;

  l = 0;
  l += genpulse (&buf[l], 1, 0);
  write (audiofd, buf, l * 2);
  memset (buf, 0, 4096);
  for (i = 0; i < 2; i++)
    write (audiofd, buf, 4096);

/*
 * The actual playback starts here 
 */

  randomplay ();

  t0 = time (0);
  while (!done)
    {
      int n;

/*
 * Set up select for output events on the audio device and input events on
 * the keyboard.
 */
      FD_ZERO (&readfds);
      FD_ZERO (&writefds);

      FD_SET (audiofd, &writefds);
      FD_SET (0, &readfds);

/*
 * Call select with no timeouts
 */
      if ((n = select (audiofd + 1, &readfds, &writefds, NULL, NULL)) == -1)
	{
	  perror ("select");
	  exit (-1);
	}

      if (n == 0)
	continue;

      if (FD_ISSET (0, &readfds))	/* 0 means stdin */
	{
/*
 * Handling of keyboard input. Check if the answer was right.
 */
	  if (read (0, line, 1) == 1)
	    {
	      if (*line == 27)	/* ESC */
		terminate (SIGINT);
	      if ((unsigned char) *line != (unsigned char) playc)
		{
		  int x;

		  totalerrors++;
		  chars_to_play += 4;
		  randomlist[nrandom++] = playc;
		  randomlist[nrandom++] = playc;
		  for (x = 0; x < nrandom; x++)
		    if (randomlist[x] == *line)
		      {
			randomlist[nrandom++] = *line;
			break;
		      }
		  playerror ();
		  if (++errors > 3)
		    {
		      printf ("It is '%c' not '%c'\r\n", playc, *line);
		      fflush (stdout);
		    }
		  playchar (playc);
		}
	      else
		{
		  errors = 0;
		  randomplay ();
		}
	    }
	}

      if (FD_ISSET (audiofd, &writefds))
	{
/*
 * The audio device is ready to accept more data. Keep the device happy by
 * writing some silent samples to it.
 *
 * Note that the real "playload" signal will be played by the playchar()
 * routine.
 */
	  memset (buf, 0, 1024);
	  write (audiofd, buf, 1024);
	}
    }

/*
 * Everything done. Restore the teminal settings and exit.
 */
  terminate (15);

  close (audiofd);

  exit (0);
}
Пример #2
0
int main(int argc, char *argv[])
{

#if XPAR_MICROBLAZE_0_USE_ICACHE
      microblaze_init_icache_range(0, XPAR_MICROBLAZE_0_CACHE_BYTE_SIZE);
      microblaze_enable_icache();
   #endif

   #if XPAR_MICROBLAZE_0_USE_DCACHE
      microblaze_init_dcache_range(0, XPAR_MICROBLAZE_0_DCACHE_BYTE_SIZE);
      microblaze_enable_dcache();
   #endif
	
	rows imago;
	int crom_flag,i,j,lvl,temprows,tempblocks,offset[]={0,4,32,36},simb,size;
	int  luminance_table[]={16, 11, 10, 16,  24,  40,  51,  61,
				12, 12, 14, 19,  26,  58,  60,  55,
				14, 13, 16, 24,  40,  57,  69,  56,
				14, 17, 22, 29,  51,  87,  80,  62,
				18, 22, 37, 56,  68, 109, 103,  77,
				24, 35, 55, 64,  81, 104, 113,  92,
				49, 64, 78, 87, 103, 121, 120, 101,
				72, 92, 95, 98, 112, 100, 103,  99};

	int crominance_table[]={17, 18, 24, 47, 99, 99, 99, 99,
				18, 21, 26, 66, 99, 99, 99, 99,
				24, 26, 56, 99, 99, 99, 99, 99,
				47, 66, 99, 99, 99, 99, 99, 99,
				99, 99, 99, 99, 99, 99, 99, 99,
				99, 99, 99, 99, 99, 99, 99, 99,
				99, 99, 99, 99, 99, 99, 99, 99,
				99, 99, 99, 99, 99, 99, 99, 99,};
	int lum_tab_corr[64], crom_tab_corr[64];
				
	info infoimago;
	int_rows intimago;
	huffman_tab tbl_dclum, tbl_aclum, tbl_dccrom, tbl_accrom;
	FILE *file_in, *file_out;
	unsigned  char buffer,field_free_space=8;
	buffer = 0x0;		//UCCIDI UCCIDI UCCIDI

/*
   xil_printf("\n\rINIZIO COMPRESSIONE!!!");
*/
	if (argc > 1)
        {
           file_in=fopen(argv[1],"r");
        }
        else 
           file_in=fopen("./software/apps/jpeg/img.ppm","r");			//binario?

        if (!file_in) return(0);
	
	//Avvio del timer
/*
	XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0);
	XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK );
*/
	imago=rdppm(&infoimago, file_in);
	// Stop timer e stampa dei cicli di computazione
/*
	XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0);
	xil_printf("\n\rlettura file: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0));
*/
	
	if (imago==NULL) return(0);

	fclose(file_in);



	intimago=(p_intblock *) malloc (infoimago.numrows*sizeof(p_intblock));
	for (i=0;i<infoimago.numrows;i++)
		*(intimago+i)=(intblock *) malloc (infoimago.numblocks*sizeof(intblock));

	if (infoimago.color==1)
	{	
		//Avvio del timer
/*
		XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0);
		XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK );
*/
		for (i=0;i<infoimago.numrows;i++)
			for (j=0;j<infoimago.numblocks;j++)
				RGBtoYUV(&(*(imago+i)+j)->comp1[0],&(*(imago+i)+j)->comp2[0],&(*(imago+i)+j)->comp3[0]);
		// Stop timer e stampa dei cicli di computazione
/*
		XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0);
		xil_printf("\n\rrgb to yuv: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0));
*/
	}

	if (infoimago.color==1)
	{
		rows tempimago;
		
		//Avvio del timer
/*
		XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0);
		XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK );
*/
		tempimago=expand_image(imago,infoimago.numrows,infoimago.numblocks,&temprows,&tempblocks);

		for (i=0;i<temprows;i++)
			for (j=0;j<tempblocks;j++)
				downsample((*(tempimago+i)+j),(*(imago+i/2)+j/2),offset[j%2+(i%2)*2]);
		for (i=0;i<temprows;i++)
		free(*(tempimago+i));
		free(tempimago);
		
		// Stop timer e stampa dei cicli di computazione
/*
		XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0);
		xil_printf("\n\rdownsample e espandsione: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0));
*/
	}

	lvl= 80;
	
	//Avvio del timer
/*
	XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0);
	XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK );
*/
	set_quantization_tbl(lvl, &luminance_table[0]);
	set_quantization_tbl(lvl, &crominance_table[0]);
	correct_quantization_tbl(luminance_table, lum_tab_corr);
	correct_quantization_tbl(crominance_table, crom_tab_corr);
	// Stop timer e stampa dei cicli di computazione
/*
	XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0);
	xil_printf("\n\rrset quantization table: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0));
*/

	//Avvio del timer
/*
	XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0);
	XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK );
*/
	for (i=0;i<infoimago.numrows;i++)
		for (j=0;j<infoimago.numblocks;j++)
		{
			if ((infoimago.color==1)&&(i<temprows/2)&&(j<tempblocks/2)) crom_flag=1;
			else crom_flag=0;

			DCT_and_quantization(*(imago+i)+j,lum_tab_corr,crom_tab_corr,*(intimago+i)+j,crom_flag);
		}
	
	for (i=0;i<infoimago.numrows;i++)
		free(*(imago+i));
	free(imago);

	for (i=0;i<infoimago.numrows;i++)
		for (j=0;j<infoimago.numblocks;j++)
			arrange(*(intimago+i)+j);
	// Stop timer e stampa dei cicli di computazione
/*
	XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0);
	xil_printf("\n\rdct and quantization: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0));
*/
//Avvio del timer
/*XTmrCtr_mLoadTimerCounterReg( XPAR_OPB_TIMER_1_BASEADDR, 0);
XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, 0, XTC_CSR_ENABLE_TMR_MASK );
*/
initialize_huff_tbl(&tbl_dclum,0);
initialize_huff_tbl(&tbl_aclum,16);
initialize_huff_tbl(&tbl_dccrom,1);
initialize_huff_tbl(&tbl_accrom,17);

arrange_table(&luminance_table[0]);
arrange_table(&crominance_table[0]);
if (argc > 2)
   file_out=fopen(argv[2],"w");
else
   file_out=fopen("img_out.jpg","w");			//binario??
writeheaders (file_out, infoimago, &luminance_table[0], &crominance_table[0],
tbl_aclum,tbl_accrom,tbl_dclum,tbl_dccrom);

if (infoimago.color==1)
{
	int oldlum=0, oldcrom1=0, oldcrom2=0;
	for (i=0;i<infoimago.numrows;i+=2)
		for (j=0;j<infoimago.numblocks;j+=2)
		{

			writescan(oldlum,(*(intimago+i)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago);
			if ((j+1)<infoimago.numblocks)
			{

				writescan((*(intimago+i)+j)->intcomp1[0],(*(intimago+i)+j+1)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago);
				if ((i+1)<infoimago.numrows)
				{

					writescan((*(intimago+i)+j+1)->intcomp1[0],(*(intimago+i+1)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago);

					writescan((*(intimago+i+1)+j)->intcomp1[0],(*(intimago+i+1)+j+1)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago);
					oldlum=(*(intimago+i+1)+j+1)->intcomp1[0];
				}
				else
				{
					simb=findcode(0,tbl_dclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
					simb=findcode(0,tbl_aclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
					simb=findcode(0,tbl_dclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
					simb=findcode(0,tbl_aclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
					oldlum=(*(intimago+i)+j+1)->intcomp1[0];
				}
			}
			else
			{
				simb=findcode(0,tbl_dclum,&size);
				wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
				simb=findcode(0,tbl_aclum,&size);
				wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
				if ((i+1)<infoimago.numrows)
				{

				writescan((*(intimago+i)+j)->intcomp1[0],(*(intimago+i+1)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago);
				simb=findcode(0,tbl_dclum,&size);
				wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
				simb=findcode(0,tbl_aclum,&size);
				wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
				oldlum=(*(intimago+i+1)+j)->intcomp1[0];
				}
				else
				{
					simb=findcode(0,tbl_dclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
					simb=findcode(0,tbl_aclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
					simb=findcode(0,tbl_dclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
					simb=findcode(0,tbl_aclum,&size);
					wrbuffer(file_out,simb,&field_free_space,&buffer,size,0,0);
				}
			}


			writescan(oldcrom1,(*(intimago+i/2)+j/2)->intcomp2,&field_free_space,&buffer,file_out,tbl_accrom,tbl_dccrom,infoimago);

			writescan(oldcrom2,(*(intimago+i/2)+j/2)->intcomp3,&field_free_space,&buffer,file_out,tbl_accrom,tbl_dccrom,infoimago);
			oldcrom1=(*(intimago+i/2)+j/2)->intcomp2[0];
			oldcrom2=(*(intimago+i/2)+j/2)->intcomp3[0];

		}
	}
	else
	{
	int oldlum=0;
	for (i=0;i<infoimago.numrows;i++)
		for (j=0;j<infoimago.numblocks;j++)
		{

			writescan(oldlum,(*(intimago+i)+j)->intcomp1,&field_free_space,&buffer,file_out,tbl_aclum,tbl_dclum,infoimago);
			oldlum=(*(intimago+i)+j)->intcomp1[0];
		}
	}
write_end_of_image(file_out);
fclose(file_out);

// Stop timer e stampa dei cicli di computazione
/*
XTmrCtr_mDisable(XPAR_OPB_TIMER_1_BASEADDR,0);
xil_printf("\n\rcodifiche entropiche: %d\n\r",XTmrCtr_mGetTimerCounterReg(XPAR_OPB_TIMER_1_BASEADDR,0));

xil_printf("\n\rFINE COMPRESSIONE!!!");
*/
 return 0;
}
Пример #3
0
int
main (int argc, char *argv[])
{
  char *devname = "/dev/dsp";
  short buf[16 * BUFSZ];
  char line[1024];
  int i, parm;
  int l, speed, wpm = 12;

  fd_set readfds, writefds;

  if (argc > 1)
    devname = argv[1];

  if (argc > 2)
    {
      wpm = atoi (argv[2]);
      if (wpm == 0)
	wpm = 12;
    }

  ncodes = strlen (Chars);

  srandom (time (0));

  if (argc > 3)
    {
      for (i = 3; i < argc; i++)
	parse_charlist (argv[i]);
    }
  else
    {
      strcpy (randomlist, Chars);
      nrandom = strlen (randomlist);
    }

  if (nrandom < 2)
    {
      printf ("Bad character list\n");
      exit (-1);
    }

  randomlist[nrandom] = 0;

  memset (typed_chars, ' ', sizeof (typed_chars));

  printf ("Practicing codes: %s\n", randomlist);
  for (i = 0; i <= nrandom; i += 4)
    {
      int j, k;
      char line[256], tmp[20];
      memset (line, ' ', 80), line[78] = 0;

      for (j = 0; j < 4; j++)
	if (i + j <= nrandom)
	  {
	    int ix;

	    ix = findcode (randomlist[i + j]);

	    sprintf (tmp, "%c %s", randomlist[i + j], Codes[ix]);
	    for (k = 0; k < strlen (tmp); k++)
	      line[j * 20 + k] = tmp[k];
	  }

      printf ("%s\n", line);
    }

  speed = wpm;

  printf ("Words per minute %d. Characters per minute %d\n", wpm, wpm * 5);

  dotsize = SRATE / speed;

  if ((audiofd = open (devname, O_WRONLY, 0)) == -1)
    {
      perror (devname);
      exit (-1);
    }

  parm = 0x0004000a;
  ioctl (audiofd, SNDCTL_DSP_SETFRAGMENT, &parm);

  parm = AFMT_S16_LE;
  if (ioctl (audiofd, SNDCTL_DSP_SETFMT, &parm) == -1)
    {
      perror ("SETFMT");
      close (audiofd);
      exit (-1);
    }

  if (parm != AFMT_S16_LE)
    {
      printf
	("Error: 32/24 bit sample format is not supported by the device\n");
      printf ("%08x/%08x\n", parm, AFMT_S16_LE);
      close (audiofd);
      exit (-1);
    }

  parm = SRATE;
  if (ioctl (audiofd, SNDCTL_DSP_SPEED, &parm) == -1)
    {
      perror ("SPEED");
      close (audiofd);
      exit (-1);
    }

  if (parm != SRATE)
    {
      printf
	("Error: %d Hz sampling rate is not supported by the device (%d)\n",
	 SRATE, parm);
      close (audiofd);
      exit (-1);
    }

  if (tcgetattr (terminal_fd, &saved_ti) == -1)
    {
      perror ("tcgetattr");
      exit (-1);
    }

  signal (SIGINT, terminate);
/*
 * Line setup
 */

  if (tcgetattr (terminal_fd, &ti) == -1)
    {
      perror ("tcgetattr");
      exit (-1);
    }


  ti.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  ti.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  ti.c_cflag &= ~(CSIZE | PARENB);
  ti.c_cflag |= CS8;
  ti.c_oflag &= ~(OPOST);

  ti.c_cc[VMIN] = 1;
  ti.c_cc[VTIME] = 1;

  if (tcsetattr (terminal_fd, TCSAFLUSH, &ti) == -1)
    {
      perror ("tcgetattr");
      exit (-1);
    }

  a = 0.0;

  step = 360.0 * 600.0 / parm;

  l = 0;
  l += genpulse (&buf[l], 1, 0);
  write (audiofd, buf, l * 2);
  memset (buf, 0, 4096);
  for (i = 0; i < 2; i++)
    write (audiofd, buf, 4096);

  charspeed = speed * 5;
  if (charspeed > 25)
    charspeed = 25;
  charsize = 60 * SRATE * 2 / charspeed;
  printf ("Charrate %d chars/min -> (%d samples)\n", charspeed, charsize);

  printf ("\r\n");
  randomplay ();

  t0 = time (0);
  while (!done)
    {
      int n;

      FD_ZERO (&readfds);
      FD_ZERO (&writefds);

      FD_SET (audiofd, &writefds);
      FD_SET (0, &readfds);

      if ((n = select (audiofd + 1, &readfds, &writefds, NULL, NULL)) == -1)
	{
	  perror ("select");
	  exit (-1);
	}

      if (n == 0)
	continue;

      if (FD_ISSET (0, &readfds))
	{
	  if (read (0, line, 1) == 1)
	    editor (*line);
	}

      if (FD_ISSET (audiofd, &writefds))
	{
	  if (!(nplayed % 5))
	    playchar (' ');
	  randomplay ();
	}
    }

  terminate (15);

  close (audiofd);

  exit (0);
}