Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

JimmySkull

By: a guest on Jun 19th, 2008  |  syntax: Delphi  |  size: 6.43 KB  |  hits: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. // Redimensionar imagens
  2. // http://0x1f.blogspot.com/
  3.  
  4. // Unit com algoritmos de redimensionamento de imagens
  5.  
  6. unit BitmapResize;
  7.  
  8. interface
  9.  
  10. uses
  11.   Windows, Classes, SysUtils, Graphics;
  12.  
  13. const PixelCountMax = 32768;
  14.  
  15. type
  16.   PRGBTripleArray = ^TRGBTripleArray;
  17.   TRGBTripleArray = array[0..PixelCountMax - 1] of TRGBTriple;
  18.  
  19. procedure ResizeBicubic(Src: TBitmap; var Dest: TBitmap;
  20.   DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
  21.  
  22. procedure ResizeBilinear(Src: TBitmap; var Dest: TBitmap;
  23.   DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
  24.  
  25. procedure ResizeNearestNeighbor(Src: TBitmap; var Dest: TBitmap;
  26.   DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
  27.  
  28. implementation
  29.  
  30. function Sinc(x: double): double;
  31. begin
  32.   if abs(x) < style = "font - weight: bold; " > then
  33.     Result := 1 - 2 * x * x + x * x * abs(x)
  34.   else if (abs(x) >= 1) and (abs(x) < style = "font - weight: bold; " > then
  35.     Result := 4 - 8 * abs(x) + 5 * x * x - x * x * abs(x)
  36. else Result := 0;
  37. end;
  38.  
  39. procedure Bicubic(I1, I2, I3, I4: TRGBTriple; var New: TRGBTriple; u: double);
  40. var
  41.   t: integer;
  42. begin
  43.   t := trunc(I1.rgbtRed * Sinc(u + 1) + I2.rgbtRed
  44.     * Sinc(u) + I3.rgbtRed * Sinc(u - 1) + I4.rgbtRed * Sinc(u - 2));
  45.   if t > 255 then t := 255;
  46.   if t < style = "font - weight: bold; " > then t := 0;
  47.   New.rgbtRed := Byte(t);
  48.  
  49.   t := trunc(I1.rgbtGreen * Sinc(u + 1) + I2.rgbtGreen
  50.     * Sinc(u) + I3.rgbtGreen * Sinc(u - 1) + I4.rgbtGreen * Sinc(u - 2));
  51.   if t > 255 then t := 255;
  52.   if t < style = "font - weight: bold; " > then t := 0;
  53.   New.rgbtGreen := Byte(t);
  54.  
  55.   t := trunc(I1.rgbtBlue * Sinc(u + 1) + I2.rgbtBlue
  56.     * Sinc(u) + I3.rgbtBlue * Sinc(u - 1) + I4.rgbtBlue * Sinc(u - 2));
  57.   if t > 255 then t := 255;
  58.   if t < style = "font - weight: bold; " > then t := 0;
  59.   New.rgbtBlue := Byte(t);
  60. end;
  61.  
  62. procedure ResizeBicubic(Src: TBitmap; var Dest: TBitmap;
  63.   DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
  64. var
  65.   hScale, wScale: double;
  66.   f1, f2, f3, f4, fNew: TRGBTriple;
  67.   temp1, temp2, temp3, temp4, tempDst: PRGBTripleArray;
  68.   x, y, u, v: double;
  69.   x1, x2, x3, x4, y1, y2, y3, y4, i, j, tempRGB: integer;
  70. begin
  71.   Dest := TBitmap.Create;
  72.   Dest.PixelFormat := pf24Bit;
  73.   Dest.Width := DestWidth;
  74.   Dest.Height := DestHeight;
  75.  
  76.   Src.PixelFormat := pf24Bit;
  77.   Src.Width := SrcWidth;
  78.   Src.Height := SrcHeight;
  79.  
  80.   hScale := DestHeight / SrcHeight;
  81.   wScale := DestWidth / SrcWidth;
  82.  
  83.   for i := 0 to DestHeight - 1 do
  84.   begin
  85.     x := i / hScale;
  86.     x2 := trunc(x);
  87.     x1 := x2 - 1;
  88.     x3 := x2 + 1;
  89.     x4 := x2 + 2;
  90.     if x1 < style = "font - weight: bold; " > then x1 := 0;
  91.     if x3 > SrcHeight - 1 then x3 := SrcHeight - 1;
  92.     if x4 > SrcHeight - 1 then x4 := SrcHeight - 1;
  93.  
  94.     temp1 := Src.ScanLine[x1];
  95.     temp2 := Src.ScanLine[x2];
  96.     temp3 := Src.ScanLine[x3];
  97.     temp4 := Src.ScanLine[x4];
  98.     tempDst := Dest.ScanLine[i];
  99.  
  100.     v := x - x2;
  101.  
  102.     for j := 0 to DestWidth - 1 do
  103.     begin
  104.       y := j / wScale;
  105.       y2 := trunc(y);
  106.       y1 := y2 - 1;
  107.       y3 := y2 + 1;
  108.       y4 := y2 + 2;
  109.       if y1 < style = "font - weight: bold; " > then y1 := 0;
  110.       if y3 > SrcWidth - 1 then y3 := SrcWidth - 1;
  111.       if y4 > SrcWidth - 1 then y4 := SrcWidth - 1;
  112.  
  113.       u := y - y2;
  114.  
  115.       Bicubic(temp1^[y1], temp1^[y2], temp1^[y3], temp1^[y4], f1, u);
  116.       Bicubic(temp2^[y1], temp2^[y2], temp2^[y3], temp2^[y4], f2, u);
  117.       Bicubic(temp3^[y1], temp3^[y2], temp3^[y3], temp3^[y4], f3, u);
  118.       Bicubic(temp4^[y1], temp4^[y2], temp4^[y3], temp4^[y4], f4, u);
  119.       Bicubic(f1, f2, f3, f4, fNew, v);
  120.  
  121.       tempDst^[j] := fNew;
  122.     end;
  123.   end;
  124. end;
  125.  
  126. procedure ResizeBilinear(Src: TBitmap; var Dest: TBitmap;
  127.   DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
  128. var
  129.   hScale, wScale: double;
  130.   f1, f2, f3, f4, f12, f34, fNew: TRGBTriple;
  131.   temp1, temp2, tempDst: PRGBTripleArray;
  132.   x, y: double;
  133.   x1, x2, y1, y2, i, j: integer;
  134. begin
  135.   Dest := TBitmap.Create;
  136.   Dest.PixelFormat := pf24Bit;
  137.   Dest.Width := DestWidth;
  138.   Dest.Height := DestHeight;
  139.  
  140.   Src.PixelFormat := pf24Bit;
  141.   Src.Width := SrcWidth;
  142.   Src.Height := SrcHeight;
  143.  
  144.   hScale := DestHeight / SrcHeight;
  145.   wScale := DestWidth / SrcWidth;
  146.  
  147.   for i := 0 to DestHeight - 1 do
  148.   begin
  149.     x := i / hScale;
  150.     x1 := trunc(x);
  151.     x2 := x1 + 1;
  152.     if x2 > SrcHeight - 1 then x2 := SrcHeight - 1;
  153.  
  154.     temp1 := Src.ScanLine[x1];
  155.     temp2 := Src.ScanLine[x2];
  156.     tempDst := Dest.ScanLine[i];
  157.  
  158.     for j := 0 to DestWidth - 1 do
  159.     begin
  160.       y := j / wScale;
  161.       y1 := trunc(y);
  162.       y2 := y1 + 1;
  163.       if y2 > SrcWidth - 1 then y2 := SrcWidth - 1;
  164.  
  165.       f1 := temp1^[y1];
  166.       f2 := temp1^[y2];
  167.       f3 := temp2^[y1];
  168.       f4 := temp2^[y2];
  169.  
  170.       f12.rgbtRed := trunc(f1.rgbtRed + (y - y1) * (f2.rgbtRed - f1.rgbtRed));
  171.       f12.rgbtGreen := trunc(f1.rgbtGreen + (y - y1) * (f2.rgbtGreen - f1.rgbtGreen));
  172.       f12.rgbtBlue := trunc(f1.rgbtBlue + (y - y1) * (f2.rgbtBlue - f1.rgbtBlue));
  173.  
  174.       f34.rgbtRed := trunc(f3.rgbtRed + (y - y1) * (f4.rgbtRed - f3.rgbtRed));
  175.       f34.rgbtGreen := trunc(f3.rgbtGreen + (y - y1) * (f4.rgbtGreen - f3.rgbtGreen));
  176.       f34.rgbtBlue := trunc(f3.rgbtBlue + (y - y1) * (f4.rgbtBlue - f3.rgbtBlue));
  177.  
  178.       fNew.rgbtRed := trunc(f12.rgbtRed + (x - x1) * (f34.rgbtRed - f12.rgbtRed));
  179.       fNew.rgbtGreen := trunc(f12.rgbtGreen + (x - x1) * (f34.rgbtGreen - f12.rgbtGreen));
  180.       fNew.rgbtBlue := trunc(f12.rgbtBlue + (x - x1) * (f34.rgbtBlue - f12.rgbtBlue));
  181.  
  182.       tempDst^[j] := fNew;
  183.     end;
  184.   end;
  185. end;
  186.  
  187. procedure ResizeNearestNeighbor(Src: TBitmap; var Dest: TBitmap;
  188.   DestWidth, DestHeight, SrcWidth, SrcHeight: integer);
  189. var
  190.   hScale, wScale: double;
  191.   fNew: TRGBTriple;
  192.   tempSrc, tempDst: PRGBTripleArray;
  193.   x, y, i, j: integer;
  194. begin
  195.   Dest := TBitmap.Create;
  196.   Dest.PixelFormat := pf24Bit;
  197.   Dest.Width := DestWidth;
  198.   Dest.Height := DestHeight;
  199.  
  200.   Src.PixelFormat := pf24Bit;
  201.   Src.Width := SrcWidth;
  202.   Src.Height := SrcHeight;
  203.  
  204.   hScale := DestHeight / SrcHeight;
  205.   wScale := DestWidth / SrcWidth;
  206.  
  207.   for i := 0 to DestHeight - 1 do
  208.   begin
  209.     x := round(i / hScale);
  210.     if x > SrcHeight - 1 then x := SrcHeight - 1;
  211.  
  212.     tempSrc := Src.ScanLine[x];
  213.     tempDst := Dest.ScanLine[i];
  214.  
  215.     for j := 0 to DestWidth - 1 do
  216.     begin
  217.       y := round(j / wScale);
  218.       if y > SrcWidth - 1 then y := SrcWidth - 1;
  219.  
  220.       fNew := tempSrc^[y];
  221.       tempDst^[j] := fNew;
  222.     end;
  223.   end;
  224. end;
  225.  
  226. end.