【问题标题】:CImg problem with color-interpolated 2D triangle颜色插值 2D 三角形的 CImg 问题
【发布时间】:2021-10-28 23:40:38
【问题描述】:

我觉得我在这里遗漏了一些明显的东西。

我无法让 CImg 的颜色插值 2D 三角形按预期工作。 更令人困惑的是,它在我的 CImg 系统版本 (cimg_version 245) 与 Github 中的最新版本 (cimg_version 300) 上的行为不同。

如果我画一个简单的实心三角形,一切都会按预期工作。 如果我为每个顶点指定颜色,则取决于我使用的 CImg 版本:

cimg_version 245: 插值在一定程度上起作用,但颜色通道对于大于 0 的值被限制为 255。 您可以通过比较标题为“CImg 版本:245”的图像中的中心三角形和直角三角形来看到这一点。 中心图像从 {0, 0, 0} 渐变到 {255, 255, 255} 而右图从 {100, 100, 100} 变为 {255, 255, 255}。

cimg_version 300: 在这个版本中,我无法让任何插值工作。

那么,我是缺少启用插值的设置还是应该提交错误报告?

#include <iostream>
#include <stdio.h>
#include "CImg.h"
using namespace cimg_library;

#define TITLE_LEN 100

int main() {
  char title [TITLE_LEN];
  snprintf(title, TITLE_LEN, "CImg version: %d", cimg_version);
  std::cout << title << "\n";

  CImg<unsigned char> image(600, 200, 1, 3, 0);
  CImgDisplay main_disp(image, title);

  const unsigned char black[3] = {0, 0, 0};
  const unsigned char grey[3] = {100, 100, 100};
  const unsigned char white[3] = {255, 255, 255};

  // Left
  image.draw_triangle(100, 10, 10, 190, 190, 190, grey);

  // Center
  image.draw_triangle(300, 10, 210, 190, 390, 190, white, black, white);

  // Right
  image.draw_triangle(500, 10, 410, 190, 590, 190, white, grey, white);

  image.display(main_disp);

  while (!main_disp.is_closed()) {
    main_disp.wait();
  }

  return 0;
}

[编辑] 忘了包括我正在运行的内容:

$ lsb_release -a
LSB Version:    core-11.1.0ubuntu2-noarch:printing-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal

$ uname -a
Linux lapdancer 5.4.0-89-generic #100-Ubuntu SMP Fri Sep 24 14:50:10 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

【问题讨论】:

    标签: c++ cimg


    【解决方案1】:

    这里是 CImg 的开发者。 它看起来确实像一个错误。我会尽快修复它。 谢谢。 当您遇到这种奇怪的行为时,请不要犹豫,在我们的 github 站点 (https://github.com/dtschump/CImg/issues) 上填写问题。

    编辑:现在应该通过 github.com/dtschump/CImg/issues/332 修复此问题。 CImg 网站已推送新的预发布版本。

    【讨论】:

    • 您可以(应该)将您的评论编辑到您的答案中。对 Stack Overflow 的评论通常被认为是“二等公民”,仅应真正用于寻求帖子作者的澄清。
    • 感谢您的快速回复!我实际上只是进一步研究了它。遵循原始实现的以下 hack 也有效:``` $ diff CImg.h CImg.h.fix 46749c46749,46753 get_shared_channel(c).draw_triangle(x0,y0,x1,y1,x2,y2,&one, > 1. + (float)color1[c] / 255, > 1. + (float)color2[c] / 255, > 1. + (float)color3[c] / 255, > 不透明度); ```
    • 胡,显然在 cmets 中没有代码格式。没关系。总之,我将颜色分量除以 255 以将它们转换为被包裹的 Gouraud 阴影 2D 三角形方法所期望的范围。
    • 不幸的是,您的 hack 不适用于范围在 [0,255] 之外的颜色,这可能是这种情况,例如如果有人想在 CImg 图像中绘制颜色(负值或高于 255 的值)。我所做的修复可以处理这些情况。
    • 明白。谢谢。我已经提取了您的最新更改,并且可以确认它对我有用。再次感谢您的快速修复。
    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    相关资源
    最近更新 更多