简体   繁体   English

c ++,无符号字符之间的线性插值

[英]c++, Linear interpolation between unsigned chars

Unexpectedly for me I faced strange issue: 出乎我的意料,我遇到了一个奇怪的问题:

Here is example of LOGICAL implementation of trivial linear Lagrange interpolation : 这是平凡线性Lagrange插值的LOGICAL实现示例:

unsigned char mix(unsigned char x0, unsigned char x1, float position){
    // LOGICALLY must be something like (real implementation should be 
    // with casts)...
    return x0 + (x1 - x0) * position;
}

Arguments x0 , x1 are always in range 0 - 255. 参数x0x1始终在0-255的范围内。
Argument position is always in range 0.0f - 1.0f . 参数位置始终在0.0f - 1.0f范围内。

Really I tried huge amount of implementations (with different casts and etc.) but it doesn't work in my case! 确实我尝试了大量的实现(使用不同的强制转换等),但在我的情况下不起作用! It returns incorrect results (looks like variable overflow or something similar. After looking for solution in internet for a whole week i decided to ask. May be someone has faced similar issues. 它返回错误的结果(看起来像变量溢出或类似的东西。在互联网上寻找解决方案整整一周后,我决定问一下。可能有人遇到过类似的问题。

I'm using MSVC 2017 compiler (most of parameters are default except language level). 我正在使用MSVC 2017编译器(除语言级别外,大多数参数都是默认参数)。
OS - Windows 10x64, Little Endian. 操作系统-Windows 10x64,Little Endian。

What do i do wrong and what is possible source of the issue? 我做错了什么,问题的可能根源是什么?

UPDATED: It looks like this issue is more deep than I expected (thanks for your responses). 更新:看来这个问题比我预期的要深(感谢您的答复)。

Here is the link to tiny github project which demonstrates my issue: https://github.com/elRadiance/altitudeMapVisualiser 这是一个演示我的问题的微型github项目的链接: https : //github.com/elRadiance/altitudeMapVisualiser

Output bmp-file should contain smooth altitude map. 输出的bmp文件应包含平滑的海拔图。 Instead of it, it contains garbage. 而不是它包含垃圾。 If I use just x0 or x1 as result of interpolation function (without interpolation) it works. 如果我仅将x0或x1用作插值函数的结果(不带插值),它将起作用。 Without it - doesn't (produces garbage). 没有它-不会(产生垃圾)。

Desired result (as here, but in interpolated colors, smooth) 所需的结果(如此处所示,但使用插值的颜色平滑)
所需的结果(如此处所示,但使用插值的颜色平滑)

Actual result (updated, best result achieved) 实际结果(更新,取得最佳结果)
实际结果(更新,取得最佳结果)

Main class to run it: 主类运行它:

#include "Visualiser.h"


int main() {

unsigned int width = 512;
unsigned int height = 512;


float* altitudes = new float[width * height];

float c;

for (int w = 0; w < width; w++) {
    c = (2.0f * w / width) - 1.0f;
    for (int h = 0; h < height; h++) {
        altitudes[w*height + h] = c;
    }
}

Visualiser::visualiseAltitudeMap("gggggggggg.bmp", width, height, altitudes);

delete(altitudes);
}

Thank you in advance! 先感谢您!

SOLVED: Thankfully @wololo. 求助:谢天谢地@wololo。 Mistake in my project was not in calculations. 我的项目中没有错误,这不是计算中的错误。

I should open file with option "binary": 我应该使用“二进制”选项打开文件:

file.open("test.bin", std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);

Without it in some point in data can be faced byte with value 10 In Windows environment it can be processed like LineFeed and changed to 13 . 在某些情况下,如果没有它,数据将面对值10字节。在Windows环境中,可以像LineFeed一样处理它并将其更改为13

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

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