converter.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
  3. Permission to use, copy, modify, and/or distribute this software for any purpose
  4. with or without fee is hereby granted, provided that the above copyright notice
  5. and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  7. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  8. FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  9. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  10. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  11. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  12. THIS SOFTWARE.
  13. */
  14. package resize
  15. import (
  16. "image"
  17. "image/color"
  18. )
  19. // Keep value in [0,255] range.
  20. func clampUint8(in int32) uint8 {
  21. if in < 0 {
  22. return 0
  23. }
  24. if in > 255 {
  25. return 255
  26. }
  27. return uint8(in)
  28. }
  29. // Keep value in [0,65535] range.
  30. func clampUint16(in int64) uint16 {
  31. if in < 0 {
  32. return 0
  33. }
  34. if in > 65535 {
  35. return 65535
  36. }
  37. return uint16(in)
  38. }
  39. func resizeGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []int32, filterLength int) {
  40. oldBounds := in.Bounds()
  41. newBounds := out.Bounds()
  42. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  43. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  44. interpX := scale*(float64(y)+0.5) + float64(oldBounds.Min.X)
  45. start := int(interpX) - filterLength/2 + 1
  46. var rgba [4]int64
  47. var sum int64
  48. for i := 0; i < filterLength; i++ {
  49. xx := start + i
  50. if xx < oldBounds.Min.X {
  51. xx = oldBounds.Min.X
  52. } else if xx >= oldBounds.Max.X {
  53. xx = oldBounds.Max.X - 1
  54. }
  55. coeff := coeffs[(y-newBounds.Min.Y)*filterLength+i]
  56. r, g, b, a := in.At(xx, x).RGBA()
  57. rgba[0] += int64(coeff) * int64(r)
  58. rgba[1] += int64(coeff) * int64(g)
  59. rgba[2] += int64(coeff) * int64(b)
  60. rgba[3] += int64(coeff) * int64(a)
  61. sum += int64(coeff)
  62. }
  63. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
  64. value := clampUint16(rgba[0] / sum)
  65. out.Pix[offset+0] = uint8(value >> 8)
  66. out.Pix[offset+1] = uint8(value)
  67. value = clampUint16(rgba[1] / sum)
  68. out.Pix[offset+2] = uint8(value >> 8)
  69. out.Pix[offset+3] = uint8(value)
  70. value = clampUint16(rgba[2] / sum)
  71. out.Pix[offset+4] = uint8(value >> 8)
  72. out.Pix[offset+5] = uint8(value)
  73. value = clampUint16(rgba[3] / sum)
  74. out.Pix[offset+6] = uint8(value >> 8)
  75. out.Pix[offset+7] = uint8(value)
  76. }
  77. }
  78. }
  79. func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, filterLength int) {
  80. oldBounds := in.Bounds()
  81. newBounds := out.Bounds()
  82. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  83. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  84. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  85. interpX := scale*(float64(y)+0.5) + float64(oldBounds.Min.X)
  86. start := int(interpX) - filterLength/2 + 1
  87. var rgba [4]int32
  88. var sum int32
  89. for i := 0; i < filterLength; i++ {
  90. xx := start + i
  91. if xx < oldBounds.Min.X {
  92. xx = oldBounds.Min.X
  93. } else if xx >= oldBounds.Max.X {
  94. xx = oldBounds.Max.X - 1
  95. }
  96. coeff := coeffs[(y-newBounds.Min.Y)*filterLength+i]
  97. offset := (xx - oldBounds.Min.X) * 4
  98. rgba[0] += int32(coeff) * int32(row[offset+0])
  99. rgba[1] += int32(coeff) * int32(row[offset+1])
  100. rgba[2] += int32(coeff) * int32(row[offset+2])
  101. rgba[3] += int32(coeff) * int32(row[offset+3])
  102. sum += int32(coeff)
  103. }
  104. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
  105. out.Pix[offset+0] = clampUint8(rgba[0] / sum)
  106. out.Pix[offset+1] = clampUint8(rgba[1] / sum)
  107. out.Pix[offset+2] = clampUint8(rgba[2] / sum)
  108. out.Pix[offset+3] = clampUint8(rgba[3] / sum)
  109. }
  110. }
  111. }
  112. func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []int32, filterLength int) {
  113. oldBounds := in.Bounds()
  114. newBounds := out.Bounds()
  115. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  116. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  117. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  118. interpX := scale*(float64(y)+0.5) + float64(oldBounds.Min.X)
  119. start := int(interpX) - filterLength/2 + 1
  120. var rgba [4]int64
  121. var sum int64
  122. for i := 0; i < filterLength; i++ {
  123. xx := start + i
  124. if xx < oldBounds.Min.X {
  125. xx = oldBounds.Min.X
  126. } else if xx >= oldBounds.Max.X {
  127. xx = oldBounds.Max.X - 1
  128. }
  129. coeff := coeffs[(y-newBounds.Min.Y)*filterLength+i]
  130. offset := (xx - oldBounds.Min.X) * 8
  131. rgba[0] += int64(coeff) * int64(uint16(row[offset+0])<<8|uint16(row[offset+1]))
  132. rgba[1] += int64(coeff) * int64(uint16(row[offset+2])<<8|uint16(row[offset+3]))
  133. rgba[2] += int64(coeff) * int64(uint16(row[offset+4])<<8|uint16(row[offset+5]))
  134. rgba[3] += int64(coeff) * int64(uint16(row[offset+6])<<8|uint16(row[offset+7]))
  135. sum += int64(coeff)
  136. }
  137. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
  138. value := clampUint16(rgba[0] / sum)
  139. out.Pix[offset+0] = uint8(value >> 8)
  140. out.Pix[offset+1] = uint8(value)
  141. value = clampUint16(rgba[1] / sum)
  142. out.Pix[offset+2] = uint8(value >> 8)
  143. out.Pix[offset+3] = uint8(value)
  144. value = clampUint16(rgba[2] / sum)
  145. out.Pix[offset+4] = uint8(value >> 8)
  146. out.Pix[offset+5] = uint8(value)
  147. value = clampUint16(rgba[3] / sum)
  148. out.Pix[offset+6] = uint8(value >> 8)
  149. out.Pix[offset+7] = uint8(value)
  150. }
  151. }
  152. }
  153. func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, filterLength int) {
  154. oldBounds := in.Bounds()
  155. newBounds := out.Bounds()
  156. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  157. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  158. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  159. interpX := scale*(float64(y)+0.5) + float64(oldBounds.Min.X)
  160. start := int(interpX) - filterLength/2 + 1
  161. var gray int32
  162. var sum int32
  163. for i := 0; i < filterLength; i++ {
  164. xx := start + i
  165. if xx < oldBounds.Min.X {
  166. xx = oldBounds.Min.X
  167. } else if xx >= oldBounds.Max.X {
  168. xx = oldBounds.Max.X - 1
  169. }
  170. coeff := coeffs[(y-newBounds.Min.Y)*filterLength+i]
  171. offset := (xx - oldBounds.Min.X)
  172. gray += int32(coeff) * int32(row[offset])
  173. sum += int32(coeff)
  174. }
  175. offset := (y-newBounds.Min.Y)*out.Stride + (x - newBounds.Min.X)
  176. out.Pix[offset] = clampUint8(gray / sum)
  177. }
  178. }
  179. }
  180. func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []int32, filterLength int) {
  181. oldBounds := in.Bounds()
  182. newBounds := out.Bounds()
  183. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  184. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  185. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  186. interpX := scale*(float64(y)+0.5) + float64(oldBounds.Min.X)
  187. start := int(interpX) - filterLength/2 + 1
  188. var gray int64
  189. var sum int64
  190. for i := 0; i < filterLength; i++ {
  191. xx := start + i
  192. if xx < oldBounds.Min.X {
  193. xx = oldBounds.Min.X
  194. } else if xx >= oldBounds.Max.X {
  195. xx = oldBounds.Max.X - 1
  196. }
  197. coeff := coeffs[(y-newBounds.Min.Y)*filterLength+i]
  198. offset := (xx - oldBounds.Min.X) * 2
  199. gray += int64(coeff) * int64(uint16(row[offset+0])<<8|uint16(row[offset+1]))
  200. sum += int64(coeff)
  201. }
  202. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*2
  203. value := clampUint16(gray / sum)
  204. out.Pix[offset+0] = uint8(value >> 8)
  205. out.Pix[offset+1] = uint8(value)
  206. }
  207. }
  208. }
  209. func convertYCbCrToRGBA(in *image.YCbCr) *image.RGBA {
  210. out := image.NewRGBA(in.Bounds())
  211. for y := 0; y < out.Bounds().Dy(); y++ {
  212. for x := 0; x < out.Bounds().Dx(); x++ {
  213. p := out.Pix[y*out.Stride+4*x:]
  214. yi := in.YOffset(x, y)
  215. ci := in.COffset(x, y)
  216. r, g, b := color.YCbCrToRGB(in.Y[yi], in.Cb[ci], in.Cr[ci])
  217. p[0] = r
  218. p[1] = g
  219. p[2] = b
  220. p[3] = 0xff
  221. }
  222. }
  223. return out
  224. }