linux c编程之libjpeg的使用和rgb图片格式查看

2023-12-20 19:14:04
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "jpeglib.h"

#define DEBUG_RGB_OUT  (1)

#define FILE_NAME 	 "./test.jpg"
#define RGB_FILE     "./test.rgb"

static unsigned char* jpeg_dec(char*filename,int*width,int*height)
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerrr;
	JSAMPARRAY buffer;
	int row_width;
	unsigned char *tmp = NULL;
	unsigned char *rgb_buffer;
	uint32_t i = 0;
	int ret = -1;
	
	FILE * infile = fopen(filename, "rb");
	if(!infile)
	{
		printf("not jpg file%s\r\n",filename);
		return NULL;
	}
#ifdef DEBUG_RGB_OUT
	FILE * outfile = fopen(RGB_FILE, "wb");
	if(!outfile)
	{
		printf("not RGB_FILE jpg file%s\r\n",RGB_FILE);
		return NULL;
	}
#endif 

	cinfo.scale_num = cinfo.scale_denom = 1/2 ;	
	cinfo.err = jpeg_std_error(&jerrr);
	jpeg_create_decompress(&cinfo);
	jpeg_stdio_src(&cinfo,infile);
	jpeg_read_header(&cinfo,TRUE);
	jpeg_start_decompress(&cinfo);
	*width = cinfo.output_width;
	*height = cinfo.output_height;
	row_width = cinfo.output_width * cinfo.output_components;
		
	buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_width, 1);
	rgb_buffer = (unsigned char *)malloc(row_width * cinfo.output_height);
	memset(rgb_buffer, 0, row_width * cinfo.output_height);
	tmp = rgb_buffer;


	while(cinfo.output_scanline < cinfo.output_height)
	{
		 ret = jpeg_read_scanlines(&cinfo,buffer,1);
		 memcpy(tmp,*buffer,row_width);

#ifdef DEBUG_RGB_OUT
		fwrite(tmp,row_width,1,outfile);
#endif 
	
		 tmp += row_width;
		 i +=row_width;
	}

	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	fclose(infile);

#ifdef DEBUG_RGB_OUT
	fclose(outfile);
#endif

	return rgb_buffer;
	
}


int main()
{
	int width;
	int height;
	
	jpeg_dec(FILE_NAME,&width,&height);
	
	return 0;
}

在这里插入图片描述

gcc jpeg.c -o jpeg  -ljpeg -I jpeg-6b/
./jpeg

ls -l test.rgb
工具使用:
分辨率选择: 1920x1080
像素格式选择: RGB24 

工具分享:
链接:https://pan.baidu.com/s/1MfpqsQenO1dAwdeloMMtMQ?pwd=9swr 
提取码:9swr 

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

文章来源:https://blog.csdn.net/M_1308347688/article/details/135114197
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。