Advertisement
Kojima0502

programPF

Feb 10th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GLUT/glut.h>
  4. #include <time.h>
  5. #include <math.h>
  6. /* keep the picture data*/
  7. #define  IMGFILE  "depth_combine48_cunbus_conv.bmp"
  8. #define  CHANNEL  3  // color(R, G, B)
  9. FILE *fp1,*fp2,*fp3,*fp4,*fp5;
  10. int i,j;
  11. float min;
  12. float max;
  13. int width;           // width of picture
  14. int height;          // height of picture
  15. float *file1,*file2,*file3,*file4,*file5;
  16.  
  17. GLuint texture;      // texture number
  18. GLubyte* image;      // keep the BMP data
  19.  
  20. void loadImage(const char* filepath);  // read BMP
  21.  
  22. float shiftX,shiftY;//for motion
  23. float mouseX,mouseY;//mouse
  24. float x=0.1;
  25. float y=0.1;
  26. float g_pressed = 0;//for mouse
  27. float r;
  28. float sum,sum2,sum3;
  29. float std;
  30. float i_first,j_first;
  31. int WIDTH,HEIGHT;
  32. int WIDTH1,HEIGHT1;
  33. float dn1,dn2,dn3,dn4,dn5;
  34. int b;
  35. float total1,total2,total3,total4,total5;//total
  36. float  average1,average2,average3,average4,average5;//average
  37. float start, end;//for calculating processing
  38.  
  39. void display(void)
  40. {
  41.     /*diaplay the picture */
  42.     glEnable(GL_TEXTURE_2D);
  43.     glClear(GL_COLOR_BUFFER_BIT);
  44.     glBindTexture(GL_TEXTURE_2D, texture);
  45.    
  46.     glTranslated(-0.5,-0.5,0);
  47.     glBegin(GL_QUADS);
  48.    
  49.     glTexCoord2f(1.0, 0.0);  glVertex2f(1.0, 0.0);//position
  50.     glTexCoord2f(1.0, 1.0);  glVertex2f(1.0, 1.0);
  51.     glTexCoord2f(0.0, 1.0);  glVertex2f(0.0, 1.0);
  52.     glTexCoord2f(0.0, 0.0);  glVertex2f(0.0, 0.0);
  53.    
  54.     glDisable(GL_TEXTURE_2D);
  55.    
  56.     glEnd();
  57.    
  58.    
  59.    
  60.     glTranslated(0.5,0.5,0);
  61.     glBegin(GL_LINE_LOOP);
  62.    
  63.     glVertex2d(-x+shiftX, -y+shiftY);//at the lower left
  64.     glVertex2d(x+shiftX, -y+shiftY);//at the lower right
  65.     glVertex2d(x+shiftX, y+shiftY);//at the upper rught
  66.     glVertex2d(-x+shiftX, y+shiftY);//at the upper left
  67.     //printf("ShiftX1 = %f : ShiftY1 = %f\n" , -x+shiftX , -y+shiftY);//at the lower left of square
  68.     //printf("ShiftX2 = %f : ShiftY2 = %f\n" , x+shiftX , -y+shiftY);//at the lower right of square
  69.     //printf("ShiftX3 = %f : ShiftY3 = %f\n" , x+shiftX , y+shiftY);//at the upper right of square
  70.     //printf("ShiftX4 = %f : ShiftY4 = %f\n" , -x+shiftX , y+shiftY);//at the upper left of square
  71.     //printf("b=%d\n",b);
  72.    
  73.     printf("HEIGHT = %d : WIDTH = %d : HEIGHT1 = %d : WIDTH1 = %d : y+shiftY = %f : -x+shiftX = %f : b=%d  \n" , HEIGHT,WIDTH,HEIGHT1,WIDTH1,y+shiftY,-x+shiftX,b);
  74.    
  75.     total1=0;
  76.     /*total2=0;
  77.     total3=0;
  78.     total4=0;
  79.     total5=0;
  80.     */
  81.    
  82.     i_first=-(int)((float)HEIGHT*(y+shiftY))+b;
  83.     j_first=(int)((float)WIDTH*(-x+shiftX))+b;
  84.     min=file1[0];
  85.     max=file1[0];
  86.     for(i= -(int)((float)HEIGHT*(y+shiftY))+b ;i<(int)(-(float)HEIGHT*(y+shiftY))+b+HEIGHT1;i++)
  87.     {
  88.         //printf("i=%d\n",i);
  89.         //printf("i2=%d\n",-HEIGHT*(int)(y+shiftY)+b);
  90.        
  91.         for(j=(int)((float)WIDTH*(-x+shiftX))+b;j<(int)((float)WIDTH*(-x+shiftX))+b+WIDTH1;j++)
  92.         {
  93.             //   printf("j=%d\n",j);
  94.             //  printf("check-7\n");
  95.             // printf("position=%d\n",(i-(WIDTH-HEIGHT)/2)*WIDTH+j);
  96.             //printf("position=%dĀ„n",(i-WIDTH1)*HEIGHT1+(j-HEIGHT1+((WIDTH-HEIGHT)/2)));
  97.            
  98.            
  99.             dn1=file1[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  100.             total1=total1+dn1;
  101.            
  102.             if(min>dn1)
  103.             {
  104.                 min=dn1;
  105.             }
  106.            
  107.             if(max<dn1)
  108.             {
  109.                 max=dn1;
  110.             }
  111.            
  112.                 /*
  113.             dn2=file2[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  114.             total2=total2+dn2;
  115.             dn3=file3[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  116.             total3=total3+dn3;
  117.             dn4=file4[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  118.             total4=total4+dn4;
  119.             dn5=file5[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  120.             total5=total5+dn5;
  121.            
  122.              */
  123.         }
  124.     }
  125.    
  126.     //printf("total1=%f\n",total1);
  127.     //printf("total2=%f\n",total2);
  128.    //printf("total3=%f\n",total3);
  129.     // printf("total4=%f\n",total4);
  130.     // printf("total5=%f\n",total5);
  131.    
  132.    
  133.     average1=total1/(float)(WIDTH1*HEIGHT1);
  134.     //average2=total2/(float)(WIDTH1*HEIGHT1);
  135.     //average3=total3/(float)(WIDTH1*HEIGHT1);
  136.     //average4=total4/(float)(WIDTH1*HEIGHT1);
  137.     //average5=total5/(float)(WIDTH1*HEIGHT1);
  138.  
  139.  
  140.    
  141.     printf("The average1 water depth is %f m\n " ,average1 );//DOCUMENTATION
  142.     printf("the minimum water depth is %f m\n " ,min );
  143.     printf("the maximum water depth is %f m\n " ,max );
  144.     printf("Standard deviation of the maximum water depth is %f m\n " ,sum3);
  145.     if(average1>0 && average1<20.0)
  146.     {
  147.         printf("The average water depth is under 20m. Some ships and dredgers may be able to pass here\n");
  148.     }
  149.     else if (average1>0 && average1<400.0)
  150.     {
  151.         printf("The average water depth is so deep\n");
  152.     }
  153.  
  154.     else if(average1>=400.0 && average1<1000.0)
  155.     {
  156.         printf("The average water depth is too deep\n");
  157.     }
  158.     else if(average1>1000.0)
  159.     {
  160.         printf("The average water depth is too deep,we dont neet to care about water depth \n");
  161.     }
  162.     else if(average1<0)
  163.     {
  164.         printf("The average water depth is under 0m. this means there are alomost land\n");
  165.     }
  166.     /*
  167.     printf("The average2 water depth is %f m\n " ,average2 );//average2
  168.    
  169.     if(average2>0 && average2<400.0)
  170.     {
  171.         printf("The average2 water depth is so deep\n");
  172.     }
  173.     else if(average2>=400.0 && average2<1000.0)
  174.     {
  175.         printf("The average2 water depth is too deep\n");
  176.     }
  177.     else if(average2>1000.0)
  178.     {
  179.         printf("The average2 water depth is fucking deep \n");
  180.     }
  181.     else if(average2<0)
  182.     {
  183.         printf("The average2 water depth is under 0m this means there are alomost land\n");
  184.     }
  185.    
  186.     printf("The average3 water depth is %f m\n " ,average3 );//average3
  187.    
  188.     if(average3>0 && average3<400.0)
  189.     {
  190.         printf("The average3 water depth is so deep\n");
  191.     }
  192.     else if(average3>=400.0 && average3<1000.0)
  193.     {
  194.         printf("The average3 water depth is too deep\n");
  195.     }
  196.     else if(average3>1000.0)
  197.     {
  198.         printf("The average3 water depth is fucking deep \n");
  199.     }
  200.      else if(average3<0)
  201.      {
  202.      printf("The average3 water depth is under 0m this means there are alomost land\n");
  203.      }
  204.    
  205.      printf("The average4 water depth is %f m\n " ,average4 );//averave4
  206.    
  207.     if(average4>0 && average4<400.0)
  208.     {
  209.         printf("The average4 water depth is so deep\n");
  210.     }
  211.     else if(average4>=400.0 && average4<1000.0)
  212.     {
  213.         printf("The average4 water depth is too deep\n");
  214.     }
  215.     else if(average4>1000.0)
  216.     {
  217.         printf("The average4 water depth is fucking deep \n");
  218.     }
  219.     else if(average4<0)
  220.     {
  221.      printf("The average4 water depth is under 0m this means there are alomost land\n");
  222.     }
  223.    
  224.      printf("The average5 water depth is %f m\n " ,average5 );//averave5
  225.    
  226.     if(average5>0 && average5<400.0)
  227.     {
  228.         printf("The average5 water depth is so deep\n");
  229.     }
  230.     else if(average5>=400.0 && average5<1000.0)
  231.     {
  232.         printf("The average5 water depth is too deep\n");
  233.     }
  234.     else if(average5>1000.0)
  235.     {
  236.         printf("The average5 water depth is fucking deep \n");
  237.     }
  238.      else if(average5<0)
  239.      {
  240.      printf("The average5 water depth is under 0m this means there are alomost land\n");
  241.      }
  242.      
  243.    
  244. */
  245.     end=clock();
  246.    
  247.     printf("start  %f /sec \n " ,start/1000000);
  248.     printf("end  %f /sec \n " ,end/1000000);
  249.     printf("star to end  %f /sec \n " ,(end-start)/1000000);
  250.     glEnd();
  251.    
  252.    
  253.     glutSwapBuffers();
  254. }
  255.  
  256. void myInit(void)
  257. {
  258.    
  259.     /* necessary setting for before create window*/
  260.     //window1//
  261.     glutInitWindowSize(600, 600);                  // setting for window size
  262.     glutInitWindowPosition(0, 0);              // window position
  263.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  // setting display mode
  264.     glutCreateWindow("OpenGL");// create window
  265.    
  266.     glClearColor (0.0, 0.0, 0.0, 1.0);             // window back color
  267.    
  268.     glutDisplayFunc(display);
  269.     //create texture
  270.     loadImage(IMGFILE);
  271.     glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
  272.     glGenTextures(1, &texture);
  273.     glBindTexture(GL_TEXTURE_2D, texture);
  274.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  275.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  276.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  277.                  width, height, 0, GL_RGB,
  278.                  GL_UNSIGNED_BYTE, image);
  279.    
  280.     free(image);
  281. }
  282.  
  283. void keyboard(unsigned char key, int x, int y)  //keyboard setting
  284. {
  285.     switch (key)
  286.     {
  287.         case 'q':
  288.         case 'Q':
  289.         case '\033':
  290.             exit(0);
  291.         default:
  292.             break;
  293.     }
  294. }
  295.  
  296.  
  297. void mouse(int btn, int state, int x, int y)  //mouze setting
  298. {
  299.    
  300.    
  301.     if(btn == GLUT_LEFT_BUTTON == state == GLUT_DOWN)  //when press the left button
  302.     {
  303.         mouseX = x;
  304.         mouseY = y;
  305.         g_pressed = 1;
  306.        
  307.     }
  308.     else if(btn == GLUT_LEFT_BUTTON == state ==GLUT_UP)  //when take off the left button
  309.         g_pressed = 0;
  310.    
  311. }
  312.  
  313.  
  314. void motion(int x, int y)  //controll the motion by the mouse
  315. {
  316.     {
  317.        
  318.         shiftX = (float)((x - mouseX)/320);
  319.         shiftY = (float)(-(y - mouseY)/320);
  320.         start =clock();
  321.         glutPostRedisplay();
  322.        
  323.     }
  324.    
  325.     /* printf("X = %d : Y = %d\n" , x , y);  //diplay the position of coordinate
  326.      printf("ShiftX = %f : ShiftY = %f\n" , shiftX , shiftY);  //display the amount of the shift
  327.      printf("mouseX = %f : mouseY = %f \n" , -x+shiftX , mouseY);
  328.      //printf("mouseX = %f : mouseY = %f \n" , mouseX , mouseY);  //display the mouseX and mouseY
  329.      printf("wx = %f : wy = %f \n" , get_wx , get_wy);
  330.      // printf("windowX = %f : windowY = %f \n" , get_wx , get_wy);
  331.      printf("windowW = %f : windowH = %f \n" , get_ww , get_wh);
  332.      */
  333. }
  334.  
  335. int main(int argc, char* argv[])
  336. {
  337.    
  338.    // printf("check-1\n");
  339.    
  340.    
  341.     if(argc!=4)
  342.     {
  343.         fprintf(stderr,"Usage: %s\n(1)input file name\n(2)input file name\n(3)input file name\n(4)WIDTH\n(5)HEIGHT\n",argv[0]);
  344.         exit(1);
  345.     }
  346.    
  347.     r=1/(2*x);
  348.     printf("r = %f \n",r);
  349.     WIDTH = atoi(argv[2]); printf("W = %d,", WIDTH);
  350.     HEIGHT = atoi(argv[3]); printf("H = %d\n", HEIGHT);
  351.     WIDTH1=(int)((float)WIDTH/r);
  352.     HEIGHT1=(int)((float)WIDTH/r);
  353.     b=WIDTH/2;
  354.    
  355.     file1= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  356.     //file2= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  357.     //file3= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  358.     //file4= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  359.     //file5= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  360.    
  361.  
  362.     if((fp1=fopen(argv[1],"rb"))==NULL)
  363.     {
  364.         fprintf(stderr,"input file open error\n");
  365.         exit(1);
  366.     }
  367.     /*
  368.     if((fp2=fopen(argv[2],"rb"))==NULL)
  369.     {
  370.         fprintf(stderr,"input file open error\n");
  371.         exit(1);
  372.     }
  373.    
  374.     if((fp3=fopen(argv[3],"rb"))==NULL)
  375.     {
  376.         fprintf(stderr,"input file open error\n");
  377.         exit(1);
  378.     }
  379.    
  380.      if((fp4=fopen(argv[4],"rb"))==NULL)
  381.      {
  382.      fprintf(stderr,"input file open error\n");
  383.      exit(1);
  384.      }
  385.    
  386.      if((fp5=fopen(argv[5],"rb"))==NULL)
  387.      {
  388.      fprintf(stderr,"input file open error\n");
  389.      exit(1);
  390.      }
  391.    
  392.      
  393.     */
  394.     fread((float *)file1,sizeof(float),WIDTH*HEIGHT,fp1);
  395.     //fread((float *)file2,sizeof(float),WIDTH*HEIGHT,fp2);
  396.     //fread((float *)file3,sizeof(float),WIDTH*HEIGHT,fp3);
  397.     //fread((float *)file4,sizeof(float),WIDTH*HEIGHT,fp4);
  398.     //fread((float *)file5,sizeof(float),WIDTH*HEIGHT,fp5);
  399.  
  400.     printf("check-10\n");
  401.    
  402.     glutInit(&argc, argv);
  403.    
  404.     myInit();
  405.    
  406.     glutKeyboardFunc(keyboard);  //when need keyboard function
  407.     glutMouseFunc(mouse);  //when need mouse function
  408.     glutMotionFunc(motion);  //when need mouse motion function
  409.     glutDisplayFunc(display);  //when need to set about display function
  410.     //glutIdleFunc(idle_func);
  411.     glutMainLoop();
  412.    
  413.     return 0;
  414.    
  415.     free(file1);
  416.     //free(file2);
  417.     //free(file3);
  418.     //free(file4);
  419.     //free(file5);
  420.  
  421.    
  422.     fclose(fp1);
  423.     //fclose(fp2);
  424.     //fclose(fp3);
  425.     //fclose(fp4);
  426.     //fclose(fp5);
  427. }
  428.  
  429.  
  430. void loadImage(const char* filepath)
  431. {
  432.     int i;
  433.     int size;  // size of picture (width , height, color)
  434.     FILE* fp;
  435.    
  436.     /* read file */
  437.     fp = fopen(filepath, "rb");  //read binary
  438.     if (fp == NULL)
  439.     {
  440.         fprintf(stderr, "%sfail loading\n", filepath);
  441.         exit(EXIT_FAILURE);
  442.     }
  443.    
  444.     /* get width and height  */
  445.     fseek(fp, 18, SEEK_SET);  //move fp to place where has been put width of picture
  446.     fread(&width, 4, 1, fp);
  447.     fread(&height, 4, 1, fp);
  448.    
  449.     /* read data of picture */
  450.     fseek(fp, 54, SEEK_SET);  //move fp to place where has been put dataof the picture
  451.     size = width * height * CHANNEL;
  452.     image = (GLubyte* ) malloc(size);
  453.     if (image == NULL)
  454.     {
  455.         fprintf(stderr, "fail to keep the memory\n");
  456.         exit(EXIT_FAILURE);
  457.     }
  458.    
  459.     for (i = 0; i < size; ++i)
  460.         fread(&image[i], sizeof(GLubyte), 1, fp);
  461.    
  462.     /* exit */
  463.     fclose(fp);
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement