libjpg移植及rgb转rgba

目录

目录

linux下的安装及使用

用法:

rgb转rgba

嵌入式开发版移植。



做OSD叠加图片,芯片厂商的接口需要用rgba图像才能叠加。但是客户端要求直接下发jpg图片。

因此就要自己实现将jpeg转成rgba的逻辑。

这里我们可以使用libjpeg库来实现

下载地址 http://www.ijg.org/

linux下的安装及使用

解压 :tar  -zxvf  jpegsrc.v9d.tar.gz

编译安装:

./configure --prefix=$PWD/_install CC=gcc

静态编译

./configure --prefix=$PWD/_install CC=gcc --enable-static LDFLAGS=-static

make

make install

安装成功后在jpeg-9d/_install/bin下就是编译好的工具了

这里我用到的是 djpeg

这样就得到了目标jpeg图片的rgb文件。

用法:

usage: ./djpeg [switches] [inputfile]
Switches (names may be abbreviated):
  -colors N      Reduce image to no more than N colors
  -fast          Fast, low-quality processing
  -grayscale     Force grayscale output
  -rgb           Force RGB output
  -scale M/N     Scale output image by fraction M/N, eg, 1/8
  -bmp           Select BMP output format (Windows style)
  -gif           Select GIF output format (LZW compressed)
  -gif0          Select GIF output format (uncompressed)
  -os2           Select BMP output format (OS/2 style)
  -pnm           Select PBMPLUS (PPM/PGM) output format (default)
  -targa         Select Targa output format
Switches for advanced users:
  -dct int       Use integer DCT method (default)
  -dct fast      Use fast integer DCT (less accurate)
  -dct float     Use floating-point DCT method
  -dither fs     Use F-S dithering (default)
  -dither none   Don't use dithering in quantization
  -dither ordered  Use ordered dither (medium speed, quality)
  -map FILE      Map to colors used in named image file
  -nosmooth      Don't use high-quality upsampling
  -onepass       Use 1-pass quantization (fast, low quality)
  -maxmemory N   Maximum memory to use (in kbytes)
  -outfile name  Specify name for output file
  -verbose  or  -debug   Emit debug output
 

我这里只列举我用到的用法,其他用法参考 --help

1、djpeg  -rgb  -outfile  out.rgb  in.jpeg

(将in.jpeg转成rgb格式,生成的文件为out.rgb)

2、cjpeg  -scale  1/2  -outfile out.jpeg  in.rgb

(将in.rgb按1/2的比例缩放,生成的文件为out.jpeg)

3、djpeg  -rgb  -scale  1/4  -outfile  out.rgba  in.jpeg

(将in.jpeg按1/4的比例缩放转成rgb格式,生成的文件为out.rgb)

rgb转rgba

我想要的是rgba,因此还要手动加上透明度

先看一下十六进制

前面有一个头信息记录了原始jpeg图片的宽高,这个是不需要的,可以在提取完信息后把它去掉。

上代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
  
char *RgbPash = "./tt.rgb";
char *RgbaPash = "./out.rgba";

int main()
{
	char *RgbBuf = NULL;
	char *RgbaBuf = NULL;
	char Alpha = 0xff;
	
	struct stat statbuf;
	stat(RgbPash, &statbuf);
	
	RgbBuf = (char *)malloc(statbuf.st_size);
	printf("Rgb size = %d\n", (int)statbuf.st_size);
	
	FILE *fp_rgb = fopen(RgbPash, "r");
	if (fp_rgb)
	{
		fread(RgbBuf, 1, statbuf.st_size, fp_rgb);
		fclose(fp_rgb);
	}
	
	int i = 0;
	int j = 0;
	int count = 0;
	int Alpha_flag = 0;
	char size_buf[32] = {};
	FILE *fp_rgba = fopen(RgbaPash, "w");
	if (NULL == fp_rgba)
		return -1;
	
	while (i++ < statbuf.st_size - 1)
	{
		if (('\n' == RgbBuf[i]) && (count < 3))
			count++;

		if ((1 == count) && ('\n' != RgbBuf[i]))
		{
			printf("size %#x\n", RgbBuf[i]);
			if (j < 32)
				strncpy(&size_buf[j++], &RgbBuf[i], sizeof(char));
			
			continue;
		}
			
		if ((count < 3) || ((3 == count) && ('\n' == RgbBuf[i])))
		{
			if  ((3 == count) && ('\n' == RgbBuf[i]))			
				count++;
			
			continue;
		}
			
		if (3 == Alpha_flag)
		{
			Alpha_flag = 0;
			fwrite(&Alpha, 1, 1, fp_rgba);
		}
		
		fwrite(&RgbBuf[i], 1, 1, fp_rgba);
		
		Alpha_flag++;
	}
	fwrite(&Alpha, 1, 1, fp_rgba);
	
	fclose(fp_rgba);
	
	free(RgbBuf);
	RgbBuf = NULL;
	
	printf("Source picture w:h[%s]\n", size_buf);
	
	return 0;
}

 运行

对比一下生成的文件差异(左边是转换后的rgba, 右边是rgb)

我这边透明度是0xFF, 根据需求可以自行修改

 成功。

嵌入式开发版移植。

解压 :tar  -zxvf  jpegsrc.v9d.tar.gz

编译安装:

./configure --prefix=$PWD/_install CC=mips-linux-gnu-gcc --host=mips-linux-gnu --enable-static 

如果要静态编译

./configure --prefix=$PWD/_install CC=mips-linux-gnu-gcc --host=mips-linux-gnu --enable-static LDFLAGS=-static 

我用的是uclibc + 静态编译 的所以

CFLAGS="-muclibc -O2" CPPFLAGS="-muclibc -O2" LDFLAGS="-muclibc -O2" ./configure --prefix=$PWD/_install CC=mips-linux-gnu-gcc --host=mips-linux-gnu --enable-static LDFLAGS=-static 

make

make  install

将编译好的工具放入开发版执行 --help,用以判断是否移植成功。

猜你喜欢

转载自blog.csdn.net/weixin_59665492/article/details/119648452
今日推荐