Courtesy of the OpenCV 2.3 GPU code comes a neat snippet of code for using a template parameter for reading RGB or BGR ordered components when dealing with RGB triplets.

The Code

  1. template <int blueIndex>
  2. float rgb2grey(const float *src)
  3. {
  4.    return 0.114f*src[blueIndex^2] + 0.587f*src[1] + 0.299f*src[blueIndex];
  5. }

Then to use the function you simply supply the index where the blue value resides to take care of the RGB vs BGR ordering.

For RGB ordering:

float g = rgb2grey(src);

And for BGR ordering:

float g = rgb2grey(src);

This only works for swapping R and B around and won’t work for more weird and wonderful orderings

The original OpenCV code can be found in modules/gpu/src/opencv2/gpu/device/detail/color.hpp.

Templates and CUDA

For me the fact that you can use template meta-programming is a real plus point of CUDA. It allows for good code re-use and the template expansion gives good scope for the compiler to optimise. It can also allow you to remove conditionals from kernels in appropriate circumstances – more in a future post!

Displaying equations in webpages has always been a headache. The fallback of using images was always there, but in the age of blogs and other content creation tools editing, updating and maintaining images for equations is tedious.

MathML was an effort to standardise support in browsers, but the reality of it is that it only works out of the box in very few cases.

Whilst looking for a way to easily put equations into a WordPress blog, MathJax turned up. Have a look at some of the examples!

MathJax in WordPress

A quick search turns up a couple of WordPress plugins, of which I ended up using Latex for WordPress, which
allows me to easily put Latex syntax equations directly into posts.

So
{{{
$ $x = frac{-b pm sqrt{b^2 – 4ac}}{2a}$ $
}}}
becomes

$$x = \frac{-b \pm \sqrt{b^2 – 4ac}}{2a}$$

All in all excellent, 😉

For 2.5 yrs work has made use of a Kolab server I set up, but recently it has been taking up too much of my time to maintain. Google Apps was chosen as the replacement, so the problem then became how to get 39GB of data uploaded to Google over a fairly slow ADSL link, whilst keeping everyone up and running as much as possible.

imapsync seemed to be the tool of choice for a lot of people doing migrations of IMAP users, and it has done a very good job here too. I used version 1.311 from source rather than the older package provided with Ubuntu at the time as it contained various fixes for Google’s IMAP servers.
Continue reading

At work we are getting a 64-bit version of our software up and running at the moment. Most of the usual culprits reared their head – assuming that a pointer and integer had the same size etc etc.

One more interesting one, which I’ve not come across before is related to using STL string::find and the special constant string::npos. This is not unique to our code base when you google for it and actually just boils down to data being truncated before a comparison. The nuances of the problem do lead on to a discussion about signed vs. unsigned integral types in C++ and the handling of comparisons between differently sized data types. I though it was worth looking at a bit further and definitely something to watch out for when doing code reviews.

It could also make for a particularly challenging interview question 😉

Continue reading