Ejemplo n.º 1
0
void processFrame(double **ip)//, double **dct, int ****ticm, int *T)
{
  DCT(ip, dct);

  // Find Texture for every input block
  for(int i = 0; i < 8; i++)
  {
    for(int j = 0; j < 8; j++)
    {
      int count = 0;
      for(int u = 0; u < 8; u++)
      {
        for(int v = 0; v < 8; v++)
        {
          if(ticm[i][j][u][v] == 0)
            continue;
          if(fabs(dct[u][v]) >= DCT_THRESHOLD)
            count++;
        }
      }
      if(count < 25)
        T[SIMPLE]++;
      else if(count < 30)
        T[MODERATE]++;
      else //if(count < 62)
        T[COMPLEX]++;
    }
  }
}
Ejemplo n.º 2
0
//Used to break a macro block into 4 8x8 blocks
void break_into_blocks(int macro_block[16][16], int macro_x_offset, int macro_y_offset){

  int block_matrix[8][8];
  for(int i = 0; i < 4; i++){ //Four times for the 4 8x8 blocks
    if(i == 0){
      x_offset = 0;
      y_offset = 0;
    }
    else if(i == 1){
      x_offset = 8;
      y_offset = 0;
    }
    else if(i == 2){
      x_offset = 0;
      y_offset = 8;
    }
    else if(i == 3){
      x_offset = 8;
      y_offset = 8;
    }

    for(int j = 0; j < 8; j++){
      for(int k = 0; k < 8; k++){
	block_matrix[j][k] = macro_block[j + y_offset][k + x_offset];
      }
    }
   
    //Print the offsets to the file
    fprintf(output_fileHandle, "%d %d\n",(macro_x_offset + x_offset), (macro_y_offset + y_offset)); 
    //print_8x8_matrix(block_matrix);
    DCT(block_matrix); //Send each block to the DCT function
  }
}
Ejemplo n.º 3
0
int main() {
  freopen("Lena.raw", "rb", stdin);
  int r, c;
  for (r = 0; r < N; r++)
    for (c = 0; c < N; c++)
      scanf("%c", &pic[r][c]);
  init();
  DCT();
  quantization();
  IDCT();
  MSE();
  return 0;
}
/* Perform DCT on Y channel of the image converted to YUV space

*/
void MyImage::DCTBasedCompDecomp()
{
	//memory allocated for  elements of each column.
	floatsYDCTBlock = new float *[Height];

	for( int i = 0 ; i < Height ; i++ )
	{
		floatsYDCTBlock[i] = new float[Width];
	}
	
	if(QUANT_ACTIVE == 1)
	{
		floatsYDCTQBlock = new float *[Height];

		for( int i = 0 ; i < Height ; i++ )
		{
			floatsYDCTQBlock[i] = new float[Width];
		}
	}

	if(DEQUANT_ACTIVE == 1)
	{
		floatsYDCTDQBlock = new float *[Height];

		for( int i = 0 ; i < Height ; i++ )
		{
			floatsYDCTDQBlock[i] = new float[Width];
		}
	}

	// Convert format from
	//From Linear array [YYYY...Y][UUUU...U][VVVV...V]
	//To 2D Array [YYYY..Y	[UUUU..U	[VVVV..V
	//			   YYYY..]	 UUUU..U]	 VVVV..V]
	TwoDArrayForm();
	
	if(PRINTDCT == 1)
	{
		remove("DCT.txt");
	}
	//Perform Discrete Cosine Transform
	// Converts image from spatial or pixel domain into frequency domain
	for(int row = 0; row < Height; row += 8)
	{
		for(int col = 0; col < Width; col += 8)
		{
			DCT(row, col);
			
			if(PRINTDCT == 1)
			{
				PrintDCT(row, col);
			}

			if(QUANT_ACTIVE == 1)
				Quantize(row, col);

			if(DEQUANT_ACTIVE == 1)
				DeQuantize(row, col);
		}
	}
	
	if(IDCT_ACTIVE != 1)
	{
		// Convert format from 
		//From 2D Array [YYYY..Y	[UUUU..U	[VVVV..V
		//			   YYYY..]	 UUUU..U]	 VVVV..V]
		//To Linear array [YYYY...Y][UUUU...U][VVVV...V]
		LinearArrayForm();
	}
}
Ejemplo n.º 5
0
	void MFCC::Process(char *_block, int _block_size, std::vector<float *> &cep_coeffs)
	{
		bool must_swap = (swap_specified || native_byte_order == IEEE_LE);

		int block_size = _block_size / 2;
		float *block = new float[block_size];
		for (int i = 0; i < block_size; ++i) {
			unsigned char a = _block[i * 2];
			unsigned char b = _block[i * 2 + 1];
			short s;
			if (must_swap) {
				s = (a) | (b << 8);
			} else {
				s = (a << 8) | (b);
			}
			block[i] = (float) s;
		}
		
		float log_energy, energy;
		int frame_count = 0;
		int read_bytes = 0;

		int end_pos = ReadBlock(block, 0, block_size, circular_float_buffer, cfb_size, cfb_pointer + 2, frame_length - frame_shift);

		DCOffsetFilter(circular_float_buffer, cfb_size, &cfb_pointer, frame_length - frame_shift);

		/*----------------------------------------------------------------*/
		/*                       Framing                                  */
		/*----------------------------------------------------------------*/
		while ((end_pos = ReadBlock(block, end_pos, block_size, circular_float_buffer, cfb_size, (cfb_pointer + 2) % cfb_size, frame_shift)) != -1) {
			/*-------------------*/
			/* DC offset removal */
			/*-------------------*/
			DCOffsetFilter(circular_float_buffer, cfb_size, &cfb_pointer, frame_shift);

			/*------------------*/
			/* logE computation */
			/*------------------*/
			energy = 0.0;
			for (int i = 0; i < frame_length; ++i) {
				int ind = (cfb_pointer + i + 3) % cfb_size;
				energy += circular_float_buffer[ind] * circular_float_buffer[ind];
			}

			if (minimum_energy > 0 && energy < minimum_energy) {
				continue;
			}
			if (energy < energy_floor_log_e) {
				log_energy = ENERGYFLOOR_logE;
			} else {
				log_energy = log(energy);
			}
	    
			/*-----------------------------------------------------*/
			/* Pre-emphasis, moving from circular to linear buffer */
			/*-----------------------------------------------------*/
			for (int i = 0; i < frame_length; i++) {
				int a = (cfb_pointer + i + 3) % cfb_size;
				int b = (cfb_pointer + i + 2) % cfb_size;
				float_buffer[i] = circular_float_buffer[a] - PRE_EMPHASIS * circular_float_buffer[b];
			}

			/*-----------*/
			/* Windowing */
			/*-----------*/
			Window(float_buffer, float_window, frame_length);

			/*-----*/
			/* FFT */
			/*-----*/
			/* Zero padding */
			for (int i = frame_length; i < fft_length; i++) {
				float_buffer[i] = 0.0f;
			}

			/* Real valued, in-place split-radix FFT */
			int tmp_int = (int) (log10((float) fft_length) / log10(2.0f));
			rfft(float_buffer, fft_length, tmp_int); /*tmp_int = log2(FFTLength)*/

			/* Magnitude spectrum */
			float_buffer[0] = abs(float_buffer[0]);  /* DC */
			for (int i = 1; i < fft_length / 2; ++i) { /* pi/(N/2), 2pi/(N/2), ..., (N/2-1)*pi/(N/2) */
				float_buffer[i] = sqrt(float_buffer[i] * float_buffer[i] + float_buffer[fft_length - i] * float_buffer[fft_length - i]);
			}
			float_buffer[fft_length / 2] = abs(float_buffer[fft_length / 2]);  /* pi/2 */

			/*---------------*/
			/* Mel filtering */
			/*---------------*/
			MelFilterBank(float_buffer, first_window);

			/*-------------------------------*/
			/* Natural logarithm computation */
			/*-------------------------------*/
			for (int i = 0; i < mel_filter_bank_size; ++i) {
				if (float_buffer[i] < energy_floor_fb) {
					float_buffer[i] = ENERGYFLOOR_FB;
				} else {
					float_buffer[i] = log(float_buffer[i]);
				}
			}

			/*---------------------------*/
			/* Discrete Cosine Transform */
			/*---------------------------*/
			DCT(float_buffer, dct_matrix, cepstral_coefficient_number, mel_filter_bank_size);

			/*--------------------------------------*/
			/* Append logE after c0 or overwrite c0 */
			/*--------------------------------------*/
			float_buffer[mel_filter_bank_size + cepstral_coefficient_number - (no_coefficient_zero ? 1:0)] = log_energy; 

			/*---------------*/
			/* Output result */
			/*---------------*/
			int coeff_number = (int) (cepstral_coefficient_number - (no_coefficient_zero ? 1:0) + (no_log_energy ? 0:1));
			float *cep_coeff = new float[coeff_number];
			for (int i = 0; i < coeff_number; ++i) {
				cep_coeff[i] = float_buffer[mel_filter_bank_size + i];
			}
			cep_coeffs.push_back(cep_coeff);
			frame_count++;
		}

		delete[] block;
	}
Ejemplo n.º 6
0
int main(int argc, char* argv[]) 
{
	FILE* in = fopen(argv[1], "r");
	FILE* out = fopen(argv[3], "w+");

	unsigned char* buf;
	char optbuf[16];
	char c;
	
	unsigned char* r;
	unsigned char* g;
	unsigned char* b;

	double* y = (double*)malloc(BLOK * BLOK * sizeof(double));
	double* cr = (double*)malloc(BLOK * BLOK * sizeof(double));
	double* cb = (double*)malloc(BLOK * BLOK * sizeof(double));

	double* y_transf = (double*)malloc(BLOK * BLOK * sizeof(double));
	double* cr_transf = (double*)malloc(BLOK * BLOK * sizeof(double));
	double* cb_transf = (double*)malloc(BLOK * BLOK * sizeof(double));

	int m,n;
	int rgb_comp;
	int i;
	int j;
	int k;
	int blok_x = atoi(argv[2]);
	
	int K1[] = { 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 K2[] = { 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 };

	if (argc != 4) {
		printf("Neispravan poziv programa\n");
		return 1;
	}

	if (!in) {
		printf("Nije moguce otvoriti sliku\n");
		return 1;
	}

	fgets(optbuf, sizeof(optbuf), in);
	
	if (optbuf[0] != 'P' || optbuf[1] != '6') {
		printf("Format mora biti P6\n");
		return 1;
	}

	c = getc(in);
	while (c == '#') {
		while (getc(in) != '\n');
		c = getc(in);
	}
	ungetc(c, in);

	if (fscanf(in, "%d %d", &m, &n) != 2) {
		printf("Nepravilna velicina slike\n");
		return 1;
	}
	buf = (unsigned char*)malloc(m * n * 3 * sizeof(unsigned char));
	r = (unsigned char*)malloc(m * n * sizeof(unsigned char));
	g = (unsigned char*)malloc(m * n * sizeof(unsigned char));
	b = (unsigned char*)malloc(m * n * sizeof(unsigned char));
	
	if (fscanf(in, "%d", &rgb_comp) != 1) {
		printf("Nemoguce ucitati dubinu RGB komponente\n");
		return 1;
	}

	if (rgb_comp != 255) {
		printf("Nepravilna dubina RGB komponente\n");
		return 1;
	}
	
	fscanf(in, "%d", &rgb_comp);	/* visak... */
	
	fread(buf, 3 * m, n, in);

	for (i = 0, j = 0; i < m * n * 3; i += 3, j++) {
		r[j] = buf[i];
		g[j] = buf[i + 1];
		b[j] = buf[i + 2];
	}

	/* izvadi blok */
	k = 0;
	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			int K = blok_x - (blok_x / (m / BLOK)) * (m / BLOK);
			y[k] = r[i * m + (blok_x / (m / BLOK))
					* (m * BLOK) + K * BLOK + j];
			cb[k] = g[i * m + (blok_x / (m / BLOK))
					* (m * BLOK) + K * BLOK + j];
			cr[k] = b[i * m + (blok_x / (m / BLOK))
					* (m * BLOK) + K * BLOK + j];
			k++;
		}
	}

	/* prebaci u YCbCr */
	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			int ij = i * BLOK + j;
			double r = y[ij];
			double g = cb[ij];
			double b = cr[ij];
			y[ij] = 0.299 * r + 0.587 * g + 0.114 * b;
			cb[ij] = 128 - 0.168736 * r - 0.331264 * g + 0.5 * b;
			cr[ij] = 128 + 0.5 * r -0.418688 * g - 0.081312 * b;
		}
	}

	/* napravi pomak */
	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			y[i * BLOK + j] = y[i * BLOK + j] + POMAK;
			cb[i * BLOK + j] = cb[i * BLOK + j] + POMAK;
			cr[i * BLOK + j] = cr[i * BLOK + j] + POMAK;
		}
	}

	/* napravi DCT */
	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			y_transf[i * BLOK + j] = DCT(i, j, y);
			cb_transf[i * BLOK + j] = DCT(i, j, cb);
			cr_transf[i * BLOK + j] = DCT(i, j, cr);
		}
	}
	
	/* kvantiziraj */
	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			y_transf[i * BLOK + j] = 
				round_i(y_transf[i * BLOK + j] / K1[i * BLOK + j]);
			cb_transf[i * BLOK + j] = 
				round_i(cb_transf[i * BLOK + j] / K2[i * BLOK + j]);
			cr_transf[i * BLOK + j] = 
				round_i(cr_transf[i * BLOK + j] / K2[i * BLOK + j]);
		}
	}

	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			fprintf(out, "%.0f ", y_transf[i * BLOK + j]);
		}
		fprintf(out, "\n");
	}
		
	fprintf(out, "\n");
	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			fprintf(out, "%.0f ", cb_transf[i * BLOK + j]);
		}
		fprintf(out, "\n");
	}

	fprintf(out, "\n");
	for (i = 0; i < BLOK; i++) {
		for (j = 0; j < BLOK; j++) {
			fprintf(out, "%.0f ", cr_transf[i * BLOK + j]);
		}
		fprintf(out, "\n");
	}
	
	fclose(out);
	fclose(in);
	free(buf);
	free(r);
	free(g);
	free(b);
	free(y);
	free(cb);
	free(cr);
	free(y_transf);
	free(cb_transf);
	free(cr_transf);
	return 0;
}
Ejemplo n.º 7
0
/****************************************************
	DCTDIB()

	参数:

		hDIB为输入的DIB句柄

	返回值:

		成功为TRUE;失败为FALSE

	说明:

		本函数实现DIB位图的快速余弦变换
****************************************************/
BOOL DCTDIB(HDIB hDIB)
{
	if (hDIB == NULL)
		return FALSE;

	// start wait cursor
	WaitCursorBegin();

	HDIB hDib = NULL;
	HDIB hNewDib = NULL;
	// we only convolute 24bpp DIB, so first convert DIB to 24bpp
	WORD wBitCount = DIBBitCount(hDIB);
	if (wBitCount != 24)
	{
		hNewDib = ConvertDIBFormat(hDIB, 24, NULL);
		hDib = CopyHandle(hNewDib);
	}
	else
	{
		hNewDib = CopyHandle(hDIB);
		hDib = CopyHandle(hDIB);
	}

	if (hNewDib == NULL && hDib == NULL)
	{
		WaitCursorEnd();
		return FALSE;
	}

	// process!
	LPBYTE lpSrcDIB = (LPBYTE)GlobalLock(hDib);
	LPBYTE lpDIB = (LPBYTE)GlobalLock(hNewDib);
	LPBYTE lpInput = FindDIBBits(lpSrcDIB);
	LPBYTE lpOutput = FindDIBBits(lpDIB);
	int nWidth = DIBWidth(lpSrcDIB);
	int nHeight = DIBHeight(lpSrcDIB);

	int w=1,h=1,wp=0,hp=0;
	while(w*2<=nWidth)
	{
		w*=2;
		wp++;
	}
	while(h*2<=nHeight)
	{
		h*=2;
		hp++;
	}
	int x,y;
	BYTE *lpPoints=new BYTE[nWidth*nHeight];
	GetPoints(nWidth,nHeight,lpInput,lpPoints);

	double *f=new double[w*h];
	double *W=new double[w*h];

	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			f[x+y*w]=Point(x,y);
		}
	}

	for(y=0;y<h;y++)
	{
		DCT(&f[w*y],&W[w*y],wp);
	}
	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			f[x*h+y]=W[x+w*y];
		}
	}
	for(x=0;x<w;x++)
	{
		DCT(&f[x*h],&W[x*h],hp);
	}
	double a;
	memset(lpPoints,0,nWidth*nHeight);

	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			a=fabs(W[x*h+y]);
			if (a>255) a=255;
			Point(x,nHeight-y-1)=(BYTE)(a);
		}
	}
	delete f;
	delete W;
	PutPoints(nWidth,nHeight,lpOutput,lpPoints);
	delete lpPoints;

	// recover
	DWORD dwSize = GlobalSize(hDib);
	memcpy(lpSrcDIB, lpDIB, dwSize);
	GlobalUnlock(hDib);
	GlobalUnlock(hNewDib);
	if (wBitCount != 24)
	{
		hNewDib = ConvertDIBFormat(hDib, wBitCount, NULL);

		lpSrcDIB = (LPBYTE)GlobalLock(hDIB);
		lpDIB = (LPBYTE)GlobalLock(hNewDib);
		dwSize = GlobalSize(hNewDib);
		memcpy(lpSrcDIB, lpDIB, dwSize);
		GlobalUnlock(hDIB);
		GlobalUnlock(hNewDib);
	}
	else
	{
		lpSrcDIB = (LPBYTE)GlobalLock(hDIB);
		lpDIB = (LPBYTE)GlobalLock(hDib);
		dwSize = GlobalSize(hDib);
		memcpy(lpSrcDIB, lpDIB, dwSize);
		GlobalUnlock(hDIB);
		GlobalUnlock(hDib);
	}

	// cleanup
	GlobalFree(hDib);
	GlobalFree(hNewDib);

	// return
	WaitCursorEnd();
	return TRUE;
}