#include <vcl.h>
#pragma hdrstop
#include "math.h"
#include "Compressor.h"
#include "compress.h"
#include "Video.h"
#include "audio.h"
#pragma package(smart_init)
extern Compressor compressor;
extern Video video;
//---------------------------------------------------------------------------
Compressor::Compressor()
{
    initflag = false;
}
Compressor::~Compressor(){}
void Compressor::Init(HWND hWnd)
{
        int cc;
        compvar.lpbiIn = &video.bmpinfo;
        compvar.cbSize = sizeof(COMPVARS);
        if(!(cc = ICCompressorChoose(hWnd,ICMF_CHOOSE_DATARATE|ICMF_CHOOSE_KEYFRAME,compvar.lpbiIn,NULL,&compvar,"À£ÁY¿ï¶µ¹ï¸Ü²°")))
        {
                Application->MessageBox("ICCompressorChoose ERROR","Error",MB_OK);
        }
        initflag = true;
}
LPVOID Compressor::compress(LPVOID lpdata)
{

   DWORD dwInSize = ICCompressGetSize(compvar.hic, compvar.lpbiIn, compvar.lpbiOut);
  video.packet = ceil(dwInSize / 2000) ;
  delete [] compoutbuff;
  compoutbuff = new char[dwInSize];

  if(ICCompressBegin(compvar.hic ,compvar.lpbiIn ,compvar.lpbiOut) != ICERR_OK)
  {
        Application->MessageBox("ICCompressBegin ERROR","Error",MB_OK);
  }
  if(ICCompress(compvar.hic, compvar.dwFlags, &(compvar.lpbiOut->bmiHeader),compoutbuff, &(compvar.lpbiIn->bmiHeader), lpdata, NULL, NULL,1,0,0,NULL,NULL) != ICERR_OK)
  {
        Application->MessageBox("ICCompress ERROR","Error",MB_OK);
  }

  
    int times = ceil(dwInSize / 2000);
    char temp[2002];
    temp[0]='0';
	temp[1]='1';
	strcpy(temp+2, IntToStr((int)(compressor.compvar.hic)).c_str());
	strcpy(temp+6, IntToStr((int)(compressor.compvar.lpbiIn)).c_str());
	strcpy(temp+10, IntToStr((int)(compressor.compvar.lpbiOut)).c_str());
	temp[6]='\0';
	Form1->NMUDP3->SendBuffer(temp,2002,2002);
    for(num=0 ; num<times ; num++)
    {
        int shift = num * 2000;

        temp[0]='0';
        temp[1]='0';
        for (int j=0; j<2000; j++)
            temp[2+j]='\0';
        for (int j=0; j<2000; j++)
            temp[2+j]=compoutbuff[shift+j];
        Form1->NMUDP3->SendBuffer(temp,2002,2002);
    }
    return compoutbuff;

}
LPVOID Compressor::decompress(LPVOID lpdata)
{
    if(ICDecompressBegin(compvar.hic , compvar.lpbiOut, compvar.lpbiIn) != ICERR_OK)
    {
        Application->MessageBox("ICDecompressBegin ERROR","Error",MB_OK);
    }
    if(ICDecompress(compvar.hic, ICDECOMPRESS_UPDATE , &(compvar.lpbiOut->bmiHeader), lpdata, &(compvar.lpbiIn->bmiHeader) , framebuff) != ICERR_OK)
    {

        Application->MessageBox("ICDecompress ERROR","Error",MB_OK);
    }
    return framebuff;
}

void Compressor::send(LPVOID compresseddata)
{
        LPVOID decompresseddata ;
        decompresseddata = compressor.decompress(compresseddata);
        if(!DrawDibBegin(video.hdd,video.hdc,-1,-1,&video.bmpinfo.bmiHeader,video.CAPWIDTH,video.CAPHEIGHT,NULL))
        {
                Application->MessageBox("initDisplay DrawDibBegin Error!","Error",MB_OK);
        }
        if(!DrawDibDraw(video.hdd,video.hdc,100/*video.CAPWIDTH+30*/,100,video.CAPWIDTH,video.CAPHEIGHT,&video.bmpinfo.bmiHeader, decompresseddata
              ,0,0,video.CAPWIDTH,video.CAPHEIGHT,DDF_SAME_DRAW | DDF_SAME_HDC))
        {
                Application->MessageBox("DrawDibDraw ERROR","Error",MB_OK);
        }
}

