1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/* main.c : morph main program
//
// A GUI or command line user interface to a mesh warping algorithm
//
Written and Copyright (C) 1994-2000 by Michael J. Gourlay
This file is part of Xmorph.
Xmorph is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Xmorph is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Xmorph; see the file LICENSE. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mesh.h"
#include "RgbaImage.h"
#ifdef X_GUI
#include <X11/Intrinsic.h>
#include "xmorph.h"
#endif
#include "../xmorph/main.h"
#define cl_match_arg(cmd) (!strcmp(argv[apc], cmd) && ((apc+1)<argc))
#define XT_OPTIONS "[Xt options]"
#define OPTIONS "\n" \
" options: (default value is in parentheses)\n" \
" -start starting_image_file (test image)\n" \
" -finish finishing_image_file (test image)\n" \
" -out output_image_file (warp0000.tga)\n" \
" -src source_mesh_file (uniform grid)\n" \
" -dst destination_mesh_file (uniform grid)\n" \
" -mt morph_tween (0) [between 0 and 1]\n" \
" -dt dissolve_tween (0) [between 0 and 1]\n"
#define NON_GUI_OPTIONS \
" -skip_warp (false)\n"
#ifdef X_GUI
#define USAGE "usage: %s [options] " XT_OPTIONS OPTIONS
#else
#define USAGE "usage: %s [options] " OPTIONS NON_GUI_OPTIONS
#endif
#ifndef FALSE
#define FALSE 0
#endif
int verbose = FALSE;
/* Global "original" images */
RgbaImageT orig_image[NUM_ORIG_IMAGES];
static float
sigmoid(float x)
{
static const float SHARPNESS = 2.0;
float as = atan(SHARPNESS);
return (atan((x - 0.5) * SHARPNESS * 2.0) + as) / (2.0 * as);
}
int
main(int argc, char **argv)
{
char *src_img_fn = NULL;
char *dst_img_fn = NULL;
char *src_mesh_fn = NULL;
char *dst_mesh_fn = NULL;
char *out_img_fn = "warp0000.tga";
RgbaImageT *src_imgP = &orig_image[0];
RgbaImageT *dst_imgP = &orig_image[1];
float morph_tween = 0.0;
float dissolve_tween = 0.0;
#ifndef X_GUI
int skip_warp = 0;
#endif
int apc;
#ifdef SUNOS
malloc_debug(2);
#endif
rgbaImageInit(src_imgP);
rgbaImageInit(dst_imgP);
#ifdef NEED_GIMP
{
GPrintFunc old_print_func;
int result;
extern void null_print_func(gchar *);
/* Temporarily install a print function that discards all output.
This is to avoid annoying "you must run this program under
gimp" messages when xmorph gets invoked in stand-alone
mode. */
old_print_func = g_set_print_handler (null_print_func);
/* gimp_main () returns 1 if xmorph wasn't invoked by GIMP */
result = gimp_main (argc, argv);
g_set_message_handler (old_print_func);
if (result == 0)
exit (0);
}
#endif
for(apc=1; apc < argc; apc++) {
if(argv[apc][0] != '-') {
} else {
if(cl_match_arg("-start")) {
src_img_fn = argv[++apc];
} else if(cl_match_arg("-finish")) {
dst_img_fn = argv[++apc];
} else if(cl_match_arg("-out")) {
out_img_fn = argv[++apc];
} else if (cl_match_arg("-src")) {
src_mesh_fn = argv[++apc];
} else if (cl_match_arg("-dst")) {
dst_mesh_fn = argv[++apc];
} else if (cl_match_arg("-mt")) {
morph_tween = atof(argv[++apc]);
} else if (cl_match_arg("-dt")) {
dissolve_tween = atof(argv[++apc]);
if(dissolve_tween < 0.0) {
dissolve_tween = sigmoid(-dissolve_tween);
}
} else if(!strcmp(argv[apc], "-verbose")) {
verbose ++;
fprintf(stderr, "%s: verbose reporting\n", argv[0]);
#ifndef X_GUI
} else if(!strcmp(argv[apc], "-skip_warp")) {
skip_warp = 1;
#endif
} else
#ifdef X_GUI
if(!strcmp(argv[apc], "-help"))
#endif
{
fprintf(stderr, USAGE, argv[0]);
return 1;
}
}
}
if(src_img_fn==NULL && dst_img_fn!=NULL) {
fprintf(stderr,
"%s: must have start image if finish image is given.\n", argv[0]);
fprintf(stderr, USAGE, argv[0]);
return 1;
}
/* Load the source image or create a test pattern image */
if(src_img_fn != NULL) {
if(rgbaImageRead(src_imgP, src_img_fn)) {
fprintf(stderr, "%s: could not open src image '%s'\n",
argv[0], src_img_fn);
return 1;
}
} else {
/* Create test pattern image.
//
// The unusual size of 319x239 is chosen specifically because it
// will force xmorph to perform some sort of resizing as soon as the
// user loads in a mesh or an image. The size 320x240 is very common
// and if no resizing was done by xmorph then I would not have a
// thorough test of its resizing routines.
*/
rgbaImageAlloc(src_imgP, 319, 239);
rgbaImageTestCreate(src_imgP, 2);
}
/* Load the destination image */
if(dst_img_fn != NULL) {
if(rgbaImageRead(dst_imgP, dst_img_fn)) {
fprintf(stderr, "%s: could not open dst image '%s'\n",
argv[0], src_img_fn);
return 1;
}
/* Make sure images are the same shape */
if((dst_imgP->ncols != src_imgP->ncols)
|| (dst_imgP->nrows != src_imgP->nrows))
{
fprintf(stderr, "%s: images are not the same size\n", argv[0]);
return 1;
}
} else {
rgbaImageAlloc(dst_imgP, src_imgP->ncols, src_imgP->nrows);
rgbaImageTestCreate(dst_imgP, 1);
}
#ifdef X_GUI
{
XtAppContext app;
app = initialize_application(src_imgP->ncols, src_imgP->nrows,
src_mesh_fn, dst_mesh_fn, &argc, argv, TRUE);
XtAppMainLoop(app);
}
#else
/* Command line interface */
if(verbose) fprintf(stderr, "morph_tween : %g\n", morph_tween);
if(verbose) fprintf(stderr, "dissolve_tween: %g\n", dissolve_tween);
{
MeshT *src_mesh = meshNew(4,4);
MeshT *dst_mesh = meshNew(4,4);
if (NULL != src_mesh_fn) {
if(meshRead(src_mesh, src_mesh_fn)) {
fprintf(stderr, "%s: ERROR: failed to read src mesh '%s'\n",
argv[0], src_mesh_fn);
rgbaImageFree(src_imgP); rgbaImageFree(dst_imgP);
meshDelete(src_mesh); meshDelete(dst_mesh);
return 1;
}
} else {
meshReset(src_mesh, src_imgP->ncols, src_imgP->nrows);
}
meshScale(src_mesh, src_imgP->ncols, src_imgP->nrows);
if (NULL != dst_mesh_fn) {
if(meshRead(dst_mesh, dst_mesh_fn)) {
fprintf(stderr, "%s: ERROR: failed to read dst mesh '%s'\n",
argv[0], dst_mesh_fn);
rgbaImageFree(dst_imgP); rgbaImageFree(dst_imgP);
meshFree(src_mesh);
meshDelete(src_mesh); meshDelete(dst_mesh);
return 1;
}
} else {
meshReset(dst_mesh, dst_imgP->ncols, dst_imgP->nrows);
}
meshScale(dst_mesh, dst_imgP->ncols, dst_imgP->nrows);
{
RgbaImageT *src_warped = rgbaImageNew();
RgbaImageT *dst_warped = rgbaImageNew();
if(!skip_warp) {
if(verbose) printf(".");
rgbaImageWarp(src_imgP, src_warped, src_mesh, dst_mesh, morph_tween);
if(verbose) printf(".");
rgbaImageWarp(dst_imgP, dst_warped, dst_mesh, src_mesh, morph_tween);
if(verbose) printf(".");
rgbaImageWrite(out_img_fn, src_warped, dst_warped, dissolve_tween);
if(verbose) printf(".\n");
} else {
rgbaImageWrite(out_img_fn, src_imgP, dst_imgP, dissolve_tween);
}
rgbaImageFree(dst_warped); rgbaImageDelete(dst_warped);
rgbaImageFree(src_warped); rgbaImageDelete(src_warped);
}
meshBackupFree();
meshFree(src_mesh); meshDelete(src_mesh);
meshFree(dst_mesh); meshDelete(dst_mesh);
}
#endif
rgbaImageFree(src_imgP);
rgbaImageFree(dst_imgP);
return 0;
}
|