I'm attempting to optimise some CImage manipulations by using GetBits() rather than the time consuming GetPixel() SetPixel() method.
These variables are defined (with CImage *i as a parameter);
int height = i->GetWidth(); //width and height swapped for 90 degree rotation
int width = i->GetHeight();
int bpp = i->GetBPP();
int pitch1 = i->GetPitch();
int hwidth = width / 2;
int hheight = height / 2;
double sinma = sin(1.57079633f); //90 degree rotation
double cosma = cos(1.57079633f);
CImage *dest = new CImage();
dest->Create(width, height, bpp);
int pitch2 = dest->GetPitch();
The non-optimised code works fine:
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int yt = x - hwidth;
int xt = y - hheight;
int ys = (int)round((cosma * xt - sinma * yt) + hwidth); //get rotated pixel
int xs = (int)round((sinma * xt + cosma * yt) + hheight);
if (xs >= 0 && xs < height && ys >= 0 && ys < width) {
// set target pixel (x,y) to color at (xs,ys)
dest->SetPixel(x, y, i->GetPixel(xs, ys));
}
else {
// set target pixel (x,y) to some default background
COLORREF pixel = RGB(0, 0, 0);
dest->SetPixel(x, y, pixel);
}
}
}
This produces a perfect result
but a weird result occurs when optimising:
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int yt = x - hwidth;
int xt = y - hheight;
int ys = (int)round((cosma * xt - sinma * yt) + hwidth);
int xs = (int)round((sinma * xt + cosma * yt) + hheight);
if (xs >= 0 && xs < height && ys >= 0 && ys < width) {
// set target pixel (x,y) to color at (xs,ys)
//dest->SetPixel(x, y, i->GetPixel(xs, ys));
lAdrs2 = x * pitch2 + y * 3; // set target pixel (x,y)
lAdrs1 = ys * pitch1 + xs * 3; // to color at (xs,ys)
bRed = *(pInImage + lAdrs1);
bGreen = *(pInImage + lAdrs1 + 1);
bBlue = *(pInImage + lAdrs1 + 2);
*(pOutImage + lAdrs2) = static_cast<BYTE>(bRed);
*(pOutImage + lAdrs2 + 1) = static_cast<BYTE>(bGreen);
*(pOutImage + lAdrs2 + 2) = static_cast<BYTE>(bBlue);
}
}
}
When the angle is set to 0, this happens
Any reason that this occurs when accessing the bits manually?
Aucun commentaire:
Enregistrer un commentaire