From 5cd3cb8dcf854fffb3d2b05857a42650177f5621 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sun, 4 Aug 2024 18:24:04 -0700 Subject: [PATCH] staticcheck fixes --- hashcompute.go | 1 + hashcompute_test.go | 1 - imagehash.go | 16 ++--- transforms/dct.go | 1 + transforms/dct_test.go | 23 ++++--- transforms/static.go | 8 ++- yuv.color.convert.go | 19 ++++-- yuv.fancy.upscaling.go | 140 ++++++++++++++++++++--------------------- 8 files changed, 115 insertions(+), 94 deletions(-) diff --git a/hashcompute.go b/hashcompute.go index 4e09e10..13c8556 100644 --- a/hashcompute.go +++ b/hashcompute.go @@ -40,6 +40,7 @@ func getBuf(size, capacity int) []uint8 { } return buf } + func ToGray(img image.Image, pix []uint8) *image.Gray { c := img.Bounds().Dx() * img.Bounds().Dy() if cap(pix) < c { diff --git a/hashcompute_test.go b/hashcompute_test.go index 5ae2dec..8c8c1cc 100644 --- a/hashcompute_test.go +++ b/hashcompute_test.go @@ -49,7 +49,6 @@ func TestHashCompute(t *testing.T) { } { file1, err := os.Open(tt.img1) if err != nil { - } defer file1.Close() diff --git a/imagehash.go b/imagehash.go index 4f8e73f..af46b17 100644 --- a/imagehash.go +++ b/imagehash.go @@ -36,7 +36,7 @@ const ( Unknown Kind = iota // AHash is a enum value of the average hash. AHash - //PHash is a enum value of the perceptual hash. + // PHash is a enum value of the perceptual hash. PHash // DHash is a enum value of the difference hash. DHash @@ -60,7 +60,7 @@ func (h *ImageHash) Distance(other *ImageHash) (int, error) { return -1, errNoOther } if h.GetKind() != other.GetKind() { - return -1, errors.New("Image hashes's kind should be identical") + return -1, errors.New("image hashes's kind should be identical") } lhash := h.GetHash() @@ -84,8 +84,10 @@ func (h *ImageHash) leftShiftSet(idx int) { h.hash |= 1 << uint(idx) } -const strFmtHex = "%1s:%016x" -const strFmtBin = "%1s:%064b" +const ( + strFmtHex = "%1s:%016x" + strFmtBin = "%1s:%064b" +) // Dump method writes a binary serialization into w io.Writer. func (h *ImageHash) Dump(w io.Writer) error { @@ -164,18 +166,18 @@ func (h *ExtImageHash) Bits() int { // Distance method returns a distance between two big hashes func (h *ExtImageHash) Distance(other *ExtImageHash) (int, error) { if h.GetKind() != other.GetKind() { - return -1, errors.New("Extended Image hashes's kind should be identical") + return -1, errors.New("extended Image hashes's kind should be identical") } if h.Bits() != other.Bits() { - msg := fmt.Sprintf("Extended image hash should has an identical bit size but got %v vs %v", h.Bits(), other.Bits()) + msg := fmt.Sprintf("extended image hash should has an identical bit size but got %v vs %v", h.Bits(), other.Bits()) return -1, errors.New(msg) } lHash := h.GetHash() rHash := other.GetHash() if len(lHash) != len(rHash) { - return -1, errors.New("Extended Image hashes's size should be identical") + return -1, errors.New("extended Image hashes's size should be identical") } distance := 0 diff --git a/transforms/dct.go b/transforms/dct.go index 5969d68..0653174 100644 --- a/transforms/dct.go +++ b/transforms/dct.go @@ -97,6 +97,7 @@ func DCT2DFast64(input *[]float64) (flattens [64]float64) { } return } + func DCT2DFast32(input *[]float64) (flattens [64]float64) { if len(*input) != 32*32 { panic("incorrect input size, wanted 32x32.") diff --git a/transforms/dct_test.go b/transforms/dct_test.go index 3e15024..abc105b 100644 --- a/transforms/dct_test.go +++ b/transforms/dct_test.go @@ -43,18 +43,23 @@ func TestDCT2D(t *testing.T) { for _, tt := range []struct { input [][]float64 output [][]float64 - w int - h int + w, h int }{ - {[][]float64{{1.0, 2.0, 3.0, 4.0}, - {5.0, 6.0, 7.0, 8.0}, - {9.0, 10.0, 11.0, 12.0}, - {13.0, 14.0, 15.0, 16.0}}, - [][]float64{{136.0, -12.6172881195958, 0.0, -0.8966830583359305}, + { + [][]float64{ + {1.0, 2.0, 3.0, 4.0}, + {5.0, 6.0, 7.0, 8.0}, + {9.0, 10.0, 11.0, 12.0}, + {13.0, 14.0, 15.0, 16.0}, + }, + [][]float64{ + {136.0, -12.6172881195958, 0.0, -0.8966830583359305}, {-50.4691524783832, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, - {-3.586732233343722, 0.0, 0.0, 0.0}}, - 4, 4}, + {-3.586732233343722, 0.0, 0.0, 0.0}, + }, + 4, 4, + }, } { out := DCT2D(tt.input, tt.w, tt.h) pass := true diff --git a/transforms/static.go b/transforms/static.go index 025dbac..7609e9a 100644 --- a/transforms/static.go +++ b/transforms/static.go @@ -86,7 +86,7 @@ func forwardDCT16(input []float64) { } func forwardDCT8(input []float64) { - var a, b = [4]float64{}, [4]float64{} + a, b := [4]float64{}, [4]float64{} x0, y0 := input[0], input[7] x1, y1 := input[1], input[6] @@ -159,7 +159,8 @@ var ( 0.7540148204328366, 0.7312259956095479, 0.708327050840981, 0.6853214346239888, 0.6622126115197529, 0.6390040616320315, 0.61569928008307, 0.5923017764872479, 0.5688150744225436, 0.545242710899898, 0.5215882358305511, 0.4978552114914405, 0.4740472119887347, 0.45016782271958555, 0.4262206398321827, 0.40220926968418386, 0.37813732829961255, 0.3540084408242977, 0.3298262409799401, 0.3055943705168868, 0.2813164786656985, 0.25699622158758645, 0.23263726182380973, 0.20824326774410945, - 0.1838179129942654, 0.15936487594286025, 0.1348878391273282, 0.11039048869938006, 0.08587651386988192, 0.06134960635327317, 0.03681345981160964, 0.012271769298309032} + 0.1838179129942654, 0.15936487594286025, 0.1348878391273282, 0.11039048869938006, 0.08587651386988192, 0.06134960635327317, 0.03681345981160964, 0.012271769298309032, + } dct128 = [64]float64{ 1.9998494036782892, 1.9986447691766989, 1.9962362258002984, 1.992625224365556, 1.9878139400047121, 1.98180527085556, 1.9746028363157169, 1.9662109748624328, 1.9566347414392553, 1.9458799044111204, 1.9339529420897041, 1.9208610388311316, 1.9066120807083875, 1.8912146507610426, 1.87467802382515, 1.8570121609464312, @@ -168,7 +169,8 @@ var ( 1.3967524988179458, 1.3612019955909063, 1.3248315551803436, 1.287663085779583, 1.249718976284773, 1.211022082808651, 1.1715957149128777, 1.1314636215672265, 1.0906499768440931, 1.049179365356938, 1.0070767674514352, 0.9643675441582458, 0.92107742191648, 0.8772324770770554, 0.8328591201952746, 0.7879840801220962, 0.7426343879036752, 0.696837360498869, 0.650620584324526, 0.6040118986384564, 0.5570393787701061, 0.5097313192090293, 0.4621162165613425, 0.4142227523844371, - 0.36607977591028207, 0.3177162866677228, 0.26916141701425245, 0.22044441458776637, 0.17159462468887976, 0.12264147260441731, 0.07361444588271798, 0.02454307657143989} + 0.36607977591028207, 0.3177162866677228, 0.26916141701425245, 0.22044441458776637, 0.17159462468887976, 0.12264147260441731, 0.07361444588271798, 0.02454307657143989, + } dct64 = [32]float64{ 1.9993976373924083, 1.9945809133573804, 1.9849590691974202, 1.9705552847778824, 1.9514042600770571, 1.9275521315908797, 1.8990563611860733, 1.8659855976694777, 1.8284195114070614, 1.7864486023910306, 1.7401739822174227, 1.6897071304994142, 1.6351696263031674, 1.5766928552532127, 1.5144176930129691, 1.448494165902934, diff --git a/yuv.color.convert.go b/yuv.color.convert.go index 6fa4d3b..cd22208 100644 --- a/yuv.color.convert.go +++ b/yuv.color.convert.go @@ -16,6 +16,7 @@ const ( func MultHi(v int32, coeff int32) int32 { return (v * coeff) >> 8 } + func VP8Clip8(v int32) uint8 { if (v &^ YUV_MASK2) == 0 { return uint8(v >> YUV_FIX2) @@ -25,15 +26,19 @@ func VP8Clip8(v int32) uint8 { } return 0xff } + func VP8YUVToR(y uint8, v uint8) uint8 { return VP8Clip8(MultHi(int32(y), 19077) + MultHi(int32(v), 26149) - 14234) } + func VP8YUVToG(y uint8, u uint8, v uint8) uint8 { return VP8Clip8(MultHi(int32(y), 19077) - MultHi(int32(u), 6419) - MultHi(int32(v), 13320) + 8708) } + func VP8YUVToB(y uint8, u uint8) uint8 { return VP8Clip8(MultHi(int32(y), 19077) + MultHi(int32(u), 33050) - 17685) } + func VP8YuvToRgb(y uint8, u uint8, v uint8) (rgb [3]uint8) { rgb[0] = uint8(VP8YUVToR(y, v)) rgb[1] = uint8(VP8YUVToG(y, u, v)) @@ -41,6 +46,7 @@ func VP8YuvToRgb(y uint8, u uint8, v uint8) (rgb [3]uint8) { return rgb } + func VP8YuvToBgr(y uint8, u uint8, v uint8) (bgr [3]uint8) { bgr[0] = uint8(VP8YUVToB(y, u)) bgr[1] = uint8(VP8YUVToG(y, u, v)) @@ -48,6 +54,7 @@ func VP8YuvToBgr(y uint8, u uint8, v uint8) (bgr [3]uint8) { return bgr } + func VP8YuvToRgb565(y uint8, u uint8, v uint8) (rgb [2]uint8) { var ( r = VP8YUVToR(y, v) @@ -55,13 +62,14 @@ func VP8YuvToRgb565(y uint8, u uint8, v uint8) (rgb [2]uint8) { b = VP8YUVToB(y, u) ) - var rg = (r & 0xf8) | (g >> 5) - var gb = ((g << 3) & 0xe0) | (b >> 3) + rg := (r & 0xf8) | (g >> 5) + gb := ((g << 3) & 0xe0) | (b >> 3) rgb[0] = uint8(rg) rgb[1] = uint8(gb) return rgb } + func VP8YuvToRgba4444(y uint8, u uint8, v uint8) (argb [2]uint8) { var ( r = VP8YUVToR(y, v) @@ -69,13 +77,14 @@ func VP8YuvToRgba4444(y uint8, u uint8, v uint8) (argb [2]uint8) { b = VP8YUVToB(y, u) ) - var rg = (r & 0xf0) | (g >> 4) - var ba = (b & 0xf0) | 0x0f // overwrite the lower 4 bits + rg := (r & 0xf0) | (g >> 4) + ba := (b & 0xf0) | 0x0f // overwrite the lower 4 bits argb[0] = uint8(rg) argb[1] = uint8(ba) return argb } + func VP8YuvToArgb(y uint8, u uint8, v uint8) (argb [4]uint8) { argb[0] = 0xff rgb := VP8YuvToRgb(y, u, v) @@ -83,6 +92,7 @@ func VP8YuvToArgb(y uint8, u uint8, v uint8) (argb [4]uint8) { return argb } + func VP8YuvToBgra(y uint8, u uint8, v uint8) (bgra [4]uint8) { bgr := VP8YuvToBgr(y, u, v) copy(bgra[:], bgr[:]) @@ -90,6 +100,7 @@ func VP8YuvToBgra(y uint8, u uint8, v uint8) (bgra [4]uint8) { return bgra } + func VP8YuvToRgba(y uint8, u uint8, v uint8) (rgba [4]uint8) { rgb := VP8YuvToRgb(uint8(y), uint8(u), uint8(v)) copy(rgba[:], rgb[:]) diff --git a/yuv.fancy.upscaling.go b/yuv.fancy.upscaling.go index 7846589..57044af 100644 --- a/yuv.fancy.upscaling.go +++ b/yuv.fancy.upscaling.go @@ -4,132 +4,132 @@ import ( "image" ) -type WebPUpsampleLinePairFunc func(top_y []uint8, bottom_y []uint8, - top_u []uint8, top_v []uint8, - bottom_u []uint8, bottom_v []uint8, - top_dst []uint8, bottom_dst []uint8, len int) +type WebPUpsampleLinePairFunc func(topY []uint8, bottomY []uint8, + topU []uint8, topV []uint8, + bottomU []uint8, bottomV []uint8, + topDst []uint8, bottomDst []uint8, len int) func FancyUpscale(yuv *image.YCbCr) *image.RGBA { rgb := image.NewRGBA(image.Rect(0, 0, yuv.Rect.Dx(), yuv.Rect.Dy())) var ( upsample WebPUpsampleLinePairFunc = UpsampleRgbaLinePair - last_row int = yuv.Rect.Dy() - mb_w int = yuv.Rect.Dx() + lastRow = yuv.Rect.Dy() + mbW = yuv.Rect.Dx() ) // First line is special cased. We mirror the u/v samples at boundary. upsample(yuv.Y[:yuv.YStride], nil, yuv.Cb[:yuv.CStride], yuv.Cr[:yuv.CStride], yuv.Cb[:yuv.CStride], yuv.Cr[:yuv.CStride], rgb.Pix[:rgb.Stride], nil, - mb_w, + mbW, ) var ( - top_c int - bottom_c int - top_y int - bottom_y int - top_dst int - bottom_dst int + topC int + bottomC int + topY int + bottomY int + topDst int + bottomDst int ) // Loop over each output pairs of row. - for row := 1; row+1 < last_row; row += 2 { - top_c = yuv.COffset(0, row) - bottom_c = top_c + yuv.CStride + for row := 1; row+1 < lastRow; row += 2 { + topC = yuv.COffset(0, row) + bottomC = topC + yuv.CStride - top_y = yuv.YOffset(0, row) - bottom_y = top_y + yuv.YStride + topY = yuv.YOffset(0, row) + bottomY = topY + yuv.YStride - top_dst = rgb.PixOffset(0, row) - bottom_dst = top_dst + rgb.Stride + topDst = rgb.PixOffset(0, row) + bottomDst = topDst + rgb.Stride - upsample(yuv.Y[top_y:bottom_y], yuv.Y[bottom_y:bottom_y+yuv.YStride], - yuv.Cb[top_c:bottom_c], yuv.Cr[top_c:bottom_c], - yuv.Cb[bottom_c:bottom_c+yuv.CStride], yuv.Cr[bottom_c:bottom_c+yuv.CStride], - rgb.Pix[top_dst:bottom_dst], rgb.Pix[bottom_dst:bottom_dst+rgb.Stride], - mb_w, + upsample(yuv.Y[topY:bottomY], yuv.Y[bottomY:bottomY+yuv.YStride], + yuv.Cb[topC:bottomC], yuv.Cr[topC:bottomC], + yuv.Cb[bottomC:bottomC+yuv.CStride], yuv.Cr[bottomC:bottomC+yuv.CStride], + rgb.Pix[topDst:bottomDst], rgb.Pix[bottomDst:bottomDst+rgb.Stride], + mbW, ) } // Process the very last row of even-sized picture - if last_row%2 == 0 { + if lastRow%2 == 0 { upsample(yuv.Y[len(yuv.Y)-yuv.YStride:], nil, yuv.Cb[len(yuv.Cb)-yuv.CStride:], yuv.Cr[len(yuv.Cr)-yuv.CStride:], yuv.Cb[len(yuv.Cb)-yuv.CStride:], yuv.Cr[len(yuv.Cr)-yuv.CStride:], rgb.Pix[len(rgb.Pix)-rgb.Stride:], nil, - mb_w, + mbW, ) } return rgb } -func UpsampleRgbaLinePair(top_y []uint8, bottom_y []uint8, - top_u []uint8, top_v []uint8, - bottom_u []uint8, bottom_v []uint8, - top_dst []uint8, bottom_dst []uint8, rowLength int, +func UpsampleRgbaLinePair(topY []uint8, bottomY []uint8, + topU []uint8, topV []uint8, + bottomU []uint8, bottomV []uint8, + topDst []uint8, bottomDst []uint8, rowLength int, ) { var ( - x int - last_pixel_pair int = (rowLength - 1) >> 1 - tl_uv uint32 = (uint32(top_u[0]) | (uint32(top_v[0]) << 16)) /* top-left sample */ - l_uv uint32 = (uint32(bottom_u[0]) | (uint32(bottom_v[0]) << 16)) /* left-sample */ + x int + lastPixelPair = (rowLength - 1) >> 1 + tlUV = (uint32(topU[0]) | (uint32(topV[0]) << 16)) /* top-left sample */ + lUV = (uint32(bottomU[0]) | (uint32(bottomV[0]) << 16)) /* left-sample */ ) { - var uv0 uint32 = (3*tl_uv + l_uv + 0x00020002) >> 2 - dst := VP8YuvToRgba(top_y[0], uint8(uv0&0xff), uint8((uv0 >> 16))) - copy(top_dst[0:], dst[:]) + uv0 := (3*tlUV + lUV + 0x00020002) >> 2 + dst := VP8YuvToRgba(topY[0], uint8(uv0&0xff), uint8((uv0 >> 16))) + copy(topDst[0:], dst[:]) } - if bottom_y != nil { - var uv0 uint32 = (3*l_uv + tl_uv + 0x00020002) >> 2 - dst := VP8YuvToRgba(bottom_y[0], uint8(uv0&0xff), uint8(uv0>>16)) - copy(bottom_dst[0:], dst[:]) + if bottomY != nil { + uv0 := (3*lUV + tlUV + 0x00020002) >> 2 + dst := VP8YuvToRgba(bottomY[0], uint8(uv0&0xff), uint8(uv0>>16)) + copy(bottomDst[0:], dst[:]) } - for x = 1; x <= last_pixel_pair; x++ { + for x = 1; x <= lastPixelPair; x++ { var ( - t_uv uint32 = (uint32(top_u[x]) | (uint32(top_v[x]) << 16)) /* top sample */ - uv uint32 = (uint32(bottom_u[x]) | (uint32(bottom_v[x]) << 16)) /* sample */ + tUV = (uint32(topU[x]) | (uint32(topV[x]) << 16)) /* top sample */ + uv = (uint32(bottomU[x]) | (uint32(bottomV[x]) << 16)) /* sample */ /* precompute invariant values associated with first and second diagonals*/ - avg uint32 = tl_uv + t_uv + l_uv + uv + 0x00080008 - diag_12 uint32 = (avg + 2*(t_uv+l_uv)) >> 3 - diag_03 uint32 = (avg + 2*(tl_uv+uv)) >> 3 + avg = tlUV + tUV + lUV + uv + 0x00080008 + diag12 = (avg + 2*(tUV+lUV)) >> 3 + diag03 = (avg + 2*(tlUV+uv)) >> 3 ) { var ( - uv0 uint32 = (diag_12 + tl_uv) >> 1 - uv1 uint32 = (diag_03 + t_uv) >> 1 + uv0 = (diag12 + tlUV) >> 1 + uv1 = (diag03 + tUV) >> 1 ) - dst := VP8YuvToRgba(top_y[2*x-1], uint8(uv0&0xff), uint8(uv0>>16)) - copy(top_dst[(2*x-1)*4:], dst[:]) - dst = VP8YuvToRgba(top_y[2*x-0], uint8(uv1&0xff), uint8(uv1>>16)) - copy(top_dst[(2*x-0)*4:], dst[:]) + dst := VP8YuvToRgba(topY[2*x-1], uint8(uv0&0xff), uint8(uv0>>16)) + copy(topDst[(2*x-1)*4:], dst[:]) + dst = VP8YuvToRgba(topY[2*x-0], uint8(uv1&0xff), uint8(uv1>>16)) + copy(topDst[(2*x-0)*4:], dst[:]) } - if bottom_y != nil { + if bottomY != nil { var ( - uv0 uint32 = (diag_03 + l_uv) >> 1 - uv1 uint32 = (diag_12 + uv) >> 1 + uv0 = (diag03 + lUV) >> 1 + uv1 = (diag12 + uv) >> 1 ) - dst := VP8YuvToRgba(bottom_y[2*x-1], uint8(uv0&0xff), uint8(uv0>>16)) - copy(bottom_dst[(2*x-1)*4:], dst[:]) - dst = VP8YuvToRgba(bottom_y[2*x+0], uint8(uv1&0xff), uint8(uv1>>16)) - copy(bottom_dst[(2*x+0)*4:], dst[:]) + dst := VP8YuvToRgba(bottomY[2*x-1], uint8(uv0&0xff), uint8(uv0>>16)) + copy(bottomDst[(2*x-1)*4:], dst[:]) + dst = VP8YuvToRgba(bottomY[2*x+0], uint8(uv1&0xff), uint8(uv1>>16)) + copy(bottomDst[(2*x+0)*4:], dst[:]) } - tl_uv = t_uv - l_uv = uv + tlUV = tUV + lUV = uv } if rowLength%2 == 0 { { - var uv0 uint32 = (3*tl_uv + l_uv + 0x00020002) >> 2 - dst := VP8YuvToRgba(top_y[rowLength-1], uint8(uv0&0xff), uint8(uv0>>16)) - copy(top_dst[(rowLength-1)*4:], dst[:]) + uv0 := (3*tlUV + lUV + 0x00020002) >> 2 + dst := VP8YuvToRgba(topY[rowLength-1], uint8(uv0&0xff), uint8(uv0>>16)) + copy(topDst[(rowLength-1)*4:], dst[:]) } - if bottom_y != nil { - var uv0 uint32 = (3*l_uv + tl_uv + 0x00020002) >> 2 - dst := VP8YuvToRgba(bottom_y[rowLength-1], uint8(uv0&0xff), uint8(uv0>>16)) - copy(bottom_dst[(rowLength-1)*4:], dst[:]) + if bottomY != nil { + uv0 := (3*lUV + tlUV + 0x00020002) >> 2 + dst := VP8YuvToRgba(bottomY[rowLength-1], uint8(uv0&0xff), uint8(uv0>>16)) + copy(bottomDst[(rowLength-1)*4:], dst[:]) } } }