Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » Some new functions
Some new functions [message #29820] Sat, 20 November 2010 01:01 Go to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello

Here there are a few functions that could be added to U++:

inline bool Odd(int val)	  	{return val%2;}
inline bool Even(int val) 	  	{return !Odd(val);}
inline int RoundEven(int val) 		{return Even(val) ? val : val+1;}
template<class T>
inline int Sign(T a) 			{return (a > 0) - (a < 0);}

inline const RGBA *GetPixel(const Image &img, int x, int y) {
	return img + x + y*img.GetWidth();
}
inline RGBA *GetPixel(ImageBuffer &img, int x, int y) {
	return img + x + y*img.GetWidth();
}


Best regards
Iñaki
Re: Some new functions [message #29822 is a reply to message #29820] Sat, 20 November 2010 17:22 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hi Koldo,

I think the Odd() function would be faster this way (although the compiler might already optimize it this way):

inline bool Odd(int val) {return (val & 0x1);}

Not a big optimization, rather a very tiny one. But with drops you can fill the sea Very Happy
Re: Some new functions [message #29824 is a reply to message #29820] Sat, 20 November 2010 18:07 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Instead of GetPixel, you can write

image[y][x]

Mirek
Re: Some new functions [message #29827 is a reply to message #29822] Sat, 20 November 2010 20:55 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Fun fact: I tried to come up with optimal solution for Even() and all of the following appear to have same speed as the one from Koldo (with gcc optimal+speed flag):
inline bool even1(int val)		{return !(val&1);}
inline bool even2(int val)		{return ~val&1;}
inline bool even3(int val)		{return !(val%2);}

Without the speed flag even2() seems to be slightly faster.

Also val%2 and val&1 for Odd() yields the same speed in both cases Smile

However, the proposed RoundEven() function is suboptimal thanks to the branching. Even though it won't probably be used often, I would suggest faster version:
inline int roundeven(int val)	{return ((1+val)>>1)<<1;}
//for completeness also rounding to odd numbers:
inline int roundodd(int val)	{return ((val>>1)<<1)+1;}


Regarding the image access: The img[y][x] is great, but still it would be nice to have a wrapper that would allow to put the arguments in (imho) more natural order. For example something like
RGBA* Image::Get(int x,int y){return (*this)[y][x];}


Best regards,
Honza
Re: Some new functions [message #29829 is a reply to message #29827] Sat, 20 November 2010 23:22 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Good comments from all.

I like Image::Get() in addition to image[y][x].

About Odd()... you are much smarter than me Smile.


Best regards
Iñaki
Re: Some new functions [message #29831 is a reply to message #29829] Sun, 21 November 2010 00:05 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Just some more Smile...

byte BW(Color color) {
	return byte(0.299*color.GetR() + 0.587*color.GetG() + 0.114*color.GetB());
}

Image Rotate180(const Image& orig) {
	Size sz = orig.GetSize();
	ImageBuffer dest(sz);
	for(int rw = 0; rw < sz.cy; rw++)
		for(int cl = 0; cl < sz.cx; cl++)
			dest[rw][cl] = orig[sz.cy - rw - 1][sz.cx - cl - 1];
	return dest;
}

Image GetRect(const Image& orig, const Rect &r) {
	if(r.IsEmpty())
		return Image();
	ImageBuffer ib(r.GetSize());
	for(int y = r.top; y < r.bottom; y++) {
		const RGBA *s = orig[y] + r.left;
		const RGBA *e = orig[y] + r.right;
		RGBA *t = ib[y - r.top];
		while(s < e) {
			*t = *s;
			t++;
			s++;
		}
	}
	return ib;
}

Color RandomColor() {
	int num = Random();
	return Color(num&0xFF, (num&0xFF00)>>8, (num&0xFF0000)>>16);
}


Best regards
Iñaki
Re: Some new functions [message #29833 is a reply to message #29831] Sun, 21 November 2010 00:40 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

koldo wrote on Sun, 21 November 2010 00:05

Just some more Smile...

...

Color RandomColor() {
	int num = Random();
	return Color(num&0xFF, (num&0xFF00)>>8, (num&0xFF0000)>>16);
}


Just some more comments Wink
Color RandomColor() {Color(Random(),0);}

BTW: Color BW() is useful sometimes, but it might deserve bit more readable name. What about ToGrayscale() ?

Honza

EDIT: Now I see conversion to grayscale is already available in Core:
int  Grayscale(const Color& c)
{
	return (77 * c.GetR() + 151 * c.GetG() + 28 * c.GetB()) >> 8;
}

[Updated on: Sun, 21 November 2010 00:45]

Report message to a moderator

Re: Some new functions [message #29836 is a reply to message #29833] Sun, 21 November 2010 08:26 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Thank you Honza. Grayscale implementation is interesting, I like it.

Best regards
Iñaki
Re: Some new functions [message #29847 is a reply to message #29820] Mon, 22 November 2010 09:09 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
inline int roundeven(int val)	{return ((1+val)&(~1);}
//for completeness also rounding to odd numbers:
inline int roundodd(int val)	{return val|1;}


golfing, aren't we? Smile
Re: Some new functions [message #29870 is a reply to message #29827] Thu, 25 November 2010 16:43 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
dolik.rce wrote on Sat, 20 November 2010 14:55

Fun fact: I tried to come up with optimal solution for Even() and all of the following appear to have same speed as the one from Koldo (with gcc optimal+speed flag):
inline bool even1(int val)		{return !(val&1);}
inline bool even2(int val)		{return ~val&1;}
inline bool even3(int val)		{return !(val%2);}

Without the speed flag even2() seems to be slightly faster.

Also val%2 and val&1 for Odd() yields the same speed in both cases Smile

However, the proposed RoundEven() function is suboptimal thanks to the branching. Even though it won't probably be used often, I would suggest faster version:
inline int roundeven(int val)	{return ((1+val)>>1)<<1;}
//for completeness also rounding to odd numbers:
inline int roundodd(int val)	{return ((val>>1)<<1)+1;}



Best regards,
Honza


Hi Honza,

I posted a link to a collection of optimized functions here http://www.ultimatepp.org/forum/index.php?t=msg&th=5683& amp;start=0&

It looks like it might be helpful for your experiments.


Regards,
Novo
Re: Some new functions [message #30037 is a reply to message #29820] Sat, 04 December 2010 20:38 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
koldo wrote on Fri, 19 November 2010 19:01

Hello

Here there are a few functions that could be added to U++:

[code]inline bool Odd(int val) {return val%2;}
inline bool Even(int val) {return !Odd(val);}



Thinking about it, for me it is much easier to remember that a byte has a least significant bit than to remember what is even and what is odd... Smile

[Updated on: Sat, 04 December 2010 20:38]

Report message to a moderator

Re: Some new functions [message #30043 is a reply to message #30037] Sat, 04 December 2010 23:44 Go to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
mirek wrote on Sat, 04 December 2010 20:38

koldo wrote on Fri, 19 November 2010 19:01

Hello

Here there are a few functions that could be added to U++:

[code]inline bool Odd(int val) {return val%2;}
inline bool Even(int val) {return !Odd(val);}



Thinking about it, for me it is much easier to remember that a byte has a least significant bit than to remember what is even and what is odd... Smile


For me it is the opposite. When I have to work with bits I need to open the manual to know if it is >> or << ... Smile. I have poor memory. However Odd and Even is just that.

Anyway, there will be always Functions4U Smile.


Best regards
Iñaki
Previous Topic: How to commit on svn ?
Next Topic: FileSel change
Goto Forum:
  


Current Time: Fri Mar 29 01:40:19 CET 2024

Total time taken to generate the page: 0.01004 seconds