/* * Copyright (c) 1999-2000 University of California, Riverside. * Permission to copy is granted provided that this header remains * intact. This software is provided with no warranties. * * Version : 1.0 * Version : 2.0 (changed to run on PC) 2/18/01 * Version : 3.0 (changed to use as small variables as possible) 2/25/01 * */ /*--------------------------------------------------------------------------*/ // This file does a Discrete Cosine Transform (DCT) on the 8X8 matrix inBuffer // It then does the reconstruction of the result. // The reconstruction should be similar, but not exactly equal to inBuffer #pragma SMALL #include #include const char code COS_TABLE[8][8] = { {64, 62, 59, 53, 45, 35, 24, 12}, {64, 53, 24, -13, -46, -63, -60, -36}, {64, 35, -25, -63, -46, 12, 59, 53}, {64, 12, -60, -36, 45, 53, -25, -63}, {64, -13, -60, 35, 45, -54, -25, 62}, {64, -36, -25, 62, -46, -13, 59, -54}, {64, -54, 24, 12, -46, 62, -60, 35}, {64, -63, 59, -54, 45, -36, 24, -13} }; /* taken times constant 64 and 'integerized' */ const char ONE_OVER_SQRT_TWO = 45; /* taken time constant 64 and 'integerized' */ const unsigned char code inBuffer[8][8]= { { 100, 90, 80, 70, 0, 0, 0, 0 }, { 100, 0, 0, 0, 60, 0, 0, 0 }, { 100, 0, 0, 0, 60, 0, 0, 0 }, { 100, 0, 0, 70, 60, 0, 0, 0 }, { 100, 0, 0, 0, 60, 0, 0, 0 }, { 100, 0, 0, 0, 0, 50, 0, 0 }, { 100, 0, 0, 0, 0, 50, 0, 0 }, { 100, 90, 80, 70, 60, 50, 0, 0 } }; long xdata outBuffer[8][8]; /*--------------------------------------------------------------------------*/ unsigned char C (char h) { return h ? 64 : ONE_OVER_SQRT_TWO; /* taken time constant 64 and 'integerized' */ } /*--------------------------------------------------------------------------*/ long F(char u, char v, char img[8][8]) { long r; unsigned char x,y; r = 0; for(x=0; x<8; x++) { for (y=0; y<8; y++){ r = r + ((img[x][y] * COS_TABLE[x][u] * COS_TABLE[y][v]) >> 12); } } return (short) (r * C(u) * C(v) >> 14); } /*--------------------------------------------------------------------------*/ void CodecDoFdct(void) { unsigned char u, v; for(u=0; u<8; u++) { for(v=0; v<8; v++) { outBuffer[u][v] = F(u, v, inBuffer); } } } /*--------------------------------------------------------------------------*/ void Reconstruct (void) { char x, y, u, v; long xdata temp1 = 0, temp2 = 0; long xdata recsrct [8][8]; for (x=0; x<8; x++) { for (y=0; y<8; y++) { temp1 = 0; for (u=0; u<8; u++){ for (v=0; v<8; v++){ temp1 = temp1 + ((C(u) * C(v) * outBuffer[u][v] * COS_TABLE[x][u] * COS_TABLE[y][v]) >> 24) ; } } temp2 = temp1 >> 2; /* shifted to divide by 4 */ recsrct [x][y] = temp2; } } } /*--------------------------------------------------------------------------*/ void main (void) { P1 = 1; CodecDoFdct(); P1 = 2; Reconstruct(); P1 = 3; }