From: Earl F. Glynn To: Lawrence Schlachter Subject: Re: Computer Lab Feedback Date: Thursday, September 30, 1999 11:36 AM Lawrence: >Do you have a snippet of Code that could help or any comments in this regard? Here's some code for you. I assumed a shrink "factor" that can be any integer > 0 (and less than bitmap height/width). Given "input" bitmap height and width, the "output" bitmap will have dimensions height DIV factor by width DIV factor. You only need to process the pixels of this output bitmap by fetching the correct pixels from the input bitmap. I defined "flip" to be "top to bottom" and "reverse" to be "left to right." So for a given output row y, the input row is factor*y for the "normal" case and height-1-factor*y for the "flipped" case. Stacking the IF/THEN/ELSE statement, like shown below, may be unconventional but shows the logic much more clearly (IMHO). Likewise for a given output column x, the needed input column is factor*x for the "normal" case and width-1-factor*x for the "reversed" case. I hope you can figure this out. If not, feel free to ask any followup questions. unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Button1: TButton; CheckBoxFlip: TCheckBox; Image1: TImage; EditFilename: TEdit; EditFactor: TEdit; CheckBoxReverse: TCheckBox; LabelShrinkFactor: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} // Flip/Reverse/Shrink bitmap // Shrink by resampling by specified factor in both dimensions FUNCTION FlipReverse(CONST FName: STRING; CONST FlipIt: BOOLEAN; CONST ReverseIt: BOOLEAN; CONST Factor: INTEGER): TBitmap; TYPE TRGBTripleArray = ARRAY[WORD] OF TRGBTriple; pRGBTripleArray = ^TRGBTripleArray; VAR Bitmap : TBitmap; ColumnIn: INTEGER; rowIn : pRGBTripleArray; RowOut : pRGBTripleArray; x : INTEGER; y : INTEGER; BEGIN ASSERT (Factor > 0); BitMap := TBitMap.Create; Result := TBitMap.Create; try BitMap.LoadfromFile(FName); // TRGBTripleArray Scanline is only good for pf24bit bitmap, // so force this if necessary IF Bitmap.PixelFormat <> pf24bit THEN Bitmap.PixelFormat := pf24bit; // "Output" bitmap is same size as "Input" only if Factor = 1 Result.Height := BitMap.Height DIV Factor; Result.Width := BitMap.Width DIV Factor; Result.PixelFormat := BitMap.PixelFormat; // pf24bit // Only process "output" pixels and "fetch" pixels from input bitmap // Define flip to be "top to bottom" and reverse to be "left to right" for y := 0 TO (BitMap.Height DIV Factor)-1 do begin rowOut := Result.Scanline[y]; if FlipIt then rowIn := Bitmap.Scanline[Bitmap.Height - 1 - Factor*y] else rowIn := Bitmap.Scanline[Factor*y]; for x := 0 TO (BitMap.Width DIV Factor)-1 do begin if ReverseIt then ColumnIn := Bitmap.Width - 1 - Factor*x else ColumnIn := Factor*x; with rowOut[x] do begin rgbtRed := rowIn[ColumnIn].rgbtRed; rgbtGreen := rowIn[ColumnIn].rgbtGreen; rgbtBlue := rowIn[ColumnIn].rgbtBlue; end end end finally BitMap.free; END // Don't free result. Calling program will be responsible for that. END; procedure TForm1.Button1Click(Sender: TObject); VAR Bitmap: TBitmap; begin Bitmap := FlipReverse(EditFilename.Text, CheckBoxFlip.Checked, CheckBoxReverse.Checked, StrToInt(EditFactor.Text)); TRY // Display on screen Image1.Picture.Graphic := Bitmap; FINALLY Bitmap.Free END end; end. Best wishes, ___ efg Earl F. Glynn Overland Park, KS USA efg's Computer Lab: http://www.efg2.com/Lab