首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PPM颜色问题

PPM颜色问题
EN

Stack Overflow用户
提问于 2013-12-29 00:19:27
回答 1查看 461关注 0票数 0

有没有人请知道这个代码的错误是什么颜色不匹配?我正在使用ppm加载器加载图像,但在游戏中加载图像时,颜色与图像不匹配。

当我通过白色图像时,它显示为黑色,当我通过黑色时,它显示为白色,当我通过255 ,0,0时,它显示为0 ,255,255,当我通过128,128,192时,它显示为128,128,64

Fullscreen Image

代码语言:javascript
复制
#include <fstream>
#include <glut.h>
#include "Texture.h"
#include <iostream>
#pragma warning (disable : 4996)

Texture::Texture ()
{
}

void Texture::Prepare (int texN)
{
    texName = texN;

    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

    glBindTexture (GL_TEXTURE_2D, texName);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, 
         image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, 
             image.pixels);
}

void Texture::ReadPPMImage (char* fn)
{
    int tmpint;
    char str[100];
    FILE* inFile = fopen (fn,"rb");

    if (inFile == NULL) 
        {
        printf ("Can't open input file %s. Exiting.\n",fn);
        exit (1);
    }

    fscanf (inFile,"P%d\n", &tmpint);

    if (tmpint != 6) 
        {
        printf ("Input file is not ppm. Exiting.\n");
        exit (1);
    }    

    // skip comments embedded in header
    fgets (str,100,inFile);  
    while (str[0]=='#')
        fgets(str,100,inFile);

    // read image dimensions 
    sscanf (str,"%d %d",&image.width, &image.height);
    fgets (str,100,inFile);  
    sscanf (str,"%d",&tmpint);

    if (tmpint != 255)
        printf("Warning: maxvalue is not 255 in ppm file\n");

    image.numChannels = 3;
    image.pixels = (unsigned char*) malloc (image.numChannels * image.width*image.height * sizeof (unsigned char));

    if (image.pixels == NULL) 
        {
        printf ("Can't allocate image of size %dx%d. Exiting\n", image.width, image.height);
        exit (1);
    }
    else
        printf("Reading image %s of size %dx%d\n", fn, image.width,image.height);


    fread (image.pixels, sizeof (unsigned char), image.numChannels * image.width * image.height, inFile);

    fclose (inFile);
 }
EN

回答 1

Stack Overflow用户

发布于 2013-12-29 01:59:50

看起来您没有正确地读取头文件,并且您正在读取实际开始处的像素数据偏移量。

首先,我会在读取尺寸之后删除fgets。然后开始使用地面真实测试图像检查您希望读取的像素值以及您得到的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20816642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档