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