icc

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package icc converts the colors of an image to sRGB with an ICC profile or with the CICP code points of an image (D61).

A program gives the image and the profile that its file embeds, such as the iCCP chunk of a PNG file or the colr box of an AVIF file. ToSRGB returns the image in sRGB, with the alpha of each pixel as it was. A program that converts several images with one profile parses the profile once with Parse, and calls the method ToSRGB of the Profile.

The package is a wrapper over internal/moxcms, the port of a subset of the Rust crate moxcms by Radzivon Bartoshyk, under the BSD-3-Clause license or the Apache-2.0 license. LICENSE-MOXCMS.md and LICENSE-MOXCMS-APACHE.md in this folder are its license files. The default CMYK profile is a profile of Compact-ICC-Profiles, under CC0 1.0 Universal.

The transforms of the port have samples of 8 bits. A transform of an image with samples of 16 bits keeps the alpha of 16 bits, and it converts the colors with 8 bits.

A profile is input from a stranger. A profile that is not valid gives an error that wraps ivy.ErrFormat, and it never makes the package panic. A profile that the port cannot use gives an error that wraps ivy.ErrUnsupported. A profile or an image that is too large gives an error that wraps ivy.ErrLimit.

Index

Examples

Constants

View Source
const MaxPixels = ivy.DefaultMaxPixels

MaxPixels is the number of pixels of the largest image that ToSRGB converts. It is the default of ivy.Limits.MaxPixels.

View Source
const MaxProfileSize = 10 << 20

MaxProfileSize is the size in bytes of the smallest profile that Parse refuses with ivy.ErrLimit. It is the limit of moxcms.

Variables

This section is empty.

Functions

func DefaultCMYK

func DefaultCMYK() []byte

DefaultCMYK returns a copy of the CMYK profile that ivy uses for CMYK colors without a profile, such as the color space DeviceCMYK of PDF or a JPEG 2000 image in CMYK. It is CGATS001Compat-v2-micro.icc of Compact-ICC-Profiles, a compact profile of CGATS TR 001 (SWOP), under CC0 1.0 Universal.

func ToSRGB

func ToSRGB(ctx context.Context, img image.Image, profile []byte, intent Intent) (image.Image, error)

ToSRGB parses profile and returns img in sRGB, as the method ToSRGB of Profile does. If profile is empty, ToSRGB converts an image in CMYK with DefaultCMYK, and it takes any other image as sRGB, so it only copies the pixels.

Example

A program converts a CMYK image without a profile with the default CMYK profile of ivy. With a profile, it gives the bytes of the profile instead of nil.

package main

import (
	"context"
	"fmt"
	"image"
	"image/color"

	"github.com/xo/ivy/icc"
)

func main() {
	img := image.NewCMYK(image.Rect(0, 0, 2, 1))
	img.SetCMYK(0, 0, color.CMYK{})
	img.SetCMYK(1, 0, color.CMYK{K: 255})
	out, err := icc.ToSRGB(context.Background(), img, nil, icc.Perceptual)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%T %v %v\n", out, out.At(0, 0), out.At(1, 0))
}
Output:
*image.NRGBA {255 255 255 255} {35 31 32 255}

Types

type CICP

type CICP struct {
	// ColorPrimaries is the code point of the primaries and of the white,
	// such as 1 for BT.709 and sRGB, 9 for BT.2020, or 12 for Display P3.
	ColorPrimaries uint8
	// TransferCharacteristics is the code point of the transfer function,
	// such as 13 for sRGB, 16 for PQ (SMPTE ST 2084), or 18 for HLG.
	TransferCharacteristics uint8
	// MatrixCoefficients is the code point of the matrix from RGB to YCbCr.
	MatrixCoefficients uint8
	// FullRange is true when the samples use the full range of their bits.
	FullRange bool
}

CICP holds the code points of Rec. ITU-T H.273 that describe the colors of an image without an ICC profile. An AVIF or a HEIC file holds them in a colr box of the type nclx, and a PNG file in a cICP chunk.

type Intent

type Intent uint8

Intent is a rendering intent of ICC.1. It chooses the table of a profile that maps the colors that sRGB cannot show. A matrix profile, such as the profile of a camera or of a display, has one mapping for each intent.

const (
	// Perceptual scales all the colors into sRGB. It is the intent for a
	// photograph.
	Perceptual Intent = 0
	// RelativeColorimetric keeps the colors that sRGB can show, maps the
	// white of the profile to the white of sRGB, and clips the other colors.
	RelativeColorimetric Intent = 1
	// Saturation keeps the saturation of the colors, for a chart.
	Saturation Intent = 2
	// AbsoluteColorimetric keeps the colors that sRGB can show, with the
	// white of the profile, and clips the other colors.
	AbsoluteColorimetric Intent = 3
)

The rendering intents of ICC.1, with their numbers in ICC.1.

func (Intent) String

func (i Intent) String() string

String returns the name of the intent.

type Profile

type Profile struct {
	// contains filtered or unexported fields
}

Profile is an ICC profile that Parse read, or the profile of CICP code points that FromCICP made. It is safe for concurrent use.

func FromCICP

func FromCICP(c CICP) (*Profile, error)

FromCICP returns the RGB profile of the primaries and of the transfer function of c, as moxcms makes it. The profile converts the red, the green and the blue of an image. The codec must convert YCbCr to RGB with MatrixCoefficients and FullRange first, so the transform does not use them. FromCICP only makes sure that MatrixCoefficients is a code point of H.273.

The transform does no tone mapping. A linear value of 1 is the white of sRGB. In PQ a linear value of 1 is 10000 cd/m2, so an image in PQ looks dark. A color that sRGB cannot show clips.

A code point that is reserved, or that is unspecified (2), gives an error that wraps ivy.ErrUnsupported. If a format reads an unspecified code point as sRGB, the program gives the primaries 1 and the transfer characteristics 13.

Example

An AVIF file in Display P3 holds the CICP code points 12, 13, 6 and true in its colr box. The red of Display P3 is past the red of sRGB, so it clips.

package main

import (
	"context"
	"fmt"
	"image"
	"image/color"

	"github.com/xo/ivy/icc"
)

func main() {
	p, err := icc.FromCICP(icc.CICP{
		ColorPrimaries:          12,
		TransferCharacteristics: 13,
		MatrixCoefficients:      6,
		FullRange:               true,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	img := image.NewNRGBA(image.Rect(0, 0, 1, 1))
	img.SetNRGBA(0, 0, color.NRGBA{R: 255, A: 255})
	out, err := p.ToSRGB(context.Background(), img, icc.Perceptual)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(out.At(0, 0))
}
Output:
{255 0 0 255}

func Parse

func Parse(data []byte) (*Profile, error)

Parse reads the ICC profile in data. The Profile keeps no reference to data.

Example

A program parses a profile once, and converts several images with it.

package main

import (
	"context"
	"fmt"
	"image"
	"image/color"

	"github.com/xo/ivy/icc"
)

func main() {
	p, err := icc.Parse(icc.DefaultCMYK())
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, k := range []uint8{0, 128} {
		img := image.NewCMYK(image.Rect(0, 0, 1, 1))
		img.SetCMYK(0, 0, color.CMYK{K: k})
		out, err := p.ToSRGB(context.Background(), img, icc.RelativeColorimetric)
		if err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println(out.At(0, 0))
	}
}
Output:
{255 255 255 255}
{146 148 151 255}

func (*Profile) ToSRGB

func (p *Profile) ToSRGB(ctx context.Context, img image.Image, intent Intent) (image.Image, error)

ToSRGB returns img in sRGB, with the colors of img in the space of p and the rendering intent intent. The result has the bounds of img. It is an *image.NRGBA64 when img has samples of more than 8 bits, such as an *image.Gray16, an *image.RGBA64 or an *image.NRGBA64, and an *image.NRGBA for any other image. The alpha of each pixel stays as it was.

A gray profile takes the luma of the colors of img. A profile of three channels, such as RGB or Lab, takes the red, the green and the blue of img, as the package image gives them. A CMYK profile needs an *image.CMYK, or another image with the model color.CMYKModel.

ToSRGB checks ctx between the rows of the image. It refuses an image of more than MaxPixels pixels with ivy.ErrLimit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL