[Gridflow-cvs] [svn] commit: r5785 - /trunk/src/gem.cxx

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Tue Jul 27 15:33:01 EDT 2010


Author: matju
Date: Tue Jul 27 15:32:54 2010
New Revision: 5785

Log:
second attempt at imageStruct 93

Modified:
    trunk/src/gem.cxx

Modified: trunk/src/gem.cxx
==============================================================================
--- trunk/src/gem.cxx (original)
+++ trunk/src/gem.cxx Tue Jul 27 15:32:54 2010
@@ -23,14 +23,17 @@
 #include <GL/gl.h>
 
 /* summarising GEM's headers: GemState.h and GemPixUtil.h */
-struct imageStruct {
+struct imageStruct92 { // and early 93
   GLint xsize, ysize, csize; GLenum type, format; int notowned;
   unsigned char *data; unsigned char *pdata; size_t datasize; GLboolean upsidedown;
-  void clear(); imageStruct(); /*virtual*/ ~imageStruct(); // late 93 has virtual
-  unsigned char *allocate(size_t size); unsigned char *allocate(); // 92 and early 93
-  // virtual unsigned char *allocate(size_t size); virtual unsigned char *allocate(); late 93
-  //void convertTo  (imageStruct*to,   GLenum dest_format=0);
-  //void convertFrom(imageStruct*from, GLenum dest_format=0);
+  void clear(); imageStruct92(); ~imageStruct92();
+  unsigned char *allocate(size_t size); unsigned char *allocate();
+};
+struct imageStruct93 { // except early 93
+  GLint xsize, ysize, csize; GLenum type, format; int notowned;
+  unsigned char *data; unsigned char *pdata; size_t datasize; GLboolean upsidedown;
+  void clear(); imageStruct93(); virtual ~imageStruct93();
+  virtual unsigned char *allocate(size_t size); virtual unsigned char *allocate();
 };
 #ifdef __WIN32__
 #define GEM_VECTORALIGNMENT 128
@@ -51,7 +54,8 @@
   return data; 
 }
 #endif
-struct pixBlock {pixBlock(); imageStruct image; int newimage, newfilm;};
+struct pixBlock92 {imageStruct92 image; int newimage, newfilm; pixBlock92(){newimage=newfilm=0;}};
+struct pixBlock93 {imageStruct93 image; int newimage, newfilm; pixBlock93(){newimage=newfilm=0;}};
 #ifdef __WIN32__
 pixBlock::pixBlock() : newimage(0), newfilm(0) {}
 #endif
@@ -68,13 +72,13 @@
 static int gem=0;
 static int imageStruct_has_virtual = 0;
 struct GemState92 {
-  int dirty, inDisplayList, lighting, smooth, texture; pixBlock *image;
+  int dirty, inDisplayList, lighting, smooth, texture; pixBlock92 *image;
   GemState92(); ~GemState92(); void reset();
 };
 /* you need at least one virtual dummy function in order to enable the implicit inclusion of the vtable pointer,
  * that is, C++'s class pointer. */
 struct GemState93 {
-  bool dirty, inDisplayList, lighting, smooth; int texture; pixBlock *image;
+  bool dirty, inDisplayList, lighting, smooth; int texture; pixBlock93 *image;
   GemState93(); ~GemState93(); void reset(); virtual void your_mom() = 0;
 };
 struct GemVersion {static const char *versionString();};
@@ -90,23 +94,33 @@
 //  in 1: grid
 // out 0: gem
 \class GridToPix : FObject {
-	pixBlock m_pixBlock;
+	union {
+	  pixBlock92 *pb92;
+	  pixBlock93 *pb93;
+	};
 	\attr bool yflip;
 	\decl 0 gem_state (...);
 	void render(void *state) {
-		if (gem>=93) ((GemState93 *)state)->image = &m_pixBlock;
-		else         ((GemState92 *)state)->image = &m_pixBlock;
-	}
-	void startRendering () {m_pixBlock.newimage = 1;}
+		if (gem>=93) ((GemState93 *)state)->image = pb93;
+		else         ((GemState92 *)state)->image = pb92;
+	}
+	void startRendering () {
+		if (imageStruct_has_virtual) pb93->newimage = 1;
+		else                         pb92->newimage = 1;
+	}
 	\constructor () {
 		yflip = false;
-		m_pixBlock.image = imageStruct();
-		long *hax0r = (long*)&m_pixBlock.image;
-		//post("%lx %lx %lx %lx",hax0r[0],hax0r[1],hax0r[2],hax0r[3]);
-		imageStruct &im = *(imageStruct *)(((long *)&m_pixBlock.image)+imageStruct_has_virtual);
-		im.ysize = 1; im.xsize = 1; im.csize = 4;
-		im.format = GEM_RGBA; im.type = GL_UNSIGNED_BYTE;
-		m_pixBlock.image.allocate(); *(int*)im.data = 0x000000ff;
+		if (imageStruct_has_virtual) {
+			pb93 = new pixBlock93();
+			imageStruct93 &im = pb93->image = imageStruct93();
+			im.ysize = 1; im.xsize = 1; im.csize = 4; im.format = GEM_RGBA; im.type = GL_UNSIGNED_BYTE;
+			im.allocate(); *(int*)im.data = 0x000000ff;
+		} else {
+			pb92 = new pixBlock92();
+			imageStruct92 &im = pb92->image = imageStruct92();
+			im.ysize = 1; im.xsize = 1; im.csize = 4; im.format = GEM_RGBA; im.type = GL_UNSIGNED_BYTE;
+			im.allocate(); *(int*)im.data = 0x000000ff;
+		}
 	}
 	~GridToPix () {}
 	\grin 1 int
@@ -128,28 +142,38 @@
 	int c = in.dim[2];
 	if (c!=3 && c!=4)  RAISE("expecting 3 or 4 channels (got %d)",in.dim[2]);
 	in.set_chunk(1);
-	imageStruct &im = m_pixBlock.image;
-	im.clear();
-	im.ysize = in.dim[0];
-	im.xsize = in.dim[1];
-	im.type = GL_UNSIGNED_BYTE;
-	switch (in.dim[2]) {
-	    case 1: im.csize = 1; im.format = GL_LUMINANCE; break;
-	    case 3: im.csize = 4; im.format = GEM_RGBA;     break;
-	    case 4: im.csize = 4; im.format = GEM_RGBA;     break;
-	    default: RAISE("you shouldn't see this error message.");
-	}
-	im.allocate();
+	if (imageStruct_has_virtual) {
+		imageStruct93 &im = pb93->image;
+		im.clear(); im.ysize = in.dim[0]; im.xsize = in.dim[1]; im.type = GL_UNSIGNED_BYTE;
+		switch (in.dim[2]) {
+		case 1: im.csize = 1; im.format = GL_LUMINANCE; break;
+		case 3: im.csize = 4; im.format = GEM_RGBA;     break;
+		case 4: im.csize = 4; im.format = GEM_RGBA;     break;
+		default: RAISE("you shouldn't see this error message.");
+		}
+		im.allocate();
+	} else {
+		imageStruct92 &im = pb92->image;
+		im.clear(); im.ysize = in.dim[0]; im.xsize = in.dim[1]; im.type = GL_UNSIGNED_BYTE;
+		switch (in.dim[2]) {
+		case 1: im.csize = 1; im.format = GL_LUMINANCE; break;
+		case 3: im.csize = 4; im.format = GEM_RGBA;     break;
+		case 4: im.csize = 4; im.format = GEM_RGBA;     break;
+		default: RAISE("you shouldn't see this error message.");
+		}
+		im.allocate();
+	}
 } GRID_FLOW {
-	uint8 *buf = (uint8 *)m_pixBlock.image.data;
+	uint8 *buf = imageStruct_has_virtual ? (uint8 *)pb93->image.data : (uint8 *)pb92->image.data;
+	int csize  = imageStruct_has_virtual ?         pb93->image.csize  :         pb93->image.csize;
+	if (imageStruct_has_virtual) pb93->image.upsidedown = !yflip;
+	else                         pb93->image.upsidedown = !yflip;
 	long sxc = in.dim.prod(1);
 	long sx = in.dim[1];
 	long chans = in.dim[2];
-	imageStruct &im = m_pixBlock.image;
-	im.upsidedown = !yflip;
 	for (long y=in.dex/sxc; n; data+=sxc, n-=sxc, y++) {
 		if (chans==3) {
-			uint8 *buf2 = buf+y*sx*im.csize;
+			uint8 *buf2 = buf+y*sx*csize;
 			T    *data2 = data;
 			#ifdef MACOSX
 			#define FOO buf2[0]=data2[2]; buf2[1]=data2[1]; buf2[2]=data2[0]; buf2[3]=255; data2+=3; buf2+=4;
@@ -161,7 +185,7 @@
 			for (   ; x< sx    ; x++ ) {FOO}
 			#undef FOO
 		} else if (chans==4) {
-			uint8 *buf2 = buf+y*sx*im.csize;
+			uint8 *buf2 = buf+y*sx*csize;
 			T    *data2 = data;
 			#ifdef MACOSX
 			#define FOO buf2[0]=data2[2]; buf2[1]=data2[1]; buf2[2]=data2[0]; buf2[3]=data2[3]; data2+=4; buf2+=4;
@@ -178,7 +202,8 @@
 		}
 	}
 } GRID_FINISH {
-	m_pixBlock.newimage = 1;
+	if (imageStruct_has_virtual) pb93->newimage = 1;
+	else                         pb92->newimage = 1;
 } GRID_END
 \end class {install("#to_pix",2,1); add_creator("#export_pix");}
 
@@ -198,12 +223,7 @@
 	virtual ~GridFromPix () {}
 	\decl 0 gem_state (...);
 	\decl 0 colorspace (t_symbol *s);
-	void render(void *state) {
-		pixBlock *pb = gem>=93 ?
-			((GemState93 *)state)->image:
-			((GemState92 *)state)->image;
-		if (!pb) {::post("gemstate has no pix"); return;}
-		imageStruct &im = pb->image;
+	template <class imageStructT> void render_really(imageStructT &im) {
 		//im.convertTo(im,GEM_RGBA);
 		BitPacking *bp;
 		switch (im.format) {
@@ -222,7 +242,7 @@
 		  #endif
 		  default: ::post("can't produce grid from pix type %d (0x%x)",  im.type, im.type  ); return;}
 		// on OSX, one was GL_UNSIGNED_INT_8_8_8_8 and the other was...?
-		int32 v[] = { im.ysize, im.xsize, channels };
+		int32 v[] = {im.ysize, im.xsize, channels};
 		GridOutlet out(this,0,Dim(3,v),cast);
 		long sxc = im.xsize*channels;
 		long sy = v[0];
@@ -237,6 +257,14 @@
 			TYPESWITCH(cast,FOO,)
 			#undef FOO
 		//}
+	}
+	void render (void *state) {
+		void *pb = gem>=93 ?
+			(void *)(((GemState93 *)state)->image):
+			(void *)(((GemState92 *)state)->image);
+		if (!pb) {::post("gemstate has no pix"); return;}
+		if (imageStruct_has_virtual) render_really(((pixBlock93 *)pb)->image);
+		else                         render_really(((pixBlock92 *)pb)->image);
 	}
 };
 \def 0 colorspace (t_symbol *s) {// 3 2 1 0 (numéro de byte)
@@ -272,8 +300,8 @@
 //------------------------------------------------------------------------
 // [gemdead]
 
-struct GemState      {GemState(); char trabant[666];};
-//struct imageStruct {imageStruct(); char lada[666];};
+struct GemState    {GemState(); char trabant[666];};
+struct imageStruct {imageStruct(); char lada[666];};
 #ifdef __WIN32__
 GemState::GemState() {}
 #endif



More information about the Gridflow-cvs mailing list