JPEG是一种常用的图像格式,它使用有损压缩算法来减小文件大小,在C语言中,我们可以使用开源库libjpeg来实现JPEG图像的读写操作,以下是一个简单的C语言实现JPEG图像的教程。
(图片来源网络,侵删)1、我们需要安装libjpeg库,在Ubuntu系统中,可以使用以下命令安装:
sudo aptget install libjpegdev
2、创建一个名为jpeg_example.c
的C语言源文件,并添加以下代码:
#include <stdio.h>#include <stdlib.h>#include <jpeglib.h>#include <setjmp.h>typedef struct { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *infile; JSAMPARRAY buffer; int row_stride;} my_source_ptr;typedef struct { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *outfile; JSAMPARRAY buffer;} my_destination_ptr;void read_file(my_source_ptr *src) { if (setjmp(src>jerr.setjmp_buffer)) { printf("Error during reading file"); exit(1); } src>infile = fopen("input.jpg", "rb"); if (!src>infile) { printf("Error opening input file"); exit(1); } jpeg_create_decompress(&src>cinfo); jpeg_stdio_src(&src>cinfo, src>infile); (void) jpeg_read_header(&src>cinfo, TRUE); (void) jpeg_start_decompress(&src>cinfo); src>row_stride = src>cinfo.output_width * src>cinfo.output_components; src>buffer = (*cinfo.mem>alloc_sarray)((j_common_ptr) &src>cinfo, JPOOL_IMAGE, src>row_stride, src>cinfo.output_height);}void write_file(my_destination_ptr *dst) { if (setjmp(dst>jerr.setjmp_buffer)) { printf("Error during writing file"); exit(1); } dst>outfile = fopen("output.jpg", "wb"); if (!dst>outfile) { printf("Error opening output file"); exit(1); } jpeg_create_compress(&dst>cinfo); dst>cinfo.image_width = dst>cinfo.output_width = dst>cinfo.output_components = dst>cinfo.input_components = dst>cinfo.input_chroma_subsampling = dst>cinfo.input_chroma_samplesize = dst>cinfo.input_scanlinesize = dst>cinfo.input_components; dst>buffer = (*cinfo.mem>alloc_sarray)((j_common_ptr) &dst>cinfo, JPOOL_IMAGE, dst>row_stride, dst>cinfo.output_height); jpeg_stdio_dest(&dst>cinfo, dst>outfile); (void) jpeg_set_defaults(&dst>cinfo); (void) jpeg_set_quality(&dst>cinfo, 90, TRUE); (void) jpeg_start_compress(&dst>cinfo, TRUE);}
3、编译并运行代码:
gcc o jpeg_example jpeg_example.c ljpeg lm Wall Wextra Werror pedantic std=c99 O3 ffastmath funrollloops msse3 msse4.1 msse4.2 mavx mfma m64 I/usr/include/libjpegturbo I/usr/include/libpng16 I/usr/include/libpng16/pngconf I/usr/include/zlib L/usr/lib/x86_64linuxgnu lpng16 lturbojpeg lz lm lgomp lpthread ldl lbz2 lzstd latomic lcurl lssl lcrypto lXext lX11 lXau lXdmcp lvpx lvfr lxcb lxcbshm lxcbxfixes lxcbshape lxcbrandr lxcbrender lxcbxinerama lxcbkeysyms lxcbxkb lxkbcommon lpcre28 lrt ldl lpthread lzstd lbz2 lm lgomp ldl lzstd lbz2 lm lgomp ldl lzstd lbz2 lm lgomp ldl lzstd lbz2 lm lgomp ldl lzstd lbz2 lm lgomp ldl lzstd lbz2 lm lgomp ldl lzstd lbz2 lm lgomp ldl lzstd lbz2 lm lgomp ldl noundefined asneeded enableshared disablestatic enablestripping disableprograms disablenls disablewerror prefix=/usr withsystemzlib withpic disablerpath enableavx512bw enableavx512cd enableavx512dq enableavx512vl enableavx512f enableinlineavx512 enableunalignedload enableunalignedstore enablevpclmulb enablevpclmulqdq enablepopcnt enablelz4 enablezstd enablelibpng enablelibwebp enablelibjpegturbo enablelibtiff enablelibgif disablelibxml2 disablelibvorbis disablelibtheora disablelibvpx disablelibsm disablelibxslt disablelibxcb disablelibbsd disableglx disableegl disableosmesa disabledri disableva withdrm=nouveau withdridriver=nouveau #withoutmmx #withoutsse #withoutmmxext #withoutsse2 #withoutsse3 #withoutsse4 #without3dnow #withoutavx #withoutavx2 #withoutfma #withoutamd3dnowext #withoutamd3dnow #withoutamd3dnowext #withoutamd3dnow #withoutpopcnt #withoutrtmminmax #withoutinlineasm #withoutvisibility #withoutgcseligible #withoutfpmath=sse #withoutfpmath=sse2 #withoutfpmath=387 #withoutfpmath=altivec #withoutfpmath=vmx #withoutfpmath=armv8 #withoutfpmath=native #withoutsimd=neon #withoutsimd=armv8 #withoutsimd=altivec #withoutsimd=vmx #withoutsimd=3dnowproassist #withoutsimd=3dnow #withoutsimd=mmx #withoutsimd=sse2 #withoutsimd=sse41 #withoutsimd=sse42 #withoutsimd=sse4a #withoutsimd=avx #withoutsimd=avx2 #withoutsimd=avx512f #withoutsimd=avx512cd #withoutsimd=avx512bw #withoutsimd=avx512vl #withoutsimd=avx512vnni #CFLAGS=march=native CXXFLAGS=march=native LDFLAGS=march=