LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2009, 05:09 AM   #1
anurag4u
LQ Newbie
 
Registered: Nov 2009
Location: India
Posts: 20

Rep: Reputation: 0
Question How to display gray colors in 24-bit using XWindows


Hi All,

Currently I have a very old application based on 8-bit color depth on Xsun on Solaris platform which displays gray images.

At present its working fine but I want to improve the image quality by going to 24-bit mode, so that I can get more shades of gray color.

I am a newbie in Xlib programming so don't know how to get gray color for my image in 24-bit color depth.

In 8-bit, I used to allocate <=256 pixels from the default colormap and then fill them with gray colors and store this pixel values in a array.

But in 24-bit, I am not sure what to do because it has 16 million colors, I can't allocate them all and can't store them in an array as it doesn't seem to be the logical solution.

I will appreciate if some one provides me the right pointers, to proceed further,as I am really stuck with this problem.


Regards,
Anurag

Last edited by anurag4u; 11-22-2009 at 01:25 AM.
 
Old 11-21-2009, 06:16 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
A 24-bit colour is made up of three 8-bit values, Red, Green & Blue. If I remember correctly they come in that order. For grey scale the Red, Green and Blue values will all be equal, hence a (128, 182, 128) triplet will be 50% grey. The (128, 182, 128) triplet will have a hex value of 808080.
 
Old 11-21-2009, 06:37 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
In addtion to what graemef says, there are only 256 shades of gray in a 24-bit color environment. Surprisingly, this is exactly equal to the number of grey shades you could have in an 8-bit monochrome system.

In this case it could suffice to create any array with 256 entries each containing a value with equal values vor R, G and B.

jlinkels
 
Old 11-22-2009, 01:54 AM   #4
anurag4u
LQ Newbie
 
Registered: Nov 2009
Location: India
Posts: 20

Original Poster
Rep: Reputation: 0
Question

graemef and jlinkels,

Thanks for your reply. I really appreciate your response on my issue.

But I am little confused when you say we can have only 256 shades of gray color even on 24-bit color depth. As per my understanding, I thought we will have more shades of colors in 24-bit as compared to 8-bit.

If I still get 256 gray colors in 24-bit, can I expect my image quality to improve.


Also, I am using Xlib/Xwindows calls to display my image, Can I have small program to understand it better as I am still not sure how to modify my code to make it work for 24-bit?

If required, I can provide litle more details on my implementation.

Anurag
 
Old 11-22-2009, 02:40 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You will only have 256 shades to choose from with the extremes being black and white. However you have the whole depth of different colours as well and certainly some colours that are not true greys may appear that way, for example (128,127,129) #807F81 is not a true grey because it will have more blue than green but it "looks" like a grey.

If you have access to kde look at the KColorChooser program to get an idea of how 24-bit colour works.
 
Old 11-22-2009, 07:16 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by anurag4u View Post
But I am little confused when you say we can have only 256 shades of gray color even on 24-bit color depth. As per my understanding, I thought we will have more shades of colors in 24-bit as compared to 8-bit.
In 24 bits your color triplets for gray are 010101, 020202, 030303... fefefe, ffffff. Those are really just 256 different values. There is no way to get more gray values, unless you want to try graemef's suggestion, which is mathematically not gray although it might improve your perception.

You get more shades of gray in 24-bit color than in 8-bit color, but the same as in 8-bit monochrome. I think the older Sun's were monochrome. In monochrome you have only one color value, and that happens to be gray (or green, or amber...) So your value is 01,02,03...fe,ff. Exactly the same as gray values in 24-bits.

Old Windows(TM) systems could only display 256 colors at the same time. Even then it was possible to have all of those color be a shade of gray, just by filling the palette only with equal values for R,G and B.

jlinkels
 
Old 11-22-2009, 07:17 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
oops, posted twice
 
Old 11-22-2009, 11:17 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
24BPP has the same amount of gray as 8BPP with an all gray palette! you need deep color if you want more.
 
Old 11-23-2009, 05:24 AM   #9
anurag4u
LQ Newbie
 
Registered: Nov 2009
Location: India
Posts: 20

Original Poster
Rep: Reputation: 0
Post

Quote:
If you have access to kde look at the KColorChooser program to get an idea of how 24-bit colour works.
graemef thanks for the suggestion but I don't know where to get the KDE code.

I trying searching it on the web, but couldn't get anything relavant.

I will be happy to know any other place where I can get this code or any sample code.
 
Old 11-23-2009, 05:56 AM   #10
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by anurag4u View Post
graemef thanks for the suggestion but I don't know where to get the KDE code.

I trying searching it on the web, but couldn't get anything relavant.

I will be happy to know any other place where I can get this code or any sample code.
It's right here.

Hope this helps.
 
Old 12-03-2009, 01:04 AM   #11
anurag4u
LQ Newbie
 
Registered: Nov 2009
Location: India
Posts: 20

Original Poster
Rep: Reputation: 0
Question

Hi,

Thanks for all your inputs, I got some theoretical idea about gray colors in 24-bit format.

Can anybody post a small code to show it using Xlib calls?

Basically, I want to get 256 different shades of gray colors, and then create array of pixels for these colors.

I guess we can use XParseColor or similar function for doing it, but how to do it for 256 different gray shades in loop.

I know for a single rgb string we can do something like this.

if(XParseColor(display, default_cmap, "rgb:EE/EE/EE", &fontcolor)){
XAllocColor(display, default_cmap,&fontcolor);
}

and then use fontcolor.pixel value to fill my pixel array.

but how to create different rgb strings from 00/00/00 to FF/FF/FF in a loop and pass it to XParseColor function.

I hope I am able to explain my problem clearly.

Thanks,
Anurag
 
Old 12-03-2009, 01:21 AM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
possibly
Code:
for (int i =0; i<256; ++i)
{
  colour = (((i*256)+i)*256)+i;
}
or maybe
Code:
for (int i =0; i<256; ++i)
{
  colour = (((i<<8)+i)<<8)+i;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Dark gray text on black background or psychedelic colors and 99% CPU in use w1k0 Slackware 17 10-22-2008 02:33 PM
Gray fonts look almost yellow in terminal emulators / How to display Asian languages vmlinuz.gz Linux - Newbie 1 04-05-2006 05:38 AM
16-bit color turns gray into pink! Lord Estraven Linux - Hardware 4 08-04-2005 11:07 AM
Colors messed up in Xwindows dabauer Linux - General 2 02-14-2005 03:52 PM
bash colors, wan't white, not gray king8 Red Hat 2 10-12-2003 11:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration