Possible to fake indexed grayscale PNG's with alpha-mask?

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?".
Post Reply
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Possible to fake indexed grayscale PNG's with alpha-mask?

Post by pwnedd »

From what I've read so far, it looks like it is not possible to create 8-bit indexed grayscale PNG's with 1-bit alpha channels (e.g. http://en.wikipedia.org/wiki/Portable_N ... olor_depth). Does anyone know whether it's possible to "fake" this by either create indexed grayscale images where one pixel value (e.g. "black") is transparent? Or by specifying that one of the palette values is fully transparent?

My goal is to get from:

Image + Image + Image

8-bit Grayscale + 1-bit transparency mask + 8-bit RGB color-lookup table (The actual images I'm working with are PGM and TIF files, but I converted them to PNG's to post above)

To:
Image
8-bit Indexed PNG with some fully transparent parts.

I added a checkerboard to make it easier to see transparent parts. The above PNG is close to what I am trying to produce-- I would like to get something that is 8-bit + a palette and or alpha mask, rather than an RGB image. The problem is that when adding the mask the image is converted from grayscale + alpha channel to an 24-bit RGB image which takes a relatively long time.

Any Ideas? Any suggestions would be greatly appreciated.

Thanks!
Keith

-----------------------------------
Edit: Added example images
-----------------------------------
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible to fake indexed grayscale PNG's with alpha-mask?

Post by fmw42 »

try this test

create grayscale gradient:
convert -size 100x100 gradient: grad100.png

create square mask:
convert -size 50x50 xc:black -bordercolor white -border 25 mask.png

composite to make center square transparent:
convert grad100.png mask.png -compose copy_opacity -composite -type PaletteMatte tmp1.png

look at channels:
identify -verbose tmp1.png
Image: tmp3.png
Format: PNG (Portable Network Graphics)
Class: PseudoClass
Geometry: 100x100+0+0
Resolution: 72x72
Print size: 1.38889x1.38889
Units: Undefined
Type: GrayscaleMatte
Base type: GrayscaleMatte
Endianess: Undefined
Colorspace: RGB
Depth: 16-bit
Channel depth:
gray: 16-bit
alpha: 1-bit
Channel statistics:
gray:
min: 662 (0.0101015)
max: 65535 (1)
mean: 32773.1 (0.500085)
standard deviation: 19104.1 (0.29151)
kurtosis: -1.20087
skewness: 0.00181054
alpha:
min: 0 (0)
max: 65535 (1)
mean: 49151.2 (0.75)
standard deviation: 28377.5 (0.433013)
kurtosis: -0.666667
skewness: 1.1547

overlay over checkerboard to see where transparent:
convert -size 100x100 pattern:checkerboard tmp3.png -compose over -composite tmp2.png


if you are on 16-bit IM and want 8-bit image, then use:

convert grad100.png mask.png -compose copy_opacity -composite -depth 8 -type PaletteMatte tmp1.png

see -depth and -type at http://www.imagemagick.org/script/comma ... ptions.php
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Possible to fake indexed grayscale PNG's with alpha-mask?

Post by pwnedd »

Hi fmw,

Thanks for the reply. That is close to what I'm doing now except that I also apply a color lookup table, e.g.

Code: Select all

convert grad100.png mask.png -compose copy_opacity -composite -depth 8 -type PaletteMatte ctable.png -clut tmp.png
Which results in:

Code: Select all

Image: tmp.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 100x100+0+0
  Type: PaletteMatte
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
    Alpha: 1-bit
The problem is that I want to store the image as as grayscale, using the color-lookup table as it's index. The image only actually has at most 256 colors (+ an alpha channel). When IM applies the color-table, however, it produces a 24-bit RGB image with ~16 million possible colors.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Possible to fake indexed grayscale PNG's with alpha-mask?

Post by fmw42 »

pwnedd wrote:Hi fmw,

Thanks for the reply. That is close to what I'm doing now except that I also apply a color lookup table, e.g.

Code: Select all

convert grad100.png mask.png -compose copy_opacity -composite -depth 8 -type PaletteMatte ctable.png -clut tmp.png
Which results in:

Code: Select all

Image: tmp.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 100x100+0+0
  Type: PaletteMatte
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
    Alpha: 1-bit
The problem is that I want to store the image as as grayscale, using the color-lookup table as it's index. The image only actually has at most 256 colors (+ an alpha channel). When IM applies the color-table, however, it produces a 24-bit RGB image with ~16 million possible colors.
I don't believe that IM can store color look-up tables in the pseudocolor image type (header). But you had better get confirmation from Anthony or Magick.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Possible to fake indexed grayscale PNG's with alpha-mask?

Post by anthony »

The color lookup (-clut) can not only substitute colors byt transparencys too.
It is just a little trickier as you need to tell IM which image is the grayscale image
(and thus what you are trying to do), and that you also want to work with transparency.

See the examples at
http://www.imagemagick.org/Usage/colors/#clut_rgba

ADDENDUM: sorry mistake in example, fixed no but may take a day to appear.
Replace +alpha off with -alpha off in that last command. or wait a day or so.

So create your blue gradient replacement color image, then replace the first pixel (black replacment color) with a transparent color, and save it for use by your -clut replacement commands.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pwnedd
Posts: 35
Joined: 2008-09-03T13:03:57-07:00

Re: Possible to fake indexed grayscale PNG's with alpha-mask?

Post by pwnedd »

Hi Anthony, thanks for the reply.

I'm still a bit unclear about a couple things:

1. How should I replace the lookup table transparent pixel? Should I specify that the black pixel is transparent via it's alpha channel?

Code: Select all

convert ctable.png -transparent black ctable_transparent.png
Or specify the transparent color to use?

Code: Select all

convert ctable.png -transparent-color black ctable_transparent.png
Or something else?

2. More importantly, would the resulting image be any different than if I had used the unmodified gradient as a color lookup table, and -compose copy_opacity to specify the transparent pixels?

My ultimate goal is to try and speed up the process of generating an a 256-color + binary transparent (or 255 color + binary transparent) image from an 8-bit grayscale image, a 1x256 color lookup table and a binary mask. Right now the process works well, except that the color-lookup table step takes a lot of time.

Any ideas?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Possible to fake indexed grayscale PNG's with alpha-mask?

Post by anthony »

use -transparent to replace the color that would be assigned black.
Or just -draw point the none color into location 0
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply