Page 1 of 1

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

Posted: 2018-12-10T13:17:24-07:00
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

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

Posted: 2018-12-10T14:26:18-07:00
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.

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

Posted: 2018-12-10T17:19:21-07:00
by fmw42
It would help if you provide the input image by itself.

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

Posted: 2018-12-11T12:39:20-07:00
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.