考虑引入lcmsnet库,对CMYK色彩空间图像提供ICC校正

Open
#307 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
csharp

Research direction

Start by reproducing the CMYK color issue with the linked JPG sample and the provided JPX-CMYK.zip sample, then inspect the current FreeImage handling of CMYK components and ICC data. Compare whether Libjpeg, OpenJpegDotNet, and lcmsnet can provide the needed extraction and conversion; done means CMYK JPG and JPX images are rendered with ICC correction rather than appearing color-shifted.

Written by the indexing model from the issue text.

Description

目前补丁丁没有考虑ICC配置文件。所以cmyk色彩空间的jpg和jpx图像大多是偏色的(有些情况甚至没读ICC数据)。

我不清楚freeimage是否支持解析jpg和jpx的CMYK分量,如果不支持的话,可能需要Libjpeg和OpenJpegDotNet库来提取原始分量,然后才能用lcmsnet校正(你还得自己编译一个lcms.dll)。

这里有jpg cmyk样本了,我再提供一个jpx cmyk样本

JPX-CMYK.zip

下面是AI写的调用lcmsnet的代码,配合notBald的LibJPEGv9.net和OpenJpegDotNet已经测试通过。

Profile profile = null;
if (info.ICCProfile != null)
{
    try
    {
        profile = Profile.Open(info.ICCProfile);  //读取PDF里面的icc
    }
    catch { }
}
if (profile == null)
{
    string iccfilename = "xxxx.USWebCoatedSWOP.icc";
    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream(iccfilename);
    byte[] defaultICC = stream.ToByteArray();

    profile = Profile.Open(defaultICC);
}
using var rgbprofile = Profile.Create_sRGB();
using (profile)
{
    //创建transform,从cmyk转为bgra
    //渲染意图:INTENT_PERCEPTUAL	保持视觉一致性(默认),适合照片类图像
    //标志位: 无标志
    using var transform = Transform.Create(profile, Cms.TYPE_CMYK_8, rgbprofile, Cms.TYPE_BGRA_8, Intent.Perceptual, CmsFlags.None);

    byte[] rgbBytes = new byte[cmykBytes.Length];
    transform.DoTransform(cmykBytes, rgbBytes, info.Width * info.Height);

    // 写入Bitmap
    bmp = new Bitmap(info.Width, info.Height, PixelFormat.Format32bppArgb);
    var rect = new Rectangle(0, 0, info.Width, info.Height);
    var bitmapData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);

    int srcStride = info.Width * 4;          
    int dstStride = Math.Abs(bitmapData.Stride); 

    Parallel.For(0, info.Height, y =>
    {
        IntPtr dst = bitmapData.Scan0 + y * dstStride;
        int srcOffset = y * srcStride;
        Marshal.Copy(rgbBytes, srcOffset, dst, srcStride);
    });

    bmp.UnlockBits(bitmapData);
}
Dominant language
C#
Stars
12.7k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from wmjordan/PDFPatcher

All issues in wmjordan/PDFPatcher

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.