Page 1 of 2

Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-24T13:43:00-07:00
by VTT
I'm trying to convert pngs into bitmaps with premultiplied alpha to be used as Portable Executable resources with Power Shell script:

Code: Select all

$converter = "`"C:\Program Files\Image Magick\convert.exe`""
foreach($file_path in $args)
{
  $file_name = $file_path.substring(0, ($file_path.length - 4))
  $command = "& $converter `"$file_path`" `"(`" `"+clone`" `"-alpha`" `"Extract`" `")`" `"-channel`" `"RGB`" `"-compose`" `"Multiply`" `"-define`" `"bmp:format=bmp3`" `"-composite`" `"$file_name.bmp`""
  #echo $command
  Invoke-Expression -Command $command | Out-Default
}
It worked fine, however by default imagemagick saves as Bitmap V5 (bmpv4), which can not be used as resource (e.g. loaded with LoadImageW).
I've added `"-define`" `"bmp:format=bmp3`" so this script produces Bitmap v3 images. But they have only 24bpp and alpha is lost. i've tried adding `"-alpha`" `"Activate`" `"-depth`" `"32`" but it doesn't help.

PS i'm using 64-bit windows version ("recommended" download) which is actually broken.
I've copied some CORE_RL*.dll into "Image Magick\modules\coders\" to fix dll resolving issues.

Re: Save as Bitmap v4 (bmp3) preserving alpha channel.

Posted: 2015-11-24T13:45:09-07:00
by fmw42
For your output use BMP3:output.bmp

See http://www.imagemagick.org/Usage/formats/#bmp

Re: Save as Bitmap v4 (bmp3) preserving alpha channel.

Posted: 2015-11-24T13:59:23-07:00
by VTT
fmw42 wrote:For your output use BMP3:output.bmp
this approach gives me exactly the same bitmap as -define bmp:format=bmp3

Re: Save as Bitmap v4 (bmp3) preserving alpha channel.

Posted: 2015-11-24T14:44:44-07:00
by fmw42
BMP3 does not seem to allow transparency or at least IM does not allow it. From the reference above:

If you have an older program cannot read the default BMP4 images written by
ImageMagick, (for example a Windows Background Image), you can enforce the
generation of a BMP3 format image using...

convert image BMP3:image.bmp

This format should have no transparency and should be a 'printable image',
whatever that means. In other words 'Windows' compatible.

But I am not an expert on BMP3 nor a Windows user.

Re: Save as Bitmap v4 (bmp3) preserving alpha channel.

Posted: 2015-11-24T14:50:13-07:00
by fmw42
See also http://www.fileformat.info/format/bmp/egff.htm, which seems to imply that BMP3 does not allow transparency

Re: Save as Bitmap v4 (bmp3) preserving alpha channel.

Posted: 2015-11-24T14:59:43-07:00
by VTT
It seems to be a bug since imagemagick correctly saves alpha in BMP4 while BMP3 definitely does allow transparency or at least 32 bpp (exactly as BMP4).

And it is actually stated later on that page:

For comparison, the bmp3: format allows bit depths of 0, 1, 4, 8 ,16, 24 and 32 and has extra fields which specify x and y resolution (in pixels per metre) and compression of the image data.

The question is how to force imagemagick to save alpha channel correctly (like most of other image editing software does).

Re: Save as Bitmap v4 (bmp3) preserving alpha channel.

Posted: 2015-11-24T16:45:49-07:00
by fmw42
The fileformat reference does not appear to me to allow transparency in any bmp format but BMP4.

It says for BMP3

"BitsPerPixel is the number of bits in each pixel. This value is in the range 1 to 24; the values 1, 4, 8, and 24 are the only values considered legal by the Windows 3.x API.'

The IM document at http://www.imagemagick.org/Usage/formats/#bmp appears to wrong. BMP3 does not allow 32-bit depth and 0-bit depth makes no sense to me.

I do not think that IM supports the BMP Version 3 for Microsoft Windows NT, where the
Windows NT version uses a variation of the Windows 3.x BMP format to store 16- and 32-bit data in a BMP file.

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-24T23:40:27-07:00
by VTT
MSDN page states that
Alpha values per pixel are only supported for 32-bpp BI_RGB.
Anyway, v3 bitmaps with alpha are used everywhere.

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-25T00:07:50-07:00
by fmw42
According to the reference for BMP file format, only Microsoft Windows NT version of BMP3 or normal BMP4 support 32-bits per pixel. I do not think IM supports that version of BMP3. I am not an expert on this matter. I am only relating what is in the file format document.

One of the IM developers will have to let you know more.

If you can find a document that shows that BMP3 (in general) supports 32-bits per pixel and what the file structure is, that would help.

See also
viewtopic.php?t=16259

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-25T01:06:32-07:00
by VTT
BMP3 which is BITMAPFILEHEADER + BITMAPINFOHEADER can hold alpha channel, biBitCount need to be 32 and biCompression BI_RGB as stated here.

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-25T05:15:24-07:00
by dlemstra
I just did a quick test and I have the feeling that this is not fully supported by Windows. The following image is created with the options that you requested: https://www.dropbox.com/s/v0y3izvhrbmwgj4/logo.bmp?dl=0. Can you verify that this works correctly now?

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-25T12:06:02-07:00
by VTT
This image seem to have monochrome alpha (either 0 or 255) but is fine otherwise.

I admit that it is not fully supported and some applications tend to ignore saved alpha, but it works good as a resource file.

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-25T14:55:57-07:00
by dlemstra
I don't think we can make this the default behavior but we could add an option to enable writing the alpha channel in BMP3 files. For example this:

Code: Select all

convert logo: -define bmp3:write-alpha=true bmp3:logo.bmp
Please let me know if you have better suggestions for the name of the option.

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-25T16:03:28-07:00
by fmw42
Other defines might be

-define bmp3:alpha=on|save|true

-define bmp3:transparency=on|save|true

I don't really care, just trying to avoid a hyphenated word.

Re: Save Bitmap as bmp3 preserving alpha channel.

Posted: 2015-11-26T02:18:58-07:00
by VTT
I 'm not really familiar with command style in IM, is there a reason why explicit

Code: Select all

-depth 32
is not going to work?