| 1 | package gift |
| 2 | |
| 3 | import ( |
| 4 | "image" |
| 5 | "image/color" |
| 6 | "image/draw" |
| 7 | "sync" |
| 8 | ) |
| 9 | |
| 10 | type pixel struct { |
| 11 | r, g, b, a float32 |
| 12 | } |
| 13 | |
| 14 | type pixelSrcDst struct { |
| 15 | src []pixel |
| 16 | dst []pixel |
| 17 | } |
| 18 | |
| 19 | type imageType int |
| 20 | |
| 21 | const ( |
| 22 | itGeneric imageType = iota |
| 23 | itNRGBA |
| 24 | itNRGBA64 |
| 25 | itRGBA |
| 26 | itRGBA64 |
| 27 | itYCbCr |
| 28 | itGray |
| 29 | itGray16 |
| 30 | itPaletted |
| 31 | ) |
| 32 | |
| 33 | type pixelGetter struct { |
| 34 | it imageType |
| 35 | bounds image.Rectangle |
| 36 | image image.Image |
| 37 | nrgba *image.NRGBA |
| 38 | nrgba64 *image.NRGBA64 |
| 39 | rgba *image.RGBA |
| 40 | rgba64 *image.RGBA64 |
| 41 | gray *image.Gray |
| 42 | gray16 *image.Gray16 |
| 43 | ycbcr *image.YCbCr |
| 44 | paletted *image.Paletted |
| 45 | palette []pixel |
| 46 | } |
| 47 | |
| 48 | func newPixelGetter(img image.Image) *pixelGetter { |
| 49 | switch img := img.(type) { |
| 50 | case *image.NRGBA: |
| 51 | return &pixelGetter{ |
| 52 | it: itNRGBA, |
| 53 | bounds: img.Bounds(), |
| 54 | nrgba: img, |
| 55 | } |
| 56 | |
| 57 | case *image.NRGBA64: |
| 58 | return &pixelGetter{ |
| 59 | it: itNRGBA64, |
| 60 | bounds: img.Bounds(), |
| 61 | nrgba64: img, |
| 62 | } |
| 63 | |
| 64 | case *image.RGBA: |
| 65 | return &pixelGetter{ |
| 66 | it: itRGBA, |
| 67 | bounds: img.Bounds(), |
| 68 | rgba: img, |
| 69 | } |
| 70 | |
| 71 | case *image.RGBA64: |
| 72 | return &pixelGetter{ |
| 73 | it: itRGBA64, |
| 74 | bounds: img.Bounds(), |
| 75 | rgba64: img, |
| 76 | } |
| 77 | |
| 78 | case *image.Gray: |
| 79 | return &pixelGetter{ |
| 80 | it: itGray, |
| 81 | bounds: img.Bounds(), |
| 82 | gray: img, |
| 83 | } |
| 84 | |
| 85 | case *image.Gray16: |
| 86 | return &pixelGetter{ |
| 87 | it: itGray16, |
| 88 | bounds: img.Bounds(), |
| 89 | gray16: img, |
| 90 | } |
| 91 | |
| 92 | case *image.YCbCr: |
| 93 | return &pixelGetter{ |
| 94 | it: itYCbCr, |
| 95 | bounds: img.Bounds(), |
| 96 | ycbcr: img, |
| 97 | } |
| 98 | |
| 99 | case *image.Paletted: |
| 100 | return &pixelGetter{ |
| 101 | it: itPaletted, |
| 102 | bounds: img.Bounds(), |
| 103 | paletted: img, |
| 104 | palette: convertPalette(img.Palette), |
| 105 | } |
| 106 | |
| 107 | default: |
| 108 | return &pixelGetter{ |
| 109 | it: itGeneric, |
| 110 | bounds: img.Bounds(), |
| 111 | image: img, |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | const ( |
| 117 | qf8 = 1.0 / 0xff |
| 118 | qf16 = 1.0 / 0xffff |
| 119 | epal = qf16 * qf16 / 2 |
| 120 | ) |
| 121 | |
| 122 | func pixelFromColor(c color.Color) (px pixel) { |
| 123 | r16, g16, b16, a16 := c.RGBA() |
| 124 | switch a16 { |
| 125 | case 0: |
| 126 | px = pixel{0, 0, 0, 0} |
| 127 | case 0xffff: |
| 128 | r := float32(r16) * qf16 |
| 129 | g := float32(g16) * qf16 |
| 130 | b := float32(b16) * qf16 |
| 131 | px = pixel{r, g, b, 1} |
| 132 | default: |
| 133 | q := float32(1) / float32(a16) |
| 134 | r := float32(r16) * q |
| 135 | g := float32(g16) * q |
| 136 | b := float32(b16) * q |
| 137 | a := float32(a16) * qf16 |
| 138 | px = pixel{r, g, b, a} |
| 139 | } |
| 140 | return px |
| 141 | } |
| 142 | |
| 143 | func convertPalette(p []color.Color) []pixel { |
| 144 | pal := make([]pixel, len(p)) |
| 145 | for i := range p { |
| 146 | pal[i] = pixelFromColor(p[i]) |
| 147 | } |
| 148 | return pal |
| 149 | } |
| 150 | |
| 151 | func getPaletteIndex(pal []pixel, px pixel) int { |
| 152 | var k int |
| 153 | var dmin float32 = 4 |
| 154 | for i, palpx := range pal { |
| 155 | d := px.r - palpx.r |
| 156 | dcur := d * d |
| 157 | d = px.g - palpx.g |
| 158 | dcur += d * d |
| 159 | d = px.b - palpx.b |
| 160 | dcur += d * d |
| 161 | d = px.a - palpx.a |
| 162 | dcur += d * d |
| 163 | if dcur < epal { |
| 164 | return i |
| 165 | } |
| 166 | if dcur < dmin { |
| 167 | dmin = dcur |
| 168 | k = i |
| 169 | } |
| 170 | } |
| 171 | return k |
| 172 | } |
| 173 | |
| 174 | func (p *pixelGetter) getPixel(x, y int) pixel { |
| 175 | switch p.it { |
| 176 | case itNRGBA: |
| 177 | i := p.nrgba.PixOffset(x, y) |
| 178 | r := float32(p.nrgba.Pix[i+0]) * qf8 |
| 179 | g := float32(p.nrgba.Pix[i+1]) * qf8 |
| 180 | b := float32(p.nrgba.Pix[i+2]) * qf8 |
| 181 | a := float32(p.nrgba.Pix[i+3]) * qf8 |
| 182 | return pixel{r, g, b, a} |
| 183 | |
| 184 | case itNRGBA64: |
| 185 | i := p.nrgba64.PixOffset(x, y) |
| 186 | r := float32(uint16(p.nrgba64.Pix[i+0])<<8|uint16(p.nrgba64.Pix[i+1])) * qf16 |
| 187 | g := float32(uint16(p.nrgba64.Pix[i+2])<<8|uint16(p.nrgba64.Pix[i+3])) * qf16 |
| 188 | b := float32(uint16(p.nrgba64.Pix[i+4])<<8|uint16(p.nrgba64.Pix[i+5])) * qf16 |
| 189 | a := float32(uint16(p.nrgba64.Pix[i+6])<<8|uint16(p.nrgba64.Pix[i+7])) * qf16 |
| 190 | return pixel{r, g, b, a} |
| 191 | |
| 192 | case itRGBA: |
| 193 | i := p.rgba.PixOffset(x, y) |
| 194 | a8 := p.rgba.Pix[i+3] |
| 195 | switch a8 { |
| 196 | case 0xff: |
| 197 | r := float32(p.rgba.Pix[i+0]) * qf8 |
| 198 | g := float32(p.rgba.Pix[i+1]) * qf8 |
| 199 | b := float32(p.rgba.Pix[i+2]) * qf8 |
| 200 | return pixel{r, g, b, 1} |
| 201 | case 0: |
| 202 | return pixel{0, 0, 0, 0} |
| 203 | default: |
| 204 | q := float32(1) / float32(a8) |
| 205 | r := float32(p.rgba.Pix[i+0]) * q |
| 206 | g := float32(p.rgba.Pix[i+1]) * q |
| 207 | b := float32(p.rgba.Pix[i+2]) * q |
| 208 | a := float32(a8) * qf8 |
| 209 | return pixel{r, g, b, a} |
| 210 | } |
| 211 | |
| 212 | case itRGBA64: |
| 213 | i := p.rgba64.PixOffset(x, y) |
| 214 | a16 := uint16(p.rgba64.Pix[i+6])<<8 | uint16(p.rgba64.Pix[i+7]) |
| 215 | switch a16 { |
| 216 | case 0xffff: |
| 217 | r := float32(uint16(p.rgba64.Pix[i+0])<<8|uint16(p.rgba64.Pix[i+1])) * qf16 |
| 218 | g := float32(uint16(p.rgba64.Pix[i+2])<<8|uint16(p.rgba64.Pix[i+3])) * qf16 |
| 219 | b := float32(uint16(p.rgba64.Pix[i+4])<<8|uint16(p.rgba64.Pix[i+5])) * qf16 |
| 220 | return pixel{r, g, b, 1} |
| 221 | case 0: |
| 222 | return pixel{0, 0, 0, 0} |
| 223 | default: |
| 224 | q := float32(1) / float32(a16) |
| 225 | r := float32(uint16(p.rgba64.Pix[i+0])<<8|uint16(p.rgba64.Pix[i+1])) * q |
| 226 | g := float32(uint16(p.rgba64.Pix[i+2])<<8|uint16(p.rgba64.Pix[i+3])) * q |
| 227 | b := float32(uint16(p.rgba64.Pix[i+4])<<8|uint16(p.rgba64.Pix[i+5])) * q |
| 228 | a := float32(a16) * qf16 |
| 229 | return pixel{r, g, b, a} |
| 230 | } |
| 231 | |
| 232 | case itGray: |
| 233 | i := p.gray.PixOffset(x, y) |
| 234 | v := float32(p.gray.Pix[i]) * qf8 |
| 235 | return pixel{v, v, v, 1} |
| 236 | |
| 237 | case itGray16: |
| 238 | i := p.gray16.PixOffset(x, y) |
| 239 | v := float32(uint16(p.gray16.Pix[i+0])<<8|uint16(p.gray16.Pix[i+1])) * qf16 |
| 240 | return pixel{v, v, v, 1} |
| 241 | |
| 242 | case itYCbCr: |
| 243 | iy := (y-p.ycbcr.Rect.Min.Y)*p.ycbcr.YStride + (x - p.ycbcr.Rect.Min.X) |
| 244 | |
| 245 | var ic int |
| 246 | switch p.ycbcr.SubsampleRatio { |
| 247 | case image.YCbCrSubsampleRatio444: |
| 248 | ic = (y-p.ycbcr.Rect.Min.Y)*p.ycbcr.CStride + (x - p.ycbcr.Rect.Min.X) |
| 249 | case image.YCbCrSubsampleRatio422: |
| 250 | ic = (y-p.ycbcr.Rect.Min.Y)*p.ycbcr.CStride + (x/2 - p.ycbcr.Rect.Min.X/2) |
| 251 | case image.YCbCrSubsampleRatio420: |
| 252 | ic = (y/2-p.ycbcr.Rect.Min.Y/2)*p.ycbcr.CStride + (x/2 - p.ycbcr.Rect.Min.X/2) |
| 253 | case image.YCbCrSubsampleRatio440: |
| 254 | ic = (y/2-p.ycbcr.Rect.Min.Y/2)*p.ycbcr.CStride + (x - p.ycbcr.Rect.Min.X) |
| 255 | default: |
| 256 | ic = p.ycbcr.COffset(x, y) |
| 257 | } |
| 258 | |
| 259 | const ( |
| 260 | max = 255 * 1e5 |
| 261 | inv = 1.0 / max |
| 262 | ) |
| 263 | |
| 264 | y1 := int32(p.ycbcr.Y[iy]) * 1e5 |
| 265 | cb1 := int32(p.ycbcr.Cb[ic]) - 128 |
| 266 | cr1 := int32(p.ycbcr.Cr[ic]) - 128 |
| 267 | |
| 268 | r1 := y1 + 140200*cr1 |
| 269 | g1 := y1 - 34414*cb1 - 71414*cr1 |
| 270 | b1 := y1 + 177200*cb1 |
| 271 | |
| 272 | r := float32(clampi32(r1, 0, max)) * inv |
| 273 | g := float32(clampi32(g1, 0, max)) * inv |
| 274 | b := float32(clampi32(b1, 0, max)) * inv |
| 275 | |
| 276 | return pixel{r, g, b, 1} |
| 277 | |
| 278 | case itPaletted: |
| 279 | i := p.paletted.PixOffset(x, y) |
| 280 | k := p.paletted.Pix[i] |
| 281 | return p.palette[k] |
| 282 | } |
| 283 | |
| 284 | return pixelFromColor(p.image.At(x, y)) |
| 285 | } |
| 286 | |
| 287 | func (p *pixelGetter) getPixelRow(y int, buf *[]pixel) { |
| 288 | *buf = (*buf)[:0] |
| 289 | for x := p.bounds.Min.X; x != p.bounds.Max.X; x++ { |
| 290 | *buf = append(*buf, p.getPixel(x, y)) |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | func (p *pixelGetter) getPixelColumn(x int, buf *[]pixel) { |
| 295 | *buf = (*buf)[:0] |
| 296 | for y := p.bounds.Min.Y; y != p.bounds.Max.Y; y++ { |
| 297 | *buf = append(*buf, p.getPixel(x, y)) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | func f32u8(val float32) uint8 { |
| 302 | x := int64(val + 0.5) |
| 303 | if x > 0xff { |
| 304 | return 0xff |
| 305 | } |
| 306 | if x > 0 { |
| 307 | return uint8(x) |
| 308 | } |
| 309 | return 0 |
| 310 | } |
| 311 | |
| 312 | func f32u16(val float32) uint16 { |
| 313 | x := int64(val + 0.5) |
| 314 | if x > 0xffff { |
| 315 | return 0xffff |
| 316 | } |
| 317 | if x > 0 { |
| 318 | return uint16(x) |
| 319 | } |
| 320 | return 0 |
| 321 | } |
| 322 | |
| 323 | func clampi32(val, min, max int32) int32 { |
| 324 | if val > max { |
| 325 | return max |
| 326 | } |
| 327 | if val > min { |
| 328 | return val |
| 329 | } |
| 330 | return 0 |
| 331 | } |
| 332 | |
| 333 | type pixelSetter struct { |
| 334 | it imageType |
| 335 | bounds image.Rectangle |
| 336 | image draw.Image |
| 337 | nrgba *image.NRGBA |
| 338 | nrgba64 *image.NRGBA64 |
| 339 | rgba *image.RGBA |
| 340 | rgba64 *image.RGBA64 |
| 341 | gray *image.Gray |
| 342 | gray16 *image.Gray16 |
| 343 | paletted *image.Paletted |
| 344 | palette []pixel |
| 345 | } |
| 346 | |
| 347 | func newPixelSetter(img draw.Image) *pixelSetter { |
| 348 | switch img := img.(type) { |
| 349 | case *image.NRGBA: |
| 350 | return &pixelSetter{ |
| 351 | it: itNRGBA, |
| 352 | bounds: img.Bounds(), |
| 353 | nrgba: img, |
| 354 | } |
| 355 | |
| 356 | case *image.NRGBA64: |
| 357 | return &pixelSetter{ |
| 358 | it: itNRGBA64, |
| 359 | bounds: img.Bounds(), |
| 360 | nrgba64: img, |
| 361 | } |
| 362 | |
| 363 | case *image.RGBA: |
| 364 | return &pixelSetter{ |
| 365 | it: itRGBA, |
| 366 | bounds: img.Bounds(), |
| 367 | rgba: img, |
| 368 | } |
| 369 | |
| 370 | case *image.RGBA64: |
| 371 | return &pixelSetter{ |
| 372 | it: itRGBA64, |
| 373 | bounds: img.Bounds(), |
| 374 | rgba64: img, |
| 375 | } |
| 376 | |
| 377 | case *image.Gray: |
| 378 | return &pixelSetter{ |
| 379 | it: itGray, |
| 380 | bounds: img.Bounds(), |
| 381 | gray: img, |
| 382 | } |
| 383 | |
| 384 | case *image.Gray16: |
| 385 | return &pixelSetter{ |
| 386 | it: itGray16, |
| 387 | bounds: img.Bounds(), |
| 388 | gray16: img, |
| 389 | } |
| 390 | |
| 391 | case *image.Paletted: |
| 392 | return &pixelSetter{ |
| 393 | it: itPaletted, |
| 394 | bounds: img.Bounds(), |
| 395 | paletted: img, |
| 396 | palette: convertPalette(img.Palette), |
| 397 | } |
| 398 | |
| 399 | default: |
| 400 | return &pixelSetter{ |
| 401 | it: itGeneric, |
| 402 | bounds: img.Bounds(), |
| 403 | image: img, |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | func (p *pixelSetter) setPixel(x, y int, px pixel) { |
| 409 | if !image.Pt(x, y).In(p.bounds) { |
| 410 | return |
| 411 | } |
| 412 | switch p.it { |
| 413 | case itNRGBA: |
| 414 | i := p.nrgba.PixOffset(x, y) |
| 415 | p.nrgba.Pix[i+0] = f32u8(px.r * 0xff) |
| 416 | p.nrgba.Pix[i+1] = f32u8(px.g * 0xff) |
| 417 | p.nrgba.Pix[i+2] = f32u8(px.b * 0xff) |
| 418 | p.nrgba.Pix[i+3] = f32u8(px.a * 0xff) |
| 419 | |
| 420 | case itNRGBA64: |
| 421 | r16 := f32u16(px.r * 0xffff) |
| 422 | g16 := f32u16(px.g * 0xffff) |
| 423 | b16 := f32u16(px.b * 0xffff) |
| 424 | a16 := f32u16(px.a * 0xffff) |
| 425 | i := p.nrgba64.PixOffset(x, y) |
| 426 | p.nrgba64.Pix[i+0] = uint8(r16 >> 8) |
| 427 | p.nrgba64.Pix[i+1] = uint8(r16 & 0xff) |
| 428 | p.nrgba64.Pix[i+2] = uint8(g16 >> 8) |
| 429 | p.nrgba64.Pix[i+3] = uint8(g16 & 0xff) |
| 430 | p.nrgba64.Pix[i+4] = uint8(b16 >> 8) |
| 431 | p.nrgba64.Pix[i+5] = uint8(b16 & 0xff) |
| 432 | p.nrgba64.Pix[i+6] = uint8(a16 >> 8) |
| 433 | p.nrgba64.Pix[i+7] = uint8(a16 & 0xff) |
| 434 | |
| 435 | case itRGBA: |
| 436 | fa := px.a * 0xff |
| 437 | i := p.rgba.PixOffset(x, y) |
| 438 | p.rgba.Pix[i+0] = f32u8(px.r * fa) |
| 439 | p.rgba.Pix[i+1] = f32u8(px.g * fa) |
| 440 | p.rgba.Pix[i+2] = f32u8(px.b * fa) |
| 441 | p.rgba.Pix[i+3] = f32u8(fa) |
| 442 | |
| 443 | case itRGBA64: |
| 444 | fa := px.a * 0xffff |
| 445 | r16 := f32u16(px.r * fa) |
| 446 | g16 := f32u16(px.g * fa) |
| 447 | b16 := f32u16(px.b * fa) |
| 448 | a16 := f32u16(fa) |
| 449 | i := p.rgba64.PixOffset(x, y) |
| 450 | p.rgba64.Pix[i+0] = uint8(r16 >> 8) |
| 451 | p.rgba64.Pix[i+1] = uint8(r16 & 0xff) |
| 452 | p.rgba64.Pix[i+2] = uint8(g16 >> 8) |
| 453 | p.rgba64.Pix[i+3] = uint8(g16 & 0xff) |
| 454 | p.rgba64.Pix[i+4] = uint8(b16 >> 8) |
| 455 | p.rgba64.Pix[i+5] = uint8(b16 & 0xff) |
| 456 | p.rgba64.Pix[i+6] = uint8(a16 >> 8) |
| 457 | p.rgba64.Pix[i+7] = uint8(a16 & 0xff) |
| 458 | |
| 459 | case itGray: |
| 460 | i := p.gray.PixOffset(x, y) |
| 461 | p.gray.Pix[i] = f32u8((0.299*px.r + 0.587*px.g + 0.114*px.b) * px.a * 0xff) |
| 462 | |
| 463 | case itGray16: |
| 464 | i := p.gray16.PixOffset(x, y) |
| 465 | y16 := f32u16((0.299*px.r + 0.587*px.g + 0.114*px.b) * px.a * 0xffff) |
| 466 | p.gray16.Pix[i+0] = uint8(y16 >> 8) |
| 467 | p.gray16.Pix[i+1] = uint8(y16 & 0xff) |
| 468 | |
| 469 | case itPaletted: |
| 470 | px1 := pixel{ |
| 471 | minf32(maxf32(px.r, 0), 1), |
| 472 | minf32(maxf32(px.g, 0), 1), |
| 473 | minf32(maxf32(px.b, 0), 1), |
| 474 | minf32(maxf32(px.a, 0), 1), |
| 475 | } |
| 476 | i := p.paletted.PixOffset(x, y) |
| 477 | k := getPaletteIndex(p.palette, px1) |
| 478 | p.paletted.Pix[i] = uint8(k) |
| 479 | |
| 480 | case itGeneric: |
| 481 | r16 := f32u16(px.r * 0xffff) |
| 482 | g16 := f32u16(px.g * 0xffff) |
| 483 | b16 := f32u16(px.b * 0xffff) |
| 484 | a16 := f32u16(px.a * 0xffff) |
| 485 | p.image.Set(x, y, color.NRGBA64{r16, g16, b16, a16}) |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | func (p *pixelSetter) setPixelRow(y int, buf []pixel) { |
| 490 | for i, x := 0, p.bounds.Min.X; i < len(buf); i, x = i+1, x+1 { |
| 491 | p.setPixel(x, y, buf[i]) |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | func (p *pixelSetter) setPixelColumn(x int, buf []pixel) { |
| 496 | for i, y := 0, p.bounds.Min.Y; i < len(buf); i, y = i+1, y+1 { |
| 497 | p.setPixel(x, y, buf[i]) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | var pixelBufPool = &sync.Pool{ |
| 502 | New: func() any { |
| 503 | return &pixelSrcDst{ |
| 504 | src: make([]pixel, 0), |
| 505 | dst: make([]pixel, 0), |
| 506 | } |
| 507 | }, |
| 508 | } |
| 509 | |
| 510 | func getPixelBuf(srcLen, dstLen int) *pixelSrcDst { |
| 511 | v := pixelBufPool.Get().(*pixelSrcDst) |
| 512 | if srcLen > cap(v.src) { |
| 513 | v.src = make([]pixel, srcLen) |
| 514 | } |
| 515 | if dstLen > cap(v.dst) { |
| 516 | v.dst = make([]pixel, dstLen) |
| 517 | } |
| 518 | v.src = v.src[:srcLen] |
| 519 | v.dst = v.dst[:dstLen] |
| 520 | return v |
| 521 | } |
| 522 | |
| 523 | func putPixelBuf(buf *pixelSrcDst) { |
| 524 | buf.src = buf.src[:0] |
| 525 | buf.dst = buf.dst[:0] |
| 526 | pixelBufPool.Put(buf) |
| 527 | } |