天天看點

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 <setjmp.h>
#include<cstdio>
#include "jpeglib.h"
#include <string>
#include<time.h>
#include <sys/time.h>
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<height;i++)
        {
            for(int j=0;j<width;j++)
            {
                int input_base_pos=i*width*4+j*4;
                int now_base_pos=i*width*3+j*3;
                unsigned char alpha=input[input_base_pos+3];
                if(alpha==255||alpha==0)
                {
                    tmp[now_base_pos+2]=input[input_base_pos];
                    tmp[now_base_pos+1]=input[input_base_pos+1];
                    tmp[now_base_pos+0]=input[input_base_pos+2];
                }
                else {
                    unsigned char b=input[input_base_pos];
                    unsigned char g=input[input_base_pos+1];
                    unsigned char r=input[input_base_pos+2];
                    double rate=alpha/255;
                    unsigned char tb=static_cast<unsigned char >(b/rate);
                    unsigned char tg=static_cast<unsigned char >(g/rate);
                    unsigned char tr=static_cast<unsigned char >(r/rate);
                    tmp[now_base_pos+2]=static_cast<unsigned char >(tr+(1-rate)*(255-tr));
                    tmp[now_base_pos+1]=static_cast<unsigned char >(tg+(1-rate)*(255-tg));
                    tmp[now_base_pos+0]=static_cast<unsigned char >(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;
}
           

繼續閱讀