Advertisement
Kojima0502

☆display_BMP_averave完成2_malloc 追加パフォーマンスチェック☆

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