#ifdef _USRDLL #include "plugin.h" #include "import_gate_virtuoso.h" #define wi_inst (wi_instance_get()[0]) #else #include <libutil.h> #include "sqlnode.h" #include "sqlbif.h" #include "wi.h" #include "Dk.h" #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include "gd_utils.h" #include "gd/gd_lib.h" #include "shplib/shapefil.h" typedef struct img_ctx_s { int dx_; int dy_; double minx_; double miny_; double maxx_; double maxy_; double mulx_; double muly_; int black_; int red_; int green_; int blue_; int attr_; gdImagePtr img_; } img_ctx_t; caddr_t img_create_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { img_ctx_t *ptr = (img_ctx_t *)dk_alloc_box (sizeof (img_ctx_t), DV_STRING); ptr->dx_ = bif_long_arg (qst, args, 0, "img_create_proc"); ptr->dy_ = bif_long_arg (qst, args, 1, "img_create_proc"); ptr->minx_ = bif_double_arg (qst, args, 2, "img_create_proc"); ptr->miny_ = bif_double_arg (qst, args, 3, "img_create_proc"); ptr->maxx_ = bif_double_arg (qst, args, 4, "img_create_proc"); ptr->maxy_ = bif_double_arg (qst, args, 5, "img_create_proc"); ptr->attr_ = bif_long_arg (qst, args, 6, "img_create_proc"); ptr->img_ = gdImageCreateTrueColor (ptr->dx_, ptr->dy_); ptr->mulx_ = ptr->dx_/fabs(ptr->maxx_ - ptr->minx_); ptr->muly_ = ptr->dy_/fabs(ptr->maxy_ - ptr->miny_); ptr->black_ = gdImageColorAllocate (ptr->img_, 0, 0, 0); ptr->blue_ = gdImageColorAllocate (ptr->img_, 0, 0, 255); ptr->red_ = gdImageColorAllocate (ptr->img_, 255, 0, 0); ptr->green_ = gdImageColorAllocate (ptr->img_, 0, 255, 0); return (caddr_t)ptr; } caddr_t img_saveas_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { img_ctx_t *ptr = (img_ctx_t *)bif_arg (qst, args, 0, "img_saveas_proc"); gdImagePtr im = ptr->img_; caddr_t fname = bif_string_arg (qst, args, 1, "img_saveas_proc"); FILE *out = fopen (fname, "wb"); if (NULL != out) { gdImageGif (im, out); fclose (out); return (caddr_t)0; } return (caddr_t)-1; } static unsigned char clip(int value) { if (value < 0) value = 0; else if (value > 255) value = 255; return value; } caddr_t img_fromptr_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { ptrlong addr = unbox(bif_long_arg (qst, args, 0, "img_fromptr_proc")); return addr; } caddr_t img_tostr_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { img_ctx_t *ptr = (img_ctx_t *)bif_arg (qst, args, 0, "img_tostr_proc"); gdImagePtr im = ptr->img_; void *rv = NULL; caddr_t ret = NULL; int size = 0; gdIOCtx *out = gdNewDynamicCtx (2048, NULL); gdImageGifCtx (im, out); rv = gdDPExtractData (out, &size); if (NULL == rv || size <= 0) return 0; out->gd_free (out); ret = dk_alloc_box (size, DV_STRING); memcpy (ret, rv, size); gdFree(rv); return (caddr_t)box_num (ret); } caddr_t img_destroy_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { img_ctx_t *ptr = (img_ctx_t *)bif_arg (qst, args, 0, "img_destroy_proc"); gdImagePtr im = ptr->img_; gdImageDestroy(im); /*dk_free_box (ptr);*/ return 0; } caddr_t img_draw_polyline_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { img_ctx_t *ptr = (img_ctx_t *)bif_arg (qst, args, 0, "img_draw_polyline_proc"); gdImagePtr im = ptr->img_; caddr_t data = bif_string_arg (qst, args, 1, "img_draw_polyline_proc"); SHPObject *shp = SHPDeserialize (data); int nparts = shp->nParts; int npoints = shp->nVertices; double *px = shp->padfX; double *py = shp->padfY; int i, j; int color = bif_long_arg (qst, args, 2, "img_draw_polyline_proc"); gdImageSetAntiAliased (im, color); for (i = 0; i < nparts; i++) { long ps = shp->panPartStart[i]; long psz = (i==(nparts-1))? npoints-ps : shp->panPartStart[i+1]-ps; gdPointPtr points = (gdPointPtr)malloc(sizeof (gdPoint) * psz); for (j = 0; j < psz; j++) { points[j].x = (px[ps+j] - ptr->minx_) * ptr->mulx_; points[j].y = (ptr->attr_ & 1) ? ptr->dy_ - (py[ps+j] - ptr->miny_) * ptr->muly_: (py[ps+j] - ptr->miny_) * ptr->muly_; } for (j = 1; j < psz; j++) { /*if (points[j].x == points[j-1].x && points[j].y == points[j-1].y) gdImageSetPixel (im, points[j].x, points[j].y, ptr->blue_); else*/ gdImageLine (im, points[j].x, points[j].y, points[j-1].x, points[j-1].y, gdAntiAliased); } free (points); } SHPDestroyObject (shp); return 0; } caddr_t img_draw_polygone_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { img_ctx_t *ptr = (img_ctx_t *)bif_arg (qst, args, 0, "img_draw_polygone_proc"); gdImagePtr im = ptr->img_; caddr_t data = bif_string_arg (qst, args, 1, "img_draw_polygone_proc"); SHPObject *shp = SHPDeserialize (data); int nparts = shp->nParts; int npoints = shp->nVertices; double *px = shp->padfX; double *py = shp->padfY; int i,j; int color = bif_long_arg (qst, args, 2, "img_draw_polygone_proc"); int bcolor = bif_long_arg (qst, args, 3, "img_draw_polygone_proc"); for (i = 0; i < nparts; i++) { long ps = shp->panPartStart[i]; long psz = (i==(nparts-1))? npoints-ps : shp->panPartStart[i+1]-ps; gdPointPtr points = (gdPointPtr)malloc (sizeof (gdPoint) * psz); for (j = 0; j < psz; j++) { points[j].x = (px[ps+j] - ptr->minx_) * ptr->mulx_; points[j].y = (ptr->attr_ & 1) ? ptr->dy_ - (py[ps+j] - ptr->miny_) * ptr->muly_: (py[ps+j] - ptr->miny_) * ptr->muly_; } gdImageSetAntiAliased (im, color); gdImageFilledPolygon (im, points, psz, gdAntiAliased); if (bcolor >= 0) { gdImageSetAntiAliased (im, bcolor); gdImagePolygon (im, points, psz, gdAntiAliased); } free (points); } SHPDestroyObject(shp); return 0; } caddr_t img_alloc_color_proc (caddr_t * qst, caddr_t * err, state_slot_t ** args) { img_ctx_t *ptr = (img_ctx_t *)bif_arg (qst, args, 0, "img_alloc_color_proc"); gdImagePtr im = ptr->img_; int r = bif_long_arg (qst, args, 1, "img_alloc_color_proc"); int g = bif_long_arg (qst, args, 2, "img_alloc_color_proc"); int b = bif_long_arg (qst, args, 3, "img_alloc_color_proc"); return box_num(gdImageColorAllocateAlpha (ptr->img_, r, g, b, 0)); } void init_shcall_gd_utils () { bif_define ("img_create", img_create_proc); bif_define ("img_saveas", img_saveas_proc); bif_define ("img_tostr", img_tostr_proc); bif_define ("img_fromptr", img_fromptr_proc); bif_define ("img_destroy", img_destroy_proc); bif_define ("img_alloc_color", img_alloc_color_proc); bif_define ("img_draw_polyline", img_draw_polyline_proc); bif_define ("img_draw_polygone", img_draw_polygone_proc); }