繁体   English   中英

从 PPM 文件加载的 QImage 显示不正确

[英]QImage loaded from PPM file isn't displayed properly

因此,我正在使用 QT 为我的班级进行图像处理作业,我被要求手动将我拥有的当前图像数据保存为 PPM 格式,并将其加载回 QDialog 程序。

我设法正确保存了图像(使用 gimp 验证了输出文件),但是从文件加载造成了如下灾难

这是原文:

圆圈1

和糟糕的加载:

圆2

这是我的文件加载代码:

//... opens the file and pulling out headers & etc...

            unsigned char* data = new unsigned char[width*height*3];

            //Manual loading each byte into char array
            for(long h = 0; h < height; h++){ //for all 600 rows
                getline(readPPM,temp); //readPPM is an ifstream, temp is a string
                std::stringstream oneLine(temp);

                for(long w = 0; w < width*3; w++){ //to every position in that line 800*3
                    int readVal;
                    oneLine >> readVal; //string stream autofill get the int value instead of just one number
                    data[width*h+w] = (unsigned char)readVal; //put it into unsign char
                }
            }


            //Method 1: create the QImage with constructor 
(it blacked out 2/3 of the bottom of the image, and I'm not exactly familiar with QImage data type)
            imageData = QImage(data,width,height,QImage::Format_BGR888);

            //Method 2: manually setting each pixel
            for(int h = 0; h < height; h++){
                for(int w = 0; w < width; w++){
                    int r,g,b;
                    r = (int)data[width*h+w*3];
                    g = (int)data[width*h+w*3+1];
                    b = (int)data[width*h+w*3+2];
                    QColor color = qRgb(r,g,b);
                    imageData.setPixelColor(w,h,color);
                }
            }

//...set image to display...

当我从文件加载时,我希望显示看起来像原始图像,但我不确定是什么问题导致损坏,请帮助

一行图像的大小为3 * width字节而不是width ,因此这应该在data[]索引中的任何地方修复。

即代码

data[width*h+w] = (unsigned char)readVal;

应该替换为

data[3*width*h+w] = (unsigned char)readVal;

和代码

r = (int)data[width*h+w*3];
g = (int)data[width*h+w*3+1];
b = (int)data[width*h+w*3+2];

替换为

r = (int)data[3*width*h+w*3];
g = (int)data[3*width*h+w*3+1];
b = (int)data[3*width*h+w*3+2];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM