File: lwgeom_chip.c

package info (click to toggle)
postgis 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,296 kB
  • sloc: sql: 77,621; ansic: 59,025; xml: 18,553; sh: 11,043; java: 6,061; perl: 2,133; makefile: 981; yacc: 299; python: 192
file content (1364 lines) | stat: -rw-r--r-- 28,643 bytes parent folder | download | duplicates (2)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
#include "postgres.h"
#include "access/gist.h"
#include "access/itup.h"
#include "fmgr.h"
#include "utils/elog.h"
#include "funcapi.h"

#include "lwgeom_pg.h"
#include "liblwgeom.h"

#include <math.h>
#include <float.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

/* Define this to debug CHIP ops */
/*#define DEBUG_CHIP 1*/

typedef unsigned short int UINT16;
typedef float FLOAT32;

typedef struct PIXEL_T
{
	int type; /* 1=float32, 5=int24, 6=int16 */
	uchar val[4];
}
PIXEL;

typedef struct RGB_T
{
	uchar red;
	uchar green;
	uchar blue;
}
RGB;


/* Internal funcs */
void swap_char(char *a,char *b);
void flip_endian_double(char *d);
void flip_endian_int32(char *i);
const char* pixelOpName(int op);
const char* pixelHEX(PIXEL* p);
UINT16 pixel_readUINT16(PIXEL *p);
void pixel_writeUINT16(PIXEL *p, UINT16 i);
PIXEL pixel_readval(char *buf);
void pixel_writeval(PIXEL *p, char *buf, size_t maxlen);
void pixel_add_float32(PIXEL *where, PIXEL *what);
void pixel_add_int24(PIXEL *where, PIXEL *what);
void pixel_add_int16(PIXEL *where, PIXEL *what);
void pixel_add(PIXEL *where, PIXEL *what);
size_t chip_xy_off(CHIP *c, size_t x, size_t y);
void chip_setPixel(CHIP *c, int x, int y, PIXEL *p);
PIXEL chip_getPixel(CHIP *c, int x, int y);
void chip_draw_pixel(CHIP *chip, int x, int y, PIXEL *pixel, int op);
void chip_draw_segment(CHIP *chip, int x1, int y1, int x2, int y2, PIXEL *pixel, int op);
void chip_fill(CHIP *chip, PIXEL *pixel, int op);
CHIP * pgchip_construct(BOX3D *bvol, int SRID, int width, int height, int datatype, PIXEL *initvalue);
void chip_draw_ptarray(CHIP *chip, POINTARRAY *pa, PIXEL *pixel, int op);
void chip_draw_lwpoint(CHIP *chip, LWPOINT *lwpoint, PIXEL* pixel, int op);
void chip_draw_lwline(CHIP *chip, LWLINE *lwline, PIXEL* pixel, int op);
void chip_draw_lwgeom(CHIP *chip, LWGEOM *lwgeom, PIXEL *pixel, int op);
char * text_to_cstring(text *t);


/* Prototypes */
Datum CHIP_in(PG_FUNCTION_ARGS);
Datum CHIP_out(PG_FUNCTION_ARGS);
Datum CHIP_to_LWGEOM(PG_FUNCTION_ARGS);
Datum CHIP_getSRID(PG_FUNCTION_ARGS);
Datum CHIP_getFactor(PG_FUNCTION_ARGS);
Datum CHIP_setFactor(PG_FUNCTION_ARGS);
Datum CHIP_getDatatype(PG_FUNCTION_ARGS);
Datum CHIP_getCompression(PG_FUNCTION_ARGS);
Datum CHIP_getHeight(PG_FUNCTION_ARGS);
Datum CHIP_getWidth(PG_FUNCTION_ARGS);
Datum CHIP_setSRID(PG_FUNCTION_ARGS);
Datum CHIP_send(PG_FUNCTION_ARGS);
Datum CHIP_dump(PG_FUNCTION_ARGS);
Datum CHIP_construct(PG_FUNCTION_ARGS);
Datum CHIP_getpixel(PG_FUNCTION_ARGS);
Datum CHIP_setpixel(PG_FUNCTION_ARGS);
Datum CHIP_draw(PG_FUNCTION_ARGS);
Datum CHIP_fill(PG_FUNCTION_ARGS);


/*
 * Input is a string with hex chars in it.
 * Convert to binary and put in the result
 */
PG_FUNCTION_INFO_V1(CHIP_in);
Datum CHIP_in(PG_FUNCTION_ARGS)
{
	char *str = PG_GETARG_CSTRING(0);
	CHIP *result;
	int size;
	int t;
	int input_str_len;
	int datum_size;

	/*printf("chip_in called\n"); */

	input_str_len = strlen(str);

	if  ( ( ( (int)(input_str_len/2.0) ) *2.0) != input_str_len)
	{
		elog(ERROR,"CHIP_in parser - should be even number of characters!");
		PG_RETURN_NULL();
	}

	if (strspn(str,"0123456789ABCDEF") != strlen(str) )
	{
		elog(ERROR,"CHIP_in parser - input contains bad characters.  Should only have '0123456789ABCDEF'!");
		PG_RETURN_NULL();
	}
	size = (input_str_len/2) ;
	result = (CHIP *) palloc(size);

	for (t=0; t<size; t++)
	{
		((uchar *)result)[t] = parse_hex( &str[t*2]) ;
	}
	/* if endian is wrong, flip it otherwise do nothing */
	result->size = size;
	if (result->size < sizeof(CHIP)-sizeof(void*) )
	{
		elog(ERROR,"CHIP_in parser - bad data (too small to be a CHIP)!");
		PG_RETURN_NULL();
	}

	if (result->endian_hint != 1)
	{
		/*need to do an endian flip */
		flip_endian_int32( (char *)   &result->endian_hint);

		flip_endian_double((char *)  &result->bvol.xmin);
		flip_endian_double((char *)  &result->bvol.ymin);
		flip_endian_double((char *)  &result->bvol.zmin);

		flip_endian_double((char *)  &result->bvol.xmax);
		flip_endian_double((char *)  &result->bvol.ymax);
		flip_endian_double((char *)  &result->bvol.zmax);

		flip_endian_int32( (char *)  & result->SRID);
		/*dont know what to do with future[8] ... */

		flip_endian_int32( (char *)  & result->height);
		flip_endian_int32( (char *)  & result->width);
		flip_endian_int32( (char *)  & result->compression);
		flip_endian_int32( (char *)  & result->factor);
		flip_endian_int32( (char *)  & result->datatype);

	}
	if (result->endian_hint != 1 )
	{
		elog(ERROR,"CHIP_in parser - bad data (endian flag != 1)!");
		PG_RETURN_NULL();
	}
	datum_size = 4;

	if ( (result->datatype == 6) || (result->datatype == 7) || (result->datatype == 106) || (result->datatype == 107) )
	{
		datum_size = 2;
	}
	if ( (result->datatype == 8) || (result->datatype == 108) )
	{
		datum_size=1;
	}

	if (result->compression ==0) /*only true for non-compressed data */
	{
		if (result->size != (sizeof(CHIP) - sizeof(void*) + datum_size * result->width*result->height) )
		{
			elog(ERROR,"CHIP_in parser - bad data (actual size [%d] != computed size [%ld])!", result->size, (long int)(sizeof(CHIP)-sizeof(void*) + datum_size * result->width*result->height) );
			PG_RETURN_NULL();
		}
	}

	PG_RETURN_POINTER(result);
}


/*given a CHIP structure, convert it to Hex and put it in a string */
PG_FUNCTION_INFO_V1(CHIP_out);
Datum CHIP_out(PG_FUNCTION_ARGS)
{
	CHIP *chip = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	char *result;
	int size_result;
	int t;

#if DEBUG_CHIP
	lwnotice("CHIP_out: %dx%d chip, %d in size", chip->width, chip->height, chip->size);
#endif

	size_result = (chip->size ) *2 +1; /* +1 for null char */
	result = palloc (size_result);
	result[size_result-1] = 0; /*null terminate */

	for (t=0; t< (chip->size); t++)
	{
		deparse_hex( ((uchar *) chip)[t], &result[t*2]);
	}
	PG_RETURN_CSTRING(result);
}


/*given a CHIP structure pipe it out as raw binary */
PG_FUNCTION_INFO_V1(CHIP_send);
Datum CHIP_send(PG_FUNCTION_ARGS)
{
	CHIP *chip = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	bytea *result;



	result = (bytea *)palloc( chip->size );
	/*SET_VARSIZE(result, chip->size + VARHDRSZ);*/
	/*memcpy( VARDATA(result), chip, chip->size ); */
	memcpy( result, chip, chip->size );
	PG_RETURN_POINTER(result);
	/*PG_RETURN_POINTER( (bytea *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)) );*/
}


PG_FUNCTION_INFO_V1(CHIP_to_LWGEOM);
Datum CHIP_to_LWGEOM(PG_FUNCTION_ARGS)
{
	CHIP *chip = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	PG_LWGEOM *result;
	POINT2D *pts = palloc(sizeof(POINT2D)*5);
	POINTARRAY *pa[1];
	LWPOLY *poly;
	uchar *ser;
	int wantbbox = false;

	/* Assign coordinates to POINT2D array */
	pts[0].x = chip->bvol.xmin;
	pts[0].y = chip->bvol.ymin;
	pts[1].x = chip->bvol.xmin;
	pts[1].y = chip->bvol.ymax;
	pts[2].x = chip->bvol.xmax;
	pts[2].y = chip->bvol.ymax;
	pts[3].x = chip->bvol.xmax;
	pts[3].y = chip->bvol.ymin;
	pts[4].x = chip->bvol.xmin;
	pts[4].y = chip->bvol.ymin;

	/* Construct point array */
	pa[0] = palloc(sizeof(POINTARRAY));
	pa[0]->serialized_pointlist = (uchar *)pts;
	TYPE_SETZM(pa[0]->dims, 0, 0);
	pa[0]->npoints = 5;

	/* Construct polygon */
	poly = lwpoly_construct(chip->SRID, NULL, 1, pa);

	/* Serialize polygon */
	ser = lwpoly_serialize(poly);

	/* Construct PG_LWGEOM  */
	result = PG_LWGEOM_construct(ser, chip->SRID, wantbbox);

	PG_RETURN_POINTER(result);

}

PG_FUNCTION_INFO_V1(CHIP_getSRID);
Datum CHIP_getSRID(PG_FUNCTION_ARGS)
{
	CHIP *c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	PG_RETURN_INT32(c->SRID);
}

PG_FUNCTION_INFO_V1(CHIP_getFactor);
Datum CHIP_getFactor(PG_FUNCTION_ARGS)
{
	CHIP *c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	PG_RETURN_FLOAT4(c->factor);
}

PG_FUNCTION_INFO_V1(CHIP_setFactor);
Datum CHIP_setFactor(PG_FUNCTION_ARGS)
{
	CHIP 	*c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	float	factor = PG_GETARG_FLOAT4(1);
	CHIP  *result;

	result = (CHIP *) palloc(c->size);

	memcpy(result,c,c->size);
	result->factor = factor;

	PG_RETURN_POINTER(result);
}



PG_FUNCTION_INFO_V1(CHIP_getDatatype);
Datum CHIP_getDatatype(PG_FUNCTION_ARGS)
{
	CHIP *c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	PG_RETURN_INT32(c->datatype);
}

PG_FUNCTION_INFO_V1(CHIP_getCompression);
Datum CHIP_getCompression(PG_FUNCTION_ARGS)
{
	CHIP *c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	PG_RETURN_INT32(c->compression);
}

PG_FUNCTION_INFO_V1(CHIP_getHeight);
Datum CHIP_getHeight(PG_FUNCTION_ARGS)
{
	CHIP *c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	PG_RETURN_INT32(c->height);
}

PG_FUNCTION_INFO_V1(CHIP_getWidth);
Datum CHIP_getWidth(PG_FUNCTION_ARGS)
{
	CHIP *c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	PG_RETURN_INT32(c->width);
}


PG_FUNCTION_INFO_V1(CHIP_setSRID);
Datum CHIP_setSRID(PG_FUNCTION_ARGS)
{
	CHIP *c = (CHIP *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	int32 new_srid = PG_GETARG_INT32(1);
	CHIP *result;

	result = (CHIP *) palloc(c->size);

	memcpy(result, c, c->size);
	result->SRID = new_srid;

	PG_RETURN_POINTER(result);
}

void	flip_endian_double(char 	*d)
{
	swap_char(d+7, d);
	swap_char(d+6, d+1);
	swap_char(d+5, d+2);
	swap_char(d+4, d+3);
}

void swap_char(char	*a,char *b)
{
	char c;

	c = *a;
	*a=*b;
	*b=c;
}

void		flip_endian_int32(char		*i)
{
	swap_char (i+3,i);
	swap_char (i+2,i+1);
}

/********************************************************************
 *
 * NEW chip code
 *
 ********************************************************************/

#define CHIP_BOUNDS_CHECKS 1


/* Macros to find size (in geographic units) of a CHIP cell (or pixel) */
#define CHIP_CELL_WIDTH(x) ( ((x)->bvol.xmax-(x)->bvol.xmin)/(x)->width )
#define CHIP_CELL_HEIGHT(x) ( ((x)->bvol.ymax-(x)->bvol.ymin)/(x)->height )

/* Return the size of values for pixel of the given datatype */
size_t chip_pixel_value_size(int datatype);

/********************************************************************
 *
 * IO primitives
 *
 ********************************************************************/

#define PIXELOP_OVERWRITE 1
#define PIXELOP_ADD 2
#define PIXELOP_MULTIPLY 3

char *pixelop_name[] =
{
	"Unknown",
	"Overwrite",
	"Add",
	"Multiply"
};

const char*
pixelOpName(int op)
{
	if ( op < 0 || op > 3 )
	{
		return "Invalid";
	}
	return pixelop_name[op];
}

const char*
pixelHEX(PIXEL* p)
{
	static char buf[256];
	size_t ps = chip_pixel_value_size(p->type);
	int i,j;
	static const char* hex = "0123456789ABCDEF";

	for (i=0, j=0; i<ps; ++i)
	{
		uchar val = p->val[i];
		int upper = (val & 0xF0) >> 4;
		int lower = val & 0x0F;

		buf[j++] = hex[ upper ];
		buf[j++] = hex[ lower ];
	}
	buf[j]=0;

	return buf;
}

UINT16
pixel_readUINT16(PIXEL *p)
{
	UINT16 i=0;
	memcpy(&i, &(p->val), 2);
	//i = p->val[0] | (p->val[1] << 8);
	return i;
}

void
pixel_writeUINT16(PIXEL *p, UINT16 i)
{
	//memcpy(&(p->val), &i, 2);
	p->val[0] = i&0xFF;
	p->val[1] = i>>8;
	p->val[2] = 0;
	p->val[3] = 0;
}

/*
 * Write text representation of a PIXEL value into provided
 * buffer.
 */
void
pixel_writeval(PIXEL *p, char *buf, size_t maxlen)
{
	FLOAT32 f32=0.0;
	UINT16 i16=0;

	switch (p->type)
	{
	case 1: /* float32 */
		memcpy(&f32, p->val, 4);
		sprintf(buf, "%g", f32);
		break;
	case 5: /* int24 (rgb) */
		buf[0] = '#';
		deparse_hex(p->val[0], &buf[1]);
		deparse_hex(p->val[1], &buf[3]);
		deparse_hex(p->val[2], &buf[5]);
		buf[7]='\0';
		break;
	case 6: /* int16 */
		i16=pixel_readUINT16(p);
		snprintf(buf, maxlen, "%u", i16);
		break;
	default:
		lwerror("Unsupported PIXEL value %d", p->type);
	}
}

PIXEL
pixel_readval(char *buf)
{
	FLOAT32 f32=0.0;
	long int i16=0;
	RGB rgb;
	char *ptr;
	PIXEL p;

	/* RGB */
	if ( buf[0] == '#' )
	{
		if ( strlen(buf) < 7 ) lwerror("RGB value too short");
		p.type = 5; /* rgb */
		ptr=buf+1;
		rgb.red = parse_hex(ptr);
		ptr+=2;
		rgb.green = parse_hex(ptr);
		ptr+=2;
		rgb.blue = parse_hex(ptr);
		memcpy(p.val, &rgb, 3);

		return p;
	}

	/* float32 */
	if ( strchr(buf, '.') )
	{
		f32 = strtod(buf, &ptr);
		if ( ptr != buf+strlen(buf) )
			lwerror("Malformed float value");
		p.type = 1; /* float32 */
		memcpy(p.val, &f32, 4);

		return p;
	}

	/* int16 */
	i16 = strtol(buf, &ptr, 0);
	if ( ptr != buf+strlen(buf) ) lwerror("Malformed integer value");
	if ( i16 > 65535 ) lwerror("Integer too high for an int16");
	p.type = 6; /* int16 */
	memcpy(p.val, &i16, 2);
	p.val[2]=0;
	p.val[3]=0;
	return p;
}

void
pixel_add_float32(PIXEL *where, PIXEL *what)
{
	FLOAT32 f1=0.0, f2=0.0;
	memcpy(&f1, where->val, 4);
	memcpy(&f2, what->val, 4);
	f1 += f2;
	memcpy(where->val, &f1, 4);
}

void
pixel_add_int24(PIXEL *where, PIXEL *what)
{
	unsigned int red, green, blue;
	RGB rgb1, rgb2;

	memcpy(&rgb1, where->val, 3);
	memcpy(&rgb2, what->val, 3);
	red = rgb1.red + rgb2.red;
	green = rgb1.green + rgb2.green;
	blue = rgb1.blue + rgb2.blue;
	if ( red > 255 )
	{
		lwnotice("Red channel saturated by add operation");
		red = 255;
	}
	if ( green > 255 )
	{
		lwnotice("Green channel saturated by add operation");
		green = 255;
	}
	if ( blue > 255 )
	{
		lwnotice("Blue channel saturated by add operation");
		blue = 255;
	}
	rgb1.red = red;
	rgb1.green = green;
	rgb1.blue = blue;
	memcpy(where->val, &rgb1, 3);
}

void
pixel_add_int16(PIXEL *where, PIXEL *what)
{
	UINT16 i1, i2;
	unsigned long int tmp;

	i1 = pixel_readUINT16(where);
	i2 = pixel_readUINT16(what);
	tmp = (unsigned long)i1 + i2;
	if ( tmp > 65535 )
	{
		lwnotice("UInt16 Pixel saturated by add operation (%u+%u=%u)",
		         i1, i2, tmp);
		tmp = 65535;
	}
	i1 = tmp;
	pixel_writeUINT16(where, i1);
}

void
pixel_add(PIXEL *where, PIXEL *what)
{
	if ( where->type != what->type )
		lwerror("Can't add pixels of different types");

	switch (where->type)
	{
	case 1: /*float32*/
		pixel_add_float32(where, what);
		break;

	case 5: /*int24*/
		pixel_add_int24(where, what);
		break;

	case 6: /*int16*/
		pixel_add_int16(where, what);
		break;

	default:
		lwerror("pixel_add: unkown pixel type %d",
		        where->type);
	}

}

/*
 * Return size of pixel values in bytes
 */
size_t
chip_pixel_value_size(int datatype)
{
	switch (datatype)
	{
	case 1:
	case 101:
		return 4;
	case 5:
	case 105:
		return 3;
	case 6:
	case 7:
	case 106:
	case 107:
		return 2;
	case 8:
	case 108:
		return 1;
	default:
		lwerror("Unknown CHIP datatype: %d", datatype);
		return 0;
	}
}

/*
 * Returns offset of pixel at X,Y
 */
#define CHIP_XY_OFF(c, x, y) ((x)+((y)*(c)->width))
size_t
chip_xy_off(CHIP *c, size_t x, size_t y)
{
#ifdef CHIP_BOUNDS_CHECKS
	if ( x < 0 || x >= c->width || y < 0 || y >= c->height )
	{
		lwerror("Coordinates ouf of range");
		return 0;
	}
#endif
	return x+(y*c->width);
}

/*
 * Set pixel value.
 *
 * CHIP is assumed to be:
 *	- uncompressed
 *	- in machine's byte order
 */
void
chip_setPixel(CHIP *c, int x, int y, PIXEL *p)
{
	void *where;
	size_t ps;
	size_t off;

#if DEBUG_CHIP
	lwnotice("chip_setPixel([CHIP %p], %d, %d) called", c, x, y);
#endif
	if ( c->datatype != p->type ) lwerror("Pixel datatype mismatch");

	ps = chip_pixel_value_size(c->datatype);
	off = chip_xy_off(c, x, y)*ps;
	if ( off > c->size + sizeof(CHIP) )
	{
		lwerror("Pixel offset out of CHIP size bounds");
	}

	where = ((char*)&(c->data))+off;

#if DEBUG_CHIP
	lwnotice("Writing %d bytes (%s) at offset %d (%p)",
	         ps, pixelHEX(p), off, where);
#endif

	memcpy(where, &(p->val), ps);
}

/*
 * Get pixel value.
 *
 * CHIP is assumed to be:
 *	- uncompressed
 *	- in machine's byte order
 */
PIXEL
chip_getPixel(CHIP *c, int x, int y)
{
	PIXEL p;
	size_t ps = chip_pixel_value_size(c->datatype);
	void *where = ((char*)&(c->data))+chip_xy_off(c, x, y)*ps;
	p.type = c->datatype;
	memset(p.val, '\0', 4);
	memcpy(p.val, where, ps);

	return p;
}

/********************************************************************
 *
 * Drawing primitives
 *
 ********************************************************************/

/*
 * Coordinates are in CHIP offset, so callers must
 * appropriately translate.
 */
void
chip_draw_pixel(CHIP *chip, int x, int y, PIXEL *pixel, int op)
{

#if DEBUG_CHIP
	lwnotice("chip_draw_pixel([CHIP %p], %d, %d, [PIXEL %p], %s) called",
	         (void*)chip, x, y, (void*)pixel, pixelOpName(op));
#endif

	if ( x < 0 || x >= chip->width || y < 0 || y >= chip->height )
	{
		/* should this be a warning ? */
		lwnotice("chip_draw_pixel called with out-of-range coordinates (%d,%d)", x, y);
		return;
	}

	/*
	 * Translate x and y values, which are in map units, to
	 * CHIP units
	 */

	switch ( op )
	{
		PIXEL p;

	case PIXELOP_OVERWRITE:
		chip_setPixel(chip, x, y, pixel);
		break;

	case PIXELOP_ADD:
		p = chip_getPixel(chip, x, y);
		pixel_add(&p, pixel);
		chip_setPixel(chip, x, y, &p);
		break;

	default:
		lwerror("Unsupported PIXELOP: %d", op);
	}

}

/*
 * Bresenham Line Algorithm
 *  http://www.edepot.com/linebresenham.html
 */
void
chip_draw_segment(CHIP *chip,
                  int x1, int y1, int x2, int y2,
                  PIXEL *pixel, int op)
{
	int x, y;
	int dx, dy;
	int incx, incy;
	int balance;


	if (x2 >= x1)
	{
		dx = x2 - x1;
		incx = 1;
	}
	else
	{
		dx = x1 - x2;
		incx = -1;
	}

	if (y2 >= y1)
	{
		dy = y2 - y1;
		incy = 1;
	}
	else
	{
		dy = y1 - y2;
		incy = -1;
	}

	x = x1;
	y = y1;

	if (dx >= dy)
	{
		dy <<= 1;
		balance = dy - dx;
		dx <<= 1;

		while (x != x2)
		{
			chip_draw_pixel(chip, x, y, pixel, op);
			if (balance >= 0)
			{
				y += incy;
				balance -= dx;
			}
			balance += dy;
			x += incx;
		}
		chip_draw_pixel(chip, x, y, pixel, op);
	}
	else
	{
		dx <<= 1;
		balance = dx - dy;
		dy <<= 1;

		while (y != y2)
		{
			chip_draw_pixel(chip, x, y, pixel, op);
			if (balance >= 0)
			{
				x += incx;
				balance -= dy;
			}
			balance += dx;
			y += incy;
		}
		chip_draw_pixel(chip, x, y, pixel, op);
	}
}

void
chip_fill(CHIP *chip, PIXEL *pixel, int op)
{
	int x, y;

#if DEBUG_CHIP
	lwnotice("chip_fill called");
#endif

	for (x=0; x<chip->width; x++)
	{
		for (y=0; y<chip->height; y++)
		{
			chip_draw_pixel(chip, x, y, pixel, op);
		}
	}
}

/********************************************************************
 *
 * CHIP constructors
 *
 ********************************************************************/

CHIP *
pgchip_construct(BOX3D *bvol, int SRID, int width, int height,
                 int datatype, PIXEL *initvalue)
{
	size_t pixsize = chip_pixel_value_size(datatype);
	size_t datasize = pixsize*width*height;
	size_t size = sizeof(CHIP)-sizeof(void*)+datasize;
	CHIP *chip = lwalloc(size);

#if DEBUG_CHIP
	lwnotice(" sizeof(CHIP):%d, pixelsize:%d, datasize:%d, total size:%d", sizeof(CHIP), pixsize, datasize, size);
#endif

	chip->size=size;
	chip->endian_hint=1;
	memcpy(&(chip->bvol), bvol, sizeof(BOX3D));
	chip->SRID=SRID;
	memset(chip->future, '\0', 4);
	chip->factor=1.0;
	chip->datatype=datatype;
	chip->height=height;
	chip->width=width;
	chip->compression=0; /* no compression */
	if ( ! initvalue )
	{
		memset(&(chip->data), '\0', datasize);
	}
	else
	{
		chip_fill(chip, initvalue, PIXELOP_OVERWRITE);
	}
	return chip;
}


/********************************************************************
 *
 * Drawing functions
 *
 ********************************************************************/


/*
 * Transform a POINT from geo coordinates to CHIP offsets.
 * Can be optimized by caching CHIP info somewhere and using
 * multiplications rather then divisions.
 */
static void
transform_point(CHIP* chip, POINT2D* p)
{
	/* geo size of full grid/chip */
	double geowidth = chip->bvol.xmax - chip->bvol.xmin;
	double geoheight = chip->bvol.ymax - chip->bvol.ymin;

	/* geo size of a cell/pixel */
	double xscale = geowidth / chip->width;
	double yscale = geoheight / chip->height;

	double xoff = ( chip->bvol.xmin + xscale );
	double yoff = ( chip->bvol.ymin + yscale );

#if 0
	double ox = p->x;
	double oy = p->y;
#endif

	p->x = rint ( ( p->x - xoff ) / xscale );
	p->y = rint ( ( p->y - yoff ) / yscale );

#if 0
	lwnotice("Old x: %g, new x: %g", ox, p->x);
#endif

}

void
chip_draw_ptarray(CHIP *chip, POINTARRAY *pa, PIXEL *pixel, int op)
{
	POINT2D A, B;
	int i;
	int x1, x2, y1, y2;

	for (i=1; i<pa->npoints; i++)
	{
		getPoint2d_p(pa, i-1, &A);
		getPoint2d_p(pa, i, &B);

		transform_point(chip, &A);
		transform_point(chip, &B);

		x1 = A.x;
		y1 = A.y;
		x2 = B.x;
		y2 = B.y;

		chip_draw_segment(chip, x1, y1, x2, y2, pixel, op);
	}
}

void
chip_draw_lwpoint(CHIP *chip, LWPOINT *lwpoint, PIXEL* pixel, int op)
{
	POINTARRAY *pa;
	POINT2D point;

	pa = lwpoint->point;
	getPoint2d_p(pa, 0, &point);

	/* translate to CHIP plane */
	transform_point(chip, &point);

	chip_draw_pixel(chip, point.x, point.y, pixel, op);
}

void
chip_draw_lwline(CHIP *chip, LWLINE *lwline, PIXEL* pixel, int op)
{
	POINTARRAY *pa;

	pa = lwline->points;
	chip_draw_ptarray(chip, pa, pixel, op);
	return;
}

void
chip_draw_lwgeom(CHIP *chip, LWGEOM *lwgeom, PIXEL *pixel, int op)
{
	int i;
	LWCOLLECTION *coll;

	/* Check wheter we should completely skip this geometry */
	if ( lwgeom->bbox )
	{
		if ( chip->bvol.xmax < lwgeom->bbox->xmin ) return;
		if ( chip->bvol.xmin > lwgeom->bbox->xmax ) return;
		if ( chip->bvol.ymax < lwgeom->bbox->ymin ) return;
		if ( chip->bvol.ymin > lwgeom->bbox->ymax ) return;
	}

	switch (TYPE_GETTYPE(lwgeom->type) )
	{
	case POINTTYPE:
		chip_draw_lwpoint(chip, (LWPOINT*)lwgeom, pixel, op);
		return;
	case LINETYPE:
		chip_draw_lwline(chip, (LWLINE*)lwgeom, pixel, op);
		return;
	case POLYGONTYPE:
		lwerror("%s geometry unsupported by draw operation",
		        lwgeom_typename(TYPE_GETTYPE(lwgeom->type)));
	case MULTIPOINTTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
	case COLLECTIONTYPE:
		coll = (LWCOLLECTION *)lwgeom;
		for (i=0; i<coll->ngeoms; i++)
		{
			chip_draw_lwgeom(chip, coll->geoms[i],
			                 pixel, op);
		}
		return;
	default:
		lwerror("Unknown geometry type: %d", lwgeom->type);
	}
}

/********************************************************************
 *
 * PGsql interfaces
 *
 ********************************************************************/

typedef struct CHIPDUMPSTATE_T
{
	CHIP *chip;
	int x;
	int y;
	char *values[3];
	char fmt[8];
}
CHIPDUMPSTATE;

/*
 * Convert a TEXT to a C-String.
 * Remember to lwfree the returned object.
 */
char *
text_to_cstring(text *t)
{
	char *s;
	size_t len;

	len = VARSIZE(t)-VARHDRSZ;
	s = lwalloc(len+1);
	memcpy(s, VARDATA(t), len);
	s[len] = '\0';

	return s;
}

PG_FUNCTION_INFO_V1(CHIP_dump);
Datum CHIP_dump(PG_FUNCTION_ARGS)
{
	CHIP *chip;
	FuncCallContext *funcctx;
	MemoryContext oldcontext, newcontext;
	CHIPDUMPSTATE *state;
	TupleDesc tupdesc;
	AttInMetadata *attinmeta;
	HeapTuple tuple;
	TupleTableSlot *slot;
	Datum result;

	if (SRF_IS_FIRSTCALL())
	{
		funcctx = SRF_FIRSTCALL_INIT();
		newcontext = funcctx->multi_call_memory_ctx;

		oldcontext = MemoryContextSwitchTo(newcontext);

		chip = (CHIP *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));

		/* Create function state */
		state = lwalloc(sizeof(CHIPDUMPSTATE));
		state->chip = chip;
		state->x=0;
		state->y=0;
		state->values[0] = lwalloc(256);
		state->values[1] = lwalloc(256);
		state->values[2] = lwalloc(256);

		funcctx->user_fctx = state;

		/*
		 * Build a tuple description for an
		 * geometry_dump tuple
		 */
		tupdesc = RelationNameGetTupleDesc("chip_dump");

		/* allocate a slot for a tuple with this tupdesc */
		slot = TupleDescGetSlot(tupdesc);

		/* allocate a slot for a tuple with this tupdesc */
		slot = TupleDescGetSlot(tupdesc);

		/* assign slot to function context */
		funcctx->slot = slot;

		/*
		 * generate attribute metadata needed later to produce
		 * tuples from raw C strings
		 */
		attinmeta = TupleDescGetAttInMetadata(tupdesc);
		funcctx->attinmeta = attinmeta;

		MemoryContextSwitchTo(oldcontext);
	}

	/* stuff done on every call of the function */
	funcctx = SRF_PERCALL_SETUP();
	newcontext = funcctx->multi_call_memory_ctx;

	/* get state */
	state = funcctx->user_fctx;

	/* Handled simple geometries */
	while ( state->y < state->chip->height &&
	        state->x < state->chip->width )
	{
		char buf[256];
		PIXEL p;

		if ( ! state->chip ) lwerror("state->chip corrupted");
		p = chip_getPixel(state->chip, state->x, state->y);
		pixel_writeval(&p, buf, 255);

		sprintf(state->values[0], "%d", state->x);
		sprintf(state->values[1], "%d", state->y);
		sprintf(state->values[2], "%s", buf);

		tuple = BuildTupleFromCStrings(funcctx->attinmeta,
		                               state->values);
		result = TupleGetDatum(funcctx->slot, tuple);

		if ( state->x < state->chip->width-1 )
		{
			state->x++;
		}
		else
		{
			state->x=0;
			state->y++;
		}

		SRF_RETURN_NEXT(funcctx, result);

	}

	SRF_RETURN_DONE(funcctx);
}

PG_FUNCTION_INFO_V1(CHIP_construct);
Datum CHIP_construct(PG_FUNCTION_ARGS)
{
	CHIP *chip;
	BOX3D *box = (BOX3D *)PG_GETARG_POINTER(0);
	int SRID = PG_GETARG_INT32(1);
	int width = PG_GETARG_INT32(2);
	int height = PG_GETARG_INT32(3);
	text *pixel_text = PG_GETARG_TEXT_P(4);
	char *pixel = text_to_cstring(pixel_text);
	PIXEL pix = pixel_readval(pixel);

#if DEBUG_CHIP
	lwnotice("CHIP_construct called");
#endif

	if ( width <= 0 || height <= 0 )
	{
		lwerror("Invalid values for width or height");
		PG_RETURN_NULL();
	}

	chip = pgchip_construct(box, SRID, width, height, pix.type, &pix);

#if DEBUG_CHIP
	lwnotice("Created %dx%d chip type", chip->width, chip->height);
#endif

	PG_RETURN_POINTER(chip);
}

PG_FUNCTION_INFO_V1(CHIP_getpixel);
Datum CHIP_getpixel(PG_FUNCTION_ARGS)
{
	CHIP *chip = (CHIP *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
	int x = PG_GETARG_INT32(1);
	int y = PG_GETARG_INT32(2);
	char buf[256];
	size_t len;
	text *ret;
	PIXEL p;

	if ( x < 0 || x >= chip->width )
	{
		lwerror("X out of range %d..%d",
		        0, chip->width-1);
		PG_RETURN_NULL();
	}
	if ( y < 0 || y >= chip->height )
	{
		lwerror("Y out of range %d..%d",
		        0, chip->height-1);
		PG_RETURN_NULL();
	}

	p = chip_getPixel(chip, x, y);
	pixel_writeval(&p, buf, 255);
	len = strlen(buf);
	ret = lwalloc(len+VARHDRSZ);
	SET_VARSIZE(ret, len+VARHDRSZ);
	memcpy(VARDATA(ret), buf, len);

	PG_RETURN_POINTER(ret);
}

PG_FUNCTION_INFO_V1(CHIP_setpixel);
Datum CHIP_setpixel(PG_FUNCTION_ARGS)
{
	CHIP *chip = (CHIP *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
	int x = PG_GETARG_INT32(1);
	int y = PG_GETARG_INT32(2);
	text *init_val_text = PG_GETARG_TEXT_P(3);
	char *init_val;
	PIXEL pixel;

	/* Parse pixel */
	init_val = text_to_cstring(init_val_text);
	pixel = pixel_readval(init_val);

	if ( chip->datatype != pixel.type )
	{
		lwerror("Pixel datatype %d mismatches chip datatype %d",
		        pixel.type, chip->datatype);
	}

	chip_setPixel(chip, x, y, &pixel);

	PG_RETURN_POINTER(chip);

}

PG_FUNCTION_INFO_V1(CHIP_draw);
Datum CHIP_draw(PG_FUNCTION_ARGS)
{
	CHIP *chip = (CHIP *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
	PG_LWGEOM *geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
	LWGEOM *lwgeom = pglwgeom_deserialize(geom);
	text *pixel_text = PG_GETARG_TEXT_P(2);
	char *pixel_str;
	text *pixelop_text;
	char *pixelop_str;
	int pixelop = PIXELOP_OVERWRITE;
	PIXEL pixel;

	/* Check SRID match */
	if ( chip->SRID != lwgeom->SRID )
	{
		lwerror("Operation on mixed SRID objects");
	}

	if ( PG_NARGS() > 3 )
	{
		pixelop_text = PG_GETARG_TEXT_P(3);
		pixelop_str = text_to_cstring(pixelop_text);
		if ( pixelop_str[0] == 'o' )
		{
			pixelop = PIXELOP_OVERWRITE;
		}
		else if ( pixelop_str[0] == 'a' )
		{
			pixelop = PIXELOP_ADD;
		}
		else
		{
			lwerror("Unsupported pixel operation %s", pixelop_str);
		}
	}

	/* Parse pixel */
	pixel_str = text_to_cstring(pixel_text);
	pixel = pixel_readval(pixel_str);
	lwfree(pixel_str);

	if ( pixel.type != chip->datatype )
	{
		lwerror("Pixel/Chip datatype mismatch");
	}

	/* Perform drawing */
	chip_draw_lwgeom(chip, lwgeom, &pixel, pixelop);

	PG_RETURN_POINTER(chip);
}

PG_FUNCTION_INFO_V1(CHIP_fill);
Datum CHIP_fill(PG_FUNCTION_ARGS)
{
	CHIP *chip = (CHIP *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
	text *pixel_text = PG_GETARG_TEXT_P(1);
	char *pixel_str;
	text *pixelop_text;
	char *pixelop_str;
	int pixelop = PIXELOP_OVERWRITE;
	PIXEL pixel;

	if ( PG_NARGS() > 2 )
	{
		pixelop_text = PG_GETARG_TEXT_P(2);
		pixelop_str = text_to_cstring(pixelop_text);
		if ( pixelop_str[0] == 'o' )
		{
			pixelop = PIXELOP_OVERWRITE;
		}
		else if ( pixelop_str[0] == 'a' )
		{
			pixelop = PIXELOP_ADD;
		}
		else
		{
			lwerror("Unsupported pixel operation %s", pixelop_str);
		}
	}

	/* Parse pixel */
	pixel_str = text_to_cstring(pixel_text);
	pixel = pixel_readval(pixel_str);
	lwfree(pixel_str);

	if ( pixel.type != chip->datatype )
	{
		lwerror("Pixel/Chip datatype mismatch");
	}

	/* Perform fill */
	chip_fill(chip, &pixel, pixelop);

	PG_RETURN_POINTER(chip);
}