⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfio.c

📁 s1d13716的源码 windiws ce 或8位单片机
💻 C
字号:
/*===========================================================================
** MFIO.c - Common routines to support MultiFrame JPEG File Format IO
**---------------------------------------------------------------------------
**	Copyright (c) 2002 Epson Research and Development, Inc.
**	All Rights Reserved.
**===========================================================================
*/

//---------------------------------------------------------------------------
// Include Files
//---------------------------------------------------------------------------
#include <fcntl.h>
#include <io.h>
#include "multiframe.h"

#ifndef MV850E
//-----------------------------------------------------------------------------
//	mfCreateFile - creates a new file
//
//	Input:	pszFile	pointer to file name string
//			ppStream	pointer to pointer to FILE object
//
//	Output:	TRUE means no error
//			FALSE means error
//
//-----------------------------------------------------------------------------
Boolean mfCreateFile( const char* pszFile, FILE** ppStream )
{
	FILE*	pStream		= fopen( pszFile, "wb" );
	Boolean	fSuccess	= (pStream == NULL) ? FALSE : TRUE;
	if ( !fSuccess )
	{
		//printf( "ERROR: Cannot create file %s\n", pszFile );
	}
	else
	{
		*ppStream = pStream;
	}
	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	mfGetOpenSize - opens and retrieves size of given file
//
//	Input:	pszFile	pointer to file name string
//			ppStream	pointer to pointer to FILE object
//			pointer		pointer to size of file
//
//	Output:	TRUE means no error
//			FALSE means error
//
//-----------------------------------------------------------------------------
Boolean mfGetOpenSize( const char* pszFile, FILE** ppStream, long* pSize )
{
	FILE*	pStream		= fopen( pszFile, "rb" );
	Boolean fSuccess	= (pStream != NULL) ? TRUE : FALSE;
	if ( !fSuccess )
	{
		//printf( "ERROR: Cannot open file %s\n", pszFile );
	}
	else
	{
		fSuccess = fseek( pStream, 0, SEEK_END ) ? FALSE : TRUE;
		if ( !fSuccess )
		{
			//printf( "ERROR: Cannot seek on file %s\n", pszFile );
		}
		else
		{
			*pSize		= ftell( pStream );
			fSuccess	= (*pSize != -1L) ? TRUE : FALSE;
			if ( !fSuccess )
			{
				//printf( "ERROR: Cannot tell on file %s\n", pszFile );
			}
			else
			{
				fSuccess = fseek( pStream, 0, SEEK_SET ) ? FALSE : TRUE;
				if ( !fSuccess )
				{
					//printf( "ERROR: Cannot seek on file %s\n", pszFile );
				}
				else
				{
					*ppStream	= pStream;
				}
			}
		}
	}
	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	mfReadFile - reads data to file
//
//	Input:	pszFile	pointer to file name string
//			ppStream	pointer to pointer to FILE object
//			pBuffer		pointer to buffer to read
//			size		number of bytes to read
//
//	Output:	TRUE means no error
//			FALSE means error
//
//-----------------------------------------------------------------------------
Boolean mfReadFile( const char* pszFile, FILE* pStream, void* pBuffer, UInt32 size )
{
	UInt32 sizeRead		= fread( pBuffer, 1, size, pStream );
	Boolean fSuccess	= (sizeRead != size) ? FALSE : TRUE;
	if ( !fSuccess )
	{
		//printf( "ERROR: Cannot read file %s\n", pszFile );
	}
	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	mfReadJfif - allocates a buffer and reads from file
//
//	Input:	pszFile	pointer to file name string
//			pStream		pointer to FILE object
//			*pSize		pointer to number of bytes to read
//			ppJfif		pointer to pointer to buffer to read Jfif into
//
//	Output:	TRUE means no error
//			FALSE means error
//
//-----------------------------------------------------------------------------
Boolean mfReadJfif( const char* pszFile, FILE* pStream, UInt8** ppJfif, UInt32* pSize )
{
	size_t	bufferSize	= ((*pSize + 3) & (~0x00000003));
	UInt8*	pJfif		= malloc( bufferSize );
	Boolean fSuccess	= (pJfif == NULL) ? FALSE : TRUE;
	if ( !fSuccess )
	{
		//printf( "ERROR: Not enough memory for %s\n", pszFile );
	}
	else
	{
		fSuccess = mfReadFile( pszFile, pStream, pJfif, *pSize );
		if ( fSuccess )
		{
			*ppJfif = pJfif;
			for ( ; *pSize < bufferSize; ++*pSize )
			{
				pJfif[ *pSize ] = 0;
			}
		}
	}

	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	mfWriteFile - writes data to file
//
//	Input:	pszFile	pointer to file name string
//			ppStream	pointer to pointer to FILE object
//			pBuffer		pointer to buffer to write
//			size		number of bytes to write
//
//	Output:	TRUE means no error
//			FALSE means error
//
//-----------------------------------------------------------------------------
Boolean mfWriteFile( const char* pszFile, FILE* pStream, void* pBuffer, UInt32 size )
{
	UInt32 sizeWritten	= fwrite( pBuffer, 1, size, pStream );
	Boolean fSuccess	= (sizeWritten == size) ? TRUE : FALSE;
	if ( !fSuccess )
	{
		//printf( "ERROR: Cannot write file %s\n", pszFile );
	}
	return ( fSuccess );
}

//-----------------------------------------------------------------------------
//	mfReadInputFile - reads in file whether of .JPG or MultiFrame format
//
//	Input:	pszFile			pointer to file name string
//			ppJpegData		pointer to pointer to data buffer to write
//			pJpegSize		pointer to size of JPEG image
//			pFrameCount		pointer to count of JPEG images (1 if "normal" JPEG
//								>1 if MultiFrame
//
//	Output:	TRUE means no error
//			FALSE means error
//
//-----------------------------------------------------------------------------
Boolean mfReadInputfile( const char* pszFile, UInt8** ppJpegData, UInt32* pJpegSize, UInt32* pFrameCount )
{
	FILE*	pStream		= NULL;
	Boolean fSuccess	= mfGetOpenSize( pszFile, &pStream, (long*)pJpegSize );
	*ppJpegData			= NULL;
	if ( fSuccess )
	{
		fSuccess = mfReadJfif( pszFile, pStream, ppJpegData, pJpegSize );
		fclose( pStream );

		if ( fSuccess )
		{
			JPEGMULTIFRAME* pMultiFrame = (JPEGMULTIFRAME*)*ppJpegData;
			if ( !mfValidateMultiFrame( pMultiFrame ) )
			{	//cannot find signature in the first 16 bytes -> must be one-frame normal JPEG file.
				*pFrameCount = 1;
			}
			else
			{
				UInt8 *pTmp			= (UInt8*)pMultiFrame;
				UInt32	fileSize	= *pJpegSize;
				for ( *pFrameCount = 0; fSuccess && (fileSize > 0); ++*pFrameCount )
				{
					pMultiFrame = (JPEGMULTIFRAME*)pTmp;
					fSuccess	= mfValidateMultiFrame( pMultiFrame );
					if ( (!fSuccess) || (pMultiFrame->PaddedSize + sizeof( JPEGMULTIFRAME ) > fileSize) )
					{
						//printf( "ERROR: Not valid Multi-Frame format file %s\n", pszFile );
					}
					else
					{
						pTmp		+= sizeof( JPEGMULTIFRAME) + pMultiFrame->PaddedSize;
						fileSize	-= sizeof( JPEGMULTIFRAME );
						fileSize	-= pMultiFrame->PaddedSize;
					}
				}
			}
		}
	}

	if ( !fSuccess )
	{
		if ( *ppJpegData != NULL )
		{
			free( *ppJpegData );
		}
	}
	return ( fSuccess );
}

#endif //MV850E

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -