| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package himage provides some high level image types and interfaces. |
| 15 |
package himage |
| 16 |
|
| 17 |
import "image" |
| 18 |
|
| 19 |
// AnimatedImage represents an animated image. |
| 20 |
// This is currently supported for GIF and WebP images. |
| 21 |
type AnimatedImage interface { |
| 22 |
image.Image // The first frame. |
| 23 |
GetRaw() any // *gif.GIF or *WEBP. |
| 24 |
GetLoopCount() int // Number of times to loop the animation. 0 means infinite. |
| 25 |
ImageFrames |
| 26 |
} |
| 27 |
|
| 28 |
// ImageFrames provides access to the frames of an animated image. |
| 29 |
type ImageFrames interface { |
| 30 |
GetFrames() []image.Image |
| 31 |
|
| 32 |
// Frame durations in milliseconds. |
| 33 |
// Note that Gif frame durations are in 100ths of a second, |
| 34 |
// so they need to be multiplied by 10 to get milliseconds and vice versa. |
| 35 |
GetFrameDurations() []int |
| 36 |
|
| 37 |
SetFrames(frames []image.Image) |
| 38 |
SetWidthHeight(width, height int) |
| 39 |
} |
| 40 |
|
| 41 |
// ImageConfigProvider provides access to the image.Config of an image. |
| 42 |
type ImageConfigProvider interface { |
| 43 |
GetImageConfig() image.Config |
| 44 |
} |
| 45 |
|
| 46 |
// ColorPropertiesProvider provides access to CICP color properties (for HDR images). |
| 47 |
// Images implementing this interface preserve color space information through processing. |
| 48 |
type ColorPropertiesProvider interface { |
| 49 |
GetColorPrimaries() int |
| 50 |
GetTransferCharacteristics() int |
| 51 |
GetMatrixCoefficients() int |
| 52 |
} |
| 53 |
|
| 54 |
// HasColorProperties returns true if the image has non-zero color properties that should be preserved. |
| 55 |
func HasColorProperties(img image.Image) bool { |
| 56 |
if cpp, ok := img.(ColorPropertiesProvider); ok { |
| 57 |
// Consider it as having properties if any value is non-zero. |
| 58 |
return cpp.GetColorPrimaries() > 0 || cpp.GetTransferCharacteristics() > 0 || cpp.GetMatrixCoefficients() > 0 |
| 59 |
} |
| 60 |
return false |
| 61 |
} |
| 62 |
|
| 63 |
// FrameDurationsToGifDelays converts frame durations in milliseconds to |
| 64 |
// GIF delays in 100ths of a second. |
| 65 |
func FrameDurationsToGifDelays(frameDurations []int) []int { |
| 66 |
delays := make([]int, len(frameDurations)) |
| 67 |
for i, fd := range frameDurations { |
| 68 |
delays[i] = fd / 10 |
| 69 |
if delays[i] == 0 && fd > 0 { |
| 70 |
delays[i] = 1 |
| 71 |
} |
| 72 |
} |
| 73 |
return delays |
| 74 |
} |
| 75 |
|
| 76 |
// GifDelaysToFrameDurations converts GIF delays in 100ths of a second to |
| 77 |
// frame durations in milliseconds. |
| 78 |
func GifDelaysToFrameDurations(delays []int) []int { |
| 79 |
frameDurations := make([]int, len(delays)) |
| 80 |
for i, d := range delays { |
| 81 |
frameDurations[i] = d * 10 |
| 82 |
} |
| 83 |
return frameDurations |
| 84 |
} |