Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从24位位图读取像素时出现奇怪值 我仍然是C++中的NoOB,我现在正在尝试创建一个程序,它手动加载一个256色位图到一个数组,并打印出CONSOL中每个像素的值。似乎这些价值观在大多数情况下都是不正确的_C++_Bitmap - Fatal编程技术网

从24位位图读取像素时出现奇怪值 我仍然是C++中的NoOB,我现在正在尝试创建一个程序,它手动加载一个256色位图到一个数组,并打印出CONSOL中每个像素的值。似乎这些价值观在大多数情况下都是不正确的

从24位位图读取像素时出现奇怪值 我仍然是C++中的NoOB,我现在正在尝试创建一个程序,它手动加载一个256色位图到一个数组,并打印出CONSOL中每个像素的值。似乎这些价值观在大多数情况下都是不正确的,c++,bitmap,C++,Bitmap,我的其他输出(例如信息标题中的值)都很好,除了biSizeImage,它是5个字节。这不是应该是25个字节吗? 出于测试目的,我拍摄了两张5x5的图像。其中一个全黑,另一个全白。 我希望黑色的输出与此类似: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 但我得到了: 0 0 0 0 0 0 0 0 0 0 3452816845 3452816845 3452816845 3452816845 3452816845 3452816845 34

我的其他输出(例如信息标题中的值)都很好,除了
biSizeImage
,它是5个字节。这不是应该是25个字节吗?
出于测试目的,我拍摄了两张5x5的图像。其中一个全黑,另一个全白。
我希望黑色的输出与此类似:

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
但我得到了:

0 0 0 0 0
0 0 0 0 0
3452816845 3452816845 3452816845 3452816845 3452816845
3452816845 3452816845 3452816845 3452816845 3452816845
3452816845 3452816845 3452816845 3452816845 3452816845
对于完全白色的图片,我的结果更加奇怪:

4294967295 255 4294967295 255 4294967295
255 4294967295 255 4294967295 255
3452816845 3452816845 3452816845 3452816845 3452816845
3452816845 3452816845 3452816845 3452816845 3452816845
3452816845 3452816845 3452816845 3452816845 3452816845
下面您可以看到我的当前代码:

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>  

using namespace std;

#pragma pack(push,1)
typedef struct tagBITMAPFILEHEADER {  
unsigned short      bfType;                 // Specifies the type of file. This member must be BM. (0x4D42)
unsigned int        bfSize;                 // Specifies the size of the file, in bytes.
short               bfReserved1;            // Reserved; must be set to zero.
short               bfReserved2;            // Reserved; must be set to zero.
unsigned int        bfOffBits;              // Specifies the byte offset from the BITMAPFILEHEADER structure to the actual bitmap data in the file.
} BITMAPFILEHEADER;


typedef struct tagBITMAPINFOHEADER {  
unsigned int        biSize;                 // Specifies the number of bytes required by the BITMAPINFOHEADER structure.
int                 biWidth;                // Specifies the width of the bitmap, in pixels.
int                 biHeight;               // Specifies the height of the bitmap, in pixels.
unsigned short      biPlanes;               // Specifies the number of planes for the target device. This member must be set to 1.
unsigned short      biBitCount;             // Specifies the number of bits per pixel. This value must be 1,4, 8, or 24.
unsigned int        biCompression;          // Specifies the type of compression for a compressed bitmap. Itcan be one of the following values: BI_RGB, BI_RLE8, BI_RLE4   
unsigned int        biSizeImage;            // Specifies the size, in bytes, of the image. It is valid to set this member to zero if the bitmap is in the BI_RGB format.
int                 biXPelsPerMeter;        // Specifies the horizontal resolution, in pixels per meter, of the target device for the bitmap.
int                 biYPelsPerMeter;        // Specifies the vertical resolution, in pixels per meter, of the target device for the bitmap.
unsigned int        biClrUsed;              // Specifies the number of color indexes in the color table actually used by the bitmap.
unsigned int        biClrImportant;         // Specifies the number of color indexes that are considered important for displaying the bitmap. If this value is zero, all colors are important.
} BITMAPINFOHEADER;  

typedef struct tagRGBPIXEL{
unsigned char b;
unsigned char g;
unsigned char r;
} rgbPIXEL;
#pragma pack(pop)

typedef struct tagBITMAP{
BITMAPFILEHEADER FILEHEADER;
BITMAPINFOHEADER INFOHEADER;
rgbPIXEL*    IMAGEDATA;
}BITMAP;

void main (){
string          sImageLocation      = "C:/BMP.bmp";
BITMAP          bmpImage;
FILE*           fbmpImage           = NULL;
unsigned int    ImagePixelAmount    = 0;
bool            bImageLoaded        = false;

do{

//Open the image file
fopen_s(&fbmpImage, sImageLocation.c_str(), "rb");

//Check whether the image could be loaded successfully or not
if(fbmpImage == NULL){
    cout << "Loading the image file failed!" << " " << endl;
    }
if(fbmpImage != NULL){
    cout << "Loaded the image file successfully!" << endl;
    bImageLoaded = true;
    }


cout << endl;
cout << endl;
cout << endl;

//Read the BITMAP FILE HEADER
fseek(fbmpImage, 0, SEEK_SET);
fread(&bmpImage.FILEHEADER, sizeof(BITMAPFILEHEADER), 1, fbmpImage);

//Check if the image is a valid bitmap file
if(bmpImage.FILEHEADER.bfType != 19778){
bImageLoaded = false;
cout << "Image is not a valid Bitmap file!" << endl;
fclose(fbmpImage);
}
}while(bImageLoaded = false);

//Show the information of the BITMAP FILE HEADER in the consol
cout << "Bitmap File Header Data Values:"                               << endl;
cout                                                                    << endl;
cout << "Image Type:  " << bmpImage.FILEHEADER.bfType                   << endl;
cout << "Image Size:  " << bmpImage.FILEHEADER.bfSize       << " Bytes" << endl;
cout << "Reserved:    " << bmpImage.FILEHEADER.bfReserved1              << endl;
cout << "Reserved:    " << bmpImage.FILEHEADER.bfReserved2              << endl;
cout << "Byte Offset: " << bmpImage.FILEHEADER.bfOffBits                << endl;

cout << endl;
cout << endl;
cout << endl;


//Read the BITMAP INFO HEADER
fread(&bmpImage.INFOHEADER, sizeof(BITMAPINFOHEADER), 1, fbmpImage);

cout << "Bitmap Info Header Data Values: " << endl;
cout << endl;
cout << "Size of Bitmap Info Header: "  << bmpImage.INFOHEADER.biSize       << " Bytes"              << endl;
cout << "Width of Bitmap:             " << bmpImage.INFOHEADER.biWidth      << " Pixel"              << endl;
cout << "Height of Bitmap:            " << bmpImage.INFOHEADER.biHeight     << " Pixel"              << endl;
cout << "Size of Image Data:          " << bmpImage.INFOHEADER.biHeight     << " Bytes"              << endl;
cout << "Bit Count:                   " << bmpImage.INFOHEADER.biBitCount   << " Bits Per Pixel"     << endl;
cout << "Amount of color indexes:     " << bmpImage.INFOHEADER.biClrUsed                             << endl;
cout << "Compression:                 " << bmpImage.INFOHEADER.biCompression                         << endl;

cout << endl;
cout << endl;
cout << endl;

ImagePixelAmount = bmpImage.INFOHEADER.biHeight * bmpImage.INFOHEADER.biWidth;

//Create space in memory and read in pixel data from the image
bmpImage.IMAGEDATA = (rgbPIXEL*)malloc(sizeof(rgbPIXEL) * ImagePixelAmount);
cout << "Memory for image created" << endl;
fseek(fbmpImage, bmpImage.FILEHEADER.bfOffBits, SEEK_SET);
fread(bmpImage.IMAGEDATA, ImagePixelAmount * sizeof(rgbPIXEL), 1, fbmpImage);

fclose(fbmpImage);


//Show the pixel data in the consol in numbers instead of ASCII-symbols
for(int y =0; y < bmpImage.INFOHEADER.biHeight; y++){

    for(int x = 0; x < bmpImage.INFOHEADER.biWidth; x++){
    cout << "Red: " <<(unsigned int) bmpImage.IMAGEDATA[y * bmpImage.INFOHEADER.biWidth + x].r << " ";
    cout << "Green: " <<(unsigned int) bmpImage.IMAGEDATA[y * bmpImage.INFOHEADER.biWidth + x].g << " ";
    cout << "Blue: " <<(unsigned int) bmpImage.IMAGEDATA[y * bmpImage.INFOHEADER.biWidth + x].b << " ";
    }

cout << endl;
}

cout << endl;
cout << endl;
cout << endl;

cout << "Press any key to quit the programm" << endl;
_getch();
return;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#pragma包(推送,1)
typedef结构tagBITMAPFILEHEADER{
无符号短bfType;//指定文件的类型。此成员必须是BM。(0x4D42)
unsigned int bfSize;//指定文件的大小,以字节为单位。
short bfReserved1;//Reserved;必须设置为零。
short bfReserved2;//Reserved;必须设置为零。
unsigned int bOffbits;//指定从BITMAPFILEHEADER结构到文件中实际位图数据的字节偏移量。
}位图文件头;
typedef结构TagBitMapInfo头{
unsigned int biSize;//指定BitMapInfo标头结构所需的字节数。
int biWidth;//指定位图的宽度,以像素为单位。
int biHeight;//指定位图的高度,以像素为单位。
无符号短双平面;//指定目标设备的平面数。此成员必须设置为1。
无符号短BIBITCUNT;//指定每像素的位数。此值必须为1、4、8或24。
unsigned int biCompression;//指定压缩位图的压缩类型。它可以是以下值之一:BI_RGB、BI_RLE8、BI_RLE4
unsigned int biSizeImage;//指定图像的大小(以字节为单位)。如果位图为BI_RGB格式,则将此成员设置为零是有效的。
int biXPelsPerMeter;//指定位图目标设备的水平分辨率,单位为每米像素。
int biYPelsPerMeter;//指定位图目标设备的垂直分辨率,单位为每米像素。
unsigned int biClrUsed;//指定位图实际使用的颜色表中的颜色索引数。
unsigned int biClrImportant;//指定对显示位图非常重要的颜色索引数。如果此值为零,则所有颜色都很重要。
}位图信息头;
typedef结构标记rgbPixel{
无符号字符b;
无符号字符g;
无符号字符r;
}RGB像素;
#布拉格语包(流行语)
typedef结构标记位图{
BITMAPFILEHEADER文件头;
BitMapInfo头信息头;
RGB像素*图像数据;
}位图;
空干管(){
字符串sImageLocation=“C:/BMP.BMP”;
位图图像;
文件*fbmpImage=NULL;
无符号int-ImagePixelAmount=0;
bool bImageLoaded=false;
做{
//打开图像文件
fopen_s(&fbmpImage,sImageLocation.c_str(),“rb”);
//检查映像是否可以成功加载
如果(fbmpImage==NULL){

cout你确定你正在阅读纯红色(255,0,0 RGB)的BMP吗?我问你,因为我创建了一个,你的程序输出如下:

红色:255绿色:0蓝色:0红色:255绿色:0蓝色:0

红色:0绿色:0蓝色:0红色:0绿色:255蓝色:0

仍然不正确,但更接近您的预期。但为什么第二行是错误的

表示位图像素的位按行排列。每行的大小通过填充向上舍入为4字节的倍数(32位DWORD)。-

以下是绘制生成的BMP的摘录:

0000 ff00 00ff 0000 ff00 00ff 0000

即:

00
:绿色(0)

00
:蓝色(0)

ff
:红色(255)

00
:绿色(0)

00
:蓝色(0)

ff
:红色(255)

00
:填充

00
:填充

00
:绿色(0)

00
:蓝色(0)

ff
:红色(255)

00
:绿色(0)

00
:蓝色(0)

ff
:红色(255)

00
:填充

00
:填充

请记住,填充量等于
(宽度*通道)%4
,在本例中为
(2*3)%4=2
,因此我们在每行像素的末尾添加了两个填充

我没有时间完全检查您的代码,但我不相信您会考虑此填充。请注意,32位(GBRA)BMP没有此填充,因为
(宽度*4)%4
始终是
0

那应该行。我从一个旧项目中删除了BMP加载的相关部分。你可以看到我在24位BMP文件中对填充的解释

另外,来自维基百科的文章

编辑

忘了提到这是一个2x2红色BMP图像。上面的摘录是像素阵列,完整文件如下:

424d 4600 0000 0000 0000 3600 0000 2800
0000 0200 0000 0200 0000 0100 1800 0000
0000 1000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 ff00 00ff 0000 0000
ff00 00ff 0000 

bmpImage.INFOHEADER.biSizeImage==ImagePixelAmount*sizeof(int);?对古老图像格式的不懈迷恋是不可阻挡的,8bpp在20年前才变得重要。你的malloc()调用是错误的,8bpp图像中的像素是一个字节,而不是
无符号int
。8bpp图像有一个颜色表。谢谢你们的回答@