LIBJPEG交叉编译并用其将ARGB32转JPEG

#编译脚本 build.sh
#!/bin/bash
gcc_prefix=$1
install_dir=$2
cache_dir=$3
host_name=" "
if  test -z ${gcc_prefix} ;then
	gcc_prefix=/usr/bin/
else
	host_name=arm-linux
fi
if  test -z ${install_dir} ;then
	install_dir=$(pwd)/install
fi
if  test -z ${cache_dir} ;then
	cache_dir=$(pwd)/out
fi
echo "gcc_prefix=${gcc_prefix}"
echo "install_dir=${install_dir}"
echo "cache_dir=${cache_dir}"
echo "host_name=${host_name}"
cd ./libjpeg;
./configure  --prefix=${cache_dir} --exec-prefix=${cache_dir}  --enable-shared --enable-static --host=${host_name} CC=${gcc_prefix}gcc LD=${gcc_prefix}ld
make -s &&make install
rm -rf ${install_dir}
mkdir -p ${install_dir}
mkdir -p ${install_dir}/lib
cp -r ${cache_dir}/include ${install_dir}
cp -r ${cache_dir}/lib/libjpeg.a ${install_dir}/lib
#rm ${cache_dir} -rf

#测试文件 main.cpp
#include 
#include
#include "jpeglib.h"
#include 
#include
#include 
unsigned long rgb32_to_jpeg(int width,int height,unsigned char **save,unsigned char *input,int quality)
{
    //argb : bgra bgra bgra
    //rgb24: bgr bgr bgr
    //像素转换
    unsigned char *tmp=new unsigned char[width*height*3];
        for(int i= 0;i(b/rate);
                    unsigned char tg=static_cast(g/rate);
                    unsigned char tr=static_cast(r/rate);
                    tmp[now_base_pos+2]=static_cast(tr+(1-rate)*(255-tr));
                    tmp[now_base_pos+1]=static_cast(tg+(1-rate)*(255-tg));
                    tmp[now_base_pos+0]=static_cast(tb+(1-rate)*(255-tb));
                }
            }
        }
    struct jpeg_compress_struct jpeg;
    struct jpeg_error_mgr jerr;
    //错误输出在绑定
    jpeg.err = jpeg_std_error(&jerr);
    //初始化压缩对象
    jpeg_create_compress(&jpeg);
    //压缩参数设置
    jpeg.image_width = width;
    jpeg.image_height = height;
    jpeg.input_components  = 3;
    jpeg.in_color_space = JCS_RGB;
    //参数设置为默认的
    jpeg_set_defaults(&jpeg);
    //还可以设置些其他参数:
     设定编码jpeg压缩质量
    jpeg_set_quality(&jpeg, quality, TRUE);
    unsigned long ret=width*height*3;
    *save=new unsigned char[ret];
    jpeg_mem_dest(&jpeg,save,&ret);
    //开始压缩。执行这一行数据后,无法再设置参数了!
    jpeg_start_compress(&jpeg, TRUE);

    JSAMPROW row_pointer[1];
    int row_stride = width*3;
    //从上到下,设置图片中每一行的像素值
    int i=0;
    while(jpeg.next_scanline < jpeg.image_height)
    {
        row_pointer[0] = & tmp[jpeg.next_scanline * row_stride];
        (void)jpeg_write_scanlines(&jpeg, row_pointer, 1);
        i++;
        //printf("i %d \r\n",i);
    }
    delete [] tmp;
    //结束压缩
    jpeg_finish_compress(&jpeg);
    jpeg_destroy_compress(&jpeg);
    return ret;
}
int main(int argc,char*argv[])
{
    if(argc<2)return 0;
    FILE *fp=fopen(argv[1],"rb");
    fseek(fp,0,SEEK_END);
    auto size=ftell(fp);
    printf("file_size %d\r\n",size);
    if(size<=0)return 0;
    fseek(fp,0,SEEK_SET);
    auto buf=new  unsigned char[size];
    auto ret=fread(buf,1,size,fp);
    printf("rgb _size %lu \r\n",ret);
    fclose(fp);
    int width=234;
    int height=318;
    unsigned char *save;
    struct timeval now;
    gettimeofday(&now,nullptr);
    auto jpg_size=rgb32_to_jpeg(width,height,&save,buf,100);
    struct timeval done;
    gettimeofday(&done,nullptr);
    printf("%lu  escape time %ld us\r\n",jpg_size,(done.tv_sec-now.tv_sec)*1000*1000+done.tv_usec-now.tv_usec);
    fp=fopen("test.jpg","wb+");
    fwrite(save,1,jpg_size,fp);
    delete [] save;
    fclose(fp);
    return 0;
}

 

你可能感兴趣的:(C/C++,C/C,嵌入式开发)