Skip to content

Commit 5ee5147

Browse files
author
Joseph Pan
committed
first commit
1 parent 2e25267 commit 5ee5147

12 files changed

+1124
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
2dMorphing
22
==========
33

4-
2D Image Morphing Algorithms based on mesh warping. C++ project, which is transplanted from the c codes given by Yurong Sun and George Wolberg. Details: http://davis.wpi.edu/~matt/courses/morph/2d.htm
4+
2D Image Morphing Algorithms based on mesh warping.
5+
6+
C++ project, which is transplanted from the c codes given by Yurong Sun and George Wolberg.
7+
8+
Details: http://davis.wpi.edu/~matt/courses/morph/2d.htm

catmullrom.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* ======================================================================
2+
* catmullRom.c - Catmull-Rom interpolating spline function.
3+
* Copyright (C) 1993 by George Wolberg
4+
*
5+
* Written by: George Wolberg, 1993
6+
* ======================================================================
7+
*/
8+
9+
#include "meshwarp.h"
10+
#include "catmullrom.h"
11+
#include <iostream>
12+
13+
using namespace std;
14+
15+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
* catmullRom:
17+
*
18+
* Compute a Catmull-Rom spline passing through the len1 points in arrays
19+
* x1, y1, where y1 = f(x1)
20+
* len2 positions on the spline are to be computed. Their positions are
21+
* given in x2. The spline values are stored in y2.
22+
*/
23+
void catmullRom(float *x1, float *y1, int len1, float *x2, float *y2, int len2)
24+
{
25+
int i, j, dir, j1, j2;
26+
double x, dx1, dx2;
27+
double dx, dy, yd1, yd2, p1, p2, p3;
28+
double a0y, a1y, a2y, a3y;
29+
30+
/* find direction of monotonic x1; skip ends */
31+
if(x1[0] < x1[1]) { /* increasing */
32+
if(x2[0]<x1[0] || x2[len2-1]>x1[len1-1]) dir=0;
33+
else dir = 1;
34+
} else { /* decreasing */
35+
if(x2[0]>x1[0] || x2[len2-1]<x1[len1-1]) dir=0;
36+
else dir = -1;
37+
}
38+
if(dir == 0) { /* error */
39+
cerr << "catmullRom: Output x-coord out of range of input\n";
40+
return;
41+
}
42+
43+
/* p1 is first endpoint of interval
44+
* p2 is resampling position
45+
* p3 is second endpoint of interval
46+
* j is input index for current interval
47+
*/
48+
49+
/* force coefficient initialization */
50+
if(dir==1) p3 = x2[0] - 1;
51+
else p3 = x2[0] + 1;
52+
53+
for(i=0; i<len2; i++) {
54+
/* check if in new interval */
55+
p2 = x2[i];
56+
if((dir==1 && p2>p3) || (dir== -1 && p2<p3)) {
57+
/* find the interval which contains p2 */
58+
if(dir) {
59+
for(j=0; j<len1 && p2>x1[j]; j++);
60+
if(p2 < x1[j]) j--;
61+
} else {
62+
for(j=0; j<len1 && p2<x1[j]; j++);
63+
if(p2 > x1[j]) j--;
64+
}
65+
66+
p1 = x1[j]; /* update 1st endpt */
67+
p3 = x1[j+1]; /* update 2nd endpt */
68+
69+
/* clamp indices for endpoint interpolation */
70+
j1 = MAX(j-1, 0);
71+
j2 = MIN(j+2, len1-1);
72+
73+
/* compute spline coefficients */
74+
dx = 1.0 / (p3 - p1);
75+
dx1 = 1.0 / (p3 - x1[j1]);
76+
dx2 = 1.0 / (x1[j2] - p1);
77+
dy = (y1[j+1] - y1[ j ]) * dx;
78+
yd1 = (y1[j+1] - y1[ j1]) * dx1;
79+
yd2 = (y1[j2 ] - y1[ j ]) * dx2;
80+
a0y = y1[j];
81+
a1y = yd1;
82+
a2y = dx * ( 3*dy - 2*yd1 - yd2);
83+
a3y = dx*dx*(-2*dy + yd1 + yd2);
84+
}
85+
/* use Horner's rule to calculate cubic polynomial */
86+
x = p2 - p1;
87+
y2[i] = ((a3y*x + a2y)*x + a1y)*x + a0y;
88+
}
89+
}
90+

catmullrom.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef CATMULLROM_H
2+
#define CATMULLROM_H
3+
4+
5+
void catmullRom(float *x1, float *y1, int len1, float *x2, float *y2, int len2);
6+
7+
8+
#endif

merge.cpp

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/******************************************************************/
2+
/*Merging three channel's BW format images into one RGB color image */
3+
/*File name: merge.c */
4+
/*Author: Yurong Sun, Joseph Pan */
5+
/*Description: */
6+
/* Read greyscale BW format image file */
7+
/* Create color SGI format image file */
8+
/******************************************************************/
9+
#include <iostream>
10+
11+
using namespace std;
12+
13+
#define IXSIZE 400
14+
#define IYSIZE 400
15+
#define IZSIZE 3
16+
17+
void putbyte(FILE *outf, unsigned char val)
18+
{
19+
unsigned char buf[1];
20+
21+
buf[0] = val;
22+
fwrite(buf,1,1,outf);
23+
}
24+
25+
void putshort(FILE *outf, unsigned short val)
26+
{
27+
unsigned char buf[2];
28+
29+
buf[0] = (val>>8);
30+
buf[1] = (val>>0);
31+
fwrite(buf,2,1,outf);
32+
}
33+
34+
static int putlong(FILE *outf, unsigned long val)
35+
{
36+
unsigned char buf[4];
37+
38+
buf[0] = (val>>24);
39+
buf[1] = (val>>16);
40+
buf[2] = (val>>8);
41+
buf[3] = (val>>0);
42+
return fwrite(buf,4,1,outf);
43+
}
44+
45+
unsigned short getshort(FILE *inf)
46+
{
47+
unsigned char buf[2];
48+
fread(buf, 2, 1, inf);
49+
return (buf[0]<<8) + (buf[1]<<0);
50+
}
51+
52+
static long getlong(FILE *inf)
53+
{
54+
unsigned char buf[4];
55+
fread(buf, 4, 1, inf);
56+
return (buf[0]<<24) + (buf[1]<<16)+ (buf[2]<<8) + (buf[3]<<0);
57+
}
58+
59+
void main(int argc, char **argv)
60+
{
61+
FILE *of, *inf;
62+
char iname[80];
63+
64+
int i, x, y,z;
65+
unsigned short ixsi, iysi, izsi;//, ixsi1, iysi1;
66+
67+
unsigned char inbuf[IXSIZE][IYSIZE][IZSIZE];
68+
69+
// make sure the user invokes this program properly
70+
if(argc != 5) {
71+
fprintf(stderr, "Usage: merge red.bw green.bw blue.bw out.rgb\n");
72+
exit(1);
73+
}
74+
75+
/* Read the input BW files*/
76+
77+
// red channel
78+
if ((inf = fopen(argv[1], "rb"))==NULL)
79+
{
80+
fprintf(stderr, "readImage: Can't open file %s\n", argv[1]);
81+
exit(1);
82+
}
83+
else
84+
{
85+
//ixsi1 = getshort(inf);
86+
ixsi = getshort(inf); // width
87+
//iysi1 = getshort(inf); // height
88+
iysi = getshort(inf);
89+
90+
for (y=0; y < iysi; y++)
91+
{
92+
for (x=0; x < ixsi; x++)
93+
{
94+
fread(&inbuf[x][y][0], 1, 1, inf);
95+
}
96+
}
97+
}
98+
fclose(inf);
99+
100+
// green channel
101+
if ((inf = fopen(argv[2], "rb"))==NULL)
102+
{
103+
fprintf(stderr, "readImage: Can't open file %s\n", argv[1]);
104+
exit(1);
105+
}
106+
else
107+
{
108+
//ixsi1 = getshort(inf);
109+
ixsi = getshort(inf);
110+
//iysi1 = getshort(inf);
111+
iysi = getshort(inf);
112+
113+
for (y=0; y < iysi; y++)
114+
{
115+
for (x=0; x < ixsi; x++)
116+
{
117+
fread(&inbuf[x][y][1], 1, 1, inf);
118+
}
119+
}
120+
}
121+
fclose(inf);
122+
123+
// blue channel
124+
if ((inf=fopen(argv[3], "rb"))==NULL)
125+
{
126+
fprintf(stderr, "readImage: Can't open file %s\n", argv[1]);
127+
exit(1);
128+
}
129+
else
130+
{
131+
//ixsi1 = getshort(inf);
132+
ixsi = getshort(inf);
133+
//iysi1 = getshort(inf);
134+
iysi = getshort(inf);
135+
136+
for (y=0; y < iysi; y++)
137+
{
138+
for (x=0; x < ixsi; x++)
139+
{
140+
fread(&inbuf[x][y][2], 1, 1, inf);
141+
}
142+
}
143+
}
144+
fclose(inf);
145+
146+
/*Begin to write output files */
147+
of = fopen(argv[4],"wb");
148+
if(!of) {
149+
fprintf(stderr,"sgiimage: can't open output file\n");
150+
exit(1);
151+
}
152+
putshort(of,474); /* MAGIC */
153+
putbyte(of,0); /* STORAGE is VERBATIM */
154+
putbyte(of,1); /* BPC is 1 */
155+
putshort(of,2); /* DIMENSION is 2 */
156+
putshort(of,ixsi); /* XSIZE */
157+
putshort(of,iysi); /* YSIZE */
158+
putshort(of,3); /* ZSIZE */
159+
putlong(of,0); /* PIXMIN is 0 */
160+
putlong(of,255); /* PIXMAX is 255 */
161+
for(i=0; i<4; i++) /* DUMMY 4 bytes */
162+
putbyte(of,0);
163+
strcpy(iname,"No Name");
164+
fwrite(iname,80,1,of); /* IMAGENAME */
165+
putlong(of,0); /* COLORMAP is 0 */
166+
for(i=0; i<404; i++) /* DUMMY 404 bytes */
167+
putbyte(of,0);
168+
169+
for (z=0; z < 3; z++)
170+
{
171+
for(y=0; y<iysi; y++)
172+
{
173+
for(x=0; x<ixsi; x++)
174+
{
175+
fwrite(&inbuf[x][y][z],1,1,of);
176+
}
177+
}
178+
}
179+
180+
fclose(of);
181+
}

0 commit comments

Comments
 (0)