#include <cstdlib>
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SZ_WIDTH 4000
#define MAX_SZ_HIGH 3000
#define SZ_RAW_WIDTH 2592
#define SZ_RAW_HEIGH 1944
//#define SZ_RAW_WIDTH 1280
//#define SZ_RAW_HEIGH 720
#define IDC_MAIN_TEXT 1001
#define RAW_SCALAR 1
#define RAW2RGB_SCALAR 6 // div(2^6)
//#define RAW_PTN CV_BayerBG2RGB //ov9710 1280x 720
#define RAW_PTN CV_BayerGR2RGB //MT9P401 5M 2592 x 1944
using namespace std;
IplImage *Ipl_Pic, *Ipl_Pic1, *Ipl_Pic2;
IplImage *Image1;
IplImage *Ipl_show,*Ipl_raw,*Ipl_raw8,*Ipl_gray;
DWORD *pFileRaw;
char *pByteRaw,*pByteRaw1;
short int *pWodRaw;
static void *vp,*rmp;
#define STR_FILE_RAW_NM "FILE0009.raw"
#define STR_FILE_GRAY_NM "FILE0009G.BMP"
#define STR_FILE_BMP_NM "FILE0009.BMP"
#define STR_FILE_JPG_NM "FILE0009.JPG"
/*
Input: string need end of 0x00.
return: total char(else 0x20)
*/
#define isDigital(x) (( x<='9')&&(x>='0'))
#define isXDigital(x) (( ( x<='9')&&(x>='0')) ||((x<='f')&&(x>='a') ))
#define isText(x) ((( x>='A')&&(x<='Z'))\
||(( x>='a')&&(x<='a')))
#define isSpace(x) (x == ' ')
#define isEscap(x) (x == 0x1b)
class clRawImgProc
{
public:
void vInit(void){
iImghi= SZ_RAW_HEIGH; //image Height, default is MT9P001, still
iImgWd= SZ_RAW_WIDTH; //image width, default is MT9P001, still
iRawClrPtn= CV_BayerGR2RGB; // RGB bayer patten
iRawBitRso = 14; //raw bit, A5 is 14bits.
vRawPtr = malloc(2* MAX_SZ_WIDTH* MAX_SZ_HIGH);
};
void vInitPtn(int iPtn){
iRawClrPtn= iPtn;
};
void SizeWd(int wd){
iImgWd= wd;
};
void SizeHi(int hi){
iImghi= hi;
};
int SizeHi(void){
return(iImghi);
};
int SizeWd(void){
return(iImgWd);
};
void RawAdrs(void *p)
{
vRawPtr = p;
};
void *RawAdrs(void)
{
return(vRawPtr);
}
int FilSz(void)
{
return(iImghi * iImgWd * ((iRawBitRso>8)?2:1) );
}
int showRaw(void);
private:
int iImghi;
int iImgWd;
int iRawClrPtn;
int iRawBitRso;
void *vRawPtr;
} cImg;
int clRawImgProc::showRaw(void)
{
}
int iEatSpace(char *p)
{
int i,j,len,k;
len = strlen(p);
for(j=0,i=0;j<len;j++)
{
if(!isSpace( *(p+j) )){ //check is space
*(p+i) = *(p+j);
i++;
} else if(i){
k=i-1;
if(*(p+k) !=0x00){
*(p+i) = 0x00; //insert string end. end of 0.
i++;
}
}
}
if(*(p+i-1) ==0x00) i--;
return(i);
}
//int str2u32(const char *str, unsigned int *value)
int str2u32(const char *str)
{
int i,sum;
// *value = 0;
sum=0;
if((strncmp(str,"0x",2)==0) ||
(strncmp(str,"0X",2)==0) )
{
str+=2;
while( *str != '\0'){
if( isXDigital(*str)){
if(isDigital(*str))
{
i = *str-'0';
}else{
i = *str-'a'+10;
}
}
str++;
sum = sum*16+i;
}
printf("\nsum=%d\n",sum);
}
else{
while( *str != '\0'){
if( isXDigital(*str)){
if(isDigital(*str))
{
i = *str-'0';
}else{
i = *str-'a'+10;
}
}
str++;
sum = sum*10+i;
}
printf("\nsum=%d\n",sum);
}
return(sum);
}
void vRaw2Show(void)
{
HANDLE hFile;
WORD *pwPtr;
DWORD i,j;
char *pBptr;
BOOL bSuccess=FALSE;
hFile = CreateFile( STR_FILE_RAW_NM, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
//pFileRaw = (DWORD *)GlobalAlloc(32, dwFileSize*2 );
pWodRaw= (short *) malloc(SZ_RAW_WIDTH*SZ_RAW_HEIGH*8);
pFileRaw = (DWORD *) pWodRaw;
pByteRaw = (char *)pWodRaw;
printf("\r\n CharP=%d, intP=%d, longP=%d",pByteRaw,pWodRaw,pFileRaw);
if(pFileRaw != NULL){
DWORD dwRead;
if(ReadFile(hFile, pFileRaw, dwFileSize, &dwRead, NULL))
{
//LoadFile_Callbak(pFileRaw,dwFileSize);
bSuccess = TRUE; // It worked!
pwPtr = (WORD *)pFileRaw;
}
//GlobalFree(pFileRaw);
}
}else{
MessageBox(NULL, "OpenFile", "Error",
MB_OK | MB_ICONEXCLAMATION);
return;
}
for(i=0;i<(SZ_RAW_HEIGH*SZ_RAW_WIDTH);i++){
pWodRaw[i]=pWodRaw[i]*RAW_SCALAR;
}
Ipl_Pic2->imageData = (char *)pByteRaw;
cvNamedWindow( "RawFile", 0 );
cvResizeWindow("RawFile",Ipl_Pic2->width/4,Ipl_Pic2->height/4);
cvShowImage( "RawFile", Ipl_Pic2 );
cvSaveImage(STR_FILE_GRAY_NM,Ipl_Pic2);
cvWaitKey(0);
if(1){
printf("\r\n nSize=%d",Ipl_Pic->nSize);
printf("\r\n Channels=%d",Ipl_Pic->nChannels);
printf("\r\n depth=%d",Ipl_Pic->depth);
printf("\r\n origin=%d",Ipl_Pic->origin);
printf("\r\n align=%d",Ipl_Pic->align);
printf("\r\n width=%d",Ipl_Pic->width);
printf("\r\n height=%d",Ipl_Pic->height);
printf("\r\n imageSize=%d",Ipl_Pic->imageSize);
printf("\r\n widthStep=%d",Ipl_Pic->widthStep);
printf("\r\n dataOrder=%d",Ipl_Pic->dataOrder);
}
for(i=0;i<(SZ_RAW_HEIGH*SZ_RAW_WIDTH);i++){
pWodRaw[i]=(pWodRaw[i]>>RAW2RGB_SCALAR);
}
for(i=0;i<(SZ_RAW_HEIGH*SZ_RAW_WIDTH);i++){
pByteRaw[i]=(short)(pWodRaw[i]&0x00ff);
}
Ipl_Pic->imageData = (char *)pByteRaw;
cvCvtColor(Ipl_Pic,Ipl_Pic1,RAW_PTN);
cvNamedWindow( "RawFile1", 0 );
cvResizeWindow("RawFile1",Ipl_Pic1->width/4,Ipl_Pic1->height/4);
cvShowImage( "RawFile1", Ipl_Pic1 );
cvSaveImage(STR_FILE_BMP_NM,Ipl_Pic1);
cvSaveImage(STR_FILE_JPG_NM,Ipl_Pic1);
cvWaitKey(0);
}
}
bool bRawShow(void *vp)
{
int k;
char *cp;
unsigned short *wp;
unsigned int i,szWd,szHi;
if(vp ==NULL) {
printf("\n Buffer is NULL");
return false;
}
rmp =cImg.RawAdrs();
szWd = cImg.SizeWd();
szHi = cImg.SizeHi();
printf("image Size = %d x %d",szWd,szHi);
if(!Ipl_raw)
{
Ipl_raw = cvCreateImage( cvSize( szWd, szHi ),IPL_DEPTH_16U,1);
Ipl_raw8 =cvCreateImage( cvSize( szWd, szHi ),IPL_DEPTH_8U,1);
Ipl_gray =cvCreateImage( cvSize( szWd, szHi ),IPL_DEPTH_8U,1);
Ipl_show =cvCreateImage( cvSize( szWd, szHi ),IPL_DEPTH_8U,3);
printf("\n CreateImage done");
}
cp = (char*)rmp;
wp = (unsigned short *)vp;
for(i=0;i<(szWd*szHi);i++)
{
unsigned int k;
k = *wp;
*cp = (unsigned char)((k >>6)&0x00ff);
cp++;
wp++;
}
printf("\n Trans from 16bit to 8bit");
Ipl_raw8->imageData = (char *)rmp;
cvCvtColor(Ipl_raw8,Ipl_show,RAW_PTN);
// cvNamedWindow( "ShowImage", 0 );
k=1;
while(1){
if((Ipl_show->width/k)<=1
评论0