将 Jpeg 图像转换为 Bmp - 有些图像呈蓝色 [英] Converting Jpeg images to Bmp - some images come out blue

查看:39
本文介绍了将 Jpeg 图像转换为 Bmp - 有些图像呈蓝色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些Delphi 似乎不喜欢的Jpg 图像.它似乎特定于我正在加载的文件.过程很简单 - a) 将 Jpg 图像加载到 TJpegImage,b) 将 Jpg 对象分配给 TBitmap 对象,以及 c) 保存和/或显示 Bmp 图像.出于某种原因,这些图片总是带有蓝色调.

There are some Jpg images which Delphi doesn't seem to like. It appears to be specific with the files I'm loading. And the procedure is simple - a) load Jpg image to TJpegImage, b) Assign Jpg object to a TBitmap object, and c) Save and/or display Bmp image. For some reason, these pictures keep coming out with a blueish tint.

这些图像在我加载它们的任何地方和任何地方都能完美显示(Windows 图片查看器、油漆、Photoshop 等).

These images show perfectly anywhere and everywhere else I load them (windows picture viewer, paint, photoshop, etc.).

我正在做的事情很简单...

And what I'm doing is very simple...

procedure Load;
var
  J: TJpegImage;
  B: TBitmap;
begin
  J:= TJpegImage.Create;
  B:= TBitmap.Create;
  J.LoadFromFile('C:SomeFile.jpg');
  B.Assign(J);
  //Either save or display `B` and it appears blueish at this point
....

我想尽可能避免获得任何第三方的东西.这个问题在Delphi 7、2010、XE2版本中已经存在.至少 XE2 中的 TImage 控件可以正确显示它(与旧的两个控件相反),但是如果 TBitmap 仍然不起作用,那也没关系.这个文件有什么问题?和/或,Delphi 的渲染有什么问题?

I want to avoid getting any third party stuff as much as possible. This problem has existed in Delphi versions 7, 2010, and XE2. At least the TImage control in XE2 displays it properly (as opposed to the older two) but that doesn't matter if the TBitmap still doesn't work. What is wrong with this file? And/or, what is wrong with Delphi's rendering?

添加信息

我最近发现了一些关于这些图片的信息.当它们来自供应商(产品图片)时,它们是 CMYK 格式.当时,Delphi 7 没有正确支持这些文件(存在访问冲突和不良图像),因此所有图片都通过转换器过滤为 RGB 颜色格式.许多原始图像也是 TIFF 格式并转换为 JPG.这样看来,FastStone Image Resizer 软件一定不能正确保存这些文件.蓝色图像不会出现在所有这些图像上,一次只是一些随机批次.该软件处理数千种产品,因此有数千种可能的图片.

I recently found out something about these images. When they came from the vendors (product pictures), they were in CMYK format. At that time, Delphi 7 didn't properly support these files (with access violations and bad images) so all the pictures were filtered through a converter to RGB color format. Many original images were also TIFF and were converted to JPG. So it appears that the software FastStone Image Resizer must not properly save these files when they go through. The blue image doesn't happen on all of them, just some random batches at a time. The software handles thousands of products, so there are thousands of possible pictures.

推荐答案

我发现了这个问题.这很可能是 Delphi 中的一个错误.

I figured out the issue. It's most likely a bug in Delphi.

所提供的图像是称为 Adob​​e JPEG 的 JPEG 文件的特殊格式.Adobe JPEG 最奇特的地方可能是它允许以 RGB 格式存储图像,尽管它也允许其他格式.大多数 JPEG 是 JFIF 或 EXIF 格式,不使用 RGB.

The provided image is a peculiar format for a JPEG file called Adobe JPEG. Probably the most peculiar thing about an Adobe JPEG is that it allows storing the image in RGB format, though it also allows other formats. Most JPEGs are JFIF or EXIF format, which do not use RGB.

在复制 RGB 数据时,无论 Delphi 在做什么,当它加载到画布上时,它都会反转红色和蓝色数据.它将它加载为 BGR 而不是 RGB.这可能是因为 Windows(24 位和 32 位)DIB (BMP) 以 BGR 格式存储.

When copying the RGB data, whatever Delphi's doing, it's reversing the red and blue data when it's loading it onto the canvas. It's loading it as BGR instead of RGB. This may be because Windows (24-bit and 32-bit) DIBs (BMPs) are stored in BGR format.

我猜该错误会出现在 Delphi 中的任何 RGB JPEG 中.由于大多数 JPEG 不使用 RGB,因此该错误的发生率很低.如果您有 JPEG 单元的来源,那么简单的解决方法是在加载 RGB JPEG 时颠倒顺序.

I'm guessing that the bug will appear in Delphi for any RGB JPEG. Since most JPEGs do not use RGB, the incidence of the bug is low. The easy fix, if you have the source to the JPEG unit, is to reverse the order when loading an RGB JPEG.

如果您没有资源,请继续.

If you don't have the source, then continue on.

Adobe JPEG 以这样的格式指定颜色的顺序(十六进制)43 11 00 47 11 00 42 11 00 在十六进制编辑器中看起来像这样R..G..B.如果你通过十六进制编辑器反转 RB 在这里,它在 Windows 中显示错误,在 Delphi 中正确.

The Adobe JPEG specifies the order of the colors in a format like this (in Hex) 43 11 00 47 11 00 42 11 00 that looks like this in a hex editor R..G..B. If you reverse the R and B here via a Hex editor, it shows wrong in Windows, and right in Delphi.

要识别 Adob​​e JPEG,前四个字节是(十六进制)FF D8 FF EDFF D8 FF EE,带有 EDEE 是区分字节.所有 JPEG 文件都以 FF D8 FF 开头.

To recognize an Adobe JPEG, the first four bytes are either (in Hex) FF D8 FF ED or FF D8 FF EE, with the ED and EE being the differentiating bytes. All JPEG files start with FF D8 FF.

在这些字节之后是代表类型标记长度的两个字节,然后是 (In ASCII) Adobe,然后是另外六个字节(代表版本等),最后是 (第 18 个字节)是指定格式的字节.0 表示 RGB.因此,请检查那些重要字节,然后采取相应措施.

After those bytes are two bytes that represent the length of the type marker, followed by (In ASCII) Adobe, followed by six more bytes (representing the version, etc.) and finally, (the 18th byte) is the byte that specifies the format. 0 means RGB. So, check for those significant bytes, and then act accordingly.

您必须反转文件头中的 RGB 顺序(欺骗 Delphi),或者将其复制到 TBitmap 并使用 ScanLine 将 RGB 反转到正确的顺序.

You'll have to reverse the RGB order in the file header (to lie to Delphi), or copy it to a TBitmap and use ScanLine to reverse the RGB to the proper order.

格式详细信息来自阅读libJPEG C 源代码.

The format details are from reading the libJPEG source in C.

这篇关于将 Jpeg 图像转换为 Bmp - 有些图像呈蓝色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆