1-bit PNG with transparent background (C++)

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
loremipsum

1-bit PNG with transparent background (C++)

Post by loremipsum »

Hi!

I'm trying to save an image in PNG format with 1 bit per Pixel and a transparent background. I can open such an image, but when I save it, no matter what I try, it either gets saved with 32 bits or with an opaque background.

Thanks for your help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 1-bit PNG with transparent background (C++)

Post by fmw42 »

I am not sure IM can handle that. See IM PNG formats at http://www.imagemagick.org/Usage/formats/#png_formats
loremipsum

Re: 1-bit PNG with transparent background (C++)

Post by loremipsum »

Well, I had seen that page before, but it doesn't mention 1-bit PNGs (PNG1?) at all (except for that last line with "-type BiLevel" maybe...) And since I could open such a file just fine, I thought it should be possible to save it as well.

Anyway, I need to scale the image and save it in this format because it will be used by another program that relies on the transparent background. Do you know of another C++ library (or a command line program) that can do this?

If all else fails I might have to access the raw pixels and implement the save function myself, but I don't know how complex the PNG format is...

Again thanks for your help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 1-bit PNG with transparent background (C++)

Post by fmw42 »

This seems to work:

Make transparent png
convert logo: -transparent white logot.png

convert to 1bit graylevel with transparency
convert logot.png -monochrome PNG32:logotm.png


identify -verbose logotm.png
Image: logotm.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 640x480+0+0
Resolution: 72x72
Print size: 8.88889x6.66667
Units: Undefined
Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: RGB
Depth: 8/1-bit
Channel depth:
gray: 1-bit
alpha: 1-bit
Channel statistics:
gray:
min: 0 (0)
max: 255 (1)
mean: 3.37012 (0.0132161)
standard deviation: 29.1208 (0.114199)
kurtosis: 70.6784
skewness: 8.52516
alpha:
min: 0 (0)
max: 255 (1)
mean: 44.6997 (0.175293)
standard deviation: 96.9555 (0.380217)
kurtosis: 0.917287
skewness: -1.70801
loremipsum

Re: 1-bit PNG with transparent background (C++)

Post by loremipsum »

Thanks that put me in the right direction. The following code is working:

Code: Select all

Image img;
img.read(<filename>);
img.sample(Geometry(new_width, new_height));
ImageType type = img.type(); // (1)
img.type(PaletteMatteType);  // (2)     
img.write(<filename>);
I'm still a little confused though. (1) returns PaletteMatteType, but without (2) it does not work. What's more confusing is that "identify" gives almost the same results for both files. The only differences are the timestamps (obviously) and the file size.

Code: Select all

identify -verbose wrong.png
Image: wrong.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 760x47+0+0
  Resolution: 37.79x37.79
  Print size: 20.1111x1.24372
  Units: PixelsPerCentimeter
  Type: PaletteMatte
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    red: 1-bit
    green: 8-bit
    blue: 8-bit
    alpha: 1-bit
  Channel statistics:
    red:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
    green:
      min: 0 (0)
      max: 120 (0.470588)
      mean: 99.6652 (0.390844)
      standard deviation: 45.0186 (0.176544)
      kurtosis: 1.10524
      skewness: -1.76217
    blue:
      min: 0 (0)
      max: 1 (0.00392157)
      mean: 0.830543 (0.00325703)
      standard deviation: 0.375155 (0.0014712)
      kurtosis: 1.10524
      skewness: -1.76217
    alpha:
      min: 0 (0)
      max: 255 (1)
      mean: 43.2115 (0.169457)
      standard deviation: 95.6645 (0.375155)
      kurtosis: 1.10524
      skewness: -1.76217
  Image statistics:
    OverAll:
      min: 0 (0)
      max: 255 (1)
      mean: 78.0711 (0.306161)
      standard deviation: 101.964 (0.399858)
      kurtosis: -0.914262
      skewness: 0.840331
  Alpha: rgba(0,120,1,0)   #00780100
  Histogram:
     29667: (  0,120,  1,  0) #00780100 rgba(0,120,1,0)
      6053: (  0,  0,  0,255) #000000 black
  Rendering intent: Saturation
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Interlace: None
  Background color: black
  Border color: rgba(223,223,223,1)
  Matte color: grey74
  Transparent color: none
  Page geometry: 760x47+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    create-date: 2009-04-04T18:31:06+00:00
    modify-date: 2009-04-04T18:28:03+00:00
    signature: bf96d95f680e3156078e6bc59f515a9f691bc05ecc10260257ac815e5ad93623
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 1.54kb
  Number pixels: 34.9kb
  Version: ImageMagick 6.5.0-4 2009-03-20 Q16 http://www.imagemagick.org

Code: Select all

identify -verbose right.png
Image: right.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 760x47+0+0
  Resolution: 37.79x37.79
  Print size: 20.1111x1.24372
  Units: PixelsPerCentimeter
  Type: PaletteMatte
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    red: 1-bit
    green: 8-bit
    blue: 8-bit
    alpha: 1-bit
  Channel statistics:
    red:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
    green:
      min: 0 (0)
      max: 120 (0.470588)
      mean: 99.6652 (0.390844)
      standard deviation: 45.0186 (0.176544)
      kurtosis: 1.10524
      skewness: -1.76217
    blue:
      min: 0 (0)
      max: 1 (0.00392157)
      mean: 0.830543 (0.00325703)
      standard deviation: 0.375155 (0.0014712)
      kurtosis: 1.10524
      skewness: -1.76217
    alpha:
      min: 0 (0)
      max: 255 (1)
      mean: 43.2115 (0.169457)
      standard deviation: 95.6645 (0.375155)
      kurtosis: 1.10524
      skewness: -1.76217
  Image statistics:
    OverAll:
      min: 0 (0)
      max: 255 (1)
      mean: 78.0711 (0.306161)
      standard deviation: 101.964 (0.399858)
      kurtosis: -0.914262
      skewness: 0.840331
  Alpha: rgba(0,120,1,0)   #00780100
  Histogram:
     29667: (  0,120,  1,  0) #00780100 rgba(0,120,1,0)
      6053: (  0,  0,  0,255) #000000 black
  Rendering intent: Saturation
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Interlace: None
  Background color: black
  Border color: rgba(223,223,223,1)
  Matte color: grey74
  Transparent color: none
  Page geometry: 760x47+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    create-date: 2009-04-04T18:31:52+00:00
    modify-date: 2009-04-04T18:31:45+00:00
    signature: bf96d95f680e3156078e6bc59f515a9f691bc05ecc10260257ac815e5ad93623
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 1020b
  Number pixels: 34.9kb
  Version: ImageMagick 6.5.0-4 2009-03-20 Q16 http://www.imagemagick.org
loremipsum

Re: 1-bit PNG with transparent background (C++)

Post by loremipsum »

Actually, that only sometimes works. With that same code, but a different image, I get a completely (!) transparent resized image. Identify then outputs the following:

Code: Select all

 
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 215x13+0+0
  Resolution: 28.35x28.35
  Print size: 7.58377x0.458554
  Units: PixelsPerCentimeter
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8/1-bit
  Channel depth:
    gray: 1-bit
    alpha: 1-bit
  Channel statistics:
    gray:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
    alpha:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
  Alpha: none   #00000000
  Histogram:
      2795: (  0,  0,  0,  0) #00000000 none
  Rendering intent: Undefined
  Interlace: None
  Background color: black
  Border color: rgba(223,223,223,1)
  Matte color: grey74
  Transparent color: none
  Page geometry: 215x13+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    Comment: Created with The GIMP
    create-date: 2009-04-04T20:52:52+00:00
    modify-date: 2009-04-04T20:53:02+00:00
    signature: c7f5e291ad01bee07947171ee740585b0d510b770f19dec2d5f99426ee5b1da5
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 295b
  Number pixels: 2.73kb
  Version: ImageMagick 6.5.0-4 2009-03-20 Q16 http://www.imagemagick.org
Post Reply