Click here to Skip to main content
15,898,010 members
Articles / Mobile Apps / Windows Mobile

A Picture Viewer for the Pocket PC 2002

Rate me:
Please Sign up or sign in to vote.
4.79/5 (21 votes)
2 Nov 2003CPOL4 min read 499.3K   685   43  
Putting imgdecmp.lib to work with a few extras.
// PicViewDoc.cpp : implementation of the CPicViewDoc class
//

#include "stdafx.h"
#include "PicView.h"

#include "PicViewDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


struct ReadBmp
{
	CFile*	pFile;		// Opened file pointer.
	DWORD	dwSize;		// Total size of image in file.
};


//---------------------------------------------------------------------------
//
//	CPicViewDoc
//
//---------------------------------------------------------------------------


IMPLEMENT_DYNCREATE(CPicViewDoc, CDocument)

BEGIN_MESSAGE_MAP(CPicViewDoc, CDocument)
	//{{AFX_MSG_MAP(CPicViewDoc)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_MINUS, OnUpdateZoomMinus)
	ON_COMMAND(ID_ZOOM_MINUS, OnZoomMinus)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_PLUS, OnUpdateZoomPlus)
	ON_COMMAND(ID_ZOOM_PLUS, OnZoomPlus)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//---------------------------------------------------------------------------
//
//	CPicViewDoc construction/destruction
//
//---------------------------------------------------------------------------


CPicViewDoc::CPicViewDoc()
:	m_hBmp		(NULL),
	m_nWidth	(0),
	m_nHeight	(0),
	m_nZoom		(zoomDef)
{
}


CPicViewDoc::~CPicViewDoc()
{
	if(m_hBmp)
		DeleteObject(m_hBmp);
}


BOOL CPicViewDoc::OnNewDocument()
{
	if(!CDocument::OnNewDocument())
		return FALSE;

	//
	// Bitmap initialization
	//
	if(m_hBmp)
	{
		DeleteObject(m_hBmp);
		m_hBmp = NULL;
	}

	return TRUE;
}


//---------------------------------------------------------------------------
//
//	CPicViewDoc serialization
//
//---------------------------------------------------------------------------


void CPicViewDoc::Serialize(CArchive& ar)
{
	BYTE				szBuffer[4096] = {0};
	DecompressImageInfo	dii;
	HDC					hDC;
	HRESULT				hr;
	ReadBmp				rbmp;
	CFile*				pFile;
	BITMAP				bmp;

	if(ar.IsStoring())
	{
		// Do nothing: this is a viewer
	}
	else
	{
		pFile = ar.GetFile();

		//
		// Create the reader structure
		//
		rbmp.pFile	= pFile;
		rbmp.dwSize	= pFile->GetLength();

		hDC = GetDC(NULL);

		dii.dwSize					= sizeof(dii);
		dii.pbBuffer				= szBuffer;
		dii.dwBufferMax				= sizeof(szBuffer);
		dii.dwBufferCurrent			= 0;
		dii.phBM					= &m_hBmp;
		dii.ppImageRender			= NULL;
		dii.iBitDepth				= GetDeviceCaps(hDC,BITSPIXEL);
		dii.lParam					= (LPARAM) &rbmp;
		dii.hdc						= hDC;
		dii.iScale					= zoomDef;
		dii.iMaxWidth				= 10000;
		dii.iMaxHeight				= 10000;
		dii.pfnGetData				= GetImageData;
		dii.pfnImageProgress		= ImageProgress;
		dii.crTransparentOverride	= (UINT) -1;

		hr = DecompressImageIndirect(&dii);
		if(SUCCEEDED(hr))
		{
			ReleaseDC(NULL, hDC);

			GetObject(m_hBmp, sizeof(BITMAP), &bmp);

			m_nWidth	= bmp.bmWidth;
			m_nHeight	= bmp.bmHeight;
		}
		else
		{
			m_hBmp	= NULL;
		}
	}
}


// CPicViewDoc::GetImageData
//
//		Callback function to read in the image data
//
DWORD CALLBACK CPicViewDoc::GetImageData(LPSTR	pBuf, 
										 DWORD	dwBufMax, 
										 LPARAM	lParam)
{
	ReadBmp*	pRb = (ReadBmp*) lParam;
	DWORD		dwBytesRead = 0,
				dwToRead = 0;

	//
	// Check if there is anything left to read from the file.
	//
	if(pRb->dwSize > 0)
	{
		//
		// Calculate what amount to read
		//
		if(pRb->dwSize > dwBufMax)
			dwToRead = dwBufMax;
		else
			dwToRead = pRb->dwSize;

		//
		// Read the buffer
		//
		dwBytesRead = pRb->pFile->Read(pBuf, dwToRead);

		//
		// Decrease the amount to read for the next pass
		//
		pRb->dwSize -= dwBytesRead;
	}

	return dwBytesRead;
}


// CPicViewDoc::ImageProgress
//
//		Dummy callback for image loading progress
//
void CALLBACK CPicViewDoc::ImageProgress(IImageRender*	pRender, 
										 BOOL			bComplete, 
										 LPARAM			lParam )
{
	if(bComplete )
	{
		;// (Optional) add code here for completion processing
	}
}


//---------------------------------------------------------------------------
//
//	CPicViewDoc diagnostics
//
//---------------------------------------------------------------------------


#ifdef _DEBUG
void CPicViewDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CPicViewDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


//---------------------------------------------------------------------------
//
//	CPicViewDoc commands
//
//---------------------------------------------------------------------------


// CPicViewDoc::OnUpdateZoomMinus
//
//		Check if the user can decrease magnification
//
void CPicViewDoc::OnUpdateZoomMinus(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable((m_hBmp != NULL) && (m_nZoom > zoomMin));
}


// CPicViewDoc::OnZoomMinus
//
//		Decrease magnification
//
void CPicViewDoc::OnZoomMinus() 
{
	m_nZoom -= zoomDelta;
	UpdateAllViews(NULL);
}


// CPicViewDoc::OnUpdateZoomPlus
//
//		Check if the user can increase magnification
//
void CPicViewDoc::OnUpdateZoomPlus(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable((m_hBmp != NULL) && (m_nZoom < zoomMax));
}


// CPicViewDoc::OnZoomPlus
//
//		Increase magnification
//
void CPicViewDoc::OnZoomPlus() 
{
	m_nZoom += zoomDelta;
	UpdateAllViews(NULL);
}

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 The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Frotcom International
Portugal Portugal
I work on R&D for Frotcom International, a company that develops web-based fleet management solutions.

Comments and Discussions