Advertisement
javitolin

Untitled

Mar 31st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. /*
  2. * assign1.cpp
  3. *
  4. * Created on: Mar 22, 2015
  5. * Author: jdorfsman
  6. */
  7. #include "GL/gl.h"
  8. #include "GL/glut.h"
  9. #include <fstream>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <iostream>
  14. GLuint texture;
  15. GLubyte *pic;
  16. unsigned char header[54];
  17. GLuint dataPos;
  18. GLuint width, height;
  19. GLuint imageSize;
  20. unsigned char * data;
  21. unsigned char * edges;
  22. char * filename;
  23. unsigned char * halftone;
  24. unsigned char * newHalftone;
  25. unsigned char * dither;
  26. float * ditherHelp;
  27.  
  28. using namespace std;
  29. void writeToFile(string filename, unsigned char * toPrint, char bAndW){
  30. ofstream myfile;
  31. myfile.open(filename.c_str());
  32. for(GLuint i = 0; i < width; i++){
  33. for(GLuint j = 0; j < height; j++){
  34. if(bAndW == '1'){
  35. myfile << (static_cast<unsigned>(toPrint[i+j*width]) == 255 ? "1" : "0") << ",";
  36. }
  37. else{
  38. myfile << (toPrint[i+j*width]) << ",";
  39. }
  40. }
  41. }
  42. myfile.close();
  43. }
  44. GLuint loadTexture() {
  45. FILE *f;
  46. f = fopen(filename, "rb");
  47. if (f == 0) {
  48. printf("Couldn't open file\n");
  49. exit(-1);
  50. }
  51. GLubyte header[54];
  52. fread(header, 54, 1, f);
  53. if (header[0] != 'B' || header[1] != 'M') {
  54. printf("File not bitmap\n");
  55. exit(1);
  56. }
  57. dataPos = *(int*) &(header[0x0A]);
  58. imageSize = *(int*) &(header[0x22]);
  59. width = *(int*) &(header[0x12]);
  60. height = *(int*) &(header[0x16]);
  61. if (imageSize == 0)
  62. imageSize = width * height * 3;
  63. if (dataPos == 0)
  64. dataPos = 54;
  65. data = new unsigned char[imageSize];
  66. edges = new unsigned char[imageSize];
  67. fread(data, 1, imageSize, f);
  68. fclose(f);
  69.  
  70. GLuint textureID;
  71. glGenTextures(1, &textureID);
  72. glBindTexture(GL_TEXTURE_2D, textureID);
  73. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  74. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  75. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  77. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
  78. GL_UNSIGNED_BYTE, data);
  79. glEnable(GL_TEXTURE_2D);
  80. return textureID;
  81. }
  82. GLuint applyDitheringFilter(){
  83. int colorNum=16;
  84. int sum;
  85. for(GLuint i=0;i<width;i++)
  86. {
  87. for(GLuint j=0;j<height;j++)
  88. {
  89. sum=0;
  90. ditherHelp[i+j*width]+=data[i+j*width];
  91. while(ditherHelp[i+j*width]>=256/colorNum && sum<255)
  92. {
  93. sum+=256/colorNum;
  94. ditherHelp[i+j*width]-=256/colorNum;
  95. }
  96. if(sum<255)
  97. dither[i+j*width]=sum;
  98. else dither[i+j*width]=240;
  99. if(i+j*width+1<width*height)
  100. ditherHelp[i+j*width+1]+=ditherHelp[i+j*width]*7.0/16.0;
  101. if(j<height-1)
  102. {
  103. if(i<width-1)
  104. ditherHelp[i+(1+j)*width+1]+=ditherHelp[i+j*width]*1/16.0;
  105. ditherHelp[i+(1+j)*width]+=ditherHelp[i+j*width]*5.0/16.0;
  106. ditherHelp[i+(1+j)*width-1]+=ditherHelp[i+j*width]*3.0/16.0;
  107. }
  108. }
  109. }
  110. GLuint textureID;
  111. glGenTextures(1, &textureID);
  112. glBindTexture(GL_TEXTURE_2D, textureID);
  113. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  114. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  115. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  116. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  117. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
  118. GL_UNSIGNED_BYTE, dither);
  119. return textureID;
  120. }
  121. GLuint applyHalftoneFilter()
  122. {
  123.  
  124. for(GLuint i=0;i<width;i++)
  125. {
  126.  
  127. for(GLuint j=0;j<height;j++)
  128. {
  129. GLuint k=j*2; //y
  130. GLuint l=i*2; // x
  131. halftone[l+k*width*2]=255;
  132. halftone[l+1+k*width*2]=255;
  133. halftone[l+1+(k+1)*width*2]=255;
  134. halftone[l+(k+1)*width*2]=255;
  135. if(data[i+j*width]<=255*1/5.0)
  136. halftone[l+k*width*2]=0;
  137. if(data[i+j*width]<=255*2/5.0)
  138. halftone[l+1+(k+1)*width*2]=0;
  139. if(data[i+j*width]<=255*3/5.0)
  140. halftone[l+1+(k)*width*2]=0;
  141. if(data[i+j*width]<=255*4/5.0)
  142. halftone[l+(k+1)*width*2]=0;
  143. }
  144. }
  145.  
  146. gluScaleImage(GL_LUMINANCE,width*2,height*2,GL_UNSIGNED_BYTE,halftone,width,height,GL_UNSIGNED_BYTE,newHalftone);
  147.  
  148. for(GLuint i=0;i<width;i++)
  149. {
  150. for(GLuint j=0;j<height;j++)
  151. {
  152. if(newHalftone[i+j*width]>130)
  153. newHalftone[i+j*width]=255;
  154. else newHalftone[i+j*width]=0;
  155. }
  156. }
  157.  
  158. GLuint textureID;
  159. glGenTextures(1, &textureID);
  160. glBindTexture(GL_TEXTURE_2D, textureID);
  161. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  162. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  163. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  164. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  165. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
  166. GL_UNSIGNED_BYTE, newHalftone);
  167. return textureID;
  168. }
  169. GLuint applyEdgeFilter(){
  170. int gxSobel[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
  171. int gySobel[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
  172. for(GLuint i = 0; i < width; i++){
  173. for(GLuint j = 0; j < height; j++){
  174. edges[i+j*width] = data[i+j*width];
  175. }
  176. }
  177. for(GLuint i = 0; i < width; i++){
  178. for(GLuint j = 0; j < height; j++){
  179. int sobelXPixel = 0, sobelYPixel = 0;
  180.  
  181. for(GLint sobelx = 0; sobelx < 3; sobelx++){
  182. for(GLint sobely = 0; sobely < 3; sobely++){
  183. if(i > 0 && j > 0 && i < width - 2 && j < height - 2){
  184. sobelXPixel += (gxSobel[sobelx][sobely]*edges[(i+sobelx)+(j+sobely)*width]);
  185. sobelYPixel += (gySobel[sobelx][sobely]*edges[(i+sobelx)+(j+sobely)*width]);
  186. }
  187. }
  188. }
  189. GLuint threshold = 150;
  190. GLuint val = sqrt(pow(sobelXPixel,2) + pow(sobelYPixel,2));
  191. if(val > threshold) val = 255;
  192. else val = 0;
  193. edges[i+j*width] = val;
  194. }
  195. }
  196. GLuint textureID;
  197. glGenTextures(1, &textureID);
  198. glBindTexture(GL_TEXTURE_2D, textureID);
  199. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  200. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  201. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  202. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  203. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
  204. GL_UNSIGNED_BYTE, edges);
  205. return textureID;
  206. }
  207. void mydisplay(void) {
  208. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  209. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  210.  
  211. //BOTTOM LEFT
  212. GLuint halftoneTexture = applyHalftoneFilter();
  213. //writeToFile("img5.txt",halftone, '1');
  214. glBindTexture(GL_TEXTURE_2D, halftoneTexture);
  215. glViewport(0, 0, 256, 256);
  216. glBegin(GL_QUADS);
  217. glTexCoord2f(1, 1);
  218. glVertex2f(-1, 1);
  219. glTexCoord2f(1, 0);
  220. glVertex2f(-1, -1);
  221. glTexCoord2f(0, 0);
  222. glVertex2f(1, -1);
  223. glTexCoord2f(0, 1);
  224. glVertex2f(1, 1);
  225. glEnd();
  226.  
  227. //BOTTOM RIGHT
  228. GLuint ditherTexture = applyDitheringFilter();
  229. writeToFile("img6.txt",dither, '0');
  230. glBindTexture(GL_TEXTURE_2D, ditherTexture);
  231. glViewport(256, 0, 256, 256);
  232. glBegin(GL_QUADS);
  233. glTexCoord2f(1, 1);
  234. glVertex2f(-1, 1);
  235. glTexCoord2f(1, 0);
  236. glVertex2f(-1, -1);
  237. glTexCoord2f(0, 0);
  238. glVertex2f(1, -1);
  239. glTexCoord2f(0, 1);
  240. glVertex2f(1, 1);
  241. glEnd();
  242.  
  243. ////TOP LEFT
  244. glBindTexture(GL_TEXTURE_2D, texture);
  245. glViewport(0, 256, 256, 256);
  246. glBegin(GL_QUADS);
  247. glTexCoord2f(1, 1);
  248. glVertex2f(-1, 1);
  249. glTexCoord2f(1, 0);
  250. glVertex2f(-1, -1);
  251. glTexCoord2f(0, 0);
  252. glVertex2f(1, -1);
  253. glTexCoord2f(0, 1);
  254. glVertex2f(1, 1);
  255. glEnd();
  256.  
  257. //TOP RIGHT
  258. GLuint edgeTexture = applyEdgeFilter();
  259. writeToFile("img4.txt",edges, '1');
  260. glBindTexture(GL_TEXTURE_2D, edgeTexture);
  261. glViewport(256, 256, 256, 256);
  262. glBegin(GL_QUADS);
  263. glTexCoord2f(1, 1);
  264. glVertex2f(-1, 1);
  265. glTexCoord2f(1, 0);
  266. glVertex2f(-1, -1);
  267. glTexCoord2f(0, 0);
  268. glVertex2f(1, -1);
  269. glTexCoord2f(0, 1);
  270. glVertex2f(1, 1);
  271. glEnd();
  272.  
  273. glFlush();
  274. }
  275. void init() {
  276. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  277. glutInitWindowSize(512, 512);
  278. glutCreateWindow("Sample");
  279.  
  280. glOrtho(-1.0, 1.0, -1.0, 1.0, 2.0, -2.0);
  281. texture = loadTexture();
  282. glutDisplayFunc(mydisplay);
  283. glutMainLoop();
  284. }
  285. int main(int argc, char** argv) {
  286. if (argc != 2) {
  287. printf("Usage: assign1 <img_file>");
  288. return -1;
  289. }
  290. filename = argv[1];
  291. glutInit(&argc, argv);
  292. init();
  293. return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement