1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
| // The FlipReverseRotate Library provides three functions to flip and/or
// reverse a bitmap. You can choose which approach you'd like to use from the
// three methods to flip or reverse a bitmap: ScanLine, CopyRect, StretchBlt.
//
// A "Flip" operation takes the top of an image to the bottom and the bottom of
// the image to the top. It is a reflection along a horizontal line in the
// middle of an image.
//
// A "Reverse" operation takes the left of an image to the right and the right
// of the image to the left. It is a reflection along a vertical line in the
// middle of an image.
//
// Any Flip/Reverse operation is commutative, i.e., the flip and reverse can
// be performed in any order to get the same result. A flip followed by a
// reverse is the same as a reverse followed by a flip.
//
// A "rotate" operation spins an image 0, 90, 180 or 270 degrees around an
// axis in the center of the image.
//
// A flip/reverse operation along with a rotation is not commutative in general.
// A flip followed by a rotation will not always result in the same image as a
// rotation followed by a flip. The rotation here ALWAYS follows any flip and/or
// reversal.
//
// The examples here are intended for use with bitmaps that have 24 bits/pixel.
// Palettes may be lost on 256-color bitmaps.
//
// Copyright (C) 1998, Earl F. Glynn. All Rights Reserved.
// May be used freely for non-comercial use.
UNIT FlipReverseRotateLibrary;
{.$MODE Delphi}
{$mode objfpc}{$H+}
INTERFACE
USES
Dialogs,
LCLIntf, LCLType,// LMessages, // TRGBTriple (put here to avoid TBitmap conflict in Implementation)
Graphics; // TBitmap
// Flip/Reverse functions by Method
FUNCTION FlipReverseScanLine(CONST Flip, Reverse: BOOLEAN;
CONST Bitmap: TBitmap): TBitmap;
FUNCTION FlipReverseCopyRect(CONST Flip, Reverse: BOOLEAN;
CONST Bitmap: TBitmap): TBitmap;
FUNCTION FlipReverseStretchBlt(CONST Flip, Reverse: BOOLEAN;
CONST Bitmap: TBitmap): TBitmap;
// The Rotation function is only for the Scanline Method.
// Note: Windows NT supports a "plgblt" API call that can be used to rotate
// images.
FUNCTION RotateScanline90(CONST angle: INTEGER;
CONST Bitmap: TBitmap): TBitmap;
IMPLEMENTATION
USES
Classes, // Rect
SysUtils; // Exception
CONST
MaxPixelCount = 65536; // or some other arbitrarily large value
// ouais, parce que ça fait un peu petit, ça : 256 x 256 seulement...
TYPE
EBitmapError = CLASS(Exception);
// TRGBArray = ARRAY[0..MaxPixelCount-1] OF TRGBTriple;
// pRGBArray = ^TRGBArray;
TRGBArray = ARRAY[0..MaxPixelCount-1] OF TRGBQuad;
pRGBArray = ^TRGBArray;
//////////////////////////////////////////////////////////////////////////////
FUNCTION FlipReverseScanLine(CONST Flip, Reverse: BOOLEAN;
CONST Bitmap: TBitmap): TBitmap;
VAR
i : INTEGER;
j : INTEGER;
//RowIn : pRGBArray;
//RowOut: pRGBArray;
RowIn : pRGBTriple;
RowOut: pRGBTriple;
BEGIN
IF Bitmap.PixelFormat <> pf24bit
THEN RAISE EBitmapError.Create('Can Flip/Reverse only 24-bit bitmap');
RESULT := TBitmap.Create;
RESULT.PixelFormat := Bitmap.PixelFormat;
RESULT.Width := Bitmap.Width;
RESULT.Height := Bitmap.Height;
RESULT.BeginUpdate();
FOR j := 0 TO Bitmap.Height-1 DO BEGIN
RowIn := pRGBTriple(Bitmap.RawImage.GetLineStart(j));
IF Flip
// THEN RowOut := RESULT.Scanline[Bitmap.Height - 1 - j]
// ELSE RowOut := RESULT.Scanline[j];
THEN RowOut := pRGBTriple(RESULT.RawImage.GetLineStart(Bitmap.Height - 1 - j))
ELSE RowOut := pRGBTriple(RESULT.RawImage.GetLineStart(j));
// Optimization technique: Use two FOR loops so IF is outside of inner loop
IF Reverse
THEN BEGIN
FOR i := 0 TO Bitmap.Width-1 DO begin
RowOut[i] := RowIn[Bitmap.Width-1-i] // 2 chapeaux si pas mode Delphi ! !
{ RowOut[i].rgbtBlue := RowIn[Bitmap.Width-1-i].rgbtBlue;
RowOut[i].rgbtGreen := RowIn[Bitmap.Width-1-i].rgbtGreen;
RowOut[i].rgbtRed := RowIn[Bitmap.Width-1-i].rgbtRed; }
// RowOut[i].rgbReserved := 255;
end;
END
ELSE BEGIN
FOR i := 0 TO Bitmap.Width-1 DO begin
RowOut[i] := RowIn[i] // 2 chapeaux si pas mode Delphi ! !
{ RowOut[i].rgbtBlue := RowIn[i].rgbtBlue;
RowOut[i].rgbtGreen := RowIn[i].rgbtGreen;
RowOut[i].rgbtRed := RowIn[i].rgbtRed; }
// RowOut[i].rgbReserved := 255;
end;
END;
END;
RESULT.EndUpdate();
END {FlipReverseScanLine};
//////////////////////////////////////////////////////////////////////////////
// This function implements a suggestion by David Ullrich in a July 25, 1997
// post to comp.lang.pascal.delphi.misc.
//
// The Graphics.PAS unit shows that CopyRect calls the Windows StretchBlt API
// function.
FUNCTION FlipReverseCopyRect(CONST Flip, Reverse: BOOLEAN;
CONST Bitmap: TBitmap): TBitmap;
VAR
Bottom: INTEGER;
Left : INTEGER;
Right : INTEGER;
Top : INTEGER;
BEGIN
RESULT := TBitmap.Create;
RESULT.PixelFormat := Bitmap.PixelFormat;
RESULT.Width := Bitmap.Width;
RESULT.Height := Bitmap.Height;
// Flip Top to Bottom
IF Flip
THEN BEGIN
// Unclear why extra "-1" is needed here.
Top := Bitmap.Height-1;
Bottom := -1
END
ELSE BEGIN
Top := 0;
Bottom := Bitmap.Height
END;
// Reverse Left to Right
IF Reverse
THEN BEGIN
// Unclear why extra "-1" is needed here.
Left := Bitmap.Width-1;
Right := -1;
END
ELSE BEGIN
Left := 0;
Right := Bitmap.Width;
END;
RESULT.Canvas.CopyRect(Rect(Left,Top, Right,Bottom),
Bitmap.Canvas,
Rect(0,0, Bitmap.Width,Bitmap.Height));
END {FlipReverseCopyRect};
//////////////////////////////////////////////////////////////////////////////
FUNCTION FlipReverseStretchBlt(CONST Flip, Reverse: BOOLEAN;
CONST Bitmap: TBitmap): TBitmap;
VAR
Bottom: INTEGER;
Left : INTEGER;
Right : INTEGER;
Top : INTEGER;
BEGIN
RESULT := TBitmap.Create;
RESULT.PixelFormat := Bitmap.PixelFormat;
RESULT.Width := Bitmap.Width;
RESULT.Height := Bitmap.Height;
// Flip Top to Bottom
IF Flip
THEN BEGIN
// Unclear why extra "-1" is needed here.
Top := Bitmap.Height-1;
Bottom := -1
END
ELSE BEGIN
Top := 0;
Bottom := Bitmap.Height
END;
// Reverse Left to Right
IF Reverse
THEN BEGIN
// Unclear why extra "-1" is needed here.
Left := Bitmap.Width-1;
Right := -1;
END
ELSE BEGIN
Left := 0;
Right := Bitmap.Width;
END;
StretchBlt(RESULT.Canvas.Handle, Left, Top, Right-Left, Bottom-Top,
Bitmap.Canvas.Handle,
0,0, Bitmap.Width, Bitmap.Height, cmSrcCopy);
END {FlipReverseStretchBlt};
//////////////////////////////////////////////////////////////////////////////
// Rotate 24-bits/pixel Bitmap any multiple of 90 degrees.
FUNCTION RotateScanLine90(CONST angle: INTEGER;
CONST Bitmap: TBitmap): TBitmap;
// These four internal functions parallel the four cases in rotating a
// bitmap using the Pixels property. See the RotatePixels example on
// the Image Processing page of efg's Computer Lab for an example of the
// use of the Pixels property (which is very slow).
// A Bitmap.Assign could be used for a simple copy. A complete example
// using ScanLine is included here to help explain the other three cases.
FUNCTION SimpleCopy: TBitmap;
VAR
//i : INTEGER;
//j : INTEGER;
//rowIn : pRGBQuad;//pRGBArray;
//rowOut: pRGBQuad;//pRGBArray;
h,w: Integer;
//cnt: single;
//idx: Integer;
//pbS,pbD: pByte;
pbS: pRGBTriple;
pbD: pRGBQuad;
BEGIN
RESULT := TBitmap.Create;
RESULT.PixelFormat := Bitmap.PixelFormat; // only pf24bit for now
RESULT.Width := Bitmap.Width;
RESULT.Height := Bitmap.Height;
RESULT.BeginUpdate();
// Out[i, j] = In[i, j]
{ FOR j := 0 TO Bitmap.Height - 1 DO BEGIN
rowIn := pRGBQuad(Bitmap.RawImage.GetLineStart(j));
rowOut := pRGBQuad(RESULT.RawImage.GetLineStart(j));
// Could optimize the following by using a function like CopyMemory
// from the Windows unit.
FOR i := 0 TO Bitmap.Width - 1 DO BEGIN
// RowOut[i] := RowIn[i];
// Why does this crash with RowOut[i] := RowIn[i]? Alignment?
// Use this longer form as workaround.
WITH rowOut[i] DO BEGIN
// rgbtRed := rowIn[i].rgbtRed;
// rgbtGreen := rowIn[i].rgbtGreen;
// rgbtBlue := rowIn[i].rgbtBlue;
rgbRed := rowIn[i].rgbRed;
rgbGreen := rowIn[i].rgbGreen;
rgbBlue := rowIn[i].rgbBlue;
rgbReserved := rowIn[i].rgbReserved;
END
END
END; }
{ for h := 0 to Bitmap.Height-1 do begin
pbS := Bitmap.RawImage.GetLineStart(h);
pbD := RESULT.RawImage.GetLineStart(h);
cnt := 0; idx := 0;
for w := 0 to (Bitmap.Width * 3)-1 do begin
pbD[idx] := pbS[w];
cnt := w+1; // pour chercher le 4e byte de la destination
if ( Frac(cnt / 3) = 0 ) then begin
// "bourrer" le 4e byte, le "Reserved", qui a l'air inclus par le widgetset gtk2
inc(idx);
pbD[idx]:=255;
end;
inc(idx);
end;
end; }
for h := 0 to Bitmap.Height-1 do begin
pbS := pRGBTriple(Bitmap.RawImage.GetLineStart(h));
pbD := pRGBQuad(RESULT.RawImage.GetLineStart(h));
for w := 0 to Bitmap.Width-1 do begin
pbD[w].rgbBlue := pbS[w].rgbtBlue;
pbD[w].rgbGreen := pbS[w].rgbtGreen;
pbD[w].rgbRed := pbS[w].rgbtRed;
pbD[w].rgbReserved := 255;
end;
end;
RESULT.EndUpdate();
END {SimpleCopy};
FUNCTION Rotate90DegreesCounterClockwise: TBitmap;
VAR
i : INTEGER;
j : INTEGER;
rowIn : pRGBArray;
h,w: Integer;
cnt: single;
idx: Integer;
pbS,pbD: pByte;
BEGIN
RESULT := TBitmap.Create;
RESULT.PixelFormat := Bitmap.PixelFormat; // only pf24bit for now
RESULT.Width := Bitmap.Height;
RESULT.Height := Bitmap.Width;
RESULT.BeginUpdate();
// Out[j, Right - i - 1] = In[i, j]
FOR j := 0 TO Bitmap.Height - 1 DO BEGIN
// rowIn := Bitmap.ScanLine[j];
rowIn := pRGBArray(Bitmap.RawImage.GetLineStart(j));
FOR i := 0 TO Bitmap.Width - 1 DO
pRGBArray(RESULT.ScanLine[Bitmap.Width - i - 1])[j] := rowIn[i]
END;
RESULT.EndUpdate();
END {Rotate90DegreesCounterClockwise};
// Could use Rotate90DegreesCounterClockwise twice to get a
// Rotate180DegreesCounterClockwise. Rotating 180 degrees is the same
// as a Flip and Reverse
FUNCTION Rotate180DegreesCounterClockwise: TBitmap;
VAR
i : INTEGER;
j : INTEGER;
rowIn : pRGBArray;
rowOut: pRGBArray;
BEGIN
RESULT := TBitmap.Create;
RESULT.PixelFormat := Bitmap.PixelFormat; // only pf24bit for now
RESULT.Width := Bitmap.Width;
RESULT.Height := Bitmap.Height;
RESULT.BeginUpdate();
// Out[Right - i - 1, Bottom - j - 1] = In[i, j]
FOR j := 0 TO Bitmap.Height - 1 DO BEGIN
// rowIn := Bitmap.ScanLine[j];
// rowOut := RESULT.ScanLine[Bitmap.Height - j - 1];
rowIn := pRGBArray(Bitmap.RawImage.GetLineStart(j));
rowOut := pRGBArray(RESULT.RawImage.GetLineStart(Bitmap.Height - j - 1));
FOR i := 0 TO Bitmap.Width - 1 DO
rowOut[Bitmap.Width - i - 1] := rowIn[i]
END;
RESULT.EndUpdate();
END {Rotate180DegreesCounterClockwise};
// Could use Rotate90DegreesCounterClockwise three times to get a
// Rotate270DegreesCounterClockwise
FUNCTION Rotate270DegreesCounterClockwise: TBitmap;
VAR
i : INTEGER;
j : INTEGER;
rowIn: pRGBArray;
BEGIN
RESULT := TBitmap.Create;
RESULT.PixelFormat := pf32bit;//Bitmap.PixelFormat; // only pf24bit for now
RESULT.Width := Bitmap.Height;
RESULT.Height := Bitmap.Width;
RESULT.BeginUpdate();
// Out[Bottom - j - 1, i] = In[i, j]
FOR j := 0 TO Bitmap.Height - 1 DO BEGIN
// rowIn := Bitmap.ScanLine[j];
rowIn := pRGBArray(Bitmap.RawImage.GetLineStart(j));
FOR i := 0 TO Bitmap.Width - 1 DO
// pRGBArray(RESULT.Scanline[i])[Bitmap.Height - j - 1] := rowIn[i]
pRGBArray(RESULT.RawImage.GetLineStart(i))[Bitmap.Height - j - 1] := rowIn[i]
END;
RESULT.EndUpdate();
END {Rotate270DegreesCounterClockwise};
BEGIN
// IF Bitmap.PixelFormat <> pf24bit
// THEN RAISE EBitmapError.Create('Can Rotate90 only 24-bit bitmap');
IF (angle >= 0) AND (angle MOD 90 <> 0)
THEN RAISE EBitmapError.Create('Rotate90: Angle not positive multiple of 90 degrees');
CASE (angle DIV 90) MOD 4 OF
0: RESULT := SimpleCopy;
1: RESULT := Rotate90DegreesCounterClockwise; // Anticlockwise for the Brits
2: RESULT := Rotate180DegreesCounterClockwise;
3: RESULT := Rotate270DegreesCounterClockwise
ELSE
RESULT := NIL // avoid compiler warning
END;
END {RotateScanline90};
END. |