Page 1 of 1
Equivalent of -colors 2.
Posted: 2017-05-09T04:14:12-07:00
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?
Re: Equivalent of -colors 2.
Posted: 2017-05-09T06:27:08-07:00
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".
Re: Equivalent of -colors 2.
Posted: 2017-05-09T07:18:20-07:00
by rpatelob
Thanks @snibgo. "MagickQuantizeImage" looks similar. I'll try that.
Re: Equivalent of -colors 2.
Posted: 2017-05-09T09:47:23-07:00
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
Re: Equivalent of -colors 2.
Posted: 2017-05-09T10:57:30-07:00
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
Re: Equivalent of -colors 2.
Posted: 2017-05-23T05:16:38-07:00
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
Re: Equivalent of -colors 2.
Posted: 2017-05-23T05:17:27-07:00
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?
Re: Equivalent of -colors 2.
Posted: 2017-05-23T06:28:00-07:00
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?
Re: Equivalent of -colors 2.
Posted: 2017-05-23T06:31:21-07:00
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");
Re: Equivalent of -colors 2.
Posted: 2017-05-23T07:00:30-07:00
by snibgo
There are only two dithering methods: FloydSteinberg and Riemersma. As I suspected, you have used different methods. So the results are different.
Re: Equivalent of -colors 2.
Posted: 2017-05-23T07:18:37-07:00
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.
Re: Equivalent of -colors 2.
Posted: 2017-05-23T07:44:27-07:00
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.
Re: Equivalent of -colors 2.
Posted: 2017-05-23T22:07:03-07:00
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.