framebuffer下用libjeg库显示jpeg图片源代码:(包含用libjpeg显示jpeg格式图片代码)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #include <linux/fb.h>
- #include <unistd.h>
- #include <jpeglib.h>
- #define FB_DEV "/dev/fb0"
- #define JPEG 0x001
- #define PNG 0x010
- #define BMP 0x100
- #define UNKNOWN 0x111
- static int fb_dev;
- static struct fb_var_screeninfo fb_var;
- static int screen_w, screen_h, screen_bits;
- static void *fbmem, *data;
- static FILE *infile;
-
- typedef struct jpeg_color
- {
- unsigned char r;
- unsigned char g;
- unsigned char b;
- }COLOR;;
- typedef struct jpeg_pixel_t
- {
- unsigned char r;
- unsigned char g;
- unsigned char b;
- unsigned char a;
- }PIXEL_T;
-
- typedef struct bmp_file_header
- {
- char cfType[2];
- char cfSize[4];
- char cfReserved[4];
- char cfoffBits[4];
- }__attribute__((packed)) BITMAPFILEHEADER;
- typedef struct bmp_info_header
- {
- char ciSize[4];
- char ciWidth[4];
- char ciHeight[4];
- char ciPlanes[2];
- char ciBitCount[2];
- char ciCompress[4];
- char ciSizeImage[4];
- char ciXPelsPerMeter[4];
- char ciYPelsPerMeter[4];
- char ciClrUsed[4];
- char ciClrImportant[4];
- }__attribute__((packed)) BITMAPINFOHEADER;
-
- void fb_init()
- {
-
- fb_dev = open(FB_DEV, O_RDWR);
- if(fb_dev < 0)
- {
- printf("Error:open %s error/n", FB_DEV);
- printf("Usage:[sudo ./digital_frame xxx.jpg]/n");
- exit(1);
- }
-
- ioctl(fb_dev, FBIOGET_VSCREENINFO, &fb_var);
- screen_w = fb_var.xres;
- screen_h = fb_var.yres;
- screen_bits = fb_var.bits_per_pixel;
- printf("Framebuffer:%d * %d/n", screen_w, screen_h);
- printf("screen_bits:%d/n", screen_bits);
- fbmem = mmap(0, screen_w * screen_h * screen_bits / 8
- , PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev, 0);
- return;
- }
-
- int judge_picture_mode(char **argv)
- {
- char header[8];
- printf("picture = %s/n", argv[1]);
- infile = fopen(argv[1], "rb");
- if(infile == NULL)
- {
- printf("Error:open %s error!/n", argv[1]);
- exit(1);
- }
- memset(header, 0, sizeof(header));
- fread(&header, 8, 1, infile);
- fseek(infile, -8, 1);
- if((unsigned char)header[0] == 0xff)
- {
- return JPEG;
- }
- else if((unsigned char)header[0] == 0x89)
- {
- return PNG;
- }
- else if(0 == strncmp(header, "BM", 2))
- {
- return BMP;
- }
- else
- {
- return UNKNOWN;
- }
- }
-
- void display_jpeg()
- {
- struct jpeg_decompress_struct cinfo;
- struct jpeg_error_mgr jerr;
- int picture_w, picture_h, picture_bits;
- int i, j, x, y;
- unsigned char *buffer, *tmpbuf;
- COLOR picture_color;
- PIXEL_T **fbp;
- fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w);
-
- for(i = 0, x = 400 * screen_bits / 8, y = 200; i < screen_w; i++)
- {
- fbp[i] = (PIXEL_T *)(fbmem + (y + i) * screen_w * screen_bits / 8 + x);
- }
- cinfo.err = jpeg_std_error(&jerr);
- jpeg_create_decompress(&cinfo);
-
- jpeg_stdio_src(&cinfo, infile);
-
- jpeg_read_header(&cinfo, TRUE);
-
- jpeg_start_decompress(&cinfo);
- picture_w = cinfo.output_width;
- picture_h = cinfo.output_height;
- picture_bits = cinfo.output_components;
-
-
-
-
-
-
-
- buffer = (unsigned char *)malloc(picture_w * picture_bits);
- memset(buffer, 0, picture_w * picture_bits);
- for(i = 0; i < picture_h; i++)
- {
- tmpbuf = buffer;
- jpeg_read_scanlines(&cinfo, &tmpbuf, 1);
- for(j = 0; j < picture_w; j++)
- {
- memcpy(&picture_color, tmpbuf, sizeof(picture_color));
- fbp[i][j].b = picture_color.r;
- fbp[i][j].g = picture_color.g;
- fbp[i][j].r = picture_color.b;
- tmpbuf += picture_bits;
- }
- }
-
- jpeg_finish_decompress(&cinfo);
-
- jpeg_destroy_decompress(&cinfo);
- free(buffer);
- return;
- }
-
- void display_png()
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return;
- }
-
- long chartolong(char *string, int length)
- {
- long number;
- if(length <= 4)
- {
- memset(&number, 0, sizeof(long));
- memcpy(&number, string, length);
- }
- return number;
- }
-
- void display_bmp()
- {
- BITMAPFILEHEADER FileHead;
- BITMAPINFOHEADER InfoHead;
- int rc ,i ,j, x, y;
- int ciWidth, ciHeight, ciBitCount;
- long int BytesPerLine = 0;
- COLOR picture_color;
- PIXEL_T **fbp;
- unsigned char *buffer, *tmpbuf;
-
-
- rc = fread(&FileHead, sizeof(BITMAPFILEHEADER), 1, infile);
- if(rc != 1)
- {
- printf("Error:read bmp header error!/n");
- fclose(infile);
- exit(1);
- }
-
-
- if(memcmp(FileHead.cfType, "BM", 2) != 0)
- {
- printf("This is not a bmp picture!/n");
- fclose(infile);
- exit(1);
- }
-
-
- rc = fread((char *)&InfoHead, sizeof(BITMAPINFOHEADER), 1, infile);
- if(rc != 1)
- {
- printf("Error:read bmp infoheader error!/n");
- fclose(infile);
- exit(1);
- }
- ciWidth = (int)chartolong(InfoHead.ciWidth, 4);
- ciHeight = (int)chartolong(InfoHead.ciHeight, 4);
- ciBitCount = (int)chartolong(InfoHead.ciBitCount, 4);
- fseek(infile, (int)chartolong(FileHead.cfoffBits, 4), SEEK_SET);
- BytesPerLine = (ciWidth * ciBitCount + 31) / 32 * 4;
-
-
-
-
-
- fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w);
- for(i = 0, x = 0 * screen_bits / 8, y = 0; i < screen_w; i++)
- {
- fbp[i] = (PIXEL_T *)(fbmem + (y + i) * screen_w * screen_bits / 8 + x);
- }
- buffer = (unsigned char *)malloc(BytesPerLine);
- memset(buffer, 0, BytesPerLine);
- for(i = ciHeight - 1; i >= 0; i--)
- {
- tmpbuf = buffer;
- rc = fread(tmpbuf, BytesPerLine, 1, infile);
-
- for(j = 0; j < ciWidth; j++)
- {
- memcpy(&picture_color, tmpbuf, ciBitCount / 8);
-
- fbp[i][j].r = picture_color.r;
- fbp[i][j].g = picture_color.g;
- fbp[i][j].b = picture_color.b;
- tmpbuf += ciBitCount / 8;
- }
- }
- return;
- }
- int main(int argc, char* argv[])
- {
- int picture_mode;
- if(argc != 2)
- {
- printf("Usage:[sudo ./digital_frame xxx.jpg]/n");
- exit(1);
- }
-
- fb_init();
-
- picture_mode = judge_picture_mode(argv);
- switch(picture_mode)
- {
- case JPEG:
- printf("JPEG/JPG/n");
- display_jpeg();
- break;
- case PNG:
- printf("PNG/n");
- display_png();
- break;
- case BMP:
- printf("bmp/n");
- display_bmp();
- break;
- case UNKNOWN:
- printf("UNKNOWN/n");
- break;
- default:
- break;
- }
- fclose(infile);
- return 0;
- }
(sunsea1026) |