ditherer_lk16.cc
上传用户:aoeyumen
上传日期:2007-01-06
资源大小:3329k
文件大小:3k
源码类别:

DVD

开发平台:

Unix_Linux

  1. #include "ditherer_lk16.hh"
  2. #include <iostream>
  3. unsigned short * DitherLK16::lookUpTable16=0;
  4. unsigned short * DitherLK16::lookUpTable15=0;
  5. DitherLK16::DitherLK16() {
  6.    _pixelSize=2;_bpp=0;
  7.    lookUpTable=0;
  8. }
  9. void DitherLK16::setBpp(int bpp) {
  10.    int i,j,k;
  11.    unsigned char *clp; // clip table
  12.    unsigned int r,v,b;
  13.    
  14.    Ditherer::setBpp(bpp);
  15.    if (bpp==16) {
  16.       lookUpTable=lookUpTable16;
  17.    } else if (bpp==15) {
  18.       lookUpTable=lookUpTable15;
  19.    } else {
  20.       cerr<<"only 15 and 16 are valid bpp for DitherLK16.setBpp()"<<endl;
  21.       exit (1);
  22.    }
  23.    
  24.    if (lookUpTable==0) {
  25.       clp=new unsigned char[1024];  // clip table
  26.       clp += 384;
  27.       for (i=-384; i<640; i++) 
  28.          clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
  29.       
  30.       lookUpTable=new unsigned short[1<<18];
  31.       if (bpp==16) {
  32.          lookUpTable16=lookUpTable;
  33.       } else if (bpp==15) {
  34.          lookUpTable15=lookUpTable;
  35.       }
  36.       
  37.       for (i=0;i<(1<<6);++i) { /* Y */
  38.          int Y=i<<2;
  39.          for(j=0;j<(1<<6);++j) { /* Cr */
  40.             int Cr=j<<2;
  41.             for(k=0;k<(1<<6);k++) { /* Cb */
  42.                int Cb=k<<2;
  43.                /*
  44.                  R = clp[(int)(*Y + 1.371*(*Cr-128))];  
  45.                  V = clp[(int)(*Y - 0.698*(*Cr-128) - 0.336*(*Cr-128))]; 
  46.                  B = clp[(int)(*Y++ + 1.732*(*Cb-128))];
  47.                */
  48.                r=clp[(Y*1000 + 1371*(Cr-128))/1000] >>3;
  49.                v=clp[(Y*1000 - 698*(Cr-128) - 336*(Cr-128))/1000] >> (bpp==16?2:3);
  50.                b=clp[(Y*1000 + 1732*(Cb-128))/1000] >> 3;
  51.                lookUpTable[i|(j<<6)|(k<<12)] = r | (v << 5) | (b <<  (bpp==16?11:10));
  52.             }
  53.          }
  54.       }
  55.       delete (clp-384);
  56.    }
  57. }
  58. DitherLK16::~DitherLK16() {
  59. }
  60. void DitherLK16::dither(unsigned char * tY,
  61.                         unsigned char * tCr,
  62.                         unsigned char * tCb,
  63.                         unsigned char * imgDst,
  64.                         int imgWidth,
  65.                         int imgHeight,
  66.                         int viewWidth,
  67.                         int viewHeight,
  68.                         int screenWidth) {
  69.    int i,j;
  70.    int shift=screenWidth-viewWidth;
  71.    int realX,realY;
  72.    unsigned short * ditheredImage=(unsigned short *)imgDst;
  73.    unsigned char * tY_l;
  74.    unsigned char * tCr_l;
  75.    unsigned char * tCb_l;
  76. #if 0
  77.    cerr<<"imgWidth="<<imgWidth<<" "
  78.        <<"imgHeight="<<imgHeight<<" "
  79.        <<"viewWidth="<<viewWidth<<" "
  80.        <<"viewHeight="<<viewHeight<<" "
  81.        <<"screenWidth="<<screenWidth
  82.        <<endl;
  83. #endif
  84.    if (_bpp==0) {
  85.       setBpp(16);
  86.    }
  87.    
  88.    for (j=0;j<viewHeight;j++) {
  89.       realY=j*imgHeight/viewHeight;
  90.       //cerr<< "j=" << j << " realY="<<realY<<"     ";
  91.       tY_l=tY+(realY*imgWidth);
  92.       tCr_l=tCr+(realY/2*imgWidth/2);
  93.       tCb_l=tCb+(realY/2*imgWidth/2);
  94.       for (i=0;i<viewWidth;i++) {
  95.          realX=i*imgWidth/viewWidth;
  96.          *ditheredImage++ =             
  97.             lookUpTable[(tY_l[realX]>>2)
  98.                        |((tCr_l[realX/2]>>2)<<6)
  99.                        |((tCb_l[realX/2]>>2)<<12)
  100.                ]
  101.                                         ;
  102.       }
  103.       ditheredImage+=shift;
  104.    }
  105. }
  106.   
  107.