在一些嵌入式项目中可能需要用到jpeg编解码,比如UVC的MJPEG显示, 加载JPEG图片显示到TFT屏幕等,而往往很多通用MCU平台不带硬件jpeg编解码,此时就需要软件实现,这时就希望能有一个好用的jpeg编解码库,恰好官方就提供了这么一个c库即libjpeg。网址为https://libjpeg.sourceforge.net/。可以方便将该库应用到自己的项目中,当然其实现也是学习jpeg编解码实现的不二之选。我们就来移植使用该项目源码,源码默认输入输出是针对文件的,我这里对其进行了一些可移植性改造,可以用户配置输入输出接口,这样可以针对缓存或者文件都可以,源码分享在了https://github.com/qinyunti/libjpeg_port.git.
这里先进入网站下载源码和相关的资料
进入网址https://libjpeg.sourceforge.net/
点击上述链接进入https://www.ijg.org/files/
最新版是jpegsr9f,上面还有一些pdf资料可以参考。
解压下载的压缩包jpegsr9f.zip
里面提供了VS相关的工程可以按照说明直接构建,但是我们这里是要移植到MCU上,所以只使用其源码,其他很多的图像格式处理相关的文件也暂时不添加以后有需要可以使用。
需要使用的源码如下,即只需要j开头的c和h文件。
src
|-- jaricom.c
|-- jcapimin.c
|-- jcapistd.c
|-- jcarith.c
|-- jccoefct.c
|-- jccolor.c
|-- jcdctmgr.c
|-- jchuff.c
|-- jcinit.c
|-- jcmainct.c
|-- jcmarker.c
|-- jcmaster.c
|-- jcomapi.c
|-- jcparam.c
|-- jcprepct.c
|-- jcsample.c
|-- jctrans.c
|-- jdapimin.c
|-- jdapistd.c
|-- jdarith.c
|-- jdatadst.c
|-- jdatasrc.c
|-- jdcoefct.c
|-- jdcolor.c
|-- jddctmgr.c
|-- jdhuff.c
|-- jdinput.c
|-- jdmainct.c
|-- jdmarker.c
|-- jdmaster.c
|-- jdmerge.c
|-- jdpostct.c
|-- jdsample.c
|-- jdtrans.c
|-- jerror.c
|-- jfdctflt.c
|-- jfdctfst.c
|-- jfdctint.c
|-- jidctflt.c
|-- jidctfst.c
|-- jidctint.c
|-- jmemmgr.c
|-- jquant1.c
|-- jquant2.c
`-- jutils.c
0 directories, 45 files
inc/
|-- jdct.h
|-- jerror.h
|-- jinclude.h
|-- jioport.h
|-- jmemsys.h
|-- jmorecfg.h
|-- jpegint.h
|-- jpeglib.h
`-- jversion.h
0 directories, 9 files
port/
|-- jioport.c
|-- jmemansi.c
|-- jmemdos.c
|-- jmemmac.c
|-- jmemname.c
|-- jmemnobs.c
`-- jmemport.c
0 directories, 7 files
我这里的源码如下
使用jconfig.h进行编译时静态配置
该文件由源码目录下的jconfig.txt生成,我们这里复制该文件手动修改为jconfig.h添加到自己的工程中去。
会有很多warning: unused parameter所以我们直接
添加编译参数-Wno-unused-variable,-Wno-unused-parameter忽略该告警。
我这里不使用stdlib.h的malloc和free所以
jpegsr9f/inc/jconfig.h中
删除#define HAVE_STDLIB_H
参考jmemnobs.c新建我们自己的jmemport.c
实现以下接口
jpeg_get_small
jpeg_free_small
jpeg_get_large
jpeg_free_large
jpeg_mem_available
jpeg_open_backing_store
jpeg_mem_init
jpeg_mem_term
//#ifndef HAVE_STDLIB_H /*
should declare malloc(),free() */ //extern void * malloc JPP((size_t size));
//extern void free JPP((void *ptr));
//#endif
替换malloc和free为我们自己的实现,我这里使用的是freertos
pvPortMalloc和vPortFree,如果支持标准c的malloc和free则可以使用jmemansi.c
实现如下
/*
* jmemport.c
*
* Copyright (C) 1992-1996, Thomas G. Lane.
* Modified 2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file provides a really simple implementation of the system-
* dependent portion of the JPEG memory manager. This implementation
* assumes that no backing-store files are needed: all required space
* can be obtained from malloc().
* This is very portable in the sense that it'll compile on almost anything,
* but you'd better have lots of main memory (or virtual memory) if you want
* to process big images.
* Note that the max_memory_to_use option is respected by this implementation.
*/
//#ifndef HAVE_STDLIB_H /*
should declare malloc(),free() */ //extern void * malloc JPP((size_t size));
//extern void free JPP((void *ptr));
//#endif
/*
* Memory allocation and freeing are controlled by the regular library
* routines malloc() and free().
*/
GLOBAL(void *)
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
{
void* ret;
ret = (void *) pvPortMalloc(sizeofobject);
jprintf("jpeg_get_small %p size:%ld r\n",ret, sizeofobject);
return ret;
}
GLOBAL(void)
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
{
jprintf("jpeg_free_small %p \r\n",object);
vPortFree(object);
}
/*
* "Large" objects are treated the same as "small" ones.
* NB: although we include FAR keywords in the routine declarations,
* this file won't actually work in 80x86 small/medium model; at least,
* you probably won't be able to process useful-size images in only 64KB.
*/
GLOBAL(void FAR *)
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
{
void* ret;
ret = (void FAR *) pvPortMalloc(sizeofobject);
jprintf("jpeg_get_small %p size:%ld r\n",ret, sizeofobject);
return ret;
}
GLOBAL(void)
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
{
jprintf("jpeg_free_large %p \r\n",object);
vPortFree(object);
}
/*
* This routine computes the total memory space available for allocation.
*/
GLOBAL(long)
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
long max_bytes_needed, long already_allocated)
{
if (cinfo->mem->max_memory_to_use)
return cinfo->mem->max_memory_to_use - already_allocated;
/* Here we say, "we got all you want bud!" */
return max_bytes_needed;
}
/*
* Backing store (temporary file) management.
* Since jpeg_mem_available always promised the moon,
* this should never be called and we can just error out.
*/
GLOBAL(void)
jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
long total_bytes_needed)
{
ERREXIT(cinfo, JERR_NO_BACKING_STORE);
}
/*
* These routines take care of any system-dependent initialization and
* cleanup required. Here, there isn't any.
*/
GLOBAL(long)
jpeg_mem_init (j_common_ptr cinfo)
{
return 0; /* just set max_memory_to_use to 0 */
}
GLOBAL(void)
jpeg_mem_term (j_common_ptr cinfo)
{
/* no work */
}
jpegsr9f/src/jdatadst.c中
extern void * malloc JPP((size_t size));
extern void free JPP((void *ptr));
不使用stdlib则这里依赖外部实现的malloc和free,这里可移植性不好,这里直接依赖mem接口即。
注释掉
//#ifndef HAVE_STDLIB_H /*
should declare malloc(),free() */ //extern void * malloc JPP((size_t size));
//extern void free JPP((void *ptr));
//#endif
后面的malloc改为jpeg_get_small
free改为jpeg_free_small
nextbuffer = (JOCTET *) malloc(nextsize);
改为
nextbuffer = (JOCTET *) jpeg_get_small(0,nextsize);
dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE);
改为
dest->newbuffer = *outbuffer = (unsigned char *) jpeg_get_small(0,OUTPUT_BUF_SIZE);
free(dest->newbuffer);
改为
jpeg_free_small(0,dest->newbuffer,0);
jpegsr9f/src/jdatadst.c中需要
#include "jmemsys.h"
jpegsr9f/src/jmemmgr.c中
extern char * getenv JPP((const char * name));
我们不使用getenv则需要定义NO_GETENV
jpegsr9f/inc/jconfig.h中添加
#define NO_GETENV
我们在jpegsr9f/inc/jconfig.h中
定义#define JPEG_USE_FILE_IO_CUSTOM
不定义JPEG_HAVE_FILE_IO_CUSTOM
走如下路线
即用户需要实现
jfread
jfwrite
jfflush
jferror
jpegsr9f/inc/jinclude.h中,FILE*改为void*,这样可移植
extern size_t jfread(void * __ptr, size_t __size, size_t __n, FILE * __stream);
extern size_t jfwrite(const void * __ptr, size_t __size, size_t __n, FILE * __stream);
extern int jfflush(FILE * __stream);
extern int jferror(FILE * __fp);
改为
extern size_t jfread(void * __ptr, size_t __size, size_t __n, void * __stream);
extern size_t jfwrite(const void * __ptr, size_t __size, size_t __n, void * __stream);
extern int jfflush(void * __stream);
extern int jferror(void * __fp);
为了可移植性
jdatadst.c
jdatasrc.c,
jmemsys.h
jpeglib.h
中FILE改为void
为了方便适配不同的输入输出方式,我这里添加了jioport.c/jioport.h可以用户设置对应的接口
h文件
extern "C" {
typedef struct
{
size_t (*read)(void * __ptr, size_t __size, size_t __n, void * __stream);
size_t (*write)(const void * __ptr, size_t __size, size_t __n, void * __stream);
int (*flush)(void * __stream);
int (*error)(void * __fp);
size_t infile_len;
} jioport_st;
size_t jfread(void * __ptr, size_t __size, size_t __n, void * __stream);
size_t jfwrite(const void * __ptr, size_t __size, size_t __n, void * __stream);
int jfflush(void * __stream);
int jferror(void * __fp);
void jioport_set(jioport_st* port);
}
c文件
jioport_st* s_jioport = (jioport_st*)0;
size_t jfread(void * __ptr, size_t __size, size_t __n, void * __stream)
{
jprintf("jfread ptr:%p size:%ld n:%ld stream:%p\r\n",__ptr,__size,__n,__stream);
if((s_jioport != (jioport_st*)0) && (s_jioport->read != 0))
{
return s_jioport->read(__ptr, __size, __n, __stream);
}
return 0;
}
size_t jfwrite(const void * __ptr, size_t __size, size_t __n, void * __stream)
{
jprintf("jfwrite ptr:%p size:%ld n:%ld stream:%p\r\n",__ptr,__size,__n,__stream);
if((s_jioport != (jioport_st*)0) && (s_jioport->write != 0))
{
return s_jioport->write(__ptr, __size, __n, __stream);
}
return 0;
}
int jfflush(void * __stream)
{
jprintf("jfflush stream:%p\r\n",__stream);
if((s_jioport != (jioport_st*)0) && (s_jioport->flush != 0))
{
return s_jioport->flush(__stream);
}
return 0;
}
int jferror(void * __fp)
{
jprintf("jferror fp:%p\r\n",__fp);
if((s_jioport != (jioport_st*)0) && (s_jioport->error != 0))
{
return s_jioport->flush(__fp);
}
return 0;
}
void jioport_set(jioport_st* port)
{
s_jioport = port;
}
jpegsr9f/src/jerror.c中
注释掉 //exit(EXIT_FAILURE);
因为一般MCU平台没有exit。
fprintf(stderr, "%s\n", buffer);
改为
jprintf("%s\n", buffer);
jpegsr9f/inc/jconfig.h中实现jprintf宏
可以直接在pc上测试,参加git源码
编译
./build.sh
源码见
jpeg_encode_buffer.c/h
encode_test.c
注意输入bmp图片必须是24位格式,且一行数据量要是4的倍数,即widthx3是4的倍数.
./encode_test testimg.bmp testimg.jpg 320 240 50
生成testimg.jpg如下
源码见
jpeg_decode_buffer.c/h
decode_test.c
./decode_test testimg.jpg testimg.rgb 320
使用YUView软件查看输出文件testimg.rgb
ligjpeg是Tom Lane和IJG开发的对jpeg的实现,广泛使用。本文分享了将其进行简单的移植改造适合MCU等任意嵌入式平台使用。
以上测试是在pc上进行,在mcu上比如freertos上则只需要使用jmemport.c,如果不是使用freertos则替换自己的动态分配接口,使用jconfig.h修改#define jprintf(fmt, arg...) printf(fmt, ##arg)宏为自己的打印宏即可。