LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 07-02-2005, 11:25 AM   #1
BBB
Member
 
Registered: Aug 2004
Location: sweden
Distribution: Slackware 10.2 and Win2k
Posts: 127

Rep: Reputation: 15
how to draw color images with xlib?


I know this is possible, i have seen alot of programs do it, but how?
As far as i know (correct me if im wrong) Xlib is pure pallet graphics so does
that mean that i have to allocate every color in a say .tga image file
in order to draw it? I have no internet at the moment (just moved),
only a very old Xlib programming book which doesn't discus color
images.

Thx // BBB
 
Old 07-03-2005, 04:57 AM   #2
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Rep: Reputation: 16
Have a look,
users.actcom.co.il/~choo/lupg/tutorials/ xlib-programming/xlib-programming.html
and here,
tronche.com/gui/x/xlib-tutorial/
hope it helped!
 
Old 07-05-2005, 10:17 AM   #3
BBB
Member
 
Registered: Aug 2004
Location: sweden
Distribution: Slackware 10.2 and Win2k
Posts: 127

Original Poster
Rep: Reputation: 15
No actually it didn't . I already have one of those and it only covers black and white images. Can't anyone post some code?
 
Old 07-05-2005, 12:09 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
only a very old Xlib programming book which doesn't discus color
images
Hmm, Xlib (X11) has been supporting color since its first release ...
Quote:
Xlib is pure pallet graphics
Wrong, Xlib supports both indexeded and true color visuals.
Quote:
so does that mean that i have to allocate every color in a say .tga image file in order to draw it?
Hopefully, you haven't to.
How would you allocate the millions of colors a 24 bit picture can potentially contain ?
Quote:
Can't anyone post some code?
Have a look at the xloadimage source for sample code.
 
Old 07-06-2005, 08:39 AM   #5
BBB
Member
 
Registered: Aug 2004
Location: sweden
Distribution: Slackware 10.2 and Win2k
Posts: 127

Original Poster
Rep: Reputation: 15
Finally I got the answer i wanted, thx!
 
Old 07-09-2005, 05:08 PM   #6
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 30
BBB -

I was wondering the samething and I am looking at the xloadimage/xli source right now. Which file(s) did you find the solution in?

Thanks
 
Old 07-09-2005, 05:20 PM   #7
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 30
I think I found the solution in send.c. It looks like it allocates 2^24 seperate colors, but I'm not sure. Can you share your thoughts?
 
Old 07-09-2005, 06:08 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
There's never a full 2^24 size colormap, that would be nonsense, at least until we have more than 4096 x 4096 screens.
TrueColor visuals have 3 colormaps, each 256 entries size, one for each primary color.
 
Old 07-09-2005, 07:08 PM   #9
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 30
Quote:
Originally posted by jlliagre
TrueColor visuals have 3 colormaps, each 256 entries size, one for each primary color.
(Scratches head) Then how is a Color created. For simplicity's sake: I have a color #336699. Therefore it will need R[51], G[102], B[153]. But doesnt the color need to be allocated with all three of these values?

I guess I'm just confused. The concept of allocating colors is still a bit odd to me.

Maybe it would help if I explain what I intend to do. I am making a small widget toolkit for flatpanel displays. Currently everything works fine, but it would be nice to test applications in an X session rather than having to have a flat panel attached to one of the computer's ports.

There is a Canvas class and a Screen class. For the X implementation, Canvas basically wraps an XImage and provides functions like DrawLine, DrawRect, etc. A Canvas is bound to a particular Screen. The Screen wraps a drawable such as a Window. Widgets are drawn through the canvas. Somehow I need either the Screen or Canvas to allocate colors, either initially or dynamically. I'm not sure where to start. Most of the resources online don't cover X's color model very well.

Last edited by AngryLlama; 07-09-2005 at 08:32 PM.
 
Old 07-10-2005, 10:44 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
If you want to display a color image to an X11 window, you do not need to allocate every color present in the image, just use a true color visual, and pass it your image.

If you need to use X11 primitives (lines, circles, characters, ...), you neither need to allocate a color to be able to draw with it, just pick its wanted rgb value, and set it to the graphic context you use.

Here is a sample code showing a way to do it:
Code:
#include     <stdio.h>
#include     <stdlib.h>
#include     <string.h>
#include     <X11/Xlib.h>

XImage *CreateTrueColorImage(Display *display, Visual *visual, unsigned char *image, int width, int height)
{
    int i, j;
    unsigned char *image32=(unsigned char *)malloc(width*height*4);
    unsigned char *p=image32;
    for(i=0; i<width; i++)
    {
        for(j=0; j<height; j++)
        {
            if((i<256)&&(j<256))
            {
                *p++=rand()%256; // blue
                *p++=rand()%256; // green
                *p++=rand()%256; // red
            }
            else
            {
                *p++=i%256; // blue
                *p++=j%256; // green
                if(i<256)
                    *p++=i%256; // red
                else if(j<256)
                    *p++=j%256; // red
                else
                    *p++=(256-j)%256; // red
            }
            p++;
        }
    }
    return XCreateImage(display, visual, 24, ZPixmap, 0, image32, width, height, 32, 0);
}

void processEvent(Display *display, Window window, XImage *ximage, int width, int height)
{
    static char *tir="This is red";
    static char *tig="This is green";
    static char *tib="This is blue";
    XEvent ev;
    XNextEvent(display, &ev);
    switch(ev.type)
    {
    case Expose:
        XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, 0, 0, width, height);
        XSetForeground(display, DefaultGC(display, 0), 0x00ff0000); // red
        XDrawString(display, window, DefaultGC(display, 0), 32,     32,     tir, strlen(tir));
        XDrawString(display, window, DefaultGC(display, 0), 32+256, 32,     tir, strlen(tir));
        XDrawString(display, window, DefaultGC(display, 0), 32+256, 32+256, tir, strlen(tir));
        XDrawString(display, window, DefaultGC(display, 0), 32,     32+256, tir, strlen(tir));
        XSetForeground(display, DefaultGC(display, 0), 0x0000ff00); // green
        XDrawString(display, window, DefaultGC(display, 0), 32,     52,     tig, strlen(tig));
        XDrawString(display, window, DefaultGC(display, 0), 32+256, 52,     tig, strlen(tig));
        XDrawString(display, window, DefaultGC(display, 0), 32+256, 52+256, tig, strlen(tig));
        XDrawString(display, window, DefaultGC(display, 0), 32,     52+256, tig, strlen(tig));
        XSetForeground(display, DefaultGC(display, 0), 0x000000ff); // blue
        XDrawString(display, window, DefaultGC(display, 0), 32,     72,     tib, strlen(tib));
        XDrawString(display, window, DefaultGC(display, 0), 32+256, 72,     tib, strlen(tib));
        XDrawString(display, window, DefaultGC(display, 0), 32+256, 72+256, tib, strlen(tib));
        XDrawString(display, window, DefaultGC(display, 0), 32,     72+256, tib, strlen(tib));
        break;
    case ButtonPress:
        exit(0);
    }
}

int main(int argc, char **argv)
{
    XImage *ximage;
    int width=512, height=512;
    Display *display=XOpenDisplay(NULL);
    Visual *visual=DefaultVisual(display, 0);
    Window window=XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, width, height, 1, 0, 0);
    if(visual->class!=TrueColor)
    {
        fprintf(stderr, "Cannot handle non true color visual ...\n");
        exit(1);
    }

    ximage=CreateTrueColorImage(display, visual, 0, width, height);
    XSelectInput(display, window, ButtonPressMask|ExposureMask);
    XMapWindow(display, window);
    while(1)
    {
        processEvent(display, window, ximage, width, height);
    }
}
 
Old 07-10-2005, 12:11 PM   #11
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 30
Thanks alot for the code sample, that helped out a bunch.

So the only times colormaps and pixel allocation is needed is when using Images under 24bit (32bit)? So, if I want to support 8bit images/displays I will need to create a bunch of fallbacks?
 
Old 07-10-2005, 02:19 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Colormaps (lookup tables) are used with R/W visuals (DirectColor, PseudoColor and GrayScale).
There are no real lookup tables when R/O visuals are used (TrueColor, StaticColor, StaticGray).

In both modes, you can allocate a color whan you want to use it, but you only need to do it with R/W visuals.

The display depth is less and less an issue nowadays, as 24 bit tend to be the norm. Anyway, you may still be able to use true color with 8 bits displays (3:2:2) when available, but the result is usually disappointing.
 
Old 07-10-2005, 02:55 PM   #13
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 30
Ok thanks,

My question is: do I need to allocate colors if I want to use an 8 bit image on a 24/32 bit display? Regardless, you've provided me with more information then I could obtain elsewhere.
 
Old 07-11-2005, 12:41 AM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,507

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
do I need to allocate colors if I want to use an 8 bit image on a 24/32 bit display?
Yes, unless your 8 bit image is already a 3:3:2 true color one (unlikely).
If your 8 bit image has a lookup table, you need to allocate each one of these colors before displaying it.
Alternatively, you can also convert your image to 24 bit true color, in that case no need to allocate any color.
 
Old 07-13-2005, 01:30 PM   #15
AngryLlama
Member
 
Registered: Sep 2004
Location: /dev/urandom
Distribution: Gentoo
Posts: 171

Rep: Reputation: 30
Great thanks,

Is there anyway to support transparency in or with the XImage? I either need a function like XPutImage wish will support an XImage with transparency. Or, I need a way to use a separate Transparency mask. Is there another data structure I can use besides XImage? All I need is simple 1bit transparency, no need for alpha blending.

Another option is to use XGetImage to get the image from the window then draw ontop of that. Then, actually draw the image back to the window. The problem with this is that I need to reformulate all of my images whenever the underlaying window changes.

If there isn't a way around this then I guess I will have to write my own rasterizer using XDrawPoints (it will be slow as heck). I've been looking around the forums, google, and man pages and the outlook doesn't look too good.

Last edited by AngryLlama; 07-13-2005 at 01:48 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Xlib: connection to ":0.0" refused by server Xlib: No protocol specified eyalkz Linux - Newbie 10 02-07-2014 12:53 AM
Xlib and images question SciYro Programming 2 06-14-2005 12:29 PM
[COLOR=red]ENCORE WIRELESS PCI Card 802.11b [/COLOR] enrique145 Linux - Hardware 3 07-07-2004 02:31 PM
Xlib: connection to ":0.0" refused by server Xlib: No protocol specified eyalkz Programming 1 03-02-2004 08:22 AM
[COLOR=seagreen]HELP-want to use Kickstart/rh7.2 on a notebook[/COLOR] verbali Linux - Newbie 1 05-13-2002 01:59 PM


All times are GMT -5. The time now is 06:15 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration