Source

pygame / src / surface.c

   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
/*
    PyGame - Python Game Library
    Copyright (C) 2000  Pete Shinners

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Pete Shinners
    pete@shinners.org
*/

/*
 *  PyGAME display module
 */
#define PYGAMEAPI_SURFACE_INTERNAL
#include "pygame.h"


staticforward PyTypeObject PySurface_Type;
static PyObject* PySurface_New(SDL_Surface* info);
#define PySurface_Check(x) ((x)->ob_type == &PySurface_Type)



/* surface object methods */


    /*DOC*/ static char doc_surf_get_at[] =
    /*DOC*/    "Surface.get_at([x, y]) -> int\n"
    /*DOC*/    "get a pixel color\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the mapped pixel color at the coordinates\n"
    /*DOC*/    "given point.\n"
    /*DOC*/ ;

static PyObject* surf_get_at(PyObject* self, PyObject* arg)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	SDL_PixelFormat* format = surf->format;
	Uint8* pixels = (Uint8*)surf->pixels;
	int x, y;

	if(!PyArg_ParseTuple(arg, "(ii)", &x, &y))
		return NULL;

	if(x < 0 || x >= surf->w || y < 0 || y >= surf->h)
		return RAISE(PyExc_IndexError, "buffer index out of range");

	if(!pixels)
		return RAISE(PyExc_SDLError, "Surface must be locked for pixel access");

	switch(format->BytesPerPixel)
	{
		case 1:
		{
			Uint8 col = *((Uint8*)pixels + y * surf->pitch + x);
			return PyInt_FromLong(col);
		}
		break;
		case 2:
		{
			Uint16 col = *((Uint16*)(pixels + y * surf->pitch) + x);
			return PyInt_FromLong(col); 
		}
		break;
		case 3:
		{
			Uint32 col;
			Uint8* byte_buf;
			
			byte_buf = ((Uint8*)(pixels + y * surf->pitch) + x * 3);
			col =
				*(byte_buf + (format->Rshift >> 3)) << format->Rshift |
				*(byte_buf + (format->Gshift >> 3)) << format->Gshift |
				*(byte_buf + (format->Bshift >> 3)) << format->Bshift;
			return PyInt_FromLong(col);   
		}
		break;
		case 4:
		{
			Uint32 col = *((Uint32*)(pixels + y * surf->pitch) + x);
			return PyInt_FromLong(col); 
		}
		break;
	}
	return RAISE(PyExc_RuntimeError, "Unable to determine color depth.");
}



    /*DOC*/ static char doc_surf_set_at[] =
    /*DOC*/    "Surface.set_at([x, y], pixel) -> None\n"
    /*DOC*/    "set pixel at given position\n"
    /*DOC*/    "\n"
    /*DOC*/    "Assigns a mapped pixel color to the image at the\n"
    /*DOC*/    "give position.\n"
    /*DOC*/ ;

static PyObject* surf_set_at(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	SDL_PixelFormat* format = surf->format;
	Uint8* pixels = (Uint8*)surf->pixels;
	int x, y;
	Uint32 color;
	
	if(!PyArg_ParseTuple(args, "(ii)i", &x, &y, &color))
		return NULL;

	if(x < 0 || x >= surf->w || y < 0 || y >= surf->h)
	{
		printf("%d,%d  -  %d,%d\n",x,y, surf->w, surf->h);
		PyErr_SetString(PyExc_IndexError, "buffer index out of range");
		return NULL;
	}

	if(!pixels)
		return RAISE(PyExc_SDLError, "Surface must be locked for pixel access");

	switch(format->BytesPerPixel)
	{
		case 1:
		{
			*((Uint8*)pixels + y * surf->pitch + x) = (Uint8)color;
		}
		break;
		case 2:
		{
			*((Uint16*)(pixels + y * surf->pitch) + x) = (Uint16)color;
		}
		break;
		case 3:
		{
			Uint8* byte_buf = (Uint8*)(pixels + y * surf->pitch) + x * 3;
			Uint8 r, g, b;

			r = (color & format->Rmask) >> format->Rshift;
			g = (color & format->Gmask) >> format->Gshift;
			b = (color & format->Bmask) >> format->Bshift;

			*(byte_buf + (format->Rshift >> 3)) = r;
			*(byte_buf + (format->Gshift >> 3)) = g;
			*(byte_buf + (format->Bshift >> 3)) = b;	
		}
		break;
		case 4:
		{
			*((Uint32*)(pixels + y * surf->pitch) + x) = color;
		}
		break;
		default:
			return RAISE(PyExc_SDLError, "Unable to determine color depth.");
	}

	RETURN_NONE
}



    /*DOC*/ static char doc_surf_map_rgb[] =
    /*DOC*/    "Surface.map_rgb([r, g, b]) -> int\n"
    /*DOC*/    "convert RGB into a mapped color\n"
    /*DOC*/    "\n"
    /*DOC*/    "Uses the Surface format to convert RGB into a\n"
    /*DOC*/    "mapped color value. Note that this will work if\n"
    /*DOC*/    "the RGB is passed as three arguments instead of a\n"
    /*DOC*/    "sequence.\n"
    /*DOC*/ ;

static PyObject* surf_map_rgb(PyObject* self,PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	Uint8 r, g, b;

	if(PyTuple_Size(args)==1)
	{
		if(!PyArg_ParseTuple(args, "(bbb)", &r, &g, &b))
			return NULL;
	}
	else if(!PyArg_ParseTuple(args, "bbb", &r, &g, &b))
		return NULL;

	return PyInt_FromLong(SDL_MapRGB(surf->format, r, g, b));
}



    /*DOC*/ static char doc_surf_unmap_rgb[] =
    /*DOC*/    "Surface.unmap_rgb(color) -> r, g, b\n"
    /*DOC*/    "convert mapped color into RGB\n"
    /*DOC*/    "\n"
    /*DOC*/    "This function returns the RGB components for a\n"
    /*DOC*/    "mapped color value.\n"
    /*DOC*/ ;

static PyObject* surf_unmap_rgb(PyObject* self,PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	Uint32 col;
	Uint8 r, g, b;
	
	if(!PyArg_ParseTuple(args, "i", &col))
		return NULL;

	SDL_GetRGB(col,surf->format, &r, &g, &b);	

	return Py_BuildValue("(bbb)", r, g, b);
}


    /*DOC*/ static char doc_surf_map_rgba[] =
    /*DOC*/    "Surface.map_rgba([r, g, b, a]) -> int\n"
    /*DOC*/    "convert RGBA into a mapped color\n"
    /*DOC*/    "\n"
    /*DOC*/    "Uses the Surface format to convert RGBA into a\n"
    /*DOC*/    "mapped color value. It is safe to call this on a\n"
    /*DOC*/    "surface with no pixel alpha. The alpha will simply\n"
    /*DOC*/    "be ignored.\n"
    /*DOC*/ ;

static PyObject* surf_map_rgba(PyObject* self,PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	Uint8 r, g, b, a;

	if(PyTuple_Size(args)==1)
	{
		if(!PyArg_ParseTuple(args, "(bbbb)", &r, &g, &b, &a))
			return NULL;
	}
	else if(!PyArg_ParseTuple(args, "bbbb", &r, &g, &b, &a))
		return NULL;

	return PyInt_FromLong(SDL_MapRGBA(surf->format, r, g, b, a));
}



    /*DOC*/ static char doc_surf_unmap_rgba[] =
    /*DOC*/    "Surface.unmap_rgba(color) -> r, g, b, a\n"
    /*DOC*/    "convert mapped color into RGBA\n"
    /*DOC*/    "\n"
    /*DOC*/    "This function returns the RGB components for a\n"
    /*DOC*/    "mapped color value. For surfaces with no alpha,\n"
    /*DOC*/    "the alpha will always be 255.\n"
    /*DOC*/ ;

static PyObject* surf_unmap_rgba(PyObject* self,PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	Uint32 col;
	Uint8 r, g, b, a;
	
	if(!PyArg_ParseTuple(args, "i", &col))
		return NULL;

	SDL_GetRGBA(col,surf->format, &r, &g, &b, &a);	

	return Py_BuildValue("(bbb)", r, g, b, a);
}



    /*DOC*/ static char doc_surf_lock[] =
    /*DOC*/    "Surface.lock() -> None\n"
    /*DOC*/    "locks Surface for pixel access\n"
    /*DOC*/    "\n"
    /*DOC*/    "On accelerated surfaces, it is usually required to\n"
    /*DOC*/    "lock the surface before you can access the pixel\n"
    /*DOC*/    "values. To be safe, it is always a good idea to\n"
    /*DOC*/    "lock the surface before entering a block of code\n"
    /*DOC*/    "that changes or accesses the pixel values. The\n"
    /*DOC*/    "surface must not be locked when performing other\n"
    /*DOC*/    "pyGame functions on it like fill and blit.\n"
    /*DOC*/    "\n"
    /*DOC*/    "You can doublecheck to really make sure a lock is\n"
    /*DOC*/    "needed by calling the mustlock() member. This\n"
    /*DOC*/    "should not be needed, since it is usually\n"
    /*DOC*/    "recommended to lock anyways and work with all\n"
    /*DOC*/    "surface types. If the surface does not need to be\n"
    /*DOC*/    "locked, the operation will return quickly with\n"
    /*DOC*/    "minute overhead.\n"
    /*DOC*/    "\n"
    /*DOC*/    "On some platforms a necessary lock can shut off\n"
    /*DOC*/    "some parts of the system. This is not a problem\n"
    /*DOC*/    "unless you leave surfaces locked for long periouds\n"
    /*DOC*/    "of time. Only keep the surface locked when you\n"
    /*DOC*/    "need the pixel access. At the same time, it is not\n"
    /*DOC*/    "a good too repeatedly lock and unlock the surface\n"
    /*DOC*/    "inside tight loops. It is fine to leave the\n"
    /*DOC*/    "surface locked while needed, just don't be lazy.\n"
    /*DOC*/ ;

static PyObject* surf_lock(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	if(SDL_LockSurface(surf) == -1)
		return RAISE(PyExc_SDLError, SDL_GetError());

	RETURN_NONE
}



    /*DOC*/ static char doc_surf_unlock[] =
    /*DOC*/    "Surface.unlock() -> None\n"
    /*DOC*/    "locks Surface for pixel access\n"
    /*DOC*/    "\n"
    /*DOC*/    "After a surface has been locked, you will need to\n"
    /*DOC*/    "unlock it when you are done.\n"
    /*DOC*/    "\n"
    /*DOC*/    "You can doublecheck to really make sure a lock is\n"
    /*DOC*/    "needed by calling the mustlock() member. This\n"
    /*DOC*/    "should not be needed, since it is usually\n"
    /*DOC*/    "recommended to lock anyways and work with all\n"
    /*DOC*/    "surface types. If the surface does not need to be\n"
    /*DOC*/    "locked, the operation will return quickly with\n"
    /*DOC*/    "minute overhead.\n"
    /*DOC*/ ;

static PyObject* surf_unlock(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	SDL_UnlockSurface(surf);
	RETURN_NONE
}



    /*DOC*/ static char doc_surf_mustlock[] =
    /*DOC*/    "Surface.mustlock() -> bool\n"
    /*DOC*/    "check if the surface needs locking\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns true if the surface really does need\n"
    /*DOC*/    "locking to gain pixel access. Usually the overhead\n"
    /*DOC*/    "of checking before locking outweight the overhead\n"
    /*DOC*/    "of just locking any surface before access.\n"
    /*DOC*/ ;

static PyObject* surf_mustlock(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return PyInt_FromLong(SDL_MUSTLOCK(surf));
}



    /*DOC*/ static char doc_surf_get_palette[] =
    /*DOC*/    "Surface.get_palette() -> [[r, g, b], ...]\n"
    /*DOC*/    "get the palette\n"
    /*DOC*/    "\n"
    /*DOC*/    "This will return the an array of all the color\n"
    /*DOC*/    "indexes in the Surface's palette.\n"
    /*DOC*/ ;

static PyObject* surf_get_palette(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	SDL_Palette* pal = surf->format->palette;
	PyObject* list;
	int i;
		
	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	if(!pal)
		return RAISE(PyExc_SDLError, "Surface is not palettized\n");

	list = PyTuple_New(pal->ncolors);
	if(!list)
		return NULL;

	for(i = 0;i < pal->ncolors;i++)
	{
		PyObject* color;
		SDL_Color* c = &pal->colors[i];

		color = Py_BuildValue("(bbb)", c->r, c->g, c->b);
		if(!color)
		{
			Py_DECREF(list);
			return NULL;
		}

		PyTuple_SET_ITEM(list, i, color);
	}

	return list;
}



    /*DOC*/ static char doc_surf_get_palette_at[] =
    /*DOC*/    "Surface.get_palette_at(index) -> r, g, b\n"
    /*DOC*/    "get a palette entry\n"
    /*DOC*/    "\n"
    /*DOC*/    "This will retreive an individual color entry from\n"
    /*DOC*/    "the Surface's palette.\n"
    /*DOC*/ ;

static PyObject* surf_get_palette_at(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	SDL_Palette* pal = surf->format->palette;
	SDL_Color* c;
	int index;

	if(!PyArg_ParseTuple(args, "i", &index))
		return NULL;

	if(!pal)
		return RAISE(PyExc_SDLError, "Surface is not palettized\n");
	if(index >= pal->ncolors || index < 0)
		return RAISE(PyExc_IndexError, "index out of bounds");

	c = &pal->colors[index];
	return Py_BuildValue("(bbb)", c->r, c->g, c->b);
}



    /*DOC*/ static char doc_surf_set_palette[] =
    /*DOC*/    "Surface.set_palette([[r, g, b], ...]) -> None\n"
    /*DOC*/    "set the palette\n"
    /*DOC*/    "\n"
    /*DOC*/    "This will replace the entire palette with color\n"
    /*DOC*/    "information you provide.\n"
    /*DOC*/ ;

static PyObject* surf_set_palette(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	SDL_Palette* pal = surf->format->palette;
	SDL_Color* colors;
	PyObject* list, *item;
	int i, len;
	short r, g, b;
	
	if(!PyArg_ParseTuple(args, "O", &list))
		return NULL;
	if(!PySequence_Check(list))
		return RAISE(PyExc_ValueError, "Argument must be a sequence type");


	if(!pal)
		return RAISE(PyExc_SDLError, "Surface is not palettized\n");

	len = min(pal->ncolors, PySequence_Length(list));

	colors = (SDL_Color*)malloc(len * sizeof(SDL_Color));
	if(!colors)
		return NULL;
	
	for(i = 0; i < len; i++)
	{
		item = PySequence_GetItem(list, i);

		if(!PySequence_Check(item) || PySequence_Length(item) != 3)
		{
			Py_DECREF(item);
			free((char*)colors);
			return RAISE(PyExc_TypeError, "takes a sequence of sequence of RGB");
		}
		if(!ShortFromObjIndex(item, 0, &r) || !ShortFromObjIndex(item, 1, &g) || !ShortFromObjIndex(item, 2, &b))
			return RAISE(PyExc_TypeError, "RGB sequence must contain numeric values");

		colors[i].r = (unsigned char)r;
		colors[i].g = (unsigned char)g;
		colors[i].b = (unsigned char)b;
	
		Py_DECREF(item);
	}

	SDL_SetColors(surf, colors, 0, len);

	free((char*)colors);

	RETURN_NONE
}



    /*DOC*/ static char doc_surf_set_palette_at[] =
    /*DOC*/    "Surface.set_palette_at(index, [r, g, b]) -> None\n"
    /*DOC*/    "set a palette entry\n"
    /*DOC*/    "\n"
    /*DOC*/    "This function sets the palette color at a specific\n"
    /*DOC*/    "entry.\n"
    /*DOC*/ ;

static PyObject* surf_set_palette_at(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	SDL_Palette* pal = surf->format->palette;
	SDL_Color color;
	int index;
	Uint8 r, g, b;

	if(!PyArg_ParseTuple(args, "i(bbb)", &index, &r, &g, &b))
		return NULL;

	if(!pal)
	{
		PyErr_SetString(PyExc_SDLError, "Surface is not palettized\n");
		return NULL;
	}

	if(index >= pal->ncolors || index <= 0)
	{
		PyErr_SetString(PyExc_IndexError, "index out of bounds");
		return NULL;
	}

	color.r = r;
	color.g = g;
	color.b = b;

	SDL_SetColors(surf, &color, index, 1);

	RETURN_NONE
}



    /*DOC*/ static char doc_surf_set_colorkey[] =
    /*DOC*/    "Surface.set_colorkey([color, [flags]]) -> None\n"
    /*DOC*/    "change colorkey information\n"
    /*DOC*/    "\n"
    /*DOC*/    "Set the colorkey for the surface by passing a\n"
    /*DOC*/    "mapped color value as the color argument. If no\n"
    /*DOC*/    "arguments are passed, colorkeying will be disabled\n"
    /*DOC*/    "for this surface.\n"
    /*DOC*/    "\n"
    /*DOC*/    "If your image is nonchanging and will be used\n"
    /*DOC*/    "repeatedly, you will probably want to pass the\n"
    /*DOC*/    "RLEACCEL flag to the call. This will take a short\n"
    /*DOC*/    "time to compile your surface, and increase the\n"
    /*DOC*/    "blitting speed.\n"
    /*DOC*/ ;

static PyObject* surf_set_colorkey(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	Uint32 flags = 0, key = 0;

	if(!PyArg_ParseTuple(args, "|ii", &key, &flags))
		return NULL;
	
	if(PyTuple_Size(args) > 0)
		flags |= SDL_SRCCOLORKEY;

	if(SDL_SetColorKey(surf, flags, key) == -1)
		return RAISE(PyExc_SDLError, SDL_GetError());
	RETURN_NONE
}


    /*DOC*/ static char doc_surf_get_colorkey[] =
    /*DOC*/    "Surface.get_colorkey() -> color\n"
    /*DOC*/    "query colorkey\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the current mapped color value being used\n"
    /*DOC*/    "for colorkeying. If colorkeying is not enabled for\n"
    /*DOC*/    "this surface, it returns None\n"
    /*DOC*/ ;

static PyObject* surf_get_colorkey(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);

	if(!PyArg_ParseTuple(args, ""))
		return NULL;
	
	if(surf->flags&SDL_SRCCOLORKEY)
		return PyInt_FromLong(surf->format->colorkey);

	RETURN_NONE
}


    /*DOC*/ static char doc_surf_set_alpha[] =
    /*DOC*/    "Surface.set_alpha([alpha, [flags]]) -> None\n"
    /*DOC*/    "change alpha information\n"
    /*DOC*/    "\n"
    /*DOC*/    "Set the overall transparency for the surface. If\n"
    /*DOC*/    "no alpha is passed, alpha blending is disabled for\n"
    /*DOC*/    "the surface. An alpha of 0 is fully transparent,\n"
    /*DOC*/    "an alpha of 255 is fully opaque.\n"
    /*DOC*/    "\n"
    /*DOC*/    "If your surface has a pixel alpha channel, it will\n"
    /*DOC*/    "override the overall surface transparency. You'll\n"
    /*DOC*/    "need to change the actual pixel transparency to\n"
    /*DOC*/    "make changes.\n"
    /*DOC*/    "\n"
    /*DOC*/    "If your image is nonchanging and will be used\n"
    /*DOC*/    "repeatedly, you will probably want to pass the\n"
    /*DOC*/    "RLEACCEL flag to the call. This will take a short\n"
    /*DOC*/    "time to compile your surface, and increase the\n"
    /*DOC*/    "blitting speed.\n"
    /*DOC*/ ;

static PyObject* surf_set_alpha(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	Uint32 flags = 0;
	Uint8 alpha = 0;

	if(!PyArg_ParseTuple(args, "|bi", &alpha, &flags))
		return NULL;

	if(PyTuple_Size(args) > 0)
		flags |= SDL_SRCALPHA;

	if(SDL_SetAlpha(surf, flags, alpha) == -1)
		return RAISE(PyExc_SDLError, SDL_GetError());
	RETURN_NONE
}


    /*DOC*/ static char doc_surf_get_alpha[] =
    /*DOC*/    "Surface.get_alpha() -> alpha\n"
    /*DOC*/    "query alpha information\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the current alpha value for the Surface.\n"
    /*DOC*/    "If transparency is disabled for the Surface, it\n"
    /*DOC*/    "returns None.\n"
    /*DOC*/ ;

static PyObject* surf_get_alpha(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);

	if(!PyArg_ParseTuple(args, ""))
		return NULL;
	
	if(surf->flags&SDL_SRCALPHA)
		return PyInt_FromLong(surf->format->alpha);

	RETURN_NONE
}


    /*DOC*/ static char doc_surf_convert[] =
    /*DOC*/    "Surface.convert([src_surface]) -> Surface\n"
    /*DOC*/    "new copy of surface with different format\n"
    /*DOC*/    "\n"
    /*DOC*/    "Creates a new copy of the surface with the desired\n"
    /*DOC*/    "pixel format. Surfaces with the same pixel format\n"
    /*DOC*/    "will blit much faster than those with mixed\n"
    /*DOC*/    "formats. The pixel format of the new surface will\n"
    /*DOC*/    "match the format given as the argument. If no\n"
    /*DOC*/    "surface is given, the new surface will have the\n"
    /*DOC*/    "same pixel format as the current display.\n"
    /*DOC*/ ;

static PyObject* surf_convert(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	PySurfaceObject* srcsurf = NULL;
	SDL_Surface* src;
	SDL_Surface* newsurf;
	
	if(!PyArg_ParseTuple(args, "|O!", &PySurface_Type, &srcsurf))
		return NULL;

	if(srcsurf)
	{
		src = PySurface_AsSurface(srcsurf);
		newsurf = SDL_ConvertSurface(surf, src->format, src->flags);
	}
	else
		newsurf = SDL_DisplayFormat(surf);

	return PySurface_New(newsurf);
}



    /*DOC*/ static char doc_surf_convert_alpha[] =
    /*DOC*/    "Surface.convert_alpha([src_surface]) -> Surface\n"
    /*DOC*/    "new copy of surface with different format and per pixel alpha\n"
    /*DOC*/    "\n"
    /*DOC*/    "Creates a new copy of the surface with the desired\n"
    /*DOC*/    "pixel format. The new surface will be in a format\n"
    /*DOC*/    "suited for quick blitting to the given format with\n"
    /*DOC*/    "per pixel alpha. If no surface is given, the new\n"
    /*DOC*/    "surface will be optimized for blittint to the\n"
    /*DOC*/    "current display.\n"
    /*DOC*/    "\n"
    /*DOC*/    "Unlike the convert() method, the pixel format for\n"
    /*DOC*/    "the new image will not be exactly the same as the\n"
    /*DOC*/    "requested source, but it will be optimized for\n"
    /*DOC*/    "fast alpha blitting to the destination.\n"
    /*DOC*/ ;

static PyObject* surf_convert_alpha(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	PySurfaceObject* srcsurf = NULL;
	SDL_Surface* newsurf, *src;
	
	if(!PyArg_ParseTuple(args, "|O!", &PySurface_Type, &srcsurf))
		return NULL;

	if(srcsurf)
	{
		/*hmm, we have to figure this out, not all depths have good support for alpha*/
		src = PySurface_AsSurface(srcsurf);
		newsurf = SDL_DisplayFormatAlpha(surf);
	}
	else
		newsurf = SDL_DisplayFormatAlpha(surf);

	return PySurface_New(newsurf);
}


    /*DOC*/ static char doc_surf_set_clip[] =
    /*DOC*/    "Surface.set_clip([rectstyle])) -> Rect\n"
    /*DOC*/    "assign destination clipping rectangle\n"
    /*DOC*/    "\n"
    /*DOC*/    "Assigns the destination clipping rectangle for the\n"
    /*DOC*/    "Surface. When blit or fill operations are\n"
    /*DOC*/    "performed on the Surface, they are restricted to\n"
    /*DOC*/    "the inside of the clipping rectangle. If no\n"
    /*DOC*/    "rectangle is passed, the clipping region is set to\n"
    /*DOC*/    "the entire Surface area.\n"
    /*DOC*/ ;

static PyObject* surf_set_clip(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	GAME_Rect *rect, temp;
	PyObject* r = NULL;
	int result;
	
	if(!PyArg_ParseTuple(args, "|O", &r))
		return NULL;

	if(!r)
		rect = NULL;
	else if(!(rect = GameRect_FromObject(r, &temp)))
		return RAISE(PyExc_ValueError, "invalid rectstyle object");

	result = SDL_SetClipRect(surf, (SDL_Rect*)rect);
	if(result == -1)
		return RAISE(PyExc_SDLError, SDL_GetError());
	return PyRect_New(rect);
}



    /*DOC*/ static char doc_surf_get_clip[] =
    /*DOC*/    "Surface.get_clip() -> rect\n"
    /*DOC*/    "query the clipping area\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the current destination clipping area\n"
    /*DOC*/    "being used by the Surface. If the clipping area is\n"
    /*DOC*/    "not set, it will return a rectangle containing the\n"
    /*DOC*/    "full Surface area.\n"
    /*DOC*/ ;

static PyObject* surf_get_clip(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return PyRect_New((GAME_Rect*)&surf->clip_rect);
}



    /*DOC*/ static char doc_surf_fill[] =
    /*DOC*/    "Surface.fill(color, [rectstyle])) -> Rect\n"
    /*DOC*/    "fill areas of a Surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "Fills the specified area of the Surface with the\n"
    /*DOC*/    "mapped color value. If no destination rectangle is\n"
    /*DOC*/    "supplied, it will fill the entire Surface.\n"
    /*DOC*/    "\n"
    /*DOC*/    "The fill is subject to be clipped by the active\n"
    /*DOC*/    "clipping rectangle. The return value contains the\n"
    /*DOC*/    "actual area filled.\n"
    /*DOC*/ ;

static PyObject* surf_fill(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	GAME_Rect *rect, temp;
	PyObject* r = NULL;
	Uint32 color;
	int result;
	
	if(!PyArg_ParseTuple(args, "i|O", &color, &r))
		return NULL;

	if(!r)
	{
		rect = &temp;
		temp.x = temp.y = (short)0;
		temp.w = surf->w;
		temp.h = surf->h;
	}
	else if(!(rect = GameRect_FromObject(r, &temp)))
		return RAISE(PyExc_ValueError, "invalid rectstyle object");

	result = SDL_FillRect(surf, (SDL_Rect*)rect, color);
	if(result == -1)
		return RAISE(PyExc_SDLError, SDL_GetError());
	return PyRect_New(rect);
}


/*clip blit source rect to source image*/
static void screencroprect(GAME_Rect* r, int w, int h)
{
	if(r->x >= w || r->y >= h)
		r->x = r->y = r->w = r->h = 0;
	else
	{
		if(r->x < 0) r->x = 0;
		if(r->y < 0) r->y = 0;
		if(r->x + r->w >= w) r->w = (w-1)-r->x;
		if(r->y + r->h >= h) r->h = (h-1)-r->y;
	}
}



    /*DOC*/ static char doc_surf_blit[] =
    /*DOC*/    "Surface.blit(source, destoffset, [srcoffset, [size]]) -> Rect\n"
    /*DOC*/    "copy a one Surface to another.\n"
    /*DOC*/    "\n"
    /*DOC*/    "The blitting will transfer one surface to another.\n"
    /*DOC*/    "It will respect any special modes like colorkeying\n"
    /*DOC*/    "and alpha. If hardware support is available, it\n"
    /*DOC*/    "will be used. The given source is the Surface to\n"
    /*DOC*/    "copy from. The destoffset is a 2-number-sequence\n"
    /*DOC*/    "that specifies where on the destination Surface\n"
    /*DOC*/    "the blit happens. Without srcoffset and size\n"
    /*DOC*/    "supplied, the blit will copy the entire source\n"
    /*DOC*/    "surface. If you would like to copy only a portion\n"
    /*DOC*/    "of the source, use the srcoffset and size\n"
    /*DOC*/    "arguements to control what area is copied.\n"
    /*DOC*/    "\n"
    /*DOC*/    "The blit is subject to be clipped by the active\n"
    /*DOC*/    "clipping rectangle. The return value contains the\n"
    /*DOC*/    "actual area blitted.\n"
    /*DOC*/ ;

static PyObject* surf_blit(PyObject* self, PyObject* args)
{
	SDL_Surface* src, *dest = PySurface_AsSurface(self);
	PyObject* srcobject;
	int w, h, dx, dy, sx, sy;
	SDL_Rect dest_rect, src_rect;
	int result;

	dx = dy = sx = sy = w = h = 0;

	if(!PyArg_ParseTuple(args, "O!(ii)|(ii)(ii)", &PySurface_Type, &srcobject,
				&dx, &dy, &sx, &sy, &w, &h))
		return NULL;
	src = PySurface_AsSurface(srcobject);

	if(w && h)
	{
		dest_rect.w = src_rect.w = (unsigned short)w;
		dest_rect.h = src_rect.h = (unsigned short)h;
	}
	else
	{
		dest_rect.w = src_rect.w = src->w;
		dest_rect.h = src_rect.h = src->h;
	}
	dest_rect.x = (short)dx;
	dest_rect.y = (short)dy;
	src_rect.x = (short)sx;
	src_rect.y = (short)sy;

	result = SDL_BlitSurface(src, &src_rect, dest, &dest_rect);
	if(result == -1)
		return RAISE(PyExc_SDLError, SDL_GetError());

	return PyRect_New((GAME_Rect*)&dest_rect);
}




    /*DOC*/ static char doc_surf_get_size[] =
    /*DOC*/    "Surface.get_size() -> x, y\n"
    /*DOC*/    "query the surface size\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the width and height of the Surface.\n"
    /*DOC*/ ;

static PyObject* surf_get_size(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return Py_BuildValue("(ii)", surf->w, surf->h);
}



    /*DOC*/ static char doc_surf_get_width[] =
    /*DOC*/    "Surface.get_width() -> width\n"
    /*DOC*/    "query the surface width\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the width of the Surface.\n"
    /*DOC*/ ;

static PyObject* surf_get_width(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return PyInt_FromLong(surf->w);
}



    /*DOC*/ static char doc_surf_get_height[] =
    /*DOC*/    "Surface.get_height() -> height\n"
    /*DOC*/    "query the surface height\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the height of the Surface.\n"
    /*DOC*/ ;

static PyObject* surf_get_height(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return PyInt_FromLong(surf->h);
}



    /*DOC*/ static char doc_surf_get_rect[] =
    /*DOC*/    "Surface.get_rect() -> rect\n"
    /*DOC*/    "get a rectangle covering the entire surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns a new rectangle covering the entire surface.\n"
    /*DOC*/    "This rectangle will always start at 0, 0 with a width.\n"
    /*DOC*/    "and height the same size as the image.\n"
    /*DOC*/ ;

static PyObject* surf_get_rect(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return PyRect_New4(0, 0, surf->w, surf->h);
}



    /*DOC*/ static char doc_surf_get_bitsize[] =
    /*DOC*/    "Surface.get_bitsize() -> int\n"
    /*DOC*/    "query size of pixel\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the number of bits used to represent each\n"
    /*DOC*/    "pixel. This value may not exactly fill the number\n"
    /*DOC*/    "of bytes used per pixel. For example a 15 bit\n"
    /*DOC*/    "Surface still requires a full 2 bytes.\n"
    /*DOC*/ ;

static PyObject* surf_get_bitsize(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return PyInt_FromLong(surf->format->BitsPerPixel);
}


    /*DOC*/ static char doc_surf_get_bytesize[] =
    /*DOC*/    "Surface.get_bytesize() -> int\n"
    /*DOC*/    "query size of pixel\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the number of bytes used to store each\n"
    /*DOC*/    "pixel.\n"
    /*DOC*/ ;

static PyObject* surf_get_bytesize(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return PyInt_FromLong(surf->format->BytesPerPixel);
}


    /*DOC*/ static char doc_surf_get_masks[] =
    /*DOC*/    "Surface.get_masks() -> redmask, greenmask, bluemask, alphamask\n"
    /*DOC*/    "get mapping bitmasks for each colorplane\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the bitmasks for each color plane. The\n"
    /*DOC*/    "bitmask is used to isolate each colorplane value\n"
    /*DOC*/    "from a mapped color value. A value of zero means\n"
    /*DOC*/    "that colorplane is not used (like alpha)\n"
    /*DOC*/ ;

static PyObject* surf_get_masks(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return Py_BuildValue("(iiii)", surf->format->Rmask, surf->format->Gmask,
				surf->format->Bmask, surf->format->Amask);
}


    /*DOC*/ static char doc_surf_get_shifts[] =
    /*DOC*/    "Surface.get_shifts() -> redshift, greenshift, blueshift, alphashift\n"
    /*DOC*/    "get mapping shifts for each colorplane\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the bitshifts used for each color plane.\n"
    /*DOC*/    "The shift is determine how many bits left-shifted\n"
    /*DOC*/    "a colorplane value is in a mapped color value.\n"
    /*DOC*/ ;

static PyObject* surf_get_shifts(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return Py_BuildValue("(iiii)", surf->format->Rshift, surf->format->Gshift,
				surf->format->Bshift, surf->format->Ashift);
}


    /*DOC*/ static char doc_surf_get_losses[] =
    /*DOC*/    "Surface.get_losses() -> redloss, greenloss, blueloss, alphaloss\n"
    /*DOC*/    "get mapping losses for each colorplane\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the bitloss for each color plane. The loss\n"
    /*DOC*/    "is the number of bits removed for each colorplane\n"
    /*DOC*/    "from a full 8 bits of resolution. A value of 8\n"
    /*DOC*/    "usually indicates that colorplane is not used\n"
    /*DOC*/    "(like the alpha)\n"
    /*DOC*/ ;

static PyObject* surf_get_losses(PyObject* self, PyObject* args)
{
	SDL_Surface* surf = PySurface_AsSurface(self);
	return Py_BuildValue("(iiii)", surf->format->Rloss, surf->format->Gloss,
				surf->format->Bloss, surf->format->Aloss);
}




static struct PyMethodDef surface_methods[] =
{
	{"get_at",			surf_get_at,		1, doc_surf_get_at },
	{"set_at",			surf_set_at,		1, doc_surf_set_at },

	{"map_rgb",			surf_map_rgb,		1, doc_surf_map_rgb },
	{"unmap_rgb",		surf_unmap_rgb,		1, doc_surf_unmap_rgb },
	{"map_rgba",		surf_map_rgba,		1, doc_surf_map_rgba },
	{"unmap_rgba",		surf_unmap_rgba,	1, doc_surf_unmap_rgba },

	{"get_palette",		surf_get_palette,	1, doc_surf_get_palette },
	{"get_palette_at",	surf_get_palette_at,1, doc_surf_get_palette_at },
	{"set_palette",		surf_set_palette,	1, doc_surf_set_palette },
	{"set_palette_at",	surf_set_palette_at,1, doc_surf_set_palette_at },

	{"lock",			surf_lock,			1, doc_surf_lock },
	{"unlock",			surf_unlock,		1, doc_surf_unlock },
	{"mustlock",		surf_mustlock,		1, doc_surf_mustlock },

	{"set_colorkey",	surf_set_colorkey,	1, doc_surf_set_colorkey },
	{"get_colorkey",	surf_get_colorkey,	1, doc_surf_get_colorkey },
	{"set_alpha",		surf_set_alpha,		1, doc_surf_set_alpha },
	{"get_alpha",		surf_get_alpha,		1, doc_surf_get_alpha },

	{"convert",			surf_convert,		1, doc_surf_convert },
	{"convert_alpha",	surf_convert_alpha,	1, doc_surf_convert_alpha },

	{"set_clip",		surf_set_clip,		1, doc_surf_set_clip },
	{"get_clip",		surf_get_clip,		1, doc_surf_get_clip },

	{"fill",			surf_fill,			1, doc_surf_fill },
	{"blit",			surf_blit,			1, doc_surf_blit },

/*	{"get_flags",		surf_get_flags,		1, doc_surf_get_flags },*/
	{"get_size",		surf_get_size,		1, doc_surf_get_size },
	{"get_width",		surf_get_width,		1, doc_surf_get_width },
	{"get_height",		surf_get_height,	1, doc_surf_get_height },
	{"get_rect",		surf_get_rect,		1, doc_surf_get_rect },
/*	{"get_pitch",		surf_get_pitch,		1, doc_surf_get_pitch },*/
	{"get_bitsize",		surf_get_bitsize,	1, doc_surf_get_bitsize },
	{"get_bytesize",	surf_get_bytesize,	1, doc_surf_get_bytesize },
	{"get_masks",		surf_get_masks,		1, doc_surf_get_masks },
	{"get_shifts",		surf_get_shifts,	1, doc_surf_get_shifts },
	{"get_losses",		surf_get_losses,	1, doc_surf_get_losses },

	{NULL,		NULL}
};



/* surface object internals */

static void surface_dealloc(PyObject* self)
{
	PySurfaceObject* surf = (PySurfaceObject*)self;

	if(SDL_WasInit(SDL_INIT_VIDEO))
		SDL_FreeSurface(surf->surf);
	PyMem_DEL(self);	
}



static PyObject *surface_getattr(PyObject *self, char *name)
{
	return Py_FindMethod(surface_methods, (PyObject *)self, name);
}


PyObject* surface_str(PyObject* self)
{
	char str[1024];
	SDL_Surface* surf = PySurface_AsSurface(self);
	const char* type;

	type = (surf->flags&SDL_HWSURFACE)?"HW":"SW";

	sprintf(str, "<Surface(%dx%dx%d %s)>", surf->w, surf->h, surf->format->BitsPerPixel, type);

	return PyString_FromString(str);
}


static PyTypeObject PySurface_Type =
{
	PyObject_HEAD_INIT(NULL)
	0,						/*size*/
	"Surface",				/*name*/
	sizeof(PySurfaceObject),/*basic size*/
	0,						/*itemsize*/
	surface_dealloc,		/*dealloc*/
	0,						/*print*/
	surface_getattr,		/*getattr*/
	NULL,					/*setattr*/
	NULL,					/*compare*/
	surface_str,			/*repr*/
	NULL,					/*as_number*/
	NULL,					/*as_sequence*/
	NULL,					/*as_mapping*/
	(hashfunc)NULL,			/*hash*/
	(ternaryfunc)NULL,		/*call*/
	(reprfunc)NULL,			/*str*/
};


static PyObject* PySurface_New(SDL_Surface* s)
{
	PySurfaceObject* surf;

	if(!s) return RAISE(PyExc_SDLError, SDL_GetError());

	surf = PyObject_NEW(PySurfaceObject, &PySurface_Type);
	if(surf)
		surf->surf = s;

	return (PyObject*)surf;
}



/* surface module functions */

    /*DOC*/ static char doc_surface[] =
    /*DOC*/    "pygame.surface(size, [flags, [depth|Surface, [masks]]]) -> Surface\n"
    /*DOC*/    "create a new Surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "Creates a new surface object. Size is a\n"
    /*DOC*/    "2-int-sequence containing width and height. Depth\n"
    /*DOC*/    "is the number of bits used per pixel. If omitted,\n"
    /*DOC*/    "depth will use the current\n"
    /*DOC*/    "display depth. Masks is a four item sequence\n"
    /*DOC*/    "containing the bitmask for r,g,b, and a. If\n"
    /*DOC*/    "omitted, masks will default to the usual values\n"
    /*DOC*/    "for the given bitdepth. Flags is a mix of the\n"
    /*DOC*/    "following flags: SWSURFACE, HWSURFACE, ASYNCBLIT,\n"
    /*DOC*/    "SRCCOLORKEY, or SRCALPHA. (flags = 0 is the same\n"
    /*DOC*/    "as SWSURFACE). depth and masks can be substituted\n"
    /*DOC*/    "for another surface object which will create the\n"
    /*DOC*/    "new surface with the same format as the given one.\n"
    /*DOC*/    "When using default masks, alpha will always be\n"
    /*DOC*/    "ignored. Note, if you pass SRCOLORKEY and/or\n"
    /*DOC*/    "SRCALPHA, the surface won't immediately have these\n"
    /*DOC*/    "features enabled. SDL will use these flags to help\n"
    /*DOC*/    "optimize the surface for use with the blitters.\n"
    /*DOC*/    "Also, for a plain software surface, 0 can be used\n"
    /*DOC*/    "for the flag. A plain hardware surface can just\n"
    /*DOC*/    "use 1 for the flag.\n"
    /*DOC*/ ;

static PyObject* surface(PyObject* self, PyObject* arg)
{
	Uint32 flags = 0;
	int width, height;
	PyObject *depth=NULL, *masks=NULL;
	short bpp;
	Uint32 Rmask, Gmask, Bmask, Amask;
	SDL_Surface* surface;

	if(!PyArg_ParseTuple(arg, "(ii)|iOO", &width, &height, &flags, &depth, &masks))
		return NULL;

	VIDEO_INIT_CHECK();
		
	if(depth && masks) /*all info supplied, most errorchecking needed*/
	{
		if(PySurface_Check(depth))
			return RAISE(PyExc_ValueError, "cannot pass surface for depth and color masks");
		if(!ShortFromObj(depth, &bpp))
			return RAISE(PyExc_ValueError, "invalid bits per pixel depth argument");
		if(!PySequence_Check(masks) || PySequence_Length(masks)!=4)
			return RAISE(PyExc_ValueError, "masks argument must be sequence of four numbers");
		if(!UintFromObjIndex(masks, 0, &Rmask) || !UintFromObjIndex(masks, 1, &Gmask) ||
					!UintFromObjIndex(masks, 2, &Bmask) || !UintFromObjIndex(masks, 3, &Amask))
			return RAISE(PyExc_ValueError, "invalid mask values in masks sequence");
	}
	else if(depth && PyNumber_Check(depth))/*use default masks*/
	{
		if(!ShortFromObj(depth, &bpp))
			return RAISE(PyExc_ValueError, "invalid bits per pixel depth argument");
		Amask = 0;
		switch(bpp)
		{
		case 8:
			Rmask = 0xFF >> 6 << 5; Gmask = 0xFF >> 5 << 2; Bmask = 0xFF >> 6; break;
		case 12:
			Rmask = 0xFF >> 4 << 8; Gmask = 0xFF >> 4 << 4; Bmask = 0xFF >> 4; break;
		case 15:
			Rmask = 0xFF >> 3 << 10; Gmask = 0xFF >> 3 << 5; Bmask = 0xFF >> 3; break;
		case 16:
			Rmask = 0xFF >> 3 << 11; Gmask = 0xFF >> 2 << 5; Bmask = 0xFF >> 3; break;
		case 24:
		case 32:
			Rmask = 0xFF << 16; Gmask = 0xFF << 8; Bmask = 0xFF; break;
		default:
			return RAISE(PyExc_ValueError, "no standard masks exist for given bitdepth");
		}
	}
	else /*no depth or surface*/
	{
		SDL_PixelFormat* pix;
		if(depth && PySurface_Check(depth))
			pix = ((PySurfaceObject*)depth)->surf->format;
		else if(SDL_GetVideoSurface())
			pix = SDL_GetVideoSurface()->format;
		else
			pix = SDL_GetVideoInfo()->vfmt;
		bpp = pix->BitsPerPixel;
		Rmask = pix->Rmask;
		Gmask = pix->Gmask;
		Bmask = pix->Bmask;
		Amask = pix->Amask;
	}

	surface = SDL_CreateRGBSurface(flags, width, height, bpp, Rmask, Gmask, Bmask, Amask);
	return PySurface_New(surface);
}




static PyMethodDef surface_builtins[] =
{
	{ "surface", surface, 1, doc_surface },
	{ NULL, NULL }
};



    /*DOC*/ static char doc_Surface_MODULE[] =
    /*DOC*/    "Contains the Surface object.\n"
    /*DOC*/ ;

void initsurface()
{
	PyObject *module, *dict, *apiobj;
	static void* c_api[PYGAMEAPI_SURFACE_NUMSLOTS];

	PyType_Init(PySurface_Type);


    /* create the module */
	module = Py_InitModule3("surface", surface_builtins, doc_Surface_MODULE);
	dict = PyModule_GetDict(module);

	/* export the c api */
	c_api[0] = &PySurface_Type;
	c_api[1] = PySurface_New;
	apiobj = PyCObject_FromVoidPtr(c_api, NULL);
	PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);

	/*imported needed apis*/
	import_pygame_base();
	import_pygame_rect();
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.