|
| 1 | +// Package jpegli implements an JPEG image encoder/decoder based on jpegli compiled to WASM. |
| 2 | +package jpegli |
| 3 | + |
| 4 | +import "C" |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "image" |
| 8 | + "image/draw" |
| 9 | + "io" |
| 10 | +) |
| 11 | + |
| 12 | +// Errors . |
| 13 | +var ( |
| 14 | + ErrMemRead = errors.New("jpegli: mem read failed") |
| 15 | + ErrMemWrite = errors.New("jpegli: mem write failed") |
| 16 | + ErrDecode = errors.New("jpegli: decode failed") |
| 17 | + ErrEncode = errors.New("jpegli: encode failed") |
| 18 | +) |
| 19 | + |
| 20 | +// DefaultQuality is the default quality encoding parameter. |
| 21 | +const DefaultQuality = 75 |
| 22 | + |
| 23 | +// DefaultDCTMethod is the default DCT algorithm method. |
| 24 | +const DefaultDCTMethod = DCTISlow |
| 25 | + |
| 26 | +// DCTMethod is the DCT/IDCT method type. |
| 27 | +type DCTMethod int |
| 28 | + |
| 29 | +const ( |
| 30 | + // DCTISlow is slow but accurate integer algorithm |
| 31 | + DCTISlow DCTMethod = iota |
| 32 | + // DCTIFast is faster, less accurate integer method |
| 33 | + DCTIFast |
| 34 | + // DCTFloat is floating-point: accurate, fast on fast HW |
| 35 | + DCTFloat |
| 36 | +) |
| 37 | + |
| 38 | +// EncodingOptions are the encoding parameters. |
| 39 | +type EncodingOptions struct { |
| 40 | + // Quality in the range [0,100]. Default is 75. |
| 41 | + Quality int |
| 42 | + // Chroma subsampling setting, 444|440|422|420. |
| 43 | + ChromaSubsampling image.YCbCrSubsampleRatio |
| 44 | + // Progressive level in the range [0,2], where level 0 is sequential, and greater level value means more progression steps. |
| 45 | + ProgressiveLevel int |
| 46 | + // Huffman code optimization. |
| 47 | + // Enabled by default. |
| 48 | + OptimizeCoding bool |
| 49 | + // Uses adaptive quantization for creating more zero coefficients. |
| 50 | + // Enabled by default. |
| 51 | + AdaptiveQuantization bool |
| 52 | + // Use standard quantization tables from Annex K of the JPEG standard. |
| 53 | + // By default, jpegli uses a different set of quantization tables and different scaling parameters for DC and AC coefficients. |
| 54 | + StandardQuantTables bool |
| 55 | + // Apply fancy downsampling. |
| 56 | + FancyDownsampling bool |
| 57 | + // DCTMethod is the DCT algorithm method. |
| 58 | + DCTMethod DCTMethod |
| 59 | +} |
| 60 | + |
| 61 | +// DecodingOptions are the decoding parameters. |
| 62 | +type DecodingOptions struct { |
| 63 | + // ScaleTarget is the target size to scale image. |
| 64 | + ScaleTarget image.Rectangle |
| 65 | + // Fancy upsampling. |
| 66 | + FancyUpsampling bool |
| 67 | + // Block smoothing. |
| 68 | + BlockSmoothing bool |
| 69 | + // Use arithmetic coding instead of Huffman. |
| 70 | + ArithCode bool |
| 71 | + // DCTMethod is DCT Algorithm method. |
| 72 | + DCTMethod DCTMethod |
| 73 | +} |
| 74 | + |
| 75 | +// Decode reads a JPEG image from r and returns it as an image.Image. |
| 76 | +func Decode(r io.Reader) (image.Image, error) { |
| 77 | + var err error |
| 78 | + var img image.Image |
| 79 | + |
| 80 | + img, _, err = decode(r, false, false, false, false, DefaultDCTMethod, 0, 0) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + return img, nil |
| 86 | +} |
| 87 | + |
| 88 | +// DecodeWithOptions reads a JPEG image from r with decoding options. |
| 89 | +func DecodeWithOptions(r io.Reader, o *DecodingOptions) (image.Image, error) { |
| 90 | + var err error |
| 91 | + var img image.Image |
| 92 | + |
| 93 | + tw := o.ScaleTarget.Dx() |
| 94 | + th := o.ScaleTarget.Dy() |
| 95 | + fancyUpsampling := o.FancyUpsampling |
| 96 | + blockSmoothing := o.BlockSmoothing |
| 97 | + arithCode := o.ArithCode |
| 98 | + dctMethod := o.DCTMethod |
| 99 | + |
| 100 | + img, _, err = decode(r, false, fancyUpsampling, blockSmoothing, arithCode, dctMethod, tw, th) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + return img, nil |
| 106 | +} |
| 107 | + |
| 108 | +// DecodeConfig returns the color model and dimensions of a JPEG image without decoding the entire image. |
| 109 | +func DecodeConfig(r io.Reader) (image.Config, error) { |
| 110 | + var err error |
| 111 | + var cfg image.Config |
| 112 | + |
| 113 | + _, cfg, err = decode(r, true, false, false, false, DefaultDCTMethod, 0, 0) |
| 114 | + if err != nil { |
| 115 | + return image.Config{}, err |
| 116 | + } |
| 117 | + |
| 118 | + return cfg, nil |
| 119 | +} |
| 120 | + |
| 121 | +// Encode writes the image m to w with the given options. |
| 122 | +func Encode(w io.Writer, m image.Image, o ...*EncodingOptions) error { |
| 123 | + quality := DefaultQuality |
| 124 | + chromaSubsampling := image.YCbCrSubsampleRatio420 |
| 125 | + progressiveLevel := 0 |
| 126 | + optimizeCoding := true |
| 127 | + adaptiveQuantization := true |
| 128 | + standardQuantTables := false |
| 129 | + fancyDownsampling := false |
| 130 | + dctMethod := DefaultDCTMethod |
| 131 | + |
| 132 | + if o != nil && o[0] != nil { |
| 133 | + opt := o[0] |
| 134 | + quality = opt.Quality |
| 135 | + chromaSubsampling = opt.ChromaSubsampling |
| 136 | + progressiveLevel = opt.ProgressiveLevel |
| 137 | + |
| 138 | + if quality <= 0 { |
| 139 | + quality = DefaultQuality |
| 140 | + } else if quality > 100 { |
| 141 | + quality = 100 |
| 142 | + } |
| 143 | + |
| 144 | + if progressiveLevel < 0 { |
| 145 | + progressiveLevel = 0 |
| 146 | + } else if progressiveLevel > 2 { |
| 147 | + progressiveLevel = 2 |
| 148 | + } |
| 149 | + |
| 150 | + optimizeCoding = opt.OptimizeCoding |
| 151 | + adaptiveQuantization = opt.AdaptiveQuantization |
| 152 | + standardQuantTables = opt.StandardQuantTables |
| 153 | + fancyDownsampling = opt.FancyDownsampling |
| 154 | + dctMethod = opt.DCTMethod |
| 155 | + } |
| 156 | + |
| 157 | + err := encode(w, m, quality, int(chromaSubsampling), progressiveLevel, optimizeCoding, adaptiveQuantization, |
| 158 | + standardQuantTables, fancyDownsampling, dctMethod) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func imageToRGBA(src image.Image) *image.RGBA { |
| 167 | + if dst, ok := src.(*image.RGBA); ok { |
| 168 | + return dst |
| 169 | + } |
| 170 | + |
| 171 | + b := src.Bounds() |
| 172 | + dst := image.NewRGBA(b) |
| 173 | + draw.Draw(dst, dst.Bounds(), src, b.Min, draw.Src) |
| 174 | + |
| 175 | + return dst |
| 176 | +} |
| 177 | + |
| 178 | +func yCbCrSize(r image.Rectangle, subsampleRatio image.YCbCrSubsampleRatio) (w, h, cw, ch int) { |
| 179 | + w, h = r.Dx(), r.Dy() |
| 180 | + |
| 181 | + switch subsampleRatio { |
| 182 | + case image.YCbCrSubsampleRatio422: |
| 183 | + cw = (r.Max.X+1)/2 - r.Min.X/2 |
| 184 | + ch = h |
| 185 | + case image.YCbCrSubsampleRatio420: |
| 186 | + cw = (r.Max.X+1)/2 - r.Min.X/2 |
| 187 | + ch = (r.Max.Y+1)/2 - r.Min.Y/2 |
| 188 | + case image.YCbCrSubsampleRatio440: |
| 189 | + cw = w |
| 190 | + ch = (r.Max.Y+1)/2 - r.Min.Y/2 |
| 191 | + case image.YCbCrSubsampleRatio411: |
| 192 | + cw = (r.Max.X+3)/4 - r.Min.X/4 |
| 193 | + ch = h |
| 194 | + case image.YCbCrSubsampleRatio410: |
| 195 | + cw = (r.Max.X+3)/4 - r.Min.X/4 |
| 196 | + ch = (r.Max.Y+1)/2 - r.Min.Y/2 |
| 197 | + default: |
| 198 | + cw = w |
| 199 | + ch = h |
| 200 | + } |
| 201 | + |
| 202 | + return |
| 203 | +} |
| 204 | + |
| 205 | +func init() { |
| 206 | + image.RegisterFormat("jpeg", "\xff\xd8", Decode, DecodeConfig) |
| 207 | +} |
0 commit comments