JPEG图像的显示
本文最后更新于 161 天前,其中的信息可能已经有所发展或是发生改变。
#include <stdio.h>
#include <setjmp.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include </home/gec/software/installed/jepg/include/jpeglib.h>
struct my_error_mgr {
  struct jpeg_error_mgr pub;	/* "public" fields */

  jmp_buf setjmp_buffer;	/* for return to caller */
};

typedef struct my_error_mgr * my_error_ptr;

METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  my_error_ptr myerr = (my_error_ptr) cinfo->err;

  /* Always display the message. */
  /* We could postpone this until after returning, if we chose. */
  (*cinfo->err->output_message) (cinfo);

  /* Return control to the setjmp point */
  longjmp(myerr->setjmp_buffer, 1);
}

GLOBAL(int)
read_JPEG_file (char * filename,int* lcd_buf,const char* x_start,const char* y_start)
{

  struct jpeg_decompress_struct cinfo;

  struct my_error_mgr jerr;
  /* More stuff */
  FILE * infile;		/* source file */
  JSAMPARRAY buffer;		/* Output row buffer */
  int row_stride;		/* physical row width in output buffer */


  if ((infile = fopen(filename, "rb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    return 0;
  }

  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = my_error_exit;

  if (setjmp(jerr.setjmp_buffer)) {

    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
    return 0;
  }

  jpeg_create_decompress(&cinfo);



  jpeg_stdio_src(&cinfo, infile);



  (void) jpeg_read_header(&cinfo, TRUE);


  (void) jpeg_start_decompress(&cinfo);

  row_stride = cinfo.output_width * cinfo.output_components;
  buffer = (*cinfo.mem->alloc_sarray)
		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

  // 在 while 循环外定义一个 y,用于记录当前正在处理屏幕的第几行
  int y = 0; 
  int xnum = atoi(x_start);
  int ynum = atoi(y_start);
  while (cinfo.output_scanline < cinfo.output_height) {
    // 每次读取 1 行数据,存入 buffer 中
    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
    
    // buffer[0] 指向的就是刚刚解码出来的这一行 RGB 数据
    unsigned char *row_data = buffer[0];

    // 遍历这一行的每一个像素
    for (int x = 0; x < cinfo.output_width; x++) {
        
        // 【关键保护伞】:防止图片尺寸大于屏幕导致段错误越界
        if (x < 800 && y < 480 && ynum < 480 - cinfo.output_height && xnum < 800 - cinfo.output_width) {
            
            // 依次取出 R, G, B 分量 (注意这里的顺序和 BMP 不同)
            unsigned char r = row_data[x * 3 + 0];
            unsigned char g = row_data[x * 3 + 1];
            unsigned char b = row_data[x * 3 + 2];

            // 拼装成 LCD 需要的 32位 ARGB 格式 (0x00RRGGBB)
            int color = (r << 16) | (g << 8) | b;

            // 写入内存映射的显存中
            lcd_buf[(800*ynum+xnum)+(800 * y + x)] = color;
        }else{
          printf("图片刷新位置超过屏幕!\n");
          printf("图片大小应小于800*480\n");
          printf("图片刷新位置:x轴0~%d,y轴0~%d\n",799 - cinfo.output_width,479 - cinfo.output_height);
          return 0;
        }
    }
    // 一行处理完毕,行号 +1
    y++; 
  }

  (void) jpeg_finish_decompress(&cinfo);

  jpeg_destroy_decompress(&cinfo);

  fclose(infile);

  return 1;
}



int main(int argc , char* argv[])
{ 
    //参数检查
    if(argc!=4)
    { 
        printf("用法: %s <图片路径> <x轴坐标> <y轴坐标>\n", argv[0]);
        return 0;
    }

    //打开lcd屏幕
    int lcd = open("/dev/ubuntu_lcd",O_RDWR);
    if(lcd == -1)
    {
      printf("lcd屏幕打开失败!\n");
      return 0;
    }

    //内存映射
    int* lcd_buf = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);
    if (lcd_buf == MAP_FAILED) {
        perror("mmap");
        close(lcd);
        return 1;
    }

    //清除屏幕为白色
    memset(lcd_buf,0xFFFFFFFF,800*480*4);

    //读取jpeg信息并刷入内存
    read_JPEG_file (argv[1],lcd_buf,argv[2],argv[3]);

    //关闭设备,释放内存
    close(lcd);
    munmap(lcd_buf,800*480*4);

    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇