2012年7月17日 星期二

GLUT Light

#include "windows.h"
#include "GL/glut.h"
#include "stdio.h"
#include "math.h"

#define G_PI 3.14159265358979323846f

void prepare_lighting();
void display();
void keyboard(unsigned char key, int x, int y);

float theta, phi;

int main()
{
  theta = G_PI/2;
  phi = -G_PI/2;

  glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );
  glutInitWindowSize( 640, 640 );
  glutCreateWindow( "hihi" );

  glutDisplayFunc(display);
  glutKeyboardFunc( keyboard );

  prepare_lighting();

  glutMainLoop();
  return 0;
}

void keyboard(unsigned char key, int x, int y)
{
  switch( key )
  {
    case 'w':
     theta -= .05;
     prepare_lighting();
     glutPostRedisplay();
    break;

    case 's':
     theta += .05;
     prepare_lighting();
     glutPostRedisplay();
    break;

    case 'a':
     phi -= .05;
     prepare_lighting();
     glutPostRedisplay();
    break;

    case 'd':
     phi += .05;
     prepare_lighting();
     glutPostRedisplay();
    break;
  };
}


void display()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluPerspective( 20, 1, 0.1, 10 );

    glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     gluLookAt(
     0,0,1,
     0,0,0,
     0,1,0 );

    glEnable( GL_LIGHTING );
    glEnable( GL_DEPTH_TEST );
    glutSolidTeapot( .1 );

  glutSwapBuffers();
}

void prepare_lighting()
{
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light_position[4] = { sinf(theta) * cosf(phi), cosf(theta), -sinf(theta) * sinf(phi), 0 };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

  theta = fmodf( theta, 2*G_PI );
  phi = fmodf( phi, 2*G_PI );


    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse); //
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);  //
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable( GL_LIGHT0 );
}

GLUT

#include "windows.h"
#include "GL/glut.h"
#include "stdlib.h"
#include "NeoBmp.H"
NeoBmp image;
float px=0,py=0;
void keyboard(unsigned char key, int x, int y )
{
  switch( key )
  {
    case 'd':
     px += .01;
     glutPostRedisplay();
     break;
    case 'a':
     px -= .01;
     glutPostRedisplay();
     break;
    case 'w':
     py += .01;
     glutPostRedisplay();
     break;
    case 's':
     py -= .01;
     glutPostRedisplay();
     break;
    case 27:
     exit(0);
  }
}
void display()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  glRasterPos2f( -image.width/640.0+px, -image.height/640.0+py );
  glDrawPixels( image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.rgb );
  glutSwapBuffers();
}

int main()
{
  glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );
  glutInitWindowSize( 640, 640 );
  glutCreateWindow( "Slut Test" );
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);
  image.load( "test.bmp" );
  glutMainLoop();

  return 0;
}

Read Bitmap by C++

NeoBmp.h
#ifndef Neo_Bmp_H
#define Neo_Bmp_H
class NeoBmp
{
public:
    int width,height;
    unsigned char* rgb;
    NeoBmp();
    ~NeoBmp();
    void load(const char* filename);
    void save(const char* filename);
    void flip_vectical();
private:
    void rb_swap();
};
#endif
NeoBmp.cpp

#include "stdio.h"
#include "stdlib.h"
#include "memory.h"
#include "NeoBmp.H"

#pragma pack( push, 1 )
typedef struct _bmp_header_info
{
  unsigned short bfType;
  unsigned int bfSize;
  unsigned short bfReserved1;
  unsigned short bfReserved2;
  unsigned int bfOffBits;

  // bitmap header
  unsigned int biSize;
  int biWidth;
  int biHeight;
  unsigned short biPlanes;
  unsigned short biBitCount;
  unsigned int biCompression;
  unsigned int biSizeImage;
  int biXpelsPerMeter;
  int biYpelsPerMeter;
  unsigned int biClrUsed;
  unsigned int biClrImportant;
} bmp_header_info;
#pragma pack( pop, 1 )

NeoBmp::NeoBmp()
{
    memset(this, 0, sizeof(NeoBmp));
}

NeoBmp::~NeoBmp()
{
    if(rgb!=NULL)
        free(rgb);
}

void NeoBmp::load(const char* filename)
{
    bmp_header_info bhi;
    {
          FILE *fp = fopen(filename, "rb" );
          if(fp==NULL )
          {
          printf( "[Error] NeoBmp::load, file '%s' not found.\n", filename);
          exit(-1);
          }
          fread(&bhi,sizeof(bmp_header_info),1,fp);
          fclose(fp);
    }
    if(bhi.bfType != 'MB' )
    {
        printf( "[Error] NeoBmp::load, not bitmap file\n" );
        exit(-1);
    }
    if( bhi.biCompression != 0 )
    {
        printf( "[Error] NeoBmp::load, only uncompressed bitmap is supported\n" );
        exit(-1);
    }
      if( bhi.biBitCount != 24 )
    {
        printf( "[Error] NeoBmp::load, must be 24bit bitmap\n" );
        exit(-1);
    }

    if(rgb) free(rgb);
    width = bhi.biWidth;
    height = bhi.biHeight;
    rgb = (unsigned char*) malloc(width*height*3*sizeof(unsigned char));
    {
        FILE *fp = fopen( filename, "rb" );
        fseek( fp, bhi.bfOffBits, SEEK_SET );
        int i;
        for(i=0; i < height; i++ )
        {
            fread(&rgb[i*width*3], sizeof(unsigned char), width*3, fp );
            fseek(fp, (4-width*3%4)%4, SEEK_CUR );
        }
        fclose(fp);
    }
    rb_swap();
}

void NeoBmp::rb_swap()
{
  unsigned char tmp;
  int i,j;
  for( j=0; j < height; j++ )
    for( i=0; i < width; i++ )
    {
     tmp = rgb[(j*width+i)*3];
     rgb[(j*width+i)*3] = rgb[(j*width+i)*3+2];
     rgb[(j*width+i)*3+2] = tmp;
    }
}

void NeoBmp::save( const char *filename)
{
    bmp_header_info bhi;
    bhi.bfType = 'MB';
    bhi.bfSize = width*height*3*sizeof(unsigned char) + sizeof(bhi);
    bhi.bfReserved1 = 0;
    bhi.bfReserved2 = 0;
    bhi.bfOffBits = sizeof(bhi);

    bhi.biSize = 40;
    bhi.biWidth = width;
    bhi.biHeight = height;
    bhi.biPlanes = 1;
    bhi.biBitCount = 24;
    bhi.biCompression = 0;
    bhi.biSizeImage = 0;
    bhi.biXpelsPerMeter = 0;
    bhi.biYpelsPerMeter = 0;
    bhi.biClrUsed = 0;
    bhi.biClrImportant = 0;
    int j;
    rb_swap();
    unsigned char pad[3] = {0};
    FILE *fp = fopen(filename, "wb" );
    fwrite( &bhi, sizeof(bmp_header_info), 1, fp);
     for( j=0; j < height; j++ )
     {
     fwrite( &rgb[j*width*3], sizeof(unsigned char), width*3, fp);
     fwrite(pad, sizeof(unsigned char), (4-width*3%4)%4, fp);
     }
     fclose(fp);
}
void NeoBmp::flip_vectical()
{
    unsigned char* tmp_rgb = (unsigned char*) malloc(width*height*3*sizeof(unsigned char));
    int j;
    for(j=0; j < height; j++)
    {
        memcpy( &tmp_rgb[j*width*3], &rgb[(height-j-1)*width*3], width*3*sizeof(unsigned char) );
    }
    memcpy( rgb, tmp_rgb, width*height*3*sizeof(unsigned char) );
    free(tmp_rgb);
}

main.cpp

#include "NeoBmp.H"
using namespace std;

int main()
{
    NeoBmp a;
    a.load("test.bmp");
    a.flip_vectical();
    a.save("output.bmp");
    return 0;
}

2012年7月16日 星期一

opencv-calcHist

Histogram.h
#ifndef HISTOGRAM_H_
#define HISTOGRAM_H_

#include "opencv/cv.h"

class Histogram
{
private:
    int histSize[1];
    float hrangee[2];
    const float* ranges[1];
    int channels[1];
protected:
    cv::Mat getHistogram(const cv::Mat& image);
public:
     Histogram();
     cv::Mat getHistogramImage(const cv::Mat& image, int channel);
};
#endif /*HISTOGRAM_H_*/
Histogram.cpp
#include "Histogram.h"

Histogram::Histogram()
{
    histSize[0] = 256;
    hrangee[0] = 0.0;
    hrangee[1] = 255.0;
    ranges[0] = hrangee;
    channels[0] = 0;
}

cv::Mat Histogram::getHistogram(const cv::Mat& image)
{
    cv::MatND hist;
    cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
    return hist;
}

cv::Mat Histogram::getHistogramImage(const cv::Mat& image, int channel)
{
    std::vector planes;
    cv::split(image,planes);
    cv::Scalar color;
    if(planes.size() == 1){
        channel = 0;
        color = cv::Scalar(0,0,0);
    }else{
        color = cv::Scalar(channel==0?255:0, channel==1?255:0, channel==2?255:0);
    }
     cv::MatND hist = getHistogram(planes[channel]);
    double maxVal = 0;
    double minVal = 0;
    cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0); //尋找一個矩陣中最大(maxVal)和最小值(minVal),並得到它們的位置
    cv::Mat histImg(histSize[0], histSize[0], CV_8UC3, cv::Scalar(255,255,255));
    int hpt = static_cast(0.9*histSize[0]);
    for(int h=0; h(h);
        float binVal2 = hist.at(h+1);
        int intensity = static_cast(binVal*hpt/maxVal);
        int intensity2 = static_cast(binVal2*hpt/maxVal);
        cv::line(histImg, cv::Point(h,histSize[0]-intensity), cv::Point(h,histSize[0]-intensity2), color);
    }
    return histImg;
}

main.cpp
//#include 
#include "opencv\highgui.h"
#include "opencv\cv.h"
#include "Histogram.h"
using namespace std;
//using namespace cv;
char* windowTitle = "test";
cv::Mat frame;
cv::Mat grayimage; //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
CvPoint VertexLT,VertexRD;//長方形的左上點和右下點
cv::Scalar color = CV_RGB(0,255,0);
int thickness = 2;
int shift = 0;
void onMouse(int event,int x,int y,int flags,void* param);
void onMouse(int event,int x,int y,int flag,void* param)
    {
    printf("( %d, %d) ",x,y);
    printf("The Event is : %d ",event);
    printf("The flags is : %d ",flag);
    printf("The param is : %d\n",param);
    if(event==CV_EVENT_LBUTTONDOWN||event==CV_EVENT_RBUTTONDOWN)
    {
        VertexLT=cv::Point(x,y);//得到左上角座標
    }
    if(event==CV_EVENT_LBUTTONUP||event==CV_EVENT_RBUTTONUP)
    {
        VertexRD=cv::Point(x,y); //得到右下角座標
    }
    if(flag==CV_EVENT_FLAG_LBUTTON||flag==CV_EVENT_FLAG_RBUTTON){//拖曳滑鼠
       VertexRD = cv::Point(x,y);
	   // cvReleaseImage(&Image);//如果沒有的話,記憶體會暴漲
	   // Image = cvCloneImage(Imagex);//cvCopy(Imagex, Image, 0);
	    cv::rectangle(frame,VertexLT,VertexRD,color,thickness,CV_AA,shift);
	    cv::imshow(windowTitle,frame);
        Histogram h;
        cv::namedWindow("His");
        cv::imshow("His",h.getHistogramImage(grayimage,0));
	}
}

int main()
{
    cv::namedWindow(windowTitle,1);  //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    cv::setMouseCallback(windowTitle,onMouse,NULL);//設定滑鼠callback函式
    cv::VideoCapture cap(0); // open the default camera //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    if(!cap.isOpened()) return -1;
    for(;;)
    {
        cap >> frame;
        cv::cvtColor(frame, grayimage, CV_BGR2GRAY);  //cv.h, libopencv_imgproc242.dll
       // cv:: GaussianBlur(grayimage, grayimage, cv::Size(7,7), 1.5, 1.5); //cv.h, libopencv_imgproc242.dll
       // cv::Canny(grayimage, grayimage, 0, 30, 3); //cv.h, libopencv_imgproc242.dll
       // grayimage = frame;
        cv::rectangle(grayimage,VertexLT,VertexRD,color,thickness,CV_AA,shift);
        cv::imshow(windowTitle, grayimage);
        int key = cv::waitKey(10);
        if(key==27) break;
        switch(key){
            case 'a':
            break;
        }
       // break;
    }
    /*
    char* filename = "test.jpg";
    cv::Mat image = cv::imread(filename,1);
    cv::imshow(windowTitle, image);
    */
    return 0;
}

2012年7月14日 星期六

OpenCV Read from CAMERA

#include "opencv\highgui.h"
#include "opencv\cv.h"
using namespace std;
//using namespace cv;
char* windowTitle = "test";
cv::Mat frame;
CvPoint VertexLT,VertexRD;//長方形的左上點和右下點
cv::Scalar color = CV_RGB(0,255,0);
int thickness = 2;
int shift = 0;
void onMouse(int event,int x,int y,int flags,void* param);
void onMouse(int event,int x,int y,int flag,void* param)
    {
    printf("( %d, %d) ",x,y);
    printf("The Event is : %d ",event);
    printf("The flags is : %d ",flag);
    printf("The param is : %d\n",param);
    if(event==CV_EVENT_LBUTTONDOWN||event==CV_EVENT_RBUTTONDOWN)
    {
        VertexLT=cv::Point(x,y);//得到左上角座標
    }
    if(event==CV_EVENT_LBUTTONUP||event==CV_EVENT_RBUTTONUP)
    {
        VertexRD=cv::Point(x,y); //得到右下角座標
    }
    if(flag==CV_EVENT_FLAG_LBUTTON||flag==CV_EVENT_FLAG_RBUTTON){//拖曳滑鼠
       VertexRD = cv::Point(x,y);
    // cvReleaseImage(&Image);//如果沒有的話,記憶體會暴漲
    // Image = cvCloneImage(Imagex);//cvCopy(Imagex, Image, 0);
     cv::rectangle(frame,VertexLT,VertexRD,color,thickness,CV_AA,shift);
     cv::imshow(windowTitle,frame);
 }
}

int main()
{
    cv::namedWindow(windowTitle,1);  //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    cv::setMouseCallback(windowTitle,onMouse,NULL);//設定滑鼠callback函式
    cv::VideoCapture cap(0); // open the default camera //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    if(!cap.isOpened()) return -1;
    cv::Mat grayimage; //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    for(;;)
    {
        cap >> frame;
       // cv::cvtColor(frame, grayimage, CV_BGR2GRAY);  //cv.h, libopencv_imgproc242.dll
       // cv:: GaussianBlur(grayimage, grayimage, cv::Size(7,7), 1.5, 1.5); //cv.h, libopencv_imgproc242.dll
       // cv::Canny(grayimage, grayimage, 0, 30, 3); //cv.h, libopencv_imgproc242.dll
        grayimage = frame;
        cv::rectangle(grayimage,VertexLT,VertexRD,color,thickness,CV_AA,shift);
        cv::imshow(windowTitle, grayimage);
        if(cv::waitKey(30) >= 0) break;
    }
    /*
    char* filename = "test.jpg";
    cv::Mat image = cv::imread(filename,1);
    cv::imshow(windowTitle, image);
    */
    return 0;
}

2012年5月10日 星期四

MMCompView

MP4 AVI:ffdshow
RMVB MP4 AVI wmv:LAV

 without thumbnails:
 regsvr32 /u shmedia.dll

 with thumbnails:
regsvr32 shmedia.dll

2012年4月25日 星期三

JAVA的HashMap對value排序

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class neo {
  //實作Comparator的介面宣告compare方法
   private class ValueComparator implements Comparator<Map.Entry<String, Integer>>  
   {  
      public int compare(Map.Entry<String, Integer> mp1, Map.Entry<String, Integer> mp2)   
      {  
      //大到小mp2-mp1,小到大mp1-mp2
         return mp2.getValue() - mp1.getValue();  
      }  
   }  

   public List<String> showsortedlist(Map<String, Integer> map)
   {
      //先把HashMap轉成List形式
      List<Map.Entry<String, Integer>> sortedlist = new ArrayList<Map.Entry<String, Integer>>(map.size()); 
      //把map中所有東西丟到sortedlist 
      sortedlist.addAll(map.entrySet()); 
      ValueComparator vc = new ValueComparator();  
      //利用Collections.sort排序sortedlist
      Collections.sort(sortedlist, vc); 
      final List<String> sortedvalue_keys = new ArrayList<String>(map.size()); 
      Iterator iter = sortedlist.iterator();
      while (iter.hasNext()) {
         Entry<String, Integer> data = (Entry<String, Integer>) iter.next();
         Object key = data.getKey();
         Object val = data.getValue();
         System.out.println(key+"=>"+val);
         sortedvalue_keys.add((String) key);  
     }
     return sortedvalue_keys;
  }
  public static void main(String args[]){
   Map<String,Integer> map = new HashMap<String,Integer>();  
   map.put("20030120" , new Integer (56));    
   map.put("20030118" , new Integer (19));    
   map.put("20030125" , new Integer (25));    
   map.put("20030122" , new Integer (32));    
   map.put("20030117" , new Integer (67));    
   map.put("20030123" , new Integer (34));    
   map.put("20030124" , new Integer (42));    
   map.put("20030121" , new Integer (19));    
   map.put("20030119" , new Integer (98)); 
   Iterator iter = map.entrySet().iterator();
   while (iter.hasNext()) {
       Map.Entry entry = (Map.Entry) iter.next();
       Object key = entry.getKey();
       Object val = entry.getValue();
       System.out.println(key+"=>"+val);
   } 
   System.out.println("開始排序");
   neo gosort  = new nothing();
   gosort.showsortedlist(map);
 }
}