图像翻转,使用 libjpeg OpenGL 输出到 JPEG

发布于 2025-01-08 13:59:20 字数 1248 浏览 1 评论 0原文

下面的代码帮助我使用 libjpg 将 OpenGL 输出转换为 JPEG 图像,但生成的图像垂直翻转...

代码工作完美,但最终图像翻转我不知道为什么?!

unsigned char *pdata = new unsigned char[width*height*3];
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pdata);

    FILE *outfile;
    if ((outfile = fopen("sample.jpeg", "wb")) == NULL) {
        printf("can't open %s");
        exit(1);
      }

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr       jerr;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);

    cinfo.image_width      = width;
    cinfo.image_height     = height;
    cinfo.input_components = 3;
    cinfo.in_color_space   = JCS_RGB;

    jpeg_set_defaults(&cinfo);
    /*set the quality [0..100]  */
    jpeg_set_quality (&cinfo, 100, true);
    jpeg_start_compress(&cinfo, true);

    JSAMPROW row_pointer;
    int row_stride = width * 3;

    while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer = (JSAMPROW) &pdata[cinfo.next_scanline*row_stride];
    jpeg_write_scanlines(&cinfo, &row_pointer, 1);
    }

    jpeg_finish_compress(&cinfo);

    fclose(outfile);

    jpeg_destroy_compress(&cinfo);

The below code helps me to convert OpenGL output to JPEG image using libjpg but the resultant image is flipped vertical...

The code works perfect but the final image is flipped I dont know why ?!

unsigned char *pdata = new unsigned char[width*height*3];
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pdata);

    FILE *outfile;
    if ((outfile = fopen("sample.jpeg", "wb")) == NULL) {
        printf("can't open %s");
        exit(1);
      }

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr       jerr;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);

    cinfo.image_width      = width;
    cinfo.image_height     = height;
    cinfo.input_components = 3;
    cinfo.in_color_space   = JCS_RGB;

    jpeg_set_defaults(&cinfo);
    /*set the quality [0..100]  */
    jpeg_set_quality (&cinfo, 100, true);
    jpeg_start_compress(&cinfo, true);

    JSAMPROW row_pointer;
    int row_stride = width * 3;

    while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer = (JSAMPROW) &pdata[cinfo.next_scanline*row_stride];
    jpeg_write_scanlines(&cinfo, &row_pointer, 1);
    }

    jpeg_finish_compress(&cinfo);

    fclose(outfile);

    jpeg_destroy_compress(&cinfo);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

游魂 2025-01-15 13:59:20

OpenGL的坐标系的原点在图像的左下角。 LIBJPEG 假定图像的原点位于图像的左上角。进行以下更改来修复您的代码:

while (cinfo.next_scanline < cinfo.image_height)
{
    row_pointer = (JSAMPROW) &pdata[(cinfo.image_height-1-cinfo.next_scanline)*row_stride];
    jpeg_write_scanlines(&cinfo, &row_pointer, 1);
}

OpenGL's coordinate system has the origin in the lower left corner of the image. LIBJPEG assumes that the origin of the image is in the upper left corner of the image. Make the following change to fix your code:

while (cinfo.next_scanline < cinfo.image_height)
{
    row_pointer = (JSAMPROW) &pdata[(cinfo.image_height-1-cinfo.next_scanline)*row_stride];
    jpeg_write_scanlines(&cinfo, &row_pointer, 1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文