|
| 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 | +// FrameDurationsToGifDelays converts frame durations in milliseconds to |
| 47 | +// GIF delays in 100ths of a second. |
| 48 | +func FrameDurationsToGifDelays(frameDurations []int) []int { |
| 49 | + delays := make([]int, len(frameDurations)) |
| 50 | + for i, fd := range frameDurations { |
| 51 | + delays[i] = fd / 10 |
| 52 | + if delays[i] == 0 && fd > 0 { |
| 53 | + delays[i] = 1 |
| 54 | + } |
| 55 | + } |
| 56 | + return delays |
| 57 | +} |
| 58 | + |
| 59 | +// GifDelaysToFrameDurations converts GIF delays in 100ths of a second to |
| 60 | +// frame durations in milliseconds. |
| 61 | +func GifDelaysToFrameDurations(delays []int) []int { |
| 62 | + frameDurations := make([]int, len(delays)) |
| 63 | + for i, d := range delays { |
| 64 | + frameDurations[i] = d * 10 |
| 65 | + } |
| 66 | + return frameDurations |
| 67 | +} |
0 commit comments