2017-07-15 108 views
-3

我很难确定在我的编译器中包含graphics.h文件的方式。我遇到的所有信息都是针对CodeBlocks等IDE。我希望能够使用图形文件,而不会面临任何问题。我的问题是:如何使用Atom文本编辑器创建图形对象

  1. 你可以使用像Atom这样的文本编辑器来创建图形对象吗?
  2. 如果是的话,应该采取什么措施来实现这一目标?
+1

什么编译您使用的? –

+0

我下载了MinGW – Sam

+0

@Sam当你问“你能使用像Atom这样的文本编辑器来创建一个图形对象吗?”时,你能否使它更精确一点:你是指使用一个处理图形对象的类或者你想创建图形(例如perlin噪声)还是你想手动创建一个图形对象(例如SVG)?后两种选择是 –

回答

0

有很多可用的图形格式具有不同的功能。

首先区别我想使是:

光栅图形矢量图形

光栅图形(存储由像素的图像的像素)是更经常的二进制编码的数据的量是通常与图像的大小成正比。然而,其中一些是文本编码的,或者可以是文本的以及二进制编码的。

的例子是:

虽然这些文件格式都有点异国情调,就不难发现,支持他们的软件。例如。 GIMP支持开箱即用(甚至在Windows上)。顺便说一句。他们很简单,自己编写loader和writer是不复杂的。

一个简单的PPM阅读器和书写器(Portable anymap的颜色版本)可以在我对SO: Convolution for Edge Detection in C的回答中找到。

矢量图形(构建图像的商店图形基元)通常是文本编码的。由于只需将缩放因子应用于所有坐标,矢量图就可以“无损”地缩放到任何图像大小,文件大小和目标图像大小并不直接相关。因此,矢量图形是绘图的最佳格式,尤其是在需要多个目标分辨率的情况下。

对于这一点,我会专门推荐:

这是(希望)在网上内容可升级显卡即将到来的标准。 Qt确实为SVG提供了(有限的)支持,因此,这是我的首选解决方案独立图标。


不同的(但可能相关的)选项是将图形嵌入到源代码中。如果您的图像加载器库提供从内存(以及从文件)加载的图像,则可以使用任意格式完成此操作。 (我所知道的是这样做的。) 因此,问题可以简化为:如何在C/C++源代码中将大量(ASCII或二进制)数据嵌入为常量?这是恕我直言的微不足道的解决。我的答案是SO: Paint a rect on qglwidget at specifit times


更新:

正如我注意到,对于PPM链接的样本(以及另一个用于PBM)读取实际的二进制格式,我实现这表明ASCII PPM的使用示例应用程序。

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

由于问题没有提到需要什么特定的内部图像格式,也没有什么API应当是可用的,我选用的Qt其中

  • 是我很熟悉
  • 提供的QImage它被用作图像导入的目的地
  • 只需要几行代码就可以输出结果。

的源代码test-QShowPPM-XPM.cc

// 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); 
} 

这在VS2013编译和测试在Windows 10(64位):

Snapshot of testQShowPPM-XPM

+0

感谢您的帮助。我要检查出来 – Sam

+0

@Sam我添加了一个示例应用程序。请注意,这些不是全功能的阅读器,以减少必要数量的示例代码行。 – Scheff