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

📄 jpgapi.c

📁 SAMSUNG S3C6410 CPU BSP for winmobile6
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
 * Project Name JPEG API for HW JPEG IP IN WINCE
 * Copyright  2007 Samsung Electronics Co, Ltd. All Rights Reserved. 
 *
 * This software is the confidential and proprietary information
 * of Samsung Electronics  ("Confidential Information").   
 * you shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Samsung Electronics 
 *
 * This file implements JPEG driver.
 *
 * @name JPEG DRIVER MODULE Module (JPGApi.c)
 * @author Jiyoung Shin (idon.shin@samsung.com)
 * @date 28-05-07
 */
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "JPGApi.h"
#include "SVEDriverAPI.h"

//////////////////////////////////////////////////////////////////////////////////////
// Definition																		//
//////////////////////////////////////////////////////////////////////////////////////
#define JPG_DRIVER_NAME		L"JPG1:"


#define CTL_CODE( DeviceType, Function, Method, Access ) (                 \
    ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
)

#define IOCTL_JPG_DECODE			CTL_CODE( 0, 0x810, 0, 0 )
#define IOCTL_JPG_ENCODE			CTL_CODE( 0, 0x811, 0, 0 )
#define IOCTL_JPG_GET_STRBUF		CTL_CODE( 0, 0x812, 0, 0 )
#define IOCTL_JPG_GET_FRMBUF		CTL_CODE( 0, 0x813, 0, 0 )
#define IOCTL_JPG_GET_PHY_FRMBUF	CTL_CODE( 0, 0x814, 0, 0 )
#define IOCTL_JPG_GET_THUMB_STRBUF	CTL_CODE( 0, 0x815, 0, 0 )
#define IOCTL_JPG_GET_THUMB_FRMBUF	CTL_CODE( 0, 0x816, 0, 0 )
#define IOCTL_JPG_GET_RGBBUF			CTL_CODE( 0, 0x817, 0, 0 )
#define IOCTL_JPG_GET_PHY_RGBBUF		CTL_CODE( 0, 0x818, 0, 0 )


#define TRUE	1
#define FALSE	0

#define DEBUG   0

//////////////////////////////////////////////////////////////////////////////////////
// Type Define																		//
//////////////////////////////////////////////////////////////////////////////////////
typedef enum tagENCDEC_TYPE_T{
	JPG_MAIN,
	JPG_THUMBNAIL
}ENCDEC_TYPE_T;

typedef struct tagJPG_DEC_PROC_PARAM{
	SAMPLE_MODE_T	sampleMode;
	ENCDEC_TYPE_T	decType;
	UINT32	width;
	UINT32	height;
	UINT32	dataSize;
	UINT32	fileSize;
} JPG_DEC_PROC_PARAM;

typedef struct tagJPG_ENC_PROC_PARAM{
	SAMPLE_MODE_T	sampleMode;
	ENCDEC_TYPE_T	encType;
	IMAGE_QUALITY_TYPE_T quality;
	UINT32	width;
	UINT32	height;
	UINT32	dataSize;
	UINT32	fileSize;
} JPG_ENC_PROC_PARAM;

typedef struct tagJPG_CTX{
	UINT	debugPrint;
	char	*InBuf;
	char	*OutBuf;
	char	*InThumbBuf;
	char	*OutThumbBuf;
	UINT8	thumbnailFlag;
	JPG_DEC_PROC_PARAM	*decParam;
	JPG_ENC_PROC_PARAM	*encParam;
	JPG_ENC_PROC_PARAM	*thumbEncParam;
	ExifFileInfo *ExifInfo;
}JPG_CTX;

UCHAR ExifHeader[6]=
{
	0x45,0x78,0x69,0x66,0x00,0x00
};

UCHAR TIFFHeader[8]=
{
	0x49,0x49,0x2A,0x00,0x08,0x00,0x00,0x00
};

//////////////////////////////////////////////////////////////////////////////////////
// function declair																	//
//////////////////////////////////////////////////////////////////////////////////////
static void printD(char* fmt, ...);
static void initDecodeParam(void);
static void initEncodeParam(void);
static JPEG_ERRORTYPE makeThumbImage(void *openHandle);
static JPEG_ERRORTYPE MCUCheck(SAMPLE_MODE_T sampleMode, UINT32 width, UINT32 height);
static JPEG_ERRORTYPE makeExifFile(char *ExifOut, UINT *totalLen);
static void scalDownYUV422(char *srcBuf, INT32 srcWidth, INT32 srcHeight, 
							char *dstBuf, INT32 dstWidth, INT32 dstHeight);


//////////////////////////////////////////////////////////////////////////////////////
// Variables																		//
//////////////////////////////////////////////////////////////////////////////////////
JPG_CTX *jCtx;

static void printD(char* fmt, ...) 
{
#if (DEBUG == 1)
    char str[512];

	vsprintf(str, fmt, (char *)(&fmt+1));
	printf(str);
#endif

}

/*----------------------------------------------------------------------------
*Function: initDecodeParam

*Parameters:
*Return Value:
*Implementation Notes: Initialize JPEG Decoder Default value
-----------------------------------------------------------------------------*/
static void initDecodeParam(void)
{
	jCtx = (JPG_CTX *)malloc(sizeof(JPG_CTX));
	memset(jCtx, 0x00, sizeof(JPG_CTX));

	jCtx->decParam = (JPG_DEC_PROC_PARAM *)malloc(sizeof(JPG_DEC_PROC_PARAM));
	memset(jCtx->decParam, 0x00, sizeof(JPG_DEC_PROC_PARAM));

	jCtx->debugPrint = TRUE;
	jCtx->decParam->decType = JPG_MAIN;

}

/*----------------------------------------------------------------------------
*Function: SsbSipJPEGDecodeInit

*Parameters:
*Return Value:		Decoder Init handle
*Implementation Notes: Initialize JPEG Decoder Deivce Driver
-----------------------------------------------------------------------------*/
void *SsbSipJPEGDecodeInit(void)
{
	void *JPGHandle;

	initDecodeParam();
	JPGHandle = CreateFile(JPG_DRIVER_NAME,
							GENERIC_READ|GENERIC_WRITE,
							0,
							NULL,
							OPEN_EXISTING,
							FILE_ATTRIBUTE_NORMAL,NULL);
	if(JPGHandle == INVALID_HANDLE_VALUE){
		RETAILMSG(1, (TEXT("API :: CreateFile failed\r\n")));
		return NULL;
	}

	return JPGHandle;

}

/*----------------------------------------------------------------------------
*Function: initEncodeParam

*Parameters: 
*Return Value:	
*Implementation Notes: Initialize JPEG Encoder Default value
-----------------------------------------------------------------------------*/
static void initEncodeParam(void)
{

	jCtx = (JPG_CTX *)malloc(sizeof(JPG_CTX));
	memset(jCtx, 0x00, sizeof(JPG_CTX));

	jCtx->encParam = (JPG_ENC_PROC_PARAM *)malloc(sizeof(JPG_ENC_PROC_PARAM));
	memset(jCtx->encParam, 0x00, sizeof(JPG_ENC_PROC_PARAM));
	jCtx->encParam->fileSize = MAX_FILE_SIZE;

	jCtx->thumbEncParam = (JPG_ENC_PROC_PARAM *)malloc(sizeof(JPG_ENC_PROC_PARAM));
	memset(jCtx->thumbEncParam, 0x00, sizeof(JPG_ENC_PROC_PARAM));
	jCtx->thumbEncParam->dataSize = MAX_FILE_THUMB_SIZE;

	jCtx->debugPrint = TRUE;
	jCtx->encParam->sampleMode = JPG_420;
	jCtx->encParam->encType = JPG_MAIN;
	jCtx->thumbEncParam->sampleMode = JPG_420;
	jCtx->thumbEncParam->encType = JPG_THUMBNAIL;
	jCtx->thumbnailFlag = FALSE;
}

/*----------------------------------------------------------------------------
*Function: SsbSipJPEGEncodeInit

*Parameters:
*Return Value:		Encoder Init handle
*Implementation Notes: Initialize JPEG Encoder Deivce Driver
-----------------------------------------------------------------------------*/
void *SsbSipJPEGEncodeInit(void)
{
	void *JPGHandle;

	initEncodeParam();
	JPGHandle = CreateFile(JPG_DRIVER_NAME,
							GENERIC_READ|GENERIC_WRITE,
							0,
							NULL,
							OPEN_EXISTING,
							FILE_ATTRIBUTE_NORMAL,NULL);

	if(JPGHandle == INVALID_HANDLE_VALUE){
		RETAILMSG(1, (TEXT("API :: CreateFile failed\r\n")));
		return NULL;
	}

	printD("API :: init encode handle : %ld\n", JPGHandle);

	return JPGHandle;
}

/*----------------------------------------------------------------------------
*Function: SsbSipJPEGDecodeExe

*Parameters: 		*openHandle	: openhandle from SsbSipJPEGDecodeInit
*Return Value:		JPEG_ERRORTYPE
*Implementation Notes: Decode JPEG file 
-----------------------------------------------------------------------------*/
JPEG_ERRORTYPE SsbSipJPEGDecodeExe(void *openHandle)
{
	int	ret;

	printD("API :: jCtx->decParam->fileSize : %d\n", jCtx->decParam->fileSize);
	if(jCtx->decParam->decType == JPG_MAIN){
		ret = DeviceIoControl(openHandle, IOCTL_JPG_DECODE, NULL, 
							0, NULL, 0, 
							(PDWORD)jCtx->decParam, NULL);
		if(ret == 0){
			RETAILMSG(1, (TEXT("API :: IOCTL_JPG_DECODE failed\r\n")));
			return JPEG_FAIL;
		}

		printD("API :: decParam->width : %d decParam->height : %d\n", jCtx->decParam->width, jCtx->decParam->height);
		printD("API :: streamSize : %d\n", jCtx->decParam->dataSize);
	}
	else{
		// thumbnail decode, for the future work.
	}

	return JPEG_OK;
	
}

/*----------------------------------------------------------------------------
*Function: makeThumbImage

*Parameters: 		*openHandle	: openhandle from SsbSipJPEGEncodeInit
*Return Value:		JPEG_ERRORTYPE	
*Implementation Notes: make thumbnail image
-----------------------------------------------------------------------------*/
static JPEG_ERRORTYPE makeThumbImage(void *openHandle)
{
	int	ret;
	int	result;


	if((result = MCUCheck(jCtx->thumbEncParam->sampleMode, jCtx->thumbEncParam->width, jCtx->thumbEncParam->height)) != JPEG_OK){
		RETAILMSG(1, (TEXT("API :: thumbnail width/height doesn't match with MCU\r\n")));
		return JPEG_FAIL;
	}

	// encode thumbnail image
	ret = DeviceIoControl(openHandle, IOCTL_JPG_ENCODE, NULL, 
						0, NULL, 0, (PDWORD)jCtx->thumbEncParam, NULL);
	if(ret == 0){
			RETAILMSG(1, (TEXT("API :: IOCTL_JPG_ENCODE failed\r\n")));
			return JPEG_FAIL;
	}

	if(jCtx->thumbEncParam->fileSize > MAX_FILE_THUMB_SIZE){
		RETAILMSG(1, (TEXT("API :: thumbnail data is too big\r\n")));
		return JPEG_FAIL;
	}

	printD("API :: thumbFilesize : %d\n", jCtx->thumbEncParam->fileSize);

	ret = DeviceIoControl(openHandle, IOCTL_JPG_GET_THUMB_STRBUF, NULL, 
					0, &(jCtx->OutThumbBuf), sizeof(jCtx->OutThumbBuf), NULL, NULL);

	if(ret == 0){
			RETAILMSG(1, (TEXT("API :: IOCTL_JPG_GET_THUMB_STRBUF failed\r\n")));
			return JPEG_FAIL;
	}

	printD("API :: jCtx->OutThumbBuf(addr : %x callerProcessID : 0x%x)\n", jCtx->OutThumbBuf, (HANDLE)GetCurrentProcessId());
	
	return JPEG_OK;
}

/*----------------------------------------------------------------------------
*Function: MCUCheck

*Parameters: 		*openHandle	: openhandle from SsbSipJPEGEncodeInit
*Return Value:		JPEG_ERRORTYPE	
*Implementation Notes: check MCU
-----------------------------------------------------------------------------*/
static JPEG_ERRORTYPE MCUCheck(SAMPLE_MODE_T sampleMode, UINT32 width, UINT32 height)
{

	if(width%16 == 0 && height%8 == 0)
		return JPEG_OK;
	else return JPEG_FAIL;

}

/*----------------------------------------------------------------------------
*Function: scalDownYUV422

*Parameters: 		*srcBuf: input yuv buffer
					srcWidth: width of input yuv
					srcHeight: height of input yuv
					*dstBuf: output yuv buffer
					dstWidth: width of output yuv
					dstHeight: height of output yuv
*Return Value:		None	
*Implementation Notes: scaling down YCBYCR format
-----------------------------------------------------------------------------*/
static void scalDownYUV422(char *srcBuf, INT32 srcWidth, INT32 srcHeight, 
				char *dstBuf, INT32 dstWidth, INT32 dstHeight)
{
	INT32	scaleX, scaleY;
	INT32	iXsrc, iXdst;
	INT32	iYsrc;

	scaleX = srcWidth/dstWidth;
	scaleY = srcHeight/dstHeight;

	iXdst = 0;
	for(iYsrc=0; iYsrc < srcHeight; iYsrc++){
		if(iYsrc % scaleY == 0){
			for(iXsrc = iYsrc*srcWidth*2; iXsrc < iYsrc*srcWidth*2+srcWidth*2;){
				if((iXsrc % (4*scaleX)) == 0){
					dstBuf[iXdst++] = srcBuf[iXsrc++];
					dstBuf[iXdst++] = srcBuf[iXsrc++];
					dstBuf[iXdst++] = srcBuf[iXsrc++];
					dstBuf[iXdst++] = srcBuf[iXsrc++];
				}else iXsrc++;
			}
		}
	}
}

/*----------------------------------------------------------------------------
*Function: SsbSipJPEGEncodeExe

*Parameters: 		*openHandle : openhandle from SsbSipJPEGEncodeInit
					*Exif : Exif file parameter
*Return Value:		JPEG_ERRORTYPE
*Implementation Notes: Encode JPEG file 
-----------------------------------------------------------------------------*/
JPEG_ERRORTYPE SsbSipJPEGEncodeExe(void *openHandle, ExifFileInfo *Exif)
{
	char *outBuf = NULL;
	char *ExifBuf;
	UINT ExifLen;
	UINT bufSize;
	JPEG_ERRORTYPE result;
	int ret;

	// check MUC validation with width & hegiht & sampling mode
	if((result = MCUCheck(jCtx->encParam->sampleMode, jCtx->encParam->width, jCtx->encParam->height)) != JPEG_OK){
		RETAILMSG(1, (TEXT("API :: width/height doesn't match with MCU\r\n")));
		return JPEG_FAIL;
	}

⌨️ 快捷键说明

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