+++ /dev/null
-/*M///////////////////////////////////////////////////////////////////////////////////////\r
-//\r
-// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
-//\r
-// By downloading, copying, installing or using the software you agree to this license.\r
-// If you do not agree to this license, do not download, install,\r
-// copy or use the software.\r
-//\r
-//\r
-// Intel License Agreement\r
-// For Open Source Computer Vision Library\r
-//\r
-// Copyright (C) 2000, Intel Corporation, all rights reserved.\r
-// Third party copyrights are property of their respective owners.\r
-//\r
-// Redistribution and use in source and binary forms, with or without modification,\r
-// are permitted provided that the following conditions are met:\r
-//\r
-// * Redistribution's of source code must retain the above copyright notice,\r
-// this list of conditions and the following disclaimer.\r
-//\r
-// * Redistribution's in binary form must reproduce the above copyright notice,\r
-// this list of conditions and the following disclaimer in the documentation\r
-// and/or other materials provided with the distribution.\r
-//\r
-// * The name of Intel Corporation may not be used to endorse or promote products\r
-// derived from this software without specific prior written permission.\r
-//\r
-// This software is provided by the copyright holders and contributors "as is" and\r
-// any express or implied warranties, including, but not limited to, the implied\r
-// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
-// In no event shall the Intel Corporation or contributors be liable for any direct,\r
-// indirect, incidental, special, exemplary, or consequential damages\r
-// (including, but not limited to, procurement of substitute goods or services;\r
-// loss of use, data, or profits; or business interruption) however caused\r
-// and on any theory of liability, whether in contract, strict liability,\r
-// or tort (including negligence or otherwise) arising in any way out of\r
-// the use of this software, even if advised of the possibility of such damage.\r
-//\r
-//M*/\r
-\r
-//\r
-// Image.cpp: implementation of the CImage class.\r
-//\r
-//////////////////////////////////////////////////////////////////////\r
-\r
-#include "stdafx.h"\r
-#include "Image.h"\r
-#include "vlgrfmts.h"\r
-#include "cv.h"\r
-#include <assert.h>\r
-\r
-#ifdef _DEBUG\r
-#undef THIS_FILE\r
-static char THIS_FILE[]=__FILE__;\r
-#define new DEBUG_NEW\r
-#endif\r
-\r
-//////////////////////////////////////////////////////////////////////\r
-// Construction/Destruction\r
-//////////////////////////////////////////////////////////////////////\r
-\r
-CImage::CImage()\r
-{\r
- Clear();\r
-}\r
-\r
-void CImage::Clear()\r
-{ \r
- memset( &m_img, 0, sizeof(m_img));\r
-\r
- m_memDC = 0; \r
- m_old = 0;\r
-}\r
-\r
-void CImage::Destroy()\r
-{\r
- if( m_memDC )\r
- {\r
- DeleteObject( SelectObject( m_memDC, m_old ));\r
- DeleteDC( m_memDC );\r
- }\r
- Clear();\r
-}\r
-\r
-CImage::~CImage()\r
-{\r
- Destroy();\r
-}\r
-\r
-\r
-void FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp )\r
-{\r
- assert( bmi && width > 0 && height > 0 &&\r
- (bpp == 8 || bpp == 24 || bpp == 32) );\r
-\r
- BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);\r
-\r
- memset( bmih, 0, sizeof(*bmih));\r
- bmih->biSize = sizeof(BITMAPINFOHEADER); \r
- bmih->biWidth = width;\r
- bmih->biHeight = -abs(height);\r
- bmih->biPlanes = 1; \r
- bmih->biBitCount = bpp;\r
- bmih->biCompression = BI_RGB;\r
-\r
- if( bpp == 8 )\r
- {\r
- RGBQUAD* palette = bmi->bmiColors;\r
- int i;\r
- for( i = 0; i < 256; i++ )\r
- {\r
- palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;\r
- palette[i].rgbReserved = 0;\r
- }\r
- }\r
-}\r
-\r
-\r
-bool CImage::Create( int w, int h, int bpp )\r
-{\r
- char buffer[sizeof(BITMAPINFOHEADER) + 1024];\r
- BITMAPINFO* bmi = (BITMAPINFO*)buffer;\r
- void* data = 0;\r
- int new_step = (w*(bpp/8) + 3) & -4;\r
- \r
- assert( bpp == 8 || bpp == 24 || bpp == 32 );\r
-\r
- if( (m_img.depth & 255)*m_img.nChannels == bpp && \r
- m_img.width == w && m_img.height == h )\r
- {\r
- return true;\r
- }\r
-\r
- Destroy();\r
- \r
- m_memDC = CreateCompatibleDC(0);\r
- FillBitmapInfo( bmi, w, h, bpp );\r
-\r
- HBITMAP hbmp = CreateDIBSection( m_memDC, bmi, DIB_RGB_COLORS, &data, 0, 0 );\r
- if( !hbmp )\r
- {\r
- DeleteDC( m_memDC );\r
- m_memDC = 0;\r
- }\r
- else\r
- {\r
- BITMAP bmp;\r
- m_old = SelectObject( m_memDC, hbmp );\r
-\r
- GetObject( hbmp, sizeof(bmp), &bmp );\r
-\r
- /* prepare IPL header */\r
- memset( &m_img, 0, sizeof(m_img));\r
- m_img.nSize = sizeof( m_img );\r
- m_img.nChannels = bpp/8;\r
- m_img.depth = IPL_DEPTH_8U;\r
- strncpy( m_img.colorModel, bpp > 8 ? "RGB\0" : "GRAY", 4 );\r
- strncpy( m_img.channelSeq, bpp > 8 ? "BGR\0" : "GRAY", 4 );\r
- m_img.align = 4;\r
- m_img.width = w;\r
- m_img.height = abs(h);\r
- m_img.roi = 0;\r
- m_img.widthStep = (w*(bpp/8) + 3)& -4;\r
- m_img.imageSize = m_img.widthStep*m_img.height;\r
- m_img.imageData = m_img.imageDataOrigin = (char*)bmp.bmBits;\r
-\r
- //iplSetBorderMode( &m_img, IPL_BORDER_REPLICATE, IPL_SIDE_ALL, 0 ); \r
- }\r
- return m_old != 0;\r
-}\r
-\r
-\r
-void CImage::CopyOf( CImage& image, int desired_color )\r
-{\r
- IplImage* img = image.GetImage();\r
- if( img )\r
- {\r
- CopyOf( img, desired_color );\r
- }\r
-}\r
-\r
-void CImage::CopyOf( IplImage* img, int desired_color )\r
-{\r
- if( img )\r
- {\r
- int color = desired_color;\r
- if( color < 0 ) color = img->nChannels > 1;\r
- Create( iplWidth( img ), iplHeight( img ), color ? 24 : 8 );\r
- if( m_img.nChannels == img->nChannels )\r
- {\r
- if( img->origin != m_img.origin )\r
- {\r
- long OldOrg = img->origin;\r
- img->origin = m_img.origin;\r
- cvMirror( img, &m_img, 0 );\r
- img->origin = OldOrg;\r
- }\r
- else\r
- {\r
- cvCopy( img, &m_img );\r
- }\r
- }\r
- else\r
- {\r
- CImage tmp_image;\r
- \r
- tmp_image.Create( m_img.width, m_img.height, img->nChannels*8 );\r
-\r
- IplImage* tmp_img = tmp_image.GetImage();\r
- if( img->origin != m_img.origin )\r
- {\r
- long OldOrg = img->origin;\r
- img->origin = tmp_img->origin;\r
- cvMirror( img, tmp_img, 0 );\r
- img->origin = OldOrg;\r
- }\r
- else\r
- {\r
- cvCopy( img, tmp_img );\r
- }\r
-\r
- if( color )\r
- cvCvtColor( tmp_img, &m_img, CV_GRAY2BGR );\r
- else\r
- cvCvtColor( tmp_img, &m_img, CV_BGR2GRAY );\r
- }\r
- }\r
-}\r
-\r
-#if 0\r
-static HINSTANCE hdll = 0;\r
-static int (__cdecl *gr_fmt_find_filter)( const char* file_name ) = 0; \r
-static int (__cdecl *gr_fmt_read_header)( int filter, int* width, int* height, int* color ) = 0;\r
-static int (__cdecl *gr_fmt_read_data)( int filter, void* data, int pitch, int color ) = 0;\r
-static int (__cdecl *gr_fmt_close_filter)( int filter ) = 0;\r
-static int (__cdecl *gr_fmt_write_image)( void* data, int pitch,\r
- int width, int height, int color,\r
- const char* filename, const char* format ) = 0;\r
-\r
-bool LoadGrFmtLib()\r
-{\r
- if( hdll != 0 ) return true;\r
-\r
- // load image formats dll\r
- hdll = LoadLibrary( "vlgrfmts.dll");\r
- if( !hdll ) return false;\r
-\r
- (FARPROC&)gr_fmt_find_filter = GetProcAddress( hdll, "gr_fmt_find_filter" );\r
- (FARPROC&)gr_fmt_read_header = GetProcAddress( hdll, "gr_fmt_read_header" );\r
- (FARPROC&)gr_fmt_read_data = GetProcAddress( hdll, "gr_fmt_read_data" );\r
- (FARPROC&)gr_fmt_close_filter= GetProcAddress( hdll, "gr_fmt_close_filter" );\r
- (FARPROC&)gr_fmt_write_image = GetProcAddress( hdll, "gr_fmt_write_image" );\r
-\r
- if( !gr_fmt_find_filter || !gr_fmt_read_header || !gr_fmt_read_data )\r
- {\r
- FreeLibrary( hdll );\r
- hdll = 0;\r
- return false;\r
- }\r
- return true;\r
-}\r
-#endif\r
-\r
-bool CImage::Load( const char* filename, int desired_color )\r
-{\r
- int filter = 0;\r
- int width = 0, height = 0;\r
- bool result = false;\r
- int color = 0;\r
- \r
- if( gr_fmt_find_filter == 0 ) return false;\r
-\r
- if( !(filter = gr_fmt_find_filter( filename ))) goto exit;\r
- if( !gr_fmt_read_header( filter, &width, &height, &color )) goto exit;\r
-\r
- color = desired_color >= 0 ? desired_color : color > 0;\r
- \r
- Create( width, height, color ? 24 : 8);\r
- if( m_memDC == 0 ) goto exit;\r
-\r
- result = gr_fmt_read_data( filter, m_img.imageData, m_img.widthStep, color ) != 0;\r
-exit:\r
- gr_fmt_close_filter( filter );\r
- return result;\r
-}\r
-\r
-\r
-\r
-bool CImage::LoadRect( const char* filename,\r
- int desired_color, RECT r )\r
-{\r
- int filter = 0;\r
- int width = 0, height = 0;\r
- bool result = false;\r
- int color = 0;\r
- int r_width = r.right - r.left;\r
- int r_height = r.bottom - r.top;\r
- int tmp_step = 0;\r
- char* tmp = 0;\r
- \r
- if( r_width < 0 || r_height < 0 ) return false;\r
- if( gr_fmt_find_filter == 0 ) return false;\r
-\r
- if( !(filter = gr_fmt_find_filter( filename ))) goto exit;\r
- if( !gr_fmt_read_header( filter, &width, &height, &color )) goto exit;\r
-\r
- if( r_width == 0 || r_height == 0 )\r
- {\r
- r_width = width;\r
- r_height = height;\r
- }\r
-\r
- if( (unsigned)r.left >= (unsigned)width ||\r
- (unsigned)r.top >= (unsigned)height ||\r
- (unsigned)r.right >= (unsigned)width ||\r
- (unsigned)r.bottom >= (unsigned)height ) goto exit;\r
-\r
- color = desired_color >= 0 ? desired_color : color > 0;\r
- \r
- Create( r_width, r_height, color ? 24 : 8);\r
- if( m_memDC == 0 ) goto exit;\r
-\r
- if( r.left == 0 && r.top == 0 &&\r
- (r_width == 0 || r_width == width) &&\r
- (r_height == 0 || r_height == height))\r
- {\r
- tmp = m_img.imageData;\r
- tmp_step = m_img.widthStep;\r
- }\r
- else\r
- {\r
- tmp_step = (width*m_img.nChannels + 3) & -4;\r
- tmp = (char*)malloc( tmp_step * height );\r
- if( !tmp ) goto exit;\r
- }\r
-\r
- result = gr_fmt_read_data( filter, tmp, tmp_step, color ) != 0;\r
-\r
- if( result && tmp != m_img.imageData )\r
- {\r
- int y;\r
- for( y = 0; y < r_height; y++ )\r
- {\r
- memcpy( m_img.imageData + y*m_img.widthStep,\r
- tmp + (r.top + y)*tmp_step + r.left*m_img.nChannels,\r
- r_width * m_img.nChannels );\r
- }\r
- }\r
-\r
-exit:\r
- gr_fmt_close_filter( filter );\r
- if( tmp != 0 && tmp != m_img.imageData ) free( tmp );\r
- return result;\r
-}\r
-\r
-\r
-bool CImage::Save( const char* filename )\r
-{\r
- if( !m_memDC || (m_img.nChannels != 3 && m_img.nChannels != 1) ||\r
- m_img.depth != IPL_DEPTH_8U ) return false;\r
-\r
- return gr_fmt_write_image( m_img.imageData, m_img.widthStep,\r
- iplWidth( &m_img ), iplHeight( &m_img ),\r
- m_img.nChannels == 3, filename, "BMP" ) != 0;\r
-}\r
-\r
-\r
-IplImage* CImage::GetImage()\r
-{\r
- return m_memDC != 0 ? &m_img : 0;\r
-}\r
-\r
-\r
-HDC CImage::GetDC()\r
-{\r
- return m_memDC;\r
-}\r
-\r
-\r
-void CImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )\r
-{\r
- if( m_img.width > 0 )\r
- {\r
- uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];\r
- BITMAPINFO* bmi = (BITMAPINFO*)buffer;\r
- int bmp_w = Width();\r
- int bmp_h = Height();\r
-\r
- FillBitmapInfo( bmi, bmp_w, bmp_h, m_img.nChannels*8 );\r
-\r
- int sw = MAX( MIN( bmp_w - from_x, w ), 0 );\r
- int sh = MAX( MIN( bmp_h - from_y, h ), 0 );\r
-\r
- int res = SetDIBitsToDevice(\r
- dc, // handle to DC\r
- x, // x-coord of destination upper-left corner\r
- y, // y-coord of destination upper-left corner \r
- sw, // source rectangle width\r
- sh, // source rectangle height\r
- from_x, // x-coord of source lower-left corner\r
- from_y, // y-coord of source lower-left corner\r
- from_y, // first scan line in array\r
- sh, // number of scan lines\r
- m_img.imageData + from_y*m_img.widthStep/* +\r
- from_x*m_img.nChannels*/, // array of DIB bits\r
- (BITMAPINFO*)bmi, // bitmap information\r
- DIB_RGB_COLORS ); // RGB or palette indexes\r
- }\r
-}\r
-\r
-void CImage::Fill( COLORREF color )\r
-{\r
- if( m_memDC )\r
- {\r
- HBRUSH br = CreateSolidBrush( color );\r
- RECT rect;\r
- GetClipBox( m_memDC, &rect );\r
- FillRect( m_memDC, &rect, br );\r
- DeleteObject( br );\r
- }\r
-}\r
-\r
-void CImage::DrawToHDC( HDC hDCDst, RECT* pDstRect ) \r
-{\r
- HDC hDCSrc = GetDC();\r
-\r
- if( pDstRect == NULL ) return;\r
- if( hDCDst == NULL ) return;\r
- if( hDCSrc == NULL ) return;\r
-\r
- if( Width() > pDstRect->right - pDstRect->left )\r
- {\r
- SetStretchBltMode(\r
- hDCDst, // handle to device context\r
- HALFTONE );\r
- }\r
- else\r
- {\r
- SetStretchBltMode(\r
- hDCDst, // handle to device context\r
- COLORONCOLOR );\r
- }\r
-\r
- ::StretchBlt( \r
- hDCDst,\r
- pDstRect->left, pDstRect->top,\r
- pDstRect->right - pDstRect->left, pDstRect->bottom - pDstRect->top,\r
- hDCSrc,\r
- m_img.roi ? m_img.roi->xOffset:0 ,\r
- m_img.roi ? m_img.roi->yOffset:0,\r
- Width(),\r
- Height(),\r
- SRCCOPY );\r
-}\r