閱讀提示:
《Delphi影像處理》系列以效率為側重點,一般代碼為PASCAL,核心代碼採用BASM。
《C++影像處理》系列以代碼清晰,可讀性為主,全部使用C++代碼。
儘可能保持二者內容一致,可相互對照。
本文代碼必須包括文章《Delphi影像處理 -- 資料類型及公用過程》中的ImageData.pas單元。
映像高保真反差處理很簡單,其步驟如下:
1、備份映像原資料;
2、按給定半徑對映像進行高斯模糊;
3、用原像素與高斯模糊後的像素相減,形成一個差值;
4、差值加128,完畢。
下面是高保真反差代碼,包括高斯模糊代碼(關於高斯模糊見文章《Delphi影像處理 -- 高斯模糊》,下面的高斯模糊代碼也是從該文拷貝來的):
procedure CrossBlur(var Dest: TImageData; const Source: TImageData; Weights: Pointer; Radius: Integer);var height, srcStride: Integer; dstOffset, srcOffset: Integer;asm push esi push edi push ebx push ecx mov ecx, [edx].TImageData.Stride mov srcStride, ecx call _SetCopyRegs mov height, edx mov srcOffset, eax mov dstOffset, ebx pop ebx pxor xmm7, xmm7 push esi // pst = Source.Scan0 push edi push edx push ecx // blur col mov eax, srcStride mov edx, eax shr edx, 2 // width = Source.Width mov edi, Radius shl edi, 1 imul edi, eax add edi, esi // psb = pst + Radius * 2 * Source.Stride@@cyLoop: push edx@@cxLoop: push esi push edi push ebx mov ecx, Radius pxor xmm0, xmm0 // sum = 0@@cblurLoop: movd xmm1, [esi] // for (i = 0; i < Radius; i ++) movd xmm2, [edi] // { punpcklbw xmm1, xmm7 punpcklbw xmm2, xmm7 paddw xmm1, xmm2 // ps = pst + psb punpcklwd xmm1, xmm7 cvtdq2ps xmm1, xmm1 // pfs (flaot * 4) = ps (int * 4) mulps xmm1, [ebx] // pfs *= Weights[i] addps xmm0, xmm1 // sum += pfs add ebx, 16 add esi, eax // pst += Source.Stride sub edi, eax // psb -= Source.Stride loop @@cblurLoop // } movd xmm1, [esi] punpcklbw xmm1, xmm7 punpcklwd xmm1, xmm7 cvtdq2ps xmm1, xmm1 // pfs (flaot * 4) = pst (int * 4) mulps xmm1, [ebx] // pfs *= Weights[Radius] addps xmm0, xmm1 // sum += pfs pop ebx pop edi pop esi cvtps2dq xmm0, xmm0 // ps (int * 4) = sum (flaot * 4) packssdw xmm0, xmm7 packuswb xmm0, xmm7 movd [esi], xmm0 // pst (byte * 4) = ps (int * 4) pask add esi, 4 add edi, 4 dec edx jnz @@cxLoop pop edx dec height jnz @@cyLoop pop edx pop height pop edi // pd = Dest.Scan0 pop esi // psl = pst mov eax, Radius shl eax, 1+2 add eax, esi // psr = psl + Radius * 2 // blur row@@ryLoop: push edx // width = Dest.Width@@rxLoop: push esi push ebx push eax mov ecx, Radius pxor xmm0, xmm0 // sum = 0@@rblurLoop: movd xmm1, [esi] // for (i = 0; i < Radius; i ++) movd xmm2, [eax] // { punpcklbw xmm1, xmm7 punpcklbw xmm2, xmm7 paddw xmm1, xmm2 // ps = psl + psr punpcklwd xmm1, xmm7 cvtdq2ps xmm1, xmm1 // pfs (flaot * 4) = ps (int * 4) mulps xmm1, [ebx] // pfs *= Weights[i] addps xmm0, xmm1 // sum += pfs add ebx, 16 add esi, 4 // psl ++ sub eax, 4 // psr -- loop @@rblurLoop // } movd xmm1, [esi] punpcklbw xmm1, xmm7 punpcklwd xmm1, xmm7 cvtdq2ps xmm1, xmm1 // pfs (flaot * 4) = psl (int * 4) mulps xmm1, [ebx] // pfs *= Weights[Radius] addps xmm0, xmm1 // sum += pfs cvtps2dq xmm0, xmm0 // ps (int * 4) = sum (flaot * 4) packssdw xmm0, xmm7 packuswb xmm0, xmm7 movd [edi], xmm0 // pd (byte * 4) = ps (int * 4) pask pop eax pop ebx pop esi add eax, 4 add esi, 4 add edi, 4 dec edx jnz @@rxLoop add eax, srcOffset add esi, srcOffset add edi, dstOffset pop edx dec height jnz @@ryLoop pop ebx pop edi pop esiend;// --> st x// <-- st e**x = 2**(x*log2(e))function _Expon: Extended;asm fldl2e // y = x*log2e fmul fld st(0) // i = round(y) frndint fsub st(1), st // f = y - i fxch st(1) // z = 2**f f2xm1 fld1 fadd fscale // result = z * 2**i fstp st(1)end;function GetWeights(var Buffer, Weights: Pointer; Q: Single; Radius: Integer): Integer;const _fcd1: Single = 0.1; _fc1: Single = 1.0; _fc2: Single = 2.0; _fc250: Single = 250.0; _fc255: Single = 255.0;var R: Integer; v, QQ2: double;asm mov R, ecx mov ecx, eax fld Q fabs fcom _fcd1 fstsw ax sahf jae @@1 fld _fcd1 fstp st(1) // if (Q < 0.1) Q = 0.1 jmp @@2@@1: fcom _fc250 fstsw ax sahf jbe @@2 fld _fc250 fstp st(1) // if (Q > 250) Q = 250@@2: fst Q fmul Q fmul _fc2 fstp QQ2 // QQ2 = 2 * Q * Q fwait mov eax, R test eax, eax jg @@10 push eax // if (radius <= 0) fld1 // { fadd Q // radius = Abs(Q) + 1 fistp [esp].Integer fwait pop eax@@testRadius: // while (TRUE) mov R, eax // { fldz // sum = 0@@testLoop: // for (R = radius; R > 0; R ++) fild R // { fld st(0) fmulp st(1), st fdiv QQ2 fchs call _Expon // tmp = Exp(-(R * R) / (2.0 * Q * Q)); cmp R, eax jne @@3 fst v // if (R == radius) v = tmp@@3: faddp st(1), st(0) // sum += tmp dec R jnz @@testLoop // } fmul _fc2 // sum *= 2 fadd _fc1 // sum += 1 fdivr v fmul _fc255 fistp R cmp R, 0 je @@4 // if ((INT)(v / sum * 255 + 0.5) = 0) break inc eax // radius ++ jmp @@testRadius // }@@4: dec eax jnz @@5 inc eax@@5: mov R, eax // }@@10: inc eax shl eax, 4 add eax, 12 push edx push ecx mov edx, eax mov eax, GHND call GlobalAllocPtr pop ecx pop edx test eax, eax jz @@Exit mov [ecx], eax // buffer = GlobalAllocPtr(GHND, (Radius + 1) * 16 + 12) add eax, 12 and eax, -16 mov [edx], eax // weights = ((char* )buffer + 12) & 0xfffffff0 mov ecx, R // ecx = radius mov edx, eax // edx = weights fldz // for (i = radius, sum = 0; i > 0; i --)@@clacLoop: // { fild R fld st(0) fmulp st(1), st fdiv QQ2 fchs call _Expon fstp [edx].Double // weights[i] = Expon(-(i * i) / (2 * Q * Q)) fadd [edx].Double // sum += weights[i] add edx, 16 dec R jnz @@clacLoop // } fmul _fc2 // sum *= 2 fld1 fstp [edx].Double // weights[radius] = 1 fadd [edx].Double // sum += weights[radius] push ecx inc ecx@@divLoop: // for (i = 0; i <= Radius; i ++) fld st(0) // weights[i] = Round(weights[i] / sum) fdivr [eax].Double fst [eax].Single fst [eax+4].Single fst [eax+8].Single fstp [eax+12].Single add eax, 16 loop @@divLoop ffree st(0) fwait pop eax // return Radius@@Exit:end;// 高斯模糊。procedure ImageGaussiabBlur(var Data: TImageData; Q: Single; Radius: Integer);var Buffer, Weights: Pointer; src: TImageData;begin Radius := GetWeights(Buffer, Weights, Q, Radius); if Radius = 0 then Exit; if Data.AlphaFlag then ArgbConvertPArgb(Data); src := _GetExpandData(Data, Radius); CrossBlur(Data, src, Weights, Radius); FreeImageData(src); GlobalFreePtr(Buffer); if Data.AlphaFlag then PArgbConvertArgb(Data);end;procedure DoHighPass(var Dest: TImageData; const Source: TImageData);asm push esi push edi push ebx pxor mm7, mm7 mov ecx, 128 movd mm6, ecx pshufw mm6, mm6, 0 call _SetCopyRegs@@yLoop: push ecx@@xLoop: movd mm0, [esi] // mm0 = 原像素 movd mm1, [edi] // mm1 = 高斯模糊後像素 punpcklbw mm0, mm7 punpcklbw mm1, mm7 psubw mm0, mm1 paddw mm0, mm6 // mm0 = mm0 - mm1 + 128 packuswb mm0, mm7 movd [edi], mm0 add esi, 3 add edi, 3 movsb loop @@xLoop pop ecx add esi, eax add edi, ebx dec edx jnz @@yLoop pop ebx pop edi pop esi emmsend;// 高保真反差procedure ImageHighPass(var Data: TImageData; Radius: Single); procedure CopyData(var Dest: TImageData; const Source: TImageData); asm push esi push edi mov ecx, [eax].TImageData.Width imul ecx, [eax].TImageData.Height mov edi, [eax].TImageData.Scan0 mov esi, [edx].TImageData.Scan0 rep movsd pop edi pop esi end;var IsInvertScan0: Boolean; Src: TImageData;begin // 備份映像資料 Src := NewImageData(Data.Width, Data.Height); IsInvertScan0 := Data.Stride < 0; if IsInvertScan0 then _InvertScan0(Data); CopyData(Src, Data); // 映像資料作高斯模糊 ImageGaussiabBlur(Data, Radius, 0); // 用備份映像資料和高斯模糊映像資料作高保真反差 DoHighPass(Data, Src); FreeImageData(Src); if IsInvertScan0 then _InvertScan0(Data);end;
簡單例子:
procedure TForm1.Button2Click(Sender: TObject);var bmp: TBitmap; data: TImageData;begin bmp := TBitmap.Create; bmp.LoadFromFile('d:\source.bmp'); data := GetBitmapData(bmp); ImageHighPass(Data, 3); Canvas.Draw(0, 0, bmp); bmp.Free;end;
下面是原圖以及例子運行介面:
《Delphi影像處理》系列使用GDI+單元和說明見文章《GDI+ for VCL基礎 -- GDI+ 與 VCL》。
因水平有限,錯誤在所難免,歡迎指正和指導。郵箱地址:maozefa@hotmail.com
這裡可訪問《Delphi影像處理 -- 文章索引》。