You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package main
  2. import (
  3. "bytes"
  4. "image"
  5. "image/jpeg"
  6. "image/png"
  7. "github.com/nfnt/resize"
  8. )
  9. type imgRGBA [][]float64
  10. func dataToImage(data []byte, imageExtension string) (image.Image, error) {
  11. reader := bytes.NewReader(data)
  12. var img image.Image
  13. var err error
  14. switch imageExtension {
  15. case "png":
  16. img, err = png.Decode(reader)
  17. case "jpg":
  18. img, err = jpeg.Decode(reader)
  19. case "jpeg":
  20. img, err = jpeg.Decode(reader)
  21. default:
  22. img = nil
  23. }
  24. if err != nil {
  25. return img, err
  26. }
  27. return img, err
  28. }
  29. func imageToData(img image.Image, imageExtension string) ([]byte, error) {
  30. buf := new(bytes.Buffer)
  31. var err error
  32. switch imageExtension {
  33. case "png":
  34. err = png.Encode(buf, img)
  35. case "jpg":
  36. err = jpeg.Encode(buf, img, nil)
  37. case "jpeg":
  38. err = jpeg.Encode(buf, img, nil)
  39. default:
  40. img = nil
  41. }
  42. if err != nil {
  43. return buf.Bytes(), err
  44. }
  45. return buf.Bytes(), err
  46. }
  47. func imageToPNG(img image.Image) ([]byte, error) {
  48. buf := new(bytes.Buffer)
  49. var err error
  50. err = png.Encode(buf, img)
  51. return buf.Bytes(), err
  52. }
  53. func imageToHistogram(img image.Image) [][]float64 {
  54. bounds := img.Bounds()
  55. //generate the histogram
  56. var histogram [][]float64
  57. for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
  58. for x := bounds.Min.X; x < bounds.Max.X; x++ {
  59. r, g, b, a := img.At(x, y).RGBA()
  60. var pixel []float64
  61. pixel = append(pixel, float64(r), float64(g), float64(b), float64(a))
  62. histogram = append(histogram, pixel)
  63. }
  64. }
  65. return histogram
  66. }
  67. func Resize(img image.Image) image.Image {
  68. r := resize.Resize(uint(config.ImgWidth), uint(config.ImgHeigh), img, resize.Lanczos3)
  69. return r
  70. }