来自2D阵列的C ++ 16位灰度渐变图像 [英] C++ 16 bit grayscale gradient image from 2D array
问题描述
我目前正在尝试构建一个16位灰度的渐变"图像,但是我的输出看起来很奇怪,因此我显然无法正确理解这一点.我希望有人能在我的问题上大放光彩.我认为我写的位图"是错误的吗?但是我不确定.
I'm currently trying to build a 16 bit grayscale "gradient" image but my output looks weird so I'm clearly not understanding this correctly. I was hoping somebody could shine some knowledge on my issue. I think that the "bitmap" I write is wrong? But I'm not sure.
#include "CImg.h"
using namespace std;
unsigned short buffer[1250][1250];
void fill_buffer()
{
unsigned short temp_data = 0;
for (int i =0;i < 1250; i++)
{
for (int j =0 ;j < 1250;j++)
{
buffer[i][j] = temp_data;
}
temp_data += 20;
}
}
int main()
{
fill_buffer();
auto hold_arr = (uint8_t *)&buffer[0][0];
cimg_library::CImg<uint8_t> img(hold_arr, 1250, 1250);
img.save_bmp("test.bmp");
return 0;
}
电流输出:
推荐答案
You cannot store 16-bit greyscale samples in a BMP... see Wikipedia.
BMP中的每像素16位选项允许您存储4位红色,4位绿色,4位蓝色和4位alpha,但不存储16位灰度.
The 16-bit per pixel option in a BMP allows you to store 4 bits of red, 4 bits of green, 4 bits of blue and 4 bits of alpha, but not 16-bits of greyscale.
24位格式允许您存储1字节的红色,1字节的绿色和1字节的蓝色,但不能存储16位的灰度.
The 24-bit format allows you to store 1 byte for red, 1 byte for green and one byte for blue, but not 16-bits of greyscale.
32位BMP允许您存储24位BMP加alpha.
The 32-bit BMP allows you to store a 24-bit BMP plus alpha.
您将需要使用PNG
或 NetPBM PGM格式,或TIFF
格式. PGM
格式很棒,因为CImg
可以在没有任何库的情况下编写该格式,并且您始终可以使用 ImageMagick 将其转换为其他任何格式,例如:
You will need to use PNG
, or a NetPBM PGM format, or TIFF
format. PGM
format is great because CImg
can write that without any libraries and you can always use ImageMagick to convert it to anything else, e.g.:
convert image.pgm image.png
或
convert image.pgm image.jpg
这有效:
#define cimg_use_png
#define cimg_display 0
#include "CImg.h"
using namespace cimg_library;
using namespace std;
unsigned short buffer[1250][1250];
void fill_buffer()
{
unsigned short temp_data = 0;
for (int i =0;i < 1250; i++)
{
for (int j =0 ;j < 1250;j++)
{
buffer[i][j] = temp_data;
}
temp_data += 65535/1250;
}
}
int main()
{
fill_buffer();
auto hold_arr = (unsigned short*)&buffer[0][0];
cimg_library::CImg<unsigned short> img(hold_arr, 1250, 1250);
img.save_png("test.png");
return 0;
}
请注意,当要求CImg
编写PNG文件时,您将需要使用以下命令(带有libpng
和zlib
)进行编译:
Note that when asking CImg
to write a PNG file, you will need to use a command like this (with libpng
and zlib
) to compile:
g++-7 -std=c++11 -O3 -march=native -Dcimg_display=0 -Dcimg_use_png -L /usr/local/lib -lm -lpthread -lpng -lz -o "main" "main.cpp"
仅作为解释:
-
-std=c++11
只是设置C ++标准 -
-O3 -march=native
只是为了加快速度,并非严格要求 -
-Dcimg_display=0
表示未解析所有X11标头,因此编译速度更快-但这意味着您无法显示程序中的图像,因此表示您无头" -
-Dcimg_use_png
意味着您可以使用libpng
读取/写入PNG图像,而不需要安装ImageMagick -
-lz -lpng
表示生成的代码已与PNG和ZLIB库链接.
-std=c++11
just sets the C++ standard-O3 -march=native
is only to speed things up and is not strictly required-Dcimg_display=0
means all the X11 headers are not parsed so compilation is quicker - however this means you can't display images from your program so it means you are "head-less"-Dcimg_use_png
means you can read/write PNG images usinglibpng
rather than needing ImageMagick installed-lz -lpng
means the resulting code gets linked with the PNG and ZLIB libraries.
这篇关于来自2D阵列的C ++ 16位灰度渐变图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!