DxSERGEY

Создание эскиза для JPEG изображений

Apr 20th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.72 KB | None | 0 0
  1. type
  2.   TCharArray = array of Char;
  3.   TRGBArray = array [Word] of TRGBTriple;
  4.   pRGBArray = ^TRGBArray;
  5.  
  6. procedure SmoothResize(Src, Dst: TBitmap);
  7. var
  8.   x, y: Integer;
  9.   xP, yP: Integer;
  10.   xP2, yP2: Integer;
  11.   SrcLine1, SrcLine2: pRGBArray;
  12.   t3: Integer;
  13.   z, z2, iz2: Integer;
  14.   DstLine: pRGBArray;
  15.   DstGap: Integer;
  16.   w1, w2, w3, w4: Integer;
  17. begin
  18.   Src.PixelFormat := pf24Bit;
  19.   Dst.PixelFormat := pf24Bit;
  20.  
  21.   if (Src.Width = Dst.Width) and (Src.Height = Dst.Height) then
  22.     Dst.Assign(Src)
  23.   else
  24.   begin
  25.     DstLine := Dst.ScanLine[0];
  26.     DstGap := Integer(Dst.ScanLine[1]) - Integer(DstLine);
  27.  
  28.     xP2 := MulDiv(pred(Src.Width), $10000, Dst.Width);
  29.     yP2 := MulDiv(pred(Src.Height), $10000, Dst.Height);
  30.     yP := 0;
  31.  
  32.     for y := 0 to pred(Dst.Height) do
  33.     begin
  34.       xP := 0;
  35.  
  36.       SrcLine1 := Src.ScanLine[yP shr 16];
  37.  
  38.       if (yP shr 16 < pred(Src.Height)) then
  39.         SrcLine2 := Src.ScanLine[succ(yP shr 16)]
  40.       else
  41.         SrcLine2 := Src.ScanLine[yP shr 16];
  42.  
  43.       z2 := succ(yP and $FFFF);
  44.       iz2 := succ((not yP) and $FFFF);
  45.       for x := 0 to pred(Dst.Width) do
  46.       begin
  47.         t3 := xP shr 16;
  48.         z := xP and $FFFF;
  49.         w2 := MulDiv(z, iz2, $10000);
  50.         w1 := iz2 - w2;
  51.         w4 := MulDiv(z, z2, $10000);
  52.         w3 := z2 - w4;
  53.         DstLine[x].rgbtRed := (SrcLine1[t3].rgbtRed * w1 + SrcLine1[t3 + 1].rgbtRed * w2 +
  54.           SrcLine2[t3].rgbtRed * w3 + SrcLine2[t3 + 1].rgbtRed * w4) shr 16;
  55.         DstLine[x].rgbtGreen := (SrcLine1[t3].rgbtGreen * w1 + SrcLine1[t3 + 1].rgbtGreen * w2 +
  56.  
  57.           SrcLine2[t3].rgbtGreen * w3 + SrcLine2[t3 + 1].rgbtGreen * w4) shr 16;
  58.         DstLine[x].rgbtBlue := (SrcLine1[t3].rgbtBlue * w1 + SrcLine1[t3 + 1].rgbtBlue * w2 +
  59.           SrcLine2[t3].rgbtBlue * w3 + SrcLine2[t3 + 1].rgbtBlue * w4) shr 16;
  60.         Inc(xP, xP2);
  61.       end; { for }
  62.       Inc(yP, yP2);
  63.       DstLine := pRGBArray(Integer(DstLine) + DstGap);
  64.     end; { for }
  65.   end; { if }
  66. end; { SmoothResize }
  67.  
  68. procedure DrawScaledJPGImage(Source: TStream; Canvas: TCanvas; x, y, MaxWidth: Integer);
  69. var
  70.   OldBitmap: TBitmap;
  71.   NewBitmap: TBitmap;
  72.   aWidth: Integer;
  73.   JPG: TJpegImage;
  74. begin
  75.   JPG := TJpegImage.Create;
  76.   try
  77.     Source.Position := 0;
  78.     JPG.LoadFromStream(Source);
  79.     OldBitmap := TBitmap.Create;
  80.     try
  81.       OldBitmap.Assign(JPG);
  82.       aWidth := OldBitmap.Width;
  83.       if (OldBitmap.Width > MaxWidth) then
  84.       begin
  85.         aWidth := MaxWidth;
  86.         NewBitmap := TBitmap.Create;
  87.         try
  88.           NewBitmap.Width := MaxWidth;
  89.           NewBitmap.Height := MulDiv(MaxWidth, OldBitmap.Height, OldBitmap.Width);
  90.           SmoothResize(OldBitmap, NewBitmap);
  91.           if NewBitmap.Height < 120 then
  92.             Canvas.Draw(x, y + ((Canvas.ClipRect.Bottom - NewBitmap.Height) div 2), NewBitmap)
  93.           else
  94.             Canvas.Draw(x, y, NewBitmap);
  95.         finally
  96.           NewBitmap.Free;
  97.         end;
  98.       end;
  99.     finally
  100.       OldBitmap.Free;
  101.     end;
  102.   finally
  103.     JPG.Free;
  104.   end;
  105. end;
  106.  
  107. procedure CreateThumbnail(ImageFileName: string; Stream: TMemoryStream);
  108. var
  109.   JPGStream: TMemoryStream;
  110.   Thumbnail: TBitmap;
  111.   JPG: TJpegImage;
  112. begin
  113.   JPGStream := TMemoryStream.Create;
  114.   try
  115.     JPGStream.LoadFromFile(ImageFileName);
  116.     JPGStream.Position := 0;
  117.     Thumbnail := TBitmap.Create;
  118.     try
  119.       Thumbnail.Width := 120;
  120.       Thumbnail.Height := 120;
  121.       DrawScaledJPGImage(JPGStream, Thumbnail.Canvas, 0, 0, Thumbnail.Width);
  122.       JPG := TJpegImage.Create;
  123.       try
  124.         JPG.Assign(Thumbnail);
  125.         JPG.SaveToStream(Stream);
  126.       finally
  127.         JPG.Free;
  128.       end;
  129.     finally
  130.       Thumbnail.Free;
  131.     end;
  132.   finally
  133.     JPGStream.Free;
  134.   end;
  135. end;
Advertisement
Add Comment
Please, Sign In to add comment