|
I’m trying to use ImageMagick for what I believed would be a simple task.
I want to generate a dimmed version of an image/icon at runtime in the application.
I use the magick++ c plus api.
I have found some commands that give me an ok result when run from the command line.
Converting the commands to the c++ api was a bit challenging, and the result is then not as hoped.
// Command example
convert -size 32x32 xc:"#999999" gray32.png
composite -dissolve 50% logo32.png gray32.png dim_logo32.png
How would this look in c++?
I came up with this.
Magick::Image gray;
gray.size( Magick::Geometry(image.columns(), image.rows()));
gray.read( "xc:#999999");
gray.label( "gray" );
if(gray.isValid()) {
gray.opacity(QuantumRange/2);
image.composite(gray, Magick::Geometry(image.columns(),image.rows()), Magick::DissolveCompositeOp );
But the transparency in the picture is lost.
A other suggestion as to make a dimmed image, is to make the full image semi transparent.
convert input.png -channel Alpha -evaluate Set 50% output.png
This could work. The transparency is kept when I tried this from command line.
Changing this to c++ api confused me a lot.
I ended up with this single line.
image.opacity(QuantumRange/ 2);
Now the result from this confuses me. The image is semi transparent, but the background that was originally transparent is now magenta.
orginal icon
dimmed icon
|
|
|
|
|
Thue Andersen wrote: Now the result from this confuses me. The image is semi transparent, but the background that was originally transparent is now magenta.
i suspect you've set the entire alpha channel to 128, not just reduced the exiting alpha by 1/2.
|
|
|
|
|
Aside from the transperency in the alpha channel, one pixel value can be chosen as fully transparent. This is often chosen to be magenta (R=255, G=0, B=255, A=255).
Now that you have modified the alpha component of every pixel, what was previously (R=255, G=0, B=255, A=255), is now (R=255, G=0, B=255, A=128). It's no longer recognised as fully transparent and instead treated as magenta at half transperency.
So to solve your problem, you'd have to either exclude the magenta (R=255, G=0, B=255, A=255) pixels from having their transperency halved or change the transparent pixel value to be (R=255, G=0, B=255, A=128).
modified on Friday, January 14, 2011 6:04 PM
modified 13-Sep-18 21:01pm.
|
|
|
|
|
So imagemagick internally uses magenta as transparent colour?
And when I changed the opacity of the whole image it also applied this for the transparent colour.
The two suggested solutions sound interesting,
I have tried to implement solution two.
image.opacity(MaxRGB / 2);
Magick::Color color(MaxRGB, 0, MaxRGB, MaxRGB / 2);
image.transparent(color);
But why is this exstra step necessary? In the command line we just set 50% alpha and done.
Would there be a better solution to create a dimmed icon?
|
|
|
|
|
Not an answer to your question, but what i don't understand is:
Why not render the dimmed icon offline (with the commandline) and read this icon in, during runtime? I guess you don't create icons in runtime on the fly, so what is the use to create a dimmed version in runtime?
|
|
|
|
|
Because the icons are defined by the end-user.
The system allows end-user to save his own icons into the database. To simplify this task he only has to provide one version of the icon, and we will generate the dimmed version automatically.
|
|
|
|
|
In the below code, I am attempting to count the number of letters and number of words in a phrase. In researching the return values of isalpha(), I noted that when a letter is present the first character isalpha() output is 1. But the second character isalpha() output is 2? The phrase that was input into this example was the following: How do you organize? Can someone help me with why I am not getting uniform output for the "H" character and the "o" character?
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
float avg = 0;
int spaces = 0;
int letters = 0;
int test = 0;
int ltrcnt = 0;
int spccnt = 0;
while((ch = getchar()) != '\n')
{
test = isalpha(ch);
if(isalpha(ch) == 2)
{
ltrcnt++;
}
if(isspace(ch) == 8)
{
spccnt++;
}
}
printf("The Number Of Words Are %d , The Number Of Letters Are %d ",(spccnt+1),ltrcnt);
}
|
|
|
|
|
isalpha returns zero if the given character is not an alphabet.
Returns non-zero if it is alphabet.
And may return '1' if the alphabet is of upper case, and '2' if it is lower case.
|
|
|
|
|
Yes, note the following defines from ctype.h
#define _UPPER 0x1 /* upper case letter */
#define _LOWER 0x2 /* lower case letter */
#define _DIGIT 0x4 /* digit[0-9] */
#define _SPACE 0x8 /* tab, carriage return, newline, */
#define _PUNCT 0x10 /* punctuation character */
#define _CONTROL 0x20 /* control character */
#define _BLANK 0x40 /* space char */
#define _HEX 0x80 /* hexadecimal digit */
#define _LEADBYTE 0x8000 /* multibyte leadbyte */
#define _ALPHA (0x0100|_UPPER|_LOWER) /* alphabetic character */
|
|
|
|
|
Andrew,
Ahh,
Thank you for directing me to the defines. Since I am an newbie, I have never looked in the header file. Thank you for the answer as well.
|
|
|
|
|
And you don't have to look there, what matters is the documentation: isalpha return a boolean result, either zero or non-zero; since the different non-zero values are not documented, you should not rely on them. Nor on any observation you may make, not in a .h file and not when running a test app.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Cool_Dev,
Thank you for the answer.
|
|
|
|
|
You should familiarise yourself with the MSDN pages[^]; they contain a wealth of useful information.
I must get a clever new signature for 2011.
|
|
|
|
|
Here is a sample code which works just fine:
#include <stdio.h>
#include <windows.h>
#define malloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define free(m) HeapFree(GetProcessHeap(), 0, m)
#pragma warning(disable: 4996)
wchar_t *test()
{
wchar_t aaa[] = L"aaaa,bbbb,cccc,dddd";
static wchar_t *lol = (wchar_t *)malloc(sizeof(wchar_t *) * wcslen(aaa) + 1);
wcscpy(lol, aaa);
return lol;
}
int main(int argc, char **argv)
{
wchar_t *t;
int i;
wchar_t *stuff = test();
t = wcstok(stuff, L",");
for(i = 0; t; t = wcstok(NULL, L","), i++)
{
wprintf(L"::%s\n", t);
}
return 0
}
This prints
::aaaa
::bbbb
::cccc
::dddd
Now, here is sample code which just dont work and i got no idea why. Its a wcstok inside of wcstok loop.
#include <windows.h>
#include <stdio.h>
#define malloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define free(m) HeapFree(GetProcessHeap(), 0, m)
#pragma warning(disable: 4996)
wchar_t *test()
{
wchar_t aaa[] = L"aaaa,bbbb,cccc,dddd";
static wchar_t *lol = (wchar_t *)malloc(sizeof(wchar_t *) * wcslen(aaa) + 1);
wcscpy(lol, aaa);
return lol;
}
wchar_t *lolz(wchar_t *lol)
{
int i = 2;
static wchar_t *stuff = (wchar_t *)malloc(sizeof(wchar_t *) * (wcslen(lol) + MAX_PATH)*i);
wcscpy(stuff, L" ");
while(i > 0)
{
wcscat(stuff, lol);
wcscat(stuff, L",");
i--;
}
return stuff;
}
int main(int argc, char **argv)
{
wchar_t *t;
int i;
wchar_t *stuff = test();
t = wcstok(stuff, L",");
for(i = 0; t; t = wcstok(NULL, L","), i++)
{
wprintf(L"::%s\n", t);
wchar_t *get = lolz(t);
wchar_t *t2;
int i2;
t2 = wcstok(get, L",");
for(i2 = 0; t2; t2 = wcstok(NULL, L","), i2++)
{
wprintf(L":%s\n", t2);
}
wprintf(L"End for: %s [%d]\n\n", t, i);
}
return 0;
}
this should print:
::aaaa
:aaaa
:aaaa
end for aaaa
::bbbb
:bbbb
:bbbb
end for bbbb
You can see from the code, like, for each main tokens it should get subtokens, but it prints only:
::aaaa
:aaaa
:aaaa
end for aaaa
and thats it. Why? thanks
011011010110000101100011011010000110100101101110
0110010101110011
modified on Thursday, January 13, 2011 10:19 AM
|
|
|
|
|
Did you try to use your debugger to see what's happening ?
Also, please use the code tags when pasting code, because it is very difficult to read right now.
|
|
|
|
|
Have wrapped the code with tags but it is all red now...
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Cedric Moonen wrote: please use the code tags
now that is confusing. What we want is HTML <PRE> tags, which you can type yourself or get by clicking the "code block" widget; we do not want <CODE> tags for a multi-line snippet!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
i have used exactly pre trags, not the code tags.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
I was only pointing out the wording of the advice was a bit confusing.
I have no doubt you did use PRE tags, you got the right font and background, I just don't know how you would get the red foreground color. You didn't have both PRE and CODE tags, did you?
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Nope. Just pre tags. That background is indeed strange thing. Maybe something with parsing is wrong, or i dont know...
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
did you escape the < signs properly, i.e. use < (which you get for free when pasting code while "Encode < characters when pasting" is checked)?
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Heh, that was it. Now code is properly highlighted. My bad :P
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
And that made me update my little tip article here[^].
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Yes right, I should have said "code block", I was typing too fast
|
|
|
|
|
Mixing wcstok calls for different strings does not work. See the Note here[^]. You need to use wcstok_s to work round this limitation.
Note: Please use <pre></pre> tags round your code rather than <code></code> - the "code block" button does it for you.
I must get a clever new signature for 2011.
|
|
|
|
|