MagickSetImageCompression not working

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

MagickSetImageCompression not working

Post by grant »

Hi,

Using C API I want to convert a .bmp file to a .bmp with the following properties:

color depth 8bits
colorspace grayScale
resolution 500x500 ppi
size 344x360
NoCompression

According to bitmap file format referenced in https://en.wikipedia.org/wiki/BMP_file_format:
The C API converted image shows at offset:
0x1C (biBitCount) = 8
0x1E (biCompression) = 1 (should be 0)

The following command line converts the image correctly.
magick convert tmp.bmp -resample 196x196 -resize 344x360 -grayscale Rec601Luma -depth 8 -alpha off -compress NONE -colors 256 tmp8_cmd.bmp
Our machine can read the image converted by command line and can not read the one converted by C API.
We have to convert using C API.

So the question is:
How to get the BiCompression to be 0? Or
How to convert the image using C API to get the same image as it was converted using above mentioned command line?


Following is the not working C API:

Code: Select all

#include <stdio.h>
#include <iostream> 
#include <tchar.h>
#include <windows.h>
#include <wand\magick_wand.h>


int _tmain(int argc, _TCHAR* argv[])
{
  MagickWand *l_wand = NULL;
  MagickWandGenesis();	
  l_wand = NewMagickWand();

  MagickReadImage(            l_wand, "D:\\JPG\\tmp.bmp");          
  MagickResizeImage(          l_wand, 344, 360, CatromFilter, 1.0);
  MagickSetImageUnits(        l_wand, PixelsPerInchResolution);  
  MagickSetImageResolution(   l_wand, 400, 400);  
  MagickSetImageDepth(        l_wand, 8);  
  MagickSetImageAlphaChannel( l_wand, DeactivateAlphaChannel);
  MagickSetImageCompression(  l_wand, NoCompression);
  MagickSetImageType(         l_wand, GrayscaleType);
  MagickQuantizeImage(        l_wand, 256, Rec601LumaColorspace, 0, 0, 0);
  MagickWriteImage(           l_wand, "D:\\JPG\\tmp8.bmp");
    
  if(l_wand) l_wand = DestroyMagickWand(l_wand);	
  MagickWandTerminus();
  return 0;
}
Windows 7 64bits
Version: ImageMagick 7.0.2-7 Q8 x64 2016-08-06 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

Thanks
Grant
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: MagickSetImageCompression not working

Post by snibgo »

If you are using v7, I'm surprised that program compiles. "wand" is a v6 directory. The v7 directory is "MagickWand". In my (admittedly old) v7.0.1-0 IM, there is no "magick_wand.h". And there are other problems.

Building your program as v6, it builds and runs, and exiftool reports the output is not compressed.
snibgo's IM pages: im.snibgo.com
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

Thanks for your reply.
You'r right. Now i have version 7 clean reinstalled (destroying all version 6 directories),
and use 'MagickWand\MagickWand.h'. The 'biCompression' is set now correctly to 0.
But the result image is 24 bit and not 8 bit as i explicitly used MagickSetImageDepth(wand, 8).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: MagickSetImageCompression not working

Post by snibgo »

"Depth" means bits per channel per pixel.
snibgo's IM pages: im.snibgo.com
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

Okay,
I will make a try to pass the image to our machine.
If it can process it then everything is ok.
Thanks.
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

Hi,
I am back again.
Our machine does not accept the 24bits image.
I found out some interresting docs in https://www.imagemagick.org/script/porting.php#imv7
It is stated there the following:

Code: Select all

Grayscale
Previously, grayscale images were Rec601Luminance and consumed 4 channels: red, green, blue, and alpha. 
With version 7, grayscale consumes only 1 channel requiring far less resources as a result.
And that is my target only 1 channel greyscale image.
My source code uses Imagetype 'GrayscaleType', bit depth 8 and colors 256.
This should create an 8 bit single channel image.
But it did not.
I have attached the exiftool output data.

The question here is:
How to convert to 8 bit, only 1 channel, grey scale, 256 colors?

Can you help me this way.

Code: Select all

#include <windows.h>

#pragma hdrstop
#pragma argsused

#include <MagickWand\MagickWand.h>

int _tmain(int argc, _TCHAR* argv[])
{    
  MagickWand *l_wand = NULL;  
  MagickWandGenesis();
  l_wand = NewMagickWand();
           
  MagickReadImage(            l_wand, "D:\\jpg\\tmp.bmp");  
  MagickSetImageDepth(        l_wand, 8);
  MagickSetImageType(         l_wand, GrayscaleType);
  MagickSetImageColorspace(   l_wand, GRAYColorspace);    
  MagickSetImageCompression(  l_wand, NoCompression);  
  MagickSetImageAlphaChannel( l_wand, DeactivateAlphaChannel);  
  MagickQuantizeImage(        l_wand, 256, GRAYColorspace, 0, MagickFalse, MagickFalse);  
  MagickResizeImage(          l_wand, 344, 360, CatromFilter);
  MagickSetImageUnits(        l_wand, PixelsPerInchResolution);
  MagickSetImageResolution(   l_wand, 400, 400);
  MagickWriteImage(           l_wand, "D:\\JPG\\tmp8.bmp");
    
  if(l_wand) l_wand = DestroyMagickWand(l_wand);	
	MagickWandTerminus();
  
	return 0;
}
ExifTool Version Number : 10.68

Code: Select all

File Name                       : tmp8.bmp
Directory                       : D:/JPG
File Size                       : 484 kB
File Modification Date/Time     : 2018:01:15 11:07:57+01:00
File Access Date/Time           : 2017:12:18 10:38:51+01:00
File Creation Date/Time         : 2017:12:18 10:38:51+01:00
File Permissions                : rw-rw-rw-
File Type                       : BMP
File Type Extension             : bmp
MIME Type                       : image/bmp
BMP Version                     : Windows V5
Image Width                     : 344
Image Height                    : 360
Planes                          : 1
Bit Depth                       : 32
Compression                     : Bitfields
Image Length                    : 495360
Pixels Per Meter X              : 15748
Pixels Per Meter Y              : 15748
Num Colors                      : Use BitDepth
Num Important Colors            : All
Red Mask                        : 0x00ff0000
Green Mask                      : 0x0000ff00
Blue Mask                       : 0x000000ff
Alpha Mask                      : 0xff000000
Color Space                     : sRGB
Rendering Intent                : Picture (LCS_GM_IMAGES)
Image Size                      : 344x360
Megapixels                      : 0.124
my version now is:
Version: ImageMagick 7.0.7-21 Q8 x86 2018-01-06 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype gslib jng jp2 jpeg lcms lqr openexr pangocairo png ps raw rsvg tiff webp xml zlib

thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: MagickSetImageCompression not working

Post by snibgo »

The porting guide refers to the number of channels in memory, not in files.

Resizing will almost certainly introduce new colours, so your quantize is in the wrong place.

Similarly, you set the image type before doing any processing. It should be at the end, just before writing.
snibgo's IM pages: im.snibgo.com
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

Do you mean that ImageMagick can not create 8 bit single channel 256 colors?
Then please look at the following command line: it creates correctly 8 bit single channel

Code: Select all

"C:\Program Files\ImageMagick-7.0.2-Q8\magick" tmp.bmp -resample 500x500 -resize 344x360 -grayscale Rec601Luma -depth 8 
-alpha off -compress NONE -colors 256 tmp8_cmd.bmp
Do you know if I could achieve the command line result using C API?

ExifTool Version Number : 10.68

Code: Select all

.....
File Name                       : tmp8_cmd.bmp
Bit Depth                       : 8
Compression                     : None
Image Length                    : 86400
Pixels Per Meter X              : 50000
Pixels Per Meter Y              : 50000
Num Colors                      : 256
Num Important Colors            : 256
Red Mask                        : 0x00ff0000
Green Mask                      : 0x0000ff00
Blue Mask                       : 0x000000ff
Alpha Mask                      : 0xff000000
Color Space                     : sRGB
Rendering Intent                : Picture (LCS_GM_IMAGES)
Image Size                      : 239x360
Megapixels                      : 0.086
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: MagickSetImageCompression not working

Post by snibgo »

Re-read my post. I told you how to do it.
snibgo's IM pages: im.snibgo.com
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

I rearranged the instruction as you worte but i get 24 bits instead of 8.
It does not work using C API but strangly works for command line.

Anyway thanks for your help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: MagickSetImageCompression not working

Post by snibgo »

Here is a corrected program (for IM v6, not 7):

Code: Select all

#include <windows.h>

// #pragma hdrstop
// #pragma argsused

#include "wand\MagickWand.h"

int main(int argc, char* argv[])
{    
  MagickWand *l_wand = NULL;  
  MagickWandGenesis();
  l_wand = NewMagickWand();
           
  MagickReadImage(            l_wand, "rose:");  
  MagickSetImageDepth(        l_wand, 8);
  //MagickSetImageType(         l_wand, GrayscaleType);
  MagickSetImageColorspace(   l_wand, GRAYColorspace);    
  MagickSetImageCompression(  l_wand, NoCompression);  
  MagickSetImageAlphaChannel( l_wand, DeactivateAlphaChannel);  
  MagickQuantizeImage(        l_wand, 256, GRAYColorspace, 0, MagickFalse, MagickFalse);  
  MagickResizeImage(          l_wand, 344, 360, CatromFilter, 1);
  MagickQuantizeImage(        l_wand, 256, GRAYColorspace, 0, MagickFalse, MagickFalse);  
  MagickSetImageUnits(        l_wand, PixelsPerInchResolution);
  MagickSetImageResolution(   l_wand, 400, 400);
  MagickSetImageType(         l_wand, GrayscaleType);
  MagickWriteImage(           l_wand, "tmp8.bmp");
    
  if(l_wand) l_wand = DestroyMagickWand(l_wand);	
	MagickWandTerminus();
  
	return 0;
}
It gives an 8-bit/channel, single channel result:

Code: Select all

ExifTool Version Number         : 10.68
File Name                       : tmp8.bmp
Directory                       : .
File Size                       : 149 kB
File Modification Date/Time     : 2018:01:15 15:33:45+00:00
File Access Date/Time           : 2018:01:15 13:08:39+00:00
File Creation Date/Time         : 2018:01:15 13:08:39+00:00
File Permissions                : rw-rw-rw-
File Type                       : BMP
File Type Extension             : bmp
MIME Type                       : image/bmp
BMP Version                     : Windows V5
Image Width                     : 344
Image Height                    : 360
Planes                          : 1
Bit Depth                       : 8
Compression                     : 8-Bit RLE
Image Length                    : 151042
Pixels Per Meter X              : 15748
Pixels Per Meter Y              : 15748
Num Colors                      : 256
Num Important Colors            : 256
Red Mask                        : 0x00ff0000
Green Mask                      : 0x0000ff00
Blue Mask                       : 0x000000ff
Alpha Mask                      : 0xff000000
Color Space                     : sRGB
Rendering Intent                : Picture (LCS_GM_IMAGES)
Image Size                      : 344x360
Megapixels                      : 0.124
snibgo's IM pages: im.snibgo.com
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

sorry but compression is set to : '8-Bit RLE' it must be set to 'None'
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: MagickSetImageCompression not working

Post by dlemstra »

You should use `MagickSetCompression` instead.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

Perfect, MagickSetCompression is the right one, now everything is ok.
I could convert the image to 8 bits single channel no compression.

Thank you for your help.
grant
Posts: 11
Joined: 2017-12-15T03:40:16-07:00
Authentication code: 1152

Re: MagickSetImageCompression not working

Post by grant »

The corrected program does not create 8 bits single channel any more.
Source code not changed.
Only i deinstalled all ImageMagick versions to reinstall V6 clean.

I do not understand why the corrected program suddenly create 24 bits images (8/hannel 3 channel).
Any idea what is gone wrong?

Code: Select all

Version: ImageMagick 6.9.9-27 Q8 x86 2017-12-16
Invalid Image created after i made reinstallation

Code: Select all

ExifTool Version Number         : 10.68
---- File ----
File Name                       : tmp8.bmp
Directory                       : .
File Size                       : 257 kB
File Modification Date/Time     : 2018:02:08 12:08:11+01:00
File Access Date/Time           : 2017:12:18 10:38:51+01:00
File Creation Date/Time         : 2017:12:18 10:38:51+01:00
File Permissions                : rw-rw-rw-
File Type                       : BMP
File Type Extension             : bmp
MIME Type                       : image/bmp
BMP Version                     : Windows V5
Image Width                     : 244
Image Height                    : 360
Planes                          : 1
Bit Depth                       : 24
Compression                     : None
Image Length                    : 263520
Pixels Per Meter X              : 19685
Pixels Per Meter Y              : 19685
Num Colors                      : Use BitDepth
Num Important Colors            : All
Red Mask                        : 0x00ff0000
Green Mask                      : 0x0000ff00
Blue Mask                       : 0x000000ff
Alpha Mask                      : 0xff000000
Color Space                     : sRGB
Rendering Intent                : Picture (LCS_GM_IMAGES)
---- Composite ----
Image Size                      : 244x360
Megapixels                      : 0.088
Valid Image created before i made reinstallation

Code: Select all

---- ExifTool ----
ExifTool Version Number         : 10.68
---- File ----
File Name                       : tmp8.bmp
Directory                       : .
File Size                       : 83 kB
File Modification Date/Time     : 2018:02:01 15:21:52+01:00
File Access Date/Time           : 2018:02:01 12:14:25+01:00
File Creation Date/Time         : 2018:02:01 11:43:06+01:00
File Permissions                : rw-rw-rw-
File Type                       : BMP
File Type Extension             : bmp
MIME Type                       : image/bmp
BMP Version                     : Windows V3
Image Width                     : 244
Image Height                    : 344
Planes                          : 1
Bit Depth                       : 8
Compression                     : None
Image Length                    : 83936
Pixels Per Meter X              : 19685
Pixels Per Meter Y              : 19685
Num Colors                      : 256
Num Important Colors            : 256
---- Composite ----
Image Size                      : 244x344
Megapixels                      : 0.084
Post Reply