Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * assign1.cpp
- *
- * Created on: Mar 22, 2015
- * Author: jdorfsman
- */
- #include "GL/gl.h"
- #include "GL/glut.h"
- #include <fstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <iostream>
- GLuint texture;
- GLubyte *pic;
- unsigned char header[54];
- GLuint dataPos;
- GLuint width, height;
- GLuint imageSize;
- unsigned char * data;
- unsigned char * edges;
- char * filename;
- unsigned char * halftone;
- unsigned char * newHalftone;
- unsigned char * dither;
- float * ditherHelp;
- using namespace std;
- void writeToFile(string filename, unsigned char * toPrint, char bAndW){
- ofstream myfile;
- myfile.open(filename.c_str());
- for(GLuint i = 0; i < width; i++){
- for(GLuint j = 0; j < height; j++){
- if(bAndW == '1'){
- myfile << (static_cast<unsigned>(toPrint[i+j*width]) == 255 ? "1" : "0") << ",";
- }
- else{
- myfile << (toPrint[i+j*width]) << ",";
- }
- }
- }
- myfile.close();
- }
- GLuint loadTexture() {
- FILE *f;
- f = fopen(filename, "rb");
- if (f == 0) {
- printf("Couldn't open file\n");
- exit(-1);
- }
- GLubyte header[54];
- fread(header, 54, 1, f);
- if (header[0] != 'B' || header[1] != 'M') {
- printf("File not bitmap\n");
- exit(1);
- }
- dataPos = *(int*) &(header[0x0A]);
- imageSize = *(int*) &(header[0x22]);
- width = *(int*) &(header[0x12]);
- height = *(int*) &(header[0x16]);
- if (imageSize == 0)
- imageSize = width * height * 3;
- if (dataPos == 0)
- dataPos = 54;
- data = new unsigned char[imageSize];
- edges = new unsigned char[imageSize];
- fread(data, 1, imageSize, f);
- fclose(f);
- GLuint textureID;
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
- GL_UNSIGNED_BYTE, data);
- glEnable(GL_TEXTURE_2D);
- return textureID;
- }
- GLuint applyDitheringFilter(){
- int colorNum=16;
- int sum;
- for(GLuint i=0;i<width;i++)
- {
- for(GLuint j=0;j<height;j++)
- {
- sum=0;
- ditherHelp[i+j*width]+=data[i+j*width];
- while(ditherHelp[i+j*width]>=256/colorNum && sum<255)
- {
- sum+=256/colorNum;
- ditherHelp[i+j*width]-=256/colorNum;
- }
- if(sum<255)
- dither[i+j*width]=sum;
- else dither[i+j*width]=240;
- if(i+j*width+1<width*height)
- ditherHelp[i+j*width+1]+=ditherHelp[i+j*width]*7.0/16.0;
- if(j<height-1)
- {
- if(i<width-1)
- ditherHelp[i+(1+j)*width+1]+=ditherHelp[i+j*width]*1/16.0;
- ditherHelp[i+(1+j)*width]+=ditherHelp[i+j*width]*5.0/16.0;
- ditherHelp[i+(1+j)*width-1]+=ditherHelp[i+j*width]*3.0/16.0;
- }
- }
- }
- GLuint textureID;
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
- GL_UNSIGNED_BYTE, dither);
- return textureID;
- }
- GLuint applyHalftoneFilter()
- {
- for(GLuint i=0;i<width;i++)
- {
- for(GLuint j=0;j<height;j++)
- {
- GLuint k=j*2; //y
- GLuint l=i*2; // x
- halftone[l+k*width*2]=255;
- halftone[l+1+k*width*2]=255;
- halftone[l+1+(k+1)*width*2]=255;
- halftone[l+(k+1)*width*2]=255;
- if(data[i+j*width]<=255*1/5.0)
- halftone[l+k*width*2]=0;
- if(data[i+j*width]<=255*2/5.0)
- halftone[l+1+(k+1)*width*2]=0;
- if(data[i+j*width]<=255*3/5.0)
- halftone[l+1+(k)*width*2]=0;
- if(data[i+j*width]<=255*4/5.0)
- halftone[l+(k+1)*width*2]=0;
- }
- }
- gluScaleImage(GL_LUMINANCE,width*2,height*2,GL_UNSIGNED_BYTE,halftone,width,height,GL_UNSIGNED_BYTE,newHalftone);
- for(GLuint i=0;i<width;i++)
- {
- for(GLuint j=0;j<height;j++)
- {
- if(newHalftone[i+j*width]>130)
- newHalftone[i+j*width]=255;
- else newHalftone[i+j*width]=0;
- }
- }
- GLuint textureID;
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
- GL_UNSIGNED_BYTE, newHalftone);
- return textureID;
- }
- GLuint applyEdgeFilter(){
- int gxSobel[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
- int gySobel[3][3] = {{1,2,1},{0,0,0},{-1,-2,-1}};
- for(GLuint i = 0; i < width; i++){
- for(GLuint j = 0; j < height; j++){
- edges[i+j*width] = data[i+j*width];
- }
- }
- for(GLuint i = 0; i < width; i++){
- for(GLuint j = 0; j < height; j++){
- int sobelXPixel = 0, sobelYPixel = 0;
- for(GLint sobelx = 0; sobelx < 3; sobelx++){
- for(GLint sobely = 0; sobely < 3; sobely++){
- if(i > 0 && j > 0 && i < width - 2 && j < height - 2){
- sobelXPixel += (gxSobel[sobelx][sobely]*edges[(i+sobelx)+(j+sobely)*width]);
- sobelYPixel += (gySobel[sobelx][sobely]*edges[(i+sobelx)+(j+sobely)*width]);
- }
- }
- }
- GLuint threshold = 150;
- GLuint val = sqrt(pow(sobelXPixel,2) + pow(sobelYPixel,2));
- if(val > threshold) val = 255;
- else val = 0;
- edges[i+j*width] = val;
- }
- }
- GLuint textureID;
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_LUMINANCE,
- GL_UNSIGNED_BYTE, edges);
- return textureID;
- }
- void mydisplay(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- //BOTTOM LEFT
- GLuint halftoneTexture = applyHalftoneFilter();
- //writeToFile("img5.txt",halftone, '1');
- glBindTexture(GL_TEXTURE_2D, halftoneTexture);
- glViewport(0, 0, 256, 256);
- glBegin(GL_QUADS);
- glTexCoord2f(1, 1);
- glVertex2f(-1, 1);
- glTexCoord2f(1, 0);
- glVertex2f(-1, -1);
- glTexCoord2f(0, 0);
- glVertex2f(1, -1);
- glTexCoord2f(0, 1);
- glVertex2f(1, 1);
- glEnd();
- //BOTTOM RIGHT
- GLuint ditherTexture = applyDitheringFilter();
- writeToFile("img6.txt",dither, '0');
- glBindTexture(GL_TEXTURE_2D, ditherTexture);
- glViewport(256, 0, 256, 256);
- glBegin(GL_QUADS);
- glTexCoord2f(1, 1);
- glVertex2f(-1, 1);
- glTexCoord2f(1, 0);
- glVertex2f(-1, -1);
- glTexCoord2f(0, 0);
- glVertex2f(1, -1);
- glTexCoord2f(0, 1);
- glVertex2f(1, 1);
- glEnd();
- ////TOP LEFT
- glBindTexture(GL_TEXTURE_2D, texture);
- glViewport(0, 256, 256, 256);
- glBegin(GL_QUADS);
- glTexCoord2f(1, 1);
- glVertex2f(-1, 1);
- glTexCoord2f(1, 0);
- glVertex2f(-1, -1);
- glTexCoord2f(0, 0);
- glVertex2f(1, -1);
- glTexCoord2f(0, 1);
- glVertex2f(1, 1);
- glEnd();
- //TOP RIGHT
- GLuint edgeTexture = applyEdgeFilter();
- writeToFile("img4.txt",edges, '1');
- glBindTexture(GL_TEXTURE_2D, edgeTexture);
- glViewport(256, 256, 256, 256);
- glBegin(GL_QUADS);
- glTexCoord2f(1, 1);
- glVertex2f(-1, 1);
- glTexCoord2f(1, 0);
- glVertex2f(-1, -1);
- glTexCoord2f(0, 0);
- glVertex2f(1, -1);
- glTexCoord2f(0, 1);
- glVertex2f(1, 1);
- glEnd();
- glFlush();
- }
- void init() {
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(512, 512);
- glutCreateWindow("Sample");
- glOrtho(-1.0, 1.0, -1.0, 1.0, 2.0, -2.0);
- texture = loadTexture();
- glutDisplayFunc(mydisplay);
- glutMainLoop();
- }
- int main(int argc, char** argv) {
- if (argc != 2) {
- printf("Usage: assign1 <img_file>");
- return -1;
- }
- filename = argv[1];
- glutInit(&argc, argv);
- init();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement