来自 libjpeg-turbo-1.5.1-vc.exe 的 TurboJPEG API 产生错误的输出
TurboJPEG API from libjpeg-turbo-1.5.1-vc.exe produces wrong output
我在 Visual Studio 2015 年用 C 写了一个测试程序用于测试 libjpeg-turbo TurboJPEG-API.
在这个测试程序中,我生成了一个 800x800 RGB 像素的图像并将其写入磁盘。当我在 tjCompress2
函数中使用 TJSAMP_GRAY
时,磁盘上生成的图像看起来不错:
当我使用 TJSAMP_444
时,图像看起来很奇怪:
但是,这两张图片都无法在 Adobe Photoshop 中打开,只能在 MS-Paint、Chrome 或 Internet Explorer 中打开。 TJSAMP_GRAY
以外的所有其他选项都会产生奇怪的图像,质量设置为 100 和 TJSAMP_GRAY
也会导致奇怪的输出。 (此 post 中的图像通过屏幕截图复制并保存为 png 以获取较小的数据。) 我还通过 P- 使用 turbojpeg.dll
库编写了一个 C#-程序调用。该程序正在生成有效输出。
提问:我错在哪里?
有问题的源代码:
#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# 源代码,工作正常:
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);
}
}
缺少 _wopen
-行 _O_BINARY
。因此,项目配置或 libjpeg-turbo TurboJPEG-API 没有错误。只是,写入光盘的输出被解释为编码文本,因此被 _write
函数或某些底层实例修改。
正确的源代码应该是:
#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;
}
我在 Visual Studio 2015 年用 C 写了一个测试程序用于测试 libjpeg-turbo TurboJPEG-API.
在这个测试程序中,我生成了一个 800x800 RGB 像素的图像并将其写入磁盘。当我在 tjCompress2
函数中使用 TJSAMP_GRAY
时,磁盘上生成的图像看起来不错:
当我使用 TJSAMP_444
时,图像看起来很奇怪:
但是,这两张图片都无法在 Adobe Photoshop 中打开,只能在 MS-Paint、Chrome 或 Internet Explorer 中打开。 TJSAMP_GRAY
以外的所有其他选项都会产生奇怪的图像,质量设置为 100 和 TJSAMP_GRAY
也会导致奇怪的输出。 (此 post 中的图像通过屏幕截图复制并保存为 png 以获取较小的数据。) 我还通过 P- 使用 turbojpeg.dll
库编写了一个 C#-程序调用。该程序正在生成有效输出。
提问:我错在哪里?
有问题的源代码:
#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# 源代码,工作正常:
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);
}
}
缺少 _wopen
-行 _O_BINARY
。因此,项目配置或 libjpeg-turbo TurboJPEG-API 没有错误。只是,写入光盘的输出被解释为编码文本,因此被 _write
函数或某些底层实例修改。
正确的源代码应该是:
#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;
}