/*乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//
    bolalg.h
//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗*/

#define  WINAPI  __stdcall
#define  WINAPIV __cdecl

#define  LOCALF  static __stdcall
#define  LOCALFV static __cdecl

#ifdef __cplusplus
extern "C" {
#endif

typedef long long  INT64;
typedef unsigned long long  UINT64;

typedef void      *P4B, *_p32, *pANY, *HFIL;
typedef char      I1B, _i08, *pI1B, *pSTR;
typedef BYTE      U1B, _u08, *pU1B, *pBIN;
typedef short     I2B, _i16, *pI2B;
typedef WORD      U2B, _u16, *pU2B;
typedef int       I4B, _i32, *pI4B;
typedef UINT      U4B, _u32, *pU4B, uANY;
typedef INT64     I8B, _i64, *pI8B;
typedef UINT64    U8B, _u64, *pU8B;
typedef float     F4B, _f32, *pF4B;
typedef double    F8B, _f64, *pF8B, REAL;
typedef P4B       *pP4B;
typedef LONG_PTR  iANY;

typedef const void *rANY, *PCVOID;
typedef const I1B  *rI1B, *rSTR;
typedef const U1B  *rU1B, *rBIN, *PCBYTE;
typedef const I2B  *rI2B;
typedef const U2B  *rU2B, *PCWORD;
typedef const I4B  *rI4B, *PCINT;
typedef const U4B  *rU4B, *PCUINT;
typedef const I8B  *rI8B;
typedef const U8B  *rU8B;
typedef const F4B  *rF4B;
typedef const F8B  *rF8B;
typedef const P4B  *rP4B;

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

//#define  NULL  ((void *)0)

//#define  _countof(a)  (sizeof(a) / sizeof(a[0]))
#define  NELEM(a)  _countof(a)

#define  LOU1B(w)  ((U1B)((U4B)(w) & 0x00ff))
#define  HIU1B(w)  ((U1B)((U4B)(w) >> 8))
#define  LOU2B(u)  ((U2B)((U4B)(u) & 0xffff))
#define  HIU2B(u)  ((U2B)((U4B)(u) >> 16))

#define  MKU2B(l, h)  ((U2B)(((U4B)(l) & 0x00ff)|((U4B)(h) << 8)))
#define  MKU4B(l, h)  ((U4B)(((U4B)(l) & 0xffff)|((U4B)(h) << 16)))

//#define  LOBYTE(w)  LOU1B(w)
//#define  HIBYTE(w)  HIU1B(w)
//#define  LOWORD(u)  LOU2B(u)
//#define  HIWORD(u)  HIU2B(u)

//#define  MAKEWORD(l, h)  MKU2B(l, h)
#define  MAKEUINT(l, h)  MKU4B(l, h)

#define  SWAP1B(w)  MKU2B(HIU1B(w), LOU1B(w))
#define  SWAP2B(u)  MKU4B(HIU2B(u), LOU2B(u))

#define  OCTVAL(b0, b1, b2, b3)  MKU4B(MKU2B(b0, b1), MKU2B(b2, b3))
#define  OCTREV(u)  OCTVAL(HIU1B(HIU2B(u)), LOU1B(HIU2B(u)), HIU1B(u), LOU1B(u))

#define  ISU1B(u)   (((u) & 0xffffff00) == 0)
#define  ISU2B(u)   (((u) & 0xffff0000) == 0)

#define  ISI1BN(u)  (((u) & 0xffffff80) == 0xffffff80)
#define  ISI2BN(u)  (((u) & 0xffff8000) == 0xffff8000)

#define  LBUF  512

void WINAPI memswap(pANY p1, pANY p2, I4B n);
void WINAPI memrotl(pANY pv, I4B ln, I4B n);
void WINAPI memrotr(pANY pv, I4B ln, I4B n);

I4B  WINAPI sign(F8B d);
I4B  WINAPI iround(F8B d);

U4B  WINAPI hash(rSTR st, U4B hs);

U4B  WINAPI nbound(U4B i, U4B n);

typedef struct { I4B unused;} HMEM_;
typedef HMEM_ *HMEM;

HMEM WINAPI CreatePtrList(I4B cap);
void WINAPI DeletePtrList(HMEM hm);
BOOL WINAPI AddPtr(HMEM hm, pANY pv);
BOOL WINAPI RemovePtr(HMEM hm, pANY pv);

pANY WINAPI halloc(U4B sz, BOOL clr);
pANY WINAPI hrealloc(pANY pv, U4B sz);
BOOL WINAPI hfree(pANY pv);

pANY WINAPI walloc(HMEM hm, U4B sz, BOOL clr);
pANY WINAPI wrealloc(HMEM hm, pANY pv, U4B sz);
BOOL WINAPI wfree(HMEM hm, pANY pv);

void WINAPI ClearAll(HMEM hm);

pANY WINAPI talloc(U4B sz);
pANY WINAPI ualloc(U4B sz);
pANY WINAPI valloc(U4B sz);

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

typedef struct {
  pI4B  pi;
  I4B   ni;
} IDXS;
typedef IDXS *pIDXS, *PIDXS;
typedef const IDXS *rIDXS, *PCIDXS;

I4B  WINAPI idxval(rIDXS ls, I4B id);
I4B  WINAPI srchidx(rIDXS ls, I4B va);

typedef struct {
  I4B   ky;
  I4B   va;
} MAPI;
typedef MAPI *pMAPI, *PMAPI;
typedef const MAPI *rMAPI, *PCMAPI;

typedef struct {
  pMAPI pv;
  I4B   ni;
} MAPS;
typedef MAPS *pMAPS, *PMAPS;
typedef const MAPS *rMAPS, *PCMAPS;

I4B  WINAPI mapval(rMAPS ls, I4B ky);
I4B  WINAPI srchmap(rMAPS ls, I4B va);

typedef struct {
  rSTR  *ps;
  I4B   ni;
} ENMS;
typedef ENMS *pENMS, *PENMS;
typedef const ENMS *rENMS, *PCENMS;

rSTR WINAPI enmstr(rENMS ls, I4B va);
I4B  WINAPI srchenm0(rENMS ls, rSTR st);

typedef struct {
  I4B   va;
  rSTR  st;
} CODI;
typedef CODI *pCODI, *PCODI;
typedef const CODI *rCODI, *PCCODI;

typedef struct {
  pCODI pv;
  I4B   ni;
} CODS;
typedef CODS *pCODS, *PCODS;
typedef const CODS *rCODS, *PCCODS;

rSTR WINAPI codstr(rCODS ls, I4B va);
I4B  WINAPI srchcod0(rCODS ls, rSTR st);

typedef struct {
  pANY   ptr;
  I4B    len;
  I4B    cap;
  I4B    rsz;
} HBLK_;
typedef HBLK_ BLKE, *HBLK;

HBLK WINAPI BuildArray(HBLK mb, pANY pv, I4B len, I4B cap, I4B rsz);
HBLK WINAPI BuildSlice(HBLK mb, HBLK mx, I4B ir, I4B nr);

HBLK WINAPI CreateArray(I4B cap, I4B rsz);
#define  CreateDArray(cap, rsz)  CreateArray(cap, rsz)
HBLK WINAPI CreateMemBlk(I4B cap, I4B rsz);

void WINAPI DeleteArray(HBLK mb);
#define  DeleteDArray(mb)  DeleteArray(mb)
#define  DeleteMemBlk(mb)  DeleteArray(mb)

BOOL WINAPI CopyArray(HBLK mb, HBLK mx);
BOOL WINAPI AppendArray(HBLK mb, HBLK mx);
I4B  WINAPI CompareArray(HBLK m1, HBLK m2);

#define  CreateBinBlk(cap)  CreateMemBlk(cap, 1)
#define  DeleteBinBlk(bb)  DeleteMemBlk(bb)

HBLK WINAPI MakeMemBlk(pANY pv, I4B len, I4B cap, I4B rsz);
#define  MakeBinBlk(pv, len, cap)  MakeMemBlk(pv, len, cap, 1)

pANY WINAPI Ptr(HBLK mb);
I4B  WINAPI Len(HBLK mb);
#define  NItems(mb)  Len(mb)
I4B  WINAPI ItemSize(HBLK mb);
pANY WINAPI Item(HBLK mb, I4B ir);
#define  ItemPtr(mb, ir)  Item(mb, ir)
I4B  WINAPI ItemIndex(HBLK mb, rANY pi);
I4B  WINAPI SetLen(HBLK mb, I4B len);
#define  SetNItems(mb)  SetLen(mb)
I4B  WINAPI AddLen(HBLK mb, I4B nr);

I4B  WINAPI Capacity(HBLK mb);
BOOL WINAPI ChangeCapacity(HBLK mb, I4B cap);

BOOL WINAPI InsertItem(HBLK mb, I4B ir, rANY pi);
BOOL WINAPI InsertItems(HBLK mb, I4B ir, rANY pb, I4B nr);
BOOL WINAPI DeleteItem(HBLK mb, I4B ir);
BOOL WINAPI DeleteItems(HBLK mb, I4B ir, I4B nr);
BOOL WINAPI AddItem(HBLK mb, rANY pi);
BOOL WINAPI AddItems(HBLK mb, rANY pb, I4B nr);
pANY WINAPI NewItem(HBLK mb);
BOOL WINAPI RemoveItem(HBLK mb, rANY pi);
#define  PushItem(mb, pi)  AddItem(mb, pi)
BOOL WINAPI PopItem(pANY pi, HBLK mb);

void WINAPI ReverseItems(HBLK mb, I4B ir, I4B nr);
void WINAPI RotateItemsLeft(HBLK mb, I4B ir, I4B nr);
void WINAPI RotateItemsRight(HBLK mb, I4B ir, I4B nr);
void WINAPI MoveItemToFirst(HBLK mb, I4B ir);
void WINAPI MoveItemToLast(HBLK mb, I4B ir);

I4B  WINAPI LSearchItemValue(HBLK mb, rANY pi);
I4B  WINAPI RSearchItemValue(HBLK mb, rANY pi);

typedef BOOL (CALLBACK *FSEL)(rANY pb, uANY pa);

BOOL WINAPI fselALL(rANY pb, uANY pa);
BOOL WINAPI fselU1B(rANY pb, uANY pa);
BOOL WINAPI fselU2B(rANY pb, uANY pa);
#define  fselI4B  fselU4B
BOOL WINAPI fselU4B(rANY pb, uANY pa);

I4B  WINAPI LSearchItem(HBLK mb, FSEL fsel, uANY pa);
I4B  WINAPI RSearchItem(HBLK mb, FSEL fsel, uANY pa);
I4B  WINAPI SearchItem(HBLK mb, I4B i0, FSEL fsel, uANY pa);
I4B  WINAPI CountItems(HBLK mb, FSEL fsel, uANY pa);
I4B  WINAPI SelectItems(HBLK mb, FSEL fsel, uANY pa);
I4B  WINAPI InvSelectItems(HBLK mb, FSEL fsel, uANY pa);

typedef I4B  (CALLBACK *FSRT)(rANY p1, rANY p2, uANY pa);

I4B  WINAPI fsrtI1B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtU1B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtI2B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtU2B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtI4B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtU4B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtI8B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtU8B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtF4B(rANY p1, rANY p2, uANY pa);
I4B  WINAPI fsrtF8B(rANY p1, rANY p2, uANY pa);

void WINAPI SortItems(HBLK mb, FSRT fsrt, uANY pa);

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

enum {
  e_000, E_MEM, E_DAT, E_PAR, E_FIR, E_FIW, E_DIR, E_ALO, E_CAP, E_DLL, //  0-  9
};

BOOL WINAPI register_error_descriptions(rCODS ed);
rSTR WINAPI error_description(U4B ec);
I4B  WINAPI first_error(void);
I4B  WINAPI last_error(void);
void WINAPI reset_error(void);
BOOL WINAPI enable_error_reporting(void);
BOOL WINAPI disable_error_reporting(void);
BOOL WINAPI enable_error_logging(void);
BOOL WINAPI disable_error_logging(void);

typedef BOOL (CALLBACK *FERR)(rSTR em, U4B er, rSTR fn, U4B lo);

void WINAPI override_errmsg(FERR fmsg);
void WINAPI override_errlog(FERR flog);

pANY WINAPI throw_error(rSTR em, U4B er, rSTR fn, U4B lo);

#define  ierr(ec)               throw_error(MODNAM, MKU4B(ec, __LINE__), NULL, -1)
#define  ferr(ec, nam)          throw_error(MODNAM, MKU4B(ec, __LINE__), nam, -1)
#define  serr(ec, nam, ir, ic)  throw_error(MODNAM, MKU4B(ec, __LINE__), nam, MKU4B(ir, ic))

typedef void (CALLBACK *FDEB)(rSTR st);

void WINAPI override_debmsg(FDEB fmsg);
void WINAPI override_deblog(FDEB flog);

#define  debug_reporting(st)  debug_logging(st)
void WINAPI debug_logging(rSTR st);

#define  dmsg(st)  debug_reporting(st)
#define  dlog(st)  debug_logging(st)

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

typedef F8B *pVEC, *pMAT;
typedef const F8B *rVEC, *rMAT;

pVEC WINAPI VecCpyN(pVEC vr, rVEC vc, I4B n);
F8B  WINAPI VecIprN(rVEC v1, rVEC v2, I4B n);

pMAT WINAPI MatCpyN(pMAT mr, rMAT mt, I4B n);
pVEC WINAPI MatVecN(pVEC vr, rMAT mt, rVEC vc, I4B n);
pMAT WINAPI MatMulN(pMAT mr, rMAT m1, rMAT m2, I4B n);

F8B  WINAPI MatLudN(pMAT mr, pI4B pi, rMAT mt, I4B n);
F8B  WINAPI MatDetN(rMAT mt, I4B n);
pMAT WINAPI MatInvN(pMAT mr, rMAT mt, I4B n);

BOOL WINAPI EquNu1d(pVEC vr, rMAT mt, rVEC vc, I4B n);

typedef F8B  (CALLBACK *FVAL)(F8B x, uANY pa);

BOOL WINAPI Equ1uNtn(pF8B px, FVAL fn, FVAL fd, uANY pa);

I4B  WINAPI Equ1uNd(pVEC vr, rVEC vc, I4B n);

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

#define  RPD  (M_PI / 180.0)
#define  DPR  (180.0 / M_PI)

typedef U4B RGBC;
typedef RGBC *pRGBC;
typedef const RGBC *rRGBC;

typedef struct {
  pRGBC bi;
  I4B   w;
  I4B   h;
} HIMG_;
typedef HIMG_ IMGE, *HIMG;

HIMG WINAPI CreateIMG(I4B w, I4B h, RGBC cb);
HIMG WINAPI DuplicateIMG(HIMG im);
void WINAPI DeleteIMG(HIMG im);
HIMG WINAPI MoveIMG(HIMG im, HIMG ix);

typedef U1B  (CALLBACK *FCOL)(RGBC cr);

U1B  WINAPI fvalBRI(RGBC cr);
U1B  WINAPI fvalCDB(RGBC cr);
U1B  WINAPI fvalCDR(RGBC cr);
U1B  WINAPI fvalSAT(RGBC cr);
U1B  WINAPI fvalHUE(RGBC cr);
U1B  WINAPI fvalAVG(RGBC cr);

typedef RGBC (CALLBACK *FCVT)(RGBC cr, uANY pa);

RGBC WINAPI fcvtRBX(RGBC cr, uANY pa);

typedef struct {
  RGBC  c1;
  RGBC  c2;
  FCOL  fcol;
} GRAP;

RGBC WINAPI fcvtGRA(RGBC cr, uANY pa);

typedef struct {
  RGBC  c1;
  RGBC  c2;
  FCOL  fcol;
  U4B   th;
} BINP;

RGBC WINAPI fcvtBIN(RGBC cr, uANY pa);
RGBC WINAPI fcvtGRS(RGBC cr, uANY pa);
RGBC WINAPI fcvtLGT(RGBC cr, uANY pa);
RGBC WINAPI fcvtGRY(RGBC cr, uANY pa);
RGBC WINAPI fcvtTXT(RGBC cr, uANY pa);

void WINAPI ChangeIMGColor(HIMG im, FCVT fcvt, uANY pa);

typedef RGBC (CALLBACK *FOPR)(RGBC c1, RGBC c2, uANY pa);

RGBC WINAPI foprADD(RGBC c1, RGBC c2, uANY pa);
RGBC WINAPI foprSUB(RGBC c1, RGBC c2, uANY pa);
RGBC WINAPI foprMIX(RGBC c1, RGBC c2, uANY pa);
RGBC WINAPI foprALF(RGBC c1, RGBC c2, uANY pa);

void WINAPI SuperposeIMG(HIMG im, HIMG ix, I4B x, I4B y, FOPR fopr, uANY pa);
void WINAPI PasteIMG(HIMG im, HIMG ix, I4B x, I4B y);
void WINAPI TrimIMG(HIMG im, I4B x, I4B y, I4B w, I4B h, RGBC cb);

typedef void (CALLBACK *FXFM)(pF8B px, pF8B py, F8B ix, F8B iy, uANY pa);

void WINAPI TransformIMGX(HIMG im, I4B w, I4B h, FXFM fxfm, uANY pa, RGBC cb);

typedef struct {
  I4B   x1, y1;
  I4B   x2, y2;
  I4B   x3, y3;
  I4B   x4, y4;
} XFMQ;
typedef XFMQ *pXFMQ, *PXFMQ;
typedef const XFMQ *rXFMQ, *PCXFMQ;

void WINAPI SetXFMQ(PXFMQ q, I4B x1, I4B y1, I4B x2, I4B y2, I4B x3, I4B y3, I4B x4, I4B y4);

void WINAPI TransformIMG(HIMG im, rXFMQ q, RGBC cb);

#define  IMG_HORIZONTAL  1
#define  IMG_VERTICAL    2

#define  IMG_HRZ  IMG_HORIZONTAL
#define  IMG_VRT  IMG_VERTICAL

void WINAPI InvertIMG(HIMG im, I4B sw);
void WINAPI RotateIMG(HIMG im, F8B dg, RGBC cb);

#define  IMG_MEAN    0
#define  IMG_LINEAR  1
#define  IMG_SMOOTH  2
#define  IMG_SHARP   3

#define  IMG_MEA  IMG_MEAN
#define  IMG_LIN  IMG_LINEAR
#define  IMG_SMO  IMG_SMOOTH
#define  IMG_SHA  IMG_SHARP

void WINAPI ResizeIMGX(HIMG im, I4B w, I4B h, I4B sw);

#define  ResizeIMG(im, w, h)  ResizeIMGX(im, w, h, IMG_MEA)

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

U4B  WINAPI BOLALGVer(void);

BOOL WINAPI InitBOLALG(uANY pa);
void WINAPI TermBOLALG(void);

#ifdef __cplusplus
}
#endif

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//
