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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
| //////////////////////// UNIT1.LFM
object Form1: TForm1
Left = 401
Height = 770
Top = 132
Width = 330
Caption = 'Form1'
ClientHeight = 770
ClientWidth = 330
OnCreate = FormCreate
LCLVersion = '1.4.0.4'
object trkTranspar: TTrackBar
Left = 288
Height = 238
Top = 8
Width = 36
Max = 255
Orientation = trVertical
PageSize = 15
Position = 127
Reversed = True
TabOrder = 0
end
object gbxTBitmap: TGroupBox
Left = 10
Height = 140
Top = 10
Width = 185
Caption = 'TBitmap'
ClientHeight = 123
ClientWidth = 181
TabOrder = 1
object btnTBitmap: TButton
Left = 40
Height = 25
Top = 8
Width = 81
Caption = 'Button'
OnClick = btnTBitmapClick
TabOrder = 0
end
object imgTBitmap: TImage
Left = 16
Height = 69
Top = 40
Width = 137
end
end
object gbxTLazIntfImage: TGroupBox
Left = 10
Height = 140
Top = 160
Width = 185
Caption = 'TLazIntfImage'
ClientHeight = 123
ClientWidth = 181
TabOrder = 2
object btnTLazIntfImage: TButton
Left = 40
Height = 25
Top = 8
Width = 81
Caption = 'Button'
OnClick = btnTLazIntfImageClick
TabOrder = 0
end
object imgTLazIntfImage: TImage
Left = 16
Height = 69
Top = 40
Width = 137
end
end
object gbxTbzBMPViewer: TGroupBox
Left = 10
Height = 140
Top = 310
Width = 185
Caption = 'TbzBMPViewer'
ClientHeight = 123
ClientWidth = 181
TabOrder = 3
object btnTbzBMPViewer: TButton
Left = 40
Height = 25
Top = 8
Width = 81
Caption = 'Button'
OnClick = btnTbzBMPViewerClick
TabOrder = 0
end
object pnlTbzBMPViewer: TPanel
Left = 16
Height = 69
Top = 40
Width = 137
BevelOuter = bvNone
Caption = 'pnlTbzBMPViewer'
ClientHeight = 69
ClientWidth = 137
TabOrder = 1
object imgTbzBMPViewer: TImage
Left = 0
Height = 69
Top = 0
Width = 137
Align = alClient
end
end
end
object gbxTbzFastBitmap: TGroupBox
Left = 10
Height = 140
Top = 460
Width = 185
Caption = 'TbzFastBitmap'
ClientHeight = 123
ClientWidth = 181
TabOrder = 4
object btnTbzFastBitmap: TButton
Left = 40
Height = 25
Top = 8
Width = 81
Caption = 'Button'
OnClick = btnTbzFastBitmapClick
TabOrder = 0
end
object imgTbzFastBitmap: TImage
Left = 16
Height = 69
Top = 40
Width = 137
end
end
object gbxTMitchell: TGroupBox
Left = 10
Height = 140
Top = 610
Width = 185
Caption = 'TMitchell'
ClientHeight = 123
ClientWidth = 181
TabOrder = 5
object btnTMitchell: TButton
Left = 40
Height = 25
Top = 8
Width = 81
Caption = 'Button'
OnClick = btnTMitchellClick
TabOrder = 0
end
object imgTMitchell: TImage
Left = 16
Height = 69
Top = 40
Width = 137
end
end
end
//////////////////////// UNIT1.PAS
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
LCLintf, LCLtype,
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
FPImage, GraphType, IntfGraphics,
uBMPViewer, uFastBitmap,
u_mitchell,
ExtCtrls, ComCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
btnTBitmap: TButton;
btnTbzFastBitmap: TButton;
btnTMitchell: TButton;
btnTLazIntfImage: TButton;
btnTbzBMPViewer: TButton;
gbxTBitmap: TGroupBox;
gbxTbzFastBitmap: TGroupBox;
gbxTMitchell: TGroupBox;
gbxTLazIntfImage: TGroupBox;
gbxTbzBMPViewer: TGroupBox;
imgTBitmap: TImage;
imgTbzBMPViewer: TImage;
imgTbzFastBitmap: TImage;
imgTMitchell: TImage;
imgTLazIntfImage: TImage;
pnlTbzBMPViewer: TPanel;
trkTranspar: TTrackBar;
procedure btnTBitmapClick(Sender: TObject);
procedure btnTbzBMPViewerClick(Sender: TObject);
procedure btnTbzFastBitmapClick(Sender: TObject);
procedure btnTLazIntfImageClick(Sender: TObject);
procedure btnTMitchellClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ private declarations }
HSIZE,WSIZE,aWidth: integer;
ratio: single;
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function its(i:integer):string; begin Result:=IntToStr(i); end;
function BytesPerPixel(APixelFormat: TPixelFormat): integer;
begin
Result := -1;
case APixelFormat of
pf8bit : result := 1;
pf16bit: result := 2;
pf24bit: result := 3;
pf32bit: result := 4;
end;
end;
////////// les boutons ///////////////
procedure TForm1.btnTBitmapClick(Sender: TObject);
var
aBmp: TBitmap;
procedure bmpComputeArray;
var
h, w: integer;
gray: integer;
Pixels: pRGBQuad;
FUNCTION RGBAtoBGRA(CONST red, green, blue, reserved: BYTE): TRGBQuad;
BEGIN
WITH RESULT DO
BEGIN
rgbRed := red;
rgbGreen := green;
rgbBlue := blue;
rgbReserved := reserved;
END
END {RGBQuad};
begin
aBmp := TBitmap.Create;
with aBmp do begin
PixelFormat := pf32bit;
Width := aWidth;
Height := HSIZE;
BeginUpdate();
for h := 0 to HSIZE-1 do
begin
Pixels := pRGBQuad(RawImage.GetLineStart(h));
if h = 0 then // ligne du haut
for w := 0 to aWidth-1 do
Pixels[w] := RGBAtoBGRA(0,0,255,255)
else
if h = HSIZE-1 then // ligne du bas
for w := 0 to aWidth-1 do
Pixels[w] := RGBAtoBGRA(0,255,255,255)
else // entre les deux
for w := 0 to aWidth-1 do begin
gray := round((h+w) * ratio); // le dégradé gris
if gray > 255 then gray := round(gray * ratio);
Pixels[w] := RGBAtoBGRA(gray, gray, gray, trkTranspar.Position);
end;
end;
EndUpdate();
Width := WSIZE; // traits verticaux si décommenté donc TBitmap inutilisable
end;
end;
begin
bmpComputeArray;
imgTBitmap.Picture.Bitmap.Assign(aBmp);
btnTBitmap.Caption := its(BytesPerPixel(imgTBitmap.Picture.Bitmap.PixelFormat)*8);
aBmp.Free;
end;
procedure TForm1.btnTLazIntfImageClick(Sender: TObject);
var
aBitmap: TBitmap;
IntfImage: TLazIntfImage;
procedure lazComputeArray;
var
h, w: integer;
gray: integer;
row : pRGBQuad;
begin
with IntfImage do begin
BeginUpdate;
for h := 0 to HSIZE-1 do begin
row := GetDataLineStart(h);
for w := 0 to aWidth-1 do begin
if h = 0 then
begin // ligne du haut
row[w].rgbRed := 0; // qui est en fait le bleu
row[w].rgbGreen := 0;
row[w].rgbBlue := 255; // qui est en fait le rouge
row[w].rgbReserved := 255;
end
else
if h = HSIZE-1 then
begin // ligne du bas
row[w].rgbRed := 0;
row[w].rgbGreen := 255;
row[w].rgbBlue := 255;
row[w].rgbReserved := 255;
end
else
begin // entre les deux
gray := round((h+w) * ratio); // le dégradé gris
if gray > 255 then gray := round(gray * ratio);
row[w].rgbRed := gray;
row[w].rgbGreen := gray;
row[w].rgbBlue := gray;
row[w].rgbReserved := trkTranspar.Position;
end;
end; // for w
end; // for h
EndUpdate;
end;
end;
begin
aBitmap := TBitmap.Create;
with aBitmap do begin // 1
PixelFormat := pf32bit;
Width := aWidth;
Height := HSIZE;
end;
IntfImage := aBitmap.CreateIntfImage;// 3
lazComputeArray; // 2 -- tuto dit "work on bitmap", ici c'est "work on tlazintfimage"
aBitmap.Clear; // +/- similaire au 4
aBitmap.LoadFromIntfImage(IntfImage);// 5
IntfImage.Free;
aBitmap.Width := WSIZE; // 6
//aBitmap.SaveToFile('/chemin/aBitmap.bmp'); // 7 -- taille ok, pf32
imgTLazIntfImage.Picture.Bitmap := aBitmap; // 8
aBitmap.Free;
btnTLazIntfImage.Caption := its(BytesPerPixel(imgTLazIntfImage.Picture.Bitmap.PixelFormat)*8);
{ http://forum.lazarus.freepascal.org/index.php?topic=19704.0
I've found an ADMITTEDLY DIRTY workaround:
1) Create your Bitmap as needed, ensuring that its width IS A MULTIPLE OF 8 PIXELS.
2) Work on your bitmap as needed.
3) Obtain a TLazIntfImage from the bitmap.
4) Create another graphical object (I tried only with TPortableNetworkGraphic)
5) Load from the TLazInfImage obtained in step 3.
6) Resize the another graphical object as needed (for instance, reduce the width so that it's no longer a multiple of 8 ).
7) Save this graphic.
8) At this point, the image saved will retain its transparency, even though its width is no longer a multiple of 8 pixels.
I tried this workaround in Ubuntu, and it certainly does the trick. Yes, it's dirty, but at least I got my problem solved...}
end;
procedure TForm1.btnTbzBMPViewerClick(Sender: TObject);
var
bz: TBMPViewer;
procedure bzComputeArray;
var
h, w: integer;
gray: integer;
pixelline: pColor32;
begin
pnlTbzBMPViewer.Width := aWidth;
pnlTbzBMPViewer.Height:= HSIZE;
bz := TBMPViewer.Create(self);
with bz do begin
//Parent := Self;
Parent := pnlTbzBMPViewer; // impossible de mettre le TImage, il FAUT un WinControl
Align := alClient;
AutoStretchMode := smManual;
Center := True; // le dessin de l'image est centré dans le viewer
FastBitmap.Create(aWidth, HSIZE);
// Bitmap.PixelFormat := pf32bit; //logiquement ne devrait pas être utile
for h := 0 to HSIZE-1 do begin
pixelline := FastBitmap.GetScanLine(h);
for w := 0 to aWidth-1 do begin
if h = 0 then // ligne du haut
begin
pixelline[w].Red := 255;
pixelline[w].Green := 0;
pixelline[w].Blue := 0;
pixelline[w].Alpha := 255;
end
else
if h = HSIZE-1 then // ligne du bas
begin
pixelline[w].Red := 255;
pixelline[w].Green := 255;
pixelline[w].Blue := 0;
pixelline[w].Alpha := 255;
end
else // entre les deux
begin
gray := round((h+w) * ratio); // le dégradé gris
if gray > 255 then gray := round(gray * ratio);
pixelline[w].Red := gray;
pixelline[w].Green := gray;
pixelline[w].Blue := gray;
pixelline[w].Alpha := trkTranspar.Position;
end;
end; // for w
end; // for h
pnlTbzBMPViewer.Width := WSIZE;
Width := WSIZE; // pas redimensionnée
// FastBitmap.Width:=WSIZE;// détruit l'image...
end; // with bz
end;
begin
bzComputeArray;
//imgTbzBMPViewer.Picture.Bitmap.PixelFormat:=pf32bit; // si présent, image vide -- si commenté, pf24
bz.GetLoader.FastBitmap.Draw(imgTbzBMPViewer.Canvas, 0,0);
//bz.GetLoader.FastBitmap.Draw(pnlTbzBMPViewer.Canvas, 0,0); // img flashe et disparaît
//bz.FastBitmap.GetBitmap.SaveToFile('/chemin/bz.bmp'); // 32 bit mais pas redimensionnée
btnTbzBMPViewer.Caption := its(BytesPerPixel(imgTbzBMPViewer.Picture.Bitmap.PixelFormat)*8);
bz.Free;
end;
procedure TForm1.btnTbzFastBitmapClick(Sender: TObject);
var
bzFB: TFastBitmap;
bmp: TBitmap;
procedure bzComputeFBArray;
var
h, w: integer;
gray: integer;
//pixelline: pColor32;
pixelline: pRGBQuad;
begin
bmp := TBitmap.Create;
with bmp do begin
PixelFormat := pf32bit;
Width := aWidth;
Height:= HSize;
for h := 0 to HSIZE-1 do begin
pixelline := pRGBQuad(RawImage.GetLineStart(h));
for w := 0 to aWidth-1 do begin
if h = 0 then // ligne du haut
begin
pixelline[w].rgbRed := 0; // blue
pixelline[w].rgbGreen := 0;
pixelline[w].rgbBlue := 255;// red
pixelline[w].rgbReserved := 255;
end
else
if h = HSIZE-1 then // ligne du bas
begin
pixelline[w].rgbRed := 0;
pixelline[w].rgbGreen := 255;
pixelline[w].rgbBlue := 255;
pixelline[w].rgbReserved := 255;
end
else // entre les deux
begin
gray := round((h+w) * ratio); // le dégradé gris
if gray > 255 then gray := round(gray * ratio);
pixelline[w].rgbRed := gray;
pixelline[w].rgbGreen := gray;
pixelline[w].rgbBlue := gray;
pixelline[w].rgbReserved := trkTranspar.Position;
end;
end; // for w
end; // for h
Width := WSIZE;
end; // with bmp
end;
begin
bzFB := TFastBitmap.Create(aWidth, HSIZE);
bzComputeFBArray;
//bzFB.ImportFromRawImage(bmp.RawImage);
//bmp.SaveToFile('/chemin/testbmp1.bmp');// ce fichier EST pf32 et à la bonne taille
// ces 2 lignes dessus ou les 2 dessous fonctionnent pareil
bzFB.ImportFromBitmap(bmp);
//bmp.SaveToFile('/chemin/testbmp2.bmp');// ce fichier EST pf32 et à la bonne taille
imgTbzFastBitmap.Width := WSIZE;
bzFB.Draw(imgTbzFastBitmap.Canvas, rect(0,0,imgTbzFastBitmap.Width,imgTbzFastBitmap.Height));
btnTbzFastBitmap.Caption := its(BytesPerPixel(imgTbzFastBitmap.Picture.Bitmap.PixelFormat)*8);// 24 ! aïe !
end;
procedure TForm1.btnTMitchellClick(Sender: TObject);
var
ci: TCompactImage;
lclci: TLCLCompactImage;
procedure ciComputeArray;
var
h, w: integer;
gray: integer;
begin // pas concerné par le problème de width divisible par 8 ou pas
ci := TCompactImage.Create(WSIZE, HSIZE);
with ci do
for h := 0 to HSIZE-1 do
for w := 0 to (WSIZE)-1 do begin
if h = 0 then begin // ligne du haut
ci.Data[(h * ci.Width) + w].Red := 255;
ci.Data[(h * ci.Width) + w].Green := 0;
ci.Data[(h * ci.Width) + w].Blue := 0;
ci.Data[(h * ci.Width) + w].Alpha := 255;
end
else
if h = HSIZE-1 then begin // ligne du bas
ci.Data[(h * ci.Width) + w].Red := 255;
ci.Data[(h * ci.Width) + w].Green := 255;
ci.Data[(h * ci.Width) + w].Blue := 0;
ci.Data[(h * ci.Width) + w].Alpha := 255;
end
else begin // entre les deux
gray := round((h+w) * ratio); // le dégradé gris
if gray > 255 then gray := round(gray * ratio);
ci.Data[(h * ci.Width) + w].Red := gray;
ci.Data[(h * ci.Width) + w].Green := gray;
ci.Data[(h * ci.Width) + w].Blue := gray;
ci.Data[(h * ci.Width) + w].Alpha := trkTranspar.Position;
end;
end; // for w
lclci := TLCLCompactImage.Create(ci);
end;
begin
ciComputeArray;
imgTMitchell.Picture.Bitmap.Assign(lclci.ImageBitmap);
btnTMitchell.Caption := its(BytesPerPixel(imgTMitchell.Picture.Bitmap.PixelFormat)*8);
lclci.Free;
ci.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WSIZE := 121;
// 2 lignes pour calcul d'une width divisible par 8
aWidth:= WSIZE;
while ((aWidth mod 8) <> 0) do inc(aWidth);
Caption := its(awidth); // affiche bien 128 pour WSIZE à 121
HSIZE := 69;
ratio := 256 / (HSIZE + WSIZE); // une valeur au pif pour gérer le dégradé
end;
end. |
Partager