Advertisement
Kojima0502

☆display_BMP_averave完成1☆

Feb 1st, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.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 *fp;
  10. int i,j;
  11. int width;           // width of picture
  12. int height;          // height of picture
  13. float *file1;
  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 dn;
  37. int b;
  38. float total;
  39. float  averave;
  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.     total=0;
  102.    
  103.     for(i= -(int)((float)HEIGHT*(y+shiftY))+b ;i<(int)(-(float)HEIGHT*(y+shiftY))+b+HEIGHT1;i++)
  104.     {
  105.         printf("i=%d\n",i);
  106.         printf("i2=%d\n",-HEIGHT*(int)(y+shiftY)+b);
  107.         for(j=(int)((float)WIDTH*(-x+shiftX))+b;j<(int)((float)WIDTH*(-x+shiftX))+b+WIDTH1;j++)
  108.         {
  109.             //   printf("j=%d\n",j);
  110.             //  printf("check-7\n");
  111.             // printf("position=%d\n",(i-(WIDTH-HEIGHT)/2)*WIDTH+j);
  112.             //printf("position=%d¥n",(i-WIDTH1)*HEIGHT1+(j-HEIGHT1+((WIDTH-HEIGHT)/2)));
  113.             dn=file1[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  114.             total=total+dn;
  115.             // printf("check-8\n");
  116.         }
  117.         //printf("check-9\n");
  118.     }
  119.    
  120.     printf("total=%f\n",total);
  121.     averave=total/(float)(WIDTH1*HEIGHT1);
  122.     printf("the average water depth is %f m\n " ,averave );
  123.    
  124.     end=clock();
  125.    
  126.     printf("start  %f /sec \n " ,start/1000000);
  127.     printf("end  %f /sec \n " ,end/1000000);
  128.     printf("star to end  %f /sec \n " ,(end-start)/1000000);
  129.     //get_wx=glutGet( GLUT_WINDOW_X );
  130.     //get_wy=glutGet( GLUT_WINDOW_Y );
  131.     //get_ww=glutGet( GLUT_WINDOW_WIDTH );
  132.     //get_wh=glutGet( GLUT_WINDOW_HEIGHT );
  133.    
  134.     // Xshift=x+shiftX;
  135.     // Yshift=y+shiftY;
  136.    
  137.    
  138.    
  139.     //glPushMatrix();
  140.     //glTranslatef(shiftX,shiftY,0);
  141.     // glTranslatef(0.5,0.5,0);
  142.     //glPopMatrix();
  143.     glEnd();
  144.    
  145.    
  146.     glutSwapBuffers();
  147.    
  148.     //glFlush();
  149.    
  150. }
  151.  
  152.  
  153.  
  154. void myInit(void)
  155. {
  156.    
  157.     /* necessary setting for before create window*/
  158.     //window1//
  159.     glutInitWindowSize(600, 600);                  // setting for window size
  160.     glutInitWindowPosition(0, 0);              // window position
  161.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  // setting display mode
  162.     glutCreateWindow("OpenGL");// create window
  163.    
  164.     glClearColor (0.0, 0.0, 0.0, 1.0);             // window back color
  165.    
  166.     glutDisplayFunc(display);
  167.     //create texture
  168.     loadImage(IMGFILE);
  169.     glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
  170.     glGenTextures(1, &texture);
  171.     glBindTexture(GL_TEXTURE_2D, texture);
  172.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  173.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  174.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  175.                  width, height, 0, GL_RGB,
  176.                  GL_UNSIGNED_BYTE, image);
  177.  
  178.    
  179.     //gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, WIDTH, TEXHEIGHT,
  180.     //        GL_RGB, GL_UNSIGNED_BYTE, texture);
  181.    
  182.     free(image);
  183. }
  184.  
  185. void keyboard(unsigned char key, int x, int y)  //keyboard setting
  186. {
  187.    
  188.     switch (key)
  189.     {
  190.         case 'q':
  191.         case 'Q':
  192.         case '\033':
  193.             exit(0);
  194.         default:
  195.             break;
  196.            
  197.     }
  198. }
  199.  
  200.  
  201. void mouse(int btn, int state, int x, int y)  //mouze setting
  202. {
  203.    
  204.    
  205.     if(btn == GLUT_LEFT_BUTTON == state == GLUT_DOWN)  //when press the left button
  206.     {
  207.         //Xshift=x+shiftX;
  208.         // Yshift=y+shiftY;
  209.         mouseX = x;
  210.         mouseY = y;
  211.         g_pressed = 1;
  212.        
  213.     }
  214.     else if(btn == GLUT_LEFT_BUTTON == state ==GLUT_UP)  //when take off the left button
  215.         g_pressed = 0;
  216.    
  217. }
  218.  
  219.  
  220. void motion(int x, int y)  //controll the motion by the mouse
  221. {
  222.     {
  223.        
  224.         shiftX = (float)((x - mouseX)/320);
  225.         shiftY = (float)(-(y - mouseY)/320);
  226.         start =clock();
  227.         glutPostRedisplay();
  228.        
  229.     }
  230.    
  231.     /* printf("X = %d : Y = %d\n" , x , y);  //diplay the position of coordinate
  232.      printf("ShiftX = %f : ShiftY = %f\n" , shiftX , shiftY);  //display the amount of the shift
  233.      printf("mouseX = %f : mouseY = %f \n" , -x+shiftX , mouseY);
  234.      //printf("mouseX = %f : mouseY = %f \n" , mouseX , mouseY);  //display the mouseX and mouseY
  235.      printf("wx = %f : wy = %f \n" , get_wx , get_wy);
  236.      // printf("windowX = %f : windowY = %f \n" , get_wx , get_wy);
  237.      printf("windowW = %f : windowH = %f \n" , get_ww , get_wh);
  238.      */
  239. }
  240.  
  241. int main(int argc, char* argv[])
  242. {
  243.     //float *file1;
  244.     printf("check-1\n");
  245.      //float start, end;
  246.    
  247.     if(argc!=4)
  248.     {
  249.         fprintf(stderr,"Usage: %s\n(1)input file name\n(2)WIDTH\n(3)HEIGHT\n",argv[0]);
  250.         exit(1);
  251.     }
  252.     //printf("check-2\n");
  253.     WIDTH = atoi(argv[2]); printf("W = %d,", WIDTH);
  254.     HEIGHT = atoi(argv[3]); printf("H = %d\n", HEIGHT);
  255.     WIDTH1=WIDTH/5;
  256.     HEIGHT1=WIDTH/5;
  257.     b=WIDTH/2;
  258.     // printf("check-3\n");
  259.     //float *file1;
  260.     file1= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  261.     // printf("check-4\n");
  262.     if((fp=fopen(argv[1],"rb"))==NULL)
  263.     {
  264.         fprintf(stderr,"input file open error\n");
  265.         exit(1);
  266.     }
  267.     //printf("check-5\n");
  268.     fread((float *)file1,sizeof(float),WIDTH*HEIGHT,fp);
  269.    
  270.     /*
  271.      for(i=0;i<WIDTH*HEIGHT;i++)
  272.      {
  273.      printf("file_depth=%f\n",file1[i]);
  274.      }
  275.      */
  276.     //total=0;
  277.    
  278.     //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);
  279.    
  280.    // printf("i=%d\n",-HEIGHT*(y+shiftY)+b);
  281.    // printf("i_p=%d\n",-HEIGHT*(y+shiftY)+b+HEIGHT1);
  282.     /*
  283.     for(i= -HEIGHT*(y+shiftY)+b ;i<-HEIGHT*(y+shiftY)+b+HEIGHT1;i++)
  284.     {
  285.         //printf("i=%d\n",i);
  286.         //printf("check-6\n");
  287.         for(j=WIDTH*(-x+shiftX)+b;j<WIDTH*(-x+shiftX)+b+WIDTH1;j++)
  288.         {
  289.             //  printf("j=%d\n",j);
  290.             //printf("check-7\n");
  291.             //printf("position=%d¥n",(i-WIDTH1)*HEIGHT1+(j-HEIGHT1+((WIDTH-HEIGHT)/2)));
  292.             //dn=file1[(i-(WIDTH1-HEIGHT1))*WIDTH1+j];
  293.             dn=file1[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  294.             total=total+dn;
  295.             // printf("check-8\n");
  296.         }
  297.         //printf("check-9\n");
  298.        
  299.     }
  300.     averave=total/(WIDTH1*HEIGHT1);
  301.     printf("the average water depth is %f m\n " ,averave );
  302.     */
  303.     printf("check-10\n");
  304.    
  305.     //free(file1);
  306.     //fclose(fp);
  307.    
  308.     glutInit(&argc, argv);
  309.     //glutInitDisplayMode(GLUT_RGBA);
  310.     //glutCreateWindow(argv[0]);
  311.     myInit();
  312.    
  313.    
  314.  
  315.     glutKeyboardFunc(keyboard);  //when need keyboard function
  316.     glutMouseFunc(mouse);  //when need mouse function
  317.     glutMotionFunc(motion);  //when need mouse motion function
  318.     glutDisplayFunc(display);  //when need to set about display function
  319.     //glutIdleFunc(idle_func);
  320.     glutMainLoop();
  321.    
  322.     return 0;
  323.    
  324.     free(file1);
  325.     fclose(fp);
  326.    
  327. }
  328.  
  329.  
  330. void loadImage(const char* filepath)
  331. {
  332.     int i;
  333.     int size;  // size of picture (width , height, color)
  334.     FILE* fp;
  335.    
  336.     /* read file */
  337.     fp = fopen(filepath, "rb");  //read binary
  338.     if (fp == NULL)
  339.     {
  340.         fprintf(stderr, "%sfail loading\n", filepath);
  341.         exit(EXIT_FAILURE);
  342.     }
  343.    
  344.     /* get width and height  */
  345.     fseek(fp, 18, SEEK_SET);  //move fp to place where has been put width of picture
  346.     fread(&width, 4, 1, fp);
  347.     fread(&height, 4, 1, fp);
  348.    
  349.     /* read data of picture */
  350.     fseek(fp, 54, SEEK_SET);  //move fp to place where has been put dataof the picture
  351.     size = width * height * CHANNEL;
  352.     image = (GLubyte* ) malloc(size);
  353.     if (image == NULL)
  354.     {
  355.         fprintf(stderr, "fail to keep the memory\n");
  356.         exit(EXIT_FAILURE);
  357.     }
  358.    
  359.     for (i = 0; i < size; ++i)
  360.         fread(&image[i], sizeof(GLubyte), 1, fp);
  361.    
  362.     /* exit */
  363.     fclose(fp);
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement