Advertisement
Kojima0502

☆display_BMP_average_malloc_PF☆

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