Knowledge Base Nr: 00066 Jpeg-lib.cpp - http://www.swe-kaiser.de

Downloads:

MFC: Imagefunktionen (jpeg, tiff, png, ...)

  
// ACHTUNG: MFC statisch linken!!!!

class CImageSupport
{
public:
CImageSupport();
virtual ~CImageSupport();

int LoadJPG(const char* lpszFilename);
int Draw(HDC hdc, int nPosX = 0, int nPosY = 0, int nSizeX = 0, int nSizeY = 0
, int nTargetPosX = 0, int nTargetPosY = 0
, int nTargetSizeX = 0, int nTargetSizeY = 0, boolean bNoResize = true);

int SaveJPG(const char* lpszFilename);
int GetSizeX() { return m_nWidth; }
int GetSizeY() { return m_nHeight; }

protected:
BYTE* m_buffer;
unsigned int m_nWidth;
unsigned int m_nHeight;
unsigned int m_nWidthDW;
};

////////////////////////////////////////////////////////////
//
// JpegFile - A C++ class to allow reading and writing of
// RGB and Grayscale JPEG images. (actually, it reads all forms
// that the JPEG lib will decode into RGB or grayscale) and
// writes only RGB and Grayscale.
//
// It is based on a Win32 compilation of the IJG V.6a code.
//
// This will only work on 32-bit Windows systems. I have only
// tried this with Win 95, VC++ 4.1.
//
// This class Copyright 1997, Chris Losinger
// This is free to use and modify provided my name is included.
//
// Comments:
// Thanks to Robert Johnson for discovering a DWORD-alignment bug
// Thanks to Lee Bode for catching a bug in CMfcappView::OnFileGetdimensionsjpg()
//
////////////////////////////////////////////////////////////
//
// General Usage:
//
// #include this file.
// link with jpeglib2.lib
//
// All functions here are static. There is no need to have a JpegFile object.
// There is actually nothing in a JpegFile object anyway.
//
// So, you can do this :
//
// BOOL ok = JpegFile::vertFlipBuf(buf, widthbytes, height);
//
// instead of this :
//
// JpegFile jpgOb;
// BOOL ok = jpgOb.vertFlipBuf(buf, widthbytes, height);
//
/////
//
// Linking usage :
// It is sometimes necessary to set /NODEFAULTLIB:LIBC (or LIBCD) to use this
// class.
//
/////
//
// Error reporting:
// The class generates message boxes in response to JPEG errors.
//
// The JpegFile.cpp fn my_error_exit defines the behavior for
// fatal errors : show msg box, return to caller.
//
// Warnings are handled by jpeglib.lib - which generates msg boxes too.
//
////////////////////////////////////////////////////////////////

/*
////////////////////////////////////////////////////////////////
// Reading Usage :

UINT height;
UINT width;
BYTE *dataBuf;
//read the file
dataBuf=JpegFile::JpegFileToRGB(fileName,
&width,
&height);
if (dataBuf==NULL) {
return;
}

// RGB -> BGR
JpegFile::BGRFromRGB(dataBuf, m_width, m_height);


BYTE *buf;
// create a DWORD aligned buffer from the JpegFile object
buf = JpegFile::MakeDwordAlignedBuf(dataBuf,
width,
height,
&m_widthDW);

// flip that buffer
JpegFile::VertFlipBuf(m_buf, m_widthDW, m_height);

// you now have a buffer ready to be used as a DIB

// be sure to delete [] dataBuf; // !!!!!!!!!!
// delete [] buf;


// Writing Usage


// this assumes your data is stored as a 24-bit RGB DIB.
// if you have a 1,4,8,15/16 or 32 bit DIB, you'll have to
// do some work to get it into a 24-bit RGB state.

BYTE *tmp=NULL;

// assume buf is a DWORD-aligned BGR buffer, vertically flipped
// as if read from a BMP file.

// un-DWORD-align
tmp=JpegFile::RGBFromDWORDAligned(buf,
widthPix,
widthBytes,
height);

// vertical flip
JpegFile::VertFlipBuf(tmp, widthPix * 3, height);

// reverse BGR
JpegFile::BGRFromRGB(tmp, widthPix, height);

if (tmp==NULL) {
AfxMessageBox("~DWORD Memory Error");
return;
}

// write it
BOOL ok=JpegFile::RGBToJpegFile(fileName,
tmp,
width,
height,
TRUE,
75);
if (!ok) {
AfxMessageBox("Write Error");
}

delete [] tmp;

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