/* the C part of the Data.Bitmap library (c) 2009 Balazs Komuves license: BSD3 */ #include #include #include #include "bm.h" // ----------------------------------------------------------------------------- #if __STDC_VERSION__ >= 199901L /* "inline" is a keyword */ #else #define inline /* nothing */ #endif // ----------------------------------------------------------------------------- // (to, count, fill) !!! void c_memset(word8 *q, int count, word8 x) { memset(q,x,count); } // (from, to, count) !!! void c_memcpy(word8 *p, word8 *q, int count) { memcpy(q,p,count); } // ----------------------------------------------------------------------------- #define K_WORD8 1 #define K_WORD16 2 #define K_WORD32 3 #define K_FLOAT 4 #define PLUSPTR(P,TYP,K) (TYP*)(((word8*)(P))+(K)); #define MAX(A,B) (((A)>(B))?(A):(B)) #define MIN(A,B) (((A)<(B))?(A):(B)) #define CLAMP(X,A,B) MIN(MAX(X,A),B) #define CLAMP_01(X) MIN(MAX(X,0),1) // ----------------------------------------------------------------------------- inline float toFloat_word8 (word8 x) { return ( (float)x * (3.92156862745098e-3 ) ); } inline float toFloat_word16 (word16 x) { return ( (float)x * (1.5259021896696422e-5 ) ); } inline float toFloat_word32 (word32 x) { return ( (float)x * (2.3283064370807974e-10) ); } inline float toFloat_float (float x) { return x; } inline word8 fromFloat_word8 (float x) { return ( floor ( 0.5 + 255.0 * CLAMP_01(x) ) ); } inline word16 fromFloat_word16 (float x) { return ( floor ( 0.5 + 65535.0 * CLAMP_01(x) ) ); } inline word32 fromFloat_word32 (float x) { return ( floor ( 0.5 + 4294967295.0 * CLAMP_01(x) ) ); } inline float fromFloat_float (float x) { return x; } // ----------------------------------------------------------------------------- // tgt and src can be potentally the same void c_mirror_line(int width, int bytesperpixel, void *src, void *tgt) { int i,k; word8 *p = (word8*)src; word8 *q = ((word8*)tgt) + (width-1)*bytesperpixel; if (src == tgt) { word8 *tmp = alloca(bytesperpixel); k = width / 2; for(i=0;i (width1-2) ) { *ix = width1-2; *fx = 1.0; } else { *ix = j; *fx = f; } } } // should we ask haskell for allocating the memory for the tables instead of malloc? #define C_BILINEAR_RESAMPLE_CHANNEL(TYP) \ void c_bilinear_resample_channel_ ## TYP \ ( int width1, int height1, TYP *p1, int nchn1, int pad1, int ofs1 \ , int width2, int height2, TYP *p2, int nchn2, int pad2, int ofs2 \ ) \ { int x,y; \ TYP *q2,*s1,*s2,*r1,*r2; \ float fx,fy; \ int ix,iy; \ int linesize1; \ float *fhoriztable; \ int *ihoriztable; \ float a,b,c; \ \ q2 = p2 + ofs2; \ linesize1 = nchn1 * width1 * sizeof(TYP) + pad1; \ \ fhoriztable = malloc(sizeof(float)*width2); \ ihoriztable = malloc(sizeof(int )*width2); \ for(x=0;x