Save Bitmap as bmp3 preserving alpha channel.

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
VTT
Posts: 8
Joined: 2015-11-24T13:14:45-07:00
Authentication code: 1151

Save Bitmap as bmp3 preserving alpha channel.

Post 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.
Last edited by VTT on 2015-11-24T23:20:01-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

For your output use BMP3:output.bmp

See http://www.imagemagick.org/Usage/formats/#bmp
VTT
Posts: 8
Joined: 2015-11-24T13:14:45-07:00
Authentication code: 1151

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

Post by VTT »

fmw42 wrote:For your output use BMP3:output.bmp
this approach gives me exactly the same bitmap as -define bmp:format=bmp3
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

See also http://www.fileformat.info/format/bmp/egff.htm, which seems to imply that BMP3 does not allow transparency
VTT
Posts: 8
Joined: 2015-11-24T13:14:45-07:00
Authentication code: 1151

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

Post 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).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
VTT
Posts: 8
Joined: 2015-11-24T13:14:45-07:00
Authentication code: 1151

Re: Save Bitmap as bmp3 preserving alpha channel.

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Save Bitmap as bmp3 preserving alpha channel.

Post 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
VTT
Posts: 8
Joined: 2015-11-24T13:14:45-07:00
Authentication code: 1151

Re: Save Bitmap as bmp3 preserving alpha channel.

Post by VTT »

BMP3 which is BITMAPFILEHEADER + BITMAPINFOHEADER can hold alpha channel, biBitCount need to be 32 and biCompression BI_RGB as stated here.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Save Bitmap as bmp3 preserving alpha channel.

Post 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?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
VTT
Posts: 8
Joined: 2015-11-24T13:14:45-07:00
Authentication code: 1151

Re: Save Bitmap as bmp3 preserving alpha channel.

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Save Bitmap as bmp3 preserving alpha channel.

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Save Bitmap as bmp3 preserving alpha channel.

Post 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.
VTT
Posts: 8
Joined: 2015-11-24T13:14:45-07:00
Authentication code: 1151

Re: Save Bitmap as bmp3 preserving alpha channel.

Post 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?
Post Reply