// InspectionBuffer.cpp: implementation of the CSISBuffer class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "InspectionBuffer.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// void CSISBuffer::MemSet(BYTE val) { memset(GetDataAddress(), val, GetDataSize()); return; for(int i= 0; i< GetHeight(); i++) { memset(GetDataAddress(0, i), val, GetWidth()); } } void CSISBuffer::FlipUpDown() { int hh= GetHeight()/2; int eh= GetHeight()- 1; LPBYTE lpBuffer = new BYTE[hh*GetDataWidth()]; memcpy(lpBuffer, GetDataAddress(), hh*GetDataWidth()); CSISBuffer temp(lpBuffer, GetWidth(), hh); for (int i = 0; i GetWidth()) rect.right= GetWidth(); if(rect.bottom > GetHeight()) rect.bottom= GetHeight(); return rect; } int CSISBuffer::GetXDiff22(int x, int y, double xPitch) { int ret; double xRef= x+ xPitch; int iRef= (int)xRef; double r2= xRef- iRef; double r1= 1- r2; BYTE *pData= GetDataAddress(x, y); BYTE *pData2= GetDataAddress(iRef, y); ret= *pData+ *(pData+ 1); pData+= GetDataWidth(); ret+= *pData+ *(pData+ 1); ret-= (int)((*pData2)*r1+ *(pData2+ 1)+ *(pData2+2)*r2); pData2+= GetDataWidth(); ret-= (int)((*pData2)*r1+ *(pData2+ 1)+ *(pData2+2)*r2); return ret; } int CSISBuffer::GetYDiff22(int x, int y, double yPitch) { int ret; double yRef= y+ yPitch; int iRef= (int)yRef; double r2= yRef- iRef; double r1= 1- r2; BYTE *pData= GetDataAddress(x, y); BYTE *pData2= GetDataAddress(x, iRef); ret= *pData+ *(pData+ 1); pData+= GetDataWidth(); ret+= *pData+ *(pData+ 1); ret-= (int)(((*pData2)+ *(pData2+ 1))*r1); pData2+= GetDataWidth(); ret-= (int)(((*pData2)+ *(pData2+ 1))); pData2+= GetDataWidth(); ret-= (int)(((*pData2)+ *(pData2+ 1))*r2); return ret; } int CSISBuffer::GetDiff32(int x, int y, double xPitch) { int ret; double xRef= x+ xPitch; int iRef= (int) xRef; double pb= xRef- iRef; double pa= 1- pb; BYTE *pData= GetDataAddress(x, y); BYTE *pData2= GetDataAddress(iRef, y); ret= *pData+ *(pData+ 1)+ *(pData+2); pData+= GetDataWidth(); ret+= *pData+ *(pData+ 1)+ *(pData+2); ret-= (int)((*pData2)*pa+ *(pData2+ 1)+ *(pData2+2)+ (*(pData2+3))*pb); pData2+= GetDataWidth(); ret-= (int)((*pData2)*pa+ *(pData2+ 1)+ *(pData2+2)+ (*(pData2+3))*pb); return ret; } int CSISBuffer::GetDiff23(int x, int y, double yPitch) { int ret; double yRef= y+ yPitch; int iRef= (int) yRef; double pb= yRef- iRef; double pa= 1- pb; BYTE *pData= GetDataAddress(x, y); BYTE *pData2= GetDataAddress(x, iRef); ret= *pData+ *(pData+1); pData+= GetDataWidth(); ret+= *pData+ *(pData+1); pData+= GetDataWidth(); ret+= *pData+ *(pData+1); ret-= (int)((*pData2)*pa+ (*(pData2+1))*pa); pData2+= GetDataWidth(); ret-= *pData2+ *(pData2+1); pData2+= GetDataWidth(); ret-= *pData2+ *(pData2+1); pData2+= GetDataWidth(); ret-= (int)((*pData2)*pb+ (*(pData2+1))*pb); return ret; } BOOL CSISBuffer::CopyBtoA(CSISBuffer &aBuffer, int x, int y, CSISBuffer &bBuffer, CRect &rectFrom) { if(!aBuffer.IsValidBuffer()) return FALSE; if(!bBuffer.IsValidBuffer()) return FALSE; int xx= 0, yy= 0; int width, ey; CRect rect= rectFrom; rect.NormalizeRect(); rect= bBuffer.IntersectRect(rect); if(rectFrom.top < 0) y-= rectFrom.top; if(rectFrom.left < 0) x-= rectFrom.left; width= rect.Width(); ey= rect.Height(); if(x+ rect.Width() > aBuffer.GetWidth()) { width= aBuffer.GetWidth()- x; } if(x < 0) { xx= -x; width-= xx; } if(y < 0) { yy= -y; } if(y+ rect.Height() > aBuffer.GetHeight()) { ey= aBuffer.GetHeight()- y; } if(width < 0) return FALSE; for(;yy < ey; yy++) { memcpy(aBuffer.GetDataAddress(x+ xx, y+ yy), bBuffer.GetDataAddress(rect.left+ xx, rect.top+ yy), width); } return TRUE; } void CSISBuffer::RotateImage() { if(IsValidBuffer() == FALSE) return; BYTE cTemp; for(int h = 0; h < m_Height; h++) { for(int w = 0; w < m_Width / 2; w++) { cTemp = m_pData[w + h * m_DataWidth]; m_pData[w + h * m_DataWidth] = m_pData[(m_Width - 1 - w) + (m_Height - 1 - h) * m_DataWidth]; m_pData[(m_Width - 1 - w) + (m_Height - 1 - h) * m_DataWidth] = cTemp; } } } CBufferAttach::CBufferAttach(CString &name) : m_FileName(name) { m_bFileMade= FALSE; m_FileName= name; } CBufferAttach::~CBufferAttach() { if(m_pImageFileHeader) delete m_pImageFileHeader; } BOOL CBufferAttach::MakeFile() { m_pImageFileHeader= new stImageFileHeader; CFile file; if(! file.Open(m_FileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) { //file.Close(); return FALSE; } //file.Seek(0, CFile::begin); file.Write(&m_pImageFileHeader->fileHeader, m_pImageFileHeader->fileHeader.bfOffBits); file.Close(); m_bFileMade= TRUE; return TRUE; } void CBufferAttach::AttachToFile(CSISBuffer &buffer) { CFile file; if(! m_bFileMade) { MakeFile(); } int width= buffer.GetWidth(); int height= m_pImageFileHeader->GetHeight()+ buffer.GetHeight(); m_pImageFileHeader->ChangeSize(width, height); if(! file.Open(m_FileName, CFile::modeWrite | CFile::typeBinary)) { //file.Close(); return; } file.Seek(0, CFile::begin); file.Write(&m_pImageFileHeader->fileHeader, m_pImageFileHeader->fileHeader.bfOffBits); file.Seek(0, CFile::end); int wData= CSISBuffer::ChangeDataWidth(buffer.GetWidth()); for(int i= buffer.GetHeight()- 1; i >= 0; i--) { file.Write(buffer.GetDataAddress(0, i), wData); } file.Close(); } BOOL CCopyBuffer::FillBuffer(CSISBuffer& buffer, int x, int y) { int end= y+ GetHeight(); int yy; for(yy= 0; y< end; y++, yy++) { memcpy(GetDataAddress(0, yy), buffer.GetDataAddress(x, y), GetWidth()); } return TRUE; } COwnerBuffer::COwnerBuffer(int width, int height) : CSISBuffer(NULL, width, height) { m_DataSpace= 0; SetSize(width, height); } void COwnerBuffer::ReleaseSpace() { if(m_pData) delete[] m_pData; CSISBuffer::SetSize(0, 0); m_DataSpace= 0; m_pData= NULL; } BOOL COwnerBuffer::SetSize(int width, int height) { int tempwidth= CSISBuffer::ChangeDataWidth(width);//(width+ 3)/4*4; int space= tempwidth*height; if(space < 1) { CSISBuffer::SetSize(0, 0); return FALSE; } if(m_DataSpace < space) { ReleaseSpace(); }else { CSISBuffer::SetSize(width, height); return IsValidBuffer(); } if(m_pData == NULL) { m_pData= new BYTE[space+ 16]; m_DataSpace= space; } CSISBuffer::SetSize(width, height); return IsValidBuffer(); } COwnerBuffer::~COwnerBuffer() { if(m_pData) { delete[] m_pData; m_pData= NULL; } } /* void stImageFileHeader::ChangeSize(int width, int height)// void ChangeHeader(int width, int height) { infoHeader.biWidth = width; infoHeader.biHeight = height; stBufferSize bufferSize(width, height); infoHeader.biSizeImage = bufferSize.GetDataSize();//width*height*1; // fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256; fileHeader.bfSize = fileHeader.bfOffBits + infoHeader.biSizeImage; } void stImageFileHeader::ReadFromFile(CString &fileName) { CFile file; file.Open(fileName, CFile::modeCreate | CFile::typeBinary); ReadFromFile(file); file.Close(); } void stImageFileHeader::ReadFromFile(CFile &file) { file.Seek(0, SEEK_SET); file.Read(&fileHeader, sizeof(BITMAPFILEHEADER)); file.Read(&infoHeader, sizeof(BITMAPINFOHEADER)); file.Read(quad, sizeof(RGBQUAD)*256); ChangeSize(infoHeader.biWidth, infoHeader.biHeight); } */ void CDynamicBuffer::FillBuffer(CSISBuffer &buffer, CRect &rect) { SetSize(rect.Width(), rect.Height()); CCopyBuffer copyer(*this); copyer.FillBuffer(buffer, rect.left, rect.top); } CSISImageBuffer::CSISImageBuffer() :CSISBuffer(NULL, 0, 0) { m_pFileHeader= new stImageFileHeader; m_DataSpace= 0; } CSISImageBuffer::~CSISImageBuffer() { if(m_pData) delete[] m_pData; if(m_pFileHeader) delete m_pFileHeader; } BOOL CSISImageBuffer::SetSize(int width, int height) { int tempwidth= CSISBuffer::ChangeDataWidth(width);//(width+ 3)/4*4; int space= tempwidth*height; if(space < 1) return FALSE; if(m_DataSpace < space) { ReleaseImage(); } if(m_pData == NULL) { m_pData= new BYTE[space]; m_DataSpace= space; } CSISBuffer::SetSize(width, height); m_pFileHeader->ChangeSize(GetWidth(), GetHeight()); return IsValidBuffer(); } void CSISImageBuffer::ReleaseImage() { SetSize(0, 0); if(m_pData) delete[] m_pData; m_pData= NULL; } BOOL CSISImageBuffer::ReadFromFile(CString &fileName) { CFile file; if(file.Open(fileName, CFile::modeRead | CFile::typeBinary)) { m_pFileHeader->ReadFromFile(file); if(SetSize(m_pFileHeader->GetWidth(), m_pFileHeader->GetHeight())) { file.Seek(m_pFileHeader->GetDataOffset(), SEEK_SET); for(int i= GetHeight()-1; i>= 0; i--) { file.Read(GetDataAddress(0, i), GetDataWidth()); } //file.Read(m_pData, m_pFileHeader->GetDataSize()); } } else return FALSE; file.Close(); return TRUE; } CFrameSave_OneFile::CFrameSave_OneFile(CString &fileName_, int startFrame, int saveFrame, int width_, int height_) : idStartFrame(startFrame), nSaveFrame(saveFrame), width(width_), height(height_) , iSaveFrame(0) { pBufferAttach= new CBufferAttach(fileName_); } CFrameSave_OneFile::~CFrameSave_OneFile() { if(pBufferAttach) delete pBufferAttach; } void CFrameSave_OneFile::SaveFrame(int iFrame, CSISBuffer &frameBuffer) { if(iFrame < idStartFrame) return; if(iSaveFrame >= nSaveFrame) return; int id; id= iFrame- idStartFrame; if(id != iSaveFrame) { // ¹öÆÛ ÀúÀå ¼ø¼­°¡ ´Ù¸£³×?? } pBufferAttach->AttachToFile(frameBuffer); iSaveFrame++; }