帮我写一份用C语言打开jpg图像,并且显示图像像素点矩阵数据的代码
时间: 2023-05-20 14:03:11 浏览: 186
以下是用C语言打开jpg图像,并且显示图像像素点矩阵数据的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
char *filename = argv[1];
FILE *fp = fopen(filename, "rb");
if (!fp) {
printf("Failed to open file %s\n", filename);
return 1;
}
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
int width = cinfo.output_width;
int height = cinfo.output_height;
int num_components = cinfo.output_components;
int row_stride = width * num_components;
unsigned char *buffer = (unsigned char *)malloc(row_stride * height);
unsigned char *row_pointer = buffer;
while (cinfo.output_scanline < height) {
jpeg_read_scanlines(&cinfo, &row_pointer, 1);
row_pointer += row_stride;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
printf("Image width: %d\n", width);
printf("Image height: %d\n", height);
printf("Number of components: %d\n", num_components);
printf("Pixel data:\n");
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
for (int k = 0; k < num_components; k++) {
printf("%d ", buffer[i * row_stride + j * num_components + k]);
}
printf("\n");
}
}
free(buffer);
return 0;
}
```
相关推荐
![](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)
![](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)