Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用Atom文本编辑器创建图形对象_C++ - Fatal编程技术网

C++ 如何使用Atom文本编辑器创建图形对象

C++ 如何使用Atom文本编辑器创建图形对象,c++,C++,我很难确定在编译器中包含graphics.h文件的方法。我遇到的所有信息都是关于IDE的,比如代码块。我想能够包括图形文件使用而不面临任何问题。我的问题是: 您可以使用像Atom这样的文本编辑器来创建图形对象吗? 如果是,应采取哪些步骤来实现这一目标? 有很多图形格式可供选择,具有不同的功能 我要做的第一个区别是: 光栅图形与矢量图形 逐像素存储图像的光栅图形通常采用二进制编码,因为数据量通常与图像大小成正比。然而,其中一些是文本编码的,或者可以是文本编码的,也可以是二进制编码的 例如: 虽然这

我很难确定在编译器中包含graphics.h文件的方法。我遇到的所有信息都是关于IDE的,比如代码块。我想能够包括图形文件使用而不面临任何问题。我的问题是:

您可以使用像Atom这样的文本编辑器来创建图形对象吗? 如果是,应采取哪些步骤来实现这一目标?
有很多图形格式可供选择,具有不同的功能

我要做的第一个区别是:

光栅图形与矢量图形

逐像素存储图像的光栅图形通常采用二进制编码,因为数据量通常与图像大小成正比。然而,其中一些是文本编码的,或者可以是文本编码的,也可以是二进制编码的

例如:

虽然这些文件格式有点异国情调,但不难找到支持它们的软件。例如,既支持开箱即用,也支持Windows。顺便说一句,它们很简单,所以自己编写加载器和编写器并不太复杂

一个简单的PPM阅读器和编写器,便携式anymap的彩色版本可以在我的答案中找到

矢量图形存储构建图像的图形原语,这些原语通常是文本编码的。由于矢量图形可以通过简单地对所有坐标应用比例因子,以较小的损耗缩放到任何图像大小,因此文件大小和目标图像大小没有直接关系。因此,矢量图形是图形的首选格式,尤其是在多目标分辨率下需要矢量图形时

为此,我专门建议:

这有望成为即将推出的Web内容中可伸缩图形的标准。Qt确实为SVG提供了有限的支持,因此,对于分辨率无关的图标,它是我的首选选项

另一个不同但可能相关的选择是在源代码中嵌入图形。如果您的图像加载器库提供从内存和文件加载图像,则可以使用任何格式来完成此操作。我所知道的就是这个。 因此,问题可以归结为:如何在C/C++源代码中嵌入大量ASCII或二进制数据作为常量?这是一个很难解决的问题

我在回答这个问题时是这样做的

更新:

当我注意到PPM的链接示例和PBM的另一个链接示例实际上读取的是二进制格式时,我实现了一个示例应用程序,它演示了ASCII PPM的用法

我相信XPM更适合在文本编辑器中编辑的特定需求。因此,我在样本中也考虑了这一点

因为这个问题没有提到需要什么特定的内部图像格式,也没有提到它应该在什么API中可用,所以我选择了Qt

是我熟悉的东西 提供用作图像导入目标的QImage 只需要几行代码就可以直观地输出结果。 源代码test-QShowPPM-XPM.cc:

这已在VS2013中编译并在Windows 10 64位中测试:


你在用什么编译器?我被下载了MinGW@Sam问:你能不能使用Atom这样的文本编辑器来创建图形对象?你指的是使用一个处理图形对象的类,还是要创建图形(如perlin noise),还是要创建图形对象(如SVG)手动?后两个选项。我明白你的意思。我曾试图使用图形标准库在图形用户界面中创建图像,但我的问题远不止于此。因此,我将修改我的问题:如何在Atom中创建图形对象,以及如何使用类来实现此目的?谢谢您的帮助。我要检查一下out@Sam我添加了一个示例应用程序。请注意,这些都不是减少必要的示例代码行数量的功能强大的阅读器。
// standard C++ header:
#include <cassert>
#include <iostream>
#include <string>
#include <sstream>

// Qt header:
#include <QtWidgets>

// sample image in ASCII PPM format
// (taken from https://en.wikipedia.org/wiki/Netpbm_format)
const char ppmData[] =
"P3\n"
"3 2\n"
"255\n"
"255   0   0     0 255   0     0   0 255\n"
"255 255   0   255 255 255     0   0   0\n";

// sample image in XPM3 format
/* XPM */
const char *xpmData[] = {
  // w, h, nC, cPP
  "16 16 5 1",
  // colors
  "  c #ffffff",
  "# c #000000",
  "g c #ffff00",
  "r c #ff0000",
  "b c #0000ff",
  // pixels
  "       ##       ",
  "    ###gg###    ",
  "   #gggggggg#   ",
  "  #gggggggggg#  ",
  " #ggbbggggbbgg# ",
  " #ggbbggggbbgg# ",
  " #gggggggggggg# ",
  "#gggggggggggggg#",
  "#ggrrggggggrrgg#",
  " #ggrrrrrrrrgg# ",
  " #ggggrrrrgggg# ",
  " #gggggggggggg# ",
  "  #gggggggggg#  ",
  "   #gggggggg#   ",
  "    ###gg###    ",
  "       ##       "
};

// Simplified PPM ASCII Reader (no support of comments)

inline int clamp(int value, int min, int max)
{
  return value < min ? min : value > max ? max : value;
}

inline int scale(int value, int maxOld, int maxNew)
{
  return value * maxNew / maxOld;
}

QImage readPPM(std::istream &in)
{
  std::string header;
  std::getline(in, header);
  if (header != "P3") throw "ERROR! Not a PPM ASCII file.";
  int w = 0, h = 0, max = 255; // width, height, bits per component
  if (!(in >> w >> h >> max)) throw "ERROR! Premature end of file.";
  if (max <= 0 || max > 255) throw "ERROR! Invalid format.";
  QImage qImg(w, h, QImage::Format_RGB32);
  for (int y = 0; y < h; ++y) {
    for (int x = 0; x < w; ++x) {
      int r, g, b;
      if (!(in >> r >> g >> b)) throw "ERROR! Premature end of file.";
      qImg.setPixel(x, y,
          scale(clamp(r, 0, 255), max, 255) << 16
        | scale(clamp(g, 0, 255), max, 255) << 8
        | scale(clamp(b, 0, 255), max, 255));
    }
  }
  return qImg;
}

// Simplified XPM Reader (implements sub-set of XPM3)

char getChar(const char *&p)
{
  if (!*p) throw "ERROR! Premature end of file.";
  return *p++;
}

std::string getString(const char *&p)
{
  std::string str;
  while (*p && !isspace(*p)) str += *p++;
  return str;
}

void skipWS(const char *&p)
{
  while (*p && isspace(*p)) ++p;
}

QImage readXPM(const char **xpmData)
{
  int w = 0, h = 0; // width, height
  int nC = 0, cPP = 1; // number of colors, chars per pixel
  { std::istringstream in(*xpmData);
    if (!(in >> w >> h >> nC >> cPP)) throw "ERROR! Premature end of file.";
    ++xpmData;
  }
  std::map<std::string, std::string> colTbl;
  for (int i = nC; i--; ++xpmData) {
    const char *p = *xpmData;
    std::string chr;
    for (int j = cPP; j--;) chr += getChar(p);
    skipWS(p);
    if (getChar(p) != 'c') throw "ERROR! Format not supported.";
    skipWS(p);
    colTbl[chr] = getString(p);
  }
  QImage qImg(w, h, QImage::Format_RGB32);
  for (int y = 0; y < h; ++y, ++xpmData) {
    const char *p = *xpmData;
    for (int x = 0; x < w; ++x) {
      std::string pixel;
      for (int j = cPP; j--;) pixel += getChar(p);
      qImg.setPixelColor(x, y, QColor(colTbl[pixel].c_str()));
    }
  }
  return qImg;
}

// a customized QLabel to handle scaling
class LabelImage: public QLabel {

  private:
    QPixmap _qPixmap, _qPixmapScaled;

  public:
    LabelImage();
    LabelImage(const QPixmap &qPixmap): LabelImage()
    {
      setPixmap(qPixmap);
    }
    LabelImage(const QImage &qImg): LabelImage(QPixmap::fromImage(qImg))
    { }

    void setPixmap(const QPixmap &qPixmap) { setPixmap(qPixmap, size()); }

  protected:
    virtual void resizeEvent(QResizeEvent *pQEvent);

  private:
    void setPixmap(const QPixmap &qPixmap, const QSize &size);
};

// main function
int main(int argc, char **argv)
{
  qDebug() << QT_VERSION_STR;
  // main application
#undef qApp // undef macro qApp out of the way
  QApplication qApp(argc, argv);
  // setup GUI
  QMainWindow qWin;
  QGroupBox qBox;
  QGridLayout qGrid;
  LabelImage qLblImgPPM(readPPM(std::istringstream(ppmData)));
  qGrid.addWidget(&qLblImgPPM, 0, 0, Qt::AlignCenter);
  LabelImage qLblImgXPM(readXPM(xpmData));
  qGrid.addWidget(&qLblImgXPM, 1, 0, Qt::AlignCenter);
  qBox.setLayout(&qGrid);
  qWin.setCentralWidget(&qBox);
  qWin.show();
  // run application
  return qApp.exec();
}

// implementation of LabelImage

LabelImage::LabelImage(): QLabel()
{
  setFrameStyle(Raised | Box);
  setAlignment(Qt::AlignCenter);
  //setMinimumSize(QSize(1, 1)); // seems to be not necessary
  setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
}

void LabelImage::resizeEvent(QResizeEvent *pQEvent)
{
  QLabel::resizeEvent(pQEvent);
  setPixmap(_qPixmap, pQEvent->size());
}

void LabelImage::setPixmap(const QPixmap &qPixmap, const QSize &size)
{
  _qPixmap = qPixmap;
  _qPixmapScaled = _qPixmap.scaled(size, Qt::KeepAspectRatio);
  QLabel::setPixmap(_qPixmapScaled);
}