Source

pygame / src / surfarray.c

ahvezda 99d32cc 









































































































































































































































































































































































































































































































































































































































































  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
/*
    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
*/

#include"pygame.h"
#include<Numeric/arrayobject.h>
#include<SDL_byteorder.h>



    /*DOC*/ static char doc_pixels3d[] =
    /*DOC*/    "pygame.surfarray.pixels3d(Surface) -> Array\n"
    /*DOC*/    "get a 3d reference array to a surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "This returns a new noncontigous 3d array that\n"
    /*DOC*/    "directly effects a Surface's contents. Think of it\n"
    /*DOC*/    "as a 2d image array with an RGB array for each\n"
    /*DOC*/    "pixel value.\n"
    /*DOC*/    "\n"
    /*DOC*/    "This will only work for 24 and 32 bit surfaces,\n"
    /*DOC*/    "where the RGB components can be accessed without\n"
    /*DOC*/    "requiring any masking.\n"
    /*DOC*/    "\n"
    /*DOC*/    "You'll need the surface to be locked if that is\n"
    /*DOC*/    "required. Also be aware that between unlocking and\n"
    /*DOC*/    "relocking a surface, the pixel data can be moved,\n"
    /*DOC*/    "so don't hang onto this array after you have\n"
    /*DOC*/    "unlocked the surface.\n"
    /*DOC*/ ;

static PyObject* pixels3d(PyObject* self, PyObject* arg)
{
	int dim[3];
	PyObject* array;
	SDL_Surface* surf;
	char* startpixel;
	int pixelstep;

	if(!PyArg_ParseTuple(arg, "O!", &PySurface_Type, &array))
		return NULL;
	surf = PySurface_AsSurface(array);

	if(surf->format->BytesPerPixel <= 2 || surf->format->BytesPerPixel > 4)
		return RAISE(PyExc_ValueError, "unsupport bit depth for 3D reference array");

	/*must discover information about how data is packed*/
	if(SDL_BYTEORDER == SDL_LIL_ENDIAN) /*intel-style*/
	{
		if(surf->format->Rmask == 0xff<<16 && 
					surf->format->Gmask == 0xff<<8 &&
					surf->format->Bmask == 0xff)
		{
			pixelstep = -1;
			startpixel = ((char*)surf->pixels)+2;
		}
		else if(surf->format->Bmask == 0xff<<16 && 
					surf->format->Gmask == 0xff<<8 &&
					surf->format->Rmask == 0xff)
		{
			pixelstep = 1;
			startpixel = ((char*)surf->pixels);
		}
		else
			return RAISE(PyExc_ValueError, "unsupport colormasks for 3D reference array");
	}
	else /*mips-style*/
	{
		if(surf->format->Rmask == 0xff<<16 && 
					surf->format->Gmask == 0xff<<8 &&
					surf->format->Bmask == 0xff)
		{
			pixelstep = 1;
			startpixel = ((char*)surf->pixels);
		}
		else if(surf->format->Bmask == 0xff<<16 && 
					surf->format->Gmask == 0xff<<8 &&
					surf->format->Rmask == 0xff)
		{
			pixelstep = -1;
			startpixel = ((char*)surf->pixels)+2;
		}
		else
			return RAISE(PyExc_ValueError, "unsupport colormasks 3D reference array");
	}

	dim[0] = surf->w;
	dim[1] = surf->h;
	dim[2] = 3; /*could be 4 if alpha in the house*/
	array = PyArray_FromDimsAndData(3, dim, PyArray_UBYTE, startpixel);
	if(array)
	{
		((PyArrayObject*)array)->flags = OWN_DIMENSIONS|OWN_STRIDES|SAVESPACE;
		((PyArrayObject*)array)->strides[2] = pixelstep;
		((PyArrayObject*)array)->strides[1] = surf->pitch;
		((PyArrayObject*)array)->strides[0] = surf->format->BytesPerPixel;
		((PyArrayObject*)array)->base = array;
		Py_INCREF(array);
	}
	return array;
}



    /*DOC*/ static char doc_pixels2d[] =
    /*DOC*/    "pygame.surfarray.pixels2d(Surface) -> Array\n"
    /*DOC*/    "get a 2d reference array to a surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "This returns a new noncontigous 2d array that\n"
    /*DOC*/    "directly effects a Surface's contents. Think of it\n"
    /*DOC*/    "as a 2d image array with a mapped pixel value at\n"
    /*DOC*/    "each index.\n"
    /*DOC*/    "\n"
    /*DOC*/    "This will not work on 24bit surfaces, since there\n"
    /*DOC*/    "is no native 24bit data type to access the pixel\n"
    /*DOC*/    "values.\n"
    /*DOC*/    "\n"
    /*DOC*/    "You'll need the surface to be locked if that is\n"
    /*DOC*/    "required. Also be aware that between unlocking and\n"
    /*DOC*/    "relocking a surface, the pixel data can be moved,\n"
    /*DOC*/    "so don't hang onto this array after you have\n"
    /*DOC*/    "unlocked the surface.\n"
    /*DOC*/ ;

static PyObject* pixels2d(PyObject* self, PyObject* arg)
{
	int types[] = {PyArray_UBYTE, PyArray_SHORT, 0, PyArray_INT};
	int dim[3];
	int type;
	PyObject* array;
	SDL_Surface* surf;

	if(!PyArg_ParseTuple(arg, "O!", &PySurface_Type, &array))
		return NULL;
	surf = PySurface_AsSurface(array);


	if(surf->format->BytesPerPixel == 3 || surf->format->BytesPerPixel < 1 || surf->format->BytesPerPixel > 4)
	{
		PyErr_SetString(PyExc_ValueError, "unsupport bit depth for 2D reference array");
		return NULL;
	}

	dim[0] = surf->w;
	dim[1] = surf->h;
	type = types[surf->format->BytesPerPixel-1];
	array = PyArray_FromDimsAndData(2, dim, type, (char*)surf->pixels);
	if(array)
	{
		((PyArrayObject*)array)->strides[1] = surf->pitch;
		((PyArrayObject*)array)->strides[0] = surf->format->BytesPerPixel;
		((PyArrayObject*)array)->flags = OWN_DIMENSIONS|OWN_STRIDES;
	}
	return array;
}



    /*DOC*/ static char doc_array2d[] =
    /*DOC*/    "pygame.surfarray.array2d(Surface) -> Array\n"
    /*DOC*/    "get a 2d array copied from a surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "This returns a new contigous 2d array. Think of it\n"
    /*DOC*/    "as a 2d image array with a mapped pixel value at\n"
    /*DOC*/    "each index.\n"
    /*DOC*/    "\n"
    /*DOC*/    "You'll need the surface to be locked if that is\n"
    /*DOC*/    "required. Once the array is created you can unlock\n"
    /*DOC*/    "the surface.\n"
    /*DOC*/ ;

PyObject* array2d(PyObject* self, PyObject* arg)
{
	int dim[2], loopy;
	int* data;
	PyObject* array;
	SDL_Surface* surf;

	if(!PyArg_ParseTuple(arg, "O!", &PySurface_Type, &array))
		return NULL;
	surf = PySurface_AsSurface(array);

	dim[0] = surf->w;
	dim[1] = surf->h;

	if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
		return RAISE(PyExc_ValueError, "unsupport bit depth for surface array");

	array = PyArray_FromDims(2, dim, PyArray_INT);
	if(!array) return NULL;

	data = (int*)((PyArrayObject*)array)->data;
	
	switch(surf->format->BytesPerPixel)
	{
	case 1:
		for(loopy = 0; loopy < surf->h; ++loopy)
		{
			char* pix = (char*)(((char*)surf->pixels)+loopy*surf->pitch);
			char* end = (char*)(((char*)pix)+surf->w*surf->format->BytesPerPixel);
			while(pix <= end)
				*data++ = *pix++;
		}break;
	case 2:
		for(loopy = 0; loopy < surf->h; ++loopy)
		{
			short* pix = (short*)(((char*)surf->pixels)+loopy*surf->pitch);
			short* end = (short*)(((char*)pix)+surf->w*surf->format->BytesPerPixel);
			while(pix <= end)
				*data++ = *pix++;
		}break;
	case 3:
		for(loopy = 0; loopy < surf->h; ++loopy)
		{
			char* pix = (char*)(((char*)surf->pixels)+loopy*surf->pitch);
			char* end = (char*)(((char*)pix)+surf->w*surf->format->BytesPerPixel);
			while(pix <= end)
			{
				*data++ = (*(int*)pix) >> 8;
				pix += 3;
			}
		}break;
	default: /*case 4*/
		for(loopy = 0; loopy < surf->h; ++loopy)
		{
			int* pix = (int*)(((char*)surf->pixels)+loopy*surf->pitch);
			int* end = (int*)(((char*)pix)+surf->w*surf->format->BytesPerPixel);
			while(pix <= end)
				*data++ = *pix++;
		}break;
	}

	return array;
}



    /*DOC*/ static char doc_array3d[] =
    /*DOC*/    "pygame.surfarray.array3d(Surface) -> Array\n"
    /*DOC*/    "get a 3d array copied from a surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "This returns a new contigous 3d array. Think of it\n"
    /*DOC*/    "as a 2d image array with an RGB array for each\n"
    /*DOC*/    "pixel value.\n"
    /*DOC*/    "\n"
    /*DOC*/    "You'll need the surface to be locked if that is\n"
    /*DOC*/    "required. Once the array is created you can unlock\n"
    /*DOC*/    "the surface.\n"
    /*DOC*/ ;

PyObject* array3d(PyObject* self, PyObject* arg)
{
	int dim[3], loopy;
	Uint8* data;
	PyObject* array;
	SDL_Surface* surf;
	SDL_PixelFormat* format;
	int Rmask, Gmask, Bmask, Rshift, Gshift, Bshift;

	if(!PyArg_ParseTuple(arg, "O!", &PySurface_Type, &array))
		return NULL;
	surf = PySurface_AsSurface(array);

	format = surf->format;
	dim[0] = surf->w;
	dim[1] = surf->h;
	dim[2] = 3;

	if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
		return RAISE(PyExc_ValueError, "unsupport bit depth for surface array");

	array = PyArray_FromDims(3, dim, PyArray_UBYTE);
	if(!array) return NULL;

	data = (Uint8*)((PyArrayObject*)array)->data;
	Rmask = format->Rmask; Gmask = format->Gmask; Bmask = format->Bmask;
	Rshift = format->Rshift; Gshift = format->Gshift; Bshift = format->Bshift;
	
	switch(surf->format->BytesPerPixel)
	{
	case 1:
		return RAISE(PyExc_ValueError, "colormaps unsupported");
	case 2:
		for(loopy = 0; loopy < surf->h; ++loopy)
		{
			short* pix = (short*)(((char*)surf->pixels)+loopy*surf->pitch);
			short* end = (short*)(((char*)pix)+surf->w*surf->format->BytesPerPixel);
			while(pix <= end)
			{
				short color = *pix++;
				*data++ = (color&Rmask)>>Rshift;
				*data++ = (color&Gmask)>>Gshift;
				*data++ = (color&Bmask)>>Bshift;
			}
		}break;
	case 3:
		for(loopy = 0; loopy < surf->h; ++loopy)
		{
			char* pix = (char*)(((char*)surf->pixels)+loopy*surf->pitch);
			char* end = (char*)(((char*)pix)+surf->w*surf->format->BytesPerPixel);
			while(pix <= end)
			{
				int color = (*(int*)pix++) >> 8;
				*data++ = (color&Rmask)>>Rshift;
				*data++ = (color&Gmask)>>Gshift;
				*data++ = (color&Bmask)>>Bshift;
			}
		}break;
	default: /*case 4*/
		for(loopy = 0; loopy < surf->h; ++loopy)
		{
			int* pix = (int*)(((char*)surf->pixels)+loopy*surf->pitch);
			int* end = (int*)(((char*)pix)+surf->w*surf->format->BytesPerPixel);
			while(pix <= end)
			{
				int color = *pix++;
				*data++ = (color&Rmask)>>Rshift;
				*data++ = (color&Gmask)>>Gshift;
				*data++ = (color&Bmask)>>Bshift;
			}
		}break;
	}

	return array;
}




    /*DOC*/ static char doc_map_array[] =
    /*DOC*/    "pygame.surfarray.map_array(surf, array3d) -> array2d\n"
    /*DOC*/    "map an array with RGB values into mapped colors\n"
    /*DOC*/    "\n"
    /*DOC*/    "Create a new array with the RGB pixel values of a\n"
    /*DOC*/    "3d array into mapped color values in a 2D array.\n"
    /*DOC*/    "\n"
    /*DOC*/    "Just so you know, this can also map a 2D array\n"
    /*DOC*/    "with RGB values into a 1D array of mapped color\n"
    /*DOC*/    "values\n"
    /*DOC*/ ;

PyObject* map_array(PyObject* self, PyObject* arg)
{
	int* data;
	PyObject *surfobj, *arrayobj, *newarray;
	SDL_Surface* surf;
	SDL_PixelFormat* format;
	PyArrayObject* array;
	int loopx, loopy;
	int stridex, stridey, stridez, stridez2, sizex, sizey;
	int dims[2];

	if(!PyArg_ParseTuple(arg, "O!O!", &PySurface_Type, &surfobj, &PyArray_Type, &arrayobj))
		return NULL;
	surf = PySurface_AsSurface(surfobj);
	format = surf->format;
	array = (PyArrayObject*)arrayobj;

	if(!array->nd || array->dimensions[array->nd-1] != 3)
		return RAISE(PyExc_ValueError, "array must be a 3d array of 3-value color data\n");

	if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
		return RAISE(PyExc_ValueError, "unsupport bit depth for surface array");

	switch(array->nd)
	{
	case 3: /*image of colors*/
		dims[0] = array->dimensions[0];
		dims[1] = array->dimensions[1];
		newarray = PyArray_FromDims(2, dims, PyArray_INT);
		if(!newarray) return NULL;
		data = (int*)((PyArrayObject*)newarray)->data;
		stridex = array->strides[0];
		stridey = array->strides[1];
		stridez = array->strides[2];
		sizex = array->dimensions[0];
		sizey = array->dimensions[1];
		break;
	case 2: /*list of colors*/
		dims[0] = array->dimensions[0];
		newarray = PyArray_FromDims(1, dims, PyArray_INT);
		if(!newarray) return NULL;
		data = (int*)((PyArrayObject*)newarray)->data;
		stridex = 0;
		stridey = array->strides[0];
		stridez = array->strides[1];
		sizex = 1;
		sizey = array->dimensions[0];
#if 0 /*kinda like a scalar here, use normal map_rgb*/
	case 1: /*single color*/
		dims[0] = 1;
		newarray = PyArray_FromDims(1, dims, PyArray_INT);
		if(!newarray) return NULL;
		data = (int*)((PyArrayObject*)newarray)->data;
		stridex = 0;
		stridey = 0;
		stridez = array->strides[0];
		sizex = 1;
		sizey = 1;
#endif
	default:
		return RAISE(PyExc_ValueError, "unsupported array shape");
	}
	stridez2 = stridez*2;


	switch(array->descr->elsize)
	{
	case sizeof(char):
		for(loopx = 0; loopx < sizex; ++loopx)
		{
			char* col = array->data + stridex * loopx;
			for(loopy = 0; loopy < sizey; ++loopy)
			{
				char* pix = col + stridey * loopy;
				*data++ =	(*((unsigned char*)(pix)) >>
								format->Rloss << format->Rshift) |
							(*((unsigned char*)(pix+stridez)) >>
								format->Gloss << format->Gshift) |
							(*((unsigned char*)(pix+stridez2)) >>
								format->Bloss << format->Bshift);
			}
		}break;
	case sizeof(short):
		for(loopx = 0; loopx < sizex; ++loopx)
		{
			char* col = array->data + stridex * loopx;
			for(loopy = 0; loopy < sizey; ++loopy)
			{
				char* pix = col + stridey * loopy;
				*data++ =	(*((unsigned short*)(pix)) >>
								format->Rloss << format->Rshift) |
							(*((unsigned short*)(pix+stridez)) >>
								format->Gloss << format->Gshift) |
							(*((unsigned short*)(pix+stridez2)) >>
								format->Bloss << format->Bshift);
			}
		}break;
	case sizeof(int):
		for(loopx = 0; loopx < sizex; ++loopx)
		{
			char* col = array->data + stridex * loopx;
			for(loopy = 0; loopy < sizey; ++loopy)
			{
				char* pix = col + stridey * loopy;
				*data++ =	(*((int*)(pix)) >>
								format->Rloss << format->Rshift) |
							(*((int*)(pix+stridez)) >>
								format->Gloss << format->Gshift) |
							(*((int*)(pix+stridez2)) >>
								format->Bloss << format->Bshift);
			}
		}break;
	default: 
		Py_DECREF(newarray);
		return RAISE(PyExc_ValueError, "unsupported bytesperpixel for array\n");
	}

	return newarray;
}



    /*DOC*/ static char doc_blit_array[] =
    /*DOC*/    "pygame.surfarray.blit_array(surf, array) -> None\n"
    /*DOC*/    "quickly transfer an array to a Surface\n"
    /*DOC*/    "\n"
    /*DOC*/    "Transfer an array of any type (3d or 2d) onto a\n"
    /*DOC*/    "Surface. The array must be the same dimensions as\n"
    /*DOC*/    "the destination Surface. While you can assign the\n"
    /*DOC*/    "values of an array to the pixel referenced arrays,\n"
    /*DOC*/    "using this blit method will usually be quicker\n"
    /*DOC*/    "because of it's smarter handling of noncontiguous\n"
    /*DOC*/    "arrays. Plus it allows you to blit from any image\n"
    /*DOC*/    "array type to any surface format in one step, no\n"
    /*DOC*/    "conversions.\n"
    /*DOC*/ ;

PyObject* blit_array(PyObject* self, PyObject* arg)
{
	PyObject *surfobj, *arrayobj;
	SDL_Surface* surf;
	SDL_PixelFormat* format;
	PyArrayObject* array;
	int loopx, loopy;
	int stridex, stridey, stridez, stridez2, sizex, sizey;
	int Rloss, Gloss, Bloss, Rshift, Gshift, Bshift;

	if(!PyArg_ParseTuple(arg, "O!O!", &PySurface_Type, &surfobj, &PyArray_Type, &arrayobj))
		return NULL;
	surf = PySurface_AsSurface(surfobj);
	format = surf->format;
	array = (PyArrayObject*)arrayobj;

	if(!(array->nd == 2 || (array->nd == 3 && array->dimensions[2] == 3)))
		return RAISE(PyExc_ValueError, "must be a valid 2d or 3d array\n");

	if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
		return RAISE(PyExc_ValueError, "unsupport bit depth for surface");

	stridex = array->strides[0];
	stridey = array->strides[1];
	if(array->nd == 3)
	{
		stridez = array->strides[2];
		stridez2 = stridez*2;
	}
	sizex = array->dimensions[0];
	sizey = array->dimensions[1];
	Rloss = format->Rloss; Gloss = format->Gloss; Bloss = format->Bloss;
	Rshift = format->Rshift; Gshift = format->Gshift; Bshift = format->Bshift;

	if(sizex != surf->w || sizey != surf->h)
		return RAISE(PyExc_ValueError, "array must match surface dimensions");

	switch(surf->format->BytesPerPixel)
	{
	case 1:
	case 2:
	case 3:
		return RAISE(PyExc_RuntimeError, "unsupported bit depth for image");
	case 4:
		{
			if(array->nd == 2)
			{
				if(array->descr->elsize == sizeof(char))
				{
					for(loopy = 0; loopy < sizey; ++loopy)
					{
						int* imgrow = (int*)(((char*)surf->pixels)+loopy*surf->pitch);
						char* datarow = array->data + stridey * loopy;
						for(loopx = 0; loopx < sizex; ++loopx)
						{
							int* pix = imgrow + loopx;
							char* data = datarow + stridex * loopx;
							*(imgrow + loopx) = (int)*(unsigned char*)(datarow + stridex * loopx);
						}
					}
				}
				else if(array->descr->elsize == sizeof(int))
				{
					for(loopy = 0; loopy < sizey; ++loopy)
					{
						int* imgrow = (int*)(((char*)surf->pixels)+loopy*surf->pitch);
						char* datarow = array->data + stridey * loopy;
						for(loopx = 0; loopx < sizex; ++loopx)
						{
							int* pix = imgrow + loopx;
							char* data = datarow + stridex * loopx;
							*(imgrow + loopx) = (int)*(int*)(datarow + stridex * loopx);
						}
					}
				}
			}
			else
			{
				switch(array->descr->elsize)
				{
				case sizeof(char):
					for(loopy = 0; loopy < sizey; ++loopy)
					{
						int* data = (int*)(((char*)surf->pixels) + surf->pitch * loopy);
						char* pix = array->data + stridey * loopy;
						for(loopx = 0; loopx < sizex; ++loopx)
						{
							*data++ = (*((unsigned char*)(pix)) >> Rloss << Rshift) |
									((*((unsigned char*)(pix+stridez))) >> Gloss << Gshift) |
									((*((unsigned char*)(pix+stridez2))) >> Bloss << Bshift);
							pix += stridex;
						}
					}break;
				case sizeof(int):
					for(loopy = 0; loopy < sizey; ++loopy)
					{
						int* data = (int*)(((char*)surf->pixels) + surf->pitch * loopy);
						char* pix = array->data + stridey * loopy;
						for(loopx = 0; loopx < sizex; ++loopx)
						{
							*data++ = (*((unsigned int*)(pix)) >> Rloss << Rshift) |
									((*((unsigned int*)(pix+stridez))) >> Gloss << Gshift) |
									((*((unsigned int*)(pix+stridez2))) >> Bloss << Bshift);
							pix += stridex;
						}
					}break;
				default: 
					return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
				}
			}
		}break;
	}

	RETURN_NONE;
}


static PyMethodDef surfarray_builtins[] =
{
	{ "pixels2d", pixels2d, 1, doc_pixels2d },
	{ "pixels3d", pixels3d, 1, doc_pixels3d },
	{ "array2d", array2d, 1, doc_array2d },
	{ "array3d", array3d, 1, doc_array3d },
	{ "map_array", map_array, 1, doc_map_array },
/*	{ "unmap_array", unmap_array, 1, doc_unmap_array },*/
	{ "blit_array", blit_array, 1, doc_blit_array },

	{ NULL, NULL }
};






    /*DOC*/ static char doc_pygame_surfarray_MODULE[] =
    /*DOC*/    "Contains routines for mixing numeric arrays with\n"
    /*DOC*/    "surfaces\n"
    /*DOC*/ ;

void initsurfarray()
{
	PyObject *module, *dict;

    /* create the module */
	module = Py_InitModule3("surfarray", surfarray_builtins, doc_pygame_surfarray_MODULE);
	dict = PyModule_GetDict(module);

	/*imported needed apis*/
	import_pygame_base();
	import_pygame_surface();
	import_array();
}