C ++将一个浮点数转换为一个无符号的字符? [英] C++ Converting a float to an unsigned char?

查看:746
本文介绍了C ++将一个浮点数转换为一个无符号的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,做了一些Google搜索,我认为 sprintf 会做这个工作,但是编译时出现错误,我无法转换在 unsigned char char 之间。我需要一个无符号的字符,因为我要打印到一个图像文件(0-255RGB)。
$ b $ pre $无符号字符** * pixels = new unsigned char ** [SIZE];
vector< float> pixelColors;


$ b sprintf(像素[i] [j] [k],%.4g,pixelColors.at(k));




(pixelColors大小为3,'k'表示'for循环'变量)我会猜浮游植物的范围在0.0 ... 1.0,那么你是这样的:



  float redf = 0.5f; 
unsigned char reduc = redf * 255;

变量reduc现在是128.




编辑:完整的示例,以PPM格式输出图像。 $ b

  //用法
//程序> file.ppm

#include< vector>
#include< iostream>

typedef struct
{/ *颜色范围在0..1其他都不在域内* /
float红色,绿色,蓝色;
}颜色;

使用namespace std;

int main(int argc,char ** argv)
{
int width = 10,height = 10;
vector< color>位图; //这也许应该叫做floatmap? ;)

//在内存中以浮点形式创建图像

(int y = 0; y< height; y ++)
{
for(int x = 0; x {
color temp;
temp.red =((float)x / width);
temp.green = 0;
temp.blue =((float)y / height);
bitmap.push_back(temp);
}
}

//输出图像作为Netppm像素映射
cout < P3<< endl<<宽度<< <<高度<< endl<< 255< ENDL;
for(int y = 0; y {
for(int x = 0; x {
int红,绿,蓝;
red =(unsigned char)(bitmap [y * width + x] .red * 255);
green =(unsigned char)(bitmap [y * width + x] .green * 255);
blue =(unsigned char)(bitmap [y * width + x] .blue * 255);
cout<<红色< ;
cout<<绿色< ;
cout<<蓝色< ;
}
cout<< ENDL;
}
return 0;

}

我希望这可以帮助您。您可以阅读关于 Netpbm格式的wikipedia



图像输出是纯文本。

结果是这样的:


$ b EDIT2: img src =https://i.stack.imgur.com/59kRn.pngalt =test image>(小小,不是吗?编辑第16行到512x512什么的)

而实际的产出是这样的:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 255
0 0 0 25 0 0 51 0 0 76 0 0 102 0 0 127 0 0 153 0 0 178 0 0 204 0 0 229 0 0
0 0 25 25 0 25 51 0 25 76 0 25 102 0 25 127 0 25 153 0 25 178 0 25 204 0 25 229 0 25
0 0 51 25 0 51 51 0 51 76 0 51 102 0 51 127 0 51 153 0 51 178 0 51 204 0 51 229 0 51
0 0 76 25 0 76 51 0 76 76 0 76 102 0 76 127 0 76 153 0 76 178 0 76 204 0 76 229 0 76
0 0 102 25 0 102 51 0 102 76 0 102 102 0 102 127 0 102 153 0 102 178 0 102 204 0 102 229 0 102
0 0 127 25 0 127 51 0 127 76 0 127 102 0 127 127 0 127 153 0 127 178 0 127 204 0 127 229 0 127
0 0 153 25 0 153 51 0 153 76 0 153 102 0 153 127 0 153 153 0 153 178 0 153 204 0 153 229 0 153
0 0 178 25 0 178 51 0 178 76 0 178 102 0 178 127 0 178 153 0 178 178 0 178 204 0 178 229 0 178
0 0 204 25 0 204 51 0 204 76 0 204 102 0 204 127 0 204 153 0 204 178 0 204 204 0 204 229 0 204
0 0 229 25 0 229 51 0 229 76 0 229 102 0 229 127 0 229 153 0 229 178 0 229 204 0 229 229 0 229


I'm new to C++, and doing a bit of googling I thought sprintf would do the job, but I get an error upon compiling that I can't convert between an unsigned char and a char. I need an unsigned char because I am going to print to an image file (0-255 RGB).

unsigned char*** pixels = new unsigned char**[SIZE];
vector<float> pixelColors;

...

sprintf(pixels[i][j][k], "%.4g", pixelColors.at(k));

(pixelColors has size of 3 and 'k' refers to a 'for loop' variable)

解决方案

I'll guess that the floats are in the range 0.0 ... 1.0, then you does like this:

float redf = 0.5f;
unsigned char reduc = redf * 255;

The variable reduc are now 128.


EDIT: complete example, outputting image in Net PPM format.

// Usage 
//  program > file.ppm

#include <vector>
#include <iostream>

typedef struct
{   /* colors in range 0..1 anything else is out of gamut */
    float red, green, blue;
} color;

using namespace std;

int main ( int argc, char **argv )
{   
    int width = 10, height = 10;
    vector<color> bitmap; // This should maybe be called floatmap? ;)

    // Make an image in memory as a float vector

    for( int y = 0; y < height; y++ )
    {   
        for( int x = 0; x < width; x++ )
        {
            color temp;
            temp.red = ((float)x / width);
            temp.green = 0;
            temp.blue = ((float)y / height);
            bitmap.push_back(temp);
        }
    }

    // output image as an Netppm pixmap
    cout << "P3" << endl << width << " " << height << endl << 255 << endl;
    for( int y = 0; y < height; y++ )
    {   
        for( int x = 0; x < width; x++ )
        {
            int red, green, blue;
            red = (unsigned char)(bitmap[y*width+x].red * 255);
            green = (unsigned char)(bitmap[y*width+x].green * 255);
            blue = (unsigned char)(bitmap[y*width+x].blue * 255);
            cout << red << " ";
            cout << green << " ";
            cout << blue << " ";
        }
        cout << endl;
    }
    return 0;

}

I hope this helps you. You can read about Netpbm format on wikipedia.


EDIT2: The image output is clear text.
Result is like this:
(Tiny, isn't it? edit line 16 to 512x512 or something)

And the actual output is this:

P3
10 10
255
0 0 0 25 0 0 51 0 0 76 0 0 102 0 0 127 0 0 153 0 0 178 0 0 204 0 0 229 0 0 
0 0 25 25 0 25 51 0 25 76 0 25 102 0 25 127 0 25 153 0 25 178 0 25 204 0 25 229 0 25 
0 0 51 25 0 51 51 0 51 76 0 51 102 0 51 127 0 51 153 0 51 178 0 51 204 0 51 229 0 51 
0 0 76 25 0 76 51 0 76 76 0 76 102 0 76 127 0 76 153 0 76 178 0 76 204 0 76 229 0 76 
0 0 102 25 0 102 51 0 102 76 0 102 102 0 102 127 0 102 153 0 102 178 0 102 204 0 102 229 0 102 
0 0 127 25 0 127 51 0 127 76 0 127 102 0 127 127 0 127 153 0 127 178 0 127 204 0 127 229 0 127 
0 0 153 25 0 153 51 0 153 76 0 153 102 0 153 127 0 153 153 0 153 178 0 153 204 0 153 229 0 153 
0 0 178 25 0 178 51 0 178 76 0 178 102 0 178 127 0 178 153 0 178 178 0 178 204 0 178 229 0 178 
0 0 204 25 0 204 51 0 204 76 0 204 102 0 204 127 0 204 153 0 204 178 0 204 204 0 204 229 0 204 
0 0 229 25 0 229 51 0 229 76 0 229 102 0 229 127 0 229 153 0 229 178 0 229 204 0 229 229 0 229 

这篇关于C ++将一个浮点数转换为一个无符号的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆