Equivalent of -colors 2.

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.
Post Reply
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Equivalent of -colors 2.

Post by rpatelob »

I'm using ImageMagick 7.0.5-5 Q16 x86_64

Code: Select all

convert -size 20x640 gradient: -rotate 90 -colors 2 -colorspace gray -auto-level test.jpg
C code

Code: Select all

    MagickWand * m_wand;
    PixelWand *PW1, **pixels;
    m_wand = NewMagickWand();
    PW1 = NewPixelWand();

    PixelSetColor(PW1,"none");
    MagickNewImage(m_wand, 20, 640, PW1);
    MagickSetImageMatte(m_wand, MagickTrue);
    MagickRotateImage(m_wand, PW1,90);
    MagickTransformImageColorspace(m_wand, GRAYColorspace);
    MagickAutoLevelImage(m_wand);
    PixelIterator *iterator = NULL;
    size_t x,y;
    int gray;
    char hex[128];
  	iterator=NewPixelIterator(m_wand);
  	for(y=0;y<20;y++) {
  		pixels=PixelGetNextIteratorRow(iterator,&x);
  		for(x=0;x<640;x++) {
  			gray = x*255/640;
  			sprintf(hex,"#%02x%02x%02x",gray,gray,gray);
        		PixelSetColor(pixels[x],hex);
  		}
  		PixelSyncIterator(iterator);
  	}
    MagickWriteImage(m_wand, "testing.jpg");
I'm trying to convert above CLI into C and there is my code. What I don't understand is -colors 2. What is the equivalent of it?
And another thing is I need to generate the gradient, and for that I have taken two loops according to image's dimentions and got the result. Is it a good solution? If not then Is there any other way to do it?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Equivalent of -colors 2.

Post by snibgo »

rpatelob wrote:What I don't understand is -colors 2. What is the equivalent of it?
I don't know, but MagickQuantizeImage() seems likely.
rpatelob wrote:And another thing is I need to generate the gradient, and for that I have taken two loops according to image's dimentions and got the result. Is it a good solution? If not then Is there any other way to do it?
Your solution will make a maximum of 256 different values, across an image that is 640 pixels wide. If you are using Q16, a correct gradient would have 640 different values.

If you are using Q8, having only 256 values is reasonable, but I don't think code should be dependant on a particular Q-number. Hint: I would use "QuantumRange" instead of "255".
snibgo's IM pages: im.snibgo.com
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Equivalent of -colors 2.

Post by rpatelob »

Thanks @snibgo. "MagickQuantizeImage" looks similar. I'll try that.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Equivalent of -colors 2.

Post by el_supremo »

This works with V6.9.7-10:

Code: Select all

    MagickSetSize(m_wand,20,640);
    MagickReadImage(m_wand,"gradient:white-black");
    MagickRotateImage(m_wand, PW1,90);

    // In V7 the 5th argument is a DitherMethod rather than a boolean - try NoDitherMethod
    MagickQuantizeImage(m_wand,2,GRAYColorspace,0,MagickTrue,MagickFalse);

    MagickAutoLevelImage(m_wand);
    MagickWriteImage(m_wand, "gradient_colors.jpg");
With V7, the 5th argument to MagickQuantizeImage can be one of NoDitherMethod, RiemersmaDitherMethod or FloydSteinbergDitherMethod. I presume that NoDitherMethod will work.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Equivalent of -colors 2.

Post by el_supremo »

I've just installed V7. NoDitherMethod doesn't work and the tree depth needs to be 1 instead of 0.

Code: Select all

	MagickQuantizeImage(m_wand,2,GRAYColorspace,1,FloydSteinbergDitherMethod,MagickFalse);
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Equivalent of -colors 2.

Post by rpatelob »

Hello @el_supremo,

The expected result is not the same. It looks like similar but not the same.
Please check two images

Using CLI
https://drive.google.com/file/d/0B-HZjm ... sp=sharing

Using C Code
https://drive.google.com/file/d/0B-HZjm ... sp=sharing
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Equivalent of -colors 2.

Post by rpatelob »

Hello @el_supremo,
One quick question, there are two ways to achieve gradient using PixelIterator and just using MagickReadImage(m_wand,"gradient:white-black");
Which one is the recommend solution for gradient?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Equivalent of -colors 2.

Post by snibgo »

rpatelob wrote:Please check two images

Using CLI
Using C Code
Yes, the images are different. Perhaps they used different dithering methods.

What was your command? What was your C code?
snibgo's IM pages: im.snibgo.com
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Equivalent of -colors 2.

Post by rpatelob »

snibgo wrote: 2017-05-23T06:28:00-07:00
Yes, the images are different. Perhaps they used different dithering methods.

What was your command? What was your C code?
Please check command and C code.

Code: Select all

convert -size 20x640 gradient: -rotate 90 -colors 2 -colorspace gray -auto-level test.jpg

Code: Select all

  MagickWand  * wand;
    PixelWand * PW1;
    size_t newHeight = 640, newWidth = 20;

    wand = NewMagickWand();
    PW1 = NewPixelWand();

    PixelSetColor(PW1,"none");
    MagickSetSize(wand,20,640);
    MagickReadImage(wand,"gradient:white-black");
    MagickRotateImage(wand, PW1, 90);
    MagickQuantizeImage(wand, 2, GRAYColorspace, 1, FloydSteinbergDitherMethod, MagickFalse);
    MagickAutoLevelImage(wand);
    MagickWriteImage(wand, "test1.jpg");
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Equivalent of -colors 2.

Post by snibgo »

There are only two dithering methods: FloydSteinberg and Riemersma. As I suspected, you have used different methods. So the results are different.
snibgo's IM pages: im.snibgo.com
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Equivalent of -colors 2.

Post by rpatelob »

snibgo wrote: 2017-05-23T07:00:30-07:00 There are only two dithering methods: FloydSteinberg and Riemersma. As I suspected, you have used different methods. So the results are different.
With RiemersmaDitherMethod I got the same result. Just a quick question. How do I know that which method I need to use?
Because in one of my examples I have tried all to get the result.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Equivalent of -colors 2.

Post by snibgo »

rpatelob wrote:With RiemersmaDitherMethod I got the same result.
In what? The command or C code?

Using your command, I used "-dither XXX" with the two methods. The two results I got were the same as your two results.
snibgo's IM pages: im.snibgo.com
rpatelob
Posts: 62
Joined: 2017-04-17T22:17:01-07:00
Authentication code: 1151

Re: Equivalent of -colors 2.

Post by rpatelob »

snibgo wrote: 2017-05-23T07:44:27-07:00 In what? The command or C code?
Using C code. The CLI result was perfect, I wanted the same in C. So the RiemersmaDitherMethod worked.
Post Reply