如何将图像显示在 XCB 窗口中?

15

我无法在XCB窗口中显示图像(使用libpng提取的PNG格式),它总是完全空白/白色。 我相信PNG提取是正确的,因为我可以将其完美地重写到另一个文件中。

我尝试了我能找到的一切(说明、指南、文档),但已经没有任何想法:

  • 创建一个xcb_pixmap_t,使用从PNG中获取的数据调用xcb_create_pixmap_from_bitmap_data(),然后在事件循环的EXPOSE部分调用xcb_copy_area()
  • 创建一个xcb_image_t*,使用xcb_image_create_from_bitmap_data()创建,然后尝试使用xcb_image_put()将其映射到窗口。 我甚至尝试使用xcb_image_put_pixel()来显示每个像素,但没有成功。

代码示例:

  xcb_pixmap_t pixmap = xcb_create_pixmap_from_bitmap_data(
                            connection, // xcb_connect(0, 0)    (type: xcb_connection_t*)
                            window, // xcb_generate_id(connection)    (type: xcb_window_t)
                            img.getData(), // uint8_t*
                            img.getWidth(), // 128
                            img.getHeight(), // 128
                            img.getBitDepth(), // 8
                            screen->black_pixel, // screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data    (type: xcb_screen_t*)
                            screen->white_pixel,
                            nullptr);

   // "img" is an instance of my own custom class, result of PNG reading
  xcb_image_t* image = xcb_image_create_from_bitmap_data(
                              img.getData(),
                              img.getWidth(),
                              img.getHeight()); // image->data seems fine

  xcb_image_put(connection,
                window,
                graphicsContext,
                image, 0, 0, 0); // This does nothing

  for (unsigned int i = 0; i < screen->height_in_pixels; ++i)
    for (unsigned int j = 0; j < screen->width_in_pixels; ++j)
      xcb_image_put_pixel(image, j, i, 0); // Displays nothing

  [...]

  // Into event loop
  case XCB_EXPOSE: {
    xcb_expose_event_t* exposeEvent = reinterpret_cast<xcb_expose_event_t*>(event);
    xcb_copy_area(connection,
                  pixmap,
                  window,
                  graphicsContext,
                  exposeEvent->x, exposeEvent->y, // Top left x & y coordinates of the source's region to copy
                  exposeEvent->x, exposeEvent->y, // Top left x & y coordinates of the destination's region to copy to
                  exposeEvent->width,
                  exposeEvent->height);
    xcb_flush(connection);
    break;
  }

根据我找到的例子,我发现它不需要颜色映射,但这是真的吗? 有人能告诉我哪里错了吗?


有什么问题?请尽可能具体地描述。 - n. m.
img.getBitDepth(), // 8 这可能是你的问题所在。深度必须与窗口深度相同,否则 xcb_copy_area 将失败。默认情况下,你真的有 8 位深度的窗口吗?一般来说,你应该准备处理所有深度。 - n. m.
@n.m. 感谢您的回答! 很好的观点,我编辑了以指明错误所在,我真是太傻了。 实际上,我不知道我的窗口有多深,我在创建窗口时传递了 XCB_COPY_FROM_PARENT。通过将此字段替换为 8,窗口甚至都不会出现。 - Razakhel
你需要知道你的目标窗口深度是多少位,并将图像数据转换为该特定位深度。如果你不知道源图像数据表示,那么你就无法进行转换。无操作转换(又称为 no-op 转换)只是许多可能情况之一。 - n. m.
说实话,我有点放弃了。我尝试使用xcb_image_create(如果我没记错的话,我之前已经尝试过了),但我一直收到段错误。我已经花了两天时间在这上面,只是因为XCB无法提供适当的文档。我想我会简单地创建GLX窗口(反正我以后还需要OpenGL)。 - Razakhel
显示剩余4条评论
1个回答

4

我大约4年前做了一个简单的XCB图像查看器,但是刚注意到这个问题,对于这种死灵法术表示歉意。

它使用xcb_image、stb_image和nanosvg,但编译成相对较小的静态二进制文件(使用musl或uclibc工具链)。

#include <xcb/xcb.h>
#include <xcb/xcb_image.h>
#define STBI_NO_HDR
#define STBI_NO_LINEAR
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"

int main(int argc, char **argv){
   xcb_connection_t *c = xcb_connect(0, 0);
   xcb_screen_t *s = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
   int w, h, n,
      depth = s->root_depth,
      win_class = XCB_WINDOW_CLASS_INPUT_OUTPUT,
      format = XCB_IMAGE_FORMAT_Z_PIXMAP;
   xcb_colormap_t colormap = s->default_colormap;
   xcb_drawable_t win = xcb_generate_id(c);
   xcb_gcontext_t gc = xcb_generate_id(c);
   xcb_pixmap_t pixmap = xcb_generate_id(c);
   xcb_generic_event_t *ev;
   xcb_image_t *image;
   NSVGimage *shapes = NULL;
   NSVGrasterizer *rast = NULL;
   char *data = NULL;
   unsigned *dp;
   size_t i, len;
   uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
      value_mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS,
      values[] = { s->black_pixel, value_mask };

   if (argc<2) return -1;
   if ((data = stbi_load(argv[1], &w, &h, &n, 4)))
      ;
   else if ((shapes = nsvgParseFromFile(argv[1], "px", 96.0f))) {
      w = (int)shapes->width;
      h = (int)shapes->height;
      rast = nsvgCreateRasterizer();
      data = malloc(w*h*4);
      nsvgRasterize(rast, shapes, 0,0,1, data, w, h, w*4);
   }else return -1;
   for(i=0,len=w*h,dp=(unsigned *)data;i<len;i++) //rgba to bgra
      dp[i]=dp[i]&0xff00ff00|((dp[i]>>16)&0xFF)|((dp[i]<<16)&0xFF0000);
   xcb_create_window(c,depth,win,s->root,0,0,w,h,1,win_class,s->root_visual,mask,values);
   xcb_create_pixmap(c,depth,pixmap,win,w,h);
   xcb_create_gc(c,gc,pixmap,0,NULL);
   image = xcb_image_create_native(c,w,h,format,depth,data,w*h*4,data);
   xcb_image_put(c, pixmap, gc, image, 0, 0, 0);
   xcb_image_destroy(image);
   xcb_map_window(c, win);
   xcb_flush(c);
   while ((ev = xcb_wait_for_event(c))) {
      switch (ev->response_type & ~0x80){
      case XCB_EXPOSE: {
         xcb_expose_event_t *x = (xcb_expose_event_t *)ev;
         xcb_copy_area(c,pixmap,win,gc,x->x,x->y,x->x,x->y,x->width,x->height);
         xcb_flush(c);
      }break;
      case XCB_BUTTON_PRESS: goto end;
      default: break;
      }
   }
end:
   xcb_free_pixmap(c, pixmap);
   xcb_disconnect(c);
   return 0;
}

分配的数据在哪里释放?xcb_image_destroy()或xcb_free_pixmap()是否在您提供的指针上调用free()函数? - Pablo Ariel
2
@PabloAriel xcb_image_destroy(image); 释放变量 data,因为 data == image->base,而 image->base 是由 xcb_image_create_native 设置为 data 的。xcb_image_destroy 的源代码只有三行 https://cgit.freedesktop.org/xcb/util-image/tree/image/xcb_image.c?id=d789b704ca3282d8673085dda473b6a91cd4cb4e#n300。P.S. 内存泄漏:ev 没有被释放。 - Arzet Ro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接