55
55
const int lilendian = (SDL_BYTEORDER == SDL_LIL_ENDIAN);
56
56
PyObject* lifelock;
58
58
if(!PyArg_ParseTuple(arg, "O!", &PySurface_Type, &surfobj))
60
60
surf = PySurface_AsSurface(surfobj);
66
66
if(!lifelock) return NULL;
68
68
/*must discover information about how data is packed*/
69
if(surf->format->Rmask == 0xff<<16 &&
69
if(surf->format->Rmask == 0xff<<16 &&
70
70
surf->format->Gmask == 0xff<<8 &&
71
71
surf->format->Bmask == 0xff)
73
73
pixelstep = (lilendian ? -1 : 1);
74
74
startpixel = ((char*)surf->pixels) + (lilendian ? 2 : 0);
76
else if(surf->format->Bmask == 0xff<<16 &&
76
else if(surf->format->Bmask == 0xff<<16 &&
77
77
surf->format->Gmask == 0xff<<8 &&
78
78
surf->format->Rmask == 0xff)
1020
1020
case sizeof(Uint8): COPYMACRO_2D(Uint8, Uint8) break;
1021
1021
case sizeof(Uint16): COPYMACRO_2D(Uint8, Uint16) break;
1022
1022
case sizeof(Uint32): COPYMACRO_2D(Uint8, Uint32) break;
1024
1024
if(!PySurface_Unlock(surfobj)) return NULL;
1025
1025
return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1032
1032
case sizeof(Uint8): COPYMACRO_2D(Uint16, Uint8) break;
1033
1033
case sizeof(Uint16): COPYMACRO_2D(Uint16, Uint16) break;
1034
1034
case sizeof(Uint32): COPYMACRO_2D(Uint16, Uint32) break;
1036
1036
if(!PySurface_Unlock(surfobj)) return NULL;
1037
1037
return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1041
1041
case sizeof(Uint8): COPYMACRO_3D(Uint16, Uint8) break;
1042
1042
case sizeof(Uint16):COPYMACRO_3D(Uint16, Uint16) break;
1043
1043
case sizeof(Uint32):COPYMACRO_3D(Uint16, Uint32) break;
1045
1045
if(!PySurface_Unlock(surfobj)) return NULL;
1046
1046
return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1053
1053
case sizeof(Uint8): COPYMACRO_2D_24(Uint8) break;
1054
1054
case sizeof(Uint16): COPYMACRO_2D_24(Uint16) break;
1055
1055
case sizeof(Uint32): COPYMACRO_2D_24(Uint32) break;
1057
1057
if(!PySurface_Unlock(surfobj)) return NULL;
1058
1058
return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1062
1062
case sizeof(Uint8): COPYMACRO_3D_24(Uint8) break;
1063
1063
case sizeof(Uint16):COPYMACRO_3D_24(Uint16) break;
1064
1064
case sizeof(Uint32):COPYMACRO_3D_24(Uint32) break;
1066
1066
if(!PySurface_Unlock(surfobj)) return NULL;
1067
1067
return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1074
1074
case sizeof(Uint8): COPYMACRO_2D(Uint32, Uint8) break;
1075
1075
case sizeof(Uint16): COPYMACRO_2D(Uint32, Uint16) break;
1076
1076
case sizeof(Uint32): COPYMACRO_2D(Uint32, Uint32) break;
1078
1078
if(!PySurface_Unlock(surfobj)) return NULL;
1079
1079
return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1083
1083
case sizeof(Uint8): COPYMACRO_3D(Uint32, Uint8) break;
1084
1084
case sizeof(Uint16):COPYMACRO_3D(Uint32, Uint16) break;
1085
1085
case sizeof(Uint32):COPYMACRO_3D(Uint32, Uint32) break;
1087
1087
if(!PySurface_Unlock(surfobj)) return NULL;
1088
1088
return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1102
/*DOC*/ static char doc_make_surface[] =
1103
/*DOC*/ "pygame.surfarray.make_surface(array) -> Surface\n"
1104
/*DOC*/ "create a new Surface from array data\n"
1106
/*DOC*/ "Create a new software surface that closely resembles\n"
1107
/*DOC*/ "the data and format of the image array data.\n"
1110
PyObject* make_surface(PyObject* self, PyObject* arg)
1112
PyObject *arrayobj, *surfobj, *args;
1114
PyArrayObject* array;
1115
int sizex, sizey, bitsperpixel;
1116
Uint32 rmask, gmask, bmask;
1118
if(!PyArg_ParseTuple(arg, "O!", &PyArray_Type, &arrayobj))
1120
array = (PyArrayObject*)arrayobj;
1122
if(!(array->nd == 2 || (array->nd == 3 && array->dimensions[2] == 3)))
1123
return RAISE(PyExc_ValueError, "must be a valid 2d or 3d array\n");
1124
if(array->descr->type_num > PyArray_LONG)
1125
return RAISE(PyExc_ValueError, "Invalid array datatype for surface");
1130
rmask = gmask = bmask = 0;
1135
rmask = 0xFF<<16; gmask = 0xFF<<8; bmask = 0xFF;
1137
sizex = array->dimensions[0];
1138
sizey = array->dimensions[1];
1140
surf = SDL_CreateRGBSurface(0, sizex, sizey, bitsperpixel, rmask, gmask, bmask, 0);
1142
return RAISE(PyExc_SDLError, SDL_GetError());
1143
surfobj = PySurface_New(surf);
1146
SDL_FreeSurface(surf);
1150
args = Py_BuildValue("(OO)", surfobj, array);
1156
blit_array(NULL, args);
1159
if(PyErr_Occurred())
1102
1169
static PyMethodDef surfarray_builtins[] =
1104
1171
{ "pixels2d", pixels2d, 1, doc_pixels2d },
1112
1179
/* { "unmap_array", unmap_array, 1, doc_unmap_array },*/
1113
1180
{ "blit_array", blit_array, 1, doc_blit_array },
1114
1181
/* { "clamp_array", clamp_array, 1, doc_clamp_array }, not quick enough to be worthwhile :[ */
1182
{ "make_surface", make_surface, 1, doc_make_surface },
1123
1191
/*DOC*/ static char doc_pygame_surfarray_MODULE[] =
1124
1192
/*DOC*/ "Contains routines for mixing numeric arrays with\n"
1125
/*DOC*/ "surfaces\n"
1193
/*DOC*/ "surfaces. You can create arrays that directly reference\n"
1194
/*DOC*/ "the pixel data of an image. Sometimes this can be limited\n"
1195
/*DOC*/ "to the pixel format of the Surface, so you can also create\n"
1196
/*DOC*/ "independent copies from any format.\n"
1198
/*DOC*/ "The image arrays are indexes 'X' axis first. This is different\n"
1199
/*DOC*/ "than traditional C memory access, where images are often indexed\n"
1200
/*DOC*/ "with the 'Y' axis first. All this means is to access pixel values\n"
1201
/*DOC*/ "in the array, you index them as 'X, Y' pairs. myarray[10,20] will\n"
1202
/*DOC*/ "provide you the pixel at 10, 20 in the image. If you prefer to\n"
1203
/*DOC*/ "work with the traditional framebuffer indices, use the arrays\n"
1204
/*DOC*/ "'transpose()' method to create the alternate view of the pixel\n"