elas算法源码赏析(一):PGM格式图片的读取和保存


// basic image I/O, based on Pedro Felzenszwalb's code

#ifndef IMAGE_H
#define IMAGE_H

#include <cstdlib>
#include <climits>
#include <cstring>
#include <fstream>

// use imRef to access image data.获取图片数据
#define imRef(im, x, y) (im->access[y][x])
  
// use imPtr to get pointer to image data.获取图片数据指针
#define imPtr(im, x, y) &(im->access[y][x])

#define BUF_SIZE 256

typedef unsigned char uchar;
typedef struct { uchar r, g, b; } rgb;

inline bool operator==(const rgb &a, const rgb &b) {
  return ((a.r == b.r) && (a.g == b.g) && (a.b == b.b));
}

// image class模板类 T可以用各种类型代替
template <class T> class image {
public:

  // create image
  image(const int width, const int height, const bool init = false);

  // delete image
  ~image();

  // init image
  void init(const T &val);

  // deep copy
  image<T> *copy() const;
  
  // get image width/height
  int width() const { return w; }
  int height() const { return h; }
  
  // image data
  T *data;
  
  // row pointers
  T **access;
  
private:
  int w, h;
};

template <class T> image<T>::image(const int width, const int height, const bool init) {
  w = width;
  h = height;
  data = new T[w * h];  // allocate space for image data为图片数据分配空间
  access = new T*[h];   // allocate space for row pointers为行指针分配空间
  
  // initialize row pointers
  for (int i = 0; i < h; i++)
    access[i] = data + (i * w);  //初始化行指针
  
  // init to zero
  if (init)
    memset(data, 0, w * h * sizeof(T));//初始化全0
}

template <class T> image<T>::~image() {
  delete [] data; 
  delete [] access;
}

template <class T> void image<T>::init(const T &val) {
  T *ptr = imPtr(this, 0, 0);//取(0,0)地址
  T *end = imPtr(this, w-1, h-1);
  while (ptr <= end)
    *ptr++ = val;
}


template <class T> image<T> *image<T>::copy() const {
  image<T> *im = new image<T>(w, h, false);//硬拷贝
  memcpy(im->data, data, w * h * sizeof(T));//memcpy 目的 源 大小
  return im;
}

class pnm_error {};

void pnm_read(std::ifstream &file, char *buf) {
  char doc[BUF_SIZE];
  char c;
  
  file >> c;
  while (c == '#') {
    file.getline(doc, BUF_SIZE);//至少读取BUF_SIZE个字符到doc里面
    file >> c;
  }
  file.putback(c);//把c重新放回输入流,可以被再次读取
  
  file.width(BUF_SIZE);//输入BUF_SIZE-1个字符
  file >> buf;
  file.ignore();//默认参数为cin.ignore(1,EOF),即把EOF前的1个字符清掉,没有遇到EOF就清掉一个字符然后结束
}

image<uchar> *loadPGM(const char *name) {
  char buf[BUF_SIZE];
  
  // read header
  std::ifstream file(name, std::ios::in | std::ios::binary);
  pnm_read(file, buf);
  if (strncmp(buf, "P5", 2)) {
  //str1与str2的前n个字符相同,则返回0;若s1大于s2,则返回大于0的值;若s1 小于s2,则返回小于0的值。
    std::cout << "ERROR: Could not read file " << name << std::endl;
    throw pnm_error();
  }

  pnm_read(file, buf);
  int width = atoi(buf);
  pnm_read(file, buf);
  int height = atoi(buf);

  pnm_read(file, buf);
  if (atoi(buf) > UCHAR_MAX) {//无符号char型所能表示的最大整数
    std::cout << "ERROR: Could not read file " << name << std::endl;
    throw pnm_error();
  }

  // read data
  image<uchar> *im = new image<uchar>(width, height);
  file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));

  return im;
}

void savePGM(image<uchar> *im, const char *name) {
  int width = im->width();
  int height = im->height();
  std::ofstream file(name, std::ios::out | std::ios::binary);

  file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n";
  file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
}

#endif

来说说头文件

基础好的同学自动忽视这部分内容。
cstdlib 相当于stdlib.h
可以提供一些函数与符号常量,具体如下:根据ISO标准,stdlib.h提供以下类型:
size_t, wchar_t, div_t, ldiv_t, lldiv_t
常量:
NULL, EXIT_FAILURE, EXIT_SUCCESS, RAND_MAX, MB_CUR_MAX
函数:
atof, atoi, atol, strtod, strtof, strtols, strtol, strtoll, strtoul, strtoull, rand, srand, calloc, free, malloc, realloc, abort, atexit, exit, getenv, system, bsearch, qsort, abs, div, labs, ldiv, llabs, tlldiv, mblen, mbtowc, wctomb, mbstowcs, wcstombs

climits定义了各种类型的最大值最小值

pgm格式

p5
width
height
UCHAR_MAX
所有图片数据

简约笔记

1.#ifdef #define #endif
2.atoi
3.template class classname{}
4.template(void)classname(type)::funcname
5.ofstream file(name, std::ios::out | std::ios::binary);

猜你喜欢

转载自blog.csdn.net/qq_30339595/article/details/83549537