linux---libjpeg使用(jpg to rgb)

一:libjpeg庫的編譯


下載源碼,解壓後

./configure   --prefix=/xxxx     CC=arm-linux-gcc -    -host=arm-linux    --enable-shared     --enable-static

---xxxx爲生成動靜態褲的目錄

----CC爲交叉編譯器

-----enable-shared    ---enable-static 使能動靜態庫


然後make,

再make install,在xxx目錄下生成相應的頭文件和庫

jconfig.h     jerror.h      jmorecfg.h       jpeglib.h          和   libjpeg.9.so.1.0     libjpeg.a


程序編寫

例子爲framebuffer  jpg圖片格式轉rgb(565)


test.c


#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdio.h>

#include <sys/mman.h>

#include <linux/fb.h>

#include <stdlib.h>


/////////////////頭文件

#include "libinclude/jpeglib.h"

#include "libinclude/jerror.h"



struct fb_var_screeninfo var;

unsigned short *fbp;


//////////////定義結構體

struct jpeg_decompress_struct cinfo;

struct jpeg_error_mgr jerr;


FILE * infile;



int main(int argc ,char **argv)

{

int fd,screen_size,i,j;

unsigned short value;

int pos;

char *rdata;

char *Tbmp[]={"bmp/T1.jpg","bmp/T2.jpg","bmp/T3.jpg","bmp/T4.jpg",

"bmp/T5.jpg","bmp/T6.jpg","bmp/T7.jpg","bmp/T8.jpg","bmp/T9.jpg",

"bmp/T10.jpg","bmp/T11.jpg"};

char  Tpos = 0;


fd = open("/dev/fb0",O_RDWR);

if(fd<0)

perror(" open ");

ioctl(fd,FBIOGET_VSCREENINFO,&var);

printf("var x = %d , y = %d  bits = %d ..\n",var.xres ,var.yres,var.bits_per_pixel);

screen_size = var.xres *var.yres*var.bits_per_pixel/8;

printf("screen_size is %d\n",screen_size);

      fbp =  (unsigned short *)mmap(0, screen_size, PROT_WRITE | PROT_READ,  MAP_SHARED ,  fd, 0);

 if(!fbp)

 goto  mmap_err;




////////////////////////////產生相應變量

cinfo.err = jpeg_std_error(&jerr);

   jpeg_create_decompress(&cinfo);  

while(1)

{




///////////////////打開圖片文件

if ((infile = fopen(Tbmp[Tpos], "rb")) == NULL) {


        fprintf(stderr, "can't open %s\n", "tu.jpg");

        exit(1);

    }

Tpos ++;

if(Tpos>=11)

Tpos = 0;


/////////////////獲取頭信息

jpeg_stdio_src(&cinfo, infile);

jpeg_read_header(&cinfo, TRUE);

////////////////分配內存存儲字節

rdata=(char*)calloc(cinfo.image_width*cinfo.image_height*cinfo.num_components,1);

////////////////開始解壓

jpeg_start_decompress(&cinfo);

JSAMPROW row_pointer[1];

while (cinfo.output_scanline < cinfo.output_height)

{

   row_pointer[0] = & rdata[(cinfo.output_scanline)*cinfo.image_width*cinfo.num_components];

   jpeg_read_scanlines(&cinfo,row_pointer ,1);


}

/////////////結束解壓

jpeg_finish_decompress(&cinfo);

///////////////framebuffer填充剛纔分配的字節

pos = 0;

    for(i=0;i<var.yres;i++)

for(j=0;j<var.xres;j++)

{

////////////rgb888  轉 rgb565

        value=  ((rdata[pos]>>3)<<11) |((rdata[pos+1]>>2)<<5) |(rdata[pos+2]>>3);

*(fbp+i*var.xres+j) = value;

pos +=3;

}

  sleep(3);

fclose(infile);

 

}  

        jpeg_destroy_decompress(&cinfo);

      munmap(fbp, screen_size);

      close(fd);

return 0;


mmap_err:

perror("mmap\n");

return -1;

}




注:jpg默認解壓爲rgb888



用過動態庫和靜態庫使用


靜態庫

此時test.c 目錄下有 test.c   libjpeg.a

jconfig.h     jerror.h      jmorecfg.h       jpeglib.h    


arm-linux-gcc  test.c  -o test   -L./  -ljpeg


生成test放到開發板執行




動態庫

此時test.c 目錄下有test.c     

jconfig.h     jerror.h      jmorecfg.h       jpeglib.h    libjpeg.9.so.1.0  


arm-linux-gcc   test.c -o test   -L./   -l:libjpeg.9.so.1.0


把libjpeg.9.so.1.0改爲libjpeg.9.so  放到開發板  /usr/lib 目錄下


生成test放到開發板執行




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章