Documentation
¶
Overview ¶
Package ultrahdr provides a pure-Go implementation of the UltraHDR JPEG/R format.
This is a pragmatic port focused on correctness and portability rather than performance. It uses the patched standard image/jpeg package for JPEG encode/decode and assembles/parses the JPEG/R container (MPF + XMP + ISO 21496-1 gain map metadata) in Go.
Index ¶
- func IsUltraHDR(r io.Reader) (bool, error)
- func Join(primaryJPEG, gainmapJPEG []byte, bundle *MetadataBundle, template *Result) ([]byte, error)
- func RebaseFile(inPath, newSDRPath, outPath string, opts ...RebaseOption) error
- func RebaseFromEXRFile(primaryPath, exrPath, outPath string, opts ...RebaseOption) error
- func RebaseFromTIFFFile(primaryPath, hdrPath, outPath string, opts ...RebaseOption) error
- func ResizeHDR(r io.Reader, specs ...ResizeSpec) error
- func ResizeSDR(r io.Reader, specs ...ResizeSpec) error
- type GainMapMetadata
- type GridOptions
- type Interpolation
- type MetadataBundle
- type MetadataSegments
- type RebaseOption
- func WithBaseQuality(quality int) RebaseOption
- func WithGainmapGamma(gamma float32) RebaseOption
- func WithGainmapOut(path string) RebaseOption
- func WithGainmapQuality(quality int) RebaseOption
- func WithGainmapScale(scale int) RebaseOption
- func WithHDRCapacityMax(limit float32) RebaseOption
- func WithICCProfile(profile []byte) RebaseOption
- func WithMultiChannelGainmap(enabled bool) RebaseOption
- func WithPrimaryOut(path string) RebaseOption
- type RebaseOptions
- type ResizeSpec
- type Result
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsUltraHDR ¶
IsUltraHDR performs a streaming UltraHDR check without loading the full image. It reads until the gainmap header is reached and scans APP metadata for XMP/ISO.
Example ¶
package main
import (
"os"
"github.com/vearutop/ultrahdr"
)
func main() {
f, err := os.Open("testdata/uhdr.jpg")
if err != nil {
return
}
defer f.Close()
_, _ = ultrahdr.IsUltraHDR(f)
}
func Join ¶ added in v0.0.6
func Join(primaryJPEG, gainmapJPEG []byte, bundle *MetadataBundle, template *Result) ([]byte, error)
Join assembles an UltraHDR container from primary and gainmap JPEGs. If bundle is provided, it is used as the metadata source. If template is provided, it is used to build the bundle. Otherwise gainmap metadata is extracted from the gainmap JPEG and EXIF/ICC are extracted from the primary JPEG.
func RebaseFile ¶ added in v0.0.6
func RebaseFile(inPath, newSDRPath, outPath string, opts ...RebaseOption) error
RebaseFile reads an UltraHDR JPEG, rebases it on newSDRPath, and writes the output.
func RebaseFromEXRFile ¶ added in v0.0.6
func RebaseFromEXRFile(primaryPath, exrPath, outPath string, opts ...RebaseOption) error
RebaseFromEXRFile generates an UltraHDR JPEG from an SDR primary and HDR EXR input.
func RebaseFromTIFFFile ¶ added in v0.0.6
func RebaseFromTIFFFile(primaryPath, hdrPath, outPath string, opts ...RebaseOption) error
RebaseFromTIFFFile generates an UltraHDR JPEG from an SDR primary and HDR TIFF input.
func ResizeHDR ¶ added in v0.0.6
func ResizeHDR(r io.Reader, specs ...ResizeSpec) error
ResizeHDR resizes an UltraHDR JPEG container to the requested dimensions. Results are delivered via ReceiveResult on each spec; ReceiveSplit runs before resizing.
Example ¶
package main
import (
"os"
"github.com/vearutop/ultrahdr"
)
func main() {
f, err := os.Open("testdata/uhdr.jpg")
if err != nil {
return
}
defer f.Close()
_ = ultrahdr.ResizeHDR(f, ultrahdr.ResizeSpec{
Width: 2400,
Height: 1600,
})
}
func ResizeSDR ¶ added in v0.0.6
func ResizeSDR(r io.Reader, specs ...ResizeSpec) error
ResizeSDR resizes one JPEG into multiple outputs with a single source decode. For each spec: when KeepMeta is true EXIF/ICC are preserved; otherwise output is metadata-free. Metadata-free outputs are converted to sRGB when source profile is recognized as wide gamut.
Example ¶
package main
import (
"image"
"os"
"github.com/vearutop/ultrahdr"
)
func main() {
f, err := os.Open("testdata/sample_srgb.jpg")
if err != nil {
return
}
crop := image.Rect(120, 80, 920, 680)
_ = ultrahdr.ResizeSDR(f, ultrahdr.ResizeSpec{
Width: 800,
Height: 600,
Crop: &crop,
Quality: 85,
Interpolation: ultrahdr.InterpolationLanczos2,
KeepMeta: true,
})
}
Example (Multi) ¶
package main
import (
"os"
"github.com/vearutop/ultrahdr"
)
func main() {
f, err := os.Open("testdata/sample_display_p3.jpg")
if err != nil {
return
}
specs := []ultrahdr.ResizeSpec{
{Width: 1200, Height: 800, Quality: 85, Interpolation: ultrahdr.InterpolationLanczos2, KeepMeta: true, ReceiveResult: func(res *ultrahdr.Result, err error) { _ = res }},
{Width: 600, Height: 400, Quality: 82, Interpolation: ultrahdr.InterpolationLanczos2, KeepMeta: false, ReceiveResult: func(res *ultrahdr.Result, err error) { _ = res }},
{Width: 300, Height: 200, Quality: 78, Interpolation: ultrahdr.InterpolationLanczos2, KeepMeta: false, ReceiveResult: func(res *ultrahdr.Result, err error) { _ = res }},
}
err = ultrahdr.ResizeSDR(f, specs...)
if err != nil {
return
}
}
Types ¶
type GainMapMetadata ¶
type GainMapMetadata struct {
Version string
MaxContentBoost [3]float32
MinContentBoost [3]float32
Gamma [3]float32
OffsetSDR [3]float32
OffsetHDR [3]float32
HDRCapacityMin float32
HDRCapacityMax float32
UseBaseCG bool
}
GainMapMetadata corresponds to the float metadata in the C++ library.
type GridOptions ¶ added in v0.0.7
type GridOptions struct {
Quality int // JPEG quality for the output (0 uses default).
Interpolation Interpolation // Resize interpolation mode.
Background color.Color // Background fill color (nil uses black).
ReceivePosition func(i int, top, left uint, width, height uint)
}
GridOptions configures grid rendering for SDR inputs.
type Interpolation ¶ added in v0.0.2
type Interpolation int
Interpolation selects the built-in interpolation mode.
const ( // InterpolationNearest is nearest-neighbor sampling. InterpolationNearest Interpolation = iota // InterpolationBilinear is linear sampling. InterpolationBilinear // InterpolationBicubic is cubic sampling. InterpolationBicubic // InterpolationMitchellNetravali is Mitchell-Netravali sampling. InterpolationMitchellNetravali // InterpolationLanczos2 is Lanczos sampling with a=2. InterpolationLanczos2 // InterpolationLanczos3 is Lanczos sampling with a=3. InterpolationLanczos3 )
type MetadataBundle ¶
type MetadataBundle struct {
Format string `json:"format"`
PrimaryXMP []byte `json:"primary_xmp,omitempty"`
PrimaryISO []byte `json:"primary_iso,omitempty"`
SecondaryXMP []byte `json:"secondary_xmp,omitempty"`
SecondaryISO []byte `json:"secondary_iso,omitempty"`
Exif []byte `json:"exif,omitempty"`
ICC [][]byte `json:"icc,omitempty"`
}
MetadataBundle captures the metadata needed to reassemble an UltraHDR container. Byte fields are base64-encoded in JSON.
func (*MetadataBundle) Validate ¶
func (b *MetadataBundle) Validate() error
Validate ensures the bundle has the required fields to build a container.
type MetadataSegments ¶
type MetadataSegments struct {
PrimaryXMP []byte
PrimaryISO []byte
SecondaryXMP []byte
SecondaryISO []byte
}
MetadataSegments holds raw APP payloads for XMP/ISO blocks. These payloads include the namespace prefix and null terminator.
type RebaseOption ¶ added in v0.0.6
type RebaseOption func(*RebaseOptions)
RebaseOption configures rebase behavior.
func WithBaseQuality ¶ added in v0.0.6
func WithBaseQuality(quality int) RebaseOption
WithBaseQuality sets the JPEG quality for the primary SDR output.
func WithGainmapGamma ¶ added in v0.0.6
func WithGainmapGamma(gamma float32) RebaseOption
WithGainmapGamma sets the gamma to apply to gainmap encoding.
func WithGainmapOut ¶ added in v0.0.6
func WithGainmapOut(path string) RebaseOption
WithGainmapOut sets an optional output path for the rebased gainmap JPEG.
func WithGainmapQuality ¶ added in v0.0.6
func WithGainmapQuality(quality int) RebaseOption
WithGainmapQuality sets the JPEG quality for the gainmap output.
func WithGainmapScale ¶ added in v0.0.6
func WithGainmapScale(scale int) RebaseOption
WithGainmapScale sets the downscale factor for gainmap generation.
func WithHDRCapacityMax ¶ added in v0.0.6
func WithHDRCapacityMax(limit float32) RebaseOption
WithHDRCapacityMax clamps maximum HDR capacity when generating gainmaps.
func WithICCProfile ¶ added in v0.0.6
func WithICCProfile(profile []byte) RebaseOption
WithICCProfile sets the ICC profile bytes for the new SDR image.
func WithMultiChannelGainmap ¶ added in v0.0.6
func WithMultiChannelGainmap(enabled bool) RebaseOption
WithMultiChannelGainmap toggles RGB gainmap encoding.
func WithPrimaryOut ¶ added in v0.0.6
func WithPrimaryOut(path string) RebaseOption
WithPrimaryOut sets an optional output path for the rebased primary JPEG.
type RebaseOptions ¶
type RebaseOptions struct {
BaseQuality int // JPEG quality for the primary SDR output (0 uses default).
GainmapQuality int // JPEG quality for the gainmap output (0 uses default).
GainmapScale int // Downscale factor for gainmap generation (higher is smaller/faster).
GainmapGamma float32 // Gamma to apply to gainmap encoding (0 uses default).
UseMultiChannel bool // Encode gainmap as RGB instead of single-channel.
HDRCapacityMax float32 // Clamp maximum HDR capacity when generating gainmaps.
ICCProfile []byte // ICC profile bytes for new SDR when not embedded in input.
PrimaryOut string // Optional output path for the rebased primary JPEG.
GainmapOut string // Optional output path for the rebased gainmap JPEG.
}
RebaseOptions controls gainmap rebase behavior.
type ResizeSpec ¶ added in v0.0.4
type ResizeSpec struct {
Width uint // Target width in pixels.
Height uint // Target height in pixels.
Crop *image.Rectangle // Optional crop rectangle in source pixels.
Quality int // SDR/primary JPEG quality (0 uses default).
GainmapQuality int // Gainmap JPEG quality for HDR resize (0 uses default or Quality).
Interpolation Interpolation // Resize interpolation mode for SDR and HDR paths.
KeepMeta bool // SDR: preserve EXIF/ICC and skip sRGB conversion when true.
ReceiveResult func(res *Result, err error) // Callback for each output.
ReceiveSplit func(sr *Result) // HDR: callback with split result before resizing.
}
ResizeSpec describes one output variant for ResizeSDR/ResizeHDR.
type Result ¶ added in v0.0.6
type Result struct {
Container []byte
Primary []byte
Gainmap []byte
Meta *GainMapMetadata
Segs *MetadataSegments
}
Result contains the primary/gainmap JPEGs with optional container and metadata.
func Grid ¶ added in v0.0.7
Grid builds a sprite grid from SDR images. Inputs are resized to fit each cell preserving aspect ratio and padded with black. Output is encoded as sRGB JPEG.
Example ¶
package main
import (
"image/color"
"io"
"os"
"github.com/vearutop/ultrahdr"
)
func main() {
paths := []string{
"testdata/sample_srgb.jpg",
"testdata/sample_display_p3.jpg",
"testdata/sample_adobe_rgb.jpg",
}
readers := make([]io.Reader, 0, len(paths))
for _, p := range paths {
f, err := os.Open(p)
if err != nil {
return
}
defer f.Close()
readers = append(readers, f)
}
_, _ = ultrahdr.Grid(readers, 2, 400, 300, &ultrahdr.GridOptions{
Quality: 85,
Interpolation: ultrahdr.InterpolationLanczos2,
Background: color.Black,
})
}
func Rebase ¶ added in v0.0.6
Rebase replaces the primary SDR image while adjusting the gainmap to preserve the original HDR reconstruction as closely as possible.
func Split ¶
Split extracts primary/gainmap JPEGs, metadata, and raw XMP/ISO segments.
Example (JoinWithBundle) ¶
package main
import (
"os"
"github.com/vearutop/ultrahdr"
)
func main() {
f, err := os.Open("testdata/uhdr.jpg")
if err != nil {
return
}
defer f.Close()
sr, err := ultrahdr.Split(f)
if err != nil {
return
}
bundle, err := sr.BuildMetadataBundle()
if err != nil {
return
}
_, _ = ultrahdr.Join(sr.Primary, sr.Gainmap, bundle, nil)
}
func (*Result) BuildMetadataBundle ¶ added in v0.0.6
func (r *Result) BuildMetadataBundle() (*MetadataBundle, error)
BuildMetadataBundle builds a metadata bundle from split segments and primary JPEG.