Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-01

Drawing bitmap by pixel

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Felipe Monteiro de Carvalho
to Ryan Joseph
Ryan Joseph wrote
I used the image canvas before to draw bitmap fonts so I have this code working. Is there anyway to set a pixel using TFPImageCanvas?

Use the Colors property, it is fast:

property Colors [x,y:integer] : TFPColor read GetColor write SetColor;
canvas.Brush.FPColor := colRed; // how do I set the color as an RGB?

Use this function:

function FPColor (r,g,b,a:word) : TFPColor;

fcl-image is from Free Pascal, not from Lazarus, but in lcl/lazcanvas.pas there is TLazCanvas which extends TFPImageCanvas adding convenience functions for example for alpha blending among other stuff.

Felipe Monteiro de Carvalho

Reply by email

Graeme Geldenhuys
to Ryan Joseph:
Ryan Joseph wrote
Is there anyway to set a pixel using TFPImageCanvas?

It is slightly confusing. If you are using an Indexed image, then use the Pixels[] property. If you are not using an Indexed image, then use the Colors[] property.

Attached is an example program that generates a Minecraft like grass block (texture) and then outputs it to a PNG file. I hope the example helps.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Ryan Joseph
to Graeme Geldenhuys
Show quoted text 5 lines
On Jan 20, 2017, at 7:20 AM, Graeme Geldenhuys wrote: It is slightly confusing. If you are using an Indexed image, then use the Pixels[] property. If you are not using an Indexed image, then use the Colors[] property.

Thanks guys,

Here’s the new program based on your example but it still just outputs a pure black image.

procedure Draw;
var
	image: TFPCustomImage;
	canvas: TFPImageCanvas;
	writer: TFPWriterPNG;
	x, y: integer;
begin
	image := TFPCompactImgRGB8Bit.Create(100, 100);
	image.UsePalette := False;
	for x := 0 to image.Width-1 do
	for y := 0 to image.Height-1 do
		image.Colors[x, y] := FPColor(255, 0, 0, 255);
	writer := TFPWriterPNG.Create;
	writer.Indexed := False;
  	image.SaveToFile('bitmap.png', writer);
end;

Regards,
Ryan Joseph

Reply by email

Ryan Joseph
to Ryan Joseph
Show quoted text 5 lines
On Jan 20, 2017, at 8:31 AM, Ryan Joseph wrote: Thanks guys, Here’s the new program based on your example but it still just outputs a pure black image.

never mind! The max color value is 65535. I thought it was 1.0 or 255. Got it working now. Thanks again.

Regards,
Ryan Joseph

Reply by email

Graeme Geldenhuys
to Ryan Joseph
Ryan Joseph wrote
The max color value is 65535. I thought it was 1.0 or 255.

The FPImage implementation supports 16bit color channels (a bit of future proofing that confuses many). Most programs and images use 8bits per channel. Each color channel is thus a WORD size, not a BYTE size.

That is why my example program does the following:

      // duplicate the data to fill a WORD size channel
      r := (r shl 8) or r;
      g := (g shl 8) or g;
      b := (b shl 8) or b;

eg: if r was initial $00AC (by using Random($FF) to populate it), I then duplicate the initial value to fill the WORD size value, and the above code will make r = $ACAC. Strictly not 100% correct thing to do, but seems to work with FPImage.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email