PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Subpixel accurate BlitRotates

While messing around with making a single call RotateStretch function (as opposed to the original that blits to a temporary surface for scale, then rotates that), I realized that you can get subpixel accurate BlitRotates by just changing the int's to float's in the function parameters.

Of course, this will result in 4 floating point multiplies instead of 4 int muls, but it make a huge difference if you need it.

I can post an example if anyone wants.
I'd like to see an example of that.

Thanks,
-John
Does it mean that rotated+stretched picture quality will increase greatly?

I'd want to see the example anyway.
Attached is a windows EXE demonstrating both the subpixel stuff and the anti-aliasing code I'm working on.

There are obvious pros and cons... The sub-pixel alone gives the sparkly effect, and doesn't work well for images with fine detail.

Also, this only works if you are scaling or rotating the images... if it's 100% with no rotation, there is no improvement of position or quality.
Attachments:
SUBPIXEL.zip (69 KB)
thanks i'll take a look
Can u post source of this example? I'd like to learn how to do such tricks :D
The sub-pixel without AA is just a few changes to the rasterizer.cpp file:

Code:

void Rasterizer::BlitRotatedScale( float destX, float destY, float angle, float scale, const Surface* source, int AA, int AT)
{
// Calculate destination rectangle
//todo: find better fit
//je float tw = 0.5f * source->GetWidth();
//je float th = 0.5f * source->GetHeight();
//je int diag = int(sqrtf(tw*tw+th*th) + 1.0f);

//done: found a better fit
int diag = (int)( (fabs(cos(angle)*source->GetWidth()) + fabs(sin(angle)*source->GetHeight()) )*(scale/2.0f) + 1.0f);



Rect destRect( (int)(destX-diag), (int)(destY-diag), (int)(destX+diag), (int)(destY+diag) );

// Clipping
if (!m_clipper.IsEmpty())
{
destRect.Intersect( m_clipper );
if (destRect.IsEmpty())
return;
}

float iscale = 1.0f/scale;

int c = int(iscale * cosf( -angle ) * 1024.0f);
int s = int(iscale * sinf( -angle ) * 1024.0f);
int w = source->GetWidth() << 9;
int h = source->GetHeight() << 9;

// Use fixed point (22.10)
unsigned T[5][2];

T[0][0] = c;
T[0][1] = -s;
T[1][0] = s;
T[1][1] = c;
T[2][0] = unsigned int(-c * destX - s * destY + w);
T[2][1] = unsigned int( s * destX - c * destY + h);

// Optimization: compensate for fixed point arithmetic in rasterization loops
T[2][0] <<= 6;
T[2][1] <<= 6;


// Drawing
int colorMask = source->GetColorMask();

if (colorMask < 0)
m_raster->BlitTransform( *m_surface->m_buffer, destRect, T, *source->m_buffer );
else
m_raster->BlitTransformMask( *m_surface->m_buffer, destRect, T, *source->m_buffer, colorMask );

}


Note that this is sort of a replacement for BlitRotatedStretch, but works with a scale instead of a sized rect.

The if you're interested in the anti-alias code, it requires modifications in a few places. Basically, it's the same as the regular one, except that it steps smaller increments and averages the colors.

I'll post that stuff later. It needs to be cleaned up a bit.
yes, please :)
i'll wait :)
Just a quick comment. How is your "diag" a better fit than Thierrys? I thought Thierry with his "todo" comment meant that the width and height of destRect rectangle should be calculated individually (if you have a thin object, rectangular object it's in most cases not optimal to have a square target rectangle). In your case I'm not sure how your solution is better than Thierrys.
It's not right.

I had been working with square images, and forgot what I was doing. I had it written down on paper right, but just not put coded right. I shouldn't have even had a diag variable...

it should something like this for rectangular blits:
Code:

float abscos = (float)fabs(cosf(angle));
float abssin = (float)fabs(sinf(angle));
float halfscale = scale/2.0f;
int halfwidth = (int)(abscos*source->GetWidth() + abssin*source->GetHeight())*halfscale;
int halfheight = (int)(abscos*source->GetHeight() + abssin*source->GetWidth())*halfscale;

Rect destRect( (int)(destX-halfwidth), (int)(destY-halfheight), (int)(destX+halfwidth), (int)(destY+halfheight) );



Messy, but it works. The overhead of getting a tighter bounding rect may not be worth the work... who knows.
Mm, looks better... :) I have something similar in GapiDraw...
Johan wrote:Mm, looks better... :) I have something similar in GapiDraw...


Of course you do =)
:)
Actually I was sitting with it this weekend. It occured to me that I didn't have rectangle optimization in the rotated blits in GapiDraw so I added it. The reason for my interest in this thread is mostly for the fun of it...

By the way fast_rx, due to rounding errors the rectangle will be too small (it does not round, it clips). You want to add 1.0, like:

Code:

int halfwidth = (int)((abscos*source->GetWidth() + abssin*source->GetHeight())*halfscale + 1.0f);
int halfheight = (int)((abscos*source->GetHeight() + abssin*source->GetWidth())*halfscale + 1.0f);
fast_rx,

Just wondering if you ever posted the antialiased version?


Thanks!
No, I never posted it. Sorry to keep you waiting. The way I put it into pocketfrog is a bit messy - and I never went back and cleaned it up, but here goes...

The main processing is done in algo2d.h by the following code. 'A' is the over sample value 1=2x, 2=4x, etc. 'AT' is not used and should be removed.
Code:

template<typename PixelFunction> inline
void BlitTransformMaskQ( PixelFunction& F, PHAL::Surface& buffer, const Rect& destRect, const unsigned transform[5][2], const PHAL::Surface& source, Pixel mask, int A, int AT )
{
int AA = 1<<A;
int AAA = AA*AA;
int AAS = A*2;

unsigned y1 = (destRect.top << 6) + (1<<5);
unsigned y2 = (destRect.bottom << 6) + (1<<5);
unsigned x1 = (destRect.left << 6) + (1<<5);
unsigned x2 = (destRect.right << 6) + (1<<5);

unsigned uY = x1 * transform[0][0] + y1 * transform[1][0] + transform[2][0];
unsigned vY = x1 * transform[0][1] + y1 * transform[1][1] + transform[2][1];

unsigned dudx = transform[0][0] << 6;
unsigned dvdx = transform[0][1] << 6;
unsigned dudy = transform[1][0] << 6;
unsigned dvdy = transform[1][1] << 6;

unsigned dudxAT = transform[3][0] << 6;
unsigned dvdxAT = transform[3][1] << 6;
unsigned dudyAT = transform[4][0] << 6;
unsigned dvdyAT = transform[4][1] << 6;

const unsigned long xGxRxB_mask = RED_MASK | BLUE_MASK | (GREEN_MASK << 16);



for (unsigned dy = y1; dy != y2; dy += (1<<6) )
{
Pixel* dest = buffer.GetPixels( x1 >> 6, dy >> 6 );

unsigned uX = uY;
unsigned vX = vY;

for (unsigned dx = x1; dx != x2; dx += (1<<6) )
{

unsigned uJ = uX;
unsigned vJ = vX;

unsigned long xGxRxB = 0;

unsigned long col;

for(int aay=0; aay<AA; aay++)
{
unsigned uI = uJ;
unsigned vI = vJ;

for(int aax=0; aax<AA; aax++)
{

unsigned ix = uI >> 16;
unsigned iy = vI >> 16;

if (ix < (unsigned)source.GetWidth() && iy < (unsigned)source.GetHeight() )
{
const Pixel& src = *source.GetPixels(ix,iy);
col = src;
if(col == mask)
col = *dest;
}
else
{
col = *dest;
}

xGxRxB += (col | (col<<16)) & xGxRxB_mask;

uI += (dudxAT);
vI += (dvdxAT);
}

uJ += (dudyAT);
vJ += (dvdyAT);
}

xGxRxB = (xGxRxB>>AAS) & xGxRxB_mask;
xGxRxB |= xGxRxB >> 16;

*dest = F( (Pixel)xGxRxB, *dest );


++dest;

uX += dudx;
vX += dvdx;

}

uY += dudy;
vY += dvdy;
}
}


Note that the transform matrix is 5x2... It seemed like I should have been able to calculate those extra values from the others, but I never could get it to come out right. Basically, the dudx, etc, are subdivided to get you the oversampling needed. The resulting pixel value is an average of several pixels.

The extra matrix values are calculated in a copy of the blitRotated code, with the addition of this

Code:

ATI = 1.0f/AT;

T[3][0] = unsigned int(c*ATI);
T[3][1] = unsigned int((-s)*ATI);
T[4][0] = unsigned int(s*ATI);
T[4][1] = unsigned int(c*ATI);

..blah blah blah..
m_raster->BlitTransformQ( *m_surface->m_buffer, destRect, T, *source->m_buffer, AA, AT );



An AT of 2 would give you evenly spaced oversampling and can be increased/decreased to make the image sharper/blurred.

When I have time, I may work this into a separate include file and bypass adding it to PFrog - since it's so much trouble trying to explain how to add it, although I'm not sure how that would affect the speed.
Thanks. I'll play around with this and see what I can come up with.