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

#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 

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

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 23:20:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 23:20:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 23:20:02       82 阅读
  4. Python语言-面向对象

    2023-12-21 23:20:02       91 阅读

热门阅读

  1. 随记-探究 OpenApi 的加密方式

    2023-12-21 23:20:02       63 阅读
  2. linux DHCP赛题配置

    2023-12-21 23:20:02       55 阅读
  3. 【蓝桥杯备考资料】Python基础语法

    2023-12-21 23:20:02       58 阅读
  4. 应急响应常用命令

    2023-12-21 23:20:02       49 阅读
  5. conda 虚拟环境使用

    2023-12-21 23:20:02       61 阅读
  6. c++刷题leetcode常见报错(持续更新)

    2023-12-21 23:20:02       66 阅读
  7. 晶振选型参考

    2023-12-21 23:20:02       57 阅读
  8. 5-Docker实例-nginx application

    2023-12-21 23:20:02       52 阅读
  9. Linux | 数据结构之内核链表

    2023-12-21 23:20:02       68 阅读