1 /******************************************************************************
2 * $Id: fgisEppDraw.c,v 1.2 2003-01-04 15:51:34 dron Exp $
4 * Project: fGIS core engine
5 * Purpose: Main code for drawing functions
7 ******************************************************************************
9 * Copyright (C) 1997, Victor Wagner <vitus@ice.ru>
10 * Copyright (C) 2003, Andrey Kiselev <dron@remotesensing.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
26 *****************************************************************************
36 unsigned short int *border_buffer1, *border_buffer2, *bbuf_ptr1, *bbuf_ptr2;
38 void init_border (int width, int offsite)
41 border_buffer1 = (unsigned short int *)Tcl_Alloc (
42 (width + 1) * sizeof (unsigned short int));
43 border_buffer2 = (unsigned short int *)Tcl_Alloc (
44 (width + 1) * sizeof (unsigned short int));
45 *border_buffer2 = offsite;
46 for (i = 0, bbuf_ptr1 = border_buffer1; i <= width; i++, bbuf_ptr1++)
49 void new_border_row ()
51 unsigned short int *tmp;
53 border_buffer1 = border_buffer2;
55 bbuf_ptr1 = border_buffer1;
56 bbuf_ptr2 = border_buffer2;
58 } int check_border (int class)
60 *(++bbuf_ptr1) = class;
62 return *bbuf_ptr2 != class || *(bbuf_ptr1 - 1) !=
63 class || *(bbuf_ptr2 - 1) != class;
67 Tcl_Free ((char*)border_buffer1);
68 Tcl_Free ((char*)border_buffer2);
72 òÉÓÕÅÔ ÕËÁÚÁÎÎÙÊ ËÕÓÏË ÒÁÓÔÒÁ (× ÁÌØÔÅÒÎÁÔÉ×ÎÙÈ ËÏÏÒÄÉÎÁÔÁÈ)
75 int Fgis_MakePhotoImage (Tk_PhotoHandle imghandle,
76 int xl, int yb, int xr, int yt,
77 double XL, double XR, double YT, double YB,
79 int border,/* 0 - ÎÉÞÅÇÏ, 1, ÐÒÉ ÎÅÓÏ×ÐÁÄÅÎÉÉ
80 Ã×ÅÔÏ× ÐÏÓÌÅ ÒÅËÌÁÓÓÁ,
81 2 - ÐÒÉ ÎÅÓÏ×ÐÁÄÅÎÉÉ Ã×ÅÔÏ×
86 int mapmode /* 0 - wrap (x&0xFF), -1 x>255?255:x,
91 Tk_PhotoImageBlock block =
100 EPP *e = epp (handle);
101 RECLASS r = handle->reclass;
102 int width = xr - xl, height = yb - yt;
106 double W_Alt = XR - XL, H_Alt = YB - YT;
107 int x, y, row, i, j, maxclass=0;
111 ÜÔÏ ÂÕÆÅÒ ÄÌÑ ÈÒÁÎÅÎÉÑ ÐÒÏÒÉÓÏ×ÁÎÎÏÊ ÓÔÒÏËÉ
113 irow =(int *) Tcl_Alloc (width * sizeof (int));
114 max_base_class = epp_table_size (e) + 1;
116 íÁÓÓÉ× ×ÈÏÄÏ× × ÐÁÌÉÔÒÕ ÄÌÑ ËÌÁÓÓÏ× ÂÁÚÏ×ÏÇÏ ÒÁÓÔÒÁ
118 pal = (int *)Tcl_Alloc(max_base_class * sizeof (int));
120 maxclass = Fgis_RasterMax (handle);
121 for (i = 0; i < max_base_class; i++) {
137 index = index * mapmode / maxclass;
142 pal[i] = palette[index];
147 ÜÔÏ ÔÁÂÌÉÃÁ ÐÏÒÑÄËÏ×ÙÈ ÎÏÍÅÒÏ× ËÏÌÏÎÏË × epp-ÆÁÊÌÅ, ÓÏÏÔ×ÅÔÓÔ×ÕÀÝÉÈ
150 coltab =(int *) Tcl_Alloc (width * sizeof (int));
151 for (i = xl, col = coltab, j = 0; i < xr; i++, j++, col++) {
152 *col = epp_col (e, W_Alt * (j + 0.5) / width + XL);
155 úÁÐÏÌÎÉÍ ÔÅ ÐÏÌÑ ÓÔÒÕËÔÕÒÙ block, ËÏÔÏÒÙÅ ÍÙ ÎÅ ÍÏÇÌÉ ÉÎÉÃÉÁÌÉÚÉÒÏ×ÁÔØ
157 block.pixelPtr = (unsigned char *) irow;
159 block.pitch = width * sizeof (int);
162 init_border (width, r[e->offsite]);
164 init_border (width, e->offsite);
167 Tk_PhotoBlank(imghandle);
168 for (y = yt, i = 0; y < yb; y++, i++) {
169 row = epp_row (e, H_Alt * (i + 0.5) / height + YT);
172 for (x = xl, j = 0, col = coltab, rp = irow; x < xr;
173 j++, col++, rp++, x++) {
174 base = epp_get (e, *col, row);
176 if (border == 1 ? check_border (r[base]) :
177 check_border (base)) {
185 Tk_PhotoPutBlock (imghandle, &block, xl, y, width, 1);
187 Tk_PhotoPutBlock (imghandle, &block, xl, y, width, 1, TK_PHOTO_COMPOSITE_OVERLAY);
191 Tcl_Free ((char *)pal);
192 Tcl_Free ((char *)irow);
193 Tcl_Free ((char *)coltab);
198 void Fgis_PlotPatterns(Tcl_Interp *interp,
200 int xl,int yb,int xr,int yt,
201 double XL, double XR,double YT,double YB,
202 RASTER_OBJECT handle,
203 int bordermode, PATTERNS patterns,XColor* color,int mapmode)
204 { /* NOT IMPLEMENTED YET */
206 void Fgis_PlotSymbols(Tcl_Interp *interp,
208 int xl,int yb,int xr,int yt,
209 double XL,double XR,double YT,double YB,
210 RASTER_OBJECT handle,
211 PATTERNS patterns, XColor* color,
213 { /*NOT IMPLEMENTED YET*/
218 * - implementation of command fgisRasterColorImage and
221 EXPORT(int, Fgis_RasterImage) (ClientData data, Tcl_Interp *interp,
222 int argc, char **argv)
224 int listc; char **listv; /* for parsing result of $planchet bbox $item */
225 Tk_PhotoHandle imghandle=NULL;
226 int colorplot=(int)data; /* how to distinguish between color and bw */
227 char *bitmap_image=NULL;
228 enum {PATTERNS_MODE,SYMBOLS_MODE} symbolmode=PATTERNS_MODE;
231 int bordermode=((int)data)?0:1;
232 PATTERNS patterns=NULL;
233 PALETTE palette=default_palette;
234 RASTER_OBJECT raster;
236 int xl=0,yt=0,xr,yb,imgstartx,imgstarty;
239 Tk_Window canvas=Tk_NameToWindow(interp,argv[2],Tk_MainWindow(interp));
241 clr=Tk_GetColor(interp,canvas,"black");
242 if (Fgis_CkArgs(interp,argc<5,
243 argv[0],"raster planchet item ?options?"))
245 if (!Fgis_ValidPlanchet(interp,argv[2]))
247 if (Tcl_VarEval(interp,argv[2]," bbox ",argv[3],NULL)!=TCL_OK)
249 if (Tcl_SplitList(interp,interp->result,&listc,&listv)!=TCL_OK)
251 imgstartx=atol(listv[0]);
252 imgstarty=atol(listv[1]);
255 XL=Fgis_AltX(interp,argv[2],imgstartx);
256 XR=Fgis_AltX(interp,argv[2],xr+1);
257 YT=Fgis_AltY(interp,argv[2],imgstarty);
258 YB=Fgis_AltY(interp,argv[2],yb+1);
261 Tcl_Free((char *)listv);
262 if (Tcl_VarEval(interp,argv[2]," itemcget ",argv[3]," -image",NULL)!=TCL_OK)
265 imghandle=Tk_FindPhoto(interp,interp->result);
267 Tcl_AppendResult(interp,"No valid image for item ",argv[3],
268 " in planchet ", argv[2],NULL);
273 bitmap_image=stralloc(interp->result);
275 Tcl_AppendResult(interp,"No valid image for item ",argv[3],
276 " in planchet ", argv[2],NULL);
280 raster=Fgis_GetRaster(interp,argv[1]);
282 Tcl_AppendResult(interp,argv[1]," is not valid raster",NULL);
286 for(i=4;i<argc;i++) {
287 if (!strcmp(argv[i],"-palette")&&colorplot) {
288 if(!(palette=Fgis_GetPalette(interp,argv[++i])))
290 } else if (!strcmp(argv[i],"-patterns")&&!colorplot) {
291 if(!(patterns=Fgis_GetPatterns(interp,argv[++i])))
293 symbolmode=PATTERNS_MODE;
294 } else if (!strcmp(argv[i],"-symbols")&&!colorplot) {
295 if (!(patterns=Fgis_GetPatterns(interp,argv[++i])))
297 symbolmode=SYMBOLS_MODE;
299 } else if (!strcmp(argv[i],"-border")) {
302 if (!strcmp(argv[i],"base")) {
304 } else if (!strcmp(argv[i],"yes")) {
306 } else if (!strcmp(argv[i],"none")) {
308 } else if (argv[i][0]=='-') {
309 /* -border without parameter decrease i */
313 Tcl_SetResult(interp,"Invalid border option. Should be one "
314 "of none, yes, base",TCL_STATIC);
321 if (!strcmp(argv[i],"-color")) {
322 clr=Tk_GetColor(interp,canvas,argv[++i]);
323 if (!clr) return TCL_ERROR;
324 /* What range have XColor fields? */
326 bordercolor=((clr->red&0xff)<<16)|((clr->green&0xff)<<8)|
330 if (!strcmp(argv[i],"-map")) {
332 ERROR_MESSAGE("Duplicate color remapping specification",TCL_STATIC);
334 if (!strcmp(argv[i],"wrap")) {
337 if (!strcmp(argv[i],"none")) {
340 if (Fgis_GetInt(interp, argc, argv, ++i, 0, 256, &mapmode,
341 "color index") !=TCL_OK) {
345 if (!strcmp(argv[i],"-update")) {
349 ERROR_MESSAGE("List of four doubles expected",TCL_STATIC);
350 if (Fgis_GetLimits(interp,argv[i],&X1,&Y1,&X2,&Y2)!=TCL_OK)
352 x1=Fgis_PlanchetX(interp,argv[2],X1)-imgstartx;
353 x2=Fgis_PlanchetX(interp,argv[2],X2)-imgstartx;
358 if (x1>0) {xl=x1;XL=Fgis_AltX(interp,argv[2],x1+imgstartx);}
359 if (x2<xr) {xr=x2;XR=Fgis_AltX(interp,argv[2],x2+1+imgstartx);}
360 y1=Fgis_PlanchetY(interp,argv[2],Y1)-imgstarty;
361 y2=Fgis_PlanchetY(interp,argv[2],Y2)-imgstarty;
366 if (y1>0) {yt=y1;YT=Fgis_AltY(interp,argv[2],y1+imgstarty);}
367 if (y2<yb) {yb=y2;YB=Y2;Fgis_AltY(interp,argv[2],y2+1+imgstarty);}
370 Tcl_ResetResult(interp);
371 Tcl_AppendResult(interp,"Invalid option.\"",argv[i],
372 "\" Should be one of -border, -bordercolor, -map, ",
375 Tcl_AppendResult(interp,"-palette",NULL);
377 Tcl_AppendResult(interp,"-patterns, -symbols",NULL);
378 Tcl_AppendResult(interp,", -update",NULL);
385 Fgis_MakePhotoImage(imghandle,xl,yb,xr,yt,XL,XR,YT,YB,raster,bordermode,
386 palette,bordercolor,mapmode);
388 if (symbolmode==PATTERNS_MODE) {
389 Fgis_PlotPatterns(interp,bitmap_image,xl,yb,xr,yt,XL,XR,YT,YB,raster,
390 bordermode, patterns,clr,mapmode);
392 Fgis_PlotSymbols(interp,bitmap_image,xl,yb,xr,yt,XL,XR,YT,YB,raster,
393 patterns, clr, mapmode);