~ubuntu-branches/ubuntu/feisty/pygame/feisty

« back to all changes in this revision

Viewing changes to src/surfarray.c

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2004-08-08 00:10:10 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040808001010-nts9g4z69jf1ryb1
Tags: 1.6-2
* Actually build-depend on python (for dh_python)
  * Closes: #264086

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
        int pixelstep;
55
55
        const int lilendian = (SDL_BYTEORDER == SDL_LIL_ENDIAN);
56
56
        PyObject* lifelock;
57
 
        
 
57
 
58
58
        if(!PyArg_ParseTuple(arg, "O!", &PySurface_Type, &surfobj))
59
59
                return NULL;
60
60
        surf = PySurface_AsSurface(surfobj);
66
66
        if(!lifelock) return NULL;
67
67
 
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)
72
72
        {
73
73
                pixelstep = (lilendian ? -1 : 1);
74
74
                startpixel = ((char*)surf->pixels) + (lilendian ? 2 : 0);
75
75
        }
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)
79
79
        {
84
84
                return RAISE(PyExc_ValueError, "unsupport colormasks for 3D reference array");
85
85
        if(!lilendian && surf->format->BytesPerPixel == 4)
86
86
            ++startpixel;
87
 
    
 
87
 
88
88
        /*create the referenced array*/
89
89
        dim[0] = surf->w;
90
90
        dim[1] = surf->h;
355
355
        stridey = ((PyArrayObject*)array)->strides[1];
356
356
 
357
357
        if(!PySurface_Lock(surfobj)) return NULL;
358
 
        
 
358
 
359
359
        switch(surf->format->BytesPerPixel)
360
360
        {
361
361
        case 1:
781
781
                                                                format->Bloss << format->Bshift);
782
782
                        }
783
783
                }break;
784
 
        default: 
 
784
        default:
785
785
                Py_DECREF(newarray);
786
786
                return RAISE(PyExc_ValueError, "unsupported bytesperpixel for array\n");
787
787
        }
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;
1023
 
                                default: 
 
1023
                                default:
1024
1024
                                        if(!PySurface_Unlock(surfobj)) return NULL;
1025
1025
                                        return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1026
1026
                        }
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;
1035
 
                                default: 
 
1035
                                default:
1036
1036
                                        if(!PySurface_Unlock(surfobj)) return NULL;
1037
1037
                                        return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1038
1038
                        }
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;
1044
 
                                default: 
 
1044
                                default:
1045
1045
                                        if(!PySurface_Unlock(surfobj)) return NULL;
1046
1046
                                        return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1047
1047
                        }
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;
1056
 
                                default: 
 
1056
                                default:
1057
1057
                                        if(!PySurface_Unlock(surfobj)) return NULL;
1058
1058
                                        return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1059
1059
                        }
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;
1065
 
                                default: 
 
1065
                                default:
1066
1066
                                        if(!PySurface_Unlock(surfobj)) return NULL;
1067
1067
                                        return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1068
1068
                        }
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;
1077
 
                        default: 
 
1077
                        default:
1078
1078
                                        if(!PySurface_Unlock(surfobj)) return NULL;
1079
1079
                                        return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1080
1080
                        }
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;
1086
 
                                default: 
 
1086
                                default:
1087
1087
                                        if(!PySurface_Unlock(surfobj)) return NULL;
1088
1088
                                        return RAISE(PyExc_ValueError, "unsupported datatype for array\n");
1089
1089
                        }
1099
1099
}
1100
1100
 
1101
1101
 
 
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"
 
1105
    /*DOC*/    "\n"
 
1106
    /*DOC*/    "Create a new software surface that closely resembles\n"
 
1107
    /*DOC*/    "the data and format of the image array data.\n"
 
1108
    /*DOC*/ ;
 
1109
 
 
1110
PyObject* make_surface(PyObject* self, PyObject* arg)
 
1111
{
 
1112
        PyObject *arrayobj, *surfobj, *args;
 
1113
        SDL_Surface* surf;
 
1114
        PyArrayObject* array;
 
1115
        int sizex, sizey, bitsperpixel;
 
1116
        Uint32 rmask, gmask, bmask;
 
1117
 
 
1118
        if(!PyArg_ParseTuple(arg, "O!", &PyArray_Type, &arrayobj))
 
1119
                return NULL;
 
1120
        array = (PyArrayObject*)arrayobj;
 
1121
 
 
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");
 
1126
 
 
1127
        if(array->nd == 2)
 
1128
        {
 
1129
            bitsperpixel = 8;
 
1130
            rmask = gmask = bmask = 0;
 
1131
        }
 
1132
        else
 
1133
        {
 
1134
            bitsperpixel = 32;
 
1135
            rmask = 0xFF<<16; gmask = 0xFF<<8; bmask = 0xFF;
 
1136
        }
 
1137
        sizex = array->dimensions[0];
 
1138
        sizey = array->dimensions[1];
 
1139
 
 
1140
        surf = SDL_CreateRGBSurface(0, sizex, sizey, bitsperpixel, rmask, gmask, bmask, 0);
 
1141
        if(!surf)
 
1142
                return RAISE(PyExc_SDLError, SDL_GetError());
 
1143
        surfobj = PySurface_New(surf);
 
1144
        if(!surfobj)
 
1145
        {
 
1146
            SDL_FreeSurface(surf);
 
1147
            return NULL;
 
1148
        }
 
1149
 
 
1150
        args = Py_BuildValue("(OO)", surfobj, array);
 
1151
        if(!args)
 
1152
        {
 
1153
            Py_DECREF(surfobj);
 
1154
            return NULL;
 
1155
        }
 
1156
        blit_array(NULL, args);
 
1157
        Py_DECREF(args);
 
1158
 
 
1159
        if(PyErr_Occurred())
 
1160
        {
 
1161
            Py_DECREF(surfobj);
 
1162
            return NULL;
 
1163
        }
 
1164
        return surfobj;
 
1165
}
 
1166
 
 
1167
 
 
1168
 
1102
1169
static PyMethodDef surfarray_builtins[] =
1103
1170
{
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 },
1115
1183
 
1116
1184
        { NULL, NULL }
1117
1185
};
1122
1190
 
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"
 
1197
    /*DOC*/    "\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"
 
1205
    /*DOC*/    "data.\n"
 
1206
    /*DOC*/    "\n"
1126
1207
    /*DOC*/ ;
1127
1208
 
1128
1209
PYGAME_EXPORT
1138
1219
        import_pygame_base();
1139
1220
        import_pygame_surface();
1140
1221
        import_array();
 
1222
    /*needed for Numeric in python2.3*/
 
1223
        PyImport_ImportModule("Numeric");
1141
1224
}
1142
1225
 
1143
1226