Recent

Author Topic: Lazres created lrs file can we extract the embedded image?  (Read 1149 times)

Aruna

  • Hero Member
  • *****
  • Posts: 807
Lazres created lrs file can we extract the embedded image?
« on: August 07, 2025, 04:57:35 am »
I ran lazres as shown:
Code: Pascal  [Select][+][-]
  1. aruna@debian:~/demos$ ../../lazarus/tools/lazres debug.lrs switch.png

it worked as expected and created the file debug.lrs shown below:
Code: Text  [Select][+][-]
  1. LazarusResources.Add('switch','PNG',[
  2.   #137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#0#24#0#0#0#24#8#6#0#0#0#224'w='#248#0
  3.   +#0#0#4'sBIT'#8#8#8#8'|'#8'd'#136#0#0#0#9'pHYs'#0#0#0#166#0#0#0#166#1#221'}'
  4.   +#255'8'#0#0#0#25'tEXtSoftware'#0'www.inkscape.org'#155#238'<'#26#0#0#1'+IDAT'
  5.   +'H'#137#221#212#189'J'#3'A'#20#5#224'ob'#26#193'B'#16'R'#8'bg/h!'#164#150'46'
  6.   +#150'>'#130'O h'#145#23#176#177#14#216#251#0#130#133#133#141#176')'#236#5#17
  7.   +';'#241#167#17#236'T'#198#194')'#242#183#187'Y'#221'4'#30#184'0g'#231#236#189
  8.   +'s'#207#222'Y1F'#147#2'-'#156#224#17'gh'#229'i'#139#162'!'#31'G'#248#192':'
  9.   +#238#208'+'#208#230#163#160#131#23','#165#245#28#158#177'Zg'#7#183#216'H'#135
  10.   +#248#194'9v'#234#236'`'#15#151#3'|'#23#23'U;(*'#208#196'=6'#19'_'#192#27#22
  11.   +'j'#177'('#198#248#137'c'#28'$'#254#142'kl'#215'bQ:'#245#188#159'1]K|'#31#167
  12.   +#181'X4P'#228#16#189#180'^'#193#19#26'u'#22'X'#196'+'#150#19#191#193'V-'#5
  13.   +#208'F'#134#152#19#25#218#127')'#144#161'S'#176#223'AV'#148'#$'#225'D'#132#16
  14.   +'b'#140'1'#20#13'I'#169#166#162'%e1fY%K'#166#24#136'1'#203#134','#154#198#146
  15.   +'2'#140#230'('#250#217#213#130#255'W'#160#31'B'#232#252'6Yz'#183'?'#244'p'
  16.   +#214'cZ'#233#162#133#16#186#233'P'#221'<'#205'(f'#254#13#154'%'#251'W#'#252
  17.   +'a'#10#205#16#190#1#138#171#202#191'")'#239#250#0#0#0#0'IEND'#174'B`'#130
  18. ]);

Do we have a tool that can extract the embedded image? If not what would be the simplest minimal code to do this? The image I used is attached.
« Last Edit: August 07, 2025, 05:04:16 am by Aruna »

Khrys

  • Sr. Member
  • ****
  • Posts: 456
Re: Lazres created lrs file can we extract the embedded image?
« Reply #1 on: August 07, 2025, 07:15:53 am »
This is just the raw file data converted into a string literal; the PNG chunk headers are visible in plaintext. Special characters (e.g. null bytes) are represented using escape sequences.

Off the top of my head (untested):

Code: Pascal  [Select][+][-]
  1. program ExtractResource;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils;
  7.  
  8. const
  9.   DATA: String =
  10.    #137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#0#24#0#0#0#24#8#6#0#0#0#224'w='#248#0
  11.   +#0#0#4'sBIT'#8#8#8#8'|'#8'd'#136#0#0#0#9'pHYs'#0#0#0#166#0#0#0#166#1#221'}'
  12.   +#255'8'#0#0#0#25'tEXtSoftware'#0'www.inkscape.org'#155#238'<'#26#0#0#1'+IDAT'
  13.   +'H'#137#221#212#189'J'#3'A'#20#5#224'ob'#26#193'B'#16'R'#8'bg/h!'#164#150'46'
  14.   +#150'>'#130'O h'#145#23#176#177#14#216#251#0#130#133#133#141#176')'#236#5#17
  15.   +';'#241#167#17#236'T'#198#194')'#242#183#187'Y'#221'4'#30#184'0g'#231#236#189
  16.   +'s'#207#222'Y1F'#147#2'-'#156#224#17'gh'#229'i'#139#162'!'#31'G'#248#192':'
  17.   +#238#208'+'#208#230#163#160#131#23','#165#245#28#158#177'Zg'#7#183#216'H'#135
  18.   +#248#194'9v'#234#236'`'#15#151#3'|'#23#23'U;(*'#208#196'=6'#19'_'#192#27#22
  19.   +'j'#177'('#198#248#137'c'#28'$'#254#142'kl'#215'bQ:'#245#188#159'1]K|'#31#167
  20.   +#181'X4P'#228#16#189#180'^'#193#19#26'u'#22'X'#196'+'#150#19#191#193'V-'#5
  21.   +#208'F'#134#152#19#25#218#127')'#144#161'S'#176#223'AV'#148'#$'#225'D'#132#16
  22.   +'b'#140'1'#20#13'I'#169#166#162'%e1fY%K'#166#24#136'1'#203#134','#154#198#146
  23.   +'2'#140#230'('#250#217#213#130#255'W'#160#31'B'#232#252'6Yz'#183'?'#244'p'
  24.   +#214'cZ'#233#162#133#16#186#233'P'#221'<'#205'(f'#254#13#154'%'#251'W#'#252
  25.   +'a'#10#205#16#190#1#138#171#202#191'")'#239#250#0#0#0#0'IEND'#174'B`'#130;
  26.  
  27. var
  28.   Stream: TFileStream;
  29. begin
  30.   Stream := TFileStream.Create('output.png', fmCreate or fmOpenWrite or fmShareDenyWrite);
  31.   try
  32.     Stream.Write(DATA[1], DATA.Length);
  33.   finally
  34.     Stream.Free();
  35.   end;
  36. end;

wp

  • Hero Member
  • *****
  • Posts: 13565
Re: Lazres created lrs file can we extract the embedded image?
« Reply #2 on: August 07, 2025, 12:26:53 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Image1.Picture.LoadFromLazarusResource('switch');
  4. end;

or using only the image part of the instruction:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   s: TStringStream;
  4. begin
  5.   s := TStringStream.Create(
  6.     #137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#0#24#0#0#0#24#8#6#0#0#0#224'w='#248#0
  7.     +#0#0#4'sBIT'#8#8#8#8'|'#8'd'#136#0#0#0#9'pHYs'#0#0#0#166#0#0#0#166#1#221'}'
  8.     +#255'8'#0#0#0#25'tEXtSoftware'#0'www.inkscape.org'#155#238'<'#26#0#0#1'+IDAT'
  9.     +'H'#137#221#212#189'J'#3'A'#20#5#224'ob'#26#193'B'#16'R'#8'bg/h!'#164#150'46'
  10.     +#150'>'#130'O h'#145#23#176#177#14#216#251#0#130#133#133#141#176')'#236#5#17
  11.     +';'#241#167#17#236'T'#198#194')'#242#183#187'Y'#221'4'#30#184'0g'#231#236#189
  12.     +'s'#207#222'Y1F'#147#2'-'#156#224#17'gh'#229'i'#139#162'!'#31'G'#248#192':'
  13.     +#238#208'+'#208#230#163#160#131#23','#165#245#28#158#177'Zg'#7#183#216'H'#135
  14.     +#248#194'9v'#234#236'`'#15#151#3'|'#23#23'U;(*'#208#196'=6'#19'_'#192#27#22
  15.     +'j'#177'('#198#248#137'c'#28'$'#254#142'kl'#215'bQ:'#245#188#159'1]K|'#31#167
  16.     +#181'X4P'#228#16#189#180'^'#193#19#26'u'#22'X'#196'+'#150#19#191#193'V-'#5
  17.     +#208'F'#134#152#19#25#218#127')'#144#161'S'#176#223'AV'#148'#$'#225'D'#132#16
  18.     +'b'#140'1'#20#13'I'#169#166#162'%e1fY%K'#166#24#136'1'#203#134','#154#198#146
  19.     +'2'#140#230'('#250#217#213#130#255'W'#160#31'B'#232#252'6Yz'#183'?'#244'p'
  20.     +#214'cZ'#233#162#133#16#186#233'P'#221'<'#205'(f'#254#13#154'%'#251'W#'#252
  21.     +'a'#10#205#16#190#1#138#171#202#191'")'#239#250#0#0#0#0'IEND'#174'B`'#130
  22.   );
  23.   try
  24.     Image2.Picture.LoadFromStream(s);
  25.   finally
  26.     s.Free;
  27.   end;
  28. end;
« Last Edit: August 07, 2025, 12:44:51 pm by wp »

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: Lazres created lrs file can we extract the embedded image?
« Reply #3 on: August 07, 2025, 01:10:55 pm »
This is just the raw file data converted into a string literal; the PNG chunk headers are visible in plaintext. Special characters (e.g. null bytes) are represented using escape sequences.

Off the top of my head (untested):

Code: Pascal  [Select][+][-]
  1. program ExtractResource;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils;
  7.  
  8. const
  9.   DATA: String =
  10.    #137'PNG'#13#10#26#10#0#0#0#13'IHDR'#0#0#0#24#0#0#0#24#8#6#0#0#0#224'w='#248#0
  11.   +#0#0#4'sBIT'#8#8#8#8'|'#8'd'#136#0#0#0#9'pHYs'#0#0#0#166#0#0#0#166#1#221'}'
  12.   +#255'8'#0#0#0#25'tEXtSoftware'#0'www.inkscape.org'#155#238'<'#26#0#0#1'+IDAT'
  13.   +'H'#137#221#212#189'J'#3'A'#20#5#224'ob'#26#193'B'#16'R'#8'bg/h!'#164#150'46'
  14.   +#150'>'#130'O h'#145#23#176#177#14#216#251#0#130#133#133#141#176')'#236#5#17
  15.   +';'#241#167#17#236'T'#198#194')'#242#183#187'Y'#221'4'#30#184'0g'#231#236#189
  16.   +'s'#207#222'Y1F'#147#2'-'#156#224#17'gh'#229'i'#139#162'!'#31'G'#248#192':'
  17.   +#238#208'+'#208#230#163#160#131#23','#165#245#28#158#177'Zg'#7#183#216'H'#135
  18.   +#248#194'9v'#234#236'`'#15#151#3'|'#23#23'U;(*'#208#196'=6'#19'_'#192#27#22
  19.   +'j'#177'('#198#248#137'c'#28'$'#254#142'kl'#215'bQ:'#245#188#159'1]K|'#31#167
  20.   +#181'X4P'#228#16#189#180'^'#193#19#26'u'#22'X'#196'+'#150#19#191#193'V-'#5
  21.   +#208'F'#134#152#19#25#218#127')'#144#161'S'#176#223'AV'#148'#$'#225'D'#132#16
  22.   +'b'#140'1'#20#13'I'#169#166#162'%e1fY%K'#166#24#136'1'#203#134','#154#198#146
  23.   +'2'#140#230'('#250#217#213#130#255'W'#160#31'B'#232#252'6Yz'#183'?'#244'p'
  24.   +#214'cZ'#233#162#133#16#186#233'P'#221'<'#205'(f'#254#13#154'%'#251'W#'#252
  25.   +'a'#10#205#16#190#1#138#171#202#191'")'#239#250#0#0#0#0'IEND'#174'B`'#130;
  26.  
  27. var
  28.   Stream: TFileStream;
  29. begin
  30.   Stream := TFileStream.Create('output.png', fmCreate or fmOpenWrite or fmShareDenyWrite);
  31.   try
  32.     Stream.Write(DATA[1], DATA.Length);
  33.   finally
  34.     Stream.Free();
  35.   end;
  36. end;
Thank you @Khrys very much appreciate your time. It works.

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: Lazres created lrs file can we extract the embedded image?
« Reply #4 on: August 07, 2025, 01:44:39 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Image1.Picture.LoadFromLazarusResource('switch');
  4. end;
@wp I honestly do not know how to thank you. I am working on that package editor tool Juha wanted and I did some extensive reading and research into the the way how the IDE does it's magic internally. I am starting to understand it but as you know it is a time consuming process and there is a very steep learning curve. So after many tries with little success and crashing my poor IDE again and again I decided to do this as a stand alone application to start with. The reason I asked how to extract the *.lrs image is because I wanted to display it to the user but I was at a dead loss since all the available public domain documentation on the internet and even the wiki said the way to do this is to:

.lrs
Code: Pascal  [Select][+][-]
  1. Include .lrs file in the initialization section
  2.  
  3. initialization
  4.   // .LRS files are plain-text pascal statements and need unit LResources to be included in the Uses clause
  5.  {$I MyResources.lrs}
  6. end.
  7.  

.res
Code: Pascal  [Select][+][-]
  1. Load .res file in the implementation section
  2.  
  3. implementation
  4.   // .RES files are binary resources and can be loaded
  5.   {$R MyResources.res}
  6.  
  7. var
  8.   img: TImage;
  9. begin
  10.   img.Picture.Bitmap.LoadFromResourceName( hInstance, 'IMG1' );
  11. end;
Both which require me to include the resource file in my unit and does not offer any way for me to dynamically do this on the fly ( or so I thought until I had a look at the zip you just sent).  I do not mind generating a unit on teh fly but I do not know how to get it to run once generated from within my app. So I was at that point very close to calling it quits.

Thank you for taking the time and this helps me immensely so I am very grateful and thankful to you for showing me the way. Very much appreciate you sharing the knowledge. Thank you!

I have attached a screenshot of the tool I am building it is very much in it's early stages but does seem to work so far when I was testing. The left most Tmemo shows you the user selected package file (*.lpk) and the file path at the top if the checkbox is checked else just the filename. The listbox next t o it shows the available components found in the package. Number found is shown at the top. The next and last column has two Tmemos stacked and the top one shows you the contents of the selected file in the listbox  and the Tmemo a the bottom shows you the *.lrs file contents if found. I have attached a zip if you or anyone else wants to have a look. The code is messy and like I said may not be to your exacting standrads but one has to begin somewhere?

wp

  • Hero Member
  • *****
  • Posts: 13565
Re: Lazres created lrs file can we extract the embedded image?
« Reply #5 on: August 07, 2025, 02:57:00 pm »
Just came to my mind: In folder examples/lazresexplorer of your Lazarus installation you find a resource explorer. You can find there how to read the .res files.

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: Lazres created lrs file can we extract the embedded image?
« Reply #6 on: August 12, 2025, 04:03:33 pm »
Just came to my mind: In folder examples/lazresexplorer of your Lazarus installation you find a resource explorer. You can find there how to read the .res files.
Hi @wp I tried examples/lazresexplorer and it is a nice bit of work but the image tab does not seem to work yet. I will have a look at the code when I can find some time. Screenshots attached show what I found.

wp

  • Hero Member
  • *****
  • Posts: 13565
Re: Lazres created lrs file can we extract the embedded image?
« Reply #7 on: August 12, 2025, 05:38:06 pm »
the image tab does not seem to work yet.
It does work, but only for bitmap resources, so far. In Windows they have a special type, RT_BITMAP, while png images have the generic type RT_RCDATA. The resource explorer checks the resource type, and displays RT_BITMAP in the image tab while RT_RCDATA go into the hex viewer tab by default. In order to display RT_RCDATA resources also in the image tab, the resource stream must be checked whether it contains a supported image type. Basically this is already built into TPicture, but not readily accessible. Therefore, I prefer to use the TFPCustomImage class method FindReaderFromStream which returns the appropriate fcl-image reader class from the file signature and header, or nil when the stream is not such an image:
Code: Pascal  [Select][+][-]
  1. procedure TreMainForm.LoadResourceAsBinary(R: TAbstractResource);
  2. var
  3.   ...
  4.   RC: TFCustomImageReaderClass;
  5. begin
  6. ...
  7. // to be added at the end of the method
  8.   // Check whether the resource is a non RT_BITMAP image and try to display it
  9.   R.RawData.Position := 0;
  10.   RC := TFPCustomImage.FindReaderFromStream(R.RawData);
  11.   if RC <> nil then
  12.     Image1.Picture.LoadFromStream(R.RawData);
  13. end;

[EDIT]
I updated the lazresexplorer sample project in Laz/main with this and some more additions. If you don't use Laz/main I am attaching a zip snapshot from github.
« Last Edit: August 13, 2025, 01:21:39 am by wp »

 

TinyPortal © 2005-2018