Utility: PngIron - Flatten Alpha-PNG image files onto a mask color-VBForums
Results 1 to 6 of 6

Thread: Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,531

    Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

    Version 1.1
    02-December-2015
    Robert Riemersma

    This is a small GUI utility intended to help you pre-process small PNG format images that have alpha channel transparency and translucency. Additional work may need to be done on the output saved by this program using another more generalized image editing tool.

    Often programmers using classic Visual Basic need small bitmap images for use in making icons and images for use with common Visual Basic controls. But many times they can find free images that might be useful except for being alpha-PNG images. Instead what is needed is "maskcolor" transparency, or in some cases GIF-transparency will do.

    This program can help iron out or "flatten" alpha-PNG images against a selected maskcolor and save this composited image to disk as a new PNG file. Such a flat PNG can be easier to work with using other tools to make a final product.

    But PngIron is a one-trick pony, not a general purpose graphics tool. Most versions of Windows Paint can be valuable in finishing up, and other tools like the popular IrfanView can help with this stage too.


    Requrements, Installing PngIron

    PngIron consists of a single EXE. You can copy this to any folder and create shortcuts as needed to run it.

    There are no extra DLLs or OCXs to install. However it does make use of WIA 2.0 and Shell32 4.71 or later.

    This means it requires Windows Vista or later. If you have a machine with Windows 2000 SP4 or Windows XP SP1 or later and it has WIA 2.0 installed then it should work there as well. However Microsoft no longer provides the necessary DLL for installation into those unsupported systems. Quote from MSDN:

    Developer audience

    The WIA Automation Layer API is designed for use by Microsoft Visual Basic 6.0, Active Server Pages (ASP), and scripting programmers.

    Run-time requirements

    Applications that use the WIA Automation Layer API require Windows Vista or later. Earlier versions of Windows are not supported.

    Compiling PngIron

    Nothing special is required here. You just need Visual Basic 6.0 (Professional Edition or better) installed on a Windows Vista (or later) PC. It can also be compiled under Windows XP SP1 to SP3 and Windows 2000 SP4 as long as you have the WIA 2.0 SDK installed there.


    What PngIron Does

    First you open an existing alpha-PNG file, which Pngiron will display. If the file has no alpha information PngIron will let you know and things end there.

    Otherwise you can:

    • Set a maskcolor.
    • Choose an alpha threshold level for flattening that eliminates fringing and shadows as desired.
    • "Knock out" stray pixels by replacing them with the maskcolor or selected paintcolors.
    • Reduce the color count via a simple algorithm.


    You can fiddle with the various settings until the image is clean enough for use. Then you can save it as a new PNG file.


    Limitations

    PngIron does not save in BMP format because it is bulky. It does not save as GIF because it doesn't have enough control of the image. Save as GIF would often end up with "dithered" colors.

    Many operations such as a change to the alpha threshold can be slow due to the way PngIron is working with these images. You will want to avoid working with images much larger than 256x256 pixels - they work but things can slow down quite a bit as you make changes.

    No attempt has been made to deal with DPI settings higher than the standard 96dpi display setting.

    With more effort PngIron could be modified to reduce or eliminate these limitations. If you need to do a lot of this you might want to consider more generalized and more powerful image editing tools.


    License

    Microsoft Public License (Ms-PL)

    This is a very permissive license. See the ReadMe.txt in the attachment for full details.


    Attachment

    This containts the precompiled binary PngIron.exe, ReadMe.txt file, and the entire VB6 source for compiling PngIron yourself.
    Attached Files Attached Files

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,531

    Re: Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

    Example.

    Here's an image that has two small pictures I might want to use to make +/- "zoom" buttons. Sadly there is some "fringing" we need to clean up before these will be very useful:

    Name:  sshot1.png
Views: 237
Size:  13.3 KB

    Setting the alpha flattening threshold to D0 cleans things up a little bit, though less so in this case than perhaps for other images:

    Name:  sshot2.png
Views: 238
Size:  12.9 KB

    We can use the very primitive pixel-editing feature in PngIron to "knock out" a few ugly pixels and "bang in" a few others here and there:

    Name:  sshot3.png
Views: 238
Size:  13.0 KB
    Last edited by dilettante; Dec 2nd, 2015 at 06:47 PM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,531

    Re: Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

    We can take that a step further.

    Here I have knocked out a couple more pixels and then used the color reduction feature of PngIron:

    Name:  sshot4.png
Views: 237
Size:  12.6 KB

    In this case we have dropped the color-count to merely 46 unique colors.

    Once we save that file, we find we have a PNG that still has alpha channel information but for all pixels it is set to "solid" (FF). This makes it easier to work with in another graphics tool to cut out what you need, reduce the color depth, and save as a GIF or small BMP.
    Last edited by dilettante; Dec 2nd, 2015 at 06:48 PM.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,531

    Re: Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

    Here's another example that has a shadow effect:

    Name:  sshot5.png
Views: 238
Size:  42.9 KB

    We can set the threshold to 01 and apply color reduction to get a decent flat image. Here we only have 223 unique colors left, which makes for a nice 8-bit GIF or BMP you can create from it using another tool:

    Name:  sshot6.png
Views: 240
Size:  23.8 KB


    You may note that the bottom "status" area has changed since the previous example's screenshots. PngIron 1.1 added more information, namely when you "save" it shows the save file's name below the "open" file name now.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,531

    Re: Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

    It would be possible to make another command-line or batch program based on the part of PngIron that does the heavy lifting: the Masker class. You could create a simple batch script language to specify the open and save file names or even folders and set things like the maskcolor, flattening threshold, and color reduction level.

    In case it is helpful here's a block diagram showing the high level flow within Masker:

    Last edited by dilettante; Dec 2nd, 2015 at 07:13 PM.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,531

    Re: Utility: PngIron - Flatten Alpha-PNG image files onto a mask color

    Here is the compiled PngIron 1.1 binary packaged using a reg-free COM manifest along with the redist version of wiaaut.dll. No changes in the program source at all.


    While deployment now means copying both the EXE and its accompanying "bin" folder to a destination folder, PngIron can thus be used on WinXP SP1 or later and probably works on Win2K SP4 as well.

    Oddly enough... the slow operations seem to run significantly faster than when using the system copy of wiaaut.dll on Vista and later!

    Tested on WinXP SP3, Vista SP2, and Win7 SP1.


    Edit:

    In fact it runs incredibly faster using the redist DLL. Amazingly faster!

    I opened a 1000x1000 PNG with transparency and working with it was nearly impossibly slow using the system DLL under Vista SP2. On the same PC running this reg-free COM version using the redist DLL barely shows the hourglass cursor at all.
    Attached Files Attached Files
    Last edited by dilettante; Dec 2nd, 2015 at 08:00 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.