Thread: Obtaining real types from variants

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    10

    Obtaining real types from variants

    I'm looping through an image, reading the pixel values and manipulating them. The pixel value can only be returned as a variant, therefore I'm going to need to use variant.dblVal or .iVal etc to get the value. My program will however encounter most real types, therefore I will need to use .dblVal or .iVal depending on the image. Is there a way of doing this without a hefty switch statement inside my image loop getting in the way? i.e. obtaining the value just using a single statement?

    Any help appreciated.

    Will

    Code:
    long i, j;
        long ij[2];
        VARIANT v;
        double getvalM;
        ipInRasProps->get_NoDataValue(&vInnodatavalue);
        ipOutputRasProps->get_NoDataValue(&vOutNodatavalue);
    	
    	for (i=0; i<(lHeight - 1); i++)
    	{
    	ij[0]=i;
                      for (j=0; j<(lWidth - 1); j++)
    		 {
    		 ij[1]=j;
                     ipInputBlock3->GetVal(0, i, j, &v);
    		 getvalM = v.iVal;  //here I'd have a switch statement 
     
    		if ((j + 1) < (lWidth))
    	        SafeArrayPutElement(vSrcPixelArray.parray, ij, &v);
    			
    							
    		}
                    
    		}
    	}
        ipOutputBlock = ipOutputBlock3;
    	ipOutRawPixel->Write(pPnt, ipOutputBlock);

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    !!!

    What on earth kind of ultra-slow API are you using that forces you to painstakingly retrieve individual pixel values as VARIANTs one at a time?!?!

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>Is there a way of doing this without a hefty switch statement inside my image loop getting in the way? i.e. obtaining the value just using a single statement?

    Yes, but it would not save any time at all because you still must determine from .vt whether it is VT_REAL, VT_I4 or whatever. If you know there are only two possible data types then you can do like below. If other data types are possible then a switch statement would probably be more efficient.
    Code:
     getvalM = (vt.vt == VT_I4) ? v.iVal : v.dblVal;
    If you are looking at ways to speed up that algorithm you posted, your best efforts would be spent by replacing SafeArrayPutElement() because it locks the array, puts the data into the array, the unlocks it again. That is awful time consuming. You can lock it at the beginning of the loop, add each element in your code then unlock it at the end.

  4. #4
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    very slow algorithm ... if you could even call it that.
    But anyway you could try using unions. Google it.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by ElastoManiac
    very slow algorithm ... if you could even call it that.
    But anyway you could try using unions. Google it.
    A VATIANT is a union.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    10
    Quote Originally Posted by Ancient Dragon
    your best efforts would be spent by replacing SafeArrayPutElement() because it locks the array, puts the data into the array, the unlocks it again. That is awful time consuming. You can lock it at the beginning of the loop, add each element in your code then unlock it at the end.
    Thanks, I'll look into that. Any further tips on safearray functions?

    I'll know the variant type before the loop if that makes any difference.


    Quote Originally Posted by iMalc
    What on earth kind of ultra-slow API are you using that forces you to painstakingly retrieve individual pixel values as VARIANTs one at a time?!?!
    ESRI's ArcGIS. You should see the amount of code required to unwrap the raw pixels.

    Thanks for the help.

    Will

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    10
    I Can't find a function to input data to the safearray once it is locked, other than safearrayputelement - any ideas?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  3. non-paying jobs for programmers, anyone here do this to help you get a real job?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-14-2002, 05:29 AM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM
  5. Replies: 7
    Last Post: 12-12-2001, 10:28 AM