converter.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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, offset []int, 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. var rgba [4]int64
  45. var sum int64
  46. start := offset[y]
  47. ci := (y - newBounds.Min.Y) * filterLength
  48. for i := 0; i < filterLength; i++ {
  49. coeff := coeffs[ci+i]
  50. if coeff != 0 {
  51. xi := start + i
  52. switch {
  53. case uint(xi) < uint(oldBounds.Max.X):
  54. break
  55. case xi >= oldBounds.Max.X:
  56. xi = oldBounds.Min.X
  57. default:
  58. xi = oldBounds.Max.X - 1
  59. }
  60. r, g, b, a := in.At(xi, x).RGBA()
  61. rgba[0] += int64(coeff) * int64(r)
  62. rgba[1] += int64(coeff) * int64(g)
  63. rgba[2] += int64(coeff) * int64(b)
  64. rgba[3] += int64(coeff) * int64(a)
  65. sum += int64(coeff)
  66. }
  67. }
  68. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
  69. value := clampUint16(rgba[0] / sum)
  70. out.Pix[offset+0] = uint8(value >> 8)
  71. out.Pix[offset+1] = uint8(value)
  72. value = clampUint16(rgba[1] / sum)
  73. out.Pix[offset+2] = uint8(value >> 8)
  74. out.Pix[offset+3] = uint8(value)
  75. value = clampUint16(rgba[2] / sum)
  76. out.Pix[offset+4] = uint8(value >> 8)
  77. out.Pix[offset+5] = uint8(value)
  78. value = clampUint16(rgba[3] / sum)
  79. out.Pix[offset+6] = uint8(value >> 8)
  80. out.Pix[offset+7] = uint8(value)
  81. }
  82. }
  83. }
  84. func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) {
  85. oldBounds := in.Bounds()
  86. newBounds := out.Bounds()
  87. minX := oldBounds.Min.X * 4
  88. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 4
  89. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  90. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  91. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  92. var rgba [4]int32
  93. var sum int32
  94. start := offset[y]
  95. ci := (y - newBounds.Min.Y) * filterLength
  96. for i := 0; i < filterLength; i++ {
  97. coeff := coeffs[ci+i]
  98. if coeff != 0 {
  99. xi := start + i
  100. switch {
  101. case uint(xi) < uint(oldBounds.Max.X):
  102. xi *= 4
  103. case xi >= oldBounds.Max.X:
  104. xi = maxX
  105. default:
  106. xi = minX
  107. }
  108. rgba[0] += int32(coeff) * int32(row[xi+0])
  109. rgba[1] += int32(coeff) * int32(row[xi+1])
  110. rgba[2] += int32(coeff) * int32(row[xi+2])
  111. rgba[3] += int32(coeff) * int32(row[xi+3])
  112. sum += int32(coeff)
  113. }
  114. }
  115. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
  116. out.Pix[xo+0] = clampUint8(rgba[0] / sum)
  117. out.Pix[xo+1] = clampUint8(rgba[1] / sum)
  118. out.Pix[xo+2] = clampUint8(rgba[2] / sum)
  119. out.Pix[xo+3] = clampUint8(rgba[3] / sum)
  120. }
  121. }
  122. }
  123. func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
  124. oldBounds := in.Bounds()
  125. newBounds := out.Bounds()
  126. minX := oldBounds.Min.X * 8
  127. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 8
  128. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  129. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  130. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  131. var rgba [4]int64
  132. var sum int64
  133. start := offset[y]
  134. ci := (y - newBounds.Min.Y) * filterLength
  135. for i := 0; i < filterLength; i++ {
  136. coeff := coeffs[ci+i]
  137. if coeff != 0 {
  138. xi := start + i
  139. switch {
  140. case uint(xi) < uint(oldBounds.Max.X):
  141. xi *= 8
  142. case xi >= oldBounds.Max.X:
  143. xi = maxX
  144. default:
  145. xi = minX
  146. }
  147. rgba[0] += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
  148. rgba[1] += int64(coeff) * int64(uint16(row[xi+2])<<8|uint16(row[xi+3]))
  149. rgba[2] += int64(coeff) * int64(uint16(row[xi+4])<<8|uint16(row[xi+5]))
  150. rgba[3] += int64(coeff) * int64(uint16(row[xi+6])<<8|uint16(row[xi+7]))
  151. sum += int64(coeff)
  152. }
  153. }
  154. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
  155. value := clampUint16(rgba[0] / sum)
  156. out.Pix[xo+0] = uint8(value >> 8)
  157. out.Pix[xo+1] = uint8(value)
  158. value = clampUint16(rgba[1] / sum)
  159. out.Pix[xo+2] = uint8(value >> 8)
  160. out.Pix[xo+3] = uint8(value)
  161. value = clampUint16(rgba[2] / sum)
  162. out.Pix[xo+4] = uint8(value >> 8)
  163. out.Pix[xo+5] = uint8(value)
  164. value = clampUint16(rgba[3] / sum)
  165. out.Pix[xo+6] = uint8(value >> 8)
  166. out.Pix[xo+7] = uint8(value)
  167. }
  168. }
  169. }
  170. func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, offset []int, filterLength int) {
  171. oldBounds := in.Bounds()
  172. newBounds := out.Bounds()
  173. minX := oldBounds.Min.X
  174. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1)
  175. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  176. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  177. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  178. var gray int32
  179. var sum int32
  180. start := offset[y]
  181. ci := (y - newBounds.Min.Y) * filterLength
  182. for i := 0; i < filterLength; i++ {
  183. coeff := coeffs[ci+i]
  184. if coeff != 0 {
  185. xi := start + i
  186. switch {
  187. case uint(xi) < uint(oldBounds.Max.X):
  188. break
  189. case xi >= oldBounds.Max.X:
  190. xi = maxX
  191. default:
  192. xi = minX
  193. }
  194. gray += int32(coeff) * int32(row[xi])
  195. sum += int32(coeff)
  196. }
  197. }
  198. offset := (y-newBounds.Min.Y)*out.Stride + (x - newBounds.Min.X)
  199. out.Pix[offset] = clampUint8(gray / sum)
  200. }
  201. }
  202. }
  203. func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []int32, offset []int, filterLength int) {
  204. oldBounds := in.Bounds()
  205. newBounds := out.Bounds()
  206. minX := oldBounds.Min.X * 2
  207. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 2
  208. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  209. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  210. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  211. var gray int64
  212. var sum int64
  213. start := offset[y]
  214. ci := (y - newBounds.Min.Y) * filterLength
  215. for i := 0; i < filterLength; i++ {
  216. coeff := coeffs[ci+i]
  217. if coeff != 0 {
  218. xi := start + i
  219. switch {
  220. case uint(xi) < uint(oldBounds.Max.X):
  221. xi *= 2
  222. case xi >= oldBounds.Max.X:
  223. xi = maxX
  224. default:
  225. xi = minX
  226. }
  227. gray += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
  228. sum += int64(coeff)
  229. }
  230. }
  231. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*2
  232. value := clampUint16(gray / sum)
  233. out.Pix[offset+0] = uint8(value >> 8)
  234. out.Pix[offset+1] = uint8(value)
  235. }
  236. }
  237. }
  238. func convertYCbCrToRGBA(in *image.YCbCr) *image.RGBA {
  239. out := image.NewRGBA(in.Bounds())
  240. for y := 0; y < out.Bounds().Dy(); y++ {
  241. for x := 0; x < out.Bounds().Dx(); x++ {
  242. p := out.Pix[y*out.Stride+4*x:]
  243. yi := in.YOffset(x, y)
  244. ci := in.COffset(x, y)
  245. r, g, b := color.YCbCrToRGB(in.Y[yi], in.Cb[ci], in.Cr[ci])
  246. p[0] = r
  247. p[1] = g
  248. p[2] = b
  249. p[3] = 0xff
  250. }
  251. }
  252. return out
  253. }