├── CGProject.h ├── CSE528Final.cbp ├── Global_warping.cpp ├── Interpolate.cpp ├── Project Report.pdf ├── README.txt ├── boundaries_sections.cpp ├── displace.cpp ├── displace1.cpp ├── image_representations.cpp ├── main.cpp ├── matrix4.cpp ├── mesh_place.cpp ├── my_project.cpp ├── sampling.cpp ├── seam_carvingH.cpp └── seam_carvingV.cpp /CGProject.h: -------------------------------------------------------------------------------- 1 | #ifndef CGPROJECT_H_INCLUDED 2 | #define CGPROJECT_H_INCLUDED 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //boundaries_sections 9 | void removeRed( BYTE* Buffer, int width, int height ); 10 | 11 | void markLeftBoundary( BYTE* Buffer, int width, int height ); 12 | void getLargestLeftBoundarySegment( BYTE* Buffer, int width, int height, int *yBegin, int *yEnd); 13 | void markRightBoundary( BYTE* Buffer, int width, int height ); 14 | void getLargestRightBoundarySegment( BYTE* Buffer, int width, int height, int *yBegin, int *yEnd); 15 | void markTopBoundary( BYTE* Buffer, int width, int height ); 16 | void getLargestTopBoundarySegment( BYTE* Buffer, int width, int height, int *yBegin, int *yEnd); 17 | void markBottomBoundary( BYTE* Buffer, int width, int height ); 18 | void getLargestBottomBoundarySegment(BYTE* Buffer, int width, int height, int *xBegin, int *xEnd); 19 | 20 | void markSegmentforLargestBoundary( BYTE* Buffer, int width, int height ); 21 | void getSegmentforLargestBoundary( BYTE* Buffer, int width, int height, int *xStart, int *xFin, int *yStart, int *yFin, int *tLen, int *dir ); 22 | void markSection( BYTE* Buffer, int xBegin, int xEnd, int yBegin, int yEnd, int tWidth ); 23 | void removeExtraBoundaries( BYTE* Buffer, int *width, int *height ); 24 | void getBoundaryMap(BYTE* Buffer, int width, int height, BYTE* newbuf); 25 | bool isBoundary(BYTE * Buffer, long newpos); 26 | 27 | //my_project.cpp 28 | BYTE* prepocess( BYTE* Buffer, int *width, int *height ); 29 | BYTE* LocalWarping( BYTE* Buffer, int width, int height, char * output ); 30 | 31 | 32 | 33 | //image_representations.cpp 34 | void saveAsImage(BYTE* Buffer, int width, int height, char* name); 35 | void getGreyScale( BYTE* Buffer, int width, int height, BYTE* newbuf); 36 | //BYTE* getYGradient( BYTE* I, int width, int height ); 37 | //BYTE* getXGradient( BYTE* I, int width, int height ); 38 | void getGradient( BYTE* I, int width, int height, BYTE* newbuf ); 39 | //BYTE* getIntensity( BYTE* Buffer, int width, int height ); 40 | //void printIntensity( BYTE* newbuff, int width, int height, BYTE* Buffer ); 41 | 42 | //seam_carvingH.cpp 43 | //BYTE* markHorizontalSeamAbove( BYTE* Buffer, int width, int height, int xStart, int xFin, int yStart, int yFin, BYTE* BoundaryMask); 44 | BYTE* getHDisplacementImage1(BYTE* Buffer, int width, int height, int xStart, int xFin, int yStart, int yFin, BYTE* BoundaryMask, BYTE* SeamMask); 45 | BYTE* getHDisplacementImage3(BYTE* Buffer, int width, int height, int xStart, int xFin, int yStart, int yFin, BYTE* BoundaryMask, BYTE* SeamMask); 46 | BYTE* getVDisplacementImage2(BYTE* Buffer, int width, int height, int xStart, int xFin, int yStart, int yFin, BYTE* BoundaryMask, BYTE* SeamMask); 47 | BYTE* getVDisplacementImage4(BYTE* Buffer, int width, int height, int xStart, int xFin, int yStart, int yFin, BYTE* BoundaryMask, BYTE* SeamMask); 48 | 49 | 50 | 51 | //basic_oper.c 52 | void BufCopy(BYTE* BufD, BYTE* BufS, int width, int height); 53 | void BufAvg(BYTE* Res, BYTE* Buf1, BYTE* Buf2, int width, int height); 54 | void BufImpose(BYTE* Res, BYTE* Buf1, BYTE* Buf2, int width, int height); 55 | 56 | //displace 57 | void displaceAbove(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, BYTE* SeamMask); 58 | void displaceBelow(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, BYTE* SeamMask); 59 | void displaceLeft(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer); 60 | void displaceRight(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer); 61 | 62 | void displaceAbove1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Hdisp,int xBegin, int xEnd, int yBegin, int yEnd, BYTE* SeamMask); 63 | void displaceBelow1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Hdisp,int xBegin, int xEnd, int yBegin, int yEnd, BYTE* SeamMask); 64 | void displaceLeft1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Hdisp, BYTE* SeamMask); 65 | void displaceRight1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Hdisp, BYTE* SeamMask); 66 | 67 | void displaceReal(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* D); 68 | void displaceBack(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* D); 69 | 70 | void hplace_mesh(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* temp); 71 | 72 | 73 | 74 | 75 | BYTE* scaleup2( BYTE* Buffer, int *width, int *height ); 76 | BYTE* downsampling( BYTE* Buffer, int *width, int *height, int n ); 77 | void tostring(char str[], int num); 78 | 79 | BYTE* ConvertRGBToBMPBuffer ( BYTE* Buffer, int width, int height, long* newsize ); 80 | bool SaveBMP ( BYTE* Buffer, int width, int height, long paddedsize, LPCTSTR bmpfile ); 81 | 82 | void interpolate(BYTE* Buffer, int* imesh, int* dmesh, int rows, int cols, int width, int height, char * name); 83 | void global_warping(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, char* output); 84 | void getNewMesh(int* imesh, int* dmesh, int rows, int cols, int width, int height); 85 | 86 | void testformAq(); 87 | void formAq(int a[8], float aqi[8][8]); 88 | void display88float(float A[8][8]); 89 | 90 | void getAffine(float A[16], float P[16], float Q[16]); 91 | void Affinefverify(); 92 | #endif // CGPROJECT_H_INCLUDED 93 | 94 | 95 | -------------------------------------------------------------------------------- /CSE528Final.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 48 | 49 | -------------------------------------------------------------------------------- /Global_warping.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include "CGProject.h" 6 | 7 | void displayMesh(int* mesh, int rows, int cols) 8 | { 9 | printf("\n"); 10 | for(int j=0;j=height?height-1:(y+Vdisp[newpos]); 84 | newpos_nextline = ynew * 3 * width + x + Hdisp[newpos]; 85 | imesh[(j*cols + i)*2] = x; 86 | imesh[(j*cols + i)*2 + 1] = y; 87 | 88 | dmesh[(j*cols + i)*2] = x + Hdisp[newpos]; 89 | dmesh[(j*cols + i)*2 + 1] = ynew; 90 | } 91 | 92 | //printMesh(imesh,rows,cols,width, height, strcat(output,"z12init.bmp")); 93 | output[14]='\0'; 94 | //displayMesh(imesh,rows,cols); 95 | //printMesh(dmesh,rows,cols,width, height, strcat(output,"z22dist.bmp")); 96 | output[14]='\0'; 97 | //displayMesh(dmesh,rows,cols); 98 | for(int x=0;x<50;x++) 99 | { 100 | //printf("\niteration=%x",x); 101 | getNewMesh(imesh,dmesh,rows,cols,width, height); 102 | } 103 | 104 | //printMesh(imesh,rows,cols,width, height, strcat(output,"z30fin.bmp")); 105 | //displayMesh(imesh,rows,cols); 106 | output[14]='\0'; 107 | 108 | 109 | interpolate(Buffer, imesh, dmesh, rows, cols, width, height, output); 110 | 111 | } 112 | 113 | void imposeBoundary(int* mesh, int rows, int cols, int width, int height) 114 | { 115 | int j=0,i=0; 116 | for(i=0;i 2 | #include 3 | #include "CGProject.h" 4 | 5 | 6 | void printMeshOnGuide(BYTE* Buffer, int* mesh, int rows, int cols, int width, int height) 7 | { 8 | //BYTE* Buffer = new BYTE[3 * (width) * (height)]; 9 | //memset(Buffer,0,3 * (width) * (height)); 10 | 11 | int x,y,newpos; 12 | //printf("\n"); 13 | for(int j=0;j= 0; 39 | } 40 | 41 | void fillWithT(float X[], int x1, int y1, int x2,int y2, int x3, int y3) 42 | { 43 | X[0] = x1; 44 | X[1] = x2; 45 | X[2] = x3; 46 | X[3] = 0; 47 | 48 | X[4] = y1; 49 | X[5] = y2; 50 | X[6] = y3; 51 | X[7] = 0; 52 | 53 | X[8] = 1.0; 54 | X[9] = 1.0; 55 | X[10] = 1.0; 56 | X[11] = 0; 57 | 58 | X[12] = 0; 59 | X[13] = 0; 60 | X[14] = 0; 61 | X[15] = 1.0; 62 | 63 | } 64 | 65 | void getTarget(float A[16], int source[4], int target[2]) 66 | { 67 | int r,c,k; 68 | source[2] = 1; 69 | source[3] = 0; 70 | for (r=0;r<2;r++) 71 | { 72 | for (c=0;c<1;c++) 73 | { 74 | float x = 0.0; 75 | for (k= 0;k<4;k++) 76 | { 77 | x += A[r*4+k]*source[k]; //((float)Aq[r][k])*partB[k][c]; 78 | //printf("\n%f X %f",A[r*4+k],source[k]); 79 | } 80 | //printf("\nx=%f\n",x); 81 | int x1 = (int)round(x); 82 | if(r==0) 83 | { 84 | x1 /= 3; 85 | x1 *= 3; 86 | } 87 | target[r] = x1; 88 | //printf ("r=%d, c=%d, x=%d \n", r,c,x ); 89 | } 90 | 91 | } 92 | } 93 | 94 | void Affinefverify() 95 | { 96 | int x1=1,x2=3,x3=6,y1=2,y2=6,y3=9; 97 | int xx1=21,xx2=26,xx3=31,yy1=5,yy2=10,yy3=9; 98 | 99 | float A1[16]; 100 | float P1[16],Q1[16]; 101 | 102 | fillWithT(P1,x1,y1,x2,y2,x3,y3); 103 | fillWithT(Q1,xx1,yy1,xx2,yy2,xx3,yy3); 104 | getAffine(A1,P1,Q1); 105 | int source[4], target[2]; 106 | source[0] = 2; 107 | source[1] = 4; 108 | getTarget(A1,source,target); 109 | printf("\n %d %d \n",target[0],target[1]); 110 | 111 | source[0] = 3; 112 | source[1] = 6; 113 | getTarget(A1,source,target); 114 | printf("\n %d %d \n",target[0],target[1]); 115 | 116 | } 117 | 118 | void process_this_quad(BYTE* Buffer, BYTE* guide, int* imesh, int* dmesh, int rows, int cols, int width, int height, int i, int j) 119 | { 120 | int x1,x2,x3,x4,y1,y2,y3,y4; 121 | int xx1,xx2,xx3,xx4,yy1,yy2,yy3,yy4; 122 | //********************************************* 123 | x1 = imesh[(j*cols + i)*2]; 124 | y1 = imesh[(j*cols + i)*2 + 1]; 125 | 126 | x2 = imesh[(j*cols + i+1)*2]; 127 | y2 = imesh[(j*cols + i+1)*2 + 1]; 128 | 129 | x3 = imesh[((j+1)*cols + i)*2]; 130 | y3 = imesh[((j+1)*cols + i)*2 + 1]; 131 | 132 | x4 = imesh[((j+1)*cols + i+1)*2]; 133 | y4 = imesh[((j+1)*cols + i+1)*2 + 1]; 134 | //********************************************* 135 | xx1 = dmesh[(j*cols + i)*2]; 136 | yy1 = dmesh[(j*cols + i)*2 + 1]; 137 | 138 | xx2 = dmesh[(j*cols + i+1)*2]; 139 | yy2 = dmesh[(j*cols + i+1)*2 + 1]; 140 | 141 | xx3 = dmesh[((j+1)*cols + i)*2]; 142 | yy3 = dmesh[((j+1)*cols + i)*2 + 1]; 143 | 144 | xx4 = dmesh[((j+1)*cols + i+1)*2]; 145 | yy4 = dmesh[((j+1)*cols + i+1)*2 + 1]; 146 | //********************************************* 147 | 148 | float A1[16], A2[16]; 149 | float P1[16],P2[16],Q1[16],Q2[16] ; 150 | int source[4], target[2]; 151 | fillWithT(P1,x1,y1,x2,y2,x3,y3); 152 | fillWithT(Q1,xx1,yy1,xx2,yy2,xx3,yy3); 153 | 154 | fillWithT(P2,x4,y4,x2,y2,x3,y3); 155 | fillWithT(Q2,xx4,yy4,xx2,yy2,xx3,yy3); 156 | 157 | getAffine(A1,P1,Q1); 158 | getAffine(A2,P2,Q2); 159 | 160 | //********************************************* 161 | bool iL21, iL13, iL34, iL42; 162 | int x,y,xn1,yn1; 163 | long newpos,newpos1; 164 | 165 | for (y = 0; y < height; y++ ) 166 | { 167 | for (x= 0; x < 3 * width; x+=3 ) 168 | { 169 | //(int bx, int by, int ax, int ay, int cx, int cy) 170 | //printf("1 = %d %d, 2 = %d %d, 3 = %d %d, 4=%d %d, this = %d %d",x1,y1,x2,y2,x3,y3,x4,y4,x,y); 171 | iL21 = isLeft(x1,y1,x2,y2,x,y); 172 | iL13 = isLeft(x3,y3,x1,y1,x,y); 173 | iL34 = isLeft(x4,y4,x3,y3,x,y); 174 | iL42 = isLeft(x2,y2,x4,y4,x,y); 175 | //printf("21 = %d, 13 = %d, 34 = %d, 42 = %d",iL21,iL13,iL34,iL42); 176 | if(iL21&&iL13&&iL34&&iL42) 177 | { 178 | newpos = y * 3 * width + x; 179 | source[0] = x; 180 | source[1] = y; 181 | if(isLeft(x2,y2,x3,y3,x,y)) 182 | { 183 | 184 | getTarget(A1,source,target); 185 | xn1 = target[0]<0?0:target[0]; 186 | xn1=target[0]>=(3*width)?(3*width-3):target[0]; 187 | yn1 = target[1]<0?0:target[1]; 188 | yn1=target[1]>=height?height-1:target[1]; 189 | 190 | newpos1 = yn1 * 3 * width + xn1; 191 | 192 | guide[newpos] = Buffer[newpos1]; 193 | guide[newpos+1] = Buffer[newpos1+1]; 194 | guide[newpos+2] = Buffer[newpos1+2]; 195 | 196 | } 197 | else 198 | { 199 | getTarget(A2,source,target); 200 | xn1 = target[0]<0?0:target[0]; 201 | xn1=target[0]>=(3*width)?(3*width-3):target[0]; 202 | yn1 = target[1]<0?0:target[1]; 203 | yn1=target[1]>=height?height-1:target[1]; 204 | 205 | newpos1 = yn1 * 3 * width + xn1; 206 | 207 | guide[newpos] = Buffer[newpos1]; 208 | guide[newpos+1] = Buffer[newpos1+1]; 209 | guide[newpos+2] = Buffer[newpos1+2]; 210 | 211 | } 212 | } 213 | 214 | } 215 | } 216 | } 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | void interpolate(BYTE* Buffer, int* imesh, int* dmesh, int rows, int cols, int width, int height, char * name) 226 | { 227 | 228 | //saveAsImage(Buffer, width, height, strcat(name,"iip.bmp")); 229 | //name[14]='\0'; 230 | BYTE* guide = new BYTE[3 * (width) * (height)]; 231 | long newsize = 3 * (width) * (height); 232 | memset(guide,0,newsize); 233 | //printMeshOnGuide(guide,imesh,rows,cols,width, height); 234 | for(int j=0; j,, and . Since is used, this needs windows to compile. 6 | Some functions have been described as "written for dev./testing purpose". They have not been called. 7 | 8 | 9 | Following is the list of files(in alphabetical order of names) and functions implented. First those files are mentioned which contain some code copied from internet. For such files, each function has been mentioned separately, if written by me or taken from internet. 10 | 11 | 12 | 1. main.cpp 550 lines. Contains code copied from http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html. the copied code is used to read and save bitmap images 13 | 14 | function BYTE* ConvertRGBToBMPBuffer ( BYTE* Buffer, int width, int height, long* newsize ) 15 | description save image to a buffer. copied from the reference mentioned above 16 | 17 | function BYTE* ConvertRGBToBMPBuffer1 ( BYTE* Buffer, int width, int height, long* newsize, int oldWidth ) 18 | description for dev. purpose, not used anymore. saves image to a buffer. written by me 19 | 20 | function BYTE* ConvertBMPToRGBBuffer ( BYTE* Buffer, int width, int height ) 21 | description converts bmp buffer to rgb 3-color 2D array. copied from the reference mentioned above 22 | 23 | 24 | function bool LoadBMPIntoDC ( HDC hDC, LPCTSTR bmpfile ) 25 | description Takes in a device context and the name of a bitmap to load. copied from the reference mentioned above 26 | 27 | function bool SaveBMP ( BYTE* Buffer, int width, int height, long paddedsize, LPCTSTR bmpfile ) 28 | description saves the file onto the disk by reading bmp buffer. copied from the reference mentioned above 29 | 30 | function BYTE* LoadBMP ( int* width, int* height, long* size, LPCTSTR bmpfile ) 31 | description loads the bmp buffer by reading the bmp file from disk. copied from the reference mentioned above 32 | 33 | function void TestBMPCopy (LPCTSTR input, LPCTSTR output) 34 | description for test purpose, not used anymore. written by me 35 | 36 | function void TestBMPCopy2(LPCTSTR input, char* output) 37 | description process each image. written by me 38 | 39 | function int main () 40 | description process all the images. code submitted processes only 1 images. to increase, change the test condition in for loop. written by me 41 | 42 | function int mainxx () 43 | description for test purpose, not used anymore. written by me 44 | 45 | 46 | 47 | 48 | 49 | /**************************************************************************************************************************************************************/ 50 | 51 | 2. matrix4.cpp 360 lines Contains code copied from http://rodolphe-vaillant.fr/?e=7 and http://dev.tplanet.net/Matrix2D/ 52 | 53 | 54 | function formAq(int A[8], int Aq[8][4], int AqT[4][8]) 55 | description given quad pixel locations, form the Aq(8x4) and AqT(4X8) matrices. written by me 56 | 57 | function void displayAq(int Aq[8][4]) 58 | description display on console. written by me 59 | 60 | function void displayAqF(float Aq[8][4]) 61 | description display on console for float type. written by me 62 | 63 | function void displayAqT(int Aq[4][8]) 64 | description display on console. written by me 65 | 66 | function void displayA(int A[8]) 67 | description display on console. written by me 68 | 69 | function void display44(int A[4][4]) 70 | description display on console. written by me 71 | 72 | function void mat4mulB(int AqTAq[4][4], int Aq[8][4], int AqT[4][8]) 73 | description matrix multiply. copied from the reference mentioned above, with minor modifications to suit my requirement 74 | 75 | function void mat4mulA(float partA[8][4], int Aq[8][4], float partB[4][4]) 76 | description matrix multiply. copied from the reference mentioned above, with minor modifications to suit my requirement 77 | 78 | function void mat4mulC(float partC[8][8], float partA[8][4], int AqT[4][8]) 79 | description matrix multiply. copied from the reference mentioned above, with minor modifications to suit my requirement 80 | 81 | function bool invertColumnMajor(float m[16], float invOut[16]) 82 | description matrix inverse. copied from the reference mentioned above. 83 | 84 | function void mat4mulFloat(float A[16], float P_1[16], float Q[16]) 85 | description matrix multiply. copied from the reference mentioned above, with minor modifications to suit my requirement 86 | 87 | function void display16float(float A[16]) 88 | description display on console. written by me 89 | 90 | function void getAffine(float A[16], float P[16], float Q[16]) 91 | description get Affine transformation matrix. written by me 92 | 93 | function void display44float(float A[4][4]) 94 | description display on console. written by me 95 | 96 | function void display88float(float A[8][8]) 97 | description display on console. written by me 98 | 99 | function void getInv(int a[4][4], float i[4][4]) 100 | description wrapper for matrix inversion. written by me 101 | 102 | function void testformAq() 103 | description for test purpose, not used anymore. written by me 104 | 105 | function void formAq(int a[8], float aqi[8][8]) 106 | description calculate the BIG term for the energy calculation 107 | 108 | 109 | /**************************************************************************************************************************************************************/ 110 | 111 | 3. sampling.cpp 120 lines. contains code copied from http://www.sanfoundry.com/c-program-integer-to-string-vice-versa/ 112 | 113 | function BYTE* downsampling( BYTE* Buffer, int *width, int *height, int n ) 114 | description downsample the image, by a factor of n in both the dimensions. written by me 115 | 116 | function BYTE* scaleup2( BYTE* Buffer, int *width, int *height ) 117 | description scale using linear interpolation, by a factor of 2. written by me 118 | 119 | function void tostring(char str[], int num) 120 | description convert an int to string. copied from the reference mentioned above 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | /**************************************************************************************************************************************************************/ 133 | All the code described below is completely written by me. 134 | /**************************************************************************************************************************************************************/ 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 4. boundaries_sections.cpp 760 lines 143 | 144 | function bool isBoundary(BYTE * Buffer, long newpos) 145 | description Tells if a given pixel location is inside the panorama or outside, in the white boundary. 146 | 147 | 148 | function void BufCopy(BYTE* BufD, BYTE* BufS, int width, int height) 149 | description copy one image arrays 150 | 151 | function void BufAvg(BYTE* Res, BYTE* Buf1, BYTE* Buf2, int width, int height) 152 | description find average of two images. mainly used to average the energies in different images. 153 | 154 | function void BufImpose(BYTE* Res, BYTE* Buf1, BYTE* Mask, int width, int height) 155 | description impose the white boundary on the energy map 156 | 157 | function void removeRed( BYTE* Buffer, int width, int height ) 158 | description written for testing purpose. removes the red component of each pixel 159 | 160 | function void markLeftBoundary( BYTE* Buffer, int width, int height ) 161 | description written for testing purpose.marks the left boundary 162 | 163 | function void getLargestLeftBoundarySegment(BYTE* Buffer, int width, int height, int *yBegin, int *yEnd, int *maxlen) 164 | description gets the end points of the largest white boundary left edge. 165 | 166 | function void markRightBoundary( BYTE* Buffer, int width, int height ) 167 | description written for testing purpose.marks the right boundary 168 | 169 | function void getLargestRightBoundarySegment(BYTE* Buffer, int width, int height, int *yBegin, int *yEnd, int *maxlen) 170 | description gets the end points of the largest white boundary right edge. 171 | 172 | function void markTopBoundary( BYTE* Buffer, int width, int height ) 173 | description written for testing purpose.marks the top boundary 174 | 175 | function void getLargestTopBoundarySegment(BYTE* Buffer, int width, int height, int *xBegin, int *xEnd, int *maxlen) 176 | description gets the end points of the largest white boundary top edge. 177 | 178 | 179 | function void markBottomBoundary( BYTE* Buffer, int width, int height ) 180 | description written for testing purpose.marks the top boundary bottom edge. 181 | 182 | 183 | function void getLargestBottomBoundarySegment(BYTE* Buffer, int width, int height, int *xBegin, int *xEnd, int *maxlen) 184 | description gets the end points of the largest white boundary bottom edge. 185 | 186 | 187 | function void markSectionBoundary( BYTE* Buffer, int xBegin, int xEnd, int yBegin, int yEnd, int tWidth ) 188 | description written for testing purpose.marks the rectangular section boundary 189 | 190 | 191 | function void markSection( BYTE* Buffer, int xBegin, int xEnd, int yBegin, int yEnd, int tWidth ) 192 | description written for testing purpose.marks the rectangular section 193 | 194 | 195 | 196 | function void markAllBoundaries( BYTE* Buffer, int width, int height ) 197 | description written for testing purpose.marks all the boundary edges 198 | 199 | function void markSegmentforLargestBoundary( BYTE* Buffer, int width, int height ) 200 | description written for testing purpose.marks the top boundary 201 | 202 | 203 | function void markSegmentforLargestBoundary( BYTE* Buffer, int width, int height ) 204 | description written for testing purpose.marks the rectangular section with largest white boundary 205 | 206 | 207 | function void getSegmentforLargestBoundary( BYTE* Buffer, int width, int height, int *xStart, int *xFin, int *yStart, int *yFin, int *tLen, int *dir ) 208 | description get the end points of the largest white boundary 209 | 210 | function void removeExtraLeftBoundary( BYTE* Buffer, int *width, int *height ) 211 | description used in pre-processing, to remove the extra boundary on left hand side. 212 | 213 | function void removeExtraTopBoundary( BYTE* Buffer, int *width, int *height ) 214 | description used in pre-processing, to remove the extra boundary on top. 215 | 216 | 217 | function void removeExtraBoundaries( BYTE* Buffer, int *width, int *height ) 218 | description used in pre-processing, to remove the extra boundary 219 | 220 | function void getBoundaryMap(BYTE* Buffer, int width, int height, BYTE* newbuf) 221 | description used segmentation-like technique to separate image and the white boundary 222 | 223 | 224 | 225 | /**************************************************************************************************************************************************************/ 226 | 227 | 228 | 229 | 5. displace.cpp 150 lines 230 | 231 | 232 | function void displaceAbove(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 233 | description Displace the region above the seam, one pixel up. old version, not used now 234 | 235 | 236 | function void displaceBelow(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 237 | description Displace the region below the seam, one pixel down. old version, not used now 238 | 239 | 240 | function void displaceLeft(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 241 | description Displace the region left of the seam, one pixel left. old version, not used now 242 | 243 | function void displaceRight(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 244 | description Displace the region right of the seam, one pixel right. old version, not used now 245 | 246 | /**************************************************************************************************************************************************************/ 247 | 248 | 6. displace1.cpp 240 lines 249 | 250 | 251 | function void displaceAbove1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 252 | description Displace the region above the seam, one pixel up. 253 | 254 | 255 | function void displaceBelow1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 256 | description Displace the region below the seam, one pixel down. 257 | 258 | 259 | function void displaceLeft1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 260 | description Displace the region left of the seam, one pixel left. 261 | 262 | function void displaceRight1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 263 | description Displace the region right of the seam, one pixel right. 264 | 265 | 266 | function void displaceReal(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* D) 267 | description displace directly, by using displacement map. 268 | 269 | function void displaceBack(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* D) 270 | description used for reverse displacement 271 | 272 | /**************************************************************************************************************************************************************/ 273 | 274 | 7. Global_warping 710 lines 275 | 276 | function void displayMesh(int* mesh, int rows, int cols) 277 | description display the mesh-pixel locations on console 278 | 279 | function void printMesh(int* mesh, int rows, int cols, int width, int height, char * name) 280 | description print the mesh to a bmp image 281 | 282 | function void global_warping(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, char* output) 283 | description wrapper for the energy based warping 284 | 285 | function void imposeBoundary(int* mesh, int rows, int cols, int width, int height) 286 | description push the mesh boundary to the rectangle. this is the implementation of boundary constraints 287 | 288 | function float matEi(float Cpi[8][8], int Vi[8]) 289 | description find energy value for ith quad 290 | 291 | function float getEnregyi(int i, int j, int* imesh, int* dmesh, int cols) 292 | description wrapper for above function 293 | 294 | function float getEnregyq(int j, int i, int* imesh, int* dmesh, int cols) 295 | description get quad energies for 4 quads surrounding i,j 296 | 297 | function float getEnregyqtop(int j, int i, int* imesh, int* dmesh, int cols) 298 | description get quad energies for the 2 quads below i,j. this is for mesh points on top boundary 299 | 300 | function void getBetterPositiontop(int j, int i, int* imesh, int* dmesh, int cols) 301 | description get energy optimal position for i,j mesh point which lies on top boundary 302 | 303 | function float getEnregyqbottom(int j, int i, int* imesh, int* dmesh, int cols) 304 | description get quad energies for the 2 quads above i,j. this is for mesh points on bottom boundary 305 | 306 | function void getBetterPositionbottom(int j, int i, int* imesh, int* dmesh, int cols) 307 | description get energy optimal position for i,j mesh point which lies on bottom boundary 308 | 309 | function float getEnregyqleft(int j, int i, int* imesh, int* dmesh, int cols) 310 | description get quad energies for the 2 quads right of i,j. this is for mesh points on left boundary 311 | 312 | function void getBetterPositionleft(int j, int i, int* imesh, int* dmesh, int cols) 313 | description get energy optimal position for i,j mesh point which lies on left boundary 314 | 315 | function void managethisleftCol(int i, int* imesh, int* dmesh, int rows, int cols) 316 | description getBetter positions for all the pixels on left boundary 317 | 318 | function float getEnregyqright(int j, int i, int* imesh, int* dmesh, int cols) 319 | description get quad energies for the 2 quads right of i,j. this is for mesh points on right boundary 320 | 321 | function void getBetterPositionright(int j, int i, int* imesh, int* dmesh, int cols) 322 | description get energy optimal position for i,j mesh point which lies on right boundary 323 | 324 | function void managethisrightCol(int i, int* imesh, int* dmesh, int rows, int cols) 325 | description getBetter positions for all the pixels on right boundary 326 | 327 | function void getBetterPosition(int j, int i, int* imesh, int* dmesh, int cols) 328 | description getBetter positions for the interior pixel at i,j 329 | 330 | function void managethistopRow(int j, int* imesh, int* dmesh, int cols) 331 | description getBetter positions for all the pixels on top boundary 332 | 333 | function void managethisbottomRow(int j, int* imesh, int* dmesh, int cols) 334 | description getBetter positions for all the pixels on bottom boundary 335 | 336 | function void managethisRow(int j, int* imesh, int* dmesh, int cols) 337 | description getBetter positions for all the interior pixels 338 | 339 | function void getNewMesh(int* imesh, int* dmesh, int rows, int cols, int width, int height) 340 | description wrapper for calling managethisRow iteratively 341 | 342 | 343 | 344 | /**************************************************************************************************************************************************************/ 345 | 346 | 8. image_representations.cpp 260 lines 347 | 348 | function void getGreyScale( BYTE* Buffer, int width, int height, BYTE* newbuf ) 349 | description convert to grey scale image 350 | 351 | function BYTE* getYGradient( BYTE* I, int width, int height ) 352 | description get the gradient in vertical direction 353 | 354 | function BYTE* getYGradientColor( BYTE* I, int width, int height ) 355 | description get the gradient in vertical direction for a colored image 356 | 357 | function BYTE* getXGradient( BYTE* I, int width, int height ) 358 | description get the gradient in horizontal direction 359 | 360 | function BYTE* getXGradientColor( BYTE* I, int width, int height ) 361 | description get the gradient in horizontal direction for a colored image 362 | 363 | function void getGradient( BYTE* I, int width, int height, BYTE* newbuf ) 364 | description get the gradient (wrapper for x&Y gradient functions) 365 | 366 | function void saveAsImage(BYTE* Buffer, int width, int height, char* name) 367 | description save image as .bmp file (a wrapper) 368 | 369 | 370 | /**************************************************************************************************************************************************************/ 371 | 372 | 9. mesh_place.cpp 180 lines 373 | 374 | function void hplace_mesh_v0(BYTE* Buffer, int width, int height) 375 | description old versio,not used anymore. 376 | 377 | function void hplace_mesh(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* temp) 378 | description place the initial rectangular mesh on the images. 379 | 380 | 381 | /**************************************************************************************************************************************************************/ 382 | 383 | 10. my_project.cpp 250 lines 384 | 385 | function BYTE* prepocess( BYTE* Buffer, int *width, int *height ) 386 | description preprocessing -> removing the extra boundaries 387 | 388 | function BYTE* LocalWarping( BYTE* Buffer, int width, int height, char * output ) 389 | description perform local warping using seam carving. calls to sema placement and global warping 390 | 391 | 392 | /**************************************************************************************************************************************************************/ 393 | 394 | 11. seam_carvingH.cpp 310 lines 395 | 396 | function BYTE* markHorizontalSeamAbove( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 397 | description mark a seam, by finding the first seam seed from the top. 398 | 399 | function BYTE* markHorizontalSeamBelow (BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 400 | description mark a seam, by finding the first seam seed from the bottom. 401 | 402 | function BYTE* getHDisplacementImage1( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 403 | description get displacement map above the seam 404 | 405 | function BYTE* getHDisplacementImage3( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 406 | description get displacement map below the seam 407 | 408 | 409 | /**************************************************************************************************************************************************************/ 410 | 411 | 12. seam_carvingV.cpp 310 lines 412 | 413 | function BYTE* markVerticalSeamLeft( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 414 | description mark a seam, by finding the first seam seed from the left. 415 | 416 | function BYTE* markHorizontalSeamBelowxx (BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 417 | description not used anymore. 418 | 419 | function BYTE* getVDisplacementImage2( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 420 | description get displacement map left of the seam 421 | 422 | function BYTE* getVDisplacementImage4( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 423 | description get displacement map right of the seam 424 | 425 | -------------------------------------------------------------------------------- /boundaries_sections.cpp: -------------------------------------------------------------------------------- 1 | #include "CGProject.h" 2 | 3 | bool isBoundary(BYTE * Buffer, long newpos){ 4 | if(Buffer[newpos]>249 && Buffer[newpos+1]>249 && Buffer[newpos+2]>249){ 5 | return true; 6 | } 7 | else 8 | return false; 9 | } 10 | 11 | void BufCopy(BYTE* BufD, BYTE* BufS, int width, int height) 12 | { 13 | int x,y; 14 | long newpos; 15 | 16 | for ( y = 0; y < height; y++ ) 17 | for ( x= 0; x < 3 * width; x+=3 ) 18 | { 19 | newpos = y * 3 * width + x; 20 | BufD[newpos] = BufS[newpos]; 21 | BufD[newpos+1] = BufS[newpos+1]; 22 | BufD[newpos+2] = BufS[newpos+2]; 23 | } 24 | } 25 | 26 | 27 | void BufAvg(BYTE* Res, BYTE* Buf1, BYTE* Buf2, int width, int height) 28 | { 29 | int x,y; 30 | long newpos; 31 | 32 | for ( y = 0; y < height; y++ ) 33 | for ( x= 0; x < 3 * width; x+=3 ) 34 | { 35 | newpos = y * 3 * width + x; 36 | Res[newpos] = (Buf1[newpos] + Buf2[newpos])/2; 37 | Res[newpos + 1] = (Buf1[newpos+1] + Buf2[newpos+1])/2; 38 | Res[newpos + 2] = (Buf1[newpos+2] + Buf2[newpos+2])/2; 39 | } 40 | } 41 | 42 | void BufImpose(BYTE* Res, BYTE* Buf1, BYTE* Mask, int width, int height) 43 | { 44 | int x,y; 45 | long newpos; 46 | 47 | for ( y = 0; y < height; y++ ) 48 | for ( x= 0; x < 3 * width; x+=3 ) 49 | { 50 | newpos = y * 3 * width + x; 51 | if (isBoundary(Mask,newpos)) 52 | { 53 | Res[newpos] = 255; 54 | Res[newpos + 1] = 255; 55 | Res[newpos + 2] = 255; 56 | } 57 | else 58 | { 59 | Res[newpos] = Buf1[newpos]; 60 | Res[newpos + 1] = Buf1[newpos+1]; 61 | Res[newpos + 2] = Buf1[newpos+2]; 62 | } 63 | } 64 | } 65 | 66 | void removeRed( BYTE* Buffer, int width, int height ) 67 | { int y,x; 68 | long newpos = 0; 69 | for ( y = 0; y < height; y++ ) 70 | for ( x= 0; x < 3 * width; x+=3 ) 71 | { 72 | newpos = y * 3 * width + x; 73 | 74 | Buffer[newpos] = Buffer[newpos]*0; //red 75 | Buffer[newpos + 1] = Buffer[newpos+1]; //green 76 | Buffer[newpos + 2] = Buffer[newpos+2]; //blue 77 | } 78 | } 79 | 80 | 81 | void markLeftBoundary( BYTE* Buffer, int width, int height ) 82 | { 83 | int y,x; 84 | long newpos = 0; 85 | 86 | x=0; //left bounadary 87 | for ( y = 0; y < height; y++ ){ 88 | newpos = y * 3 * width + x; 89 | //mark only if its empty 90 | if(isBoundary(Buffer,newpos)){ 91 | Buffer[newpos] = 255; //red 92 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 93 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 94 | } 95 | } 96 | } 97 | 98 | void getLargestLeftBoundarySegment(BYTE* Buffer, int width, int height, int *yBegin, int *yEnd, int *maxlen) 99 | { 100 | int y,x, yBeginTemp=-1, yEndTemp=-1, len = 0; 101 | *maxlen=0; 102 | long newpos = 0; 103 | 104 | x=0; //left bounadary 105 | for ( y = 0; y < height; y++ ){ 106 | newpos = y * 3 * width + x; 107 | 108 | if(isBoundary(Buffer,newpos)){ 109 | if(len==0){ 110 | yBeginTemp = y; 111 | yEndTemp = y; 112 | } 113 | else{ 114 | yEndTemp = y; 115 | } 116 | len++; 117 | } 118 | else{ 119 | len = 0; 120 | } 121 | 122 | if(len>(*maxlen)){ 123 | *maxlen = len; 124 | *yBegin = yBeginTemp; 125 | *yEnd = yEndTemp; 126 | } 127 | } 128 | 129 | if(yBeginTemp==-1){ 130 | return; 131 | } 132 | //paint it 133 | /*for ( y = *yBegin; y <= *yEnd; y++ ){ 134 | newpos = y * 3 * width + x; 135 | //mark only if its empty 136 | if(isBoundary(Buffer,newpos)){ 137 | Buffer[newpos] = 255; //red 138 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 139 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 140 | } 141 | }*/ 142 | } 143 | 144 | 145 | void markRightBoundary( BYTE* Buffer, int width, int height ) 146 | { 147 | int y,x; 148 | long newpos = 0; 149 | 150 | x=3 * width - 3; //right bounadary 151 | for ( y = 0; y < height; y++ ){ 152 | newpos = y * 3 * width + x; 153 | //mark only if its empty 154 | if(isBoundary(Buffer,newpos)){ 155 | Buffer[newpos] = 255; //red 156 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 157 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 158 | } 159 | } 160 | } 161 | 162 | 163 | void getLargestRightBoundarySegment(BYTE* Buffer, int width, int height, int *yBegin, int *yEnd, int *maxlen) 164 | { 165 | int y,x, yBeginTemp=-1, yEndTemp=-1, len = 0; 166 | *maxlen=0; 167 | long newpos = 0; 168 | 169 | x=3 * width - 3; //right bounadary 170 | for ( y = 0; y < height; y++ ){ 171 | newpos = y * 3 * width + x; 172 | 173 | if(isBoundary(Buffer,newpos)){ 174 | if(len==0){ 175 | yBeginTemp = y; 176 | yEndTemp = y; 177 | } 178 | else{ 179 | yEndTemp = y; 180 | } 181 | len++; 182 | } 183 | else{ 184 | len = 0; 185 | } 186 | 187 | if(len>(*maxlen)){ 188 | *maxlen = len; 189 | *yBegin = yBeginTemp; 190 | *yEnd = yEndTemp; 191 | } 192 | } 193 | 194 | if(yBeginTemp==-1){ 195 | return; 196 | } 197 | //paint it 198 | /*for ( y = *yBegin; y <= *yEnd; y++ ){ 199 | newpos = y * 3 * width + x; 200 | //mark only if its empty 201 | if(isBoundary(Buffer,newpos)){ 202 | Buffer[newpos] = 255; //red 203 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 204 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 205 | } 206 | }*/ 207 | } 208 | 209 | void markTopBoundary( BYTE* Buffer, int width, int height ) 210 | { 211 | int y,x; 212 | long newpos = 0; 213 | 214 | y=0; //top bounadary 215 | for ( x= 0; x < 3 * width; x+=3 ){ 216 | newpos = y * 3 * width + x; 217 | //mark only if its empty 218 | if(isBoundary(Buffer,newpos)){ 219 | Buffer[newpos] = 255; //red 220 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 221 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 222 | } 223 | } 224 | } 225 | 226 | 227 | void getLargestTopBoundarySegment(BYTE* Buffer, int width, int height, int *xBegin, int *xEnd, int *maxlen) 228 | { 229 | int y,x, xBeginTemp=-1, xEndTemp=-1, len = 0; 230 | *maxlen=0; 231 | long newpos = 0; 232 | 233 | y=0;//top bounadary 234 | for ( x= 0; x < 3 * width; x+=3 ){ 235 | newpos = y * 3 * width + x; 236 | 237 | if(isBoundary(Buffer,newpos)){ 238 | if(len==0){ 239 | xBeginTemp = x; 240 | xEndTemp = x; 241 | } 242 | else{ 243 | xEndTemp = x; 244 | } 245 | len++; 246 | } 247 | else{ 248 | len = 0; 249 | } 250 | 251 | if(len>(*maxlen)){ 252 | *maxlen = len; 253 | *xBegin = xBeginTemp; 254 | *xEnd = xEndTemp; 255 | } 256 | } 257 | 258 | if(xBeginTemp==-1){ 259 | return; 260 | } 261 | //paint it 262 | /*for ( x= *xBegin; x <= *xEnd; x+=3 ){ 263 | newpos = y * 3 * width + x; 264 | //mark only if its empty 265 | if(isBoundary(Buffer,newpos)){ 266 | Buffer[newpos] = 255; //red 267 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 268 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 269 | } 270 | }*/ 271 | } 272 | 273 | 274 | void markBottomBoundary( BYTE* Buffer, int width, int height ) 275 | { 276 | int y,x; 277 | long newpos = 0; 278 | 279 | y=height-1; //bottom bounadary 280 | for ( x= 0; x < 3 * width; x+=3 ){ 281 | newpos = y * 3 * width + x; 282 | //mark only if its empty 283 | if(isBoundary(Buffer,newpos)){ 284 | Buffer[newpos] = 255; //red 285 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 286 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 287 | } 288 | } 289 | } 290 | 291 | 292 | void getLargestBottomBoundarySegment(BYTE* Buffer, int width, int height, int *xBegin, int *xEnd, int *maxlen) 293 | { 294 | int y,x, xBeginTemp=-1, xEndTemp=-1, len = 0; 295 | *maxlen=0; 296 | long newpos = 0; 297 | 298 | y=height-1;//top bounadary 299 | for ( x= 0; x < 3 * width; x+=3 ){ 300 | newpos = y * 3 * width + x; 301 | 302 | if(isBoundary(Buffer,newpos)){ 303 | if(len==0){ 304 | xBeginTemp = x; 305 | xEndTemp = x; 306 | } 307 | else{ 308 | xEndTemp = x; 309 | } 310 | len++; 311 | } 312 | else{ 313 | len = 0; 314 | } 315 | 316 | if(len>(*maxlen)){ 317 | *maxlen = len; 318 | *xBegin = xBeginTemp; 319 | *xEnd = xEndTemp; 320 | } 321 | } 322 | 323 | if(xBeginTemp==-1){ 324 | return; 325 | } 326 | //paint it 327 | /* for ( x= *xBegin; x <= *xEnd; x+=3 ){ 328 | newpos = y * 3 * width + x; 329 | //mark only if its empty 330 | if(isBoundary(Buffer,newpos)){ 331 | Buffer[newpos] = 255; //red 332 | Buffer[newpos + 1] = Buffer[newpos+1]*0; //green 333 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 334 | } 335 | } */ 336 | } 337 | 338 | 339 | void markSectionBoundary( BYTE* Buffer, int xBegin, int xEnd, int yBegin, int yEnd, int tWidth ) 340 | { int y,x; 341 | long newpos = 0; 342 | for ( y = yBegin; y <= yEnd; y++ ) 343 | for ( x= xBegin; x <= xEnd; x+=3 ) 344 | { 345 | newpos = y * 3 * tWidth + x; 346 | 347 | Buffer[newpos] = 0; //red 348 | Buffer[newpos + 1] = 255; //green 349 | Buffer[newpos + 2] = Buffer[newpos+2]*0; //blue 350 | } 351 | } 352 | 353 | void markSection( BYTE* Buffer, int xBegin, int xEnd, int yBegin, int yEnd, int tWidth ) 354 | { 355 | markSectionBoundary( Buffer,xBegin,xEnd,yBegin,yBegin,tWidth ); 356 | markSectionBoundary( Buffer,xBegin,xEnd,yEnd,yEnd,tWidth ); 357 | markSectionBoundary( Buffer,xBegin,xBegin,yBegin,yEnd,tWidth ); 358 | markSectionBoundary( Buffer,xEnd,xEnd,yBegin,yEnd,tWidth ); 359 | 360 | } 361 | void markAllBoundaries( BYTE* Buffer, int width, int height ) 362 | { 363 | markTopBoundary(Buffer, width, height ); 364 | markBottomBoundary(Buffer, width, height ); 365 | markLeftBoundary(Buffer, width, height ); 366 | markRightBoundary(Buffer, width, height ); 367 | } 368 | 369 | void markSegmentforLargestBoundary( BYTE* Buffer, int width, int height ) 370 | { 371 | int dBegin=-1, dEnd=-1, yBegin=-1, yEnd=-1, xBegin=-1, xEnd=-1, maxlen=0,len=0; 372 | 373 | //left 374 | getLargestLeftBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len); 375 | if (len>maxlen){ 376 | yBegin = dBegin; 377 | yEnd = dEnd; 378 | maxlen = len; 379 | xBegin = 0; 380 | xEnd = 3*width - 3; 381 | } 382 | 383 | //right 384 | dBegin=-1; 385 | dEnd=-1; 386 | getLargestRightBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len ); 387 | if (len>maxlen){ 388 | yBegin = dBegin; 389 | yEnd = dEnd; 390 | maxlen = len; 391 | xBegin = 0; 392 | xEnd = 3*width - 3; 393 | } 394 | //markSection(Buffer,xBegin,xEnd,yBegin,yEnd,width); 395 | 396 | //top 397 | dBegin=-1; 398 | dEnd=-1; 399 | getLargestTopBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len ); 400 | if (len>maxlen){ 401 | xBegin = dBegin; 402 | xEnd = dEnd; 403 | maxlen = len; 404 | yBegin = 0; 405 | yEnd = height-1; 406 | } 407 | 408 | //bottom 409 | dBegin=-1; 410 | dEnd=-1; 411 | getLargestBottomBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len ); 412 | if (len>maxlen){ 413 | xBegin = dBegin; 414 | xEnd = dEnd; 415 | maxlen = len; 416 | yBegin = 0; 417 | yEnd = height-1; 418 | } 419 | 420 | //markAllBoundaries(Buffer, width, height ); 421 | markSection(Buffer,xBegin,xEnd,yBegin,yEnd,width); 422 | 423 | } 424 | 425 | void getSegmentforLargestBoundary( BYTE* Buffer, int width, int height, int *xStart, int *xFin, int *yStart, int *yFin, int *tLen, int *dir ) 426 | { 427 | int dBegin=-1, dEnd=-1, yBegin=-1, yEnd=-1, xBegin=-1, xEnd=-1, maxlen=0,len=0; 428 | *tLen = 0 ; 429 | 430 | //left 431 | getLargestLeftBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len); 432 | if (len>maxlen){ 433 | yBegin = dBegin; 434 | yEnd = dEnd; 435 | maxlen = len; 436 | xBegin = 0; 437 | xEnd = 3*width - 3; 438 | *dir = 2; 439 | } 440 | 441 | //right 442 | dBegin=-1; 443 | dEnd=-1; 444 | getLargestRightBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len ); 445 | if (len>maxlen){ 446 | yBegin = dBegin; 447 | yEnd = dEnd; 448 | maxlen = len; 449 | xBegin = 0; 450 | xEnd = 3*width - 3; 451 | *dir = 4; 452 | } 453 | //markSection(Buffer,xBegin,xEnd,yBegin,yEnd,width); 454 | 455 | //top 456 | dBegin=-1; 457 | dEnd=-1; 458 | getLargestTopBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len ); 459 | if (len>maxlen){ 460 | xBegin = dBegin; 461 | xEnd = dEnd; 462 | maxlen = len; 463 | yBegin = 0; 464 | yEnd = height-1; 465 | *dir = 1; 466 | } 467 | 468 | //bottom 469 | dBegin=-1; 470 | dEnd=-1; 471 | getLargestBottomBoundarySegment(Buffer, width, height, &dBegin, &dEnd, &len ); 472 | if (len>maxlen){ 473 | xBegin = dBegin; 474 | xEnd = dEnd; 475 | maxlen = len; 476 | yBegin = 0; 477 | yEnd = height-1; 478 | *dir = 3; 479 | } 480 | 481 | //markAllBoundaries(Buffer, width, height ); 482 | //markSection(Buffer,xBegin,xEnd,yBegin,yEnd,width); 483 | *xStart = xBegin; 484 | *xFin = xEnd; 485 | *yStart = yBegin; 486 | *yFin = yEnd; 487 | *tLen = maxlen; 488 | 489 | } 490 | 491 | 492 | 493 | 494 | void removeExtraLeftBoundary( BYTE* Buffer, int *width, int *height ) 495 | { 496 | int y,x, w=*width, h=*height; 497 | long newpos, newpos1; 498 | for ( y = 0; y < h; y++ ) 499 | for ( x= 0; x < 3 * w - 3; x+=3 ) 500 | { 501 | newpos = y * 3 * w + x; 502 | newpos1= y * 3 * w + x +3; 503 | Buffer[newpos] = Buffer[newpos1]; //red 504 | Buffer[newpos + 1] = Buffer[newpos1+1]; //green 505 | Buffer[newpos + 2] = Buffer[newpos1+2]; //blue 506 | } 507 | //*width = w-1; 508 | } 509 | 510 | void removeExtraTopBoundary( BYTE* Buffer, int *width, int *height ) 511 | { 512 | int y,x, w=*width, h=*height; 513 | long newpos, newpos1; 514 | for ( y = 0; y < h - 1; y++ ) 515 | for ( x= 0; x < 3 * w ; x+=3 ) 516 | { 517 | newpos = y * 3 * w + x; 518 | newpos1= (y+1) * 3 * w + x; 519 | Buffer[newpos] = Buffer[newpos1]; //red 520 | Buffer[newpos + 1] = Buffer[newpos1+1]; //green 521 | Buffer[newpos + 2] = Buffer[newpos1+2]; //blue 522 | } 523 | //*height = h - 1; 524 | } 525 | 526 | 527 | void isLargestRightBoundarySegment(BYTE* Buffer, int width, int height, int *yBegin, int *yEnd, int *maxlen, int checkAt) 528 | { 529 | int y, yBeginTemp=-1, yEndTemp=-1, len = 0; 530 | *maxlen=0; 531 | long newpos = 0; 532 | 533 | 534 | for ( y = 0; y < height; y++ ){ 535 | newpos = y * 3 * width + 3*checkAt; 536 | 537 | if(isBoundary(Buffer,newpos)){ 538 | if(len==0){ 539 | yBeginTemp = y; 540 | yEndTemp = y; 541 | } 542 | else{ 543 | yEndTemp = y; 544 | } 545 | len++; 546 | } 547 | else{ 548 | len = 0; 549 | } 550 | 551 | if(len>(*maxlen)){ 552 | *maxlen = len; 553 | *yBegin = yBeginTemp; 554 | *yEnd = yEndTemp; 555 | } 556 | } 557 | } 558 | 559 | void removeExtraBoundaries( BYTE* Buffer, int *width, int *height ) 560 | { 561 | int dBegin=-1, dEnd=-1, len=0, h=*height, w=*width, i, j; 562 | 563 | //take it to top 564 | for(i=0; i0; i--) 594 | { 595 | getLargestBottomBoundarySegment(Buffer, *width, i, &dBegin, &dEnd, &len); 596 | //printf("got len = %d \n", len); 597 | if(len0; j--) 608 | { 609 | isLargestRightBoundarySegment(Buffer, *width, *height, &dBegin, &dEnd, &len, j); 610 | //printf("got len = %d \n", len); 611 | if (len=xBegin;x-=3) 670 | { 671 | newpos = y * 3 * width + x; 672 | //printf("x= %d, y=%d\n",x,y); 673 | if(isBoundary(Buffer,newpos)) 674 | { 675 | newbuf[newpos] = 255; 676 | newbuf[newpos+1] = 255; 677 | newbuf[newpos+2] = 255; 678 | } 679 | } 680 | yEnd--; 681 | //left 682 | x=xBegin; 683 | for(y=yEnd-1;y>=yBegin;y--) 684 | { 685 | newpos = y * 3 * width + x; 686 | //printf("x= %d, y=%d\n",x,y); 687 | if(isBoundary(Buffer,newpos)) 688 | { 689 | newbuf[newpos] = 255; 690 | newbuf[newpos+1] = 255; 691 | newbuf[newpos+2] = 255; 692 | } 693 | } 694 | xBegin+=3; 695 | } 696 | 697 | BufCopy(newbuf1,newbuf,width,height); 698 | BufCopy(newbuf2,newbuf,width,height); 699 | for ( y = 1; y < height-1; y++ ) 700 | for ( x= 3; x < 3 * width-3; x+=3 ) 701 | { 702 | newpos = y * 3 * width + x; 703 | long newpos_10 = (y-1) * 3 * width + x; 704 | long newpos0_1 = y* 3 * width + x-3; 705 | long newpos10 = (y+1) * 3 * width + x; 706 | long newpos01 = y* 3 * width + x+3; 707 | 708 | if(isBoundary(newbuf1,newpos)) 709 | { 710 | int val = (isBoundary(newbuf1,newpos_10)?1:0)+(isBoundary(newbuf1,newpos0_1)?1:0)+(isBoundary(newbuf1,newpos10)?1:0)+(isBoundary(newbuf1,newpos01)?1:0); 711 | if(val<=2) 712 | { 713 | newbuf1[newpos] = 0; 714 | newbuf1[newpos+1] = 0; 715 | newbuf1[newpos+2] = 0; 716 | } 717 | } 718 | } 719 | 720 | for ( y = height-2; y > 0 ; y-- ) 721 | for ( x= 3 * width-6; x >0 ; x-=3 ) 722 | { 723 | newpos = y * 3 * width + x; 724 | long newpos_10 = (y-1) * 3 * width + x; 725 | long newpos0_1 = y* 3 * width + x-3; 726 | long newpos10 = (y+1) * 3 * width + x; 727 | long newpos01 = y* 3 * width + x+3; 728 | 729 | if(isBoundary(newbuf2,newpos)) 730 | { 731 | int val = (isBoundary(newbuf2,newpos_10)?1:0)+(isBoundary(newbuf2,newpos0_1)?1:0)+(isBoundary(newbuf2,newpos10)?1:0)+(isBoundary(newbuf2,newpos01)?1:0); 732 | if(val<=2) 733 | { 734 | newbuf2[newpos] = 0; 735 | newbuf2[newpos+1] = 0; 736 | newbuf2[newpos+2] = 0; 737 | } 738 | } 739 | } 740 | 741 | BufAvg(newbuf,newbuf1,newbuf2,width,height); 742 | delete [] newbuf1; 743 | delete [] newbuf2; 744 | 745 | for ( y = 1; y < height-1; y++ ) 746 | for ( x= 3; x < 3 * width-3; x+=3 ) 747 | { 748 | newpos = y * 3 * width + x; 749 | if (!(newbuf[newpos] == 0 && newbuf[newpos+1] == 0 && newbuf[newpos+2] == 0)) 750 | { 751 | newbuf[newpos] = 255; 752 | newbuf[newpos+1] = 255; 753 | newbuf[newpos+2] = 255; 754 | } 755 | } 756 | 757 | 758 | //return newbuf; 759 | } 760 | -------------------------------------------------------------------------------- /displace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "CGProject.h" 4 | 5 | void displaceAbove(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 6 | { 7 | int x,y; 8 | long newpos,newpos_nextline; 9 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 10 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 11 | 12 | for ( y = 0; y < height-1; y++ ) 13 | for ( x= 0; x < 3 * width; x+=3 ) 14 | { 15 | newpos = y * 3 * width + x; 16 | newpos_nextline = newpos + 3 * width; 17 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 18 | { 19 | dI[newpos] = dI[newpos_nextline]; 20 | dI[newpos+1] = dI[newpos_nextline+1]; 21 | dI[newpos+2] = dI[newpos_nextline+2]; 22 | 23 | Buffer[newpos] = Buffer[newpos_nextline]; 24 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 25 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 26 | } 27 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 28 | { 29 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 30 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 31 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 32 | 33 | Buffer[newpos] = randr; 34 | Buffer[newpos+1] = randg; 35 | Buffer[newpos+2] = randb; 36 | 37 | 38 | } 39 | } 40 | } 41 | 42 | 43 | void displaceBelow(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 44 | { 45 | int x,y; 46 | long newpos,newpos_nextline; 47 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 48 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 49 | 50 | for ( y = height-1; y>0 ; y-- ) 51 | for ( x= 0; x < 3 * width; x+=3 ) 52 | { 53 | newpos = y * 3 * width + x; 54 | newpos_nextline = newpos - 3 * width; 55 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 56 | { 57 | dI[newpos] = dI[newpos_nextline]; 58 | dI[newpos+1] = dI[newpos_nextline+1]; 59 | dI[newpos+2] = dI[newpos_nextline+2]; 60 | 61 | Buffer[newpos] = Buffer[newpos_nextline]; 62 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 63 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 64 | } 65 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 66 | { 67 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 68 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 69 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 70 | 71 | Buffer[newpos] = randr; 72 | Buffer[newpos+1] = randg; 73 | Buffer[newpos+2] = randb; 74 | } 75 | } 76 | } 77 | 78 | 79 | 80 | void displaceLeft(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 81 | { 82 | int x,y; 83 | long newpos,newpos_nextline; 84 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 85 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 86 | 87 | for ( y = 0; y < height; y++ ) 88 | for ( x= 0; x < 3 * width-3; x+=3 ) 89 | { 90 | newpos = y * 3 * width + x; 91 | newpos_nextline = newpos + 3; 92 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 93 | { 94 | dI[newpos] = dI[newpos_nextline]; 95 | dI[newpos+1] = dI[newpos_nextline+1]; 96 | dI[newpos+2] = dI[newpos_nextline+2]; 97 | 98 | Buffer[newpos] = Buffer[newpos_nextline]; 99 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 100 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 101 | } 102 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 103 | { 104 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 105 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 106 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 107 | 108 | Buffer[newpos] = randr; 109 | Buffer[newpos+1] = randg; 110 | Buffer[newpos+2] = randb; 111 | } 112 | } 113 | } 114 | 115 | 116 | void displaceRight(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer) 117 | { 118 | int x,y; 119 | long newpos,newpos_nextline; 120 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 121 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 122 | 123 | for ( y = 0; y < height; y++ ) 124 | for ( x= 3 * width-3; x > 0 ; x-=3 ) 125 | { 126 | newpos = y * 3 * width + x; 127 | newpos_nextline = newpos - 3; 128 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 129 | { 130 | dI[newpos] = dI[newpos_nextline]; 131 | dI[newpos+1] = dI[newpos_nextline+1]; 132 | dI[newpos+2] = dI[newpos_nextline+2]; 133 | 134 | Buffer[newpos] = Buffer[newpos_nextline]; 135 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 136 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 137 | } 138 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 139 | { 140 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 141 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 142 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 143 | 144 | Buffer[newpos] = randr; 145 | Buffer[newpos+1] = randg; 146 | Buffer[newpos+2] = randb; 147 | } 148 | } 149 | } 150 | 151 | -------------------------------------------------------------------------------- /displace1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "CGProject.h" 4 | 5 | void displaceAbove1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Vdisp, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* SeamMask) 6 | { 7 | int x,y; 8 | long newpos,newpos_nextline; 9 | //bool flag = true; 10 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 11 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 12 | 13 | //for ( y = 0; y < height-1 ; y++ ) 14 | for ( y = yBegin; y <= yEnd ; y++ ) 15 | for ( x= 0; x < 3 * width; x+=3 ) 16 | //for ( x= xBegin; x <= xEnd; x+=3 ) 17 | { 18 | //flag = false; 19 | newpos = y * 3 * width + x; 20 | //randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 21 | newpos_nextline = newpos + 3 * width; 22 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 23 | { 24 | //flag = true; 25 | dI[newpos] = dI[newpos_nextline]; 26 | dI[newpos+1] = dI[newpos_nextline+1]; 27 | dI[newpos+2] = dI[newpos_nextline+2]; 28 | 29 | Buffer[newpos] = Buffer[newpos_nextline]; 30 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 31 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 32 | 33 | Vdisp[newpos] += 1; 34 | Vdisp[newpos+1] += 1; 35 | Vdisp[newpos+2] += 1; 36 | } 37 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 38 | { 39 | //flag = true; 40 | 41 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 42 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 43 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 44 | 45 | Buffer[newpos] =randr; 46 | Buffer[newpos+1] = randg; 47 | Buffer[newpos+2] = randb; 48 | 49 | } 50 | } 51 | } 52 | 53 | 54 | void displaceBelow1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Vdisp, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* SeamMask) 55 | { 56 | int x,y; 57 | long newpos,newpos_nextline; 58 | //bool flag = true; 59 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 60 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 61 | 62 | for ( y = height-1; y>0 ; y-- ) 63 | for ( x= 0; x < 3 * width; x+=3 ) 64 | //for ( x= xBegin; x <= xEnd; x+=3 ) 65 | { 66 | //flag = false; 67 | newpos = y * 3 * width + x; 68 | newpos_nextline = newpos - 3 * width; 69 | //randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 70 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 71 | { 72 | //flag=true; 73 | dI[newpos] = dI[newpos_nextline]; 74 | dI[newpos+1] = dI[newpos_nextline+1]; 75 | dI[newpos+2] = dI[newpos_nextline+2]; 76 | 77 | Buffer[newpos] = Buffer[newpos_nextline]; 78 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 79 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 80 | 81 | Vdisp[newpos] -= 1; 82 | Vdisp[newpos+1] -= 1; 83 | Vdisp[newpos+2] -= 1; 84 | } 85 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 86 | { 87 | //flag=true; 88 | 89 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 90 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 91 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 92 | 93 | Buffer[newpos] =randr; 94 | Buffer[newpos+1] = randg; 95 | Buffer[newpos+2] = randb; 96 | 97 | } 98 | } 99 | } 100 | 101 | 102 | 103 | void displaceLeft1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Hdisp, BYTE* SeamMask) 104 | { 105 | int x,y; 106 | long newpos,newpos_nextline; 107 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 108 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 109 | 110 | for ( y = 0; y < height; y++ ) 111 | for ( x= 0; x < 3 * width-3; x+=3 ) 112 | { 113 | newpos = y * 3 * width + x; 114 | newpos_nextline = newpos + 3; 115 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 116 | { 117 | dI[newpos] = dI[newpos_nextline]; 118 | dI[newpos+1] = dI[newpos_nextline+1]; 119 | dI[newpos+2] = dI[newpos_nextline+2]; 120 | 121 | Buffer[newpos] = Buffer[newpos_nextline]; 122 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 123 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 124 | 125 | Hdisp[newpos] += 3; 126 | Hdisp[newpos+1] += 3; 127 | Hdisp[newpos+2] += 3; 128 | } 129 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 130 | { 131 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 132 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 133 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 134 | 135 | Buffer[newpos] = randr; 136 | Buffer[newpos+1] = randg; 137 | Buffer[newpos+2] = randb; 138 | 139 | SeamMask[newpos] = 255; 140 | SeamMask[newpos+1] = 255; 141 | SeamMask[newpos+2] = 255; 142 | } 143 | } 144 | } 145 | 146 | 147 | void displaceRight1(BYTE* dI,BYTE* D, int width, int height, BYTE* Buffer, int* Hdisp, BYTE* SeamMask) 148 | { 149 | int x,y; 150 | long newpos,newpos_nextline; 151 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 152 | int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 153 | 154 | for ( y = 0; y < height; y++ ) 155 | for ( x= 3 * width-3; x > 0 ; x-=3 ) 156 | { 157 | newpos = y * 3 * width + x; 158 | newpos_nextline = newpos - 3; 159 | if((D[newpos]==255 && D[newpos+1]==255 && D[newpos+2]==255)) 160 | { 161 | dI[newpos] = dI[newpos_nextline]; 162 | dI[newpos+1] = dI[newpos_nextline+1]; 163 | dI[newpos+2] = dI[newpos_nextline+2]; 164 | 165 | Buffer[newpos] = Buffer[newpos_nextline]; 166 | Buffer[newpos+1] = Buffer[newpos_nextline+1]; 167 | Buffer[newpos+2] = Buffer[newpos_nextline+2]; 168 | 169 | 170 | Hdisp[newpos] -= 3; 171 | Hdisp[newpos+1] -= 3; 172 | Hdisp[newpos+2] -= 3; 173 | } 174 | else if((D[newpos]==128 && D[newpos+1]==128 && D[newpos+2]==128)&& x%2) 175 | { 176 | dI[newpos] = randg;//(dI[newpos_nextline]);//+dI[newpos])/2; 177 | dI[newpos+1] = randg;//(dI[newpos_nextline+1]);//+dI[newpos+1])/2; 178 | dI[newpos+2] = randg;//(dI[newpos_nextline+2]);//+dI[newpos+2])/2; 179 | 180 | Buffer[newpos] = randr; 181 | Buffer[newpos+1] = randg; 182 | Buffer[newpos+2] = randb; 183 | 184 | SeamMask[newpos] = 255; 185 | SeamMask[newpos+1] = 255; 186 | SeamMask[newpos+2] = 255; 187 | } 188 | } 189 | } 190 | 191 | 192 | 193 | void displaceReal(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* D) 194 | { 195 | int x,y; 196 | long newpos,newpos_nextline; 197 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 198 | //int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 199 | int ynew,xnew; 200 | //printf("width = %d, height = %d\n", width, height); 201 | for ( y = height-1; y>0 ; y-- ) 202 | for ( x= 0; x < 3 * width; x+=3 ) 203 | { 204 | newpos = y * 3 * width + x; 205 | //printf("width = %d, height = %d\n", width, height); 206 | //printf("for newpos x = %d, y = %d \n", x, y); 207 | ynew = (y+Vdisp[newpos])>=height?height-1:(y+Vdisp[newpos]); 208 | //printf("for newpos_newline x = %d, y = %d \n", (x + Hdisp[newpos]), ynew); 209 | newpos_nextline = ynew * 3 * width + x + Hdisp[newpos];//newpos + 3 * width * Vdisp[newpos]; 210 | 211 | D[newpos] = Buffer[newpos_nextline]; 212 | D[newpos+1] = Buffer[newpos_nextline+1]; 213 | D[newpos+2] = Buffer[newpos_nextline+2]; 214 | } 215 | } 216 | 217 | 218 | void displaceBack(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* D) 219 | { 220 | int x,y; 221 | long newpos,newpos_nextline; 222 | //int randg = rand()%220 + 20, rands = 10-rand()%20; 223 | //int randg = 64 + rand()%128, randr = 64 + rand()%128, randb = 64 + rand()%128; 224 | int ynew,xnew; 225 | //printf("width = %d, height = %d\n", width, height); 226 | for ( y = height-1; y>0 ; y-- ) 227 | for ( x= 0; x < 3 * width; x+=3 ) 228 | { 229 | newpos_nextline = y * 3 * width + x; 230 | //printf("width = %d, height = %d\n", width, height); 231 | //printf("for newpos x = %d, y = %d \n", x, y); 232 | ynew = (y+Vdisp[newpos])>=height?height-1:(y+Vdisp[newpos]); 233 | //printf("for newpos_newline x = %d, y = %d \n", (x + Hdisp[newpos]), ynew); 234 | newpos = ynew * 3 * width + x + Hdisp[newpos];//newpos + 3 * width * Vdisp[newpos]; 235 | 236 | D[newpos] = (D[newpos] + Buffer[newpos_nextline]); 237 | D[newpos+1] = (D[newpos+1] + Buffer[newpos_nextline+1]); 238 | D[newpos+2] = (D[newpos+2] + Buffer[newpos_nextline+2]); 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /image_representations.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "CGProject.h" 5 | 6 | void getGreyScale( BYTE* Buffer, int width, int height, BYTE* newbuf ) 7 | { 8 | int x,y; 9 | long newsize = 3 * (width) * (height), newpos = 0; 10 | //BYTE* newbuf = new BYTE[newsize]; 11 | BYTE I; 12 | //0.2989, 0.5870, 0.1140 13 | 14 | for ( y = 0; y < height; y++ ) 15 | for ( x= 0; x < 3 * width; x+=3 ) 16 | { 17 | newpos = y * 3 * width + x; 18 | I= 0.2989*Buffer[newpos] + 0.5870*Buffer[newpos+1] + 0.1140*Buffer[newpos+2]; 19 | 20 | newbuf[newpos] = I; //red 21 | newbuf[newpos + 1] = I; //green 22 | newbuf[newpos + 2] = I; //blue 23 | } 24 | //return newbuf; 25 | } 26 | 27 | 28 | 29 | 30 | BYTE* getYGradient( BYTE* I, int width, int height ) 31 | { 32 | int x,y; 33 | long newsize = 3 * (width) * (height), newpos,newpos_b,newpos_a; 34 | BYTE* newbuf = new BYTE[newsize]; 35 | BYTE I_b, I_a, dIy; 36 | //0.2989, 0.5870, 0.1140 37 | 38 | for ( y = 0; y < height; y++ ) 39 | for ( x= 0; x < 3 * width; x+=3 ) 40 | { 41 | 42 | newpos = y * 3 * width + x; 43 | newpos_b = (y-1) * 3 * width + x; 44 | newpos_a = (y+1) * 3 * width + x; 45 | //I= 0.2989*Buffer[newpos] + 0.5870*Buffer[newpos+1] + 0.1140*Buffer[newpos+2]; 46 | 47 | //get Ib 48 | if (y==0){ 49 | I_b = 0; 50 | } 51 | else{ 52 | I_b = I[newpos_b]; 53 | } 54 | 55 | //get Ib 56 | if (y==height-1){ 57 | I_a = 0; 58 | } 59 | else{ 60 | I_a = I[newpos_a]; 61 | } 62 | 63 | dIy = abs((I_a-I_b)); 64 | newbuf[newpos] = dIy; //red 65 | newbuf[newpos + 1] = dIy; //green 66 | newbuf[newpos + 2] = dIy; //blue 67 | } 68 | return newbuf; 69 | } 70 | 71 | 72 | 73 | 74 | BYTE* getYGradientColor( BYTE* I, int width, int height ) 75 | { 76 | int x,y; 77 | long newsize = 3 * (width) * (height), newpos,newpos_b,newpos_a; 78 | BYTE* newbuf = new BYTE[newsize]; 79 | BYTE I_br, I_bg, I_bb, I_ar, I_ag, I_ab, dIy; 80 | //0.2989, 0.5870, 0.1140 81 | 82 | for ( y = 0; y < height; y++ ) 83 | for ( x= 0; x < 3 * width; x+=3 ) 84 | { 85 | 86 | newpos = y * 3 * width + x; 87 | newpos_b = (y-1) * 3 * width + x; 88 | newpos_a = (y+1) * 3 * width + x; 89 | //I= 0.2989*Buffer[newpos] + 0.5870*Buffer[newpos+1] + 0.1140*Buffer[newpos+2]; 90 | 91 | //get Ib 92 | if (y==0){ 93 | I_br = 0; 94 | I_bg = 0; 95 | I_bb = 0; 96 | } 97 | else{ 98 | I_br = I[newpos_b]; 99 | I_bg = I[newpos_b+1]; 100 | I_bb = I[newpos_b+2]; 101 | } 102 | 103 | //get Ib 104 | if (y==height-1){ 105 | I_ar = 0; 106 | I_ag = 0; 107 | I_ab = 0; 108 | } 109 | else{ 110 | I_ar = I[newpos_a]; 111 | I_ag = I[newpos_a+1]; 112 | I_ab = I[newpos_a+2]; 113 | } 114 | 115 | I_ar = abs((I_ar-I_br)); 116 | I_ag = abs((I_ag-I_bg)); 117 | I_ab = abs((I_ab-I_bb)); 118 | 119 | dIy = I_ar/3 + I_ag/3 + I_ab/3 ; 120 | newbuf[newpos] = dIy; //red 121 | newbuf[newpos + 1] = dIy; //green 122 | newbuf[newpos + 2] = dIy; //blue 123 | } 124 | return newbuf; 125 | } 126 | 127 | 128 | 129 | BYTE* getXGradient( BYTE* I, int width, int height ) 130 | { 131 | int x,y; 132 | long newsize = 3 * (width) * (height), newpos,newpos_b,newpos_a; 133 | BYTE* newbuf = new BYTE[newsize]; 134 | BYTE I_b, I_a, dIx; 135 | //0.2989, 0.5870, 0.1140 136 | 137 | for ( y = 0; y < height; y++ ) 138 | for ( x= 0; x < 3 * width; x+=3 ) 139 | { 140 | 141 | newpos = y * 3 * width + x; 142 | newpos_b = y * 3 * width + x-3; 143 | newpos_a = y * 3 * width + x+3; 144 | //I= 0.2989*Buffer[newpos] + 0.5870*Buffer[newpos+1] + 0.1140*Buffer[newpos+2]; 145 | 146 | //get Ib 147 | if (x==0){ 148 | I_b = 0; 149 | } 150 | else{ 151 | I_b = I[newpos_b]; 152 | } 153 | 154 | //get Ib 155 | if (x==width-3){ 156 | I_a = 0; 157 | } 158 | else{ 159 | I_a = I[newpos_a]; 160 | } 161 | 162 | dIx = abs((I_a-I_b)) ; 163 | 164 | newbuf[newpos] = dIx; //red 165 | newbuf[newpos + 1] = dIx; //green 166 | newbuf[newpos + 2] = dIx; //blue 167 | } 168 | return newbuf; 169 | } 170 | 171 | 172 | BYTE* getXGradientColor( BYTE* I, int width, int height ) 173 | { 174 | int x,y; 175 | long newsize = 3 * (width) * (height), newpos,newpos_b,newpos_a; 176 | BYTE* newbuf = new BYTE[newsize]; 177 | BYTE I_br, I_bg, I_bb, I_ar, I_ag, I_ab, dIx; 178 | //0.2989, 0.5870, 0.1140 179 | 180 | for ( y = 0; y < height; y++ ) 181 | for ( x= 0; x < 3 * width; x+=3 ) 182 | { 183 | 184 | newpos = y * 3 * width + x; 185 | newpos_b = y * 3 * width + x-3; 186 | newpos_a = y * 3 * width + x+3; 187 | //I= 0.2989*Buffer[newpos] + 0.5870*Buffer[newpos+1] + 0.1140*Buffer[newpos+2]; 188 | 189 | //get Ib 190 | if (x==0){ 191 | I_br = 0; 192 | I_bg = 0; 193 | I_bb = 0; 194 | } 195 | else{ 196 | I_br = I[newpos_b]; 197 | I_bg = I[newpos_b+1]; 198 | I_bb = I[newpos_b+2]; 199 | } 200 | 201 | //get Ib 202 | if (x==width-3){ 203 | I_ar = 0; 204 | I_ag = 0; 205 | I_ab = 0; 206 | } 207 | else{ 208 | I_ar = I[newpos_a]; 209 | I_ag = I[newpos_a+1]; 210 | I_ab = I[newpos_a+2]; 211 | } 212 | 213 | I_ar = abs((I_ar-I_br)); 214 | I_ag = abs((I_ag-I_bg)); 215 | I_ab = abs((I_ab-I_bb)); 216 | 217 | dIx = I_ar/3 + I_ag/3 + I_ab/3 ; 218 | 219 | newbuf[newpos] = dIx; //red 220 | newbuf[newpos + 1] = dIx; //green 221 | newbuf[newpos + 2] = dIx; //blue 222 | } 223 | return newbuf; 224 | } 225 | 226 | 227 | void getGradient( BYTE* I, int width, int height, BYTE* newbuf ) 228 | { 229 | long newsize = 3 * (width) * (height), newpos; 230 | BYTE* dIx = getXGradient(I, width, height); 231 | BYTE* dIy = getYGradient(I, width, height); 232 | //BYTE* newbuf = new BYTE[newsize]; 233 | 234 | BYTE dI; 235 | int y,x; 236 | for ( y = 0; y < height; y++ ) 237 | for ( x= 0; x < 3 * width; x+=3 ) 238 | { 239 | newpos = y * 3 * width + x; 240 | dI = (dIx[newpos] + dIy[newpos])/2; 241 | 242 | newbuf[newpos] = dI; //red 243 | newbuf[newpos + 1] = dI; //green 244 | newbuf[newpos + 2] = dI; //blue 245 | } 246 | delete [] dIx; 247 | delete [] dIy; 248 | //return newbuf; 249 | } 250 | 251 | 252 | void saveAsImage(BYTE* Buffer, int width, int height, char* name){ 253 | long s2; 254 | BYTE* c = ConvertRGBToBMPBuffer ( Buffer, width, height, &s2 ); 255 | SaveBMP ( c, width, height, s2, name ); 256 | delete [] c; 257 | 258 | 259 | } 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | /* 269 | BYTE* getIntensity( BYTE* Buffer, int width, int height ) 270 | { 271 | int x,y; 272 | long newsize = (width) * (height), newpos, buffpos; 273 | BYTE* newbuf = new BYTE[newsize]; 274 | BYTE I; 275 | 276 | //0.2989, 0.5870, 0.1140 277 | 278 | for ( y = 0; y < height; y++ ) 279 | for ( x= 0; x < width; x++ ) 280 | { 281 | buffpos = y * 3 * width + x*3; 282 | newpos = y * width + x; 283 | I= 0.2989*Buffer[buffpos] + 0.5870*Buffer[buffpos+1] + 0.1140*Buffer[buffpos+2]; 284 | 285 | newbuf[newpos] = I; 286 | } 287 | return newbuf; 288 | } 289 | 290 | void printIntensity( BYTE* newbuff, int width, int height, BYTE* Buffer ) 291 | { 292 | int x,y; 293 | //long newsize = (width) * (height), 294 | long newpos, buffpos; 295 | BYTE I; 296 | 297 | //0.2989, 0.5870, 0.1140 298 | 299 | for ( y = 0; y < height; y++ ) 300 | for ( x= 0; x < width; x++ ) 301 | { 302 | buffpos = y * width + x; 303 | newpos = y * 3 * width + x*3; 304 | //I= 0.2989*Buffer[buffpos] + 0.5870*Buffer[buffpos+1] + 0.1140*Buffer[buffpos+2]; 305 | I = Buffer[buffpos]; 306 | 307 | newbuff[newpos] = I; 308 | newbuff[newpos+1] = I; 309 | newbuff[newpos+2] = I; 310 | } 311 | } 312 | */ 313 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | This peice of code has been created by Adheesh Gokhale for his project 3 | Rectangling the panorama for CSE528. It contains of two sections. First 4 | code developed by Adheesh. Second is code copied from 5 | http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html 6 | and has been marked so. 7 | *************************************************************************/ 8 | #include 9 | #include 10 | #include "CGProject.h" 11 | 12 | //#inlcude "basic_modify.c" 13 | 14 | //void doNothing1( BYTE* Buffer, int width, int height ); 15 | /* 16 | void doSomething( BYTE* Buffer, int *width, int *height ) 17 | { 18 | 19 | //removeRed(Buffer, width, height ); 20 | //markLargestBoundary(Buffer, *width, *height ); 21 | //removeExtraBoundaries(Buffer, width, height ); 22 | getGreyScale(Buffer, *width, *height ); 23 | }*/ 24 | 25 | 26 | //The code after this has been copied from http://www.runicsoft.com/bmp.cpp 27 | //Courtesy: http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html 28 | /************************************************************************* 29 | 30 | file created on: 2002/08/30 19:33 31 | filename: Bmp.cpp 32 | author: Andreas Hartl 33 | 34 | visit http://www.runicsoft.com for updates and more information 35 | 36 | purpose: functions to load raw bmp data, 37 | to save raw bmp data, 38 | to convert RGB data to raw bmp data, 39 | to convert raw bmp data to RGB data 40 | and to use the WinAPI to select 41 | a bitmap into a device context 42 | 43 | file updated on 2010/09/13 44 | in the 8 years since i first wrote this the windows file functions 45 | have changed their input from char* to LPCTSTR. 46 | Updated this in the code here 47 | 48 | **************************************************************************/ 49 | 50 | 51 | 52 | 53 | /******************************************************************* 54 | BYTE* ConvertRGBToBMPBuffer ( BYTE* Buffer, int width, 55 | int height, long* newsize ) 56 | 57 | 58 | This function takes as input an array of RGB values, it's width 59 | and height. 60 | The buffer gets then transformed to an array that can be used 61 | to write to a windows bitmap file. The size of the array 62 | is returned in newsize, the array itself is the 63 | return value of the function. 64 | Both input and output buffers must be deleted by the 65 | calling function. 66 | 67 | The input buffer is expected to consist of width * height 68 | RGB triplets. Thus the total size of the buffer is taken as 69 | width * height * 3. 70 | 71 | The function then transforms this buffer so that it can be written 72 | to a windows bitmap file: 73 | First the RGB triplets are converted to BGR. 74 | Then the buffer is swapped around since .bmps store 75 | images uside-down. 76 | Finally the buffer gets DWORD ( 32bit ) aligned, 77 | meaning that each scanline ( 3 * width bytes ) gets 78 | padded with 0x00 bytes up to the next DWORD boundary 79 | 80 | 81 | *******************************************************************/ 82 | 83 | BYTE* ConvertRGBToBMPBuffer ( BYTE* Buffer, int width, int height, long* newsize ) 84 | { 85 | 86 | // first make sure the parameters are valid 87 | if ( ( NULL == Buffer ) || ( width == 0 ) || ( height == 0 ) ) 88 | return NULL; 89 | 90 | // now we have to find with how many bytes 91 | // we have to pad for the next DWORD boundary 92 | 93 | int padding = 0; 94 | int scanlinebytes = width * 3; 95 | while ( ( scanlinebytes + padding ) % 4 != 0 ) // DWORD = 4 bytes 96 | padding++; 97 | // get the padded scanline width 98 | int psw = scanlinebytes + padding; 99 | 100 | // we can already store the size of the new padded buffer 101 | *newsize = height * psw; 102 | 103 | // and create new buffer 104 | BYTE* newbuf = new BYTE[*newsize]; 105 | 106 | // fill the buffer with zero bytes then we dont have to add 107 | // extra padding zero bytes later on 108 | memset ( newbuf, 0, *newsize ); 109 | 110 | // now we loop trough all bytes of the original buffer, 111 | // swap the R and B bytes and the scanlines 112 | long bufpos = 0; 113 | long newpos = 0; 114 | for ( int y = 0; y < height; y++ ) 115 | for ( int x = 0; x < 3 * width; x+=3 ) 116 | { 117 | bufpos = y * 3 * width + x; // position in original buffer 118 | newpos = ( height - y - 1 ) * psw + x; // position in padded buffer 119 | 120 | newbuf[newpos] = Buffer[bufpos+2]; // swap r and b 121 | newbuf[newpos + 1] = Buffer[bufpos + 1]; // g stays 122 | newbuf[newpos + 2] = Buffer[bufpos]; // swap b and r 123 | } 124 | 125 | return newbuf; 126 | } 127 | 128 | BYTE* ConvertRGBToBMPBuffer1 ( BYTE* Buffer, int width, int height, long* newsize, int oldWidth ) 129 | { 130 | 131 | // first make sure the parameters are valid 132 | if ( ( NULL == Buffer ) || ( width == 0 ) || ( height == 0 ) ) 133 | return NULL; 134 | 135 | // now we have to find with how many bytes 136 | // we have to pad for the next DWORD boundary 137 | 138 | int padding = 0; 139 | int scanlinebytes = width * 3; 140 | while ( ( scanlinebytes + padding ) % 4 != 0 ) // DWORD = 4 bytes 141 | padding++; 142 | // get the padded scanline width 143 | int psw = scanlinebytes + padding; 144 | 145 | // we can already store the size of the new padded buffer 146 | *newsize = height * psw; 147 | 148 | // and create new buffer 149 | BYTE* newbuf = new BYTE[*newsize]; 150 | 151 | // fill the buffer with zero bytes then we dont have to add 152 | // extra padding zero bytes later on 153 | memset ( newbuf, 0, *newsize ); 154 | 155 | // now we loop trough all bytes of the original buffer, 156 | // swap the R and B bytes and the scanlines 157 | long bufpos = 0; 158 | long newpos = 0; 159 | for ( int y = 0; y < height; y++ ) 160 | for ( int x = 0; x < 3 * width; x+=3 ) 161 | { 162 | bufpos = y * 3 * oldWidth + x; // position in original buffer 163 | newpos = ( height - y - 1 ) * psw + x; // position in padded buffer 164 | 165 | newbuf[newpos] = Buffer[bufpos+2]; // swap r and b 166 | newbuf[newpos + 1] = Buffer[bufpos + 1]; // g stays 167 | newbuf[newpos + 2] = Buffer[bufpos]; // swap b and r 168 | } 169 | 170 | return newbuf; 171 | } 172 | 173 | /*************************************************************** 174 | BYTE* ConvertBMPToRGBBuffer ( BYTE* Buffer, 175 | int width, int height ) 176 | 177 | This function takes as input the data array 178 | from a bitmap and its width and height. 179 | It then converts the bmp data into an RGB array. 180 | The calling function must delete both the input 181 | and output arrays. 182 | The size of the returned array will be 183 | width * height * 3 184 | On error the returb value is NULL, else the 185 | RGB array. 186 | 187 | 188 | The Buffer is expected to be the exact data read out 189 | from a .bmp file. 190 | The function will swap it around, since images 191 | are stored upside-down in bmps. 192 | The BGR triplets from the image data will 193 | be converted to RGB. 194 | And finally the function removes padding bytes. 195 | The returned arraay consits then of 196 | width * height RGB triplets. 197 | 198 | *****************************************************************/ 199 | 200 | BYTE* ConvertBMPToRGBBuffer ( BYTE* Buffer, int width, int height ) 201 | { 202 | // first make sure the parameters are valid 203 | if ( ( NULL == Buffer ) || ( width == 0 ) || ( height == 0 ) ) 204 | return NULL; 205 | 206 | // find the number of padding bytes 207 | 208 | int padding = 0; 209 | int scanlinebytes = width * 3; 210 | while ( ( scanlinebytes + padding ) % 4 != 0 ) // DWORD = 4 bytes 211 | padding++; 212 | // get the padded scanline width 213 | int psw = scanlinebytes + padding; 214 | 215 | // create new buffer 216 | BYTE* newbuf = new BYTE[width*height*3]; 217 | 218 | // now we loop trough all bytes of the original buffer, 219 | // swap the R and B bytes and the scanlines 220 | long bufpos = 0; 221 | long newpos = 0; 222 | for ( int y = 0; y < height; y++ ) 223 | for ( int x = 0; x < 3 * width; x+=3 ) 224 | { 225 | newpos = y * 3 * width + x; 226 | bufpos = ( height - y - 1 ) * psw + x; 227 | 228 | newbuf[newpos] = Buffer[bufpos + 2]; //red 229 | newbuf[newpos + 1] = Buffer[bufpos+1]; //green 230 | newbuf[newpos + 2] = Buffer[bufpos]; 231 | } 232 | 233 | return newbuf; 234 | } 235 | 236 | 237 | 238 | 239 | 240 | /*********************************************** 241 | bool LoadBMPIntoDC ( HDC hDC, LPCTSTR bmpfile ) 242 | 243 | Takes in a device context and the name of a 244 | bitmap to load. If an error occurs the function 245 | returns false, else the contents of the bmp 246 | are blitted to the HDC 247 | 248 | ************************************************/ 249 | 250 | bool LoadBMPIntoDC ( HDC hDC, LPCTSTR bmpfile ) 251 | { 252 | // check if params are valid 253 | if ( ( NULL == hDC ) || ( NULL == bmpfile ) ) 254 | return false; 255 | 256 | // load bitmap into a bitmap handle 257 | HANDLE hBmp = LoadImage ( NULL, bmpfile, IMAGE_BITMAP, 0, 0, 258 | LR_LOADFROMFILE ); 259 | 260 | if ( NULL == hBmp ) 261 | return false; // failed to load image 262 | 263 | // bitmaps can only be selected into memory dcs: 264 | HDC dcmem = CreateCompatibleDC ( NULL ); 265 | 266 | // now select bitmap into the memory dc 267 | if ( NULL == SelectObject ( dcmem, hBmp ) ) 268 | { // failed to load bitmap into device context 269 | DeleteDC ( dcmem ); 270 | return false; 271 | } 272 | 273 | 274 | // now get the bmp size 275 | BITMAP bm; 276 | GetObject ( hBmp, sizeof(bm), &bm ); 277 | // and blit it to the visible dc 278 | if ( BitBlt ( hDC, 0, 0, bm.bmWidth, bm.bmHeight, dcmem, 279 | 0, 0, SRCCOPY ) == 0 ) 280 | { // failed the blit 281 | DeleteDC ( dcmem ); 282 | return false; 283 | } 284 | 285 | DeleteDC ( dcmem ); // clear up the memory dc 286 | 287 | return true; 288 | } 289 | 290 | 291 | 292 | /*************************************************************** 293 | bool SaveBMP ( BYTE* Buffer, int width, int height, 294 | long paddedsize, LPCTSTR bmpfile ) 295 | 296 | Function takes a buffer of size 297 | and saves it as a * sized bitmap 298 | under the supplied filename. 299 | On error the return value is false. 300 | 301 | ***************************************************************/ 302 | 303 | bool SaveBMP ( BYTE* Buffer, int width, int height, long paddedsize, LPCTSTR bmpfile ) 304 | { 305 | // declare bmp structures 306 | BITMAPFILEHEADER bmfh; 307 | BITMAPINFOHEADER info; 308 | 309 | // andinitialize them to zero 310 | memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) ); 311 | memset ( &info, 0, sizeof (BITMAPINFOHEADER ) ); 312 | 313 | // fill the fileheader with data 314 | bmfh.bfType = 0x4d42; // 0x4d42 = 'BM' 315 | bmfh.bfReserved1 = 0; 316 | bmfh.bfReserved2 = 0; 317 | bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize; 318 | bmfh.bfOffBits = 0x36; // number of bytes to start of bitmap bits 319 | 320 | // fill the infoheader 321 | 322 | info.biSize = sizeof(BITMAPINFOHEADER); 323 | info.biWidth = width; 324 | info.biHeight = height; 325 | info.biPlanes = 1; // we only have one bitplane 326 | info.biBitCount = 24; // RGB mode is 24 bits 327 | info.biCompression = BI_RGB; 328 | info.biSizeImage = 0; // can be 0 for 24 bit images 329 | info.biXPelsPerMeter = 0x0ec4; // paint and PSP use this values 330 | info.biYPelsPerMeter = 0x0ec4; 331 | info.biClrUsed = 0; // we are in RGB mode and have no palette 332 | info.biClrImportant = 0; // all colors are important 333 | 334 | // now we open the file to write to 335 | HANDLE file = CreateFile ( bmpfile , GENERIC_WRITE, FILE_SHARE_READ, 336 | NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); 337 | if ( file == NULL ) 338 | { 339 | CloseHandle ( file ); 340 | return false; 341 | } 342 | 343 | // write file header 344 | unsigned long bwritten; 345 | if ( WriteFile ( file, &bmfh, sizeof ( BITMAPFILEHEADER ), &bwritten, NULL ) == false ) 346 | { 347 | CloseHandle ( file ); 348 | return false; 349 | } 350 | // write infoheader 351 | if ( WriteFile ( file, &info, sizeof ( BITMAPINFOHEADER ), &bwritten, NULL ) == false ) 352 | { 353 | CloseHandle ( file ); 354 | return false; 355 | } 356 | // write image data 357 | if ( WriteFile ( file, Buffer, paddedsize, &bwritten, NULL ) == false ) 358 | { 359 | CloseHandle ( file ); 360 | return false; 361 | } 362 | 363 | // and close file 364 | CloseHandle ( file ); 365 | 366 | return true; 367 | } 368 | 369 | /******************************************************************* 370 | BYTE* LoadBMP ( int* width, int* height, long* size 371 | LPCTSTR bmpfile ) 372 | 373 | The function loads a 24 bit bitmap from bmpfile, 374 | stores it's width and height in the supplied variables 375 | and the whole size of the data (padded) in 376 | and returns a buffer of the image data 377 | 378 | On error the return value is NULL. 379 | 380 | NOTE: make sure you [] delete the returned array at end of 381 | program!!! 382 | *******************************************************************/ 383 | 384 | BYTE* LoadBMP ( int* width, int* height, long* size, LPCTSTR bmpfile ) 385 | { 386 | // declare bitmap structures 387 | BITMAPFILEHEADER bmpheader; 388 | BITMAPINFOHEADER bmpinfo; 389 | // value to be used in ReadFile funcs 390 | DWORD bytesread; 391 | // open file to read from 392 | HANDLE file = CreateFile ( bmpfile , GENERIC_READ, FILE_SHARE_READ, 393 | NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL ); 394 | if ( NULL == file ) 395 | return NULL; // coudn't open file 396 | 397 | 398 | // read file header 399 | if ( ReadFile ( file, &bmpheader, sizeof ( BITMAPFILEHEADER ), &bytesread, NULL ) == false ) 400 | { 401 | CloseHandle ( file ); 402 | return NULL; 403 | } 404 | 405 | //read bitmap info 406 | 407 | if ( ReadFile ( file, &bmpinfo, sizeof ( BITMAPINFOHEADER ), &bytesread, NULL ) == false ) 408 | { 409 | CloseHandle ( file ); 410 | return NULL; 411 | } 412 | 413 | // check if file is actually a bmp 414 | if ( bmpheader.bfType != 'MB' ) 415 | { 416 | CloseHandle ( file ); 417 | return NULL; 418 | } 419 | 420 | // get image measurements 421 | *width = bmpinfo.biWidth; 422 | *height = abs ( bmpinfo.biHeight ); 423 | 424 | // check if bmp is uncompressed 425 | if ( bmpinfo.biCompression != BI_RGB ) 426 | { 427 | CloseHandle ( file ); 428 | return NULL; 429 | } 430 | 431 | // check if we have 24 bit bmp 432 | if ( bmpinfo.biBitCount != 24 ) 433 | { 434 | CloseHandle ( file ); 435 | return NULL; 436 | } 437 | 438 | 439 | // create buffer to hold the data 440 | *size = bmpheader.bfSize - bmpheader.bfOffBits; 441 | BYTE* Buffer = new BYTE[ *size ]; 442 | // move file pointer to start of bitmap data 443 | SetFilePointer ( file, bmpheader.bfOffBits, NULL, FILE_BEGIN ); 444 | // read bmp data 445 | if ( ReadFile ( file, Buffer, *size, &bytesread, NULL ) == false ) 446 | { 447 | delete [] Buffer; 448 | CloseHandle ( file ); 449 | return NULL; 450 | } 451 | 452 | // everything successful here: close file and return buffer 453 | 454 | CloseHandle ( file ); 455 | 456 | return Buffer; 457 | } 458 | 459 | void TestBMPCopy (LPCTSTR input, LPCTSTR output) 460 | { 461 | int x, y; 462 | long s; 463 | BYTE* b = LoadBMP ( &x, &y, &s, input ); 464 | SaveBMP ( b, x, y, s, output ); 465 | delete [] b; 466 | } 467 | 468 | void TestBMPCopy2(LPCTSTR input, char* output) 469 | { 470 | int x, y, x1; 471 | long s, s2; 472 | BYTE* a = LoadBMP ( &x, &y, &s, input ); 473 | BYTE* b = ConvertBMPToRGBBuffer ( a, x, y ); 474 | printf("original dimensions = %d , %d \n", x, y); 475 | BYTE* b11 = downsampling(b, &x,&y,4); 476 | printf("downsampling dimensions = %d , %d \n", x, y); 477 | BYTE* b1 = prepocess(b11,&x,&y); 478 | BYTE* c = ConvertRGBToBMPBuffer ( b1, x, y, &s2 ); 479 | //SaveBMP ( c, x, y, s2, strcat(output,"_p.bmp") ); 480 | output[14]='\0'; 481 | printf("preprocessing dimensions = %d , %d \n", x, y); 482 | 483 | 484 | 485 | 486 | BYTE* b2 = LocalWarping( b1, x, y, output); 487 | b2 = LocalWarping( b2, x, y, output); 488 | 489 | BYTE* b3 = scaleup2( b2, &x, &y); 490 | b3 = scaleup2( b3, &x, &y); 491 | printf("upscaled dimensions = %d , %d \n", x, y); 492 | 493 | c = ConvertRGBToBMPBuffer ( b3, x, y, &s2 ); 494 | SaveBMP ( c, x, y, s2, strcat(output,".bmp") ); 495 | delete [] a; 496 | delete [] b; 497 | delete [] c; 498 | delete [] b1; 499 | delete [] b11; 500 | delete [] b2; 501 | delete [] b3; 502 | } 503 | 504 | int main () 505 | { 506 | // TestBMPCopy (L"test.bmp", L"copy.bmp"); 507 | char input[30]; 508 | char output[30]; 509 | 510 | strcpy(input, "images/input "); 511 | strcpy(output, "images/output "); 512 | printf("\n"); 513 | 514 | 515 | for (char i = '1'; i<='1';i++){ //change the test condition from 1-9 for more files 516 | input[12]=i; 517 | input[13]='\0'; 518 | 519 | output[13]=i; 520 | output[14]='\0'; 521 | strcat(input,".bmp"); 522 | printf(input); 523 | printf("\n"); 524 | //strcat(output,".bmp"); 525 | printf(output); 526 | printf("\n********************************\n"); 527 | TestBMPCopy2 (input, output); 528 | } 529 | return 1; 530 | 531 | } 532 | 533 | int mainxx () 534 | { 535 | Affinefverify(); 536 | } 537 | -------------------------------------------------------------------------------- /matrix4.cpp: -------------------------------------------------------------------------------- 1 | 2 | //code in this section is largely copied from http://rodolphe-vaillant.fr/?e=7 and http://dev.tplanet.net/Matrix2D/ 3 | #include 4 | #include 5 | #include "CGProject.h" 6 | 7 | 8 | void formAq(int A[8], int Aq[8][4], int AqT[4][8]) 9 | { 10 | 11 | for(int i=0;i<8;i++) 12 | { 13 | if (i%2==0) 14 | { 15 | Aq[i][0] = A[i]; 16 | Aq[i][1] = -A[i+1]; 17 | Aq[i][2] = 1; 18 | Aq[i][3] = 0; 19 | 20 | AqT[0][i] = A[i]; 21 | AqT[1][i] = -A[i+1]; 22 | AqT[2][i] = 1; 23 | AqT[3][i] = 0; 24 | } 25 | else 26 | { 27 | Aq[i][0] = A[i]; 28 | Aq[i][1] = A[i-1]; 29 | Aq[i][2] = 0; 30 | Aq[i][3] = 1; 31 | 32 | AqT[0][i] = A[i]; 33 | AqT[1][i] = A[i-1]; 34 | AqT[2][i] = 0; 35 | AqT[3][i] = 1; 36 | } 37 | } 38 | 39 | } 40 | 41 | void displayAq(int Aq[8][4]) 42 | { 43 | int i,j; 44 | for (i= 0;i<8;i++) 45 | { 46 | printf("\n[\t"); 47 | for (j= 0;j<4;j++) 48 | { 49 | printf("%d\t",Aq[i][j]); 50 | } 51 | printf("]\n"); 52 | } 53 | } 54 | 55 | void displayAqF(float Aq[8][4]) 56 | { 57 | int i,j; 58 | for (i= 0;i<8;i++) 59 | { 60 | printf("\n[\t"); 61 | for (j= 0;j<4;j++) 62 | { 63 | printf("%f\t",Aq[i][j]); 64 | } 65 | printf("]\n"); 66 | } 67 | } 68 | 69 | void displayAqT(int Aq[4][8]) 70 | { 71 | int i,j; 72 | for (i= 0;i<4;i++) 73 | { 74 | printf("\n[\t"); 75 | for (j= 0;j<8;j++) 76 | { 77 | printf("%d\t",Aq[i][j]); 78 | } 79 | printf("]\n"); 80 | } 81 | } 82 | 83 | void displayA(int A[8]) 84 | { 85 | int i,j; 86 | 87 | printf("\n[\t"); 88 | for (j= 0;j<8;j++) 89 | { 90 | printf("%d\t",A[j]); 91 | } 92 | printf("]\n"); 93 | 94 | } 95 | 96 | void display44(int A[4][4]) 97 | { 98 | int i,j; 99 | for (i= 0;i<4;i++) 100 | { 101 | printf("\n[\t"); 102 | for (j= 0;j<4;j++) 103 | { 104 | printf("%d\t",A[i][j]); 105 | } 106 | printf("]\n"); 107 | } 108 | 109 | } 110 | void mat4mulB(int AqTAq[4][4], int Aq[8][4], int AqT[4][8]) 111 | { 112 | int r,c,k; 113 | for (r=0;r<4;r++) 114 | { 115 | for (c=0;c<4;c++) 116 | { 117 | int x = 0; 118 | for (k= 0;k<8;k++) 119 | { 120 | x += AqT[r][k]*Aq[k][c]; 121 | } 122 | AqTAq[r][c] = x; 123 | //printf ("r=%d, c=%d, x=%d \n", r,c,x ); 124 | } 125 | 126 | } 127 | } 128 | 129 | 130 | void mat4mulA(float partA[8][4], int Aq[8][4], float partB[4][4]) 131 | { 132 | int r,c,k; 133 | for (r=0;r<8;r++) 134 | { 135 | for (c=0;c<4;c++) 136 | { 137 | float x = 0.0; 138 | for (k= 0;k<4;k++) 139 | { 140 | x += ((float)Aq[r][k])*partB[k][c]; 141 | } 142 | partA[r][c] = x; 143 | //printf ("r=%d, c=%d, x=%d \n", r,c,x ); 144 | } 145 | 146 | } 147 | } 148 | 149 | 150 | void mat4mulC(float partC[8][8], float partA[8][4], int AqT[4][8]) 151 | { 152 | int r,c,k; 153 | for (r=0;r<8;r++) 154 | { 155 | for (c=0;c<8;c++) 156 | { 157 | float x = 0.0; 158 | for (k= 0;k<4;k++) 159 | { 160 | x += partA[r][k]*((float)AqT[k][c]); 161 | } 162 | partC[r][c] = x; 163 | //printf ("r=%d, c=%d, x=%d \n", r,c,x ); 164 | } 165 | 166 | } 167 | } 168 | 169 | bool invertColumnMajor(float m[16], float invOut[16]) 170 | { 171 | float inv[16], det; 172 | int i; 173 | 174 | inv[ 0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10]; 175 | inv[ 4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10]; 176 | inv[ 8] = m[4] * m[ 9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[ 9]; 177 | inv[12] = -m[4] * m[ 9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[ 9]; 178 | inv[ 1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10]; 179 | inv[ 5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10]; 180 | inv[ 9] = -m[0] * m[ 9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[ 9]; 181 | inv[13] = m[0] * m[ 9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[ 9]; 182 | inv[ 2] = m[1] * m[ 6] * m[15] - m[1] * m[ 7] * m[14] - m[5] * m[2] * m[15] + m[5] * m[3] * m[14] + m[13] * m[2] * m[ 7] - m[13] * m[3] * m[ 6]; 183 | inv[ 6] = -m[0] * m[ 6] * m[15] + m[0] * m[ 7] * m[14] + m[4] * m[2] * m[15] - m[4] * m[3] * m[14] - m[12] * m[2] * m[ 7] + m[12] * m[3] * m[ 6]; 184 | inv[10] = m[0] * m[ 5] * m[15] - m[0] * m[ 7] * m[13] - m[4] * m[1] * m[15] + m[4] * m[3] * m[13] + m[12] * m[1] * m[ 7] - m[12] * m[3] * m[ 5]; 185 | inv[14] = -m[0] * m[ 5] * m[14] + m[0] * m[ 6] * m[13] + m[4] * m[1] * m[14] - m[4] * m[2] * m[13] - m[12] * m[1] * m[ 6] + m[12] * m[2] * m[ 5]; 186 | inv[ 3] = -m[1] * m[ 6] * m[11] + m[1] * m[ 7] * m[10] + m[5] * m[2] * m[11] - m[5] * m[3] * m[10] - m[ 9] * m[2] * m[ 7] + m[ 9] * m[3] * m[ 6]; 187 | inv[ 7] = m[0] * m[ 6] * m[11] - m[0] * m[ 7] * m[10] - m[4] * m[2] * m[11] + m[4] * m[3] * m[10] + m[ 8] * m[2] * m[ 7] - m[ 8] * m[3] * m[ 6]; 188 | inv[11] = -m[0] * m[ 5] * m[11] + m[0] * m[ 7] * m[ 9] + m[4] * m[1] * m[11] - m[4] * m[3] * m[ 9] - m[ 8] * m[1] * m[ 7] + m[ 8] * m[3] * m[ 5]; 189 | inv[15] = m[0] * m[ 5] * m[10] - m[0] * m[ 6] * m[ 9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[ 9] + m[ 8] * m[1] * m[ 6] - m[ 8] * m[2] * m[ 5]; 190 | 191 | det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12]; 192 | 193 | if(det == 0) 194 | return false; 195 | 196 | det = 1.f / det; 197 | 198 | for(i = 0; i < 16; i++) 199 | invOut[i] = inv[i] * det; 200 | 201 | return true; 202 | } 203 | 204 | void mat4mulFloat(float A[16], float P_1[16], float Q[16]) 205 | { 206 | int r,c,k; 207 | for (r=0;r<4;r++) 208 | { 209 | for (c=0;c<4;c++) 210 | { 211 | float x = 0.0; 212 | for (k= 0;k<4;k++) 213 | { 214 | x += Q[r*4+k]*P_1[k*4+c]; //((float)Aq[r][k])*partB[k][c]; 215 | } 216 | A[r*4+c] = x; 217 | //printf ("r=%d, c=%d, x=%d \n", r,c,x ); 218 | } 219 | 220 | } 221 | } 222 | 223 | 224 | 225 | 226 | 227 | void display16float(float A[16]) 228 | { 229 | int i,j; 230 | for (i= 0;i<4;i++) 231 | { 232 | printf("\n[\t"); 233 | for (j= 0;j<4;j++) 234 | { 235 | printf("%f\t",A[i*4 + j]); 236 | } 237 | printf("]\n"); 238 | } 239 | 240 | } 241 | 242 | 243 | void getAffine(float A[16], float P[16], float Q[16]) 244 | { 245 | float P_1[16]; 246 | invertColumnMajor(P,P_1); 247 | mat4mulFloat(A,P_1,Q); 248 | //printf("\nAffine:\n"); 249 | //display16float(A); 250 | } 251 | 252 | 253 | void display44float(float A[4][4]) 254 | { 255 | int i,j; 256 | for (i= 0;i<4;i++) 257 | { 258 | printf("\n[\t"); 259 | for (j= 0;j<4;j++) 260 | { 261 | printf("%f\t",A[i][j]); 262 | } 263 | printf("]\n"); 264 | } 265 | 266 | } 267 | 268 | void display88float(float A[8][8]) 269 | { 270 | int i,j; 271 | for (i= 0;i<8;i++) 272 | { 273 | printf("\n[\t"); 274 | for (j= 0;j<8;j++) 275 | { 276 | printf("%f\t",A[i][j]); 277 | } 278 | printf("]\n"); 279 | } 280 | 281 | } 282 | 283 | void getInv(int a[4][4], float i[4][4]) 284 | { 285 | float m[16], im[16]; 286 | int r,c; 287 | for (r=0;r<4;r++) 288 | { 289 | for (c=0;c<4;c++) 290 | { 291 | m[r*4 + c] = a[r][c]; 292 | } 293 | } 294 | invertColumnMajor(m, im); 295 | for (r=0;r<4;r++) 296 | { 297 | for (c=0;c<4;c++) 298 | { 299 | i[r][c] = im[r*4 + c]; 300 | } 301 | } 302 | //display44float(i); 303 | 304 | } 305 | 306 | void testformAq() 307 | { 308 | int a[8] = {1,2,3,4,5,6,7,8}; 309 | int aq[8][4]; 310 | int aqt[4][8]; 311 | int aqtaq[4][4]; 312 | float ib[4][4], pa[8][4],pc[8][8]; 313 | 314 | formAq(a, aq, aqt); 315 | displayA(a); 316 | displayAq(aq); 317 | displayAqT(aqt); 318 | mat4mulB(aqtaq, aq, aqt); 319 | printf("\npartB\n"); 320 | display44(aqtaq); 321 | printf("\npartIB\n"); 322 | getInv(aqtaq,ib); 323 | mat4mulA(pa, aq, ib); 324 | printf("\npartA\n"); 325 | displayAqF(pa); 326 | mat4mulC(pc, pa, aqt); 327 | printf("\npartC\n"); 328 | display88float(pc); 329 | } 330 | 331 | 332 | void formAq(int a[8], float aqi[8][8]) 333 | { 334 | //int a[8] = {1,2,3,4,5,6,7,8}; 335 | int aq[8][4]; 336 | int aqt[4][8]; 337 | int aqtaq[4][4]; 338 | float ib[4][4], pa[8][4],pc[8][8]; 339 | 340 | formAq(a, aq, aqt); 341 | //displayA(a); 342 | //displayAq(aq); 343 | //displayAqT(aqt); 344 | mat4mulB(aqtaq, aq, aqt); 345 | //printf("\npartB\n"); 346 | //display44(aqtaq); 347 | //printf("\npartIB\n"); 348 | getInv(aqtaq,ib); 349 | mat4mulA(pa, aq, ib); 350 | //printf("\npartA\n"); 351 | //displayAqF(pa); 352 | mat4mulC(pc, pa, aqt); 353 | //printf("\npartC\n"); 354 | //display88float(pc); 355 | for(int x=0;x<8;x++) 356 | { 357 | for(int y=0;y<8;y++) 358 | { 359 | aqi[x][y] = pc[x][y]; 360 | } 361 | } 362 | 363 | } 364 | -------------------------------------------------------------------------------- /mesh_place.cpp: -------------------------------------------------------------------------------- 1 | #include "CGProject.h" 2 | #include 3 | 4 | void hplace_mesh_v0(BYTE* Buffer, int width, int height) 5 | { 6 | int hmesh_beg,hmesh_end,hmesh_delta; 7 | int vmesh_beg,vmesh_end,vmesh_delta; 8 | int margin = 5; 9 | width = width - 2*margin; 10 | height = height - 2*margin; 11 | 12 | hmesh_delta = (sqrt(height)*sqrt(width))/20; 13 | vmesh_delta = (sqrt(height)*sqrt(width))/20; 14 | 15 | width = width + 2*margin; 16 | height = height + 2*margin; 17 | hmesh_beg = margin; 18 | hmesh_end = height - margin; 19 | //vmesh_beg = margin*3; 20 | vmesh_end = width - margin; 21 | 22 | int x=1000,y,newpos; 23 | 24 | for ( y = hmesh_beg; y <= hmesh_end ; y+= hmesh_delta ){} 25 | hmesh_beg += (hmesh_end - (y-hmesh_delta))/2; 26 | hmesh_end = (hmesh_end + (y-hmesh_delta))/2; 27 | 28 | 29 | for ( x= margin; x <= vmesh_end; x+=vmesh_delta){} 30 | vmesh_beg = (margin + (vmesh_end - (x-vmesh_delta))/2)*3; 31 | vmesh_end = (vmesh_end + (x-vmesh_delta))/2; 32 | 33 | //hmesh_end = y-hmesh_delta; 34 | //vmesh_end = x-(3*vmesh_delta); 35 | 36 | 37 | for ( y = hmesh_beg; y <= hmesh_end ; y+= hmesh_delta ) 38 | //for ( x= 0; x < 3 * width; x+=3 ) 39 | for ( x= vmesh_beg; x <= (3 * vmesh_end); x+=3 ) 40 | { 41 | 42 | newpos = y * 3 * width + x; 43 | Buffer[newpos] = 0; 44 | Buffer[newpos+1] = 255; 45 | Buffer[newpos+2] = 0; 46 | } 47 | 48 | for ( y = hmesh_beg; y <= hmesh_end ; y+= 1 ) 49 | //for ( x= 0; x < 3 * width; x+=3 ) 50 | for ( x= vmesh_beg; x <= (3 * vmesh_end); x+=(3*vmesh_delta) ) 51 | { 52 | 53 | newpos = y * 3 * width + x; 54 | Buffer[newpos] = 0; 55 | Buffer[newpos+1] = 255; 56 | Buffer[newpos+2] = 0; 57 | } 58 | 59 | for ( y = hmesh_beg; y <= hmesh_end ; y+= hmesh_delta ) 60 | //for ( x= 0; x < 3 * width; x+=3 ) 61 | for ( x= vmesh_beg; x <= (3 * vmesh_end); x+=(3*vmesh_delta) ) 62 | { 63 | 64 | newpos = y * 3 * width + x; 65 | Buffer[newpos] = 255; 66 | Buffer[newpos+1] = 0; 67 | Buffer[newpos+2] = 0; 68 | } 69 | } 70 | 71 | 72 | void hplace_mesh(BYTE* Buffer, int width, int height, int* Vdisp, int* Hdisp, BYTE* temp) 73 | { 74 | int hmesh_beg,hmesh_end,hmesh_delta; 75 | int vmesh_beg,vmesh_end,vmesh_delta; 76 | int margin = 0; 77 | width = width - 2*margin; 78 | height = height - 2*margin; 79 | 80 | hmesh_delta = (sqrt(height)*sqrt(width))/9; 81 | vmesh_delta = (sqrt(height)*sqrt(width))/9; 82 | 83 | width = width + 2*margin; 84 | height = height + 2*margin; 85 | hmesh_beg = margin; 86 | hmesh_end = height - margin; 87 | //vmesh_beg = margin*3; 88 | vmesh_end = width - margin; 89 | 90 | int x=1000,y,ynew,newpos, newpos_nextline; 91 | 92 | for ( y = hmesh_beg; y <= hmesh_end ; y+= hmesh_delta ){} 93 | hmesh_beg += (hmesh_end - (y-hmesh_delta))/2; 94 | hmesh_end = (hmesh_end + (y-hmesh_delta))/2; 95 | 96 | 97 | for ( x= margin; x <= vmesh_end; x+=vmesh_delta){} 98 | vmesh_beg = (margin + (vmesh_end - (x-vmesh_delta))/2)*3; 99 | vmesh_end = (vmesh_end + (x-vmesh_delta))/2; 100 | 101 | //hmesh_end = y-hmesh_delta; 102 | //vmesh_end = x-(3*vmesh_delta); 103 | 104 | 105 | for ( y = hmesh_beg; y <= hmesh_end ; y+= hmesh_delta ) 106 | //for ( x= 0; x < 3 * width; x+=3 ) 107 | for ( x= vmesh_beg; x <= (3 * vmesh_end); x+=3 ) 108 | { 109 | 110 | newpos = y * 3 * width + x; 111 | 112 | ynew = (y+Vdisp[newpos])>=height?height-1:(y+Vdisp[newpos]); 113 | //printf("width = %d, height = %d\n", width, height); 114 | //printf("for newpos_newline x = %d, y = %d \n", (x + Hdisp[newpos]), ynew); 115 | newpos_nextline = ynew * 3 * width + x + Hdisp[newpos]; 116 | 117 | /*Buffer[newpos] = 128*y/(hmesh_end) + 64; 118 | Buffer[newpos+1] = 50*y/(hmesh_end); 119 | Buffer[newpos+2] = 50*y/(hmesh_end); 120 | 121 | temp[newpos_nextline] = 128*y/(hmesh_end) + 64; 122 | temp[newpos_nextline+1] = 50*y/(hmesh_end); 123 | temp[newpos_nextline+2] = 50*y/(hmesh_end);*/ 124 | 125 | Buffer[newpos] = 0; 126 | Buffer[newpos+1] = 255; 127 | Buffer[newpos+2] = 0; 128 | 129 | temp[newpos_nextline] = 0; 130 | temp[newpos_nextline+1] = 255; 131 | temp[newpos_nextline+2] = 0; 132 | } 133 | 134 | for ( y = hmesh_beg; y <= hmesh_end ; y+= 1 ) 135 | //for ( x= 0; x < 3 * width; x+=3 ) 136 | for ( x= vmesh_beg; x <= (3 * vmesh_end); x+=(3*vmesh_delta) ) 137 | { 138 | 139 | newpos = y * 3 * width + x; 140 | ynew = (y+Vdisp[newpos])>=height?height-1:(y+Vdisp[newpos]); 141 | //printf("width = %d, height = %d\n", width, height); 142 | //printf("for newpos_newline x = %d, y = %d \n", (x + Hdisp[newpos]), ynew); 143 | newpos_nextline = ynew * 3 * width + x + Hdisp[newpos]; 144 | 145 | /*Buffer[newpos] = 120; 146 | Buffer[newpos+1] = 128*x/(3*vmesh_end) + 64; 147 | Buffer[newpos+2] = 0; 148 | 149 | temp[newpos_nextline] = 120; 150 | temp[newpos_nextline+1] = 128*x/(3*vmesh_end) + 64; 151 | temp[newpos_nextline+2] = 0;*/ 152 | 153 | Buffer[newpos] = 0; 154 | Buffer[newpos+1] = 255; 155 | Buffer[newpos+2] = 0; 156 | 157 | temp[newpos_nextline] = 0; 158 | temp[newpos_nextline+1] = 255; 159 | temp[newpos_nextline+2] = 0; 160 | } 161 | 162 | for ( y = hmesh_beg; y <= hmesh_end ; y+= hmesh_delta ) 163 | //for ( x= 0; x < 3 * width; x+=3 ) 164 | for ( x= vmesh_beg; x <= (3 * vmesh_end); x+=(3*vmesh_delta) ) 165 | { 166 | 167 | newpos = y * 3 * width + x; 168 | ynew = (y+Vdisp[newpos])>=height?height-1:(y+Vdisp[newpos]); 169 | //printf("width = %d, height = %d\n", width, height); 170 | //printf("for newpos_newline x = %d, y = %d \n", (x + Hdisp[newpos]), ynew); 171 | newpos_nextline = ynew * 3 * width + x + Hdisp[newpos]; 172 | 173 | Buffer[newpos] = 255; 174 | Buffer[newpos+1] = 0; 175 | Buffer[newpos+2] = 0; 176 | 177 | temp[newpos_nextline] = 255; 178 | temp[newpos_nextline+1] = 0; 179 | temp[newpos_nextline+2] = 0; 180 | } 181 | } 182 | 183 | -------------------------------------------------------------------------------- /my_project.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "CGProject.h" 5 | 6 | BYTE* prepocess( BYTE* Buffer, int *width, int *height ) 7 | { 8 | 9 | int oldWidth = *width; 10 | 11 | removeExtraBoundaries(Buffer, width, height ); 12 | 13 | long newsize = 3 * (*width) * (*height); 14 | BYTE* newbuf = new BYTE[newsize]; 15 | memset ( newbuf, 0, newsize ); 16 | 17 | long bufpos = 0, newpos = 0; 18 | 19 | //printf("buffer Created with %d, %d but old Width was %d, newsize= %l", (*width), (*height), oldWidth, newsize); 20 | for ( int y = 0; y < *height; y++ ) 21 | for ( int x = 0; x < 3 * (*width); x+=3 ) 22 | { 23 | bufpos = y * 3 * oldWidth + x; // position in original buffer 24 | newpos = y * 3 * (*width) + x; // position in new buffer 25 | 26 | //printf("x=%d, y=%d", x, y); 27 | newbuf[newpos] = Buffer[bufpos]; // swap r and b 28 | newbuf[newpos + 1] = Buffer[bufpos + 1]; // g stays 29 | newbuf[newpos + 2] = Buffer[bufpos + 2]; // swap b and r 30 | } 31 | //printf("preprocessing done"); 32 | return newbuf; 33 | } 34 | /* 35 | BYTE* LocalWarping1( BYTE* Buffer, int width, int height ) 36 | { 37 | //getGreyScale(Buffer, *width, *height ); 38 | int xStart = -1, xFin = -1, yStart = -1, yFin = -1, tLen = 0, dir = 0; 39 | long newsize = 3 * (width) * (height); 40 | BYTE* B = new BYTE[newsize]; 41 | BYTE* I = new BYTE[newsize]; 42 | BYTE* dI = new BYTE[newsize]; 43 | BYTE* D = new BYTE[newsize]; 44 | BYTE* S = new BYTE[newsize]; 45 | 46 | getGreyScale(Buffer, width, height, I ); 47 | 48 | getGradient(I, width, height, dI); 49 | getBoundaryMap(I, width, height, B ); 50 | 51 | BufImpose(dI,dI,B,width,height); 52 | //markSegmentforLargestBoundary(I, width, height ); 53 | getSegmentforLargestBoundary( I, width, height, &xStart, &xFin, &yStart, &yFin, &tLen, &dir ); 54 | printf("len of boundary segment =%d, dir = %d\n", tLen, dir); 55 | int zbc=0; 56 | while(tLen > 1)// && zbc<500) 57 | //for (int zbc=0; zbc<100; zbc++) 58 | { 59 | printf("%d, tLen=%d, dir=%d \n",zbc,tLen, dir); 60 | zbc++; 61 | 62 | if(dir==1) 63 | { 64 | //printf("reached"); 65 | delete [] D; 66 | //printf("reached"); 67 | D = getHDisplacementImage1( dI, width, height, xStart, xFin, yStart, yFin, B, S); 68 | printf("found displacement"); 69 | displaceAbove(I, D, width, height, Buffer); 70 | printf("displacemed"); 71 | } 72 | else if(dir==3) 73 | { 74 | delete [] D; 75 | D = getHDisplacementImage3( dI, width, height, xStart, xFin, yStart, yFin, B, S); 76 | displaceBelow(I, D, width, height, Buffer); 77 | } 78 | else if(dir==2) 79 | { 80 | delete [] D; 81 | D = getVDisplacementImage2( dI, width, height, xStart, xFin, yStart, yFin, B, S); 82 | displaceLeft(I, D, width, height, Buffer); 83 | //zbc=500; 84 | //break; 85 | //displaceBelow(I, D, width, height, Buffer); 86 | } 87 | else if(dir==4) 88 | { 89 | delete [] D; 90 | D = getVDisplacementImage4( dI, width, height, xStart, xFin, yStart, yFin, B, S); 91 | displaceRight(I, D, width, height, Buffer); 92 | //zbc=500; 93 | //break; 94 | //displaceBelow(I, D, width, height, Buffer); 95 | } 96 | else 97 | { 98 | printf("dir=%d\n",dir); 99 | break; 100 | } 101 | 102 | //getGreyScale(I,width,height,I); 103 | getGreyScale(Buffer, width, height, I ); 104 | getGradient(I, width, height, dI); 105 | getBoundaryMap(I, width, height, B ); 106 | BufImpose(dI,dI,B,width,height); 107 | xStart = -1; xFin = -1; yStart = -1; yFin = -1; tLen = 0; dir = 0; 108 | getSegmentforLargestBoundary( I, width, height, &xStart, &xFin, &yStart, &yFin, &tLen, &dir ); 109 | //tLen=0; 110 | } 111 | 112 | 113 | //return 114 | BufCopy(I,Buffer,width,height); 115 | return I; 116 | } 117 | */ 118 | 119 | 120 | BYTE* LocalWarping( BYTE* Buffer, int width, int height, char * output ) 121 | { 122 | //getGreyScale(Buffer, *width, *height ); 123 | int xStart = -1, xFin = -1, yStart = -1, yFin = -1, tLen = 0, dir = 0; 124 | long newsize = 3 * (width) * (height); 125 | 126 | /*BYTE* tp = new BYTE[newsize]; 127 | BufCopy(tp,Buffer,width,height); 128 | return tp;*/ 129 | 130 | BYTE* forfinal = new BYTE[newsize]; 131 | BYTE* temp = new BYTE[newsize]; 132 | BYTE* B = new BYTE[newsize]; 133 | BYTE* S = new BYTE[newsize]; 134 | BYTE* I = new BYTE[newsize]; 135 | BYTE* dI = new BYTE[newsize]; 136 | BYTE* D = new BYTE[newsize]; 137 | int* Vdisp = new int[newsize]; 138 | int* Hdisp = new int[newsize]; 139 | 140 | // for(int kkk=0;kkk<2;kkk++) 141 | { 142 | 143 | memset(S,0,newsize); 144 | memset(Vdisp,0,newsize); 145 | memset(Hdisp,0,newsize); 146 | BufCopy(temp,Buffer,width,height); 147 | BufCopy(forfinal,Buffer,width,height); 148 | BufCopy(I,Buffer,width,height); 149 | getGreyScale(Buffer, width, height, I ); 150 | 151 | char suffix[10]; 152 | getGradient(I, width, height, dI); 153 | getBoundaryMap(I, width, height, B ); 154 | 155 | BufImpose(dI,dI,B,width,height); 156 | //markSegmentforLargestBoundary(I, width, height ); 157 | getSegmentforLargestBoundary( I, width, height, &xStart, &xFin, &yStart, &yFin, &tLen, &dir ); 158 | //printf("len of boundary segment =%d, dir = %d\n", tLen, dir); 159 | int zbc=0; 160 | long s2; 161 | while(tLen > 1)// && zbc<500) 162 | //for (int zbc=0; zbc<100; zbc++) 163 | { 164 | 165 | zbc++; 166 | 167 | if(zbc%5==0){ 168 | tostring(suffix,zbc); 169 | printf("%d, tLen=%d, dir=%d \n",zbc,tLen, dir); 170 | strcat(strcat(strcat(output,"_sc"),suffix),".bmp"); 171 | //saveAsImage(I, width, height, output); 172 | output[14]='\0'; 173 | } 174 | 175 | 176 | if(dir==1) 177 | { 178 | //printf("reached"); 179 | delete [] D; 180 | //printf("reached"); 181 | D = getHDisplacementImage1( dI, width, height, xStart, xFin, yStart, yFin, B, S); 182 | //printf("found displacement"); 183 | displaceAbove1(I, D, width, height, Buffer,Vdisp, xStart, xFin, yStart, yFin, S); 184 | //printf("displacemed"); 185 | } 186 | else if(dir==3) 187 | { 188 | delete [] D; 189 | D = getHDisplacementImage3( dI, width, height, xStart, xFin, yStart, yFin, B, S); 190 | displaceBelow1(I, D, width, height, Buffer,Vdisp, xStart, xFin, yStart, yFin, S); 191 | } 192 | else if(dir==2) 193 | { 194 | delete [] D; 195 | D = getVDisplacementImage2( dI, width, height, xStart, xFin, yStart, yFin, B, S); 196 | displaceLeft1(I, D, width, height, Buffer, Hdisp, S); 197 | //zbc=500; 198 | //break; 199 | //displaceBelow(I, D, width, height, Buffer); 200 | } 201 | else if(dir==4) 202 | { 203 | delete [] D; 204 | D = getVDisplacementImage4( dI, width, height, xStart, xFin, yStart, yFin, B, S); 205 | displaceRight1(I, D, width, height, Buffer, Hdisp, S); 206 | //zbc=500; 207 | //break; 208 | //displaceBelow(I, D, width, height, Buffer); 209 | } 210 | else 211 | { 212 | printf("dir=%d\n",dir); 213 | break; 214 | } 215 | 216 | //getGreyScale(I,width,height,I); 217 | BufCopy(I,Buffer,width,height); 218 | getGreyScale(Buffer, width, height, I ); 219 | getGradient(I, width, height, dI); 220 | getBoundaryMap(I, width, height, B ); 221 | BufImpose(dI,dI,B,width,height); 222 | xStart = -1; xFin = -1; yStart = -1; yFin = -1; tLen = 0; dir = 0; 223 | getSegmentforLargestBoundary( I, width, height, &xStart, &xFin, &yStart, &yFin, &tLen, &dir ); 224 | //tLen=0; 225 | } 226 | 227 | 228 | hplace_mesh(I, width, height, Vdisp,Hdisp,temp); 229 | //saveAsImage(I, width, height, strcat(output,"z11mp.bmp")); 230 | output[14]='\0'; 231 | 232 | //saveAsImage(temp, width, height, strcat(output,"z21rb.bmp")); 233 | output[14]='\0'; 234 | 235 | /*memset(temp,0,newsize); 236 | hplace_mesh(I, width, height, Vdisp,Hdisp,temp); 237 | c = ConvertRGBToBMPBuffer ( temp, width, height, &s2 ); 238 | SaveBMP ( c, width, height, s2, strcat(output,"bm.bmp") ); 239 | output[14]='\0';*/ 240 | global_warping(forfinal, width, height, Vdisp,Hdisp, output); 241 | 242 | BufCopy(I,forfinal,width,height); 243 | 244 | xStart = -1; xFin = -1; yStart = -1; yFin = -1; tLen = 0; dir = 0; 245 | 246 | } 247 | delete [] forfinal; 248 | delete [] temp; 249 | delete [] Vdisp; 250 | delete [] Hdisp; 251 | delete [] B; 252 | delete [] S; 253 | delete [] dI; 254 | delete [] D; 255 | return I; 256 | } 257 | -------------------------------------------------------------------------------- /sampling.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "CGProject.h" 5 | #include 6 | 7 | BYTE* downsampling( BYTE* Buffer, int *width, int *height, int n ) 8 | { 9 | //n=n*n; 10 | int oldwidth = *width, oldheight = *height, newwidth = floor(oldwidth/n), newheight = floor(oldheight/n); 11 | int x,xo=0,y,yo=0; 12 | 13 | long newsize = 3*newwidth*newheight, newpos, oldpos; 14 | BYTE* newbuf = new BYTE[newsize]; 15 | //printf("oldwidth = %d, oldheight = %d, newwidth = %d, newheight = %d\n",oldwidth,oldheight, newwidth,newheight ); 16 | 17 | for ( y = 0; y < newheight; y++,yo+=n ) 18 | for ( x= 0, xo=0; x < 3 * newwidth; x+=3,xo+=(3*n) ) 19 | { 20 | newpos = y * 3 * newwidth + x; 21 | oldpos = yo * 3 * oldwidth + xo; 22 | //printf("y=%d, yo=%d, x=%d, xo=%d\n",y,yo,x,xo); 23 | //printf("oldwidth = %d, oldheight = %d, newwidth = %d, newheight = %d\n",oldwidth,oldheight, newwidth,newheight ); 24 | 25 | newbuf[newpos] = Buffer[oldpos]; 26 | newbuf[newpos+1] = Buffer[oldpos+1]; 27 | newbuf[newpos+2] = Buffer[oldpos+2]; 28 | } 29 | *width = newwidth; 30 | *height = newheight; 31 | return newbuf; 32 | } 33 | 34 | 35 | BYTE* scaleup2( BYTE* Buffer, int *width, int *height ) 36 | { 37 | int oldwidth = *width, oldheight = *height, newwidth = oldwidth*2, newheight = oldheight; 38 | long newsize = 3*newwidth*newheight,newpos, oldpos; 39 | BYTE* newbuf1 = new BYTE[newsize]; 40 | int x,xo,y,yo; 41 | 42 | for ( y = 0, yo = 0; y < newheight; yo++,y++ ) 43 | for ( x= 0, xo=0; x < 3 * newwidth; xo+=3,x+=6 ) 44 | { 45 | newpos = y * 3 * newwidth + x; 46 | oldpos = yo * 3 * oldwidth + xo; 47 | //printf("y=%d, yo=%d, x=%d, xo=%d\n",y,yo,x,xo); 48 | //printf("oldwidth = %d, oldheight = %d, newwidth = %d, newheight = %d\n",oldwidth,oldheight, newwidth,newheight ); 49 | 50 | newbuf1[newpos] = Buffer[oldpos]; 51 | newbuf1[newpos+1] = Buffer[oldpos+1]; 52 | newbuf1[newpos+2] = Buffer[oldpos+2]; 53 | } 54 | 55 | for ( y = 0; y < newheight; y++ ) 56 | for ( x= 3; x < 3 * newwidth ; x+=6 ) 57 | { 58 | xo=(x>(3 * newwidth - 6))?-3:3; 59 | newpos = y * 3 * newwidth + x; 60 | //printf("y=%d, yo=%d, x=%d, xo=%d\n",y,yo,x,xo); 61 | //printf("oldwidth = %d, oldheight = %d, newwidth = %d, newheight = %d\n",oldwidth,oldheight, newwidth,newheight ); 62 | 63 | newbuf1[newpos] = newbuf1[newpos+xo]/2 + newbuf1[newpos-3]/2; 64 | newbuf1[newpos+1] = newbuf1[newpos+xo+1]/2 + newbuf1[newpos+1-3]/2; 65 | newbuf1[newpos+2] = newbuf1[newpos+xo+2]/2 + newbuf1[newpos+2-3]/2; 66 | } 67 | 68 | //****************************************************************************************** 69 | oldheight = newheight; 70 | newheight *= 2; 71 | newsize = 3*newwidth*newheight; 72 | BYTE* newbuf2 = new BYTE[newsize]; 73 | 74 | for ( y = 0, yo = 0; y < newheight; yo++,y+=2 ) 75 | for ( x= 0, xo=0; x < 3 * newwidth; xo+=3,x+=3 ) 76 | { 77 | newpos = y * 3 * newwidth + x; 78 | oldpos = yo * 3 * newwidth + xo; 79 | //printf("y=%d, yo=%d, x=%d, xo=%d\n",y,yo,x,xo); 80 | //printf("oldwidth = %d, oldheight = %d, newwidth = %d, newheight = %d\n",oldwidth,oldheight, newwidth,newheight ); 81 | 82 | newbuf2[newpos] = newbuf1[oldpos]; 83 | newbuf2[newpos+1] = newbuf1[oldpos+1]; 84 | newbuf2[newpos+2] = newbuf1[oldpos+2]; 85 | } 86 | 87 | 88 | for ( y = 1; y < newheight; y+=2 ) 89 | for ( x= 0; x < 3 * newwidth ; x+=3 ) 90 | { 91 | yo = y==(newheight-1)?-1:1; 92 | newpos = y * 3 * newwidth + x; 93 | //printf("y=%d, yo=%d, x=%d, xo=%d\n",y,yo,x,xo); 94 | //printf("oldwidth = %d, oldheight = %d, newwidth = %d, newheight = %d\n",oldwidth,oldheight, newwidth,newheight ); 95 | 96 | newbuf2[newpos] = newbuf2[newpos+(3 * newwidth)*yo]/2 + newbuf2[newpos-(3 * newwidth)]/2; 97 | newbuf2[newpos+1] = newbuf2[newpos+(3 * newwidth)*yo+1]/2 + newbuf2[newpos+1-(3 * newwidth)]/2; 98 | newbuf2[newpos+2] = newbuf2[newpos+(3 * newwidth)*yo+2]/2 + newbuf2[newpos+2-(3 * newwidth)]/2; 99 | } 100 | 101 | *width = newwidth; 102 | *height = newheight; 103 | delete [] newbuf1; 104 | return newbuf2; 105 | } 106 | //copied from http://www.sanfoundry.com/c-program-integer-to-string-vice-versa/ 107 | void tostring(char str[], int num) 108 | { 109 | int i, rem, len = 0, n; 110 | 111 | n = num; 112 | while (n != 0) 113 | { 114 | len++; 115 | n /= 10; 116 | } 117 | for (i = 0; i < len; i++) 118 | { 119 | rem = num % 10; 120 | num = num / 10; 121 | str[len - (i + 1)] = rem + '0'; 122 | } 123 | str[len] = '\0'; 124 | } 125 | -------------------------------------------------------------------------------- /seam_carvingH.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "CGProject.h" 4 | 5 | 6 | 7 | BYTE* markHorizontalSeamAbove( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 8 | { 9 | //printf("reached"); 10 | int x,y,ymin,ymax; 11 | long newsize =3 * (width) * (height), newpos, posa,posb,posc;//newposi_1j_1, newposij_1, newposi1j_1; 12 | BYTE Ma, Mb, Mc, Mmin, MM; 13 | BYTE* M = new BYTE[newsize]; 14 | 15 | BufCopy(M,Buffer,width, height); 16 | 17 | 18 | //cumulative energy calculation 19 | //return M; 20 | BufImpose(M,M,SeamMask,width,height); 21 | Mmin = 255; 22 | ymin=yBegin + rand()%yEnd; 23 | for ( x= xBegin + 3; x <= xEnd; x+=3 ) 24 | { 25 | for ( y = yEnd; y >= yBegin; y-- ) 26 | { 27 | newpos = y * 3 * width + x; 28 | if(isBoundary(M,newpos)){ 29 | continue; 30 | } 31 | posb = newpos - 3; 32 | posa = posb - 3 * width; 33 | posc = posb + 3 * width; 34 | //printf("width = %d, height = %d x = %d, y=%d\n",width,height,x,y); 35 | //printf("posa=%ld, posb=%ld, posc=%ld\n", posa,posb,posc); 36 | Ma = (y==0)?255:M[posa]; 37 | Mb = M[posb]; 38 | Mc = (y==(height-1))?255:M[posc]; 39 | 40 | if(MaxBegin && ymin>0) 93 | { 94 | 95 | y=ymin; 96 | newpos = y * 3 * width + x; 97 | 98 | posb = newpos - 3; 99 | posa = posb - 3 * width; 100 | posc = posb + 3 * width; 101 | //printf("width = %d, height = %d x = %d, y=%d\n",width,height,x,y); 102 | //printf("posa=%ld, posb=%ld, posc=%ld\n", posa,posb,posc); 103 | Ma = (y==0)?255:M[posa]; 104 | Mb = M[posb]; 105 | Mc = (y==(height-1))?255:M[posc]; 106 | 107 | 108 | if(Ma<=Mb) 109 | { 110 | if(Ma<=Mc) {ymin=y?(y-1):0;} 111 | else {ymin = ymin%height?(ymin+1):0;} 112 | } 113 | else 114 | { 115 | if(Mb<=Mc) {ymin = ymin;} 116 | else {ymin = ymin%height?(ymin+1):0;} 117 | } 118 | x-=3; 119 | newpos = ymin * 3 * width + x; 120 | //printf("x= %d, y=%d\n",x,y); 121 | M[newpos] = 0; 122 | M[newpos+1] = 255; 123 | M[newpos+2] = 0; 124 | } 125 | //printf("seam carved"); 126 | return M; 127 | } 128 | 129 | BYTE* markHorizontalSeamBelow (BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 130 | { 131 | int x,y,ymin; 132 | long newsize =3 * (width) * (height), newpos, posa,posb,posc;//newposi_1j_1, newposij_1, newposi1j_1; 133 | BYTE Ma, Mb, Mc, Mmin, MM; 134 | BYTE* M = new BYTE[newsize]; 135 | 136 | BufCopy(M,Buffer,width, height); 137 | 138 | BufImpose(M,M,SeamMask,width,height); 139 | //cumulative energy calculation 140 | //return M; 141 | Mmin = 255; 142 | for ( x= xBegin + 3; x <= xEnd; x+=3 ) 143 | { 144 | for ( y = yBegin; y <= yEnd; y++ ) 145 | { 146 | newpos = y * 3 * width + x; 147 | 148 | if(xxBegin && ymin>0) 204 | { 205 | 206 | y=ymin; 207 | newpos = y * 3 * width + x; 208 | 209 | posb = newpos - 3; 210 | posa = posb - 3 * width; 211 | posc = posb + 3 * width; 212 | 213 | Ma = (y==0)?255:M[posa]; 214 | Mb = M[posb]; 215 | Mc = (y==(height-1))?255:M[posc]; 216 | 217 | 218 | if(Ma<=Mb) 219 | { 220 | if(Ma<=Mc) {ymin=y?(y-1):0;} 221 | else {ymin = ymin%height?(ymin+1):0;} 222 | } 223 | else 224 | { 225 | if(Mb<=Mc) {ymin = ymin;} 226 | else {ymin = ymin%height?(ymin+1):0;} 227 | } 228 | x-=3; 229 | newpos = ymin * 3 * width + x; 230 | //printf("x= %d, y=%d\n",x,y); 231 | M[newpos] = 0; 232 | M[newpos+1] = 255; 233 | M[newpos+2] = 0; 234 | } 235 | //printf("seam carved"); 236 | return M; 237 | } 238 | 239 | 240 | BYTE* getHDisplacementImage1( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 241 | { 242 | //printf("reached"); 243 | BYTE* M = markHorizontalSeamAbove( Buffer, width, height, xBegin, xEnd,yBegin, yEnd, BoundaryMask, SeamMask); 244 | //return M; 245 | int x,y,ymin,val=255,val1=128; 246 | long newsize =3 * (width) * (height), newpos; 247 | BYTE* D = new BYTE[newsize]; 248 | memset(D, 0 , newsize); 249 | bool flag=false; 250 | 251 | for ( x= xBegin; x <= xEnd; x+=3 ) 252 | { 253 | flag=false; 254 | for ( y = yBegin; y <= yEnd; y++ ) 255 | { 256 | newpos = y * 3 * width + x; 257 | if(flag||(M[newpos]==0 && M[newpos+1]==255 && M[newpos+2]==0)) 258 | { 259 | flag=true; 260 | 261 | D[newpos]= val1; 262 | D[newpos+1]= val1; 263 | D[newpos+2]= val1; 264 | break; 265 | } 266 | else 267 | { 268 | D[newpos]= val; 269 | D[newpos+1]= val; 270 | D[newpos+2]= val; 271 | } 272 | } 273 | } 274 | delete [] M; 275 | return D; 276 | 277 | } 278 | 279 | 280 | 281 | BYTE* getHDisplacementImage3( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 282 | { 283 | BYTE* M = markHorizontalSeamAbove( Buffer, width, height, xBegin, xEnd,yBegin, yEnd, BoundaryMask, SeamMask); 284 | //return M; 285 | int x,y,ymin,val=255, val1=128; 286 | long newsize =3 * (width) * (height), newpos; 287 | BYTE* D = new BYTE[newsize]; 288 | memset(D, 0 , newsize); 289 | bool flag=false; 290 | 291 | for ( x= xBegin; x <= xEnd; x+=3 ) 292 | { 293 | flag=false; 294 | for ( y = yEnd; y >= yBegin; y-- ) 295 | { 296 | newpos = y * 3 * width + x; 297 | if(flag||(M[newpos]==0 && M[newpos+1]==255 && M[newpos+2]==0)) 298 | { 299 | flag=true; 300 | D[newpos]= val1; 301 | D[newpos+1]= val1; 302 | D[newpos+2]= val1; 303 | 304 | break; 305 | } 306 | else 307 | { 308 | D[newpos]= val; 309 | D[newpos+1]= val; 310 | D[newpos+2]= val; 311 | } 312 | } 313 | } 314 | delete [] M; 315 | return D; 316 | 317 | } 318 | -------------------------------------------------------------------------------- /seam_carvingV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "CGProject.h" 4 | 5 | 6 | 7 | BYTE* markVerticalSeamLeft( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask) 8 | { 9 | int x,y,xmin,xmax; 10 | long newsize =3 * (width) * (height), newpos, posa,posb,posc;//newposi_1j_1, newposij_1, newposi1j_1; 11 | BYTE Ma, Mb, Mc, Mmin, MM; 12 | BYTE* M = new BYTE[newsize]; 13 | 14 | BufCopy(M,Buffer,width, height); 15 | 16 | 17 | //cumulative energy calculation 18 | //return M; 19 | Mmin = 255; 20 | xmin=xBegin + rand()%xEnd; 21 | for ( y = yEnd-1; y >= yBegin; y-- ) 22 | { 23 | for ( x= xBegin; x <= xEnd; x+=3 ) 24 | { 25 | newpos = y * 3 * width + x; 26 | if(isBoundary(M,newpos)){ 27 | continue; 28 | } 29 | posb = newpos + 3 * width; 30 | posa = posb - 3 ; 31 | posc = posb + 3 ; 32 | 33 | Ma = (x==0)?255:M[posa]; 34 | Mb = M[posb]; 35 | Mc = (x==(width-3))?255:M[posc]; 36 | 37 | if(Ma0) 88 | { 89 | //xmax = y>1720?printf(""): 90 | x=xmin; 91 | newpos = y * 3 * width + x; 92 | 93 | posb = newpos + 3 * width; 94 | posa = posb - 3 ; 95 | posc = posb + 3 ; 96 | 97 | Ma = (y==0)?255:M[posa]; 98 | Mb = M[posb]; 99 | Mc = (y==(height-1))?255:M[posc]; 100 | 101 | 102 | if(Ma<=Mb) 103 | { 104 | if(Ma<=Mc) {xmin=x?(x-3):0;} 105 | else {xmin = xmin%width?(xmin+3):0;} 106 | } 107 | else 108 | { 109 | if(Mb<=Mc) {xmin = xmin;} 110 | else {xmin = xmin%width?(xmin+3):0;} 111 | } 112 | y++; 113 | newpos = y * 3 * width + xmin; 114 | //printf("xmin= %d, y=%d\n",xmin,y); 115 | M[newpos] = 0; 116 | M[newpos+1] = 255; 117 | M[newpos+2] = 0; 118 | } 119 | //printf("seam carved\n"); 120 | return M; 121 | } 122 | 123 | BYTE* markHorizontalSeamBelowxx (BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask) 124 | { 125 | int x,y,ymin; 126 | long newsize =3 * (width) * (height), newpos, posa,posb,posc;//newposi_1j_1, newposij_1, newposi1j_1; 127 | BYTE Ma, Mb, Mc, Mmin, MM; 128 | BYTE* M = new BYTE[newsize]; 129 | 130 | BufCopy(M,Buffer,width, height); 131 | 132 | 133 | //cumulative energy calculation 134 | //return M; 135 | Mmin = 255; 136 | for ( x= xBegin + 3; x <= xEnd; x+=3 ) 137 | { 138 | for ( y = yBegin; y <= yEnd; y++ ) 139 | { 140 | newpos = y * 3 * width + x; 141 | if(xxBegin && ymin>0) 197 | { 198 | 199 | y=ymin; 200 | newpos = y * 3 * width + x; 201 | 202 | posb = newpos - 3; 203 | posa = posb - 3 * width; 204 | posc = posb + 3 * width; 205 | 206 | Ma = (y==0)?255:M[posa]; 207 | Mb = M[posb]; 208 | Mc = (y==(height-1))?255:M[posc]; 209 | 210 | 211 | if(Ma<=Mb) 212 | { 213 | if(Ma<=Mc) {ymin=y?(y-1):0;} 214 | else {ymin = ymin%height?(ymin+1):0;} 215 | } 216 | else 217 | { 218 | if(Mb<=Mc) {ymin = ymin;} 219 | else {ymin = ymin%height?(ymin+1):0;} 220 | } 221 | x-=3; 222 | newpos = ymin * 3 * width + x; 223 | //printf("x= %d, y=%d\n",x,y); 224 | M[newpos] = 0; 225 | M[newpos+1] = 255; 226 | M[newpos+2] = 0; 227 | } 228 | //printf("seam carved"); 229 | return M; 230 | } 231 | 232 | 233 | BYTE* getVDisplacementImage2( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 234 | { 235 | BYTE* M = markVerticalSeamLeft( Buffer, width, height, xBegin, xEnd,yBegin, yEnd, BoundaryMask); 236 | //printf("found one seam\n"); 237 | //return M; 238 | int x,y,ymin,val=255,val1=128; 239 | long newsize =3 * (width) * (height), newpos; 240 | BYTE* D = new BYTE[newsize]; 241 | memset(D, 0 , newsize); 242 | bool flag=false; 243 | 244 | for ( y = yBegin; y <= yEnd; y++ ) 245 | { 246 | flag=false; 247 | for ( x= xBegin ; x <= xEnd; x+=3 ) 248 | { 249 | newpos = y * 3 * width + x; 250 | if(flag||(M[newpos]==0 && M[newpos+1]==255 && M[newpos+2]==0)) 251 | { 252 | flag=true; 253 | 254 | D[newpos]= val1; 255 | D[newpos+1]= val1; 256 | D[newpos+2]= val1; 257 | break; 258 | } 259 | else 260 | { 261 | D[newpos]= val; 262 | D[newpos+1]= val; 263 | D[newpos+2]= val; 264 | } 265 | } 266 | } 267 | delete [] M; 268 | return D; 269 | 270 | } 271 | 272 | 273 | BYTE* getVDisplacementImage4( BYTE* Buffer, int width, int height, int xBegin, int xEnd, int yBegin, int yEnd, BYTE* BoundaryMask, BYTE* SeamMask) 274 | { 275 | BYTE* M = markVerticalSeamLeft( Buffer, width, height, xBegin, xEnd,yBegin, yEnd, BoundaryMask); 276 | //printf("found one seam\n"); 277 | //return M; 278 | int x,y,ymin,val=255,val1=128; 279 | long newsize =3 * (width) * (height), newpos; 280 | BYTE* D = new BYTE[newsize]; 281 | memset(D, 0 , newsize); 282 | bool flag=false; 283 | 284 | for ( y = yBegin; y <= yEnd; y++ ) 285 | { 286 | flag=false; 287 | for ( x= xEnd ; x >= xBegin; x-=3 ) 288 | { 289 | newpos = y * 3 * width + x; 290 | if(flag||(M[newpos]==0 && M[newpos+1]==255 && M[newpos+2]==0)) 291 | { 292 | flag=true; 293 | 294 | D[newpos]= val1; 295 | D[newpos+1]= val1; 296 | D[newpos+2]= val1; 297 | break; 298 | } 299 | else 300 | { 301 | D[newpos]= val; 302 | D[newpos+1]= val; 303 | D[newpos+2]= val; 304 | } 305 | } 306 | } 307 | delete [] M; 308 | return D; 309 | 310 | } 311 | 312 | 313 | 314 | --------------------------------------------------------------------------------