Commits

Anonymous committed 3fafaab

image.frombuffer

  • Participants
  • Parent commits 43748ac

Comments (0)

Files changed (11)

 # BUG	= fixed a bug that was (or could have been) crashing
 
 July 17, 2004
+    image.frombuffer() for lightning fast fromstring()'s
     SysFont extra styles being applied incorrect
     psuedo "configure" and "Makefile" for foolproof installs
     draw.aaline now works with pixel alphas

File docs/index.html

 <a href=ref/pygame_constants.html#events >constants.events </a> - These constants define the various event types<br>
 <a href=ref/pygame_constants.html#keyboard >constants.keyboard </a> - These constants represent the keys on the keyboard.<br>
 <a href=ref/pygame_constants.html#modifiers >constants.modifiers </a> - These constants represent the modifier keys on the keyboard.<br>
-<a href=ref/pygame_constants.html#zdepracated >constants.zdepracated </a> - The following constants are made available, but generally not needed<br>
 <a href=ref/pygame_cursor.html#compile>cursor.compile</a> - compile cursor strings into cursor data<br>
 <a href=ref/pygame_cursors.html#load_xbm>cursors.load_xbm</a> - reads a pair of XBM files into set_cursor arguments<br>
 <a href=ref/pygame_display.html#Info>display.Info</a> - get display capabilities and settings<br>
 <a href=ref/pygame_font.html#init>font.init</a> - initialize the display module<br>
 <a href=ref/pygame_font.html#match_font>font.match_font</a> -        find the filename for the named system font<br>
 <a href=ref/pygame_font.html#quit>font.quit</a> - uninitialize the font module<br>
+<a href=ref/pygame_image.html#frombuffer>image.frombuffer</a> - create a surface from a python memory buffer<br>
 <a href=ref/pygame_image.html#fromstring>image.fromstring</a> - create a surface from a raw string buffer<br>
 <a href=ref/pygame_image.html#get_extended>image.get_extended</a> - returns true if SDL_image formats are available<br>
 <a href=ref/pygame_image.html#load>image.load</a> - load an image to a new Surface<br>

File docs/ref/pygame_constants.html

 These constants represent the modifier keys on the keyboard.</td></tr>
 
 
-<tr><td><a href=#zdepracated >zdepracated </a></td><td> -
-The following constants are made available, but generally not needed</td></tr>
-
-
 </table>
 
 <hr>
 KMOD_ALT, KMOD_LMETA, KMOD_RMETA, KMOD_META, KMOD_NUM, KMOD_MODE<br>
 </ul><br>&nbsp;<br>
 
-<a name=zdepracated ><font size=+2><b>zdepracated 
-</b></font><br><font size=+1><tt>
-pygame.constants.zdepracated (constants)
-</tt></font><ul>
-The flags labeled as readonly should never be used,
-except when comparing checking flags against <a href=Surface.html#get_flags>Surface.get_flags()</a>.
-<br>&nbsp;<br>
-SWSURFACE - not really usable as a surface flag, equates to 0 and
-is always default<br>
-ANYFORMAT - creates a display with in best possible bit depth<br>
-HWACCEL - surface is hardware accelerated, readonly<br>
-SRCCOLORKEY- surface has a colorkey for blits, readonly<br>
-SRCALPHA - surface has alpha enabled, readonly<br>
-RLEACCELOK - surface is rle accelrated but uncompiled, readonly
-</ul><br>&nbsp;<br>
-
 
 <hr>
 </body></html>

File docs/ref/pygame_font.html

 
 <a name=SysFont><font size=+2><b>SysFont
 </b></font><br><font size=+1><tt>
-pygame.font.SysFont(name, size, bold=0, italic=0) -> Font
+pygame.font.SysFont(name, size, bold=False, italic=False) -> Font
 </tt></font><ul>
        This will search the system fonts for the given font
        name. You can also enable bold or italic styles, and

File docs/ref/pygame_image.html

 <hr>
 
 <table>
+<tr><td><a href=#frombuffer>frombuffer</a></td><td> -
+create a surface from a python memory buffer</td></tr>
+
+
 <tr><td><a href=#fromstring>fromstring</a></td><td> -
 create a surface from a raw string buffer</td></tr>
 
 
 <hr>
 
+<a name=frombuffer><font size=+2><b>frombuffer
+</b></font><br><font size=+1><tt>
+pygame.image.frombuffer(string, size, format) -> Surface
+</tt></font><ul>
+This works like the <a href=#fromstring>fromstring()</a> method, but uses Python
+buffer objects. It is about 20 times faster than <a href=#fromstring>fromstring()</a>.
+Strings and memory maps are examples of buffers in Python.
+<br>&nbsp;<br>
+See the <a href=#fromstring>fromstring()</a> function for information about the size
+and format arguments.
+</ul><br>&nbsp;<br>
+
 <a name=fromstring><font size=+2><b>fromstring
 </b></font><br><font size=+1><tt>
 pygame.image.fromstring(string, size, format, flipped=0) -> Surface
 a string. This can be used to transfer images from other
 libraries like PIL's <u>fromstring()</u>. 
 <br>&nbsp;<br>
+In most cases you can use the <a href=#frombuffer>frombuffer()</a> which accepts strings
+and is about 20 times faster.
+<br>&nbsp;<br>
 The flipped argument should be set to true if the image in
 the string is.
 <br>&nbsp;<br>

File lib/version.py

 releases. (hmm, until we get to versions > 10)
 """
 
-ver =   '1.6.1'
-vernum = 1,6,1
+ver =   '1.6.2'
+vernum = 1,6,2
 <font size=+5><b>Pygame Readme</b></font>
 </td></tr><tr><td align=left width=33%><big>
 
-Version 1.6.1<br>Devel Release
+Version 1.6.2<br>Devel Release
 
 
 </td><td align=center width=34%><big>
 
 METADATA = {
     "name":             "pygame",
-    "version":          "1.6.1",
+    "version":          "1.6.2",
     "license":          "LGPL",
     "url":              "http://www.pygame.org",
     "author":           "Pete Shinners",
  */
 #include "pygame.h"
 
-
 static int is_extended = 0;
 static int SaveTGA(SDL_Surface *surface, char *file, int rle);
 static int SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle);
     /*DOC*/    "a string. This can be used to transfer images from other\n"
     /*DOC*/    "libraries like PIL's fromstring(). \n"
     /*DOC*/    "\n"
+    /*DOC*/    "In most cases you can use the frombuffer() which accepts strings\n"
+    /*DOC*/    "and is about 20 times faster.\n"
+    /*DOC*/    "\n"
     /*DOC*/    "The flipped argument should be set to true if the image in\n"
     /*DOC*/    "the string is.\n"
     /*DOC*/    "\n"
 	{
 		if(len != w*h)
 			return RAISE(PyExc_ValueError, "String length does not equal format and resolution size");
-		surf = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
-		if(!surf)
-			return RAISE(PyExc_SDLError, SDL_GetError());
-		SDL_LockSurface(surf);
-		for(looph=0; looph<h; ++looph)
-			memcpy(((char*)surf->pixels)+looph*surf->pitch, DATAROW(data, looph, w, h, flipped), w);
-		SDL_UnlockSurface(surf);
+
+                surf = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
+                if(!surf)
+                        return RAISE(PyExc_SDLError, SDL_GetError());
+                SDL_LockSurface(surf);
+                for(looph=0; looph<h; ++looph)
+                        memcpy(((char*)surf->pixels)+looph*surf->pitch, DATAROW(data, looph, w, h, flipped), w);
+                SDL_UnlockSurface(surf);
 	}
 	else if(!strcmp(format, "RGB"))
 	{
 
 
 
+    /*DOC*/ static char doc_frombuffer[] =
+    /*DOC*/    "pygame.image.frombuffer(string, size, format) -> Surface\n"
+    /*DOC*/    "create a surface from a python memory buffer\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "This works like the fromstring() method, but uses Python\n"
+    /*DOC*/    "buffer objects. It is about 20 times faster than fromstring().\n"
+    /*DOC*/    "Strings and memory maps are examples of buffers in Python.\n"
+    /*DOC*/    "\n"
+    /*DOC*/    "See the fromstring() function for information about the size\n"
+    /*DOC*/    "and format arguments.\n"
+    /*DOC*/ ;
+
+PyObject* image_frombuffer(PyObject* self, PyObject* arg)
+{
+	PyObject *buffer;
+	char *format, *data;
+	SDL_Surface *surf = NULL;
+	int w, h, len;
+        PyObject *surfobj;
+
+	if(!PyArg_ParseTuple(arg, "O(ii)s|i", &buffer, &w, &h, &format))
+		return NULL;
+
+	if(w < 1 || h < 1)
+		return RAISE(PyExc_ValueError, "Resolution must be positive values");
+
+        /* breaking constness here, we should really not change this string */
+        if(PyObject_AsCharBuffer(buffer, (const char**)&data, &len) == -1)
+        	return NULL;
+        
+	if(!strcmp(format, "P"))
+	{
+		if(len != w*h)
+			return RAISE(PyExc_ValueError, "Buffer length does not equal format and resolution size");
+
+                surf = SDL_CreateRGBSurfaceFrom(data, w, h, 8, 3, 0, 0, 0, 0);
+	}
+	else if(!strcmp(format, "RGB"))
+	{
+		if(len != w*h*3)
+			return RAISE(PyExc_ValueError, "Buffer length does not equal format and resolution size");
+		surf = SDL_CreateRGBSurfaceFrom(data, w, h, 24, w*3, 0xFF, 0xFF<<8, 0xFF<<16, 0);
+	}
+	else if(!strcmp(format, "RGBA") || !strcmp(format, "RGBX"))
+	{
+		int alphamult = !strcmp(format, "RGBA");
+                if(len != w*h*4)
+			return RAISE(PyExc_ValueError, "Buffer length does not equal format and resolution size");
+		surf = SDL_CreateRGBSurfaceFrom(data, w, h, 32, w*4,
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+                                        0xFF, 0xFF<<8, 0xFF<<16, (alphamult?0xFF<<24:0));
+#else
+                                        0xFF<<24, 0xFF<<16, 0xFF<<8, (alphamult?0xFF:0));
+#endif
+                if(alphamult)
+                    surf->flags |= SDL_SRCALPHA;
+	}
+	else if(!strcmp(format, "ARGB"))
+	{
+                if(len != w*h*4)
+			return RAISE(PyExc_ValueError, "Buffer length does not equal format and resolution size");
+		surf = SDL_CreateRGBSurfaceFrom(data, w, h, 32, w*4,
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+                                        0xFF<<24, 0xFF, 0xFF<<8, 0xFF<<16);
+#else
+                                        0xFF, 0xFF<<24, 0xFF<<16, 0xFF<<8);
+#endif
+                surf->flags |= SDL_SRCALPHA;
+	}
+	else
+		return RAISE(PyExc_ValueError, "Unrecognized type of format");
+
+	if(!surf)
+		return RAISE(PyExc_SDLError, SDL_GetError());
+	surfobj = PySurface_New(surf);
+        Py_INCREF(buffer);
+        ((PySurfaceObject*)surfobj)->dependency = buffer;
+        return surfobj;
+}
+
+
+
 /*******************************************************/
 /* tga code by Mattias Engdeg�rd, in the public domain */
 /*******************************************************/
 
 
 
-
-
-
-
-
 static PyMethodDef image_builtins[] =
 {
 	{ "load_basic", image_load_basic, 1, doc_load },
 
 	{ "tostring", image_tostring, 1, doc_tostring },
 	{ "fromstring", image_fromstring, 1, doc_fromstring },
+	{ "frombuffer", image_frombuffer, 1, doc_frombuffer },
 
 	{ NULL, NULL }
 };

File src/pygame.h

 	SDL_Surface* surf;
 	struct SubSurface_Data* subsurface;  /*ptr to subsurface data (if a subsurface)*/
         PyObject* weakreflist;
+        PyObject *dependency;
 } PySurfaceObject;
 #define PySurface_AsSurface(x) (((PySurfaceObject*)x)->surf)
 #ifndef PYGAMEAPI_SURFACE_INTERNAL

File src/surface.c

 		PyMem_Del(self->subsurface);
                 self->subsurface = NULL;
 	}
+        if(self->dependency) {
+            Py_DECREF(self->dependency);
+            self->dependency = NULL;
+        }
 }