P5を読み込む場合のサンプル(暫定版)


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <GL/glut.h>
#include <GL/gl.h>

void display();

using namespace std;

struct Image{
char _type[3];
unsigned int _x;
unsigned int _y;
int _bright;
unsigned char *_data;
unsigned long int _size;
};
typedef struct Image Image;


void ImageLoad(char *filename,Image *pnm)
{
fstream file(filename,ios::in|ios::binary);
if(file.fail())
{
perror(filename);
exit(1);
}



file >> pnm->_type; //typeチェック
char str[256]={0};

for(int count = 0;count < 3;)
{
file >> str;
if(strncmp(str,"#",1)== 0)//コメント無視
{
file.ignore(256,'\n');
}else
{
switch(count)
{
case 0: // X
pnm->_x = atoi(str);
break;
case 1: // Y
pnm->_y = atoi(str);
break;
case 2: // bright
pnm->_bright = atoi(str);
break;
default:
break;
}
count++;
}
}
cout <<"filename :"<< filename << endl;
cout << "type: " << pnm->_type << endl;
cout << "X: " << pnm->_x << endl;
cout << "Y: "<< pnm->_y<< endl;
cout << "bright: " <<pnm->_bright << endl;

unsigned long size;
size = pnm->_x * pnm->_y *3;
pnm->_data = new unsigned char[size];
cout << "size" << size << endl;
file.read((char *)pnm->_data,size);

char temp;
for(unsigned int y = 0; y< pnm->_y/2 ;y++)
{
for(unsigned int x = 0; x< pnm->_x ;x++)
{
temp = pnm->_data[y*pnm->_x + x];
pnm->_data[y*pnm->_x + x] = pnm->_data[(pnm->_y-1-y)*pnm->_x+x];
pnm->_data[(pnm->_y-1-y)*pnm->_x+x] = temp;

}
}



}
Image *pnm;
void LoadGLTextures()
{
pnm = new Image();
ImageLoad("test.pgm",pnm);

};


inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGB);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
}

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
LoadGLTextures();
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();
return 0;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glutReshapeWindow(pnm->_x, pnm->_y);
glDrawPixels(pnm->_x, pnm->_y, GL_LUMINANCE, GL_UNSIGNED_BYTE, pnm->_data);
glFlush();
}

暫定版その2


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <GL/glut.h>
#include <GL/gl.h>


void display();

using namespace std;

struct Image{
char _type[3];
unsigned int _x;
unsigned int _y;
int _bright;
unsigned char *_data;
int _Ptype;
};
typedef struct Image Image;

char *file= "h1.pgm";


void ReadData(char *filename,Image *pnm,int seek)
{

fstream file(filename,ios::in|ios::binary);
if(file.fail())
{
perror(filename);
exit(1);
}

file.seekp(seek);


unsigned long size;
size = pnm->_x * pnm->_y *3;
pnm->_data = new unsigned char[size];
cout << "size" << size << endl;

file.read((char *)pnm->_data,size);

char temp;
for(unsigned int y = 0; y< pnm->_y/2 ;y++)
{
for(unsigned int x = 0; x< pnm->_x ;x++)
{
temp = pnm->_data[y*pnm->_x + x];
pnm->_data[y*pnm->_x + x] = pnm->_data[(pnm->_y-1-y)*pnm->_x+x];
pnm->_data[(pnm->_y-1-y)*pnm->_x+x] = temp;

}
}
}

int ImageLoad(char *filename,Image *pnm)
{
fstream file(filename,ios::in|ios::binary);
if(file.fail())
{
perror(filename);
exit(1);
}


file >> pnm->_type; //typeチェック
char str[256]={0};

for(int count = 0;count < 3;)
{
file >> str;
if(strncmp(str,"#",1)== 0)//コメント無視
{
file.ignore(256,'\n');
}else
{
switch(count)
{
case 0: // X
pnm->_x = atoi(str);
break;
case 1: // Y
pnm->_y = atoi(str);
break;
case 2: // bright
pnm->_bright = atoi(str);
break;
default:
break;
}
count++;
}
}


return (file.tellp());

}

Image *pnm;
void LoadGLTextures()
{
int seek;

try{
pnm = new Image();
}catch(std::bad_alloc)
{
std::exit(0);
}

seek = ImageLoad(file,pnm);
ReadData(file,pnm,seek);

};


inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGB);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
}

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
LoadGLTextures();
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();
return 0;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glutReshapeWindow(pnm->_x, pnm->_y);
glDrawPixels(pnm->_x, pnm->_y, GL_LUMINANCE, GL_UNSIGNED_BYTE, pnm->_data);
glFlush();
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます