2

我要做的是使用 .ptm 文件显示图像。

所以,我打开文件,读取 W 和 H 并将 RGBA 设置为每个像素。但是当我尝试显示时,我在glutSwapBuffers().

这是代码。这有点乱,因为我正在尝试许多不同的方法来解决它,但我开始认为每次我创建一个新问题时:D

图像.h

    class Image {
public:
    Image() {};
    Image(int w, int h);
    ~Image();
    void setPixel(int rgb, int x, int y) {
        pixels[x + y] = rgb;
    }
    int setRgb(int r, int g, int b);
    int setRgb(int r, int g, int b, int a);
    int getPixel(int x, int y) {
        return pixels[x + y*width];
    }
    int * getPixel() { return pixels; }
    int getWidth() { return width; }
    int getHeight() { return height; }
private:
    int *pixels; 
    int width, height;
};

图像.cpp

#include "Image.h"
#include <stdlib.h>


Image::Image(int w, int h)
{
    width = w; height = h;
    pixels = (int*)malloc(h*w);
}


Image::~Image()
{
}

int Image::setRgb(int r, int g, int b)
{
    int rgb;
    rgb = (r << 16) | (g << 8) | b;
    return rgb;
}

int Image::setRgb(int r, int g, int b, int a)
{
    int rgba;
    rgba = (a<< 24) | (r << 16) | (g << 8) | b;
    return rgba;
}

源码.cpp

#include <GL\freeglut.h>
#include <GL\GL.h>
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include "Image.h"

using namespace std;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    Image *img = new Image(800, 500);
    int w, h, max; //width height MaxValue
    char *w_ = new char; //in case of the file doesn't have a comment
    string fileModel, comment;
    ifstream myfile("dba.ptm"); //open the file

    if (myfile.is_open())
    {
        myfile >> fileModel;
        if (fileModel == "P7")
        {
            myfile >> w_;
            if (*w_ == '#')
                getline(myfile, comment);
            else
                w = atoi(w_);
            myfile >> h;

            int a, r, g, b, rgba;
            myfile >> max;
            for (int i = 0; i < w; i++)
            {
                for (int k = 0; k < h; k++)
                {
                    myfile >> a;
                    myfile >> r;
                    myfile >> g;
                    myfile >> b;
                    rgba = img->setRgb(r, g, b, a);
                    img->setPixel(rgba, i, k);
                }
            }
        }
    }
    myfile.close();


    glDrawPixels(img->getWidth(), img->getHeight(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, img->getPixel());
    glutSwapBuffers();
}

void init(void)
{

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 800.0, 0.0, 500.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(800, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("PTM");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;   
}
4

2 回答 2

3

对不起,但你错了很多...... :)

  • 您正在使用 malloc 而不是new[]
  • 你从来没有freedelete任何事情
  • 在大多数情况下(除了pixels)你不需要在堆上创建东西
  • pixels[x + y] = rgb;显然是错误的
  • 这 : char *w_ = new char; //in case of the file doesn't have a comment myfile >> w_; if (*w_ == '#') .. 绝对不会做你认为它做的事情,或者你想让它做的事情。

可能还有更多。:(

于 2016-04-27T22:14:04.163 回答
2

malloc(h*w)应该是malloc(h*w*sizeof(int)),否则您只能为图像的四分之一分配足够的内存。

您似乎还在每一帧都泄漏了整个图像,这将很快耗尽您的可用 RAM。你需要freedelete一切你mallocnew,分别。

您可能还想立即清除为图像分配的内存,以避免仅设置一些像素的问题(其余像素将是随机值,这可能不如完全透明等可取)。您可以使用memsetstd::fill之后执行此操作malloc

于 2016-04-27T18:45:52.310 回答