// DLL source file pascal.cpp #include "pascal.h" #include #include #include /* used for EXIT_SUCCESS */ #include LPSAFEARRAY lpsa1; LPSAFEARRAY lpsa2; #define FALSE 0 #define TRUE 1 // double Fourn(double *Data, int *NN, int NDIM, int ISIGN); double __stdcall MyFunc_atan2(double a, double b) { return atan2(a,b); } double __stdcall MyFunc_atan(double a) { return atan(a); } double __stdcall MyFunc_floor(double a) { return floor(a); } double __stdcall MyFunc_asin(double a) //dimensiot { return asin(a); } double __stdcall MyFunc_acos(double a) //dimensiot { return acos(a); } int __stdcall MyFunc_mktime(struct tm *time_s ) { int i; i = mktime(time_s); return 1; } double __stdcall MyFunc_ccor(unsigned char *akuva, short na, short ma,unsigned char *bkuva, short nb, short mb, short x_a, short y_a, short x_b, short y_b, short w, short h) // double __stdcall MyFunc_ccor(unsigned char *akuva, short na,short ma) // akuva = Bytearray size na * ma, position x_a, y_a, window height h, width w // bkuva = Bytearray size nb * mb, position x_b, y_b, window height h, width w { // char buffer[100]; unsigned char temp_a[50][50],temp_b[50][50]; // size of akuva [0...na*nb-1] int i,j; int l,m, start_row, start_col, start_index, elements, lkm_a, lkm_b; double os, nim1, nim2, meanA, meanB, cor; long jump; //j = sprintf(buffer,"Rajat %d %d %d %d \n", na, ma, nb, mb); //MessageBox ( NULL, buffer,"rajat",MB_OK); //j = sprintf(buffer,"Rajat %d %d %d %d \n", x_a, y_a, x_b, y_b); //MessageBox ( NULL, buffer,"rajat",MB_OK); //j = sprintf(buffer,"Rajat %d %d \n", w, h); //MessageBox ( NULL, buffer,"rajat",MB_OK); // fill temp_a and temp_b templates (1 to w, 1 to h) m=0; l=0; jump=0; start_row = y_a - h/2; start_col = x_a - w/2; start_index = (start_row)*(na)+start_col; elements =0; lkm_a=0; for (l=1;l<=w;l++) { jump = long(na)*long(l-1); for (m=1;m<=h;m++) { lkm_a=lkm_a+1; if ((start_index+m+jump) < 0 || (start_index+m+jump) >= (na*ma-1)) return double(elements); temp_a[l][m]=akuva[start_index+m+jump]; elements = elements + 1; } } m=0; l=0; jump=0; start_row = y_b - h/2; start_col = x_b - w/2; start_index = (start_row)*(nb)+start_col; elements =0; lkm_b=0; for (l=1;l<=w;l++) { //jump = nb-w-1; jump = long(nb)*long(l-1); for (m=1;m<=h;m++) { lkm_b=lkm_b+1; if ((start_index+m+jump) < 0 || (start_index+m+jump) >= (nb*mb-1)) return double(elements); temp_b[l][m]=bkuva[start_index+m+jump]; elements = elements + 1; } } os = 0.0; nim1 = 0.0; nim2 = 0.0; meanA = 0.0; meanB = 0.0; for (i=1;i<=w;i++) { for (j=1;j<=h;j++) { meanA = meanA + double(temp_a[i][j]); meanB = meanB + double(temp_b[i][j]); } } meanA = meanA / double(h*w); meanB = meanB / double(h*w); for (i=1;i<=w;i++) { for (j=1;j<=h;j++) { os = os + (double(temp_a[i][j]) - meanA) * (double(temp_b[i][j]) - meanB); nim1 = nim1 + (double(temp_a[i][j]) - meanA) * (double(temp_a[i][j]) - meanA); nim2 = nim2 + (double(temp_b[i][j]) - meanB) * (double(temp_b[i][j]) - meanB); } } if ((nim1 * nim2) < 0.002 ) return -2.0; cor = os / pow((nim1 * nim2),0.5); return cor; } double __stdcall MyFunc_corima(unsigned char *akuva, short width,short height, unsigned char *temp, short wt, short ht, short c_col, short c_row, double *rkuva) { /* This routine computes [-1,1] correlation image using cross-correlation. Correlation is computed for image akuva[] and template[] for the common area. BW-image BYTE (uchar) array *akuva[], declared as akuva(0 to width-1, 0 to height-1) in VB Template is the BYTE (uchar) array *temp[], declared also as temp(0 to wt-1, 0 to ht-1) in VB The center of the template (tree top's location) is given in (c_col, c_row) The output goes to the correlation double-array, declared same size as akuva-image-array. */ const int MAXDIM = 100; char buffer[MAXDIM]; unsigned char temp_a[MAXDIM][MAXDIM],temp_b[MAXDIM][MAXDIM]; double nom_term1[MAXDIM][MAXDIM]; int i,j; int l,m, startrow, startcol, startindex, lkm_a, lkm_b; double nominator, denominator1, denominator2, meanA, meanB; // j = sprintf(buffer,"Passed arguments width: %d height: %d wt: %d ht: %d \n", width, height, wt, ht); // MessageBox ( NULL, buffer,"MyFunc_corima",MB_OK); lkm_a=0; meanA=0.0; // Loop the template area, calculate mean of template, omit all zero values (outside ! for (l=0;l<=wt-1;l++) { // cols for (m=0;m<=ht-1;m++) { // rows temp_a[l][m]=temp[m*wt+l]; // minimum is zero, maximum is (ht-1)*wt+wt-1 <=> wt((ht-1)+1)-1= wt*ht-1, in VB-declaration wt*ht elemnets if (temp_a[l][m]!=0) { meanA += double(temp_a[l][m]); lkm_a++; } } } meanA /= double(lkm_a); // Too be sure we do not travel outside the image area, main loop is restricted to the area // with template width & height removed from top&bottom, left & right. // Constants denominator1=0.0; for (l=0;l<=wt-1;l++) { // loop template columns for (m=0;m<=ht-1;m++) { // loop template rows if (temp_a[l][m]!=0) // it's a valid element { denominator1 += (double(temp_a[l][m]) - meanA) * (double(temp_a[l][m]) - meanA); nom_term1[l][m]= double(temp_a[l][m]) - meanA; } } } for (i=wt;i<=(width-1)-wt;i++) { // i loops columns for (j=ht;j<=(height-1)-ht;j++) { // j loops rows // Cross-correlation is now computed for point (i,j), => rkuva[j*width+i] // c_col and c_row give the location of the center point of the template startrow = j-(c_row); // the location of the upper left corner of the template on the image startcol = i-(c_col); // " startindex = startrow*width+startcol; // " index in akuva[] lkm_b=0; meanB=0.0; for (l=0;l<=wt-1;l++) { // loop template columns for (m=0;m<=ht-1;m++) { // loop template rows if (temp_a[l][m]!=0) // it's a valid element { lkm_b++; temp_b[l][m]=akuva[startindex+(m*width)+l]; meanB += double(temp_b[l][m]); } } } meanB /= double(lkm_b); nominator=0.0; denominator2=0.0; for (l=0;l<=wt-1;l++) { // loop cols for (m=0;m<=ht-1;m++) { // loop rows if (temp_a[l][m]!=0) { nominator += nom_term1[l][m] * (double(temp_b[l][m]) - meanB); denominator2 += (double(temp_b[l][m]) - meanB) * (double(temp_b[l][m]) - meanB); } } } if ((denominator1 * denominator2) < 0.000002 ) { j = sprintf(buffer,"Trying to divide by zero in Myfunc_corima"); MessageBox ( NULL, buffer,"MyFunc_corima",MB_OK); return -1.0; } rkuva[j*width+i] = nominator/pow((denominator1 * denominator2),0.5); } // main loop rows } // main loop cols return 1.0; } double __stdcall MyFunc_Corima_Fourier(unsigned char *akuva, short na,short ma, unsigned char *temp, short wt, short ht,double *rkuva) { // akuva width na, height ma, indexing starts from [0], passed as kuva(1,1) in VBasic // temp passed temp[0] as temp(1,1), width wt, height ht // answer in double rkuva(1,1) in VB passed as rkuva[0] (C) // Compute crosscorr-image in rkuva[] na* nb // Image akuva[] na x ma // Template temp[] wt x ht // store values in FFTt[] (template) // store values in FFTima[] (image) // w_padd & H_apdd tell the numebr of zero's to padd FFTima with double FFTt[524288] ; // max size 512x512 x 2 (complex array) /* double FFTima[long(524288)+1]; // max size 512x512 x 2 double FFTres[long(524288)+1] ; // max size 512x512 x 2 (stores the FFT'd product of temp x image* int i,j, i_re,i_im,ix,jx, lkm_c; int l,m, start_row, start_col, start_index, jump, lkm_a, lkm_b; double os, nim1, nim2, meanA, meanB; int NN[3]; int NDIM, ISIGN; char buffer[100]; j = sprintf(buffer,"Rajat %d %d %d %d \n", na, ma, wt, ht); MessageBox ( NULL, buffer,"rajat",MB_OK); for (l=1;l<=long(2)*long(na)*long(ma);l=l++) // Fill FFTt-array with template's values & zeros { FFTt[l]=double(temp[l-1]); } NN[1]=int(na); NN[2]=int(ma); // os = Fourn(&FFTt[0], &NN[0], 2, 1); for (l=1;l<=int(2)*int(na)*int(ma);l=l++) // Fill FFTt-array with template's values & zeros { FFTima[l]=double(akuva[l-1]); } // os = Fourn(&FFTima[0], &NN[0], 2, 1); for (ix = 1;ix<=wt;ix++) { for (jx = 1;jx<=ht;jx++) { // Indeces i_im = (ix * wt * 2) - wt * 2 + 2 * jx; i_re = i_im - 1; // real part: FFTres[i_re] = FFTima[i_re] * FFTt[i_re] + FFTima[i_im] * FFTt[i_im]; // imaginary part: FFTres[i_im] = FFTima[i_re] * FFTt[i_im] - FFTt[i_re] * FFTima[i_im]; } } // Calculate the inverse Fourier spectrum // os = Fourn(&FFTres[0], &NN[0], 2, -1); rkuva[0]=0.0; for (l=1;l<=long(2)*long(na)*long(ma);l=l++) // Fill FFTt-array with template's values & zeros { rkuva[l-1] = (FFTres[l] / double(double(na) * double(ma))); } // i = sprintf(buffer,"Rajat %f \n", FFTres[1]); // MessageBox ( NULL, buffer,"rajat",MB_OK); */ return 1.0; } /* double __stdcall MyFunc_Comp_Conj_Mult(double *FFTima, double *FFTt, int dummy, double *FFTres, int WCW, int HCW) { int ix, jx, i_im, i_re, i; char buffer[100]; i = sprintf(buffer,"Rajat %d %d \n", WCW, HCW); MessageBox ( NULL, buffer,"rajat",MB_OK); for (ix=1;ix<=WCW;ix++) { for (jx=1;jx<=HCW;jx++) { // real part is at index (ix * WCW * 2) - WCW * 2 + 2 * jx - 1 // imag part is at index (ix * WCW * 2) - WCW * 2 + 2 * jx i_im = (ix * WCW * 2) - WCW * 2 + 2 * jx; i_re = i_im - 1; // real part: FFTres[i_re] = FFTima[i_re] * FFTt[i_re] + FFTima[i_im] * FFTt[i_im]; // imaginary part: FFTres[i_im] = FFTima[i_re] * FFTt[i_im] - FFTt[i_re] * FFTima[i_im]; } } return 1.0; } */ double __stdcall MyFunc_Fourn(double *Data, int *NN, int NDIM, int ISIGN) { double WR, WI, WPR, WPI, WTEMP, THETA; long NTOT, IDIM, NPREV, N, NREM, IP1, IP2, IP3; long I2REV, I2, I1, I3, I3REV, IBIT; long IFP1, K1, K2, IFP2; double TEMPR, TEMPI; NTOT = 1; for (IDIM=1; IDIM<=NDIM;IDIM++) { // loop thru dimensions (2), compute total number of complex values NTOT = NTOT * NN[IDIM]; // to be stored / computed into data() complex -array, in C arrays start at [0] } NPREV = 1; for (IDIM=1;IDIM<=NDIM;IDIM++) // Main loop over the dimensions NDIM { N = NN[IDIM]; NREM = (NTOT) / (N * NPREV); IP1 = 2 * NPREV; IP2 = IP1 * N; IP3 = IP2 * NREM; I2REV = 1; for (I2 = 1;I2<=IP2; I2=I2+IP1) { // Bit reversal section of the routine if (I2 < I2REV) { for (I1=I2; I1<=(I2 + IP1 - 2);I1=I1+2) { for (I3=I1;I3<=IP3;I3=I3+IP2) { I3REV = I2REV + I3 - I2; TEMPR = Data[I3]; TEMPI = Data[I3 + 1]; Data[I3] = Data[I3REV]; Data[I3 + 1]= Data[I3REV + 1]; Data[I3REV] = TEMPR; Data[I3REV + 1] = TEMPI; } } } IBIT = IP2 / 2; line1: if ((IBIT >= IP1) && (I2REV > IBIT)) { I2REV = I2REV - IBIT; IBIT = IBIT / 2; goto line1; } I2REV = I2REV + IBIT; } // bit reversal section stops here. IFP1 = IP1; line2: if (IFP1 < IP2) { IFP2 = 2 * IFP1; THETA = ISIGN * 2.0 * PI / (IFP2 / IP1) ; // initialize for troigonometric recurrence WPR = -2.0 * sin(0.5 * THETA) * sin(0.5 * THETA) ; WPI = sin(THETA); WR = 1.0; WI = 0.0; for (I3 = 1;I3<=IFP1;I3=I3+IP1) { for (I1 = I3;I1<=(I3 + IP1 - 2);I1=I1+2) { for (I2 = I1;I2<=IP3;I2=I2+IFP2) { K1 = I2; K2 = K1 + IFP1; TEMPR = WR * Data[K2] - WI * Data[K2 + 1]; TEMPI = WR * Data[K2 + 1] + WI * Data[K2]; Data[K2] = Data[K1] - TEMPR; Data[K2 + 1] = Data[K1 + 1] - TEMPI; Data[K1] = Data[K1] + TEMPR; Data[K1 + 1] = Data[K1 + 1] + TEMPI; } } WTEMP = WR; WR = WR * WPR - WI * WPI + WR; WI = WI * WPR + WTEMP * WPI + WI; } IFP1 = IFP2; goto line2; } NPREV = N * NPREV; } return -1.0; } long __stdcall MyFunc_Make3dpoints(double origoX, double origoY, double origoZ, double gridXextent, double gridYextent, double gridZextent, double gridXtess, double gridYtess, double gridZtess, double XYrotangle, char *filename, long stringlength) { FILE *f; double XYrotangle_sin; double XYrotangle_cos; double x1;double y1; double z1; double x, y, z; int l; XYrotangle_sin = sin(XYrotangle * 3.1415926 / 180); XYrotangle_cos = cos(XYrotangle * 3.1415926 / 180); f = fopen(filename,"w"); if (f==NULL) return 0; l=0; for (x=-gridXextent/2.0;x<=gridXextent/2.0;x=x+gridXtess) { for (y=-gridYextent/2.0;y<=gridYextent/2.0;y=y+gridYtess) { for (z=-gridZextent/2.0;z<=gridZextent/2.0;z=z+gridZtess) { l = l+1; x1 = XYrotangle_cos * x - XYrotangle_sin * y + origoX; y1 = XYrotangle_sin * x + XYrotangle_cos * y + origoY; z1 = origoZ+z; fprintf(f, "%.3f,%.3f,%.3f\n",x1,y1,z1); } } } fclose(f); return l; } long __stdcall MyFuncSplittoRGB(long kork, long lev, char *filename, char *filenameRed,char *filenameBlue,char *filenameGreen) { // 2.Dec.2001, Ilkka Korpela // Routine to read a RAW (rgb) -file of the size width: lev, height: kork and output // its channel split in three binary files. The file names are passed as strings (NUL-ended). struct RGBtriplet { unsigned char r; unsigned char g; unsigned char b; }; FILE *fpIn, *fpRed, *fpGreen, *fpBlue; // file pointers char buffer[100]; int i,j; RGBtriplet ImageRow[16384]; unsigned char RedRow[16384],GreenRow[16384],BlueRow[16384]; // Let the user know if the dimensions were correctly passed. i = sprintf(buffer,"Split: arvo kork %d \n", kork); MessageBox ( NULL, buffer,"Slpit",MB_OK); i = sprintf(buffer,"Split: arvo lev %d \n", lev); MessageBox ( NULL, buffer,"Slpit",MB_OK); // Open files, return if failure fpIn = fopen(filename, "rb"); if (fpIn==NULL) { i = sprintf(buffer,"Split: luettavan tiedoston avaus ei onnistunut! \n"); MessageBox ( NULL, buffer,"Slpit",MB_OK); return 0; } fpRed = fopen(filenameRed, "wb"); if (fpRed==NULL) { i = sprintf(buffer,"Split:kirjoitettavan (Red) tiedoston avaus ei onnistunut! \n"); MessageBox ( NULL, buffer,"Slpit",MB_OK); fclose(fpIn); return 0; } fpGreen = fopen(filenameGreen, "wb"); if (fpGreen==NULL) { i = sprintf(buffer,"Split:kirjoitettavan (Green) tiedoston avaus ei onnistunut! \n"); MessageBox ( NULL, buffer,"Split",MB_OK); fclose(fpIn); fclose(fpRed); return 0; } fpBlue = fopen(filenameBlue, "wb"); if (fpBlue==NULL) { i = sprintf(buffer,"Split:kirjoitettavan (Blue) tiedoston avaus ei onnistunut! \n"); MessageBox ( NULL, buffer,"Slpit",MB_OK); fclose(fpIn); fclose(fpRed); fclose(fpGreen); return 0; } // Start reading the input file row by row for (i=0; i<=int(kork)-1; i++) { fread(&ImageRow[0], sizeof(ImageRow[0])*int(lev), 1, fpIn); for (j=0; j<=int(lev)-1;j++) { RedRow[j]=ImageRow[j].r; GreenRow[j]=ImageRow[j].g; BlueRow[j]=ImageRow[j].b; } fwrite(&RedRow[0], sizeof(unsigned char)*int(lev), 1, fpRed ); fwrite(&GreenRow[0], sizeof(unsigned char)*int(lev), 1, fpGreen ); fwrite(&BlueRow[0], sizeof(unsigned char)*int(lev), 1, fpBlue ); } fclose(fpIn); fclose(fpRed); fclose(fpGreen); fclose(fpBlue); return 1; } double __stdcall MyFuncReadBinaryFiles(unsigned char *bkuva, int lstartrow, int rstartrow, int lstartcol, int rstartcol, int lendcol, int nwidth) { int i,m; char buffer[100]; unsigned char lrow[1200*2], rrow[1200*2]; char nimiL[16+1]="d:\\test_left.raw"; char nimiR[17+1]="d:\\test_right.raw"; FILE *fpInL; FILE *fpInR; MessageBox ( NULL, nimiL,"Msgbox from withn pascall.DLL",MB_OK); MessageBox ( NULL, nimiR,"Msgbox from withn pascall.DLL",MB_OK); fpInL = fopen("d:\\test_left.raw", "rb"); if (fpInL==NULL) { i = sprintf(buffer,"Normalized: Opening the RAW input file failed left, \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); return 1; } fpInR = fopen(nimiR, "rb"); if (fpInR==NULL) { i = sprintf(buffer,"Normalized: Opening the RAW input file failed right, \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpInL); return 1; } int k,j; long rl, rr, paikkal, paikkar; k = -2; for(i = 0;i<=1199;i++) { k = k + 2; rl = lstartrow + k; rr = rstartrow + k; paikkal = long(11736 - rl) * long(nwidth) + long(lstartcol) ; paikkar = long(11736 - rr) * long(nwidth) + long(rstartcol) ; fseek(fpInL, paikkal, SEEK_SET); fread(&lrow[0], sizeof(unsigned char)*1200*2, 1, fpInL); fseek(fpInR, paikkal, SEEK_SET); fread(&rrow[0], sizeof(unsigned char)*1200*2, 1, fpInR); j = -1; for (m = 0; m <= (lendcol - lstartcol);m=m+2) { j = j + 1; if (lrow[j] * 1.4 < 255) lrow[j] = lrow[j] * 1.4; bkuva[j+i*1200*3]= rrow[m]; bkuva[j+i*1200*3+1]=lrow[m]; bkuva[j+i*1200*3+2]=0; } } fclose(fpInL); fclose(fpInR); return 1; } double __stdcall MyFuncCreateNormalizedImage(double c, int Nwidth, int Nheight, int Owidth, int Oheight, char *filename_in, char *filename_out, double *pixelsize, double *af, double *R) { // af [] coeffiecients of the interrior orientation // R [] coeffiecienst of the 3x3 normalizing matrice struct RGBtriplet { unsigned char r; unsigned char g; unsigned char b; }; int i, row, col; // long maxpaikka; long paikka; double a_, b_, c_, d_, e_, f_, x_n, y_n, ima_x, ima_y, p_x, p_y; // double ec, fb, cd, af2 // double rowb, rowa, rowc; // double a1, a2, a3, a4; // double jako; FILE *fpIn; FILE *fpOut; char buffer[150]; unsigned char *norm_row; unsigned char *InImage[20000]; // double r4r8, r1r8, r2r7, r5r7, r4r6, r3r7, r0r7, r1r6, r3r8, r2r6, r0r8, r5r6; double ymax, xmin, pixelwidth; int color; int InRow, InCol; int j; unsigned int numcount; xmin=pixelsize[0]; ymax=pixelsize[1]; pixelwidth=pixelsize[2]; color=int(pixelsize[3]); if (color < 1) goto BwImage; i = _heapmin(); fpIn = fopen(filename_in, "rb"); if (fpIn==NULL) { i = sprintf(buffer,"Normalized: Opening the RAW input file failed, \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpIn); return 0; } fpOut = fopen(filename_out, "wb"); if (fpOut==NULL) { i = sprintf(buffer,"Opening the RAW output file failed\n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpOut); return 0; } norm_row = (unsigned char *) calloc(Nwidth*3, sizeof(unsigned char)); if (norm_row==NULL) { i = sprintf(buffer,"Normalized: Could not allocate memory: norm_row \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpIn); fclose(fpOut); return 0; } for (i=0;i<=Oheight-1;i++) { InImage[i] = (unsigned char *) calloc(Owidth*3,sizeof(unsigned char)); if (InImage[i]==NULL) { j = sprintf(buffer,"Normalized: Could not allocate enough memory, row: InImage %d \n",i); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpIn); fclose(fpOut); for (j=0;j<=i;j++) { free(InImage[j]); } free(norm_row); return 0; } } a_=af[0]; b_=af[1]; c_=af[2]; d_=af[3]; e_=af[4]; f_=af[5]; // Read the whole image by rows numcount = 0; paikka = 0; for (i=0;i<=Oheight-1;i++) { paikka=i*Owidth*3; fseek(fpIn, paikka, SEEK_SET); numcount += fread(&InImage[i][0], sizeof(unsigned char)*Owidth*3, 1, fpIn); } // i = sprintf(buffer,"InImage READ Bytes: %d \n", (numcount) ); // MessageBox ( NULL, buffer,"rajat",MB_OK); // Now compute OutImage for (row = 0;row<=Nheight-1;row++) { // y-coordinate in the normalized coordinates y_n = ymax - double(row)*pixelwidth + pixelwidth/2; for (col = 0;col<=Nwidth*3-1;col=col+3) { // Using 14 um pixel size, 1st row (0) y = ymin, row (height-1) y = ymax x_n = xmin + double(col/3)*pixelwidth + pixelwidth/2.0; // These are the original camera coordinates ima_x=c*(-c*R[3]*R[7]+c*R[4]*R[6]-R[3]*y_n*R[8]-x_n*R[5]*R[7]+R[4]*x_n*R[8]+y_n*R[5]*R[6])/(-c*R[3]*R[1]+c*R[4]*R[0]-R[3]*y_n*R[2]+R[4]*x_n*R[2]+y_n*R[5]*R[0]-x_n*R[5]*R[1]); ima_y=(x_n*R[2]*R[7]-x_n*R[8]*R[1]-R[6]*c*R[1]-R[2]*y_n*R[6]+c*R[0]*R[7]+R[8]*y_n*R[0])*c/(-c*R[3]*R[1]+c*R[4]*R[0]-R[3]*y_n*R[2]+R[4]*x_n*R[2]+y_n*R[5]*R[0]-x_n*R[5]*R[1]); // These are the pixel coordinates in the original image, whete to sample p_x = a_ * ima_x + b_ * ima_y + c_; p_y = d_ * ima_x + e_ * ima_y + f_; if ( (p_x < 0) || (p_x > (Owidth-1)) || (p_y < 0) || (p_y > (Oheight-1)) ) { norm_row[col] = 0; norm_row[col+1] = 0; norm_row[col+2] = 0; goto skip; } // This is the position in the file, note inverted row-direction InRow = long(Oheight-1-p_y); InCol = long(p_x)*3; norm_row[col] = InImage[InRow][InCol]; norm_row[col+1] = InImage[InRow][InCol+1]; norm_row[col+2] = InImage[InRow][InCol+2]; skip: ; } // Print a row of data in the normalized image fseek(fpOut, long(Nheight-1-row) * long(Nwidth)*3, SEEK_SET); fwrite(&norm_row[0], sizeof(unsigned char)*Nwidth*3, 1, fpOut); } free (norm_row); for (i=0;i<=Oheight-1;i++) { free (InImage[i]); } fclose(fpOut); fclose(fpIn); i = _heapmin(); return 1; // Here starts the section for BW-images (grayscale) BwImage: // i = sprintf(buffer,"InImage BWFile: %d \n", (color) ); // MessageBox ( NULL, buffer,"rajat",MB_OK); i = _heapmin(); fpIn = fopen(filename_in, "rb"); if (fpIn==NULL) { i = sprintf(buffer,"Normalized: Opening the RAW input file failed, \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpIn); return 0; } fpOut = fopen(filename_out, "wb"); if (fpOut==NULL) { i = sprintf(buffer,"Opening the RAW output file failed\n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpOut); return 0; } norm_row = (unsigned char *) calloc(Nwidth, sizeof(unsigned char)); if (norm_row==NULL) { i = sprintf(buffer,"Normalized: Could not allocate memory: norm_row \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpIn); fclose(fpOut); return 0; } for (i=0;i<=Oheight-1;i++) { InImage[i] = (unsigned char *) calloc(Owidth, sizeof(unsigned char)); if (InImage[i]==NULL) { j = sprintf(buffer,"Normalized: Could not allocate enough memory, row: InImage %d \n",i); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpIn); fclose(fpOut); for (j=0;j<=i;j++) { free(InImage[j]); } free(norm_row); return 0; } } a_=af[0]; b_=af[1]; c_=af[2]; d_=af[3]; e_=af[4]; f_=af[5]; // Read the whole image by rows numcount = 0; paikka = 0; for (i=0;i<=Oheight-1;i++) { paikka=i*Owidth; fseek(fpIn, paikka, SEEK_SET); numcount += fread(&InImage[i][0], sizeof(unsigned char)*Owidth, 1, fpIn); } // Now compute OutImage for (row = 0;row<=Nheight-1;row++) { // y-coordinate in the normalized coordinates y_n = ymax - double(row)*pixelwidth + pixelwidth/2; for (col = 0;col<=Nwidth-1;col++) { // Using 14 um pixel size, 1st row (0) y = ymin, row (height-1) y = ymax x_n = xmin + double(col)*pixelwidth + pixelwidth/2.0; // These are the original camera coordinates ima_x=c*(-c*R[3]*R[7]+c*R[4]*R[6]-R[3]*y_n*R[8]-x_n*R[5]*R[7]+R[4]*x_n*R[8]+y_n*R[5]*R[6])/(-c*R[3]*R[1]+c*R[4]*R[0]-R[3]*y_n*R[2]+R[4]*x_n*R[2]+y_n*R[5]*R[0]-x_n*R[5]*R[1]); ima_y=(x_n*R[2]*R[7]-x_n*R[8]*R[1]-R[6]*c*R[1]-R[2]*y_n*R[6]+c*R[0]*R[7]+R[8]*y_n*R[0])*c/(-c*R[3]*R[1]+c*R[4]*R[0]-R[3]*y_n*R[2]+R[4]*x_n*R[2]+y_n*R[5]*R[0]-x_n*R[5]*R[1]); // These are the pixel coordinates in the original image, whete to sample p_x = a_ * ima_x + b_ * ima_y + c_; p_y = d_ * ima_x + e_ * ima_y + f_; if ( (p_x < 0) || (p_x > (Owidth-1)) || (p_y < 0) || (p_y > (Oheight-1)) ) { norm_row[col] = 0; goto skip2; } // This is the position in the file, note inverted row-direction InRow = long(Oheight-1-p_y); InCol = long(p_x); norm_row[col] = InImage[InRow][InCol]; skip2: ; } // Print a row of data in the normalized image fseek(fpOut, long(Nheight-1-row) * long(Nwidth), SEEK_SET); fwrite(&norm_row[0], sizeof(unsigned char)*Nwidth, 1, fpOut); } // i = sprintf(buffer,"Now freeing memory: %d \n", (numcount) ); // MessageBox ( NULL, buffer,"rajat",MB_OK); free (norm_row); for (i=0;i<=Oheight-1;i++) { free (InImage[i]); } fclose(fpOut); fclose(fpIn); i = _heapmin(); return 1; } double __stdcall MyFuncCreateBmp(char *lfile, char *rfile, char *ofile, int index, int lstartcol, int rstartcol, int lstartrow, int rstartrow, int lendcol, int lendrow, int rendcol, int rendrow, double pan_x, double pan_y, int nwidth, int nheight, int stereoheight, int stereowidth, int colormodel) { // This routine reads a RAW-files and makes it into a BMP-file (Anaglyph) struct BITMAPFILEHEADER { short bfType; int bfSize; short bfReserved1; short bfReserved2; int bfOffBits; }; struct BITMAPINFOHEADER { int biSize; int biWidth; int biHeight; short biPlanes; short biBitCount; int biCompression; int biSizeImage; int biXPelsPerMeter; int biYPelsPerMeter; int biClrUsed; int biClrImportant; }; struct RGBtriplet { unsigned char r; unsigned char g; unsigned char b; }; struct BGRQ { unsigned char b; unsigned char g; unsigned char r; }; int i,j, w_byte_array, h_byte_array; int lisa, padding; int shift; FILE *fpInL; FILE *fpInR; FILE *fpOut; BITMAPFILEHEADER filehederi; BITMAPINFOHEADER hederi; char buffer[100]; unsigned char *p_lrow[2000]; //unsigned char *p_lrowbyte; //unsigned char *p_rrowbyte; unsigned char *p_rrow[2000]; unsigned char *p_bkuva[2000]; h_byte_array = stereoheight; w_byte_array = stereowidth; shift = 100; i = _heapmin(); lisa = (w_byte_array * 3) % 4; if (lisa == 0) padding = 0; else padding = 4 - lisa; int rl, rr, paikkal, paikkar, colorm, numcount; int k, m; if (colormodel==1) colorm=3; else colorm=1; // Allocate storage for (i=0;i<=stereoheight-1;i++) { p_bkuva[i] = (unsigned char *) calloc(stereowidth*3,sizeof(unsigned char)); if (p_bkuva[i]==NULL) { j = sprintf(buffer,"Normalized: Could not allocate memory, row: p_bkuva \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); for (j=0;j<=i;j++) { free(p_bkuva[j]); } return 0; } } fpInL = fopen(lfile, "rb"); if (fpInL==NULL) { i = sprintf(buffer,"Opening the RAW input file failed, \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); return 0; } fpInR = fopen(rfile, "rb"); if (fpInR==NULL) { i = sprintf(buffer,"Opening the RAW input file failed, \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpInL); return 0; } fpOut = fopen(ofile, "wb+"); if (fpOut==NULL) { i = sprintf(buffer,"Opening the BMP output file failed\n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpInL); fclose(fpInR); return 0; } //filehederi.bfType = 0x4D42; "BM" filehederi.bfType = 0x4D42; //2 filehederi.bfSize = (w_byte_array + padding) * h_byte_array * 3; //4 filehederi.bfReserved1 = 0; //2 filehederi.bfReserved2 = 0; //2 // Offset from the BitMapFileheader to the Bitmap Bits filehederi.bfOffBits = 54; // 55 (54 byte offset, read 55th as first data-byte) //24 // Rem akind of Dib - bitmap , header is defined here hederi.biSize = 40; // 4 bytes hederi.biWidth = w_byte_array; // 4 hederi.biHeight = -h_byte_array; // 4 // Rem By making Height negative we assume (WINDOWS programs assume) it's a BMP with Low-left corner on first scan-line!! hederi.biPlanes = 1; // 2 bytes hederi.biBitCount = 24; // 2 hederi.biCompression = 0; // 4 hederi.biSizeImage = 0; // 4 hederi.biXPelsPerMeter = 0; // 4 hederi.biYPelsPerMeter = 0; // 4 hederi.biClrUsed = 0; // 4 hederi.biClrImportant = 0; // 4 // Write the headers of the BMP-file (first 54 bytes) fseek(fpOut, 0, SEEK_SET); fwrite(&filehederi.bfType, 2, 1, fpOut); fseek(fpOut, 2, SEEK_SET); fwrite(&filehederi.bfSize, 4, 1, fpOut); fseek(fpOut, 2+4, SEEK_SET); fwrite(&filehederi.bfReserved1, 2, 1, fpOut); fseek(fpOut, 2+4+2, SEEK_SET); fwrite(&filehederi.bfReserved2, 2, 1, fpOut); fseek(fpOut, 2+4+2+2, SEEK_SET); fwrite(&filehederi.bfOffBits, 4, 1, fpOut); fseek(fpOut, 14, SEEK_SET); fwrite(&hederi.biSize, 4, 1, fpOut); fseek(fpOut, 14+4, SEEK_SET); fwrite(&hederi.biWidth, 4, 1, fpOut); fseek(fpOut, 18+4, SEEK_SET); fwrite(&hederi.biHeight, 4, 1, fpOut); fseek(fpOut, 22+4, SEEK_SET); fwrite(&hederi.biPlanes, 2, 1, fpOut); fseek(fpOut, 26+2, SEEK_SET); fwrite(&hederi.biBitCount, 2, 1, fpOut); fseek(fpOut, 28+2, SEEK_SET); fwrite(&hederi.biCompression, 4, 1, fpOut); fseek(fpOut, 30+4, SEEK_SET); fwrite(&hederi.biSizeImage, 4, 1, fpOut); fseek(fpOut, 34+4, SEEK_SET); fwrite(&hederi.biXPelsPerMeter, 4, 1, fpOut); fseek(fpOut, 38+4, SEEK_SET); fwrite(&hederi.biYPelsPerMeter, 4, 1, fpOut); fseek(fpOut, 42+4, SEEK_SET); fwrite(&hederi.biClrUsed, 4, 1, fpOut); fseek(fpOut, 46+4, SEEK_SET); fwrite(&hederi.biClrImportant, 4, 1, fpOut); numcount=0; int addcolor; if (colorm==1) addcolor=0; else addcolor=2; for (i=0;i<=stereoheight-1;i++) { p_lrow[i] = (unsigned char *) calloc(nwidth*colorm,sizeof(unsigned char)); if (p_lrow[i]==NULL) { j = sprintf(buffer,"Normalized: Could not allocate memory, row: p_lrow \n"); MessageBox ( NULL, buffer,"Msgbox from withn pascall.DLL",MB_OK); fclose(fpInL); fclose(fpInR); fclose(fpOut); return 0; } } for (i=0;i<=stereoheight-1;i++) { p_rrow[i] = (unsigned char *) calloc(nwidth*colorm,sizeof(unsigned char)); if (p_rrow[i]==NULL) { j = sprintf(buffer,"Normalized: Could not allocate memory, row: p_rrow \n"); MessageBox ( NULL, buffer,"Msgbox from within pascall.DLL",MB_OK); fclose(fpInL); fclose(fpInR); fclose(fpOut); return 0; } } int c1, c2, c3, c4; c1=(nwidth) * colorm; c2=(lstartcol) * colorm; c3=(rstartcol) * colorm; c4=sizeof(unsigned char)*nwidth*colorm; for (i = 0; i <= stereoheight-1;i++) { rl = lstartrow + i; paikkal = (nheight - rl) * c1 + c2; if ((paikkal < 0) || (paikkal > nheight * nwidth * colorm)) goto skipleft; fseek(fpInL,paikkal+1,SEEK_SET); numcount = fread(&p_lrow[i][0], c4, 1, fpInL); skipleft:; } for (i = 0; i <= stereoheight-1;i++) { rr = rstartrow + i; paikkar = (nheight - rr) * c1 + c3; // Read a row from the left image, colorm tells if grayscale or rgb if ((paikkar < 0) || (paikkar > nheight * nwidth * colorm)) goto skiprigth; fseek(fpInR,paikkar+1,SEEK_SET); numcount = fread(&p_rrow[i][0], c4, 1, fpInR); skiprigth:; } for (i = 0; i <= stereoheight-1;i++) { k = -colorm; for (m = 0; m <= (lendcol - lstartcol -1); m++) { k = k + colorm; // Compute average if colormodel is RGB else take graylevels switch (colormodel) { case 0: p_bkuva[i][m*3] = 0 ; // blue-cmoponenet p_bkuva[i][m*3+1] = p_rrow[i][k + addcolor]; // green p_bkuva[i][m*3+2] = p_lrow[i][k]; // red break; case 1: p_bkuva[i][m*3] = 0 ; // blue p_bkuva[i][m*3+1] = (p_rrow[i][k]+p_rrow[i][k+1]+p_rrow[i][k+2])/3; // green p_bkuva[i][m*3+2] = (p_lrow[i][k]+p_lrow[i][k+1]+p_lrow[i][k+2])/3; // red break; } } fseek(fpOut,54+i*stereowidth*3,SEEK_SET); fwrite(&p_bkuva[i][0], 3 * stereowidth, 1, fpOut); } // Write a row in the BMP-file // We're done, close files and free allocated storage & return. fclose(fpInL); fclose(fpInR); fclose(fpOut); for (j=0;j<=stereoheight-1;j++) { free(p_bkuva[j]); free(p_lrow[j]); free(p_rrow[j]); } return 1; // End of function } long __stdcall MyFuncReadTiffHeader(long *kork, long *lev, long *offset, long *konfiguraatio,int *kompressio, int *kanavia,char *filename, long *osoitteet ) { typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; typedef struct Tag { WORD nNumber; WORD nType; DWORD dwLen; DWORD dwData; } TAG; typedef struct Hdr { BYTE cProcessor[2]; WORD nVersion; DWORD dwIFD; } HDR; typedef struct Img { DWORD ImageWidth; DWORD ImageLength; WORD Compression; DWORD StripOffset; WORD SamplesPerPixel; WORD PlanarConfig; } IMG; short int i,j,nTags, nNumTags, NTiles; DWORD dwIFD; HDR Hdr; IMG Img; FILE *fpIn; TAG Tag; char buffer[100]; fpIn = NULL; long TileWidth,RowsPerStrip,StripByteCounts,TileHeight,TileOffsets,TileByteCounts; short BitsPerSample, PhotometricInterpretation; i = sprintf(buffer,"Harmiton ilmoitus Dll:n sisältä\n"); MessageBox ( NULL, buffer,"Tageja",MB_OK); fpIn = fopen(filename, "rb"); if (fpIn==NULL) return 0; /* Initialize and read header from input file */ memset(&Hdr, 0, sizeof(Hdr)); fread(&Hdr, sizeof(Hdr), 1, fpIn); /* Read Offset to 0'th IFD*/ dwIFD = Hdr.dwIFD; i = sprintf(buffer,"Rajat %d \n", dwIFD); MessageBox ( NULL, buffer,"Tageja",MB_OK); /* Seek IFD address and read number of tags */ fseek(fpIn, dwIFD, SEEK_SET); fread(&nNumTags, sizeof(nNumTags), 1, fpIn); /* Read Tags */ i = sprintf(buffer,"Rajat %d \n", nNumTags); MessageBox ( NULL, buffer,"Tageja",MB_OK); nTags = nNumTags; for (j = 0; j < nTags; j++) { /* Read tag */ fread(&Tag, sizeof(TAG), 1, fpIn); switch (Tag.nNumber) { case 256: Img.ImageWidth = Tag.dwData; break; case 257: Img.ImageLength = Tag.dwData; break; case 258: BitsPerSample = (WORD)Tag.dwData;break; case 259: Img.Compression = (WORD)Tag.dwData;break; case 262: PhotometricInterpretation=(WORD)Tag.dwData;break; case 273: Img.StripOffset = Tag.dwData; break; case 277: Img.SamplesPerPixel = (WORD)Tag.dwData;break; case 278: RowsPerStrip=(WORD)Tag.dwData;break; case 279: StripByteCounts=Tag.dwData;break; case 284: Img.PlanarConfig = (WORD)Tag.dwData;break; case 322: TileWidth=Tag.dwData; i = sprintf(buffer,"Tiilitetty: TileWidth= %d \n",TileWidth); MessageBox ( NULL, buffer,"Tageja",MB_OK); break; case 323: TileHeight=Tag.dwData; i = sprintf(buffer,"Tiilitetty: TileHeight= %d \n",TileHeight); MessageBox ( NULL, buffer,"Tageja",MB_OK); break; case 324: TileOffsets=Tag.dwData; NTiles = Tag.dwLen; i = sprintf(buffer,"Tiilitetty: TileOffsets and Tiles= %d %d\n",TileOffsets,NTiles); MessageBox ( NULL, buffer,"Tageja",MB_OK); // There are Tag.dwlen tiles, the first offset (tile(0)) is at TileOffsets break; case 325: TileByteCounts=Tag.dwData; i = sprintf(buffer,"Tiilitetty: TileByteCounts= %d \n",TileByteCounts); MessageBox ( NULL, buffer,"Tageja",MB_OK); break; } i = sprintf(buffer,"Luettu Tagi %d %d %d %d \n", (WORD)Tag.nNumber, (WORD)Tag.nType ,(WORD)Tag.dwLen, (DWORD)Tag.dwData); MessageBox ( NULL, buffer,"rajat",MB_OK); } fseek(fpIn, TileOffsets, SEEK_SET); // The first tile offset is here // NOTE Here we explicitly say the number of tiles (its the value in Tag.dwLen of Tag 324!! for (i=0; i<=NTiles-1; i++) // loop thru all in order, fill osoitteet() array- (to be returned) { fread(&osoitteet[i], sizeof(osoitteet[i]), 1, fpIn); } i = sprintf(buffer,"Rajat %d %d %d %d %d %d\n", Img.ImageWidth, Img.ImageLength ,Img.Compression, Img.StripOffset,Img.SamplesPerPixel,Img.PlanarConfig); MessageBox ( NULL, buffer,"rajat",MB_OK); i = sprintf(buffer,"Rajat %d %d \n", RowsPerStrip ,PhotometricInterpretation); MessageBox ( NULL, buffer,"rajat",MB_OK); /* Check TIFF-format if (Img.Compression != 1) { fclose(fpIn); MessageBox (NULL,"Palaan. Kompressio vaara"," ",MB_OK); return(-1); } */ *lev = Img.ImageWidth; *kork = Img.ImageLength; *kanavia = Img.SamplesPerPixel; *offset=Img.StripOffset; *kompressio=Img.Compression; *konfiguraatio=Img.PlanarConfig; fcloseall(); return 1; /* OK */ }