⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codec.cpp

📁 用VC編的數碼相機仿真程序 包括圖像的預處理 DCT變換 壓縮等 最后可用verify.exe做比較
💻 CPP
字号:
/*
* Copyright 2008, writen by shikosan
* File name: cntrl.cpp
* FDCT變換
* ibuffer holds original 8 x 8 block
* obuffer holds encoded 8 x 8 block
*/

#include <iostream.h>
#include "codec.h"
#include <math.h>

static short ibuffer[8][8], obuffer[8][8], idx;

const double PI = 3.1415926;

void CodecInitialize(void)
{ 
    idx = 0; 
}

// This function calls 64 times to fill ibuffer with original block
void CodecPushPixel(short p) 
{
    if( idx == 64 ) idx = 0;
    ibuffer[idx / 8][idx % 8] = p; 
    idx++;
}

// This function calls once to transform 8 x 8 block
void CodecDoFdct(void) 
{
    int x, y;
    for(x=0; x<8; x++) 
    {
        for(y=0; y<8; y++) 
			obuffer[x][y] = FDCT(x, y, ibuffer);
    }
		
    idx = 0;
}

// This function calls 64 times to retrieve encoded block from obuffer
short CodecPopPixel(void) 
{
    short p;
    if( idx == 64 ) idx = 0;
    p = obuffer[idx / 8][idx % 8]; idx++;
    return p;
}


// Only 64 possible inputs to COS, so table can be used to save performance time
// Floating-point values multiplied by 32,678 and rounded to nearest integer
// 32,678 chosen in order to store each value in 2 bytes of memory
static const int COS_TABLE[8][8] = 
{
  { 32768,  32138,  30273,  27245,  23170,  18204,  12539,   6392 },
  { 32768,  27245,  12539,  -6392, -23170, -32138, -30273, -18204 },
  { 32768,  18204, -12539, -32138, -23170,   6392,  30273,  27245 },
  { 32768,   6392, -30273, -18204,  23170,  27245, -12539, -32138 },
  { 32768,  -6392, -30273,  18204,  23170, -27245, -12539,  32138 },
  { 32768, -18204, -12539,  32138, -23170,  -6392,  30273, -27245 },
  { 32768, -27245,  12539,   6392, -23170,  32138, -30273,  18204 },
  { 32768, -32138,  30273, -27245,  23170, -18204,  12539,  -6392 }
};

static short ONE_OVER_SQRT_TWO = 23170;

static double COS(int xy, int uv) 
{
	return COS_TABLE[xy][uv] / 32768.0; 
}

static double C(int h) 
{ 
	return h ? 1.0 : ONE_OVER_SQRT_TWO / 32768.0;
}

// This function unrolls inner loop of summation
// and implements outer summation as two consecutive for loops
static short FDCT(int u, int v, short img[8][8]) 
{
    double s[8], r = 0; int x;
    for(x=0; x<8; x++) 
    {
       s[x] = img[x][0]*COS(0, v)+img[x][1]*COS(1, v) + 
              img[x][2]*COS(2, v)+img[x][3]*COS(3, v) + 
              img[x][4]*COS(4, v)+img[x][5]*COS(5, v) +
              img[x][6]*COS(6, v)+img[x][7]*COS(7, v);
    }
    for(x=0; x<8; x++) 
		r += s[x] * COS(x, u);
    return (short)(r * 0.25 * C(u) * C(v));
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -