Solved

Reduction of JPeg Bitmap

Posted on 2004-04-09
4
285 Views
Last Modified: 2013-12-03
How do I propotionally reduce a the width of a jpeg fiele from 2240 pixels to 120 pixels
0
Comment
Question by:Vector7
  • 2
4 Comments
 
LVL 11

Assisted Solution

by:shaneholmes
shaneholmes earned 125 total points
ID: 10792120
Try this:

Procedure ScaleDownJPG(Fname:string;newheight,newWidth:integer) ;
var JPG1,JPG2:TJpegImage;
    BMP:Tbitmap;
Begin
  try
     JPG1:=TJpegimage.create;
     JPG2:=TJpegimage.create;
     BMP:=Tbitmap.create;
     Jpg1.LoadFromFile(Fname);
     With BMP do
     begin
       height:=newheight;
       Width:=newWidth;
       canvas.stretchdraw(canvas.cliprect,jpg1);
     end;
     bmp.savetofile('Z:\test.bmp');
     JPG2.assign(BMP);
     DeleteFile(fname);
     Jpg2.savetofile(fname);
   finally
     Jpg1.free;
     Jpg2.free;
     BMP.free;
   end;
end;

Shane
0
 

Author Comment

by:Vector7
ID: 10793629
Thanks Shane.  I've tried this and it works - many thanks.  However, how do you preserve a degree of quality? Stephen
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 125 total points
ID: 10794581
I found this on Google Groups: Give it a try!

Shane


I do not really understand what you are getting at, but I assmue the
problem is that you are changing the Size of the Timage, assuming that
will rescale the image?
Well resizing a TImage will make the image look rescaled, but it does
NOT rescale the underlining bitmap that is stored in the TImage.

Also stretchdraw does not give as agood a resized quality (i.e. does no
resampling).

Try the following SmoothResize code.
It requires 24-bit bitmaps and D3 or higher.  
Call it with something like:

  SmoothResize(Image1.Picture.Bitmap,NewImage1Width,NewImageHeight);
  Image1.Refresh;

type
  TRGBArray = ARRAY[0..32767] OF TRGBTriple;
  pRGBArray = ^TRGBArray;

procedure SmoothResize(abmp:TBitmap; NuWidth,NuHeight:integer);
var
   weight_x, weight_y     : array[0..1] of Single;
   ifrom_y, ifrom_x       : Integer;
   to_y, to_x             : Integer;
   new_red, new_green     : Integer;
   new_blue               : Integer;
   ix, iy                 : Integer;
   xscale, yscale         : Single;
   sfrom_y, sfrom_x       : Single;
   weight                 : Single;
   total_red, total_green : Single;
   total_blue             : Single;
   bTmp                   : TBitmap;
   sli, slo               : pRGBArray;
   // pointers for scanline access
   liPByte, loPByte, p    : PByte;
   // offset increment
   liSize, loSize         : integer;
begin
   abmp.PixelFormat := pf24bit;
   bTmp := TBitmap.Create;
   bTmp.PixelFormat := pf24bit;
   bTmp.Width := NuWidth;
   bTmp.Height := NuHeight;
   xscale := bTmp.Width / (abmp.Width-1);
   yscale := bTmp.Height / (abmp.Height-1);
   liPByte := abmp.Scanline[0];
   liSize := integer(abmp.Scanline[1]) -integer(liPByte);
   loPByte := bTmp.Scanline[0];
   loSize := integer(bTmp.Scanline[1]) -integer(loPByte);
   for to_y := 0 to bTmp.Height-1 do begin
     sfrom_y := to_y / yscale;
     ifrom_y := Trunc(sfrom_y);
     weight_y[1] := sfrom_y - ifrom_y;
     weight_y[0] := 1 - weight_y[1];
     for to_x := 0 to bTmp.Width-1 do begin
       sfrom_x := to_x / xscale;
       ifrom_x := Trunc(sfrom_x);
       weight_x[1] := sfrom_x - ifrom_x;
       weight_x[0] := 1 - weight_x[1];
       total_red   := 0.0;
       total_green := 0.0;
       total_blue  := 0.0;
       for ix := 0 to 1 do begin
         for iy := 0 to 1 do begin
           p := liPByte;
           Inc(p, liSize *(ifrom_y + iy));
           sli := pRGBArray(p);
           new_red := sli[ifrom_x + ix].rgbtRed;
           new_green := sli[ifrom_x + ix].rgbtGreen;
           new_blue := sli[ifrom_x + ix].rgbtBlue;
           weight := weight_x[ix] * weight_y[iy];
           total_red   := total_red   + new_red   * weight;
           total_green := total_green + new_green * weight;
           total_blue  := total_blue  + new_blue  * weight;
         end;
       end;
       p := loPByte;
       Inc(p, loSize *to_y);
       slo := pRGBArray(p);
       slo[to_x].rgbtRed := Round(total_red);
       slo[to_x].rgbtGreen := Round(total_green);
       slo[to_x].rgbtBlue := Round(total_blue);
     end;
   end;
   abmp.Width := bTmp.Width;
   abmp.Height := bTmp.Height;
   abmp.Canvas.Draw(0,0,bTmp);
   bTmp.Free;
end;

--
Charles Hacker
Lecturer in Electronics and Computing
School of Engineering
Griffith University - Gold Coast
Australia
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 122
True Type Font Files? 4 82
LAN or WAN ? 11 81
SUM 2 INTEGER ARRAYS INTO 1 10 93
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now