Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include "ttwain_conversion.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __cplusplus
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
float TTWAIN_Fix32ToFloat(TW_FIX32 fix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TW_INT32 val;
Toshihiro Shimizu 890ddd
	val = ((TW_INT32)fix.Whole << 16) | ((TW_UINT32)fix.Frac & 0xffff);
Toshihiro Shimizu 890ddd
	return (float)(val / 65536.0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
TW_FIX32 TTWAIN_FloatToFix32(float fl)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TW_FIX32 fix;
Toshihiro Shimizu 890ddd
	TW_INT32 val;
Toshihiro Shimizu 890ddd
	assert(sizeof(TW_FIX32) == sizeof(float));
Toshihiro Shimizu 890ddd
	assert(sizeof(TW_FIX32) == sizeof(long));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* Note 1: This round-away-from-0 is new in TWAIN 1.7
Toshihiro Shimizu 890ddd
   Note 2: ANSI C converts float to int by truncating toward 0.*/
Toshihiro Shimizu 890ddd
	val = (TW_INT32)(fl * 65536.0 + (fl < 0 ? -0.5 : +0.5));
Toshihiro Shimizu 890ddd
	fix.Whole = (TW_INT16)(val >> 16);	/* most significant 16 bits */
Toshihiro Shimizu 890ddd
	fix.Frac = (TW_UINT16)(val & 0xffff); /* least */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return fix;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
void TTWAIN_ConvertRevStrToRevNum(const char *rev_str,
Toshihiro Shimizu 890ddd
								  TW_UINT16 *maj_num,
Toshihiro Shimizu 890ddd
								  TW_UINT16 *min_num)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	char *maj_str;
Toshihiro Shimizu 890ddd
	char *min_str;
Toshihiro Shimizu 890ddd
	size_t maj_size;
Toshihiro Shimizu 890ddd
	size_t min_size;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*maj_num = *min_num = 0;
Toshihiro Shimizu 890ddd
	maj_size = strcspn(rev_str, ".");
Toshihiro Shimizu 890ddd
	maj_str = (char *)calloc(sizeof(char), maj_size + 1);
Toshihiro Shimizu 890ddd
	if (!maj_str)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	memcpy(maj_str, rev_str, maj_size); /*already 0term*/
Toshihiro Shimizu 890ddd
	*maj_num = (TW_UINT16)atoi(maj_str);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	min_size = strlen(rev_str) - maj_size + 1;
Toshihiro Shimizu 890ddd
	min_str = (char *)calloc(sizeof(char), min_size + 1);
Toshihiro Shimizu 890ddd
	if (!min_str)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	memcpy(min_str, &(rev_str[maj_size + 1]), min_size); /*already 0term*/
Toshihiro Shimizu 890ddd
	*min_num = (TW_UINT16)atoi(min_str);
Toshihiro Shimizu 890ddd
	if (maj_str)
Toshihiro Shimizu 890ddd
		free(maj_str);
Toshihiro Shimizu 890ddd
	if (min_str)
Toshihiro Shimizu 890ddd
		free(min_str);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __cplusplus
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif