// Resample returns a resampled copy of the tipimage slice r of m. // The returned tipimage has width w and height h. func Resample(m tipimage.Image, r tipimage.Rectangle, w, h int) tipimage.Image { if w < 0 || h < 0 { return nil } if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 { return tipimage.NewRGBA64(tipimage.Rect(0, 0, w, h)) } curw, curh := r.Dx(), r.Dy() img := tipimage.NewRGBA(tipimage.Rect(0, 0, w, h)) for y := 0; y < h; y++ { for x := 0; x < w; x++ { // Get a source pixel. subx := x * curw / w suby := y * curh / h r32, g32, b32, a32 := m.At(subx, suby).RGBA() r := uint8(r32 >> 8) g := uint8(g32 >> 8) b := uint8(b32 >> 8) a := uint8(a32 >> 8) img.SetRGBA(x, y, color.RGBA{r, g, b, a}) } } return img }
func PHash(src tipimage.Image) (hash int64, part [8]int8) { scaled := Resize(src, src.Bounds(), 32, 32) var image [32][32]float64 for x := 0; x < 32; x++ { for y := 0; y < 32; y++ { image[x][y] = float64(color.GrayModel.Convert(scaled.At(x, y)).(color.Gray).Y) } } //caculate DCT matrix kernel as below: // var kernel [32][32]float64 // alpha := math.Sqrt(1.0 / 32.0) // x := math.Pi + 1.0 // for i := 0; i < 32; i++ { // kernel[i][0] = alpha * math.Cos((x*float64(i))/64.0) // } // alpha = math.Sqrt(2.0 / 32.0) // for i := 0; i < 32; i++ { // for j := 1; j < 32; j++ { // kernel[i][j] = alpha * math.Cos((math.Pi*(2*float64(j)+1)*float64(i))/64.0) // } // } kernel := [32][32]float64{[32]float64{0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25}, [32]float64{0.24729412749119525, 0.24250781329863602, 0.2353860162957552, 0.22599732328086083, 0.21443215250006803, 0.20080188287016124, 0.18523778133873978, 0.1678897387117546, 0.14892482612310837, 0.12852568604830544, 0.10688877335757055, 0.08422246334805501, 0.060745044975815995, 0.03668261861384044, 0.012266918581854533, -0.012266918581854502, -0.03668261861384041, -0.06074504497581597, -0.08422246334805499, -0.10688877335757047, -0.1285256860483054, -0.1489248261231083, -0.1678897387117546, -0.18523778133873972, -0.2008018828701612, -0.21443215250006803, -0.22599732328086086, -0.23538601629575517, -0.242507813298636, -0.24729412749119525, -0.2496988640512931}, [32]float64{0.2392350839330522, 0.22048031608708876, 0.19325261334068425, 0.1585983210409114, 0.11784918420649945, 0.07257116931361557, 0.024504285082390193, -0.024504285082390165, -0.07257116931361556, -0.11784918420649942, -0.15859832104091132, -0.19325261334068425, -0.22048031608708873, -0.2392350839330522, -0.2487961816680492, -0.2487961816680492, -0.23923508393305223, -0.22048031608708876, -0.19325261334068428, -0.15859832104091148, -0.11784918420649947, -0.0725711693136156, -0.024504285082390113, 0.024504285082390023, 0.07257116931361553, 0.1178491842064994, 0.1585983210409114, 0.19325261334068417, 0.2204803160870887, 0.2392350839330522, 0.2487961816680492}, [32]float64{0.22599732328086083, 0.18523778133873978, 0.12852568604830544, 0.060745044975815995, -0.012266918581854446, -0.08422246334805504, -0.14892482612310823, -0.20080188287016126, -0.23538601629575517, -0.2496988640512931, -0.24250781329863602, -0.21443215250006806, -0.16788973871175467, -0.10688877335757062, -0.03668261861384058, 0.036682618613840486, 0.10688877335757055, 0.16788973871175458, 0.214432152500068, 0.24250781329863594, 0.2496988640512931, 0.23538601629575517, 0.2008018828701612, 0.1489248261231085, 0.08422246334805523, 0.012266918581854538, -0.06074504497581613, -0.12852568604830533, -0.18523778133873983, -0.22599732328086078, -0.24729412749119525}, [32]float64{0.20786740307563634, 0.13889255825490057, 0.048772580504032076, -0.04877258050403205, -0.1388925582549005, -0.20786740307563634, -0.24519632010080758, -0.2451963201008076, -0.20786740307563634, -0.13889255825490054, -0.048772580504032166, 0.04877258050403207, 0.13889255825490046, 0.20786740307563634, 0.24519632010080758, 0.2451963201008076, 0.20786740307563636, 0.13889255825490057, 0.048772580504032194, -0.048772580504031826, -0.13889255825490043, -0.2078674030756363, -0.24519632010080764, -0.24519632010080766, -0.20786740307563636, -0.1388925582549006, -0.048772580504032007, 0.0487725805040318, 0.1388925582549004, 0.20786740307563628, 0.2451963201008076}, [32]float64{0.18523778133873978, 0.08422246334805501, -0.03668261861384041, -0.1489248261231083, -0.2259973232808608, -0.2496988640512931, -0.21443215250006806, -0.12852568604830547, -0.012266918581854507, 0.10688877335757055, 0.20080188287016112, 0.24729412749119525, 0.23538601629575523, 0.16788973871175453, 0.06074504497581606, -0.06074504497581613, -0.1678897387117544, -0.23538601629575512, -0.24729412749119528, -0.20080188287016132, -0.10688877335757069, 0.012266918581854355, 0.1285256860483057, 0.21443215250006797, 0.2496988640512931, 0.22599732328086086, 0.14892482612310837, 0.03668261861384089, -0.08422246334805461, -0.18523778133873978, -0.24250781329863602}, [32]float64{0.1585983210409114, 0.024504285082390193, -0.11784918420649942, -0.22048031608708873, -0.24879618166804923, -0.19325261334068422, -0.07257116931361582, 0.07257116931361574, 0.19325261334068417, 0.2487961816680492, 0.22048031608708876, 0.11784918420649951, -0.02450428508238999, -0.1585983210409112, -0.23923508393305212, -0.23923508393305218, -0.15859832104091134, -0.024504285082390175, 0.11784918420649934, 0.22048031608708848, 0.24879618166804923, 0.1932526133406841, 0.07257116931361549, -0.07257116931361522, -0.19325261334068394, -0.2487961816680492, -0.2204803160870886, -0.11784918420649958, 0.024504285082390338, 0.15859832104091112, 0.23923508393305223}, [32]float64{0.12852568604830544, -0.03668261861384041, -0.18523778133873972, -0.2496988640512931, -0.2008018828701613, -0.060745044975815815, 0.10688877335757034, 0.2259973232808609, 0.24250781329863605, 0.1489248261231083, -0.012266918581854386, -0.16788973871175472, -0.24729412749119517, -0.21443215250006809, -0.08422246334805526, 0.08422246334805505, 0.21443215250006797, 0.24729412749119523, 0.1678897387117549, 0.012266918581855041, -0.14892482612310814, -0.24250781329863602, -0.2259973232808609, -0.10688877335757113, 0.06074504497581561, 0.20080188287016118, 0.2496988640512931, 0.18523778133874003, 0.03668261861384051, -0.12852568604830564, -0.23538601629575537}, [32]float64{0.09567085809127246, -0.09567085809127243, -0.23096988312782168, -0.2309698831278217, -0.09567085809127258, 0.0956708580912725, 0.23096988312782163, 0.23096988312782168, 0.09567085809127263, -0.09567085809127246, -0.2309698831278216, -0.23096988312782168, -0.09567085809127264, 0.09567085809127245, 0.2309698831278216, 0.23096988312782168, 0.09567085809127267, -0.09567085809127242, -0.23096988312782157, -0.23096988312782188, -0.0956708580912727, 0.09567085809127239, 0.23096988312782174, 0.2309698831278219, 0.09567085809127272, -0.09567085809127236, -0.23096988312782174, -0.2309698831278219, -0.09567085809127276, 0.09567085809127232, 0.23096988312782174}, [32]float64{0.060745044975815995, -0.1489248261231083, -0.2496988640512931, -0.16788973871175467, 0.036682618613840264, 0.214432152500068, 0.23538601629575523, 0.08422246334805503, -0.12852568604830533, -0.24729412749119525, -0.18523778133873994, 0.012266918581854355, 0.20080188287016124, 0.242507813298636, 0.1068887733575707, -0.10688877335757045, -0.2425078132986359, -0.20080188287016137, -0.01226691858185463, 0.18523778133873917, 0.24729412749119523, 0.1285256860483052, -0.08422246334805539, -0.2353860162957551, -0.21443215250006814, -0.03668261861384054, 0.16788973871175464, 0.24969886405129313, 0.1489248261231088, -0.06074504497581551, -0.2259973232808607}, [32]float64{0.024504285082390193, -0.19325261334068425, -0.23923508393305223, -0.0725711693136156, 0.1585983210409112, 0.2487961816680492, 0.11784918420649951, -0.11784918420649935, -0.2487961816680492, -0.15859832104091134, 0.07257116931361525, 0.23923508393305226, 0.1932526133406844, -0.02450428508239037, -0.22048031608708868, -0.2204803160870886, -0.024504285082390678, 0.19325261334068394, 0.23923508393305234, 0.07257116931361596, -0.15859832104091112, -0.24879618166804923, -0.11784918420649886, 0.11784918420649923, 0.2487961816680492, 0.15859832104091146, -0.07257116931361553, -0.23923508393305196, -0.19325261334068478, 0.02450428508239022, 0.22048031608708882}, [32]float64{-0.012266918581854502, -0.22599732328086086, -0.20080188287016124, 0.036682618613840486, 0.23538601629575515, 0.18523778133873964, -0.0607450449758157, -0.24250781329863605, -0.16788973871175486, 0.08422246334805505, 0.24729412749119517, 0.14892482612310837, -0.10688877335757005, -0.2496988640512931, -0.12852568604830594, 0.12852568604830567, 0.24969886405129313, 0.10688877335757035, -0.14892482612310773, -0.24729412749119536, -0.08422246334805579, 0.16788973871175464, 0.24250781329863602, 0.060745044975816884, -0.18523778133873972, -0.23538601629575523, -0.036682618613840604, 0.20080188287016057, 0.22599732328086092, 0.012266918581853895, -0.2144321525000683}, [32]float64{-0.04877258050403205, -0.24519632010080758, -0.13889255825490054, 0.13889255825490046, 0.24519632010080766, 0.04877258050403198, -0.20786740307563606, -0.20786740307563614, 0.0487725805040318, 0.2451963201008076, 0.13889255825490063, -0.13889255825490038, -0.24519632010080766, -0.0487725805040325, 0.20786740307563598, 0.20786740307563623, -0.04877258050403214, -0.2451963201008076, -0.13889255825490068, 0.13889255825489957, 0.2451963201008077, 0.04877258050403172, -0.20786740307563642, -0.20786740307563675, 0.04877258050403118, 0.24519632010080758, 0.13889255825490004, -0.13889255825490024, -0.24519632010080752, -0.048772580504032687, 0.20786740307563636}, [32]float64{-0.08422246334805499, -0.2496988640512931, -0.06074504497581603, 0.214432152500068, 0.18523778133873994, -0.10688877335757051, -0.24729412749119528, -0.036682618613840416, 0.22599732328086075, 0.16788973871175455, -0.1285256860483053, -0.242507813298636, -0.01226691858185463, 0.2353860162957551, 0.14892482612310876, -0.14892482612310845, -0.23538601629575523, 0.012266918581854233, 0.24250781329863588, 0.128525686048306, -0.1678897387117546, -0.22599732328086095, 0.0366826186138409, 0.24729412749119514, 0.10688877335757127, -0.18523778133874028, -0.21443215250006772, 0.060745044975814566, 0.2496988640512931, 0.0842224633480551, -0.20080188287016107}, [32]float64{-0.11784918420649942, -0.23923508393305223, 0.024504285082390023, 0.2487961816680492, 0.07257116931361586, -0.2204803160870889, -0.1585983210409117, 0.1585983210409115, 0.220480316087089, -0.07257116931361564, -0.24879618166804923, -0.024504285082389794, 0.23923508393305198, 0.1178491842064996, -0.19325261334068392, -0.1932526133406842, 0.11784918420649923, 0.2392350839330521, -0.024504285082389367, -0.2487961816680491, -0.07257116931361605, 0.22048031608708882, 0.1585983210409115, -0.15859832104091032, -0.22048031608708912, 0.07257116931361544, 0.24879618166804918, 0.02450428508239089, -0.23923508393305218, -0.11784918420649902, 0.19325261334068491}, [32]float64{-0.1489248261231083, -0.21443215250006806, 0.10688877335757055, 0.23538601629575523, -0.0607450449758157, -0.2472941274911952, 0.012266918581854355, 0.2496988640512931, 0.03668261861384089, -0.24250781329863602, -0.08422246334805573, 0.22599732328086072, 0.12852568604830597, -0.20080188287016118, -0.16788973871175494, 0.16788973871175464, 0.20080188287016143, -0.12852568604830558, -0.22599732328086095, 0.08422246334805446, 0.24250781329863622, -0.036682618613840874, -0.2496988640512931, -0.012266918581854813, 0.24729412749119514, 0.060745044975817, -0.23538601629575528, -0.10688877335757216, 0.2144321525000678, 0.14892482612310898, -0.1852377813387402}, [32]float64{-0.17677669529663687, -0.17677669529663692, 0.17677669529663684, 0.17677669529663692, -0.17677669529663667, -0.1767766952966368, 0.17677669529663664, 0.1767766952966368, -0.17677669529663662, -0.17677669529663687, 0.1767766952966366, 0.17677669529663687, -0.1767766952966366, -0.1767766952966369, 0.17677669529663656, 0.17677669529663692, -0.17677669529663653, -0.17677669529663692, 0.17677669529663653, 0.17677669529663756, -0.17677669529663648, -0.17677669529663695, 0.1767766952966371, 0.17677669529663761, -0.17677669529663645, -0.176776695296637, 0.17677669529663706, 0.17677669529663767, -0.17677669529663642, -0.17677669529663706, 0.176776695296637}, [32]float64{-0.2008018828701612, -0.12852568604830547, 0.22599732328086078, 0.08422246334805503, -0.24250781329863594, -0.036682618613840416, 0.24969886405129307, -0.012266918581854767, -0.24729412749119528, 0.06074504497581647, 0.23538601629575523, -0.10688877335757081, -0.21443215250006814, 0.14892482612310842, 0.18523778133874008, -0.18523778133873972, -0.14892482612310884, 0.2144321525000679, 0.10688877335757127, -0.23538601629575473, -0.06074504497581611, 0.24729412749119514, 0.012266918581853957, -0.24969886405129318, 0.0366826186138399, 0.24250781329863585, -0.08422246334805515, -0.225997323280861, 0.12852568604830464, 0.20080188287016107, -0.16788973871175442}, [32]float64{-0.22048031608708873, -0.0725711693136156, 0.2487961816680492, -0.02450428508238999, -0.23923508393305232, 0.11784918420649934, 0.1932526133406844, -0.19325261334068422, -0.11784918420649958, 0.23923508393305223, 0.024504285082390706, -0.24879618166804923, 0.07257116931361556, 0.22048031608708865, -0.15859832104091107, -0.15859832104091148, 0.2204803160870884, 0.07257116931361608, -0.24879618166804918, 0.024504285082388392, 0.23923508393305212, -0.11784918420649987, -0.19325261334068372, 0.19325261334068378, 0.11784918420649983, -0.23923508393305215, -0.0245042850823901, 0.24879618166804934, -0.07257116931361444, -0.2204803160870892, 0.15859832104091084}, [32]float64{-0.23538601629575517, -0.012266918581854507, 0.24250781329863602, -0.12852568604830533, -0.16788973871175486, 0.22599732328086092, 0.03668261861384089, -0.24729412749119523, 0.10688877335757004, 0.18523778133874003, -0.21443215250006792, -0.06074504497581599, 0.24969886405129313, -0.08422246334805532, -0.20080188287016146, 0.20080188287016112, 0.08422246334805504, -0.24969886405129307, 0.060745044975814566, 0.2144321525000682, -0.18523778133873905, -0.10688877335757056, 0.2472941274911954, -0.036682618613838994, -0.225997323280861, 0.16788973871175378, 0.12852568604830547, -0.24250781329863583, 0.01226691858185304, 0.2353860162957553, -0.14892482612310887}, [32]float64{-0.24519632010080758, 0.04877258050403207, 0.20786740307563636, -0.2078674030756363, -0.04877258050403244, 0.24519632010080758, -0.13889255825490038, -0.13889255825490066, 0.2451963201008076, -0.04877258050403214, -0.20786740307563673, 0.20786740307563645, 0.048772580504032596, -0.24519632010080752, 0.13889255825490027, 0.13889255825490004, -0.2451963201008074, 0.04877258050403112, 0.2078674030756368, -0.20786740307563586, -0.04877258050403275, 0.24519632010080772, -0.1388925582549016, -0.1388925582549009, 0.24519632010080755, -0.04877258050403184, -0.20786740307563636, 0.2078674030756353, 0.04877258050403377, -0.24519632010080758, 0.13889255825490074}, [32]float64{-0.2496988640512931, 0.10688877335757055, 0.1489248261231083, -0.24729412749119525, 0.08422246334805462, 0.16788973871175455, -0.2425078132986359, 0.06074504497581647, 0.18523778133874003, -0.23538601629575537, 0.03668261861384009, 0.20080188287016143, -0.2259973232808607, 0.01226691858185417, 0.21443215250006817, -0.2144321525000683, -0.012266918581854813, 0.22599732328086058, -0.20080188287016107, -0.0366826186138416, 0.23538601629575528, -0.1852377813387402, -0.060745044975816224, 0.24250781329863627, -0.16788973871175442, -0.0842224633480544, 0.24729412749119528, -0.14892482612310745, -0.10688877335757072, 0.24969886405129316, -0.12852568604830678}, [32]float64{-0.2487961816680492, 0.1585983210409114, 0.07257116931361564, -0.23923508393305218, 0.193252613340684, 0.024504285082389762, -0.22048031608708904, 0.22048031608708887, -0.02450428508238943, -0.1932526133406842, 0.23923508393305196, -0.07257116931361553, -0.15859832104091215, 0.24879618166804918, -0.11784918420649837, -0.11784918420649895, 0.24879618166804934, -0.15859832104091165, -0.07257116931361703, 0.23923508393305268, -0.1932526133406832, -0.0245042850823901, 0.22048031608708876, -0.22048031608708787, 0.024504285082389977, 0.1932526133406844, -0.23923508393305212, 0.0725711693136135, 0.15859832104091176, -0.24879618166804934, 0.11784918420650042}, [32]float64{-0.24250781329863602, 0.20080188287016126, -0.012266918581854386, -0.18523778133873994, 0.24729412749119517, -0.12852568604830567, -0.08422246334805573, 0.23538601629575523, -0.21443215250006792, 0.03668261861384009, 0.16788973871175497, -0.2496988640512931, 0.14892482612310767, 0.06074504497581522, -0.22599732328086095, 0.22599732328086103, -0.0607450449758154, -0.14892482612310823, 0.24969886405129316, -0.16788973871175314, -0.03668261861384166, 0.21443215250006828, -0.23538601629575526, 0.08422246334805422, 0.1285256860483055, -0.24729412749119542, 0.18523778133873947, 0.01226691858185775, -0.20080188287016165, 0.24250781329863558, -0.10688877335757116}, [32]float64{-0.2309698831278217, 0.23096988312782163, -0.09567085809127246, -0.09567085809127264, 0.23096988312782188, -0.23096988312782177, 0.09567085809127157, 0.0956708580912719, -0.2309698831278219, 0.23096988312782174, -0.09567085809127229, -0.0956708580912728, 0.23096988312782193, -0.23096988312782135, 0.0956708580912714, 0.09567085809127208, -0.23096988312782163, 0.23096988312782166, -0.09567085809127213, -0.09567085809127462, 0.23096988312782202, -0.23096988312782196, 0.09567085809127286, 0.09567085809127389, -0.23096988312782238, 0.23096988312782157, -0.09567085809127361, -0.09567085809127314, 0.2309698831278214, -0.23096988312782119, 0.0956708580912727}, [32]float64{-0.21443215250006806, 0.24729412749119523, -0.1678897387117544, 0.012266918581854355, 0.14892482612310873, -0.242507813298636, 0.22599732328086072, -0.10688877335757081, -0.06074504497581685, 0.2008018828701609, -0.24969886405129307, 0.18523778133873967, -0.03668261861383912, -0.1285256860483061, 0.23538601629575526, -0.23538601629575528, 0.12852568604830467, 0.03668261861384075, -0.1852377813387408, 0.24969886405129316, -0.20080188287016096, 0.06074504497581611, 0.1068887733575699, -0.2259973232808618, 0.24250781329863558, -0.1489248261231088, -0.012266918581853343, 0.16788973871175597, -0.24729412749119542, 0.21443215250006764, -0.08422246334805486}, [32]float64{-0.19325261334068428, 0.24879618166804923, -0.22048031608708868, 0.11784918420649934, 0.024504285082390647, -0.1585983210409114, 0.23923508393305234, -0.23923508393305223, 0.1585983210409111, -0.024504285082390248, -0.11784918420649969, 0.22048031608708868, -0.24879618166804918, 0.1932526133406838, -0.07257116931361456, -0.07257116931361532, 0.19325261334068433, -0.24879618166804926, 0.22048031608708832, -0.1178491842064982, -0.024504285082390158, 0.1585983210409117, -0.23923508393305193, 0.23923508393305185, -0.1585983210409101, 0.024504285082391622, 0.11784918420649848, -0.22048031608709012, 0.24879618166804923, -0.1932526133406841, 0.07257116931361503}, [32]float64{-0.16788973871175467, 0.23538601629575523, -0.24729412749119525, 0.20080188287016124, -0.10688877335757005, -0.01226691858185463, 0.12852568604830597, -0.21443215250006814, 0.24969886405129313, -0.2259973232808607, 0.14892482612310767, -0.036682618613840874, -0.0842224633480559, 0.18523778133874014, -0.24250781329863605, 0.24250781329863608, -0.18523778133873897, 0.08422246334805429, 0.03668261861384081, -0.14892482612310975, 0.22599732328086142, -0.24969886405129305, 0.21443215250006864, -0.12852568604830372, 0.012266918581854693, 0.10688877335757162, -0.20080188287016065, 0.2472941274911953, -0.2353860162957546, 0.16788973871175486, -0.06074504497581498}, [32]float64{-0.13889255825490054, 0.20786740307563636, -0.24519632010080766, 0.2451963201008076, -0.207867403075636, 0.1388925582549011, -0.04877258050403127, -0.048772580504031694, 0.13889255825490146, -0.20786740307563625, 0.2451963201008077, -0.24519632010080775, 0.20786740307563542, -0.13889255825490018, 0.048772580504031056, 0.0487725805040319, -0.1388925582549009, 0.20786740307563586, -0.2451963201008079, 0.2451963201008072, -0.20786740307563578, 0.13889255825490074, -0.04877258050403172, -0.04877258050403473, 0.13889255825490182, -0.20786740307563648, 0.24519632010080744, -0.24519632010080733, 0.20786740307563614, -0.13889255825490132, 0.04877258050403413}, [32]float64{-0.10688877335757062, 0.1678897387117547, -0.21443215250006809, 0.24250781329863608, -0.24969886405129307, 0.23538601629575537, -0.20080188287016063, 0.14892482612310842, -0.08422246334805449, 0.012266918581855057, 0.06074504497581694, -0.1285256860483061, 0.18523778133874014, -0.22599732328086097, 0.24729412749119525, -0.24729412749119525, 0.2259973232808602, -0.18523778133874014, 0.12852568604830458, -0.06074504497581349, -0.012266918581855057, 0.08422246334805532, -0.14892482612310842, 0.20080188287016326, -0.2353860162957551, 0.24969886405129307, -0.2425078132986362, 0.2144321525000667, -0.1678897387117529, 0.10688877335756862, -0.03668261861384208}, [32]float64{-0.0725711693136156, 0.11784918420649951, -0.15859832104091134, 0.1932526133406844, -0.22048031608708904, 0.2392350839330521, -0.24879618166804923, 0.2487961816680492, -0.23923508393305196, 0.22048031608708882, -0.19325261334068328, 0.15859832104091098, -0.1178491842064983, 0.07257116931361537, -0.024504285082389183, -0.0245042850823901, 0.07257116931361625, -0.11784918420649913, 0.1585983210409117, -0.193252613340685, 0.22048031608708965, -0.23923508393305196, 0.2487961816680492, -0.24879618166804915, 0.23923508393305182, -0.22048031608708776, 0.1932526133406847, -0.1585983210409086, 0.11784918420649869, -0.07257116931361408, 0.02450428508239138}, [32]float64{-0.03668261861384058, 0.06074504497581606, -0.08422246334805526, 0.1068887733575707, -0.12852568604830594, 0.14892482612310806, -0.16788973871175494, 0.18523778133873947, -0.20080188287016146, 0.21443215250006817, -0.22599732328086095, 0.23538601629575526, -0.24250781329863605, 0.24729412749119525, -0.2496988640512931, 0.2496988640512931, -0.24729412749119498, 0.24250781329863605, -0.23538601629575465, 0.22599732328086014, -0.21443215250006814, 0.20080188287016038, -0.18523778133874003, 0.1678897387117536, -0.14892482612310873, 0.12852568604830436, -0.10688877335757106, 0.08422246334805394, -0.0607450449758132, 0.036682618613839424, -0.012266918581855303}} //caculate DCT matrix DCT = kernel*image*kernel var tempArr [32][32]float64 var iRow [32]float64 for i := 0; i < 32; i++ { iRow = kernel[i] for j := 0; j < 32; j++ { tempArr[i][j] += iRow[j] * image[j][i] } } var DCTArr [32][32]float64 for i := 0; i < 32; i++ { iRow = tempArr[i] for j := 0; j < 32; j++ { DCTArr[i][j] += iRow[j] * kernel[j][i] } } //caculate average of 8x8 DCT var sum float64 for i := 1; i < 8; i++ { sum += DCTArr[0][i] + DCTArr[i][0] } for i := 1; i < 8; i++ { for j := 1; j <= i; j++ { sum += DCTArr[i][j] + DCTArr[j][i] } } avg := sum / 63.0 //contruct the hash var i, j uint8 for i = 0; i < 8; i++ { for j = 0; j < 8; j++ { if DCTArr[i][j] > avg { part[i] = part[i] | (1 << j) hash = hash | (1 << (i*8 + j)) } } } return hash, part }
// Resize returns a scaled copy of the tipimage slice r of m. // The returned tipimage has width w and height h. func Resize(m tipimage.Image, r tipimage.Rectangle, w, h int) tipimage.Image { if w < 0 || h < 0 { return nil } if w == 0 || h == 0 || r.Dx() <= 0 || r.Dy() <= 0 { return tipimage.NewRGBA64(tipimage.Rect(0, 0, w, h)) } switch m := m.(type) { case *tipimage.RGBA: return resizeRGBA(m, r, w, h) case *tipimage.YCbCr: if m, ok := resizeYCbCr(m, r, w, h); ok { return m } } ww, hh := uint64(w), uint64(h) dx, dy := uint64(r.Dx()), uint64(r.Dy()) // The scaling algorithm is to nearest-neighbor magnify the dx * dy source // to a (ww*dx) * (hh*dy) intermediate tipimage and then minify the intermediate // tipimage back down to a ww * hh destination with a simple box filter. // The intermediate tipimage is implied, we do not physically allocate a slice // of length ww*dx*hh*dy. // For example, consider a 4*3 source tipimage. Label its pixels from a-l: // abcd // efgh // ijkl // To resize this to a 3*2 destination tipimage, the intermediate is 12*6. // Whitespace has been added to delineate the destination pixels: // aaab bbcc cddd // aaab bbcc cddd // eeef ffgg ghhh // // eeef ffgg ghhh // iiij jjkk klll // iiij jjkk klll // Thus, the 'b' source pixel contributes one third of its value to the // (0, 0) destination pixel and two thirds to (1, 0). // The implementation is a two-step process. First, the source pixels are // iterated over and each source pixel's contribution to 1 or more // destination pixels are summed. Second, the sums are divided by a scaling // factor to yield the destination pixels. // TODO: By interleaving the two steps, instead of doing all of // step 1 first and all of step 2 second, we could allocate a smaller sum // slice of length 4*w*2 instead of 4*w*h, although the resultant code // would become more complicated. n, sum := dx*dy, make([]uint64, 4*w*h) for y := r.Min.Y; y < r.Max.Y; y++ { for x := r.Min.X; x < r.Max.X; x++ { // Get the source pixel. r32, g32, b32, a32 := m.At(x, y).RGBA() r64 := uint64(r32) g64 := uint64(g32) b64 := uint64(b32) a64 := uint64(a32) // Spread the source pixel over 1 or more destination rows. py := uint64(y) * hh for remy := hh; remy > 0; { qy := dy - (py % dy) if qy > remy { qy = remy } // Spread the source pixel over 1 or more destination columns. px := uint64(x) * ww index := 4 * ((py/dy)*ww + (px / dx)) for remx := ww; remx > 0; { qx := dx - (px % dx) if qx > remx { qx = remx } sum[index+0] += r64 * qx * qy sum[index+1] += g64 * qx * qy sum[index+2] += b64 * qx * qy sum[index+3] += a64 * qx * qy index += 4 px += qx remx -= qx } py += qy remy -= qy } } } return average(sum, w, h, n*0x0101) }