Recent

Author Topic: BGRABitmap and Icon files  (Read 2005 times)

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
BGRABitmap and Icon files
« on: January 17, 2020, 01:18:11 am »
I'd like to save a bgrabitmap (loaded from a file as a png) as an .ico, that is an Icon file.
While BGRA seems happy to load an .ico file, its won't save.  Message about that format not being registered.

I note that in bgrabitmap/. there are some readers, including bmp, png and importantly (for me) ico. And some writers but only a few and not including one for ico.   I am guessing that its not happening ?

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

lainz

  • Hero Member
  • *****
  • Posts: 3766
  • Leandro Diaz
Re: BGRABitmap and Icon files
« Reply #1 on: January 17, 2020, 01:24:18 am »
There is green fish icon editor pro made with Lazarus and supports ico files.

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: BGRABitmap and Icon files
« Reply #2 on: January 17, 2020, 01:26:16 am »
Hi!

Everywhere png-files are now accepted for icons. I think the icon-reader exists for historical reasons: There are so many files around in that old fileformat - which has many restrictions.

If you realy  need the .ico format then save it as png/tiff/jpg/bmp ... and load it into the  Gimp.
It can can save it in the icon format.

Winni


VTwin

  • Hero Member
  • *****
  • Posts: 936
  • Former Turbo Pascal 3 user
Re: BGRABitmap and Icon files
« Reply #3 on: January 17, 2020, 01:44:47 am »
I also use Greenfish Icon Editor. Mac uses an icns format, Windows and Linux use ico. They have different required sizes.

icns: 16, 32, 64, 128, 256, 512, 1024
ico: 16, 20, 24, 32, 40, 48, 60, 64, 72, 96, 256, 512, 768

I make an icon as an svg, scale it, save as png (in GIMP), and copy into Greenfish. Tedious, but it makes a professional looking app.

http://greenfishsoftware.org/gfie.php

I run it on a Mac in VBox Windows.
« Last Edit: January 17, 2020, 02:07:11 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.0
macOS 10.13.6: Lazarus 2.0.10 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.10 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.10 (64 bit on VBox)

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: BGRABitmap and Icon files
« Reply #4 on: January 17, 2020, 04:44:14 am »
Thanks for your input folks.
Everywhere png-files are now accepted for icons.......
Almost Everywhere. Except Lazarus System Tray Icon at run time.

Someone is making me some new icons for may app, they look great except the system tray one. So I wanted to send them a simple app to put one of their png files into the system tray at runtime so they can see what looks good and what does not. I hoped to do it directly from a png file for simplicity but looks like I will tell them to make a ico file first. Not hard as you say (even Eye of [Mate|Gnome] will do it.

Thanks anyway !
Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

circular

  • Hero Member
  • *****
  • Posts: 3512
    • Personal webpage
Re: BGRABitmap and Icon files
« Reply #5 on: January 18, 2020, 10:45:22 am »
Hello dbannon,

BGRABitmap can save icon files. To do that you can use the unit BGRAIconCursor unit.

Create a TBGRAIconCursor object, and add the images into the icon. There can be multiple images in an icon, with different sizes and bit depth.

You can use BGRADitherIconCursor function to reduce the number of colors if necessary.

Regards
Conscience is the debugger of the mind

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: BGRABitmap and Icon files
« Reply #6 on: January 18, 2020, 12:50:53 pm »
Wow, thanks Circular, I have, sort of, got it working by -
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MakeIcon();
  2. var
  3.     image : TBGRABitmap;
  4.     ICur : TBGRAIconCursor;
  5. begin
  6.     image := TBGRABitmap.Create('tomboy24black.png');
  7.     ICur := TBGRAIconCursor.Create();
  8.     ICur.Add(Image, 8);
  9.     ICur.FileType:=ifIco;
  10.     ICur.SaveToFile('test.ico');
  11.     Icur.Free;
  12.     image.free;
  13. end;
               

The '8' (ABitDepth) is a guess and, I suspect not a very good one. It does generate an ico file for my black and white png but not the colour ones.  I assume I should be able to get the depth from the Image, is that right ?

The generated Icon, at this early stage has a bad case of the jaggies ....

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

circular

  • Hero Member
  • *****
  • Posts: 3512
    • Personal webpage
Re: BGRABitmap and Icon files
« Reply #7 on: January 18, 2020, 02:52:27 pm »
You're welcome.

A bit depth of 8 means at most 256 colors. There may be more than 256 colors in your image. What is the error message?

For a True color image, specify 24 if it is opaque or 32 if it has a transparent background.

Or, you can reduce the number of colors using BGRADitherIconCursor function.

If it is just black and white without gray, you can specify a bit depth of 1.

A fully transparent pixel is not counted as one color because icons can have a bit mask for simple opacity.

I assume I should be able to get the depth from the Image, is that right ?
Hmm there is no function directly available to determine the bit depth. I suppose I could add that.
« Last Edit: January 18, 2020, 02:56:21 pm by circular »
Conscience is the debugger of the mind

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: BGRABitmap and Icon files
« Reply #8 on: January 18, 2020, 03:43:00 pm »
Hi!

I am loading 24/32 bit png images, convert  them  to 8 bit and then the code from @dbannon works fine.

Just insert this line after creating the image:

Code: Pascal  [Select][+][-]
  1. image := TBGRABitmap.Create(filename);
  2. BGRAReplace(Image,BGRADitherIconCursor(Image, 8, daFloydSteinberg ));  

But the next question arises:

How to assign the BGRAicon to the Form1.Icon?

Typcasting crashes!

Do I have to save the BGRAicon to a file and then load it into the Form1.Icon????

Winni

lainz

  • Hero Member
  • *****
  • Posts: 3766
  • Leandro Diaz
Re: BGRABitmap and Icon files
« Reply #9 on: January 18, 2020, 04:13:40 pm »
BGRAIconCursor has procedure SaveToStream(ADestination: TStream); override;

And form icon has:

Form1.Icon.LoadFromStream();

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: BGRABitmap and Icon files
« Reply #10 on: January 18, 2020, 07:19:13 pm »
Thanx @lainz:

Yes - assigning a MemoryStream did the trick:

Loading an icon at runtime and assigning it to the main form.
Appears at once in the taskbar.

Winni

circular

  • Hero Member
  • *****
  • Posts: 3512
    • Personal webpage
Re: BGRABitmap and Icon files
« Reply #11 on: January 19, 2020, 04:43:16 pm »
Hi folks,

On the dev branch, I've added a function BGRABitDepthIconCursor to detect the bit depth to store an image in an icon. You can use that as a parameter to Add so that you don't need to always convert or always save in 32 bit.

Also I've added Assign function to multifile types, and in particular to TBGRAIconCursor. This way you assign it to/from a TIcon or TCursorImage. I've used the stream trick. Of course this works only if LCL is available.

Regards
« Last Edit: January 19, 2020, 04:44:50 pm by circular »
Conscience is the debugger of the mind

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: BGRABitmap and Icon files
« Reply #12 on: January 19, 2020, 11:48:26 pm »
Now, I have not tried circular's recent addition (but it sounds great!). 
However, doing the conversion myself, using -
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MakeIcon();
  2. var
  3.     image : TBGRABitmap;
  4.     ICur : TBGRAIconCursor;
  5. begin
  6.     image := TBGRABitmap.Create('24x24s.png');
  7.     BGRAReplace(Image,BGRADitherIconCursor(Image, 8, daFloydSteinberg ));
  8.     ICur := TBGRAIconCursor.Create();
  9.     ICur.Add(Image, 8 );
  10.     ICur.FileType:=ifIco;
  11.     ICur.SaveToFile('24x24t.ico');
  12.     Icur.Free;
  13.     image.free;
  14. end;
                   

I do note a reduction in image quality.  I don't see that same reduction if, for example, I use "Eye of Mate" to convert a png to ico format. See attached example, the one on the right is generated using BGRA code from the one on the left.

Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

circular

  • Hero Member
  • *****
  • Posts: 3512
    • Personal webpage
Re: BGRABitmap and Icon files
« Reply #13 on: January 20, 2020, 12:10:19 am »
Hi

If you want to keep full quality just save in 32 bit depth.
Conscience is the debugger of the mind

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: BGRABitmap and Icon files
« Reply #14 on: January 22, 2020, 10:26:17 am »
Yep, thats fixed it.  Changed both the '8's to '32' and I get, apparently, the resolution as the origional.

Thanks for that. And thanks for what appears to be a very powerful package.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

 

TinyPortal © 2005-2018