all repos — honk @ c770ba04f6c1fd649fc5740f552e1bf61de9bb4e

my fork of honk

image.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

package main

import (
	"bytes"
	"fmt"
	"image"
	"image/jpeg"
	"image/png"
	"math"
)

func lineate(s uint8) float64 {
	x := float64(s)
	x /= 255.0
	if x < 0.04045 {
		x /= 12.92
	} else {
		x += 0.055
		x /= 1.055
		x = math.Pow(x, 2.4)
	}
	return x
}

func delineate(x float64) uint8 {
	if x > 0.0031308 {
		x = math.Pow(x, 1/2.4)
		x *= 1.055
		x -= 0.055
	} else {
		x *= 12.92
	}
	x *= 255.0
	return uint8(x)
}

func blend(d []byte, s1, s2, s3, s4 int) byte {
	l1 := lineate(d[s1])
	l2 := lineate(d[s2])
	l3 := lineate(d[s3])
	l4 := lineate(d[s4])
	return delineate((l1 + l2 + l3 + l4) / 4.0)
}

func squish(d []byte, s1, s2, s3, s4 int) byte {
	return uint8((uint32(d[s1]) + uint32(d[s2]) + uint32(d[s3]) + uint32(d[s4])) / 4)
}

func vacuumwrap(img image.Image, format string) ([]byte, string, error) {
	maxdimension := 2048
	for img.Bounds().Max.X > maxdimension || img.Bounds().Max.Y > maxdimension {
		switch oldimg := img.(type) {
		case *image.NRGBA:
			w, h := oldimg.Rect.Max.X/2, oldimg.Rect.Max.Y/2
			newimg := image.NewNRGBA(image.Rectangle{Max: image.Point{X: w, Y: h}})
			for j := 0; j < h; j++ {
				for i := 0; i < w; i++ {
					p := newimg.Stride*j + i*4
					q1 := oldimg.Stride*(j*2+0) + i*4*2
					q2 := oldimg.Stride*(j*2+1) + i*4*2
					newimg.Pix[p+0] = blend(oldimg.Pix, q1+0, q1+4, q2+0, q2+4)
					newimg.Pix[p+1] = blend(oldimg.Pix, q1+1, q1+5, q2+1, q2+5)
					newimg.Pix[p+2] = blend(oldimg.Pix, q1+2, q1+6, q2+2, q2+6)
					newimg.Pix[p+3] = squish(oldimg.Pix, q1+3, q1+7, q2+3, q2+7)
				}
			}
			img = newimg
		case *image.YCbCr:
			w, h := oldimg.Rect.Max.X/2, oldimg.Rect.Max.Y/2
			newimg := image.NewYCbCr(image.Rectangle{Max: image.Point{X: w, Y: h}},
				oldimg.SubsampleRatio)
			for j := 0; j < h; j++ {
				for i := 0; i < w; i++ {
					p := newimg.YStride*j + i
					q1 := oldimg.YStride*(j*2+0) + i*2
					q2 := oldimg.YStride*(j*2+1) + i*2
					newimg.Y[p+0] = blend(oldimg.Y, q1+0, q1+1, q2+0, q2+1)
				}
			}
			switch newimg.SubsampleRatio {
			case image.YCbCrSubsampleRatio444:
				w, h = w, h
			case image.YCbCrSubsampleRatio422:
				w, h = w/2, h
			case image.YCbCrSubsampleRatio420:
				w, h = w/2, h/2
			case image.YCbCrSubsampleRatio440:
				w, h = w, h/2
			case image.YCbCrSubsampleRatio411:
				w, h = w/4, h
			case image.YCbCrSubsampleRatio410:
				w, h = w/4, h/2
			}
			for j := 0; j < h; j++ {
				for i := 0; i < w; i++ {
					p := newimg.CStride*j + i
					q1 := oldimg.CStride*(j*2+0) + i*2
					q2 := oldimg.CStride*(j*2+1) + i*2
					newimg.Cb[p+0] = blend(oldimg.Cb, q1+0, q1+1, q2+0, q2+1)
					newimg.Cr[p+0] = blend(oldimg.Cr, q1+0, q1+1, q2+0, q2+1)
				}
			}
			img = newimg
		default:
			return nil, "", fmt.Errorf("can't support image format")
		}
	}
	maxsize := 512 * 1024
	quality := 80
	var buf bytes.Buffer
	for {
		switch format {
		case "png":
			png.Encode(&buf, img)
		case "jpeg":
			jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
		default:
			return nil, "", fmt.Errorf("can't encode format: %s", format)
		}
		if buf.Len() > maxsize && quality > 30 {
			switch format {
			case "png":
				format = "jpeg"
			case "jpeg":
				quality -= 10
			}
			buf.Reset()
			continue
		}
		break
	}
	return buf.Bytes(), format, nil
}