将浮点二进制数据转换为int64。

huangapple go评论72阅读模式
英文:

Converting float point binary data into int64

问题

I'm in a situation where I have a float64 image stored in binary format. I would like to read the content of the file and store it as int64 so that I can tonemap it.

The question is how to convert float images into int64 in C++. Here is my approach to reading the file:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include "FreeImage.h"

int main()
{

    std::ifstream stream("c:/output/15_init_image.bin", std::ios::in | std::ios::binary);
    std::vector<uint8_t> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());

    int width = 2048; // get the width of the image;
    int height = 3096; // get the height of the image;
    int bpp = 32;
    FreeImage_Initialise();
    int pitch = 512;

    FIBITMAP* src = FreeImage_ConvertFromRawBitsEx(true, contents.data(), FIT_BITMAP, width, height, pitch, FI_RGBA_RED_MASK,
        FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
    FreeImage_Save(FIF_JPEG, src, "viewport.jpeg");


    return 0;

}

You can find the file here: 15_init_image.bin

英文:

I'm in situation that I have a float64 image stored in a binary format. I would like to read the content of the file and store it in int64, so that I can tonemap it.

The question how would I convert float images into int64 in C++.
Here is my approach of reading the file
The file is here
https://www.mediafire.com/file/ffv6qzvpg05xaja/15_init_image.bin/file

#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;
#include &quot;FreeImage.h&quot;

int main()
{

    std::ifstream stream(&quot;c:/output/15_init_image.bin&quot;, std::ios::in | std::ios::binary);
    std::vector&lt;uint8_t&gt; contents((std::istreambuf_iterator&lt;char&gt;(stream)), std::istreambuf_iterator&lt;char&gt;());

    int width = 2048; // get the width of the image ;
    int height = 3096; // get the height of the image ;
    int bpp = 32;
    FreeImage_Initialise();
    int pitch = 512;

    FIBITMAP* src = FreeImage_ConvertFromRawBitsEx(true, contents.data(), FIT_BITMAP, width, height, pitch, FI_RGBA_RED_MASK,
        FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
    FreeImage_Save(FIF_JPEG, src, &quot;viewport.jpeg&quot;);

    
    return 0;

}

答案1

得分: 1

From the comments:
> > Do you want the numeric value of the float64 stored in an int64, or do you want the bit pattern of the float stored as an int64?
>
> the bit pattern actually

Getting the bit pattern with C++20:

    double d   = 1.0;
    
    uint64_t u = std::bit_cast&lt;uint64_t&gt;(d);
 
    u ^= 0x80&#39;00&#39;00&#39;00&#39;00&#39;00&#39;00&#39;00; // toggle sign bit
    
    d = std::bit_cast&lt;double&gt;(u);

    std::cout &lt;&lt; d; // prints -1

Getting the bit pattern without C++20:

double d = 0.1;

std::uint64_t u;

std::memcpy(&amp;u, &amp;d, sizeof d);

Once you have the bit pattern, you may also be interested in this post

英文:

From the comments:
> > Do you want the numeric value of the float64 stored in an int64, or do you want the bit pattern of the float stored as an int64?
>
> the bit pattern actually

Getting the bit pattern with C++20:

    double d   = 1.0;
    
    uint64_t u = std::bit_cast&lt;uint64_t&gt;(d);
 
    u ^= 0x80&#39;00&#39;00&#39;00&#39;00&#39;00&#39;00&#39;00; // toggle sign bit
    
    d = std::bit_cast&lt;double&gt;(u);

    std::cout &lt;&lt; d; // prints -1

Getting the bit pattern without C++20:

double d = 0.1;

std::uint64_t u;

std::memcpy(&amp;u, &amp;d, sizeof d);

Once you have the bit pattern, you may also be interested in this post

huangapple
  • 本文由 发表于 2023年5月13日 23:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定