xlib-programming-Basic Graphics Programming With The Xlib Library

目录

What Is An X Bitmap? An X Pixmap?

Loading A Bitmap From A File

Drawing A Bitmap In A Window

Creating A Pixmap

Drawing A Pixmap In A Window

Freeing A Pixmap


One thing many so-called "Multi-Media" applications need to do, is display images. In the X world, this is done using bitmaps and pixmaps. We have already seen some usage of them when setting an icon for our application. Lets study them further, and see how to draw these images inside a window, along side the simple graphics and text we have seen so far.
One thing to note before delving further, is that Xlib supplies no means of manipulating popular image formats, such as gif, jpeg or tiff. It is left up to the programmer (or to higher level graphics libraries) to translate these image formats into formats that the X server is familiar with - x bitmaps, and x pixmaps.

What Is An X Bitmap? An X Pixmap?

An X bitmap is a two-color image stored in a format specific to the X window system. When stored in a file, the bitmap data looks like a C source file. It contains variables defining the width and height of the bitmap, an array containing the bit values of the bitmap (the size of the array = width * height), and an optional hot-spot location (will be explained later, when discussing mouse cursors).
A X pixmap is a format used to store images in the memory of an X server. This format can store both black and white images (such as x bitmaps) as well as color images. It is the only image format supported by the X protocol, and any image to be drawn on screen, should be first translated into this format.
In actuality, an X pixmap can be thought of as a window that does not appear on the screen. Many graphics operations that work on windows, will also work on pixmaps - just supply the pixmap ID instead of a window ID. In fact, if you check the manual pages, you will see that all these functions accept a 'Drawable', not a 'Window'. since both windows and pixmaps are drawables, they both can be used to "draw on" using functions such as XDrawArc(), XDrawText(), etc.

Loading A Bitmap From A File

We have already seen how to load a bitmap from a file to memory, when we demonstrated setting an icon for an application. The method we showed earlier required the inclusion of the bitmap file in our program, using the C pre-processor '#include' directive. We will see here how we can access the file directly.

/* this variable will contain the ID of the newly created pixmap. */
Pixmap bitmap;
/* these variables will contain the dimensions of the loaded bitmap. */
unsigned int bitmap_width, bitmap_height;
/* these variables will contain the location of the hot-spot of the */
/* loaded bitmap. */
int hotspot_x, hotspot_y;
/* this variable will contain the ID of the root window of the screen */
/* for which we want the pixmap to be created. */
Window root_win = DefaultRootWindow(display);
/* load the bitmap found in the file "icon.bmp", create a pixmap */
/* containing its data in the server, and put its ID in the 'bitmap' */
/* variable. */
int rc = XReadBitmapFile(display, root_win,"icon.bmp",&bitmap_width, &bitmap_height,&bitmap,&hotspot_x, &hotspot_y);
/* check for failure or success. */
switch (rc) {case BitmapOpenFailed:fprintf(stderr, "XReadBitmapFile - could not open file 'icon.bmp'.\n");break;case BitmapFileInvalid:fprintf(stderr,"XReadBitmapFile - file '%s' doesn't contain a valid bitmap.\n","icon.bmp");break;case BitmapNoMemory:fprintf(stderr, "XReadBitmapFile - not enough memory.\n");break;case BitmapSuccess:/* bitmap loaded successfully - do something with it... */break;
}

Note that the 'root_win' parameter has nothing to do with the given bitmap - the bitmap is not associated with this window. This window handle is used just to specify the screen that we want the pixmap to be created for. This is important, as the pixmap must support the same number of colors as the screen does, in order to make it useful.

Drawing A Bitmap In A Window

Once we got a handle to the pixmap generated from a bitmap, we can draw it on some window, using the XCopyPlane() function. This function allows us to specify what drawable (a window, or even another pixmap) to draw the given pixmap onto, and at what location in that drawable.

/* draw the previously loaded bitmap on the given window, at location */
/* 'x=100, y=50' in that window. we want to copy the whole bitmap, so */
/* we specify location 'x=0, y=0' of the bitmap to start the copy from, */
/* and the full size of the bitmap, to specify how much of it to copy. */
XCopyPlane(display, bitmap, win, gc,0, 0,bitmap_width, bitmap_height,100, 50,1);

As you can see, we could also copy a given rectangle of the pixmap, instead of the whole pixmap. Also note the last parameter to the XCopyPlane function (the '1' at the end). This parameter specifies which plane of the source image we want to copy to the target window. For bitmaps, we always copy plane number 1. This will become clearer when we discuss color depths below.

Creating A Pixmap

Sometimes we want to create an un-initialized pixmap, so we can later draw into it. This is useful for image drawing programs (creating a new empty canvas will cause the creation of a new pixmap on which the drawing can be stored). It is also useful when reading various image formats - we load the image data into memory, cerate a pixmap on the server, and then draw the decoded image data onto that pixmap.

/* this variable will store the handle of the newly created pixmap. */
Pixmap pixmap;
/* this variable will contain the ID of the root window of the screen */
/* for which we want the pixmap to be created. */
Window root_win = DefaultRootWindow(display);
/* this variable will contain the color depth of the pixmap to create. */
/* this 'depth' specifies the number of bits used to represent a color */
/* index in the color map. the number of colors is 2 to the power of */
/* this depth. */
int depth = DefaultDepth(display, DefaultScreen(display));
/* create a new pixmap, with a width of 30 pixels, and height of 40 pixels. */
pixmap = XCreatePixmap(display, root_win, 30, 40, depth);
/* just for fun, draw a pixel in the middle of this pixmap. */
XDrawPoint(display, pixmap, gc, 15, 20);

Drawing A Pixmap In A Window

Once we got a handle to pixmap, we can draw it on some window, using the XCopyArea() function. This function allows us to specify what drawable (a window, or even another pixmap) to draw the given pixmap onto, and at what location in that drawable.

/* draw the previously loaded bitmap on the given window, at location */
/* 'x=100, y=50' in that window. we want to copy the whole bitmap, so */
/* we specify location 'x=0, y=0' of the bitmap to start the copy from, */
/* and the full size of the bitmap, to specify how much of it to copy. */
XCopyArea(display, bitmap, win, gc,0, 0,bitmap_width, bitmap_height,100, 50);

As you can see, we could also copy a given rectangle of the pixmap, instead of the whole pixmap.
One important note should be made - it is possible to create pixmaps of different depths on the same screen. When we perform copy operations (a pixmap onto a window, etc), we should make sure that both source and target have the same depth. If they have a different depth, the operation would fail. The exception to this is if we copy a specific bit plane of the source pixmap, using the XCopyPlane() function shown earlier. In such an event, we can copy a specific plain to the target window - in actuality, setting a specific bit in the color of each pixel copied. This can be used to generate strange graphic effects in window, but is beyond the scope of out tutorial.

Freeing A Pixmap

Finally, when we are done using a given pixmap, we should free it, in order to free resources of the X server. This is done using the XFreePixmap() function:

/* free the pixmap with the given ID. */
XFreePixmap(display, pixmap);

After freeing a pixmap - we must not try accessing it again.
To summarize this section, take a look at the draw-pixmap.c program, to see a pixmap being created using a bitmap file, and then tiled up on a window on your screen.

X Window Bitmaps And Pixmaps相关推荐

  1. X Window Messing With The Mouse Cursor

    xlib-programming-Basic Graphics Programming With The Xlib Library 目录 Creating And Destroying A Mouse ...

  2. 2021年大数据Flink(十八):Flink Window操作

    目录 ​​​​​​​Flink-Window操作 为什么需要Window Window的分类 按照time和count分类 ​​​​​​​按照slide和size分类 ​​​​​​​总结 Window ...

  3. Ipython的Window与Linux详细安装

    IPython 是一个 python 的交互式 shell,支持补全等等一些强大的功能: IPython 为交互式计算提供了一个丰富的架构,包含: 强大的交互式 shell Jupyter 内核 交互 ...

  4. window 10 桌面显示计算机的操作

    点击设置--个性化设置--主题--桌面图标设置--勾选即可 (这个个性化设置可以点击window右键点击可以,这个看自己的习惯.win7的计算机在win10叫此电脑) 下面放一些图片

  5. [skill] vim 操作多个window

    前言: 分辨率越来越高,屏幕越来越大,行最长80不变,屏幕利用空白越来越大. 开多个window吧! 开window的命令: 平行开一个window :split <//path/file> ...

  6. How to set window title name on Oracle EBS Form?

    --1. 置換掉原來Winodw Property上的Title String ex. SET_WINDOW_PROPERTY('XXDII_INV_MISC_TXN_V',TITLE,:misc_t ...

  7. JS中window.event事件使用详解

    一.描述 event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有意义.比如:from ...

  8. js 打开窗口window.open

    js改变原有的地址 window.open(webPath+'/index?code='+code,'_self'); 转载于:https://www.cnblogs.com/hwaggLee/p/4 ...

  9. 事件绑定在IE下this是window的问题

    昨天写一个函数的时候,后来用了事件绑定,开始没在IE下测试,在chrome下都是没问题的.后来在IE下测试发现出错. 后来修改一下,发现oBox.οnclick=function(){}没问题,而ad ...

最新文章

  1. VS2015 win64下配置Opencv3.4.7
  2. Winform分页控件之纯分页显示处理
  3. C语言中的#ifndef、#def、#endif等宏
  4. SynchronousQueue队列
  5. JZOJ 2413. 【NOI2005】维护数列
  6. TOGAF:从业务架构到业务需求
  7. 共模电压和差模电压-(定义及测量)
  8. Lisp尺寸标注增加前后缀_求一CAD标注加前缀与后缀lisp
  9. aspx 修改了样式但是在点击按钮后被刷新_产品经理教程-Axure RP 9 基础操作(元件、布局、草图、样式)...
  10. strtotime php,php strtotime函数怎么用
  11. allego如何输出pdf_[LaTeX 尝试] 利用 PDF 附件和终端输出,从 Overleaf 里获得更多信息...
  12. 软工第一次个人作业博客(一)
  13. 大师级中国风复古景区网站设计及html前端源码
  14. vue 中引入阿里云的云盾防水墙
  15. 博途V15.1激活工具出错。
  16. Java利用数组求某年某日某月是某年的第几天(数组)
  17. Maven是主要干嘛的呢
  18. centos7查看磁盘io
  19. 内存卡不小心格式化后怎么找回丢失数据?
  20. 【贪心】Bin Packing

热门文章

  1. 2019.7.24循环结构以及昨天的预习题。
  2. Linux思维导图之inode、mv、cp和硬软链接
  3. Oracle 增加修改删除字段与添加注释
  4. 怎样查阅电脑最大能够扩充多大的内存
  5. python操作各种excel库
  6. Oracle sqlplus prelim 参数介绍
  7. 从fragment启动另一个fragment
  8. 清掉数据_值得收藏!面试中有哪些经典的数据库问题?
  9. 全球最顶级的电脑配置_全球最顶级外汇交易员,非这10位莫属
  10. linnux 流量控制模块tc_FS4008-40-08-CV-A气体质量流量计【汉川仪器】阿坝资讯