picture.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package docx
  2. /*
  3. picture.go - turning and mirroring a picture's bitmap
  4. A DrawingML picture may be rotated by quarter turns or flipped. The
  5. editor has no notion of a turned picture (a CSS transform would not move
  6. the text around it), so the import bakes the turn into the bitmap and
  7. swaps the frame: the picture then lays out, prints and saves as it looks.
  8. */
  9. import (
  10. "bytes"
  11. "image"
  12. "image/draw"
  13. _ "image/gif"
  14. "image/jpeg"
  15. "image/png"
  16. "math"
  17. )
  18. // pictureTurns reads a DrawingML rotation (60000ths of a degree, clockwise)
  19. // as a count of quarter turns; ok is false for any other angle
  20. func pictureTurns(rot float64) (int, bool) {
  21. deg := rot / 60000
  22. q := math.Round(deg / 90)
  23. if math.Abs(deg-q*90) > 1 {
  24. return 0, false
  25. }
  26. return ((int(q) % 4) + 4) % 4, true
  27. }
  28. // orientPixels mirrors (first) and then turns an image clockwise
  29. func orientPixels(src image.Image, turns int, flipH, flipV bool) *image.NRGBA {
  30. b := src.Bounds()
  31. w, h := b.Dx(), b.Dy()
  32. in := image.NewNRGBA(image.Rect(0, 0, w, h))
  33. draw.Draw(in, in.Bounds(), src, b.Min, draw.Src)
  34. ow, oh := w, h
  35. if turns%2 == 1 {
  36. ow, oh = h, w
  37. }
  38. out := image.NewNRGBA(image.Rect(0, 0, ow, oh))
  39. for y := 0; y < h; y++ {
  40. for x := 0; x < w; x++ {
  41. sx, sy := x, y
  42. if flipH {
  43. sx = w - 1 - x
  44. }
  45. if flipV {
  46. sy = h - 1 - y
  47. }
  48. dx, dy := x, y
  49. switch turns {
  50. case 1:
  51. dx, dy = h-1-y, x
  52. case 2:
  53. dx, dy = w-1-x, h-1-y
  54. case 3:
  55. dx, dy = y, w-1-x
  56. }
  57. si := in.PixOffset(sx, sy)
  58. di := out.PixOffset(dx, dy)
  59. copy(out.Pix[di:di+4], in.Pix[si:si+4])
  60. }
  61. }
  62. return out
  63. }
  64. // orientPicture re-encodes a picture turned and mirrored as DrawingML asks;
  65. // ok is false when the format cannot be decoded (the picture stays as is)
  66. func orientPicture(data []byte, ext string, turns int, flipH, flipV bool) ([]byte, string, bool) {
  67. if turns == 0 && !flipH && !flipV {
  68. return data, ext, true
  69. }
  70. img, format, err := image.Decode(bytes.NewReader(data))
  71. if err != nil {
  72. return data, ext, false
  73. }
  74. out := orientPixels(img, turns, flipH, flipV)
  75. var buf bytes.Buffer
  76. if format == "jpeg" {
  77. if err := jpeg.Encode(&buf, out, &jpeg.Options{Quality: 92}); err != nil {
  78. return data, ext, false
  79. }
  80. return buf.Bytes(), "jpeg", true
  81. }
  82. if err := png.Encode(&buf, out); err != nil {
  83. return data, ext, false
  84. }
  85. return buf.Bytes(), "png", true
  86. }
  87. // orientInsets moves a crop (top, right, bottom, left) with the bitmap
  88. func orientInsets(in [4]float64, turns int, flipH, flipV bool) [4]float64 {
  89. t, r, b, l := in[0], in[1], in[2], in[3]
  90. if flipH {
  91. l, r = r, l
  92. }
  93. if flipV {
  94. t, b = b, t
  95. }
  96. for i := 0; i < turns; i++ {
  97. // a clockwise turn: the left edge becomes the top
  98. t, r, b, l = l, t, r, b
  99. }
  100. return [4]float64{t, r, b, l}
  101. }