简体   繁体   English

我如何在 Xlib 中将 XCopyArea 与 colors 一起使用而不是蓝色?

[英]How can I use XCopyArea with colors other than blue in Xlib?

I'm trying to build a ray tracer in C++. To that end, I'd like to draw the final image to a window (instead of just outputting to PPM, which is what I'm currently doing).我正在尝试在 C++ 中构建光线追踪器。为此,我想将最终图像绘制到 window(而不是仅输出到 PPM,这是我目前正在做的)。 I have some code below that saves the color of each pixel to an array that matches the size of an image and then attempts to display that image using Xlib.我在下面有一些代码将每个像素的颜色保存到一个与图像大小相匹配的数组中,然后尝试使用 Xlib 显示该图像。 This code snippet below allocates data to a 100 x 100 array image:下面的代码片段将数据分配给 100 x 100 数组图像:

int width = 100;
int height = 100;

int data[width * height * 4];

for (int i = 0; i < width * height; i++){

  data[i] = 255;

}
XImage *image  = XCreateImage(d, DefaultVisual(d, 0), 24, ZPixmap, 0, (char*) data, 100, 100, 32, 0); 

Pixmap pm = XCreatePixmap(d, w, 100, 100, 24);

XPutImage(d, pm, gc, image, 0,0,0,0, 100, 100);

This outputs a nice blue square in my window. But, if I change the for loop in the above example to:这会在我的 window 中输出一个漂亮的蓝色方块。但是,如果我将上面示例中的for循环更改为:

for (int i = 0; i < width * height; i+= 4){

  data[i] = 255;
  data[i + 1] = 255;
  data[i + 2] = 0;  
  data[i + 3] = 0;

}

I still get blue with some black mixed in, but I expected yellow.我仍然得到蓝色和一些黑色混合,但我希望是黄色。 What am I doing wrong here?我在这里做错了什么?

Full code below for reference:以下完整代码供参考:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  Display *d;
  Window w;
  XEvent e;
  const char *msg = "Hello, World!";
  int s;

  d = XOpenDisplay(NULL);
  if (d == NULL) {
    fprintf(stderr, "Cannot open display\n");
    exit(1);
  }

  s = DefaultScreen(d);
  w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 500, 500, 1,
                          BlackPixel(d, s), WhitePixel(d, s));
  
  GC gc = XCreateGC(d, w, 0, NULL);

  XSelectInput(d, w, ExposureMask | KeyPressMask);
  XMapWindow(d, w);

  int width = 100;
  int height = 100;

  int data[width * height * 4];

  for (int i = 0; i < width * height; i++){

    data[i] = 255;

  }

  XImage *image  = XCreateImage(d, DefaultVisual(d, 0), 24, ZPixmap, 0, (char*) data, 100, 100, 32, 0); 

  Pixmap pm = XCreatePixmap(d, w, 100, 100, 24);

  XPutImage(d, pm, gc, image, 0,0,0,0, 100, 100);

  while (1) {
    XNextEvent(d, &e);
    if (e.type == Expose) {
        XCopyArea(d, pm, w, gc, 0, 0, width, height, 10, 10);
    }
    if (e.type == KeyPress)
        break;
  }

  XCloseDisplay(d);
  return 0;
}

Finally figured it out.终于想通了。 If I remember where I found the original sources for some of this stuff, I'll link it in an edit.如果我记得我在哪里找到这些东西的原始来源,我会在编辑中链接它。 But basically below is a working for loop.但基本上下面是一个for循环。 I'm not entirely sure why I need it, but after every RGB triplet, we need a byte of 0x00 .我不完全确定为什么需要它,但在每个 RGB 三元组之后,我们需要一个字节0x00 This is on my computer where I have 24-bit true color depth (which is probably the same as every modern computer).这是在我的电脑上,我有 24 位真色深度(这可能与每台现代电脑都一样)。 So if you have 24 bits, then you have 3 bytes on every line which, for many reasons that I don't know, is illegal.所以如果你有 24 位,那么每行有 3 个字节,出于许多我不知道的原因,这是非法的。 If someone could explain this to me, I'd really love to know.如果有人可以向我解释这一点,我真的很想知道。 The code is below:代码如下:

unsigned char data[width * height * 4];

  for (int i = 0; i < width * height * 4; i+=4){

    data[i] = 255; // blue channel
    data[i + 1] = 0; // green channel
    data[i + 2] = 255; // red channel
    data[i + 3] = 0; // dead pixel that is always zero

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM