QImage::setPixel:坐标超出范围
QImage::setPixel: coordinate out of range
我是QT初学者
我试着打开二进制文件并逐像素绘制
当我调试
时,我得到了这个警告QImage::setPixel: coordinate (67,303) out of range
QImage::setPixel: coordinate (67,306) out of range
QImage::setPixel: coordinate (67,309) out of range
QImage::setPixel: coordinate (67,312) out of range
这是代码
unsigned char* data = new unsigned char[row_padded];
unsigned char tmp;
QImage myImage;
myImage = QImage(width, height, QImage::Format_RGB888);
for(int i = 0; i < height; i++)
{
fread(data, sizeof(unsigned char), row_padded, file);
for(int j = 0; j < width*3; j += 3)
{
// Convert (B, G, R) to (R, G, B)
tmp = data[j];
data[j] = data[j+2];
data[j+2] = tmp;
myImage.setPixel((width*3)-j, height-i, RGB((int)data[j],(int)data[j+1],(int)data[j+2]));
}
}
提前感谢:)
您错误地计算了这一行的x和y坐标:
myImage.setPixel((width*3)-j, height-i, RGB((int)data[j],(int)data[j+1],(int)data[j+2]));
x应该改为:
width - j / 3 - 1
y应该是
height - i - 1
或者最好为x使用另一个变量来避免除法:
for(int i = 0; i < height; i++)
{
fread(data, sizeof(unsigned char), row_padded, file);
int x = width;
for(int j = 0; j < width*3; j += 3)
{
// Convert (B, G, R) to (R, G, B)
tmp = data[j];
data[j] = data[j+2];
data[j+2] = tmp;
myImage.setPixel(--x, height-i-1, RGB((int)data[j],(int)data[j+1],(int)data[j+2]));
}
}
建议:最好在使用变量之前定义它:
unsigned char tmp = data[j];
data[j] = data[j+2];
data[j+2] = tmp;
或者更好的
std::swap( data[j], data[j+2] );
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 如何设置一个范围来提取我想要获得的信息
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- QImage::p ixel 和 QImage::setPixel 坐标超出范围误差
- GPS伪范围到纬度/纬度坐标
- Q::p ixel:坐标(75,3039)超出范围
- QImage::setPixel:坐标超出范围
- 在越来越多的坐标范围内运行prim的最快方法