英文:
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 <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;
}
答案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<uint64_t>(d);
u ^= 0x80'00'00'00'00'00'00'00; // toggle sign bit
d = std::bit_cast<double>(u);
std::cout << d; // prints -1
Getting the bit pattern without C++20:
double d = 0.1;
std::uint64_t u;
std::memcpy(&u, &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<uint64_t>(d);
u ^= 0x80'00'00'00'00'00'00'00; // toggle sign bit
d = std::bit_cast<double>(u);
std::cout << d; // prints -1
Getting the bit pattern without C++20:
double d = 0.1;
std::uint64_t u;
std::memcpy(&u, &d, sizeof d);
Once you have the bit pattern, you may also be interested in this post
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论