【发布时间】:2021-12-05 11:56:56
【问题描述】:
我正在尝试使用 c 学习图像处理。
#include <stdio.h>
int main(){
FILE *streamIn;
streamIn=fopen("lena512.bmp","r");
//read imageHeader and colorTable
unsigned char header[54];//to store the image header
//unsigned char colorTable[1024];//to store the colorTable,if it exists
for(int i=0;i<54;i++){
header[i]=getc(streamIn);
}
/*
width of the image(18th byte)
height of the image(22nd byte)
bitDepth of the image(28th byte)
*/
int width=*(int*)(&header[18]);
int height=*(int*)(&header[22]);
int bitDepth=*(int*)(&header[28]);
}
我遇到了我无法理解的线路。
int width=*(int*)(&header[18]);
为什么我们不能像int width=(int)(header[18]);? 那样简单地进行类型转换
【问题讨论】:
-
因为
header被声明为unsigned char (array of)。另外:最好使用官方的 Windows 标头。 -
澄清一下,
(int)<something>并不意味着将<something>视为int引用。相反,它的意思是将<something>的任何内容转换 为(int)。在这种情况下,<something>是header[18],这是一个unsigned char值。所以这将采用那个一个字节的值并将其扩展为int大小。
标签: c type-conversion bmp bitmapimage unsigned-char