Click here to Skip to main content
15,891,607 members
Articles / Desktop Programming / MFC

FreeImage Display Demo

Rate me:
Please Sign up or sign in to vote.
3.98/5 (20 votes)
4 Jun 2008Public Domain17 min read 97.2K   10.4K   54  
How to display a bitmap in your MFC SDI application using FreeImage. Various rescaling algorithms considered.
//	FreeImage Display Demo v3.1

//	Copyright (C) 2008 monday2000@yandex.ru
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//	http://www.gnu.org/copyleft/gpl.html

// fi_testDoc.h : interface of the CFi_testDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_FI_TESTDOC_H__6E1B28EB_2A56_11DC_82F4_CF7D89946643__INCLUDED_)
#define AFX_FI_TESTDOC_H__6E1B28EB_2A56_11DC_82F4_CF7D89946643__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "FreeImage.h"

extern FIBITMAP *  
FreeImage_Rescale2(FIBITMAP *src, int dst_width, int dst_height, FREE_IMAGE_FILTER filter, BYTE* pBits); 


class CFi_testDoc : public CDocument
{
protected: // create from serialization only
	CFi_testDoc();
	DECLARE_DYNCREATE(CFi_testDoc)

// Attributes
public:

// Operations
public:

FIBITMAP* m_dib;

FIBITMAP* m_dib2;

FIBITMAP* GenericLoader(const char* lpszPathName, int flag);

BITMAPINFO* m_pbi;

BITMAPINFOHEADER* m_pbih;

CString m_strFilename;

unsigned m_zdib_width;

unsigned m_zdib_height;

unsigned m_dib_w;

unsigned m_dib_h;


BYTE* m_pBits;

CBitmap m_OffscreenBitmap;

CBitmap* m_pOldBitmap;

CDC m_dcOffscreen;

int m_NoEraseFlag;


BOOL OpenBitmapFile(CString FileName);

BOOL m_MRU_CalledFlag;

double m_zoom;

double m_zoom_fit;

BOOL DisplayBitmap(double zoom);

unsigned m_view_init_height, m_view_init_width;

CPoint m_OffscreenSize;

BOOL m_fit_height;

// Resampling functions

BOOL FI_Rescale2(FIBITMAP *src, int dst_width, int dst_height, FREE_IMAGE_FILTER filter, int bpp_dst, CPoint OffscreenSize, BYTE* pBits);

BOOL RescalePnm(FIBITMAP* src_dib, UINT nWidth, UINT nHeight, CPoint OffscreenSize, BYTE* pBits);

void horizontal_scale(const BYTE* inputxelrow, BYTE* newxelrow, UINT cols, UINT newcols, UINT sxscale, int btpp);

BOOL RescalePnm8(FIBITMAP* src_dib, unsigned nWidth, unsigned nHeight, CPoint OffscreenSize, BYTE* pBits, RGBQUAD *dst_pal);

void horizontal_scale8(const BYTE* inputxelrow, BYTE* newxelrow, UINT cols, UINT newcols, UINT sxscale);

BYTE* FI_GetScanLine(BYTE* pBits, unsigned pitch, int scanline);

unsigned m_ResampleMode;

BOOL LeptonicaScaleBySamplingLow(FIBITMAP* src_dib, unsigned wd, unsigned hd, CPoint OffscreenSize, BYTE* pBits, RGBQUAD *dst_pal);

BOOL LeptonicaScaleSmoothLow(FIBITMAP* src_dib, unsigned wd, unsigned hd,  CPoint OffscreenSize, unsigned size, BYTE* pBits, RGBQUAD *dst_pal);

BOOL LeptonicaScaleLILow(FIBITMAP* src_dib, unsigned wd, unsigned hd, CPoint OffscreenSize, BYTE  *pBits, RGBQUAD *dst_pal, BOOL bInterpolate);

void Get1BitPixel(BYTE* line, int x, int color_mode, BYTE* value);


// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CFi_testDoc)
	public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CFi_testDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:	

// Generated message map functions
public: // <- here was "protected" originally. "Public" is made to
// allow the "void CFi_testView::OnLButtonDblClk(UINT nFlags, CPoint point)" method
	//{{AFX_MSG(CFi_testDoc)
	afx_msg void OnFileOpen();
	afx_msg void OnFileClose();
	afx_msg BOOL OnOpenRecentFile(UINT nID);
	afx_msg void OnUpdateFileSaveAs(CCmdUI* pCmdUI);
	afx_msg void OnUpdateFileClose(CCmdUI* pCmdUI);
	afx_msg void OnUpdateButtonFit(CCmdUI* pCmdUI);
	afx_msg void OnButtonFit();
	afx_msg void OnFileSaveAs();
	afx_msg void OnScaleNoFilter();
	afx_msg void OnUpdateScaleNoFilter(CCmdUI* pCmdUI);
	afx_msg void OnScaleLowpass();
	afx_msg void OnUpdateScaleLowpass(CCmdUI* pCmdUI);
	afx_msg void OnScaleNearest();
	afx_msg void OnUpdateScaleNearest(CCmdUI* pCmdUI);
	afx_msg void OnScaleLinear();
	afx_msg void OnUpdateScaleLinear(CCmdUI* pCmdUI);
	afx_msg void OnScalePnm();
	afx_msg void OnUpdateScalePnm(CCmdUI* pCmdUI);
	afx_msg void OnScaleBilinear();
	afx_msg void OnUpdateScaleBilinear(CCmdUI* pCmdUI);
	afx_msg void OnScaleBox();
	afx_msg void OnUpdateScaleBox(CCmdUI* pCmdUI);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_FI_TESTDOC_H__6E1B28EB_2A56_11DC_82F4_CF7D89946643__INCLUDED_)

#define GRAPH_FILES_FILTER "Supported graphical formats \
|*.bmp;*.cut;*.dds;*.g3; \
*.gif;*.hdr;*.ico;*.iff;*.lbm;*.jng;*.jpg;*.jif;*.jpeg; \
*.jpe;*.koa;*.mng;*.pbm;*.pcd;*.pcx;*.pgm;*.png;*.ppm; \
*.psd;*.ras;*.sgi;*.tga;*.targa;*.tif;*.tiff;*.wap;*.wbmp; \
*.wbm;*.xbm;*.xpm| \
Windows or OS/2 Bitmap (*.bmp)|*.bmp| \
Dr. Halo (*.cut)|*.cut| \
DirectX Surface (*.dds)|*.dds| \
Raw fax format CCITT G.3 (*.g3)|*.g3| \
Graphics Interchange Format (*.gif)|*.gif| \
High Dynamic Range (*.hdr)|*.hdr| \
Windows Icon (*.ico)|*.ico| \
IFF Interleaved Bitmap (*.iff,*.lbm)|*.iff;*.lbm| \
JPEG Network Graphics (*.jng)|*.jng| \
JPEG-JFIF Compliant (*.jpg,*.jif,*.jpeg,*.jpe)|*.jpg;*.jif;*.jpeg;*.jpe| \
C64 Koala Graphics (*.koa)|*.koa| \
Multiple Network Graphics (*.mng)|*.mng| \
Portable Bitmap (ASCII) (*.pbm)|*.pbm| \
Portable Bitmap (RAW) (*.pbm)|*.pbm| \
Kodak PhotoCD (*.pcd)|*.pcd| \
Zsoft Paintbrush (*.pcx)|*.pcx| \
Portable Greymap (ASCII) (*.pgm)|*.pgm| \
Portable Greymap (RAW) (*.pgm)|*.pgm| \
Portable Network Graphics (*.png)|*.png| \
Portable Pixelmap (ASCII) (*.ppm)|*.ppm| \
Portable Pixelmap (RAW) (*.ppm)|*.ppm| \
Adobe Photoshop (*.psd)|*.psd| \
Sun Raster Image (*.ras)|*.ras| \
Silicon Graphics SGI image format (*.sgi)|*.sgi| \
Truevision Targa (*.tga,*.targa)|*.tga;*.targa| \
Tagged Image File Format (*.tif,*.tiff)|*.tif;*.tiff| \
Wireless Bitmap (*.wap,*.wbmp,*.wbm)|*.wap;*.wbmp;*.wbm| \
X11 Bitmap Format (*.xbm)|*.xbm| \
X11 Pixmap Format (*.xpm)|*.xpm| \
All files (*.*)|*.*||"

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions