On Mon, 31 Mar 2003 17:13:58 -0500 "Josh Davis" <[EMAIL PROTECTED]> babbled:

> I am trying to create an XImage, but after the function call to 
> XCreateImage(), my XImage pointer's value is 0.  I really don't know much 
> about Xlib.  I want to create an XImage for image data that I already have.
> 
> Can someone give me a list of steps to follow that lead to a properly 
> created XImage?
> 
> I am currently getting the WindowAttributes from the root window and passing 
> its Visual* to the XCreateImage() function, while guessing at some of the 
> other values.  The image data that I have is for a 24-bit image.  I assume 
> that I should put the data into a contiguous char array.  Below is the 
> relevant portion of my code...
> --------------------------------------
> const int DEPTH = 24;

try DEPTH=32 (24 isn't really allowed)

> const int WIDTH = 100;
> const int HEIGHT = 100;
> const int FORMAT = ZPixmap;
> const int OFFSET = 0;
> const int BITMAP_PAD = 0;
> const int BYTES_PER_LINE = (WIDTH * 3);

try this:
XImage *
my_new_ximage(Display *display, Visual *visual, int width, int height, void
*data)
{
  /* this creates an ximage of the same depth as the specified visual */
  /* NOTE: Ximages must have pixels aligned to 8, 16 or 32bit boundaries. */
  /* 24bit (ie 3 bytes per pixel) is not valid and must be padded out to 32 */
  /* bits per pixel (4 bytes). The red, greeb and blue masks for your visual */
  /* will let you know what parst of the pixel are used for which color or */
  /* are padding. You really should use XPutPixel(ximage, x, y, value); to */
  /* set pixels if you want to not have portability problems. */
  /* If you don't you need to be VERY careful how you address the data of */
  /* the XImage *./
  XImage *ximage;
  XVisualInfo visualinfo, *visualinfo_return;
  int number_return;

  if ((width < 1) || (height < 1) || (!display) || (!visual)) return NULL;
  visualinfo.visualid = visual->visualid;
  visualinfo_return = XGetVisualInfo(display, VisualIDMask, &visualid,
                                     &number_return);
  /* no pointer return. abort */
  if (!visualinfo_return) return NULL;
  /* should only return 1 visual with that id */
  if (number_return != 1) return NULL;

  ximage = XCreateImage(display, visual, visualinfo_return[0].depth, ZPixmap, 
                        data, width, height, 32, 0);
  return ximage;
}

read the comments above. you can't do what you are doing as XImages cant be
24bit. you will have to either work with your data in 32 bit or have a 24bit to
32 bit converter. Also note that if you aren't running 32bpp (or 24bpp) you
can't just make 32bit XImages and have X do the color conversion fro you - you
need to handle each bit depth yourself. So if the server is running 16bpp or
15bpp or 8bpp or even monochrome this will simply not work. This is why there
are many libraries and toolkits that handle this work for you.

Several:
Imlib2
SDL
GTK+ (Gdk)
Qt
and others... I'll leave it to you to do the research, or do the hard work all
yourself. :)

> const int IMAGE_BYTES = WIDTH * HEIGHT * 3;
> 
> XWindowAttributes* rootWindowAttributes = new XWindowAttributes;
> 
> Visual* visual = rootWindowAttributes->visual;
> 
> char* data = new char[IMAGE_BYTES];
> 
> XImage* xImage = XCreateImage(display,
>                               visual,
>                               DEPTH,
>                               FORMAT,
>                               OFFSET,
>                               data,
>                               WIDTH,
>                               HEIGHT,
>                               BITMAP_PAD,
>                               BYTES_PER_LINE);
> --------------------------------------
> 
> Josh Davis
> 
> 
> _________________________________________________________________
> Protect your PC - get McAfee.com VirusScan Online  
> http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
> 
> _______________________________________________
> Devel mailing list
> [EMAIL PROTECTED]
> http://XFree86.Org/mailman/listinfo/devel


-- 
--------------- Codito, ergo sum - "I code, therefore I am" --------------------
The Rasterman (Carsten Haitzler)    [EMAIL PROTECTED]
                                    [EMAIL PROTECTED]
Mobile Phone: +61 (0)413 451 899    Home Phone: 02 9698 8615
_______________________________________________
Devel mailing list
[EMAIL PROTECTED]
http://XFree86.Org/mailman/listinfo/devel

Reply via email to