static int qgs_write_endstop(xmlTextWriter *writer, const char *name, qgs_entry_t *entry) { if (xmlTextWriterStartElement(writer, BAD_CAST "prop") < 0 ) { btrace("error from open prop"); return 1; } if (qgs_attribute(writer, "k", name, "prop") != 0) return 1; char *stop; if ((stop = end_stop(entry)) == NULL) { btrace("error creating endstop"); return 1; } if (qgs_attribute(writer, "v", stop, "prop") != 0) return 1; free(stop); if (xmlTextWriterEndElement(writer) < 0) { btrace("error from close prop"); return 1; } return 0; }
static int qgs_write_qgis(xmlTextWriter *writer, qgs_t *qgs) { if (xmlTextWriterStartElement(writer, BAD_CAST "qgis_style") < 0 ) { btrace("error from open qgis"); return 1; } if (qgs_attribute(writer, "version", "1", "qgis_style") != 0) return 1; if (qgs_write_symbols(writer, qgs) != 0) { btrace("failed to write symbols"); return 1; } if (qgs_write_colorramps(writer, qgs) != 0) { btrace("failed to write colorramps"); return 1; } if (xmlTextWriterEndElement(writer) < 0) { btrace("error from close qgis"); return 1; } return 0; }
static int qgs_write_mem(xmlTextWriter *writer, qgs_t *qgs) { if (qgs->n < 2) { btrace("not enough stops for a gradient (%zi)", qgs->n); return 1; } if (xmlTextWriterWriteDTD(writer, BAD_CAST "qgis_style", NULL, NULL, NULL) < 0) { btrace("error writing doctype"); return 1; } if (qgs_write_qgis(writer, qgs) != 0) { btrace("failed to write qgis"); return 1; } if (xmlTextWriterEndDocument(writer) < 0) { btrace("error from end document"); return 1; } return 0; }
static int qgs_write_colorramp(xmlTextWriter *writer, qgs_t *qgs) { if (xmlTextWriterStartElement(writer, BAD_CAST "colorramp") < 0 ) { btrace("error from open coloramp"); return 1; } if (qgs_attribute(writer, "type", "gradient", "colorramp") != 0) return 1; if (qgs_attribute(writer, "name", qgs->name, "colorramp") != 0) return 1; if (qgs_write_colorramp_props(writer, qgs) != 0) return 1; if (xmlTextWriterEndElement(writer) < 0) { btrace("error from close colorramp"); return 1; } return 0; }
static int pssvg_convert_ith(grd5_t *grd5, size_t i, gstack_t *gstack, pssvg_opt_t opt) { grd5_grad_t *grd5_grad = grd5->gradients + i; svg_t *svg; int err = 0, pushed = 0; switch (grd5_grad->type) { case GRD5_GRAD_CUSTOM: if ((svg = svg_new()) == NULL) err++; else { if (pssvg_title(grd5_grad, svg, opt, i+1) != 0) err++; else { if (pssvg_convert_one(&(grd5_grad->u.custom), svg, opt) != 0) { /* failure to convert is not an error */ btrace("failed convert of gradient %zi", i); } else { if (gstack_push(gstack, &svg) != 0) { btrace("error pushing result to stack"); err++; } else { pushed++; } } } if (! pushed) svg_destroy(svg); } break; case GRD5_GRAD_NOISE: btrace("no conversion of (noise) gradient %zi", i); break; default: btrace("bad type (%i) for gradient %zi", grd5_grad->type, i); } return err; }
static int cpt_write_stream(FILE *stream, cpt_t *cpt) { char zfmt[ZFMTLEN]; if (common_format(cpt, zfmt) != 0) { btrace("failed to build z-format string"); return 1; } const char *model; switch (cpt->model) { case model_rgb : model = "RGB"; break; case model_hsv : model = "HSV"; break; default : btrace("cpt file corrupt : bad model specified"); return 1; } time_t t = time(NULL); fprintf(stream, "# autogenerated GMT palette"); if ((cpt->name) && (strlen(cpt->name) > 0)) fprintf(stream, " \"%s\"", cpt->name); fprintf(stream, "\n"); fprintf(stream, "# cptutils version %s, %s", VERSION, ctime(&t)); fprintf(stream, "# COLOR_MODEL = %s\n", model); cpt_seg_t *seg = cpt->segment; /* needs to write annotation too */ while (seg) { fprintf_cpt_sample(stream, seg->lsmp, cpt->model, zfmt); fprintf(stream, " "); fprintf_cpt_sample(stream, seg->rsmp, cpt->model, zfmt); if (seg->label) fprintf(stream, " ; %s", seg->label); fprintf(stream, "\n"); seg = seg->rseg; } fprintf_cpt_aux(stream, 'B', cpt->bg, cpt->model); fprintf_cpt_aux(stream, 'F', cpt->fg, cpt->model); fprintf_cpt_aux(stream, 'N', cpt->nan, cpt->model); return 0; }
extern int cpt_write(const char *outfile, cpt_t *cpt) { int err; if (outfile) { FILE *stream = fopen(outfile, "wb"); if (stream) { fprintf(stream, "# %s\n", outfile); err = cpt_write_stream(stream, cpt); fclose(stream); } else { btrace("failed to open %s : %s", outfile, strerror(errno)); err = 1; } } else err = cpt_write_stream(stdout, cpt); return err; }
static int qgs_write_symbols(xmlTextWriter *writer, qgs_t *qgs) { if (xmlTextWriterStartElement(writer, BAD_CAST "symbols") < 0 ) { btrace("error from open symbols"); return 1; } if (xmlTextWriterEndElement(writer) < 0) { btrace("error from close symbols"); return 1; } return 0; }
extern double colour_rgb_dist(colour_t a, colour_t b, model_t model) { double da[3] = {0}, db[3] = {0}, sum; int i; switch (model) { case model_rgb : rgb_to_rgbD(a.rgb, da); rgb_to_rgbD(b.rgb, db); break; case model_hsv : hsv_to_rgbD(a.hsv, da); hsv_to_rgbD(b.hsv, db); break; default: btrace("no such colour model"); return -1.0; } for (sum=0.0, i=0 ; i<3 ; i++) { double d = da[i] - db[i]; sum += d*d; } return sqrt(sum); }
static int svgx_list(svgx_opt_t opt, svg_list_t *list) { int n,err=0; n = svg_list_size(list); if (opt.verbose) { if (n==0) { /* worrying, but not an error */ printf("no gradient found!\n"); } else { printf("found %i gradient%s\n", n, (n==1 ? "" : "s")); err = svg_list_iterate(list, (int (*)(svg_t*,void*))svg_id," %s\n"); } } else { err = svg_list_iterate(list, (int (*)(svg_t*,void*))svg_id, "%s\n"); } if (err) btrace("error listing svg"); return err; }
// must be called with mutex locked void *size_region(int nr,int diff) { int change; unsigned char *start; if (nr<0 || nr>=32) return NULL; region[nr].usedsize+=diff; start=reg_addr[nr].start; //(void*)(0x50000000+(nr*24*1024*1024)); !!!!!!!!!! change=region[nr].usedsize-region[nr].havesize; if (change>=0) change=((change+4095)/4096)*4096; else change=(change/4096)*4096; if (region[nr].havesize+change>=reg_addr[nr].size) { elog("trying to allocate too much memory in region %d. terminating.",nr); btrace("memory"); kill(getpid(),11); } if (change>0 && mmap(start+region[nr].havesize,change,PROT_READ|PROT_WRITE,MAP_ANON|MAP_PRIVATE,0,region[nr].havesize)==(void*)(-1)) return 0; region[nr].havesize+=change; mmem_usage+=change; if (change<0 && munmap(start+region[nr].havesize,-change)) return NULL; region[nr].top=start+region[nr].usedsize; return start+region[nr].usedsize-diff; }
int questlog_done(int cn,int qnr) { struct quest *quest; int cnt,val; if (qnr<0 || qnr>=MAXQUEST) { elog("trying to set done for quest %d",qnr); btrace("questlog"); return 0; } if (!(quest=set_data(cn,DRD_QUESTLOG_PPD,sizeof(struct quest)*MAXQUEST))) return 0; cnt=quest[qnr].done++; quest[qnr].flags=QF_DONE; val=questlog_scale(cnt,questlog[qnr].exp); // scale down by level for those rushing ahead if (ch[cn].level>44) val=min(level_value(ch[cn].level)/6,val); else if (ch[cn].level>19) val=min(level_value(ch[cn].level)/4,val); else if (ch[cn].level>4) val=min(level_value(ch[cn].level)/2,val); else val=min(level_value(ch[cn].level),val); give_exp(cn,val); if (questlog[qnr].exp>0) dlog(cn,0,"Received %d exp for doing quest %s for the %d. time (nominal value %d exp)",val,questlog[qnr].name,(cnt+1),questlog[qnr].exp); sendquestlog(cn,ch[cn].player); return quest[qnr].done; }
static fill_stack_t* xyread1i(FILE* stream, char* buf, int n) { fill_stack_t* f; int i; do if (fgets(buf, BUFSIZE, stream) == NULL) return NULL; while (skipline(buf)); if (sscanf(buf, "%d", &i) != 1) { btrace("bad line: %s", buf); return NULL; } if ((f = malloc(sizeof(fill_stack_t))) == NULL) return NULL; f->fill.type = fill_grey; f->fill.u.grey = i; f->val = n; f->next = xyread1i(stream, buf, n+1); return f; }
static fill_stack_t* xyread4f(FILE* stream, char* buf) { fill_stack_t* f; double r, g, b; double z; do if (fgets(buf, BUFSIZE, stream) == NULL) return NULL; while (skipline(buf)); if (sscanf(buf, "%lf %lf %lf %lf", &z, &r, &g, &b) != 4) { btrace("bad line: %s", buf); return NULL; } if ((f = malloc(sizeof(fill_stack_t))) == NULL) return NULL; f->fill.type = fill_colour; f->val = z; f->fill.u.colour.rgb.red = colour8(r); f->fill.u.colour.rgb.green = colour8(g); f->fill.u.colour.rgb.blue = colour8(b); f->next = xyread4f(stream, buf); return f; }
static int cptclip_convert(cpt_t* cpt,cptclip_opt_t opt) { if (cpt->segment == NULL) { btrace("cpt has no segments"); return 1; } if (opt.segments) { int i,nseg = cpt_nseg(cpt); for (i=1 ; i<opt.u.segs.min ; i++) { cpt_seg_t* s = cpt_pop(cpt); cpt_seg_destroy(s); } for (i=nseg ; i>opt.u.segs.max ; i--) { cpt_seg_t* s = cpt_shift(cpt); cpt_seg_destroy(s); } return 0; } return ( cpt_increasing(cpt) ? cptclip_z_inc(cpt,opt) : cptclip_z_dec(cpt,opt) ); }
static fill_stack_t* xyread3i(FILE* stream, char* buf, int n) { fill_stack_t* f; int r, g, b; do if (fgets(buf, BUFSIZE, stream) == NULL) return NULL; while (skipline(buf)); if (sscanf(buf, "%d %d %d", &r, &g, &b) != 3) { btrace("bad line: %s", buf); return NULL; } if ((f = malloc(sizeof(fill_stack_t))) == NULL) return NULL; f->fill.type = fill_colour; f->val = n; f->fill.u.colour.rgb.red = r; f->fill.u.colour.rgb.green = g; f->fill.u.colour.rgb.blue = b; f->next = xyread3i(stream, buf, n+1); return f; }
static fill_stack_t* xyread2f(FILE* stream, char* buf) { fill_stack_t* f; double v, d; do if (fgets(buf, BUFSIZE, stream) == NULL) return NULL; while (skipline(buf)); if (sscanf(buf, "%lf %lf", &v, &d) != 2) { btrace("bad line: %s", buf); return NULL; } if ((f = malloc(sizeof(fill_stack_t))) == NULL) return NULL; f->fill.type = fill_grey; f->fill.u.grey = colour8(d); f->val = v; f->next = xyread2f(stream, buf); return f; }
static char* join_strings(char **strings, size_t n, char sep) { size_t sz = n; for (size_t i = 0 ; i < n ; i++) sz += strlen(strings[i]); char *buffer; if ((buffer = malloc(sz)) != NULL) { char *p = buffer; size_t k; int err = 0; if ((k = snprintf(buffer, sz, "%s", strings[0])) >= sz) err++; for (int i = 1 ; i < n ; i++) { p += k; sz -= k; if ((k = snprintf(p, sz, "%c%s", sep, strings[i])) >= sz) err++; } if (err == 0) return buffer; btrace("string truncation"); free(buffer); } return NULL; }
extern int cpthsv(char* infile,char* outfile,cpthsv_opt_t opt) { cpt_t *cpt; int err=0; if ((cpt = cpt_new()) != NULL) { if (cpt_read(infile, cpt) == 0) { if (cpthsv_convert(cpt, opt) == 0) { if (cpt_write(outfile, cpt) == 0) { /* success */ } else { btrace("error writing cpt struct"); err = 1; } } else { btrace("failed to convert"); err = 1; } } else { btrace("failed to load cpt from %s", (infile ? infile : "<stdin>")); err = 1; } cpt_destroy(cpt); } else { btrace("failed to create cpt struct"); err = 1; } if (err) btrace("failed to write cpt to %s", (outfile ? outfile : "<stdout>")); return err; }
extern int svgx(svgx_opt_t opt) { int err = 0; svg_list_t *list; if ((list = svg_list_new()) == NULL) { btrace("error creating new list"); err++; } else { if (svg_read(opt.input.file, list) != 0) { btrace("error reading svg"); err++; } else { switch (opt.job) { case job_list: err = svgx_list(opt, list); break; case job_first: err = svgx_first(opt, list); break; case job_named: err = svgx_named(opt, list); break; case job_all: err = svgx_all(opt, list); break; default: return 1; } } svg_list_destroy(list); } return err; }
static int cpthsv_convert(cpt_t* cpt,cpthsv_opt_t opt) { /* check we have at least one cpt segment */ if (cpt->segment == NULL) { btrace("cpt has no segments"); return 1; } /* check colour model */ if (cpt->model != model_rgb) { btrace("cannot process non-rgb palette, sorry"); return 1; } /* convert cpt segments */ cpt_seg_t *seg; int n,m; for (n=0,m=0,seg=cpt->segment ; seg ; seg=seg->rseg,n++) { if (seg->lsmp.fill.type != seg->rsmp.fill.type) { btrace("sorry, can't convert mixed fill types"); return 1; } if (seg->lsmp.fill.type == fill_colour) { m++; rgb_transform(&(seg->lsmp.fill.u.colour.rgb),opt); rgb_transform(&(seg->rsmp.fill.u.colour.rgb),opt); } } if (opt.verbose) printf("transformed %i/%i segments\n",m,n); return 0; }
static int pssvg_convert(grd5_t *grd5, svgset_t *svgset, pssvg_opt_t opt) { int n = grd5->n; gstack_t *gstack; if ((gstack = gstack_new(sizeof(svg_t*), n, 1)) == NULL) return 1; int err = pssvg_convert_all(grd5, svgset, gstack, opt); if (! err) { size_t m = gstack_size(gstack); if (m == 0) { btrace("no gradients converted"); err++; } else { if (m < n) btrace("only %zd/%d gradient converted", m, n); if (gstack_reverse(gstack) != 0) err++; else { svgset->n = m; if ((svgset->svg = malloc(m*sizeof(svg_t*))) == NULL) err++; else { for (size_t i = 0 ; i < m ; i++) gstack_pop(gstack, svgset->svg+i); } } } } gstack_destroy(gstack); return err; }
static int cpt_append_err(cpt_seg_t* seg, cpt_t* cpt) { if (cpt_append(seg, cpt) != 0) { btrace("error adding segment"); return 1; } return 0; }
extern int grdxsvg(gstack_t *rgb_stops, gstack_t *op_stops, svg_t *svg) { gstack_t *merged_stops; if ((merged_stops = merge(rgb_stops, op_stops)) == NULL) { btrace("no merged stops"); return 1; } int err = (merged_svg(merged_stops, svg) != 0); if (err) btrace("failed conversion of merged stops to svg"); gstack_destroy(merged_stops); return err; }
int questlog_count(int cn,int qnr) { struct quest *quest; if (qnr<0 || qnr>=MAXQUEST) { elog("trying to get count for quest %d",qnr); btrace("questlog"); return 0; } if (!(quest=set_data(cn,DRD_QUESTLOG_PPD,sizeof(struct quest)*MAXQUEST))) return 0; return quest[qnr].done; }
static int svgx_all(svgx_opt_t opt, svg_list_t *list) { /* coerce explicit */ if (svg_list_iterate(list, svg_explicit2, NULL) != 0) { btrace("failed coerce explicit"); return 1; } /* coerce flat */ if (flatten_type(opt.type)) { if (svg_list_iterate(list, (int (*)(svg_t*, void*))svg_flatten2, &(opt.format.alpha)) != 0) { btrace("failed coerce explicit"); return 1; } } int (*dump)(svg_t*, void*); if ((dump = (int (*)(svg_t*, void*))dump_type(opt.type)) == NULL) return 1; if (opt.verbose) printf("converting all gradients:\n"); if (svg_list_iterate(list, dump, &opt) != 0) { btrace("failed writing all gradients"); return 1; } return 0; }
static int svgx_single(svgx_opt_t opt, svg_t *svg) { const char *file; if (svg_explicit(svg) != 0) { btrace("failed adding explicit stops"); return 1; } file = opt.output.file; if (flatten_type(opt.type)) { if (svg_flatten(svg, opt.format.alpha) != 0) { btrace("failed to flatten transparency"); return 1; } } dump_f dump; if ((dump = dump_type(opt.type)) == NULL) return 1; if (dump(svg, &opt) != 0) { btrace("failed conversion"); return 1; } if (opt.verbose) printf("wrote %s to %s\n", (opt.name ? opt.name : "gradient"), (file ? file : "<stdout>")); return 0; }
static int qgs_write_colorramps(xmlTextWriter *writer, qgs_t *qgs) { if (xmlTextWriterStartElement(writer, BAD_CAST "colorramps") < 0 ) { btrace("error from open colorramps"); return 1; } if (qgs_write_colorramp(writer, qgs) != 0) { btrace("failed to write colorramp"); return 1; } if (xmlTextWriterEndElement(writer) < 0) { btrace("error from close colorramps"); return 1; } return 0; }
static int qgs_write_midstops(xmlTextWriter *writer, qgs_t *qgs) { int n = qgs->n; if (n < 3) return 0; if (xmlTextWriterStartElement(writer, BAD_CAST "prop") < 0 ) { btrace("error from open prop"); return 1; } if (qgs_attribute(writer, "k", "stops", "prop") != 0) return 1; char *str; if ((str = mid_stops_string(qgs)) == NULL) return 1; int err = 0; if (qgs_attribute(writer, "v", str, "prop") != 0) { btrace("error writing attribute"); err++; } free(str); if (xmlTextWriterEndElement(writer) < 0) { btrace("error from close prop"); err++; } return err > 0; }
static int svgx_named(svgx_opt_t opt, svg_list_t *list) { svg_t *svg; if ((svg = svg_list_select(list, (int (*)(svg_t*,void*))svg_select_name, opt.name)) == NULL) { btrace("couldn't find gradient named %s", opt.name); return 1; } return svgx_single(opt, svg); }