Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1097125
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 746
  • 用 户 组: 普通用户
  • 注册时间: 2018-06-12 20:01
个人简介

寫写code、调調bug、填填坑,僅此而已。

文章分类

全部博文(80)

文章存档

2019年(30)

2018年(50)

分类: C/C++

2019-03-09 10:36:58


点击(此处)折叠或打开

  1. typedef struct {
  2.      unsigned char red,green,blue;
  3. } PPMPixel;

  4. typedef struct {
  5.      int width, height;
  6.      PPMPixel *data;
  7. } PPMImage;
  8. int CudaJpegCode::readPpmFile(const char *ppmfile)
  9. {
  10.     char buff[16];
  11.     FILE *fp;
  12.     int c, rgb_comp_color;
  13.          //open PPM file for reading
  14.     fp = fopen(ppmfile, "rb");
  15.     if (!fp)
  16.     {
  17.         fprintf(stderr, "Unable to open file '%s'\n", ppmfile);
  18.          exit(1);
  19.     }

  20.     //read image format
  21.     if (!fgets(buff, sizeof(buff), fp))
  22.     {
  23.            perror(ppmfile);
  24.               exit(1);
  25.     }

  26.     //check the image format
  27.     if (buff[0] != 'P' || buff[1] != '6') {
  28.          fprintf(stderr, "Invalid image format (must be 'P6')\n");
  29.          exit(1);
  30.     }
  31.     printf(">>>>>>format:%c%c\n",buff[0],buff[1]);
  32.     //alloc memory form image
  33.     m_ppmimg = (PPMImage *)malloc(sizeof(PPMImage));
  34.     if (!m_ppmimg) {
  35.          fprintf(stderr, "Unable to allocate memory\n");
  36.          exit(1);
  37.     }

  38.     //check for comments
  39.     c = getc(fp);
  40.     while (c == '#') {
  41.     while (getc(fp) != '\n') ;
  42.          c = getc(fp);
  43.     }

  44.     ungetc(c, fp);
  45.     //read image size information
  46.     if (fscanf(fp, "%d %d", &m_ppmimg->width, &m_ppmimg->height) != 2) {
  47.          fprintf(stderr, "Invalid image size (error loading '%s')\n", ppmfile);
  48.          exit(1);
  49.     }
  50.     printf(">>>>>width:%d,height=%d\n",m_ppmimg->width,m_ppmimg->height);
  51.     //read rgb component
  52.     if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
  53.          fprintf(stderr, "Invalid rgb component (error loading '%s')\n", ppmfile);
  54.          exit(1);
  55.     }
  56.     printf(">>>>>rgb_comp_color===%d\n",rgb_comp_color);
  57.     //check rgb component depth
  58.     if ( RGB_COMPONENT_COLOR) {
  59.          fprintf(stderr, "'%s' does not have 8-bits components\n", ppmfile);
  60.          exit(1);
  61.     }

  62.     while (fgetc(fp) != '\n') ;
  63.     //memory allocation for pixel data
  64.     m_ppmimg->data = (PPMPixel*)malloc(m_ppmimg->width * m_ppmimg->height * sizeof(PPMPixel));

  65.     if (!m_ppmimg) {
  66.          fprintf(stderr, "Unable to allocate memory\n");
  67.          exit(1);
  68.     }

  69.     //read pixel data from file
  70.     fread(m_ppmimg->data, sizeof(PPMPixel) ,m_ppmimg->width*m_ppmimg->height, fp);
  71.     fclose(fp);
  72. }

阅读(6434) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~