gpt4 book ai didi

c - C语言中位图的理解和获取信息

转载 作者:行者123 更新时间:2023-11-30 18:30:30 25 4
gpt4 key购买 nike

我很难理解和解析位图图像中存在的信息数据。为了更好地理解,我阅读了以下教程,Raster Data.

现在,代码如下,(灰度8位颜色值)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*-------STRUCTURES---------*/
typedef struct {int rows; int cols; unsigned char* data;} sImage;

/*-------PROTOTYPES---------*/
long getImageInfo(FILE*, long, int);

int main(int argc, char* argv[])
{
FILE *bmpInput, *rasterOutput;
sImage originalImage;
unsigned char someChar;
unsigned char* pChar;
int nColors; /* BMP number of colors */
long fileSize; /* BMP file size */
int vectorSize; /* BMP vector size */
int r, c; /* r = rows, c = cols */


/* initialize pointer */
someChar = '0';
pChar = &someChar;

if(argc < 2)
{
printf("Usage: %s bmpInput.bmp\n", argv[0]);
//end the execution
exit(0);
}
printf("Reading filename %s\n", argv[1]);

/*--------READ INPUT FILE------------*/
bmpInput = fopen(argv[1], "rb");
//fseek(bmpInput, 0L, SEEK_END);

/*--------DECLARE OUTPUT TEXT FILE--------*/
rasterOutput = fopen("data.txt", "w");

/*--------GET BMP DATA---------------*/
originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
fileSize = getImageInfo(bmpInput, 2, 4);
nColors = getImageInfo(bmpInput, 46, 4);
vectorSize = fileSize - (14 + 40 + 4*nColors);

/*-------PRINT DATA TO SCREEN-------------*/
printf("Width: %d\n", originalImage.cols);
printf("Height: %d\n", originalImage.rows);
printf("File size: %ld\n", fileSize);
printf("# Colors: %d\n", nColors);
printf("Vector size: %d\n", vectorSize);

/*----START AT BEGINNING OF RASTER DATA-----*/
fseek(bmpInput, (54 + 4*nColors), SEEK_SET);

/*----------READ RASTER DATA----------*/
for(r=0; r<=originalImage.rows - 1; r++)
{
for(c=0; c<=originalImage.cols - 1; c++)
{
/*-----read data and print in (row,column) form----*/
fread(pChar, sizeof(char), 1, bmpInput);
fprintf(rasterOutput, "(%d, %d) = %d\n", r, c, *pChar);
}
}

fclose(bmpInput);
fclose(rasterOutput);


}


/*----------GET IMAGE INFO SUBPROGRAM--------------*/
long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC;
long value = 0L;
unsigned char dummy;
int i;

dummy = '0';
ptrC = &dummy;

fseek(inputFile, offset, SEEK_SET);

for(i=1; i<=numberOfChars; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
/* calculate value based on adding bytes */
value = (long)(value + (*ptrC)*(pow(256, (i-1))));
}
return(value);

} /* end of getImageInfo */

我不明白的是:-

  1. 我无法理解代码试图获取图像信息(如行数、列数等)的“GET IMAGE INTOSUBPROGRAM”部分。为什么这些信息存储在4个字节,value = (long)(value + (*ptrC)*(pow(256, (i-1))));有什么用?说明。

  2. 为什么有unsigned char dummy ='0'创建后 ptrC =&dummy已分配?

  3. 为什么我们不能通过读取 1 个字节的数据来获取图像中的行数,就像获取特定行和列的灰度值一样。

  4. 为什么我们使用unsigned char来存储字节,是否有其他数据类型或int或long我们可以在这里有效地使用?

请帮助我理解我的这些疑问(困惑!!?),如果它们听起来很菜鸟,请原谅我。

谢谢。

最佳答案

我想说这个教程在某些方面相当糟糕,你理解它的问题并不总是因为是初学者。

I am unable the understand the 'GET IMAGE INTOSUBPROGRAM' part where the code is trying to get the image infos like no of rows,columns, etc. Why are these infos stored over 4 bytes and what is the use of the value = (long)(value + (ptrC)(pow(256, (i-1)))); instruction.

存储超过 4 个字节的原因是允许图像的高度和宽度在 0 到 2^32-1 之间。如果我们只使用一个字节,我们只能得到大小为 0..255 的图像和 2 个字节的图像 0..65535。

奇怪的value = (long)(value + (*ptrC)*(pow(256, (i-1))));这是我以前从未见过的。它用于将字节转换为长整型,以便它可以处理任何字节序。这个想法是使用 256 的幂来设置 *ptrCvalue ,即将第一个字节乘以 1,下一个字节乘以 256,下一个字节乘以 65536 等等。

一种更具可读性的方法是使用轮类,例如value = value + ((long)(*ptrC) << 8*(i-1)); 。或者更好的是从最高字节读取字节到较低字节并使用 value = value << 8 + *ptrC; 。在我看来好多了,但是当字节以不同的顺序出现时,并不总是那么简单。

简单的重写使其更容易理解

long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char ptrC;
long value = 0L;
int i;

fseek(inputFile, offset, SEEK_SET);

for(i=0; i<numberOfChars; i++) // Start with zero to make the code simpler
{
fread(&ptrC, 1, 1, inputFile); // sizeof(char) is always 1, no need to use it
value = value + ((long)ptrC << 8*i); // Shifts are a lot simpler to look at and understand what's the meaning
}
return value; // Parentheses would make it look like a function
}

Why there unsigned char dummy ='0' is created and then ptrC =&dummy is assigned?

这也是没有意义的。他们本来可以使用 unsigned char ptrC然后使用&ptrC而不是ptrCptrC而不是*ptrC 。这也表明它只是一个普通的静态变量。

Why can't we just get the no of rows in an image by just reading 1 byte of data like getting the Greyscale value at a particular row and column.

如果图像有 3475 行高怎么办?一字节不够。所以它需要更多的字节。只是读取方式有点复杂。

Why are we using unsigned char to store the byte, isn't there some other data type or int or long we can use effectively here?

无符号字符正好是一个字节长。那么为什么我们要使用任何其他类型来存储字节呢?

关于c - C语言中位图的理解和获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30556656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com