我有一个程序可以加载jpeg图像并允许用户在图像上绘制并重新保存它。
我有加载和绘图工作,但当我尝试保存图像时,我得到了这个结果
结果应该是带有线的蜗牛的图像。
这是我的代码
bool IOManager::save_jpg_to_file(const char *file_name) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile;
JSAMPROW row_pointer[1];
int row;
JSAMPLE * image_buffer;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((outfile = fopen(file_name_.c_str(), "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", file_name_);
return false;
}
jpeg_stdio_dest(&cinfo, outfile);
int img_width = pixel_buffer_->width();
int img_height = pixel_buffer_->height();
cinfo.image_width = img_width;
cinfo.image_height = img_height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, TRUE);
jpeg_start_compress(&cinfo, TRUE);
unsigned char bytes[img_width * 3];
while (cinfo.next_scanline < cinfo.image_height) {
for (int i = 0; i < img_width; i+=3){
ColorData colorData = pixel_buffer_->get_pixel(i, cinfo
.next_scanline);
bytes[i] = static_cast<int>(colorData.red()*255) & 0xff;
bytes[i+1] = static_cast<int>(colorData.green()*255) & 0xff;
bytes[i+2] = static_cast<int>(colorData.blue()*255) & 0xff;
}
row_pointer[0] = bytes;
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
std::cout << "Done" << std::endl;
return true;
}
PixelBuffer只是一个RGB值的二维数组。
我无法弄清楚为什么会产生那些奇怪的线条,任何帮助?
答案 0 :(得分:0)
只需将我的评论作为单独的答案发布。您似乎以错误的方式访问您的像素缓冲区数据:您在源缓冲区和目标缓冲区中跳过3个像素,而(给定您的代码片段)您似乎需要处理每个像素你的来源。在迭代pixel_buffer_