Mailing lists / fpc-pascal / 2017-01
Drawing bitmap by pixel
6 messages · 3 people · –
- RRyan Joseph 3
- GGraeme Geldenhuys 2
- FFelipe Monteiro de Carvalho 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Ryan Joseph wroteI 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.
Ryan Joseph wroteIs 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
Show quoted text 5 lines
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
Show quoted text 5 lines
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
Ryan Joseph wroteThe 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
