如何使用 jpg 库从 jpg 中获取 DC 系数?

发布于 2024-11-28 23:57:54 字数 2271 浏览 1 评论 0原文

我对这个东西很陌生,但我需要使用 jpeg 库从 jpeg 中获取直流系数? 有人提示我相应的函数在jdhuff.c中,但我找不到它。我试图找到一篇关于 jpg 库的不错的文章,在那里我可以得到这个,但到目前为止没有成功。

所以我希望你们能帮我一点,并给我指出一些文档或提示。 所以,这就是我所知道的:

一张 jpg 图片由 8x8 块组成。那是 64 像素。其中 63 个被命名为 AC,1 个被命名为 DC。这就是系数。该位置位于 array[0][0]。

但是我如何用 jpg 库准确地读取它呢?我正在使用 C++。

编辑: 这是我到目前为止所拥有的:

read_jpeg::read_jpeg( const std::string& filename )
{
    FILE* fp = NULL;                // File-Pointer
    jpeg_decompress_struct cinfo;   // jpeg decompression parameters
    JSAMPARRAY buffer;              // Output row-buffer
    int row_stride = 0;             // physical row width
    my_error_mgr jerr;              // Custom Error Manager


    // Set Error Manager
    cinfo.err = jpeg_std_error(&jerr.pub);
    jerr.pub.error_exit = my_error_exit;

    // Handle longjump
    if (setjmp(jerr.setjmp_buffer)) {

        // JPEG has signaled an error. Clean up and throw an exception.
        jpeg_destroy_decompress(&cinfo);
        fclose(fp);
        throw std::runtime_error("Error: jpeg has reported an error.");
    }   

    // Open the file
    if ( (fp = fopen(filename.c_str(), "rb")) == NULL )
    {
        std::stringstream ss;
        ss << "Error: Cannot read '" <<  filename.c_str() << "' from the specified location!";
        throw std::runtime_error(ss.str());
    }

    // Initialize jpeg decompression
    jpeg_create_decompress(&cinfo);

    // Show jpeg where to read the data
    jpeg_stdio_src(&cinfo, fp);

    // Read the header
    jpeg_read_header(&cinfo, TRUE);

    // Decompress the file
    jpeg_start_decompress(&cinfo);

    // JSAMPLEs per row in output buffer
    row_stride = cinfo.output_width * cinfo.output_components;

    // Make a one-row-high sample array 
    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

    // Read image using jpgs counter
    while (cinfo.output_scanline < cinfo.output_height) 
    {

         // Read the image
         jpeg_read_scanlines(&cinfo, buffer, 1);
    }

    // Finish the decompress 
    jpeg_finish_decompress(&cinfo);

    // Release memory
    jpeg_destroy_decompress(&cinfo);

    // Close the file 
    fclose(fp);
}

I am new to this stuff, but I need to get the dc-coefficient from a jpeg using the jpeg library?
I was told as a hint that the corresponding function is in jdhuff.c, but I can't find it. I tried to find a decent article about the jpg library where I can get this, but no success so far.

So I hope you guys can help me a bit and point me to either some documentation or have a hint.
So, here is what I know:

A jpg picture consists of 8x8 Blocks. That are 64 Pixels. 63 of it are named AC and 1 is named DC. Thats the coefficient. The position is at array[0][0].

But how do I exactly read that with the jpg library? I am using C++.

edit:
This is what I have so far:

read_jpeg::read_jpeg( const std::string& filename )
{
    FILE* fp = NULL;                // File-Pointer
    jpeg_decompress_struct cinfo;   // jpeg decompression parameters
    JSAMPARRAY buffer;              // Output row-buffer
    int row_stride = 0;             // physical row width
    my_error_mgr jerr;              // Custom Error Manager


    // Set Error Manager
    cinfo.err = jpeg_std_error(&jerr.pub);
    jerr.pub.error_exit = my_error_exit;

    // Handle longjump
    if (setjmp(jerr.setjmp_buffer)) {

        // JPEG has signaled an error. Clean up and throw an exception.
        jpeg_destroy_decompress(&cinfo);
        fclose(fp);
        throw std::runtime_error("Error: jpeg has reported an error.");
    }   

    // Open the file
    if ( (fp = fopen(filename.c_str(), "rb")) == NULL )
    {
        std::stringstream ss;
        ss << "Error: Cannot read '" <<  filename.c_str() << "' from the specified location!";
        throw std::runtime_error(ss.str());
    }

    // Initialize jpeg decompression
    jpeg_create_decompress(&cinfo);

    // Show jpeg where to read the data
    jpeg_stdio_src(&cinfo, fp);

    // Read the header
    jpeg_read_header(&cinfo, TRUE);

    // Decompress the file
    jpeg_start_decompress(&cinfo);

    // JSAMPLEs per row in output buffer
    row_stride = cinfo.output_width * cinfo.output_components;

    // Make a one-row-high sample array 
    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

    // Read image using jpgs counter
    while (cinfo.output_scanline < cinfo.output_height) 
    {

         // Read the image
         jpeg_read_scanlines(&cinfo, buffer, 1);
    }

    // Finish the decompress 
    jpeg_finish_decompress(&cinfo);

    // Release memory
    jpeg_destroy_decompress(&cinfo);

    // Close the file 
    fclose(fp);
}

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

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

发布评论

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

评论(1

摘星┃星的人 2024-12-05 23:57:54

使用标准 API 是不可能实现这一点的。使用 libjpeg API,您可以获得的最接近的是 Y/Cb/Cr 通道的原始像素数据。

要获取系数数据,您需要破解 decode_mcu 函数(或其调用者)以保存在那里解码的数据。

This is not possible using the standard API. With libjpeg API the closest you can get is raw pixel data of Y/Cb/Cr channels.

To get coefficients' data you'd need to hack the decode_mcu function (or its callers) to save the data decoded there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文