Borland C++Builder 6和Embarcadero C++Builder 2010中也出现了同样的问题。
当我试图反转图像时,表单会冻结大约5秒,什么也不会发生。当我再次点击时,图像在眨眼之间就会反转,一次又一次。。。
要复制,请使用以下代码创建一个具有一个TImage
和两个TButton
的表单:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <jpeg.hpp>
#pragma hdrstop
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "Unit1.h"
TForm1 *Form1;
Graphics::TBitmap *CurrentBitmap;
bool bLoaded = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
CurrentBitmap = new Graphics::TBitmap;
}
//---------------------------------------------------------------------------
bool __fastcall Invert( TImage *img ) {
if( bLoaded ) {
double Scale, ScaleW, ScaleH;
Graphics::TBitmap *bmp = new Graphics::TBitmap;
bmp->Assign( CurrentBitmap );
DWORD **pixel = new DWORD*[bmp->Height];
for( long y=0; y<bmp->Height; y++ ) {
pixel[y] = (DWORD*)(bmp->ScanLine[y]);
for( long x=0; x<bmp->Width; x++ ) {
if( pixel[y][x] == clBlack ) {
pixel[y][x] = clWhite;
} else if( pixel[y][x] == clWhite ) {
pixel[y][x] = clBlack;
}
}
}
delete[] pixel;
ScaleW = (double)bmp->Width / img->ClientWidth;
ScaleH = (double)bmp->Height / img->ClientHeight;
if( ScaleW > ScaleH ) {
Scale = ScaleW;
} else {
Scale = ScaleH;
}
CurrentBitmap->Assign( bmp );
img->Picture->Bitmap->Canvas->StretchDraw(Rect(0, 0, bmp->Width/Scale, bmp->Height/Scale), bmp );
delete bmp;
return true;
} else {
return false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) {
TJPEGImage *jpg = new TJPEGImage();
jpg->LoadFromFile( "V:\CBuilder\BCB10\GerberTest\Testdata\GerberTest.dpi2400.mskcmp.jpg" );
CurrentBitmap->Width = jpg->Width;
CurrentBitmap->Height = jpg->Height;
CurrentBitmap->Canvas->StretchDraw(Rect(0, 0, jpg->Width, jpg->Height), jpg );
bLoaded = true;
double Scale, ScaleW, ScaleH;
ScaleW = (double)jpg->Width / Image1->ClientWidth;
ScaleH = (double)jpg->Height / Image1->ClientHeight;
if( ScaleW > ScaleH ) {
Scale = ScaleW;
} else {
Scale = ScaleH;
}
Image1->Canvas->StretchDraw(Rect(0, 0, jpg->Width/Scale, jpg->Height/Scale), jpg );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) {
Invert( Image1 );
}
我无法解释应用程序冻结的原因,也无法解释代码在5秒延迟期间在做什么。
就像雷米·勒博提到的那样,你应该设置PixelFormat。我试过你的解决方案,似乎没有正确地反转图像。下面你可以找到一个正常工作的解决方案。
#include <JPEG.hpp>
Byte __fastcall IntToByte(int AValue)
{
if (AValue > 255)
return 255;
else if (AValue < 0)
return 0;
else
return AValue;
}
// ---------------------------------------------------------------------------
void __fastcall InvertBitmap(Graphics::TBitmap *ABitmap)
{
Byte *p0;
int red, green, blue;
for (int y = 0; y < ABitmap->Height; y++) {
p0 = (Byte*) ABitmap->ScanLine[y];
for (int x = 0; x < ABitmap->Width; x++) {
red = p0[x * 3];
green = p0[x * 3 + 1];
blue = p0[x * 3 + 2];
// invert
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
p0[x * 3] = IntToByte(red);
p0[x * 3 + 1] = IntToByte(green);
p0[x * 3 + 2] = IntToByte(blue);
}
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Screen->Cursor = crHourGlass;
TJPEGImage *jpeg = new TJPEGImage();
Graphics::TBitmap *bitmap = new Graphics::TBitmap();
try {
jpeg->LoadFromFile("**YOUR-JPEG-FILE**");
bitmap->SetSize(jpeg->Width, jpeg->Height);
bitmap->Canvas->Draw(0, 0, jpeg);
bitmap->PixelFormat = pf24bit;
InvertBitmap(bitmap);
Image1->Picture->Bitmap->Assign(bitmap);
}
__finally {
jpeg->Free();
bitmap->Free();
}
Screen->Cursor = crDefault;
}
// ---------------------------------------------------------------------------