Can TFPCustomImage load from an array of bytes? lazarus / 21-General ---------------------------------------------------------------------- Jonny · 2025-02-02 17:50 I receive a stream of bytes from an external source - they are the contents of a .png image file. Can this be converted to a TFPCustomImage without writing any intermediate files to disk? For testing purposes, I have placed the received data in a const array of both byte and char to emulate the process. The best contender seems to be TFPMemoryImage.LoadFromStream but have I wrongly assumed that a stream is not simply an array of the bytes/chars? 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 TFPMemoryIm age.LoadFromStream(@my_array,TFPCustomImageReader);Incompatible type for arg no. 1: Got "Pointer", expected "TStream" Simply looking for the quickest way to convert the incoming data, if I should be assembling it as something other than an array then can you please advise? ---------------------------------------------------------------------- TRon · 2025-02-02 18:28 Create a TMemoryStream and use its methods  to store the bytes that you received. In case the code requires a bytearray then receive the bytes in the array, then store them into the memory stream. Use that memorystream in the fpcustomimage loadfromstream method. It could be you need to specifically create a pngimagereader and use that as parameter to the loadfromstream method as well. ---------------------------------------------------------------------- wp · 2025-02-02 19:04 Quote from: Jonny on February 02, 2025, 06:50:34 pmI receive a stream of bytes from an external source - they are the contents of a .png image file. What do you mean with "contents"? A complete png image separated into individual bytes? Then it is a valid image, simply store it to disk. Or the pixel matrix only? In this case the next question is: Is the pixel matrix compressed? If not, you must know about the pixel format? How many bytes are combined in each pixel? Is it a monochrome image or a color image? What is the order of the color channels? Red-green-blue, or Blue- green-red? Or is it a paletted image where each pixel is the index into a color palette. What is the width and the height since there is no unique solution for width*height=number_of_pixels. All this information goes into the TRawImageDescription record of a TLazIntfImages. In the final step I would copy the pixel stream into the Data block of the RawImage (of the LazIntfImage). If the bytes you received are a compressed image, you must also know the compression method - png can support several of them. Then probably... Hmmmh, I don't know... Maybe try to uncompress the byte stream by calling the corresponding method of the png reader (if that is public...). Or call the corresponding decompression from a standalone routine. I can try it for you, but I'll need an answer to above questions first. ---------------------------------------------------------------------- TRon · 2025-02-02 19:16 You are (of course) correct there wp but I think TS only wants embedded png images (which they seem to be) stored in an array to be loaded. If the array does not contain a valid png image then that is a programmer fault. To TS depending on your use case it might be easier to attach your images in your executable as a resource though what you are trying to accomplish is a nice exercise as well. ---------------------------------------------------------------------- Jonny · 2025-02-02 19:28 Quote from: TRonCreate a TMemoryStream and use its methods  to store the bytes that you received. In case the code requires a bytearray then receive the bytes in the array, then store them into the memory stream. Thanks, this is what I now have: 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 program load; uses  Classes, FPImage, FPReadPNG, FPWritePNG; {$include bytes.inc}{$include char.inc} var  imgLogo: TFPCustomImage;  imgStream: TMemoryStream;  imgReader: TFPCustomImageReader;  imgWriter: TFPCustomImageWriter; begin  imgStream := TMemoryStream.Create;  imgStream.Read(bytes_logo,SizeOf(bytes_logo));//  imgStream.Read(chars_logo,SizeOf(bytes_logo));  imgLogo := TFPMemoryImage.Create(0,0);  imgReader := TFPReaderPNG.Create;  imgLogo.LoadFromStream(imgStream,imgReader);  imgWriter := TFPWriterPNG.Create;  imgLogo.SaveToFile('test.png');end. But I get an error: 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 $ ./load An unhandled exception occurred at $0000000000458DE0:FPImageException: Wrong image format  $0000000000458DE0  $00000000004561B9 Quote from: TRonUse that memorystream in the fpcustomimage loadfromstream method. It could be you need to specifically create a pngimagereader and use that as parameter to the loadfromstream method as well. The code already had TFPReaderPNG, are you saying that I was using it incorrectly? Quote from: wpWhat do you mean with "contents"? A complete png image separated into individual bytes? Correct. A remote computer snapshots an external video input. It then creates a valid complete .png file which I receive byte-by-byte over a serial port. If you look at the .inc files in my sample application, they are the bytes of the logo.png file included in the same archive. Quote from: wpThen it is a valid image, simply store it to disk. The tiny embedded device that will eventually run this will have no disk storage to write or save files to, hence my comment about "without writing any intermediate files to disk" and attempt to use a stream. Quote from: TRonit might be easier to attach your images in your executable as a resource Not possible, sadly, the images will be generated in an ongoing routine - this is a (small) part of a free museum exhibition that uses the images from cameras and other source to produce some arty effects. Also, please excuse my ignorance, but what is "TS"? ---------------------------------------------------------------------- TRon · 2025-02-02 19:37 Quote from: Jonny on February 02, 2025, 08:28:17 pmThanks, this is what I now have: ... But I get an error: Small pitfall: when using read use arrayname[0]. QuoteThe code already had TFPReaderPNG, are you saying that I was using it incorrectly? No, you did good. I reacted to your post before actually looking at your attached example code. QuoteNot possible, sadly, the images will be generated in an ongoing routine - this is a (small) part of a free museum exhibition that uses the images from cameras and other source to produce some arty effects. Ok, no problem then. Note that you could store received bytes (I do not know the details so might not be applicable) directly into the memorystream by either storing it directly using the streams methods or set the size of the memorystream and use the memory property. QuoteAlso, please excuse my ignorance, but what is "TS"? I use it for Topic Starter aka OP (Original Poster). I prefer the former because a thread can be long and sometimes someone starts another topic/discussion within the same thread. I consider OP to be person with the very first post of a new thread/topic. ---------------------------------------------------------------------- Jonny · 2025-02-02 19:51 Quote from: TRonSmall pitfall: when using read use arrayname[0]. Replacing 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 imgStream.Read(bytes_logo,SizeOf(bytes_logo));with 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 imgStream.Read(bytes_logo[0],SizeOf(bytes_logo));makes no difference unfortunately. Code: [Select]$ ./load An unhandled exception occurred at $00000000004242D0: FPImageException: Wrong image format   $00000000004242D0 Quote from: TRonOk, no problem then. Note that you could store received bytes (I do not know the details so might not be applicable) directly into the memorystream by either storing it directly using the streams methods or set the size of the memorystream and use the memory property. Hmm, if I understand you correctly, replace 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 imgLogo.LoadFromStream(imgStream,imgReader);with 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 imgLogo.LoadFromStream(imgStream.Memory,imgReader);but that gives: Got "Pointer", expected "TStream" or 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 imgLogo.LoadFromStream(imgStream.Memory[0],imgReader);but this time: Array type required Quote from: TRonI use it for Topic Starter aka OP (Original Poster).Haha, learning both pascal and forum-speak here - thank you! ---------------------------------------------------------------------- TRon · 2025-02-02 20:18 Quote from: Jonny on February 02, 2025, 08:51:37 pmReplacing .. makes no difference unfortunately. :facepalm: It should be write, not read. The function should be interpreted from viewpoint of the stream (e.g. you want to write data /to/ the stream, not read /from/ it  ). Might be after the write you need to set position to zero before using the imagereader. QuoteHmm, if I understand you correctly, replace No, not exactly  What I meant is you receive data form somewhere else (you do not generated the image yourself). So far I understood from your situation that somehow somewhere some thing offers you bytes that represent a picture in the png format. Instead of storing those received bytes into an array you can opt to store them directly into the memorystream by either using one of the write methods or by setting the size (allocating memory) and using the pointer property to use as address to start storing those bytes that were received. That is unless I misunderstood what you wrote. edit: like method2 (though still using an array but it can be anything as long as it is somewhere in memory) 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 program load; {$mode objfpc}{$h+} uses  sysutils, classes, FPImage, FPReadPNG, FPWritePNG; {.$define method1} {$include bytes.inc}{$include char.inc} var  imgLogo   : TFPCustomImage = nil;  imgReader : TFPCustomImageReader = nil;  imgWriter : TFPCustomImageWriter = nil;  MS        : TMemoryStream = nil; begin  MS        := TMemoryStream.Create;  imgLogo   := TFPMemoryImage.Create(0,0);  imgReader := TFPReaderPNG.Create;  imgWriter := TFPWriterPNG.Create;   try    {$ifdef method1}    MS.Write(bytes_logo[0], sizeof(bytes_logo));    MS.Position := 0;    {$else}  // method2: store bytes directly into memory    MS.Size := sizeof(bytes_logo);    move(bytes_logo[0], MS.Memory^, sizeof(bytes_logo));    {$endif}     imgLogo.LoadFromStream(MS, imgReader);    imgLogo.SaveToFile('test.png');  finally    imgwriter.Free;    imgReader.Free;    imgLogo.Free;    MS.Free;  end;end.[code] ---------------------------------------------------------------------- Jonny · 2025-02-02 21:08 Quote from: TRon:facepalm: It should be write, not read. The function should be interpreted from viewpoint of the stream (e.g. you want to write data /to/ the stream, not read /from/ it  ). Wow, this works (and my head hurts!): 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 program load; uses  Classes, FPImage, FPReadPNG, FPWritePNG; {$include bytes.inc} var  imgLogo: TFPCustomImage;  imgStream: TMemoryStream;  imgReader: TFPCustomImageReader;  imgWriter: TFPCustomImageWriter; begin  imgReader := TFPReaderPNG.Create;  imgWriter := TFPWriterPNG.Create;   imgStream := TMemoryStream.Create;  imgStream.Write(bytes_logo,SizeOf(bytes_logo));  imgStream.Position := 0;   imgLogo := TFPMemoryImage.Create(0,0);  imgLogo.LoadFromStream(imgStream,imgReader);  imgLogo.SaveToFile('stream.png',imgWriter);   imgStream.SaveToFile('original.png');end.  Thank you for explaining, lots to learn. But one last thing, by going though the stream, the image changes from 8-bit with alpha to 16-bit without alpha: Code: [Select]$ file original.png original.png: PNG image data, 1141 x 1142, 8-bit/color RGBA, non- interlaced $ file stream.png stream.png: PNG image data, 1141 x 1142, 16-bit/color RGB, non- interlaced I assumed that since we are not manipulating the file then what goes in should come out. Is there a parameter of TFPCustomImage/TFPMemoryImage that needs to be set? ---------------------------------------------------------------------- TRon · 2025-02-02 21:23 Quote from: Jonny on February 02, 2025, 10:08:45 pmWow, this works (and my head hurts!): QuoteI assumed that since we are not manipulating the file then what goes in should come out. Is there a parameter of TFPCustomImage/TFPMemoryImage that needs to be set? Probably play with properties usealpha and wordsized , see also reference. ---------------------------------------------------------------------- Jonny · 2025-02-02 21:41 I read and re-read the reference pages but never find what I am looking for - thanks for the pointers. Not sure if I am doing this correctly but it now works - although the code looks wrong to me (writer=reader). 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 program load; uses  Classes, FPImage, FPReadPNG, FPWritePNG; {$include logo.inc} var  imgLogo: TFPCustomImage;  imgStream: TMemoryStream;  imgReader: TFPReaderPNG;  imgWriter: TFPWriterPNG; begin  imgReader := TFPReaderPNG.Create;  imgWriter := TFPWriterPNG.Create;   imgStream := TMemoryStream.Create;  imgStream.Write(logo_01,SizeOf(logo));  imgStream.Position := 0;   imgLogo := TFPMemoryImage.Create(0,0);  imgLogo.LoadFromStream(imgStream,imgReader);   imgWriter.UseAlpha := imgReader.UseAlpha;  imgWriter.WordSized := imgReader.WordSized;  imgLogo.SaveToFile('stream.png',imgWriter);   imgStream.SaveToFile('original.png');end.  Thanks again. Hopefully someone else can benefit from this too. ---------------------------------------------------------------------- TRon · 2025-02-02 21:49 Quote from: Jonny on February 02, 2025, 10:41:04 pmI read and re-read the reference pages but never find what I am looking for - thanks for the pointers. TFPImage is a bit of undocumented territory. If you toy with it a little then it al makes sense (at least if you have a bit of understanding about graphic formats). QuoteNot sure if I am doing this correctly but it now works - although the code looks wrong to me (writer=reader). That is ok. The code copies the value of the property from one component to the property of another component. Actually it is even neater then doing it manually because now those values depend on what the reader encounters when loading the image. QuoteThanks again. Hopefully someone else can benefit from this too. For sure. That is how the wiki gets expanded f.i. Someone asks a question, we try to figure it out and in case it is important (enough) it ends up in the wiki. see also here. So, thank you for your contribution ! Happy coding !  PS: the initial example code that you posted with the logo does not identify itself as having an alpha channel for me ? ---------------------------------------------------------------------- Jonny · 2025-02-02 22:32 Quote from: TRonHappy coding !  Thank you and the forum team! Quote from: TRonPS: the initial example code that you posted with the logo does not identify itself as having an alpha channel for me ? Indeed, I had originally included a small test png file - have since tried with real data of various formats and sizes. ---------------------------------------------------------------------- TRon · 2025-02-02 22:36 Quote from: Jonny on February 02, 2025, 11:32:00 pmIndeed, I had originally included a small test png file - have since tried with real data of various formats and sizes. Excellent, that explains it. Thank you for having demystified that (now I can sleep at night  )