Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
C programming

libpng12: png_set_sRGB, gAMA, and cHRM

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use png_set_sRGB() only when your encoded samples are already in the sRGB color space. It writes an sRGB declaration and rendering intent. Use png_set_sRGB_gAMA_and_cHRM() when you also want libpng to write the sRGB-compatible gAMA and cHRM chunks. Neither function converts arbitrary RGB data to sRGB.

What the two libpng setters declare

The libpng 1.2.5 manual documents both functions as metadata-writing APIs. They describe the color characteristics of the samples that will be encoded; they do not alter those samples.

Function Metadata written Use when
png_set_sRGB(png_ptr, info_ptr, srgb_intent) An sRGB chunk containing the rendering intent Your samples conform to sRGB and you do not need libpng to add compatibility gAMA and cHRM chunks
png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, srgb_intent) An sRGB chunk plus sRGB-compatible gAMA and cHRM chunks Your samples conform to sRGB and you want those additional chunks for compatibility

The sRGB chunk identifies the image samples as sRGB and stores one rendering-intent value. A file being made of red, green and blue channels is not sufficient evidence that it is sRGB.

How to set sRGB metadata

Call the setter after creating the libpng write structures and before writing the PNG information (normally before png_write_info()):

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);

When compatibility chunks are wanted, use:

png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
                           PNG_sRGB_INTENT_PERCEPTUAL);

Choose the intent that reflects the intended rendering goal, not the pixel format alone.

Rendering-intent constants

Value Intent Typical use described by PNG 1.2
0 Perceptual Photographs; adapt to the output gamut while accepting reduced colorimetric accuracy
1 Relative colorimetric Logos; preserve color appearance relative to the output device white point
2 Saturation Charts and graphs; preserve saturation at the expense of hue and lightness
3 Absolute colorimetric Proofs intended for another output device; preserve absolute colorimetry

The setter records the intent. It cannot force a viewer or operating system to perform a particular color-management operation.

What gAMA and cHRM mean

gAMA: sample-to-display intensity relationship

The PNG gAMA chunk describes the power-function relationship between normalized image samples and desired display output intensity. PNG stores gamma multiplied by 100,000; the sRGB-compatible value is 45455, approximately 1/2.2 in the specification’s encoding. Alpha is linear opacity and is not affected by gAMA. If the encoder does not know the image gamma, it should omit the chunk.

cHRM: primaries and reference white

The cHRM chunk stores CIE 1931 x,y chromaticities for the white point and red, green and blue primaries. Each value is multiplied by 100,000. The PNG 1.2 sRGB values are:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Coordinate Stored value
White point x, y 31270, 32900
Red x, y 64000, 33000
Green x, y 30000, 60000
Blue x, y 15000, 6000

Known chromaticities can be recorded with cHRM. Omitting it leaves primaries device-dependent. If you write cHRM, PNG encoder guidance recommends writing gAMA as well, because chromaticities alone provide limited information to decoders.

png_set_sRGB() versus png_set_sRGB_gAMA_and_cHRM()

The practical difference is the additional compatibility metadata. Both functions assert sRGB samples and both carry the rendering intent. The combined helper also causes libpng to emit the standard sRGB-compatible gamma and chromaticity values. Select between them based on whether those extra chunks are desired by your compatibility requirements; do not select either one as a color-conversion operation.

Chunk precedence and conflicts

A recognized sRGB chunk takes precedence over gAMA and cHRM. The PNG specification states: “When the sRGB chunk is present, applications that recognize it and are capable of color management [ICC] must ignore the gAMA and cHRM chunks and use the sRGB chunk instead.”

The specification also says that sRGB and iCCP should not both be present. Do not emit an embedded ICC profile that conflicts with an sRGB declaration. A non-sRGB image should instead carry accurate known gAMA/cHRM metadata or an appropriate ICC profile.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do not confuse metadata with gamma conversion

png_set_gAMA() writes image gamma metadata. By contrast, png_set_gamma() is a decoder-side transformation API: it changes decoded pixel values for a display path using screen gamma and file gamma. A typical decoder obtains file gamma with png_get_gAMA() and passes both values to png_set_gamma(). Calling png_set_sRGB() or png_set_sRGB_gAMA_and_cHRM() does not perform that transformation.

Code-review checklist

  • Verify that the encoded samples are already sRGB before calling either sRGB setter.
  • Choose a rendering intent that matches the output goal.
  • Use png_set_sRGB_gAMA_and_cHRM() when sRGB compatibility chunks are specifically wanted.
  • Never add sRGB merely because the image uses RGB channels.
  • Do not combine conflicting sRGB and iCCP declarations.
  • If the image is not sRGB, write accurate known color metadata or use an appropriate ICC profile.
  • Keep encoder metadata calls such as png_set_gAMA() separate from decoder transforms such as png_set_gamma().

Version scope

These API descriptions come from the archived libpng 1.2.5 manual and the PNG Specification, Version 1.2. They document behavior for that API and specification context; they do not establish the current maintenance status of the libpng 1.2 branch. Check the support status of the library version you plan to deploy before selecting it for new work.

Frequently Asked Questions

How do I set sRGB gamma and chromaticity in libpng?

Call png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, srgb_intent) before writing the PNG information, and only when the encoded samples truly conform to sRGB.

Does png_set_sRGB convert RGB pixels to sRGB?

No. It writes an sRGB declaration and rendering intent; the caller must perform any required color conversion before encoding.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.