"Apply" ICC profile to sRGB image in PNG format without embedding it

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
rime
Posts: 5
Joined: 2018-01-25T08:42:47-07:00
Authentication code: 1152

"Apply" ICC profile to sRGB image in PNG format without embedding it

Post by rime »

Input:
- PNG image with sRGB color space, no ICC profile embedded
- ICC profile file
Expected output:
- PNG image with sRGB color space, no ICC profile embedded but image colors should be "translated" with input ICC profile
- the output has to be in PNG format, no quality loss is acceptable (i.e. JPG)
In other words I want IM temporarily act as an image viewer, and "flatten" image with ICC profile to sRGB

So far I was only able to embed ICC profile (Delphi code):

Code: Select all

function ApplyICCProfile(AInputFileName, AOutputFileName, AICCProfilePath: String): Boolean;
var
  msProfile: TMemoryStream;
  wand: PMagickWand;
begin
  Result := False;

  MagickWandGenesis;
  try
    wand := NewMagickWand;
    try
      if MagickReadImage(wand, PUTF8Char(UTF8Encode(AInputFileName))) = MagickTrue then
      begin
        msProfile := TMemoryStream.Create();
        try
          msProfile.LoadFromFile(AICCProfilePath);
          MagickProfileImage(wand, 'ICC', msProfile.Memory, msProfile.Size);
          Result := (MagickWriteImage(wand, PUTF8Char(UTF8Encode(AOutputFileName))) = MagickTrue);
        finally
          msProfile.Free;
        end;
      end;
    finally
      DestroyMagickWand(wand);
    end;
  finally
    MagickWandTerminus;
  end;
end;
It is displayed incorrectly in IrfanView, while GIMP asks to do a conversion to sRGB since profile is still embedded:
Image

I've tried calling MagickRemoveImageProfile after MagickProfileImage, but it simply removed ICC profile and output file is color exact to input.
Do I need to add sRGB profile first to image, and then do conversion ? If so, what MagickWand method has to be called ?
Any help appreciated, C, PHP - it doesn't matter I will understand your code. IM 7.0.8-Q16
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: "Apply" ICC profile to sRGB image in PNG format without embedding it

Post by snibgo »

What you ask for doesn't seem to make logical sense.

If your input image has no embedded profile, and you have an ICC file, how will "image colors should be "translated" with input ICC profile"? Translated from what? To what?

ICC profiles can be "assigned" and "converted". Assignment changes metadata without changing pixel values, declaring what colours should be shown with those values. Conversion from one profile to another changes metadata and pixel values without changing colours (except for limitations imposed by the profile).

Profiles can be removed from images, of course, which changes metadata only. But if you merely assign then remove, nothing changes.

Perhaps you want to assign one profile, then convert to another profile, then remove that profile. That's no problem, but it needs two profiles.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Apply" ICC profile to sRGB image in PNG format without embedding it

Post by fmw42 »

It would help if you provide the input image by itself.
rime
Posts: 5
Joined: 2018-01-25T08:42:47-07:00
Authentication code: 1152

Re: "Apply" ICC profile to sRGB image in PNG format without embedding it

Post by rime »

Solved.

Code: Select all

          MagickSetImageProfile(wand, 'ICC', msProfileRGB.Memory, msProfileRGB.Size);
          MagickProfileImage(wand, 'ICC', msProfile.Memory, msProfile.Size);
          MagickTransformImageColorspace(wand, CMYKColorspace);
Where msProfileRGB is a sRGB profile file.
Post Reply