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
|
// Copyright 2004 David Hilvert <dhilvert@auricle.dyndns.org>,
// <dhilvert@ugcs.caltech.edu>
/* This file is part of the Anti-Lamenessing Engine.
The Anti-Lamenessing Engine 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 3 of the License, or
(at your option) any later version.
The Anti-Lamenessing Engine 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 the Anti-Lamenessing Engine; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __render_parse_h__
#define __render_parse_h__
#include "render.h"
#include "render/combine.h"
#include "render/invariant.h"
#include "render/incremental.h"
#include "render/zero.h"
/*
* Parse strings describing renderers, and return a renderer satisfying
* the string.
*/
class render_parse {
private:
static int strpfix(const char *a, const char *b) {
return strncmp(a, b, strlen(a));
}
static void nomem() {
fprintf(stderr, "\n\n*** Error: unable to allocate memory in render_parse. ***\n\n");
exit(1);
}
static void syntax_error(const char *explanation) {
fprintf(stderr, "\n\n*** Error: Render syntax: %s ***\n\n", explanation);
exit(1);
}
static filter::filter *get_SF_atomic(const char *type) {
double param;
if (!strcmp("sinc", type)) {
return new filter::sinc();
} else if (!strpfix("lanc:", type)) {
if (sscanf(type + strlen("lanc:"), "%lf", ¶m) != 1)
syntax_error("Unable to get lanczos diameter.");
return new filter::lanczos(param/2);
} else if (!strpfix("triangle:", type)) {
if (sscanf(type + strlen("triangle:"), "%lf", ¶m) != 1)
syntax_error("Unable to get triangle diameter.");
return new filter::triangle(param/2);
} else if (!strpfix("box:", type)) {
if (sscanf(type + strlen("box:"), "%lf", ¶m) != 1)
syntax_error("Unable to get box diameter.");
return new filter::box(param/2);
} else if (!strpfix("gauss:", type)) {
if (sscanf(type + strlen("gauss:"), "%lf", ¶m) != 1)
syntax_error("Unable to get gauss deviation.");
return new filter::gauss(param);
} else if (!strpfix("zero", type)) {
return new filter::zero();
} else {
fprintf(stderr, "get_SF_atomic type %s\n", type);
syntax_error("Unable to get filter.");
}
assert (0);
/*
* This line should never be reached; it's included to avoid
* 'unreachable' messages emitted by some compilers.
*/
return NULL;
}
static filter::filter *get_SF(const char *orig_type) {
char *type = strdup(orig_type);
if (type == NULL)
nomem();
char *star_index = (char *) type;
while (*star_index != '\0'
&& *star_index != '*')
star_index++;
if (*star_index == '\0') {
free(type);
return get_SF_atomic(orig_type);
}
*star_index = '\0';
filter::filter *result = new filter::mult(
get_SF_atomic(type),
get_SF(star_index + 1));
*star_index = '*';
free(type);
return result;
}
public:
static filter::scaled_filter *get_SSF(const char *type) {
if (!strpfix("coarse:", type)) {
return new filter::scaled_filter(get_SF(type + strlen("coarse:")), 1);
} else if (!strpfix("fine:", type)) {
return new filter::scaled_filter(get_SF(type + strlen("fine:")), 0);
} else if (!strpfix("auto:", type)) {
return new filter::scaled_filter(get_SF(type + strlen("auto:")), 2);
} else {
return new filter::scaled_filter(get_SF(type), 1);
}
}
static filter::ssfe *get_SSFE(const char *type) {
if (!strpfix("ex:", type)) {
return new filter::ssfe(get_SSF(type + strlen("ex:")), 1);
} else if (!strpfix("nex:", type)) {
return new filter::ssfe(get_SSF(type + strlen("nex:")), 0);
} else {
return new filter::ssfe(get_SSF(type), 1);
}
}
static render *get_invariant(const char *type) {
double param;
int offset;
invariant *i;
if (!strpfix("min:", type)) {
i = new invariant(get_SSFE(type + strlen("min:")));
i->set_min();
} else if (!strpfix("max:", type)) {
i = new invariant(get_SSFE(type + strlen("max:")));
i->set_max();
} else if (!strpfix("last:", type)) {
i = new invariant(get_SSFE(type + strlen("last:")));
i->set_last();
} else if (!strpfix("first:", type)) {
i = new invariant(get_SSFE(type + strlen("first:")));
i->set_first();
} else if (!strpfix("avg:", type)) {
i = new invariant(get_SSFE(type + strlen("avg:")));
i->set_avg();
} else if (!strpfix("avgf:", type)) {
if (sscanf(type + strlen("avgf:"), "%lf%n", ¶m, &offset) != 1)
syntax_error("Unable to get avgf weight criterion.");
i = new invariant(get_SSFE(type + strlen("avgf:") + offset + strlen(":")));
i->set_avgf(param);
} else if (!strpfix("median:", type)) {
i = new invariant(get_SSFE(type + strlen("median:")));
i->set_median();
} else {
i = new invariant(get_SSFE(type));
i->set_avg();
}
for (int index = 0; index < render::render_count(); index++)
if (typeid(*render::render_num(index)) == typeid(incremental)
&& i->equals(((incremental *)render::render_num(index))->get_invariant())) {
delete i;
return (incremental *)render::render_num(index);
} else if (typeid(*i->ssfe()->get_scaled_filter()->get_filter()) == typeid(filter::zero)) {
return new zero(i);
}
// fprintf(stderr, " new '%s'.\n", type);
return new incremental(i);
}
static render *get_combination(const char *ptype, const char *dtype) {
render *partial = get_invariant(ptype);
render *_default = get(dtype);
for (int index = 0; index < render::render_count(); index++)
if (typeid(*render::render_num(index)) == typeid(combine)
&& _default == ((combine *)render::render_num(index))->get_default()
&& partial == ((combine *)render::render_num(index))->get_partial()) {
return render::render_num(index);
}
// fprintf(stderr, " new '%s,%s'.\n", ptype, dtype);
return new combine(_default, partial);
}
static render *get(const char *orig_type) {
char *type = strdup(orig_type);
if (type == NULL)
nomem();
char *comma_index = (char *) type;
while (*comma_index != '\0'
&& *comma_index != ',')
comma_index++;
if (*comma_index == '\0') {
free(type);
return get_invariant(orig_type);
}
*comma_index = '\0';
render *result = get_combination(type, comma_index + 1);
*comma_index = ',';
free(type);
return result;
}
};
#endif
|