How to get R,G,B values of pixels in TLazIntfImage? lazarus / 39-Graphics ---------------------------------------------------------------------- domasz · 2024-05-07 08:32 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for y:=0 to Bitmap.Height-1 do    for x:=0 to Bitmap.Width-1 do begin      Col := Bitmap.Pixels[x,y];   Is this the right approach? Will Col contain always R,G,B,A values even if I load a monochrome image into TLazIntfImage? ---------------------------------------------------------------------- wp · 2024-05-07 09:26 You question is a bit unclear: The variable "Bitmap" and the usage of the Pixels property suggest that it is a TBitmap, but then the post title speaks of a TLazIntfImage... If "Bitmap" is a TLazIntfImage the Pixels property refers to the palette index in case of paletted images. Based on this, you would use this palette index in the image palette and from that you'd get the FPColor of the pixels at position X, Y: Normally, however, images are not paletted nowadays, and you would use the Colors property to get the color components Red, Green, Blue and Alpha from any pixel, even for monochrome images. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  img: TLazIntfImage;  // or any TFPCustomImage descendant  col: TFPColor;...  if img.UsePalette then    col := img.Palette[img.Palette[X, Y]]  else    col := img.Colors[x, Y]; ---------------------------------------------------------------------- TRon · 2024-05-07 09:54 Correct me in case wrong but to my knowledge it is even so that whenever there isn't a palette present the pixels property returns a (simple) zero. So indeed a bit of a confusing question perhaps caused by having mixed some things together ? ---------------------------------------------------------------------- wp · 2024-05-07 10:09 Quote from: TRon on May 07, 2024, 11:54:37 amCorrect me in case wrong but to my knowledge it is even so that whenever there isn't a palette present the pixels property returns a (simple) zero. Yes, just checked it. And the second step, retrieving the color of that index zero from the non-existing palette, will result in an access violation. ---------------------------------------------------------------------- domasz · 2024-05-07 10:28 There is something wrong with Colors. I read a JPEG and get a total black image. The showmessage is never fired. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var Laz: TLazIntfImage;    Reader: TFPReaderJPEG;    x,y: Integer;    Col: TFPColor;begin  Reader := TFPReaderJPEG.Create;  Laz := TLazIntfImage.Create(1,1);   Laz.LoadFromFile('test.jpg', Reader);   for y:=0 to Laz.Height-1 do    for x:=0 to Laz.Width-1 do begin      Col := Laz.Colors[x, y];      if Col.red <> 0 then showmessage('x');  end;   Reader.Free;  Laz.Free; ---------------------------------------------------------------------- TRon · 2024-05-07 11:01 @domasz The reason you get an empty image is because there is no space reserved for the actual data (e.g. it is/was never part of the image). What is your actual aim here ? To actually work with lazintfimage or retrieve information about the colors ? Different answer, different solution. see also wiki. Note that the number of options to initialize tlazintimage properly is limited. edit: a possible solution Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  Img : TFPMemoryImage;  Reader: TFPReaderJPEG;  Laz: TLazIntfImage;  x,y: Integer;  Col: TFPColor;begin  // create reader.  // Note that initializing a reader can be omitted as long as a filename  // is used that is recognized by the (standard) reader and the depending  // reader (jpeg in this case) is included in the uses clause.  Reader := TFPReaderJPEG.Create;  // Create image  Img := TFPMemoryImage.Create(0,0);  // Load image using jpeg specific reader  Img.LoadFromFile('test.jpg', Reader);  // release reader on first opportunity it isn't needed anymore  Reader.Free;   // create interface  Laz := TLazIntfImage.Create(0,0);  // set description of the data  Laz.DataDescription:=GetDescriptionFromDevice(0);  // match size of the interface with that of the original image.  Laz.SetSize(Img.Width, Img.Height);  // actually copy the content of the image. Conversion of pixelformat happens  // automatically in case necessary.  Laz.CopyPixels(Img);  // release image on first opportunity  Img.Free;   // original code ...  // ... but for reading the rgba values only that can be replaced with using  // Img variable in which case there is no need to use lazintfImage at all  for y:=0 to Laz.Height-1 do    for x:=0 to Laz.Width-1 do    begin      Col := Laz.Colors[x, y];      if Col.red <> 0 then showmessage('x');    end;  Laz.Free;end; ---------------------------------------------------------------------- wp · 2024-05-07 11:53 TLazIntfImage stores the image in a TRawImage. This mainly consists of a Description (TRawImageDescription) and a pointer to the pixel data. The description defines how the color channels are arranged in the data block. In your case there is no description, i.e. everything is at zero and the LazIntfImage cannot distinguish between the color channels. The correct way to do it is described in https://wiki.freepascal.org/D eveloping_with_Graphics#TLazIntfImage.LoadFromFile (EDIT: or you could TRon's code): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  AImage: TLazIntfImage;  lRawImage: TRawImage;begin  // create a TLazIntfImage with 32 bits per pixel, alpha 8bit, red 8 bit, green 8bit, blue 8bit,  // Bits In Order: bit 0 is pixel 0, Top To Bottom: line 0 is top  lRawImage.Init;  lRawImage.Description.Init_BPP32_A8R8G8B8_BIO_TTB(0,0);  lRawImage.CreateData(false);  // false = Zeroing memory is not needed since the image is read from the file later anyway.  AImage := TLazIntfImage.Create(0,0);  try    AImage.SetRawImage(lRawImage);    // Load an image from disk.    // It uses the file extension to select the right registered image reader.    // The AImage will be resized to the width, height of the loaded image.    AImage.LoadFromFile('lazarus /examples/openglcontrol/data/texture1.png');  finally    AImage.Free;  end;  Quite complicated... An easier way to setup a LazIntfImage correctly is to create an LCL TGraphics instance which knows the correct raw image description and to create the LazIntfImage from that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  jpeg: TJpegImage;begin  jpeg := TJpegImage.Create;  laz := jpeg.CreateIntfImage;  laz.LoadFromFile(...);  // reader normally not needed since it is determined automatically from the file extension. Depending on why you need the IntfImage there is maybe an even simpler method: You could use a TFPMemoryImage rather than a TLazIntfImage, it takes the pixel layout from the image itself. The disadvantage is that it takes more effort to display it in the LCL than with an IntfImage. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  FPImage, FPReadJPEG;var  img: TFPMemoryImage;begin  img := TFPMemoryImage.Create(0,0);  try    img.LoadFromFile('test.jpg');    for y := 0 to img.Height-1 do      for x := 0 to img.Width -1 do      if img.Colors[x, y].Red <> 0 then ShowMessage('red detected');  finally    img.Free;  end;end; ---------------------------------------------------------------------- domasz · 2024-05-07 13:45 Thank you both!