unsigned short int定义的一维数组与unsigned char定义的一维数组有什么不同
时间: 2024-06-04 09:08:42 浏览: 15
在C语言中,unsigned short int和unsigned char都是基本数据类型。它们之间的主要区别在于它们所占用的内存大小不同。
- unsigned short int通常占用2个字节(16位)的内存空间,它能够存储的数字范围是0到65535。
- unsigned char通常占用1个字节(8位)的内存空间,它能够存储的数字范围是0到255。
因此,如果将它们分别用于定义一维数组,这些数组的元素大小和占用的内存空间也会有所不同。例如,定义一个包含10个元素的unsigned short int数组,它将占用20个字节的内存空间;而定义一个包含10个元素的unsigned char数组,它将占用10个字节的内存空间。
在使用这些数组时,需要注意它们的数据类型和所占用的内存空间大小,以确保正确使用它们。
相关问题
用c语言实现一个对一个BMP灰度图片文件读取,将它保存到一个二维数组里面
#include <stdio.h>
#include <stdlib.h>
// 定义BMP文件头结构体
typedef struct {
unsigned short bfType; // BMP文件类型,必须是0x424D,即字符“BM”
unsigned int bfSize; // BMP文件大小
unsigned short bfReserved1; // 保留字段1
unsigned short bfReserved2; // 保留字段2
unsigned int bfOffBits; // BMP文件数据偏移量,即文件头+位图信息头大小
} __attribute__((packed)) BMPFileHeader;
// 定义BMP位图信息头结构体
typedef struct {
unsigned int biSize; // 位图信息头大小,即40字节
int biWidth; // 位图宽度
int biHeight; // 位图高度,正数表示正向,负数表示倒置
unsigned short biPlanes; // 总是为1
unsigned short biBitCount; // 位图颜色位数,即每个像素占用位数
unsigned int biCompression; // 采用的压缩方法,0为不压缩
unsigned int biSizeImage; // 数据区大小,0表示没有压缩
int biXPelsPerMeter; // 水平分辨率
int biYPelsPerMeter; // 垂直分辨率
unsigned int biClrUsed; // 颜色表中实际使用的颜色数,0表示使用全部颜色
unsigned int biClrImportant;// 针对重要的颜色数,0表示所有颜色都重要
} __attribute__((packed)) BMPInfoHeader;
int main() {
// 打开BMP文件
FILE *fp = fopen("test.bmp", "rb");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
// 读取BMP文件头
BMPFileHeader bmpFileHeader;
fread(&bmpFileHeader, sizeof(BMPFileHeader), 1, fp);
// 判断是否为BMP文件
if (bmpFileHeader.bfType != 0x4D42) {
printf("This is not a BMP file!\n");
fclose(fp);
exit(2);
}
// 读取BMP位图信息头
BMPInfoHeader bmpInfoHeader;
fread(&bmpInfoHeader, sizeof(BMPInfoHeader), 1, fp);
// 判断是否为灰度图像
if (bmpInfoHeader.biBitCount != 8) {
printf("This is not a grayscale BMP file!\n");
fclose(fp);
exit(3);
}
// 读取颜色表
unsigned char colorTable[1024];
fread(colorTable, sizeof(unsigned char), 1024, fp);
// 分配二维数组存储图像数据
unsigned char **imageData = (unsigned char **)malloc(bmpInfoHeader.biHeight * sizeof(unsigned char *));
for (int i = 0; i < bmpInfoHeader.biHeight; i++) {
imageData[i] = (unsigned char *)malloc(bmpInfoHeader.biWidth * sizeof(unsigned char));
fread(&imageData[i][0], sizeof(unsigned char), bmpInfoHeader.biWidth, fp);
}
// 关闭文件
fclose(fp);
// 输出图像数据
for (int i = 0; i < bmpInfoHeader.biHeight; i++) {
for (int j = 0; j < bmpInfoHeader.biWidth; j++) {
printf("%d ", imageData[i][j]);
}
printf("\n");
}
// 释放内存
for (int i = 0; i < bmpInfoHeader.biHeight; i++) {
free(imageData[i]);
}
free(imageData);
return 0;
}
请你用c语言写一段代码,实现如下功能:读取一张bmp格式的图像到一维数组中,并将每一个像素点的四个分量,即A,R,G,B四个分量,分别存到一个数组中,并将他们的值打印到4个txt文件中,每个数以逗号隔开,txt文件中每一行的数的个数和bmp图片每一行的像素数一致。
好的,以下是实现上述功能的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma pack(1) // 结构体按1字节对齐
// BMP文件头结构体
typedef struct {
unsigned short bfType; // 文件类型,必须为0x4D42
unsigned int bfSize; // 文件大小,单位为字节
unsigned short bfReserved1; // 保留字段1,必须为0
unsigned short bfReserved2; // 保留字段2,必须为0
unsigned int bfOffBits; // 从文件头到像素数据的偏移量,单位为字节
} BMPFileHeader;
// BMP信息头结构体
typedef struct {
unsigned int biSize; // 信息头大小,必须为40
int biWidth; // 图像宽度,单位为像素
int biHeight; // 图像高度,单位为像素
unsigned short biPlanes; // 颜色平面数,必须为1
unsigned short biBitCount; // 每个像素的位数,必须为24
unsigned int biCompression; // 压缩类型,必须为0
unsigned int biSizeImage; // 图像数据大小,单位为字节
int biXPelsPerMeter; // 水平分辨率,单位为像素/米
int biYPelsPerMeter; // 垂直分辨率,单位为像素/米
unsigned int biClrUsed; // 使用的颜色数,必须为0
unsigned int biClrImportant; // 重要的颜色数,必须为0
} BMPInfoHeader;
// BMP像素结构体
typedef struct {
unsigned char b; // 蓝色分量
unsigned char g; // 绿色分量
unsigned char r; // 红色分量
unsigned char a; // Alpha分量
} BMPPixel;
int main() {
char bmpFileName[] = "test.bmp"; // BMP文件名
char aFileName[] = "a.txt"; // Alpha分量文件名
char rFileName[] = "r.txt"; // 红色分量文件名
char gFileName[] = "g.txt"; // 绿色分量文件名
char bFileName[] = "b.txt"; // 蓝色分量文件名
// 打开BMP文件
FILE* bmpFile = fopen(bmpFileName, "rb");
if (!bmpFile) {
printf("Failed to open BMP file!\n");
return 1;
}
// 读取BMP文件头
BMPFileHeader fileHeader;
fread(&fileHeader, sizeof(BMPFileHeader), 1, bmpFile);
// 检查文件类型是否为0x4D42
if (fileHeader.bfType != 0x4D42) {
printf("Invalid BMP file!\n");
fclose(bmpFile);
return 1;
}
// 读取BMP信息头
BMPInfoHeader infoHeader;
fread(&infoHeader, sizeof(BMPInfoHeader), 1, bmpFile);
// 检查位数是否为24
if (infoHeader.biBitCount != 24) {
printf("Unsupported BMP format!\n");
fclose(bmpFile);
return 1;
}
// 计算每行像素所占的字节数
int rowSize = (infoHeader.biWidth * 3 + 3) / 4 * 4;
// 分配像素数组内存
BMPPixel* pixels = (BMPPixel*)malloc(infoHeader.biWidth * infoHeader.biHeight * sizeof(BMPPixel));
// 读取像素数据
fseek(bmpFile, fileHeader.bfOffBits, SEEK_SET);
for (int y = 0; y < infoHeader.biHeight; y++) {
for (int x = 0; x < infoHeader.biWidth; x++) {
BMPPixel* pixel = &pixels[y * infoHeader.biWidth + x];
fread(pixel, sizeof(BMPPixel), 1, bmpFile);
}
fseek(bmpFile, rowSize - infoHeader.biWidth * sizeof(BMPPixel), SEEK_CUR);
}
// 关闭BMP文件
fclose(bmpFile);
// 打开Alpha分量文件
FILE* aFile = fopen(aFileName, "w");
if (!aFile) {
printf("Failed to open Alpha file!\n");
free(pixels);
return 1;
}
// 打开红色分量文件
FILE* rFile = fopen(rFileName, "w");
if (!rFile) {
printf("Failed to open Red file!\n");
fclose(aFile);
free(pixels);
return 1;
}
// 打开绿色分量文件
FILE* gFile = fopen(gFileName, "w");
if (!gFile) {
printf("Failed to open Green file!\n");
fclose(aFile);
fclose(rFile);
free(pixels);
return 1;
}
// 打开蓝色分量文件
FILE* bFile = fopen(bFileName, "w");
if (!bFile) {
printf("Failed to open Blue file!\n");
fclose(aFile);
fclose(rFile);
fclose(gFile);
free(pixels);
return 1;
}
// 写入分量数据到文件
for (int y = 0; y < infoHeader.biHeight; y++) {
for (int x = 0; x < infoHeader.biWidth; x++) {
BMPPixel* pixel = &pixels[y * infoHeader.biWidth + x];
fprintf(aFile, "%d,", pixel->a);
fprintf(rFile, "%d,", pixel->r);
fprintf(gFile, "%d,", pixel->g);
fprintf(bFile, "%d,", pixel->b);
}
fprintf(aFile, "\n");
fprintf(rFile, "\n");
fprintf(gFile, "\n");
fprintf(bFile, "\n");
}
// 关闭文件
fclose(aFile);
fclose(rFile);
fclose(gFile);
fclose(bFile);
// 释放内存
free(pixels);
printf("Done!\n");
return 0;
}
```
这段代码可以读取一张BMP格式的图像到一维数组中,并将每个像素点的四个分量分别存到一个数组中,并将它们的值打印到4个txt文件中,每个数以逗号隔开,txt文件中每一行的数的个数和BMP图片每一行的像素数一致。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)