Пример #1
0
Файл: main.c Проект: SNce/ARMaze
static void mouseEvent(int button, int state, int x, int y)
{
    unsigned char   *p, *p1;
    int             ssx, ssy, eex, eey;
    int             i, j, k;

    if( button == GLUT_RIGHT_BUTTON  && state == GLUT_UP ) {
        if( status == 0 ) {
            arVideoCapStop();
            arVideoClose();

            if( patt.loop_num > 0 ) {
                calc_distortion( &patt, xsize, ysize, dist_factor );
                printf("--------------\n");
                printf("Center X: %f\n", dist_factor[0]);
                printf("       Y: %f\n", dist_factor[1]);
                printf("Dist Factor: %f\n", dist_factor[2]);
                printf("Size Adjust: %f\n", dist_factor[3]);
                printf("--------------\n");
                status = 2;
                check_num = 0;
                print_comment(5);
            }
            else {
                glutDestroyWindow( win );
                exit(0);
            }
        }
        else if( status == 1 ) {
            if( patt.loop_num == 0 ) {printf("error!!\n"); exit(0);}
            patt.loop_num--;
            free( patt.point[patt.loop_num] );
            free( patt.savedImage[patt.loop_num] );
            status = 0;
            point_num = 0;
            arVideoCapStart();

            if( patt.loop_num == 0 ) print_comment(0);
             else                    print_comment(4);
        }
    }

    if( button == GLUT_LEFT_BUTTON  && state == GLUT_DOWN ) {
        if( status == 1 && point_num < patt.h_num*patt.v_num ) {
            sx = ex = x;
            sy = ey = y;

            p  = &(patt.savedImage[patt.loop_num-1][(y*xsize+x)*AR_PIX_SIZE]);
            p1 = &(clipImage[0]);
#ifdef  AR_PIX_FORMAT_BGRA
            k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
            if( k < thresh ) k = 0;
                else k = 255;
            *(p1+0) = *(p1+1) = *(p1+2) = k;
#endif
#ifdef  AR_PIX_FORMAT_ABGR
            k = (255*3 - (*(p+1) + *(p+2) + *(p+3))) / 3;
            if( k < thresh ) k = 0;
                else k = 255;
            *(p1+1) = *(p1+2) = *(p1+3) = k;
#endif
#ifdef  AR_PIX_FORMAT_BGR
            k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
            if( k < thresh ) k = 0;
                else k = 255;
            *(p1+0) = *(p1+1) = *(p1+2) = k;
#endif
#ifdef  AR_PIX_FORMAT_RGBA
            k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
            if( k < thresh ) k = 0;
                else k = 255;
            *(p1+0) = *(p1+1) = *(p1+2) = k;
#endif
#ifdef  AR_PIX_FORMAT_RGB
            k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
            if( k < thresh ) k = 0;
                else k = 255;
            *(p1+0) = *(p1+1) = *(p1+2) = k;
#endif
        }
    }

    if( button == GLUT_LEFT_BUTTON  && state == GLUT_UP ) {
        if( status == 0 && patt.loop_num < LOOP_MAX ) {
            while( (p = (unsigned char *)arVideoGetImage()) == NULL ) {
                arUtilSleep(2);
            }
#ifdef USE_TEXMAP
            patt.savedImage[patt.loop_num] = (unsigned char *)malloc( xsize*tex1Ysize*AR_PIX_SIZE );
#else
            patt.savedImage[patt.loop_num] = (unsigned char *)malloc( xsize*ysize*AR_PIX_SIZE );
#endif
            if( patt.savedImage[patt.loop_num] == NULL ) exit(0);

            p1 = patt.savedImage[patt.loop_num];
            for(i=0;i<xsize*ysize*AR_PIX_SIZE;i++) *(p1++) = *(p++);
            arVideoCapStop();

            patt.point[patt.loop_num] = (CALIB_COORD_T *)malloc( sizeof(CALIB_COORD_T)*patt.h_num*patt.v_num );
            if( patt.point[patt.loop_num] == NULL ) exit(0);

            patt.loop_num++;
            status = 1;
            sx = sy = ex= ey = -1;

            print_comment(1);
        }
        else if( status == 1 && point_num == patt.h_num*patt.v_num ) {
            status = 0;
            point_num = 0;
            arVideoCapStart();

            printf("### No.%d ###\n", patt.loop_num);
            for( j = 0; j < patt.v_num; j++ ) {
                for( i = 0; i < patt.h_num; i++ ) {
                    printf("%2d, %2d: %6.2f, %6.2f\n", i+1, j+1,
                           patt.point[patt.loop_num-1][j*patt.h_num+i].x_coord,
                           patt.point[patt.loop_num-1][j*patt.h_num+i].y_coord);
                }
            }
            printf("\n\n");
            if( patt.loop_num < LOOP_MAX ) print_comment(4);
             else                          print_comment(6);
        }
        else if( status == 1 ) {
            if( sx < ex ) { ssx = sx; eex = ex; }
             else         { ssx = ex; eex = sx; }
            if( sy < ey ) { ssy = sy; eey = ey; }
             else         { ssy = ey; eey = sy; }

            patt.point[patt.loop_num-1][point_num].x_coord = 0.0;
            patt.point[patt.loop_num-1][point_num].y_coord = 0.0;
            p = clipImage;
            k = 0;
            for( j = 0; j < (eey-ssy+1); j++ ) {
                for( i = 0; i < (eex-ssx+1); i++ ) {
                    patt.point[patt.loop_num-1][point_num].x_coord += i * *(p+1);
                    patt.point[patt.loop_num-1][point_num].y_coord += j * *(p+1);
                    k += *(p+1);
                    p += AR_PIX_SIZE;
                }
            }
            if( k != 0 ) {
                patt.point[patt.loop_num-1][point_num].x_coord /= k;
                patt.point[patt.loop_num-1][point_num].y_coord /= k;
                patt.point[patt.loop_num-1][point_num].x_coord += ssx;
                patt.point[patt.loop_num-1][point_num].y_coord += ssy;
                point_num++;
            }
            sx = sy = ex= ey = -1;

            printf(" # %d/%d\n", point_num, patt.h_num*patt.v_num);
            if( point_num == patt.h_num*patt.v_num ) print_comment(2);
        }
        else if( status == 2 ) {
            check_num++;
            if( check_num == patt.loop_num ) {
                if(patt.loop_num >= 2) {
                    if( calc_inp(&patt, dist_factor, xsize, ysize, mat) < 0 ) {
                        printf("Calibration failed.\n");
                        exit(0);
                    }
                    save_param();
                }
                glutDestroyWindow( win );
                exit(0);
            }

            if( check_num+1 == patt.loop_num ) {
                printf("\nLeft Mouse Button: Next Step.\n");
            }
            else {
                printf("   %d/%d.\n", check_num+1, patt.loop_num);
            }
        }
    }
}
Пример #2
0
static void mouseEvent(int button, int state, int x, int y)
{
    AR2VideoBufferT *buff;
    unsigned char   *p, *p1;
    int             ssx, ssy, eex, eey;
    int             i, j, k;
    char            line[256];

	if( x < 0 ) x = 0;
	if( x >= xsize ) x = xsize-1;
	if( y < 0 ) y = 0;
	if( y >= ysize ) y = ysize-1;

	x *= SCALE;
	y *= SCALE;

    if( button == GLUT_RIGHT_BUTTON  && state == GLUT_UP ) {
        if( status == 0 ) {
            arVideoCapStop();
            arVideoClose();

            if( patt.loop_num > 0 ) {
                calc_distortion( &patt, xsize, ysize, aspect_ratio, dist_factor, dist_function_version );
                ARLOG("--------------\n");
				if (dist_function_version == 3) {
					ARLOG("Center X: %f\n", dist_factor[0]);
					ARLOG("       Y: %f\n", dist_factor[1]);
					ARLOG("Size Adjust: %f\n", dist_factor[2]);
					ARLOG("Aspect Ratio: %f\n", dist_factor[3]);
					ARLOG("Dist Factor1: %f\n", dist_factor[4]);
					ARLOG("Dist Factor2: %f\n", dist_factor[5]);
				} else if (dist_function_version == 2) {
					ARLOG("Center X: %f\n", dist_factor[0]);
					ARLOG("       Y: %f\n", dist_factor[1]);
					ARLOG("Size Adjust: %f\n", dist_factor[2]);
					ARLOG("Dist Factor1: %f\n", dist_factor[3]);
					ARLOG("Dist Factor2: %f\n", dist_factor[4]);
				} else if (dist_function_version == 1) {
					ARLOG("Center X: %f\n", dist_factor[0]);
					ARLOG("       Y: %f\n", dist_factor[1]);
					ARLOG("Size Adjust: %f\n", dist_factor[2]);
					ARLOG("Dist Factor: %f\n", dist_factor[3]);
				}
                ARLOG("--------------\n");
                status = 2;
                check_num = 0;
                print_comment(5);
            }
            else {
                exit(0);
            }
        }
        else if( status == 1 ) {
            if( patt.loop_num == 0 ) {ARLOGe("error!!\n"); exit(0);}
            patt.loop_num--;
            free( patt.point[patt.loop_num] );
            free( patt.savedImage[patt.loop_num] );
            status = 0;
            point_num = 0;
            arVideoCapStart();

            if( patt.loop_num == 0 ) print_comment(0);
             else                    print_comment(4);
        }
    }

    if( button == GLUT_LEFT_BUTTON  && state == GLUT_DOWN ) {
        if( status == 1 && point_num < patt.h_num*patt.v_num ) {
            sx = ex = x;
            sy = ey = y;

            p  = &(patt.savedImage[patt.loop_num-1][(y*xsize+x)*pixelSize]);
            p1 = &(clipImage[0]);
			if (pixelFormat == AR_PIXEL_FORMAT_BGRA || pixelFormat == AR_PIXEL_FORMAT_RGBA) {
				k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
				if( k < thresh ) k = 0;
                else k = 255;
				*(p1+0) = *(p1+1) = *(p1+2) = k;
			}
			else if (pixelFormat == AR_PIXEL_FORMAT_ARGB || pixelFormat == AR_PIXEL_FORMAT_ABGR) {
				k = (255*3 - (*(p+1) + *(p+2) + *(p+3))) / 3;
				if( k < thresh ) k = 0;
                else k = 255;
				*(p1+1) = *(p1+2) = *(p1+3) = k;
			}
			else if (pixelFormat == AR_PIXEL_FORMAT_BGR || pixelFormat == AR_PIXEL_FORMAT_RGB) {
				k = (255*3 - (*(p+0) + *(p+1) + *(p+2))) / 3;
				if( k < thresh ) k = 0;
                else k = 255;
				*(p1+0) = *(p1+1) = *(p1+2) = k;
			}
			else if (pixelFormat == AR_PIXEL_FORMAT_MONO || pixelFormat == AR_PIXEL_FORMAT_420v || pixelFormat == AR_PIXEL_FORMAT_420f) {
				k = 255 - *p;
				if( k < thresh ) k = 0;
                else k = 255;
				*p1 = k;
			}
			else if (pixelFormat == AR_PIXEL_FORMAT_2vuy) {
				k = 255 - *(p+1);
				if( k < thresh ) k = 0;
                else k = 255;
				*(p1+1) = k;
			}
			else if (pixelFormat == AR_PIXEL_FORMAT_yuvs) {
				k = 255 - *p;
				if( k < thresh ) k = 0;
                else k = 255;
				*p1 = k;
			}
        }
    }

    if( button == GLUT_LEFT_BUTTON  && state == GLUT_UP ) {
        if( status == 0 && patt.loop_num < LOOP_MAX ) {
            while (!(buff = arVideoGetImage()) || !buff->fillFlag) arUtilSleep(2);
            p = buff->buff;
            patt.savedImage[patt.loop_num] = (unsigned char *)malloc( xsize*ysize*pixelSize );
            if( patt.savedImage[patt.loop_num] == NULL ) exit(0);

            p1 = patt.savedImage[patt.loop_num];
            for(i=0;i<xsize*ysize*pixelSize;i++) *(p1++) = *(p++);
            arVideoCapStop();

            patt.point[patt.loop_num] = (CALIB_COORD_T *)malloc( sizeof(CALIB_COORD_T)*patt.h_num*patt.v_num );
            if( patt.point[patt.loop_num] == NULL ) exit(0);

            patt.loop_num++;
            status = 1;
            sx = sy = ex= ey = -1;

            print_comment(1);
        }
        else if( status == 1 && point_num == patt.h_num*patt.v_num ) {
            status = 0;
            point_num = 0;
            arVideoCapStart();

            ARLOG("### No.%d ###\n", patt.loop_num);
            for( j = 0; j < patt.v_num; j++ ) {
                for( i = 0; i < patt.h_num; i++ ) {
                    ARLOG("%2d, %2d: %6.2f, %6.2f\n", i+1, j+1,
                           patt.point[patt.loop_num-1][j*patt.h_num+i].x_coord,
                           patt.point[patt.loop_num-1][j*patt.h_num+i].y_coord);
                }
            }
            ARLOG("\n\n");
            if( patt.loop_num < LOOP_MAX ) print_comment(4);
             else                          print_comment(6);
        }
        else if( status == 1 ) {
            if( sx < ex ) { ssx = sx; eex = ex; }
             else         { ssx = ex; eex = sx; }
            if( sy < ey ) { ssy = sy; eey = ey; }
             else         { ssy = ey; eey = sy; }

            patt.point[patt.loop_num-1][point_num].x_coord = 0.0;
            patt.point[patt.loop_num-1][point_num].y_coord = 0.0;
            p = clipImage;
            k = 0;
            for( j = 0; j < (eey-ssy+1); j++ ) {
                for( i = 0; i < (eex-ssx+1); i++ ) {
                    if( pixelSize == 1 ) {
                        patt.point[patt.loop_num-1][point_num].x_coord += i * *p;
                        patt.point[patt.loop_num-1][point_num].y_coord += j * *p;
                        k += *p;
                    }
                    else {
                        patt.point[patt.loop_num-1][point_num].x_coord += i * *(p+1);
                        patt.point[patt.loop_num-1][point_num].y_coord += j * *(p+1);
                        k += *(p+1);
                    }
                    p += pixelSize;
                }
            }
            if( k != 0 ) {
                patt.point[patt.loop_num-1][point_num].x_coord /= k;
                patt.point[patt.loop_num-1][point_num].y_coord /= k;
                patt.point[patt.loop_num-1][point_num].x_coord += ssx;
                patt.point[patt.loop_num-1][point_num].y_coord += ssy;
                point_num++;
            }
            sx = sy = ex= ey = -1;

            ARLOG(" # %d/%d\n", point_num, patt.h_num*patt.v_num);
            if( point_num == patt.h_num*patt.v_num ) print_comment(2);
        }
        else if( status == 2 ) {
            check_num++;
            if( check_num == patt.loop_num ) {
                if(patt.loop_num >= 2) {
                    if( calc_inp(&patt, dist_factor, xsize, ysize, mat, dist_function_version) < 0 ) {
                        ARLOGe("Calibration failed.\n");
                        exit(0);
                    }
                    save_param();
					if (dist_function_version == 3) {
						printf("Do you want to repeat again?");
						scanf("%s", line);
						if( line[0] == 'y' ) {
							aspect_ratio *= mat[0][0] / mat[1][1];
							ARLOG("New aspect ratio = %f\n", aspect_ratio);
							calc_distortion( &patt, xsize, ysize, aspect_ratio, dist_factor, dist_function_version );
							ARLOG("--------------\n");
							ARLOG("Center X: %f\n", dist_factor[0]);
							ARLOG("       Y: %f\n", dist_factor[1]);
							ARLOG("Size Adjust: %f\n", dist_factor[2]);
							ARLOG("Aspect Ratio: %f\n", dist_factor[3]);
							ARLOG("Dist Factor1: %f\n", dist_factor[4]);
							ARLOG("Dist Factor2: %f\n", dist_factor[5]);
							ARLOG("--------------\n");
							status = 2;
							check_num = 0;
							print_comment(5);
							return;
						}
					}
                }
                exit(0);
            }

            if( check_num+1 == patt.loop_num ) {
                ARLOG("\nLeft Mouse Button: Next Step.\n");
            }
            else {
                ARLOG("   %d/%d.\n", check_num+1, patt.loop_num);
            }
        }
    }

    return;
}