As already said by Mike, nice patch (and also nice test script). I think I was the one who added the previous comment (and it's true that I only tested it without the COMPLEX bit set).
The only (minor) gripe I would have with this patch is the use of the 'log() / log()' to compute the mipmap count (I usually dislike floating point match - it's maybe my background in embedded programming where FPU are inexistant :-) )... I would have found it simpler to just have a 'while (min > 0) { min
= 1; count += 1 }'.
Lionel