【VC开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了VC开源代码-CompressJpegDlg.cpp的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
// CompressJpegDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CompressJpeg.h"
#include "CompressJpegDlg.h"
#include "math.h"
extern "C"
{
#include "jpeglib.h"
}
#include <setjmp.h>
#include <stdio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
void Scanline(unsigned char *pLine, int width, unsigned char *pOutBuf, int Rows)
{
int Offset = Rows * width * 3;
for (int i=0; i<width ; i++)
{
unsigned char unRed, unBlu, unGrn;
unsigned char* pRed, *pBlu, *pGrn;
unRed = *(pLine + i * 3 + 0);
unGrn = *(pLine + i * 3 + 1);
unBlu = *(pLine + i * 3 + 2);
pRed = pOutBuf + Offset + i * 3 + 0;
pGrn = pOutBuf + Offset + i * 3 + 1;
pBlu = pOutBuf + Offset + i * 3 + 2;
*pRed = unRed;
*pGrn = unGrn;
*pBlu = unBlu;
}
}
void ScanlineToRGB(unsigned char *pLine, int width, unsigned char *pOutBuf,int Rows)
{
int Offset = Rows * width * 3;
for (int i =0 ; i < width ; i++)
{
unsigned char unGray;
unsigned char* pRed, *pBlu, *pGrn;
unGray = *(pLine + i);
pRed = pOutBuf + Offset + i * 3;
pGrn = pOutBuf + Offset + i * 3 + 1;
pBlu = pOutBuf + Offset + i * 3 + 2;
*pRed = unGray;
*pGrn = unGray;
*pBlu = unGray;
}
}
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCompressJpegDlg dialog
CCompressJpegDlg::CCompressJpegDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCompressJpegDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCompressJpegDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
m_pBmpData = NULL;
}
void CCompressJpegDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCompressJpegDlg)
DDX_Control(pDX, IDC_DEMOZONE, m_Demo);
DDX_Control(pDX, IDC_FILENAME, m_FileName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCompressJpegDlg, CDialog)
//{{AFX_MSG_MAP(CCompressJpegDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BROWN, OnBrown)
ON_BN_CLICKED(IDC_SAVEBMP, OnSaveBmp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCompressJpegDlg message handlers
BOOL CCompressJpegDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CCompressJpegDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCompressJpegDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
if (m_pBmpData)
{
BITMAPINFO bInfo;
bInfo.bmiHeader = bmiHeader;
CDC*pDC = m_Demo.GetDC();
CRect rc;
m_Demo.GetClientRect(rc);
StretchDIBits(pDC->m_hDC,0,0,rc.Width(),rc.Height(),1,1,m_JpegWidth,m_JpegHeight,m_pBmpData,&bInfo,DIB_RGB_COLORS,SRCCOPY);
pDC->DeleteDC();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCompressJpegDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//根据JPEG的文件名称获得解压后的数据
unsigned char* CCompressJpegDlg::GetJpegData(LPCSTR lpstrFileName,int &bmpWidth,int &bmpHeight)
{
//定义解压信息
jpeg_decompress_struct DeComInfo;
jpeg_error_mgr Error;
//定义缓冲区
FILE* pFile;
JSAMPARRAY pBuffer;
int rows;
//打开JPEG文件
if ((pFile = fopen(lpstrFileName, "rb")) == NULL)
{
CString str ="打开文件";
str += lpstrFileName;
str+="出错!";
MessageBox(str,"提示");
return NULL;
}
//为JPEG文件解压对象分配内存并对其初始化
DeComInfo.err = jpeg_std_error(&Error);
jpeg_create_decompress(&DeComInfo);
//设定数据源
jpeg_stdio_src(&DeComInfo, pFile);
//读取JPEG文件参数
(void) jpeg_read_header(&DeComInfo, TRUE);
//开始解压
(void) jpeg_start_decompress(&DeComInfo);
unsigned char *pData;
pData=new unsigned char[DeComInfo.output_width * 3 * DeComInfo.output_height];
if (pData==NULL)
{
jpeg_destroy_decompress(&DeComInfo);
fclose(pFile);
return NULL;
}
bmpWidth = DeComInfo.output_width;
bmpHeight = DeComInfo.output_height
上一篇:
CompressJpeg.cpp
下一篇:
科研管理杂志简介