首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自libjpeg-turbo-1.5.1-vc.exe的TurboJPEG API产生错误的输出。

来自libjpeg-turbo-1.5.1-vc.exe的TurboJPEG API产生错误的输出。
EN

Stack Overflow用户
提问于 2016-10-10 02:15:21
回答 1查看 409关注 0票数 0

我在Visual 2015中用C编写了一个测试程序,用于测试libjpeg-turbo TurboJPEG-API。

在这个测试程序中,我生成了一个800×800 RGB像素的映像,并将其写入磁盘。当我在TJSAMP_GRAY函数中使用tjCompress2时,磁盘上产生的映像看起来很好:

当我使用TJSAMP_444代替时,图像看起来是头晕的:

然而,这两种图像不能在中打开,而只能在MS、Chrome或Internet中打开。除TJSAMP_GRAY之外的所有其他选项都会产生一个白发图像,质量设置为100与TJSAMP_GRAY也会导致白头发输出。(这篇文章中的图片通过屏幕截图复制,并保存为png以获取更小的数据。)我还通过P-Invoke使用turbojpeg.dll库编写了一个C#程序。这个程序正在产生有效的输出。

问题:我的错在哪里?

所涉及的源代码:

代码语言:javascript
复制
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "turbojpeg.h"

unsigned char buffer[800 * 800 * 3];

int main(int argc, char** argv)
{
    unsigned long size = 0;

    unsigned char* compressedImage = NULL;

    for (int x = 0; x < 800; x++)
        for (int y = 0; y < 800; y++)
        {
            switch ((x / 80) % 3)
            {
            case 0:
                buffer[(y * 800 + x) * 3] = 255;
                buffer[(y * 800 + x) * 3 + 1] = 0;
                buffer[(y * 800 + x) * 3 + 2] = 0;
                break;
            case 1:
                buffer[(y * 800 + x) * 3] = 0;
                buffer[(y * 800 + x) * 3 + 1] = 255;
                buffer[(y * 800 + x) * 3 + 2] = 0;
                break;
            case 2:
                buffer[(y * 800 + x) * 3] = 0;
                buffer[(y * 800 + x) * 3 + 1] = 0;
                buffer[(y * 800 + x) * 3 + 2] = 255;
                break;
            }
        }

    tjhandle compressor = tjInitCompress();

    compressedImage = tjAlloc(1024 * 1024 * 4);

    size = 1024 * 1024 * 4;

    tjCompress2(compressor, buffer, 800, 0, 800, TJPF_RGB, &compressedImage, &size, TJSAMP_444, 80, TJFLAG_NOREALLOC | TJFLAG_ACCURATEDCT);

    tjDestroy(compressor);

    int handle = _wopen(L"D:\\file.jpeg", _O_CREAT | _O_WRONLY | _O_TRUNC, _S_IREAD | _S_IWRITE);

    printf("LENGTH=%ld\n", size);

    if (_write(handle, compressedImage, size) != size)
        printf("Write Error.\n");

    _close(handle);

    tjFree(compressedImage);

    return 0;
}

为了完成测试,测试C#源代码,它工作得很好:

代码语言:javascript
复制
class Program
{
    [DllImport("turbojpeg", CallingConvention = CallingConvention.Cdecl)]
    private static extern IntPtr tjInitCompress();

    [DllImport("turbojpeg", CallingConvention = CallingConvention.Cdecl)]
    private static extern IntPtr tjAlloc(int bytes);

    [DllImport("turbojpeg", CallingConvention = CallingConvention.Cdecl)]
    private static extern int tjCompress2(IntPtr handle, IntPtr srcBuf, int width, int pitch, int height, int pixelFormat, ref IntPtr jpegBuf, ref ulong jpegSize, int jpegSubsamp, int jpegQual, int flags);

    [DllImport("turbojpeg", CallingConvention = CallingConvention.Cdecl)]
    private static extern int tjDestroy(IntPtr handle);

    [DllImport("turbojpeg", CallingConvention = CallingConvention.Cdecl)]
    private static extern void tjFree(IntPtr buffer);

    static void Main(string[] args)
    {
        ulong size = 1024 * 1024 * 4;

        byte[] buffer = new byte[800 * 800 * 3];

        for (int x = 0; x < 800; x++)
            for (int y = 0; y < 800; y++)
            {
                switch ((x / 80) % 3)
                {
                    case 0:
                        buffer[(y * 800 + x) * 3] = 255;
                        buffer[(y * 800 + x) * 3 + 1] = 0;
                        buffer[(y * 800 + x) * 3 + 2] = 0;
                        break;
                    case 1:
                        buffer[(y * 800 + x) * 3] = 0;
                        buffer[(y * 800 + x) * 3 + 1] = 255;
                        buffer[(y * 800 + x) * 3 + 2] = 0;
                        break;
                    case 2:
                        buffer[(y * 800 + x) * 3] = 0;
                        buffer[(y * 800 + x) * 3 + 1] = 0;
                        buffer[(y * 800 + x) * 3 + 2] = 255;
                        break;
                }
            }

        IntPtr umBuffer = Marshal.AllocHGlobal(800 * 800 * 3);
        Marshal.Copy(buffer, 0, umBuffer, 800 * 800 * 3);

        IntPtr compressor = tjInitCompress();

        IntPtr compressedImage = tjAlloc(1024 * 1024 * 4);

        tjCompress2(compressor, umBuffer, 800, 0, 800, 0, ref compressedImage, ref size, 0, 80, 5 * 1024);

        byte[] mCPData = new byte[size];

        Marshal.Copy(compressedImage, mCPData, 0, (int)size);

        System.IO.File.WriteAllBytes("D:\\managed.jpeg", mCPData);

        tjFree(compressedImage);

        Marshal.FreeHGlobal(umBuffer);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-11 23:38:07

_wopen-Line正在丢失_O_BINARY。因此,在项目配置或libjpeg-turbo TurboJPEG-API中没有错误。只是,写入磁盘的输出被解释为编码文本,因此被_write-function或某些底层实例修改。

正确的源代码是:

代码语言:javascript
复制
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "turbojpeg.h"

unsigned char buffer[800 * 800 * 3];

int main(int argc, char** argv)
{
    unsigned long size = 0;

    unsigned char* compressedImage = NULL;

    for (int x = 0; x < 800; x++)
        for (int y = 0; y < 800; y++)
        {
            switch ((x / 80) % 3)
            {
            case 0:
                buffer[(y * 800 + x) * 3] = 255;
                buffer[(y * 800 + x) * 3 + 1] = 0;
                buffer[(y * 800 + x) * 3 + 2] = 0;
                break;
            case 1:
                buffer[(y * 800 + x) * 3] = 0;
                buffer[(y * 800 + x) * 3 + 1] = 255;
                buffer[(y * 800 + x) * 3 + 2] = 0;
                break;
            case 2:
                buffer[(y * 800 + x) * 3] = 0;
                buffer[(y * 800 + x) * 3 + 1] = 0;
                buffer[(y * 800 + x) * 3 + 2] = 255;
                break;
            }
        }

    tjhandle compressor = tjInitCompress();

    compressedImage = tjAlloc(1024 * 1024 * 4);

    size = 1024 * 1024 * 4;

    tjCompress2(compressor, buffer, 800, 0, 800, TJPF_RGB, &compressedImage, &size, TJSAMP_444, 80, TJFLAG_NOREALLOC | TJFLAG_ACCURATEDCT);

    tjDestroy(compressor);

    int handle = _wopen(L"D:\\file.jpeg", _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IREAD | _S_IWRITE);

    printf("LENGTH=%ld\n", size);

    if (_write(handle, compressedImage, size) != size)
        printf("Write Error.\n");

    _close(handle);

    tjFree(compressedImage);

    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39950457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档