GLWidget.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include <QtGui>
  2. #include <QtOpenGL>
  3. #include <cstring>
  4. #include <math.h>
  5. #include <iostream>
  6. #include "GLWidget.hpp"
  7. GLWidget::GLWidget(Image *tabim[], QWidget *parent)
  8. : QGLWidget(parent)
  9. {
  10. im=tabim;
  11. canal1_row=0;
  12. canal2_row=0;
  13. canal3_row=0;
  14. zoom=1;
  15. transX=0;
  16. transY=0;
  17. rotX=0;
  18. rotY=0;
  19. translation=false;
  20. rotation=false;
  21. mode2D=true;
  22. recompose();
  23. }
  24. GLWidget::~GLWidget()
  25. {
  26. }
  27. void GLWidget::maj_pt_vue()
  28. {
  29. if(mode2D)
  30. glViewport (transX, transY, composition->taillex*zoom, composition->tailley*zoom);
  31. else
  32. glViewport (0, 0, composition->taillex, composition->tailley);
  33. glMatrixMode (GL_PROJECTION);
  34. glLoadIdentity ();
  35. if(mode2D)
  36. glOrtho(0,composition->taillex,0,composition->tailley,-10,10);
  37. else
  38. {
  39. if(composition->taillex==composition->tailley)
  40. glOrtho(-125,375,-125,375,-40000,40000);
  41. else
  42. {
  43. int min,max;
  44. if(composition->taillex>composition->tailley)
  45. {
  46. min=-125-(500-500*((composition->taillex*1.0)/(composition->tailley*1.0))/2);
  47. max=375+(500-500*((composition->taillex*1.0)/(composition->tailley*1.0))/2);
  48. glOrtho(min,max,-125,375,-40000,40000);
  49. }
  50. else
  51. {
  52. min=-125-(500-500*((composition->tailley*1.0)/(composition->taillex*1.0))/2);
  53. max=375+(500-500*((composition->tailley*1.0)/(composition->taillex*1.0))/2);
  54. glOrtho(-125,375,min,max,-40000,40000);
  55. }
  56. }
  57. }
  58. glMatrixMode(GL_MODELVIEW);
  59. }
  60. QSize GLWidget::minimumSizeHint() const
  61. {
  62. return QSize(im[0]->taillex, im[0]->tailley);
  63. }
  64. QSize GLWidget::sizeHint() const
  65. {
  66. return QSize(im[0]->taillex, im[0]->tailley);
  67. }
  68. void GLWidget::modif_canal1(int row)
  69. {
  70. canal1_row=row;
  71. recompose();
  72. updateGL();
  73. }
  74. void GLWidget::modif_canal2(int row)
  75. {
  76. canal2_row=row;
  77. recompose();
  78. updateGL();
  79. }
  80. void GLWidget::modif_canal3(int row)
  81. {
  82. canal3_row=row;
  83. recompose();
  84. updateGL();
  85. }
  86. void GLWidget::sauver_image(const QString& str, int resolution)
  87. {
  88. string nom_fichier(QString(str).toStdString().data());
  89. if(nom_fichier.size()<4)
  90. nom_fichier.append(".png");
  91. if(nom_fichier[nom_fichier.size()-4]!='.')
  92. nom_fichier.append(".png");
  93. string extension=nom_fichier.substr(nom_fichier.size()-3,3);
  94. for(int i=0;i<3;i++)
  95. if(extension[i]<='z' && extension[i]>='a')
  96. extension[i]=extension[i]-32;
  97. if(string(extension).compare("PNG"))
  98. nom_fichier.append(".png");
  99. if(mode2D)
  100. resolution=1;
  101. QImage qim[resolution][resolution];
  102. for(int i=0;i<resolution;i++)
  103. for(int j=0;j<resolution;j++)
  104. {
  105. glViewport(-im[0]->taillex*i, -im[0]->tailley*j, im[0]->taillex*resolution, im[0]->tailley*resolution);
  106. updateGL();
  107. qim[i][j] = grabFrameBuffer();
  108. }
  109. QImage tosave(im[0]->taillex*resolution,im[0]->tailley*resolution,qim[0][0].format());
  110. for(int i=0;i<im[0]->taillex*resolution;i++)
  111. for(int j=0;j<im[0]->tailley*resolution;j++)
  112. tosave.setPixel(i, j, qim[i/im[0]->taillex][resolution-j/im[0]->tailley-1].pixel(i-((i/im[0]->taillex)*im[0]->taillex),j-((j/im[0]->tailley)*im[0]->tailley)));
  113. tosave.save(QString(nom_fichier.c_str()),"PNG");
  114. glViewport(0, 0, im[0]->taillex, im[0]->tailley);
  115. updateGL();
  116. }
  117. void GLWidget::passage_2D_3D()
  118. {
  119. mode2D=!mode2D;
  120. transX=0;
  121. transY=0;
  122. zoom=1;
  123. maj_pt_vue();
  124. updateGL();
  125. }
  126. void GLWidget::initializeGL()
  127. {
  128. qglClearColor(QColor::fromRgb(255,255,255));
  129. glClearDepth(1.0f);
  130. glEnable(GL_DEPTH_TEST);
  131. glDepthFunc(GL_LEQUAL);
  132. glDisable(GL_CULL_FACE);
  133. }
  134. void GLWidget::paintGL()
  135. {
  136. glClearColor(255.0,255.0,255.0,0.0);
  137. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  138. glLoadIdentity();
  139. if(!mode2D)
  140. {
  141. glScalef(zoom,zoom,zoom);
  142. glTranslatef(transX,transY,0);
  143. glTranslatef(128,128,128);
  144. glRotatef(rotY/2.0,1.0,0.0,0.0);
  145. glRotatef(rotX/2.0,0.0,1.0,0.0);
  146. glTranslatef(-128,-128,-128);
  147. }
  148. else
  149. glPointSize(zoom);
  150. if(mode2D)
  151. {
  152. for(int j=0;j<composition->tailley;j++)
  153. for(int i=0;i<composition->taillex;i++)
  154. {
  155. glColor4f(((composition->pix[j*composition->taillex+i][0])*1.0)/255.0,
  156. ((composition->pix[j*composition->taillex+i][1])*1.0)/255.0,
  157. ((composition->pix[j*composition->taillex+i][2])*1.0)/255.0,1.0);
  158. glBegin(GL_POINTS);
  159. glVertex2i(i,composition->tailley-j);
  160. glEnd();
  161. }
  162. }
  163. else
  164. {
  165. /*Affichage du repère*/
  166. glLineWidth(2.0);
  167. glColor4f(1.0,0.0,0.0,1.0);
  168. glBegin(GL_LINES);
  169. glVertex3f(0,0,0);
  170. glVertex3f(255,0,0);
  171. glEnd();
  172. glColor4f(0.0,1.0,0.0,1.0);
  173. glBegin(GL_LINES);
  174. glVertex3f(0,0,0);
  175. glVertex3f(0,255,0);
  176. glEnd();
  177. glColor4f(0.0,0.0,1.0,1.0);
  178. glBegin(GL_LINES);
  179. glVertex3f(0,0,0);
  180. glVertex3f(0,0,255);
  181. glEnd();
  182. glPointSize (2.0);
  183. for(int j=0;j<composition->tailley;j++)
  184. for(int i=0;i<composition->taillex;i++)
  185. {
  186. glColor4f(((composition->pix[j*composition->taillex+i][0])*1.0)/255.0,
  187. ((composition->pix[j*composition->taillex+i][1])*1.0)/255.0,
  188. ((composition->pix[j*composition->taillex+i][2])*1.0)/255.0,1.0);
  189. glBegin(GL_POINTS);
  190. glVertex3f(((composition->pix[j*composition->taillex+i][0])*1.0),
  191. ((composition->pix[j*composition->taillex+i][1])*1.0),
  192. ((composition->pix[j*composition->taillex+i][2])*1.0));
  193. glEnd();
  194. }
  195. }
  196. }
  197. void GLWidget::resizeGL(int width, int height)
  198. {
  199. width=height;
  200. height=width;
  201. maj_pt_vue();
  202. }
  203. void GLWidget::mousePressEvent(QMouseEvent *event)
  204. {
  205. if (event->button() & Qt::RightButton)
  206. {
  207. lastposx = event->x();
  208. lastposy = event->y();
  209. translation=true;
  210. }
  211. if (event->button() & Qt::MidButton)
  212. {
  213. transX=0;
  214. transY=0;
  215. rotX=0;
  216. rotY=0;
  217. zoom=1;
  218. maj_pt_vue();
  219. updateGL();
  220. }
  221. if(event->button() && Qt::LeftButton && !mode2D)
  222. {
  223. lastposx = event->x();
  224. lastposy = event->y();
  225. rotation=true;
  226. }
  227. }
  228. void GLWidget::mouseReleaseEvent(QMouseEvent *event)
  229. {
  230. translation=false;
  231. rotation=false;
  232. }
  233. void GLWidget::mouseMoveEvent(QMouseEvent *event)
  234. {
  235. if(translation)
  236. {
  237. double dx = 0, dy = 0;
  238. dx = event->x() - lastposx;
  239. dy = event->y() - lastposy;
  240. lastposx = event->x();
  241. lastposy = event->y();
  242. transX += dx;
  243. transY -= dy;
  244. maj_pt_vue();
  245. updateGL();
  246. }
  247. if(rotation)
  248. {
  249. double dx = 0, dy = 0;
  250. dx = event->x() - lastposx;
  251. dy = event->y() - lastposy;
  252. lastposx = event->x();
  253. lastposy = event->y();
  254. rotX += dx;
  255. rotY += dy;
  256. maj_pt_vue();
  257. updateGL();
  258. }
  259. }
  260. void GLWidget::wheelEvent(QWheelEvent * event)
  261. {
  262. if(event->delta()>0)
  263. {
  264. if(zoom<15)
  265. {
  266. if(mode2D)
  267. zoom=zoom*2;
  268. else
  269. zoom*=1.1;
  270. }
  271. }
  272. else
  273. {
  274. if(zoom>1.0001)
  275. {
  276. if(mode2D)
  277. zoom=zoom/2;
  278. else
  279. zoom/=1.1;
  280. if(transX>composition->taillex)
  281. transX=0;
  282. if(transY>composition->tailley)
  283. transY=0;
  284. if(-transX>composition->taillex*zoom)
  285. transX=(-composition->taillex*zoom+composition->taillex);
  286. if(-transY>composition->tailley*zoom)
  287. transY=(-composition->tailley*zoom+composition->tailley);
  288. }
  289. }
  290. maj_pt_vue();
  291. updateGL();
  292. }
  293. void GLWidget::recompose()
  294. {
  295. int mod1=canal1_row%3;
  296. int mod2=canal2_row%3;
  297. int mod3=canal3_row%3;
  298. int *can1=im[(canal1_row-mod1)/3]->extraire_canal(mod1);
  299. int *can2=im[(canal2_row-mod2)/3]->extraire_canal(mod2);
  300. int *can3=im[(canal3_row-mod3)/3]->extraire_canal(mod3);
  301. composition = new Image(im[0]->taillex,im[0]->tailley,can1,can2,can3);
  302. delete[] can1;
  303. delete[] can2;
  304. delete[] can3;
  305. }