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放到开发板执行




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