从delphi转换过来的,具体参考
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1600015
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1153142
//旋转方向
typedef enum RotateDirection
{
RD_CLOCKWISE = 1,
RD_ANTI_CLOCKWISE = 2
}ROTATEDIRECTION;
long __fastcall GetPixelSize(Graphics::TBitmap *aBitmap)
{
long nBitCount, nMultiplier;
long BitsPerByte = 8;
switch (aBitmap->PixelFormat)
{
case pfDevice:
nBitCount = GetDeviceCaps(aBitmap->Canvas->Handle, BITSPIXEL);
nMultiplier = nBitCount / BitsPerByte;
if (nBitCount % BitsPerByte)
{
nMultiplier ++;
}
break;
case pf1bit:
nMultiplier = 1;
break;
case pf4bit:
nMultiplier = 1;
break;
case pf8bit:
nMultiplier = 1;
break;
case pf15bit:
nMultiplier = 2;
break;
case pf16bit:
nMultiplier = 2;
break;
case pf24bit:
nMultiplier = 3;
break;
case pf32bit:
nMultiplier = 4;
break;
default :
//ShowMessage("Unknow bitmap pileformat/n");
nMultiplier = -1;
}
return (nMultiplier);
}
//---------------------------------------------------------------------------
void __fastcall ImageRotate90(Graphics::TBitmap *aBitmap,ROTATEDIRECTION RDir)
{
long nIdx,nOfs,x,y,i,nMultiplier;
long nMemWidth, nMemHeight, nMemSize,nScanLineSize;
char *aScnLnBuffer;
PByteArray aScanLine;
if (!aBitmap)
{
return;
}
nMultiplier = GetPixelSize(aBitmap);
if (nMultiplier < 3)
{
aBitmap->PixelFormat = pf24bit;
nMultiplier = 3;
}
nMemWidth = aBitmap->Height;
nMemHeight = aBitmap->Width;
nMemSize = nMemWidth * nMemHeight * nMultiplier;
aScnLnBuffer = (char *)malloc(sizeof(char *) * nMemSize);
try
{
nScanLineSize = aBitmap->Width * nMultiplier;
aScanLine = (PByteArray)malloc(nScanLineSize);
try
{
for (y = 0;y < aBitmap->Height;y ++)
{
Move(aBitmap->ScanLine[y],aScanLine,nScanLineSize);
for (x = 0;x < aBitmap->Width;x ++)
{
if (RDir == RD_ANTI_CLOCKWISE)
{
nIdx = ((aBitmap->Width - 1) - x) * nMultiplier;
nOfs = (x * nMemWidth * nMultiplier) + (y * nMultiplier);
}
else
{
nIdx = x * nMultiplier;
nOfs = (x * nMemWidth * nMultiplier) + (((aBitmap->Height - 1) - y) * nMultiplier);
}
for (i = 0;i < nMultiplier;i ++)
{
(Byte)aScnLnBuffer[nOfs + i] = aScanLine[nIdx + i];
}
}
}
aBitmap->Height = nMemHeight;
aBitmap->Width = nMemWidth;
for (y = 0;y < nMemHeight;y ++)
{
nOfs = y * nMemWidth * nMultiplier;
Move(&(aScnLnBuffer[nOfs]),aBitmap->ScanLine[y],nMemWidth * nMultiplier);
}
}
catch (...)
{
//ShowMessage("error/n");
}
}
catch (...)
{
//ShowMessage("error error/n");
}
free(aScanLine);
free(aScnLnBuffer);
}