Reading Tips:
《Delphi Image ProcessingThe series focuses on efficiency. The general code is Pascal, and the core code is BaSm.
The C ++ image processing series focuses on code clarity and readability, all using C ++ code.
Make sure that the two items are consistent and can be compared with each other.
The code in this article must include the imagedata. Pas unit in "Delphi Image Processing-data type and public process" and "Delphi Image Processing-Plane Geometric transformation" transformmatrix. Pas unit.
The general principle of image relief is to form a difference between each pixel on the image and its diagonal pixel, so that similar color values are reduced and different color values are highlighted, resulting in a vertical depth, to achieve the relief effect, the specific approach is to subtract two pixel values in the diagonal line, plus a background constant, generally 128. This algorithm is simple and quick, but cannot adjust the angle and depth of the image relief effect.
The image relief effect implemented by Photoshop allows you to adjust the relief angle and depth (the distance between two pixels) and adjust the number of pixel differences. The basic algorithm principle is the same as the general relief effect, but the specific method is different: For each pixel to be processed, the position of two corresponding points is calculated according to the relief angle and depth, then, calculate the color value of the two positions and make it form the difference value. Then, multiply the percentage of the difference value in the relief, and add the background color of 128. Note that the two corresponding points calculated here are logical points rather than actual pixels. For example, to implement an image relief effect with a 45 degree angle and a depth of 3, P (x, y). The locations of the two logical points are P0 (X-3*0.7071/2, Y-3*0.7071/2) and p1 (x + 3*0.7071/2, Y + 3*0.7071/2), obviously for such two logical points, it is impossible to find the corresponding pixel from the image directly. If it is simply rounded down, it will produce a large number of identical relief effects formed by different angles and depths, this is not the expected result, and it makes the relief angle and depth parameters lose its original meaning. To this end, the original image must be scaled according to the relief angle and depth, then the relief effect of each pixel is processed, and then the original image size is reduced to complete the relief effect process. The following is the code for implementing the Photoshop embossed effect after repeated experiments:
Procedure getsrccolor; ASM push ESI // ESI = SRC. scan0 mov eax, ECx SAR eax, 12 imul eax, EBX add ESI, eax // ESI + = (y/4096 * SRC. stride) mov eax, edx sar eax, 12 SHL eax, 2 add ESI, eax // ESI + = (x/4096*4) Call _ getbilinearcolor movd eax, xmm0 movd mm0, eax punpcklbw mm0, mm7 // return mm0 = argb (word * 4) Pop esiend; Procedure imagegraysculpture (VAR data: timagedata; angle: single; Size: longword; num: longword = 100); var X, Y, radius: integer; xdelta, ydelta: integer; width, height: integer; dstoffset: integer; SRC: timagedata; begin radius: = (size + 1) SHR 1; // image border extension radius angle: = pI * angle/180; Size: = size SHL 12; // size = size * 256 num: = (Num SHL 9) Div 100; // num * = power xdelta of 5.12 to 2: = round (COS (angle) * size); ydelta: = round (sin (angle) * size); X: = (radius SHL 12)-(xdelta Div 2); y: = (radius SHL 12) -(ydelta Div 2); width: = x + (data. width SHL 12); Height: = Y + (data. height SHL 12); If data. alphaflag then argbconvertpargb (data); SRC: = _ getexpanddata (data, radius); ASM mov eax, data Lea edX, Src call _ setcopyregs mov dstoffset, EBX movq MM5, qword PTR argbtab [128*8] // MM5 = 00 128 00 128 00 128 00 128 movd mm6, num // mm6 = 00 num 00 num 00 num 00 num 00 num punpcklwd mm6, mm6 punpcklwd mm6, mm6 pxor mm7, mm7 // mm7 = 00 00 00 00 00 00 00 pxor xmm7, xmm7 mov ESI, SRC. scan0 mov EBX, SRC. stride mov ECx, y // For (; y
The following is a Demo code:
procedure TForm1.Button3Click(Sender: TObject);var bmp: TGpBitmap; g: TGpGraphics; data: TImageData;begin bmp := TGpBitmap.Create('..\media\20041001.jpg'); g := TGpGraphics.Create(Canvas.Handle); g.DrawImage(bmp, 0, 0); data := LockGpBitmap(bmp); ImageGraySculpture(data, 45, 3, 100); UnlockGpBitmap(bmp, data); g.DrawImage(bmp, 0, data.Height); g.Free; bmp.Free;end;
Run:
Compared with the Photoshop relief effect, it is basically the same.
Note: In the article comments, some people say that gray relief is not colored, but it is actually incorrect. I tested a lot of code on the Internet and on the book, and I also tested Photoshop, which may be colored, when the relief depth is very small and the original image color is relatively dull, it may not be colored. You can cut down the source image above and use Photoshop to try it out. Of course, if you want to achieve completely no color, it is also very easy, just make an image grayscale before the relief processing. If the gray-scale relief is completely non-colored, the program code can be simplified, and the speed can be improved by at least 60%.
For details about the use of GDI + units and descriptions in the "Delphi image processing" series, see the article 《GDI + for VCL basics-GDI + and VCL.
Due to limited levels, errors are inevitable. Correction and guidance are welcome. Email Address:Maozefa@hotmail.com
Here, you can access "Delphi Image Processing-Article Index".