2012-09-21 20:02:25 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or 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 resize
|
|
|
|
|
2014-07-30 08:08:58 +02:00
|
|
|
import "image"
|
2012-09-21 20:02:25 +02:00
|
|
|
|
2014-07-19 13:19:31 +02:00
|
|
|
// Keep value in [0,255] range.
|
|
|
|
func clampUint8(in int32) uint8 {
|
2015-03-18 20:19:43 +01:00
|
|
|
// casting a negative int to an uint will result in an overflown
|
|
|
|
// large uint. this behavior will be exploited here and in other functions
|
|
|
|
// to achieve a higher performance.
|
2015-03-15 22:44:39 +01:00
|
|
|
if uint32(in) < 256 {
|
|
|
|
return uint8(in)
|
2012-12-10 19:27:21 +01:00
|
|
|
}
|
2014-07-19 13:19:31 +02:00
|
|
|
if in > 255 {
|
|
|
|
return 255
|
|
|
|
}
|
2015-03-15 22:44:39 +01:00
|
|
|
return 0
|
2012-12-10 19:27:21 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 13:19:31 +02:00
|
|
|
// Keep value in [0,65535] range.
|
|
|
|
func clampUint16(in int64) uint16 {
|
2015-03-15 22:44:39 +01:00
|
|
|
if uint64(in) < 65536 {
|
|
|
|
return uint16(in)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
if in > 65535 {
|
|
|
|
return 65535
|
|
|
|
}
|
2015-03-15 22:44:39 +01:00
|
|
|
return 0
|
2012-12-10 19:27:21 +01:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:25:38 +01:00
|
|
|
func resizeGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
|
2014-07-19 13:19:31 +02:00
|
|
|
newBounds := out.Bounds()
|
2014-08-20 20:54:59 +02:00
|
|
|
maxX := in.Bounds().Dx() - 1
|
2012-12-11 19:57:34 +01:00
|
|
|
|
2014-07-19 13:19:31 +02:00
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var rgba [4]int64
|
|
|
|
var sum int64
|
2014-08-20 21:12:39 +02:00
|
|
|
start := offset[y]
|
2014-09-15 11:50:59 +02:00
|
|
|
ci := y * filterLength
|
2014-07-19 13:19:31 +02:00
|
|
|
for i := 0; i < filterLength; i++ {
|
2014-07-29 22:53:35 +02:00
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
2014-08-20 20:54:59 +02:00
|
|
|
case xi < 0:
|
|
|
|
xi = 0
|
|
|
|
case xi >= maxX:
|
|
|
|
xi = maxX
|
2014-07-29 22:53:35 +02:00
|
|
|
}
|
2016-01-07 21:25:38 +01:00
|
|
|
|
2014-08-20 20:54:59 +02:00
|
|
|
r, g, b, a := in.At(xi+in.Bounds().Min.X, x+in.Bounds().Min.Y).RGBA()
|
2015-05-12 15:19:11 +02:00
|
|
|
|
2014-07-29 22:53:35 +02:00
|
|
|
rgba[0] += int64(coeff) * int64(r)
|
|
|
|
rgba[1] += int64(coeff) * int64(g)
|
|
|
|
rgba[2] += int64(coeff) * int64(b)
|
|
|
|
rgba[3] += int64(coeff) * int64(a)
|
|
|
|
sum += int64(coeff)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
|
2014-07-19 13:19:31 +02:00
|
|
|
offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
2016-01-07 21:25:38 +01:00
|
|
|
|
|
|
|
value := clampUint16(rgba[0] / sum)
|
2014-07-19 13:19:31 +02:00
|
|
|
out.Pix[offset+0] = uint8(value >> 8)
|
|
|
|
out.Pix[offset+1] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[1] / sum)
|
2014-07-19 13:19:31 +02:00
|
|
|
out.Pix[offset+2] = uint8(value >> 8)
|
|
|
|
out.Pix[offset+3] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[2] / sum)
|
2014-07-19 13:19:31 +02:00
|
|
|
out.Pix[offset+4] = uint8(value >> 8)
|
|
|
|
out.Pix[offset+5] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[3] / sum)
|
2014-07-19 13:19:31 +02:00
|
|
|
out.Pix[offset+6] = uint8(value >> 8)
|
|
|
|
out.Pix[offset+7] = uint8(value)
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:25:38 +01:00
|
|
|
func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) {
|
2014-07-19 13:19:31 +02:00
|
|
|
newBounds := out.Bounds()
|
2014-08-20 20:54:59 +02:00
|
|
|
maxX := in.Bounds().Dx() - 1
|
2012-09-21 20:02:25 +02:00
|
|
|
|
2014-07-19 13:19:31 +02:00
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
2014-08-20 21:12:39 +02:00
|
|
|
row := in.Pix[x*in.Stride:]
|
2014-07-19 13:19:31 +02:00
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var rgba [4]int32
|
|
|
|
var sum int32
|
2014-08-20 21:12:39 +02:00
|
|
|
start := offset[y]
|
2014-09-15 11:50:59 +02:00
|
|
|
ci := y * filterLength
|
2014-07-19 13:19:31 +02:00
|
|
|
for i := 0; i < filterLength; i++ {
|
2014-07-29 22:53:35 +02:00
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
2015-03-15 22:44:39 +01:00
|
|
|
case uint(xi) < uint(maxX):
|
|
|
|
xi *= 4
|
2014-08-20 20:54:59 +02:00
|
|
|
case xi >= maxX:
|
|
|
|
xi = 4 * maxX
|
2014-07-29 22:53:35 +02:00
|
|
|
default:
|
2015-03-15 22:44:39 +01:00
|
|
|
xi = 0
|
2014-07-29 22:53:35 +02:00
|
|
|
}
|
2015-05-12 15:19:11 +02:00
|
|
|
|
2016-01-07 16:39:12 +01:00
|
|
|
rgba[0] += int32(coeff) * int32(row[xi+0])
|
|
|
|
rgba[1] += int32(coeff) * int32(row[xi+1])
|
|
|
|
rgba[2] += int32(coeff) * int32(row[xi+2])
|
|
|
|
rgba[3] += int32(coeff) * int32(row[xi+3])
|
2014-07-29 22:53:35 +02:00
|
|
|
sum += int32(coeff)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
|
2014-07-29 22:53:35 +02:00
|
|
|
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
|
2016-01-07 17:38:35 +01:00
|
|
|
|
2016-01-07 21:25:38 +01:00
|
|
|
out.Pix[xo+0] = clampUint8(rgba[0] / sum)
|
|
|
|
out.Pix[xo+1] = clampUint8(rgba[1] / sum)
|
|
|
|
out.Pix[xo+2] = clampUint8(rgba[2] / sum)
|
|
|
|
out.Pix[xo+3] = clampUint8(rgba[3] / sum)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
}
|
|
|
|
|
2016-01-07 21:25:38 +01:00
|
|
|
func resizeNRGBA(in *image.NRGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) {
|
2015-03-25 18:38:23 +01:00
|
|
|
newBounds := out.Bounds()
|
|
|
|
maxX := in.Bounds().Dx() - 1
|
|
|
|
|
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
|
|
|
row := in.Pix[x*in.Stride:]
|
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var rgba [4]int32
|
|
|
|
var sum int32
|
|
|
|
start := offset[y]
|
|
|
|
ci := y * filterLength
|
|
|
|
for i := 0; i < filterLength; i++ {
|
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
|
|
|
case uint(xi) < uint(maxX):
|
|
|
|
xi *= 4
|
|
|
|
case xi >= maxX:
|
|
|
|
xi = 4 * maxX
|
|
|
|
default:
|
|
|
|
xi = 0
|
|
|
|
}
|
2016-01-07 16:39:12 +01:00
|
|
|
|
|
|
|
// Forward alpha-premultiplication
|
|
|
|
a := int32(row[xi+3])
|
2016-07-24 22:39:20 +02:00
|
|
|
r := int32(row[xi+0]) * a
|
|
|
|
r /= 0xff
|
|
|
|
g := int32(row[xi+1]) * a
|
|
|
|
g /= 0xff
|
|
|
|
b := int32(row[xi+2]) * a
|
|
|
|
b /= 0xff
|
|
|
|
|
|
|
|
rgba[0] += int32(coeff) * r
|
|
|
|
rgba[1] += int32(coeff) * g
|
|
|
|
rgba[2] += int32(coeff) * b
|
|
|
|
rgba[3] += int32(coeff) * a
|
|
|
|
sum += int32(coeff)
|
2015-03-25 18:38:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
|
2016-01-07 17:38:35 +01:00
|
|
|
|
2016-01-07 21:25:38 +01:00
|
|
|
out.Pix[xo+0] = clampUint8(rgba[0] / sum)
|
|
|
|
out.Pix[xo+1] = clampUint8(rgba[1] / sum)
|
|
|
|
out.Pix[xo+2] = clampUint8(rgba[2] / sum)
|
|
|
|
out.Pix[xo+3] = clampUint8(rgba[3] / sum)
|
2015-03-25 18:38:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:25:38 +01:00
|
|
|
func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
|
2015-03-25 18:38:23 +01:00
|
|
|
newBounds := out.Bounds()
|
|
|
|
maxX := in.Bounds().Dx() - 1
|
|
|
|
|
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
|
|
|
row := in.Pix[x*in.Stride:]
|
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var rgba [4]int64
|
|
|
|
var sum int64
|
|
|
|
start := offset[y]
|
|
|
|
ci := y * filterLength
|
|
|
|
for i := 0; i < filterLength; i++ {
|
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
|
|
|
case uint(xi) < uint(maxX):
|
|
|
|
xi *= 8
|
|
|
|
case xi >= maxX:
|
|
|
|
xi = 8 * maxX
|
|
|
|
default:
|
|
|
|
xi = 0
|
|
|
|
}
|
2015-05-12 15:19:11 +02:00
|
|
|
|
2016-01-07 16:39:12 +01:00
|
|
|
rgba[0] += int64(coeff) * (int64(row[xi+0])<<8 | int64(row[xi+1]))
|
|
|
|
rgba[1] += int64(coeff) * (int64(row[xi+2])<<8 | int64(row[xi+3]))
|
|
|
|
rgba[2] += int64(coeff) * (int64(row[xi+4])<<8 | int64(row[xi+5]))
|
|
|
|
rgba[3] += int64(coeff) * (int64(row[xi+6])<<8 | int64(row[xi+7]))
|
2015-03-25 18:38:23 +01:00
|
|
|
sum += int64(coeff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
2016-01-07 21:25:38 +01:00
|
|
|
|
|
|
|
value := clampUint16(rgba[0] / sum)
|
2015-03-25 18:38:23 +01:00
|
|
|
out.Pix[xo+0] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+1] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[1] / sum)
|
2015-03-25 18:38:23 +01:00
|
|
|
out.Pix[xo+2] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+3] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[2] / sum)
|
2015-03-25 18:38:23 +01:00
|
|
|
out.Pix[xo+4] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+5] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[3] / sum)
|
2015-03-25 18:38:23 +01:00
|
|
|
out.Pix[xo+6] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+7] = uint8(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 21:25:38 +01:00
|
|
|
func resizeNRGBA64(in *image.NRGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
|
2014-07-19 13:19:31 +02:00
|
|
|
newBounds := out.Bounds()
|
2014-08-20 20:54:59 +02:00
|
|
|
maxX := in.Bounds().Dx() - 1
|
2014-07-19 13:19:31 +02:00
|
|
|
|
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
2014-08-20 21:12:39 +02:00
|
|
|
row := in.Pix[x*in.Stride:]
|
2014-07-19 13:19:31 +02:00
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var rgba [4]int64
|
|
|
|
var sum int64
|
2014-08-20 21:12:39 +02:00
|
|
|
start := offset[y]
|
2014-09-15 11:50:59 +02:00
|
|
|
ci := y * filterLength
|
2014-07-19 13:19:31 +02:00
|
|
|
for i := 0; i < filterLength; i++ {
|
2014-07-29 22:53:35 +02:00
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
2015-03-15 22:44:39 +01:00
|
|
|
case uint(xi) < uint(maxX):
|
|
|
|
xi *= 8
|
2014-08-20 20:54:59 +02:00
|
|
|
case xi >= maxX:
|
|
|
|
xi = 8 * maxX
|
2014-07-29 22:53:35 +02:00
|
|
|
default:
|
2015-03-15 22:44:39 +01:00
|
|
|
xi = 0
|
2014-07-29 22:53:35 +02:00
|
|
|
}
|
2016-01-07 17:38:35 +01:00
|
|
|
|
2016-01-09 12:18:50 +01:00
|
|
|
// Forward alpha-premultiplication
|
|
|
|
a := int64(uint16(row[xi+6])<<8 | uint16(row[xi+7]))
|
2016-07-24 22:39:20 +02:00
|
|
|
r := int64(uint16(row[xi+0])<<8|uint16(row[xi+1])) * a
|
|
|
|
r /= 0xffff
|
|
|
|
g := int64(uint16(row[xi+2])<<8|uint16(row[xi+3])) * a
|
|
|
|
g /= 0xffff
|
|
|
|
b := int64(uint16(row[xi+4])<<8|uint16(row[xi+5])) * a
|
|
|
|
b /= 0xffff
|
|
|
|
|
|
|
|
rgba[0] += int64(coeff) * r
|
|
|
|
rgba[1] += int64(coeff) * g
|
|
|
|
rgba[2] += int64(coeff) * b
|
|
|
|
rgba[3] += int64(coeff) * a
|
|
|
|
sum += int64(coeff)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 22:53:35 +02:00
|
|
|
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
|
2016-01-07 21:25:38 +01:00
|
|
|
|
|
|
|
value := clampUint16(rgba[0] / sum)
|
2014-07-29 22:53:35 +02:00
|
|
|
out.Pix[xo+0] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+1] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[1] / sum)
|
2014-07-29 22:53:35 +02:00
|
|
|
out.Pix[xo+2] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+3] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[2] / sum)
|
2014-07-29 22:53:35 +02:00
|
|
|
out.Pix[xo+4] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+5] = uint8(value)
|
2016-01-07 21:25:38 +01:00
|
|
|
value = clampUint16(rgba[3] / sum)
|
2014-07-29 22:53:35 +02:00
|
|
|
out.Pix[xo+6] = uint8(value >> 8)
|
|
|
|
out.Pix[xo+7] = uint8(value)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 22:53:35 +02:00
|
|
|
func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, offset []int, filterLength int) {
|
2014-07-19 13:19:31 +02:00
|
|
|
newBounds := out.Bounds()
|
2014-08-20 20:54:59 +02:00
|
|
|
maxX := in.Bounds().Dx() - 1
|
2014-07-19 13:19:31 +02:00
|
|
|
|
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
2014-08-20 20:54:59 +02:00
|
|
|
row := in.Pix[(x-newBounds.Min.X)*in.Stride:]
|
2014-07-19 13:19:31 +02:00
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var gray int32
|
|
|
|
var sum int32
|
2014-08-20 21:12:39 +02:00
|
|
|
start := offset[y]
|
2014-09-15 11:50:59 +02:00
|
|
|
ci := y * filterLength
|
2014-07-19 13:19:31 +02:00
|
|
|
for i := 0; i < filterLength; i++ {
|
2014-07-29 22:53:35 +02:00
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
2014-08-20 20:54:59 +02:00
|
|
|
case xi < 0:
|
|
|
|
xi = 0
|
|
|
|
case xi >= maxX:
|
2014-07-29 22:53:35 +02:00
|
|
|
xi = maxX
|
|
|
|
}
|
|
|
|
gray += int32(coeff) * int32(row[xi])
|
|
|
|
sum += int32(coeff)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := (y-newBounds.Min.Y)*out.Stride + (x - newBounds.Min.X)
|
|
|
|
out.Pix[offset] = clampUint8(gray / sum)
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 22:53:35 +02:00
|
|
|
func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []int32, offset []int, filterLength int) {
|
2014-07-19 13:19:31 +02:00
|
|
|
newBounds := out.Bounds()
|
2014-08-20 20:54:59 +02:00
|
|
|
maxX := in.Bounds().Dx() - 1
|
2014-07-19 13:19:31 +02:00
|
|
|
|
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
2014-08-20 21:12:39 +02:00
|
|
|
row := in.Pix[x*in.Stride:]
|
2014-07-19 13:19:31 +02:00
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var gray int64
|
|
|
|
var sum int64
|
2014-08-20 21:12:39 +02:00
|
|
|
start := offset[y]
|
2014-09-15 11:50:59 +02:00
|
|
|
ci := y * filterLength
|
2014-07-19 13:19:31 +02:00
|
|
|
for i := 0; i < filterLength; i++ {
|
2014-07-29 22:53:35 +02:00
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
2015-03-15 22:44:39 +01:00
|
|
|
case uint(xi) < uint(maxX):
|
|
|
|
xi *= 2
|
2014-08-20 20:54:59 +02:00
|
|
|
case xi >= maxX:
|
|
|
|
xi = 2 * maxX
|
2014-07-29 22:53:35 +02:00
|
|
|
default:
|
2015-03-15 22:44:39 +01:00
|
|
|
xi = 0
|
2014-07-29 22:53:35 +02:00
|
|
|
}
|
|
|
|
gray += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
|
|
|
|
sum += int64(coeff)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*2
|
|
|
|
value := clampUint16(gray / sum)
|
|
|
|
out.Pix[offset+0] = uint8(value >> 8)
|
|
|
|
out.Pix[offset+1] = uint8(value)
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
}
|
|
|
|
|
2014-07-30 08:08:58 +02:00
|
|
|
func resizeYCbCr(in *ycc, out *ycc, scale float64, coeffs []int16, offset []int, filterLength int) {
|
|
|
|
newBounds := out.Bounds()
|
2014-08-20 20:54:59 +02:00
|
|
|
maxX := in.Bounds().Dx() - 1
|
2014-07-30 08:08:58 +02:00
|
|
|
|
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
2014-08-20 21:12:39 +02:00
|
|
|
row := in.Pix[x*in.Stride:]
|
2014-07-30 08:08:58 +02:00
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var p [3]int32
|
|
|
|
var sum int32
|
2014-08-20 21:12:39 +02:00
|
|
|
start := offset[y]
|
2014-09-15 11:50:59 +02:00
|
|
|
ci := y * filterLength
|
2014-07-30 08:08:58 +02:00
|
|
|
for i := 0; i < filterLength; i++ {
|
|
|
|
coeff := coeffs[ci+i]
|
|
|
|
if coeff != 0 {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
2015-03-15 22:44:39 +01:00
|
|
|
case uint(xi) < uint(maxX):
|
|
|
|
xi *= 3
|
2014-08-20 20:54:59 +02:00
|
|
|
case xi >= maxX:
|
|
|
|
xi = 3 * maxX
|
2014-07-30 08:08:58 +02:00
|
|
|
default:
|
2015-03-15 22:44:39 +01:00
|
|
|
xi = 0
|
2014-07-30 08:08:58 +02:00
|
|
|
}
|
|
|
|
p[0] += int32(coeff) * int32(row[xi+0])
|
|
|
|
p[1] += int32(coeff) * int32(row[xi+1])
|
|
|
|
p[2] += int32(coeff) * int32(row[xi+2])
|
|
|
|
sum += int32(coeff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
|
|
|
|
out.Pix[xo+0] = clampUint8(p[0] / sum)
|
|
|
|
out.Pix[xo+1] = clampUint8(p[1] / sum)
|
|
|
|
out.Pix[xo+2] = clampUint8(p[2] / sum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nearestYCbCr(in *ycc, out *ycc, scale float64, coeffs []bool, offset []int, filterLength int) {
|
|
|
|
newBounds := out.Bounds()
|
2014-08-20 20:54:59 +02:00
|
|
|
maxX := in.Bounds().Dx() - 1
|
2014-07-30 08:08:58 +02:00
|
|
|
|
|
|
|
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
|
2014-08-20 21:12:39 +02:00
|
|
|
row := in.Pix[x*in.Stride:]
|
2014-07-30 08:08:58 +02:00
|
|
|
for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
|
|
|
|
var p [3]float32
|
|
|
|
var sum float32
|
2014-08-20 21:12:39 +02:00
|
|
|
start := offset[y]
|
2014-09-15 11:50:59 +02:00
|
|
|
ci := y * filterLength
|
2014-07-30 08:08:58 +02:00
|
|
|
for i := 0; i < filterLength; i++ {
|
|
|
|
if coeffs[ci+i] {
|
|
|
|
xi := start + i
|
|
|
|
switch {
|
2015-03-15 22:44:39 +01:00
|
|
|
case uint(xi) < uint(maxX):
|
|
|
|
xi *= 3
|
2014-08-20 20:54:59 +02:00
|
|
|
case xi >= maxX:
|
|
|
|
xi = 3 * maxX
|
2014-07-30 08:08:58 +02:00
|
|
|
default:
|
2015-03-15 22:44:39 +01:00
|
|
|
xi = 0
|
2014-07-30 08:08:58 +02:00
|
|
|
}
|
|
|
|
p[0] += float32(row[xi+0])
|
|
|
|
p[1] += float32(row[xi+1])
|
|
|
|
p[2] += float32(row[xi+2])
|
|
|
|
sum++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
|
|
|
|
out.Pix[xo+0] = floatToUint8(p[0] / sum)
|
|
|
|
out.Pix[xo+1] = floatToUint8(p[1] / sum)
|
|
|
|
out.Pix[xo+2] = floatToUint8(p[2] / sum)
|
2014-07-19 13:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:02:25 +02:00
|
|
|
}
|