converter.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "image"
  16. // Keep value in [0,255] range.
  17. func clampUint8(in int32) uint8 {
  18. if in < 0 {
  19. return 0
  20. }
  21. if in > 255 {
  22. return 255
  23. }
  24. return uint8(in)
  25. }
  26. // Keep value in [0,65535] range.
  27. func clampUint16(in int64) uint16 {
  28. if in < 0 {
  29. return 0
  30. }
  31. if in > 65535 {
  32. return 65535
  33. }
  34. return uint16(in)
  35. }
  36. func resizeGeneric(in image.Image, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
  37. newBounds := out.Bounds()
  38. maxX := in.Bounds().Dx() - 1
  39. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  40. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  41. var rgba [4]int64
  42. var sum int64
  43. start := offset[y]
  44. ci := y * filterLength
  45. for i := 0; i < filterLength; i++ {
  46. coeff := coeffs[ci+i]
  47. if coeff != 0 {
  48. xi := start + i
  49. switch {
  50. case xi < 0:
  51. xi = 0
  52. case xi >= maxX:
  53. xi = maxX
  54. }
  55. r, g, b, a := in.At(xi+in.Bounds().Min.X, x+in.Bounds().Min.Y).RGBA()
  56. rgba[0] += int64(coeff) * int64(r)
  57. rgba[1] += int64(coeff) * int64(g)
  58. rgba[2] += int64(coeff) * int64(b)
  59. rgba[3] += int64(coeff) * int64(a)
  60. sum += int64(coeff)
  61. }
  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, offset []int, filterLength int) {
  80. newBounds := out.Bounds()
  81. maxX := in.Bounds().Dx() - 1
  82. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  83. row := in.Pix[x*in.Stride:]
  84. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  85. var rgba [4]int32
  86. var sum int32
  87. start := offset[y]
  88. ci := y * filterLength
  89. for i := 0; i < filterLength; i++ {
  90. coeff := coeffs[ci+i]
  91. if coeff != 0 {
  92. xi := start + i
  93. switch {
  94. case xi < 0:
  95. xi = 0
  96. case xi >= maxX:
  97. xi = 4 * maxX
  98. default:
  99. xi *= 4
  100. }
  101. rgba[0] += int32(coeff) * int32(row[xi+0])
  102. rgba[1] += int32(coeff) * int32(row[xi+1])
  103. rgba[2] += int32(coeff) * int32(row[xi+2])
  104. rgba[3] += int32(coeff) * int32(row[xi+3])
  105. sum += int32(coeff)
  106. }
  107. }
  108. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
  109. out.Pix[xo+0] = clampUint8(rgba[0] / sum)
  110. out.Pix[xo+1] = clampUint8(rgba[1] / sum)
  111. out.Pix[xo+2] = clampUint8(rgba[2] / sum)
  112. out.Pix[xo+3] = clampUint8(rgba[3] / sum)
  113. }
  114. }
  115. }
  116. func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
  117. newBounds := out.Bounds()
  118. maxX := in.Bounds().Dx() - 1
  119. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  120. row := in.Pix[x*in.Stride:]
  121. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  122. var rgba [4]int64
  123. var sum int64
  124. start := offset[y]
  125. ci := y * filterLength
  126. for i := 0; i < filterLength; i++ {
  127. coeff := coeffs[ci+i]
  128. if coeff != 0 {
  129. xi := start + i
  130. switch {
  131. case xi < 0:
  132. xi = 0
  133. case xi >= maxX:
  134. xi = 8 * maxX
  135. default:
  136. xi *= 8
  137. }
  138. rgba[0] += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
  139. rgba[1] += int64(coeff) * int64(uint16(row[xi+2])<<8|uint16(row[xi+3]))
  140. rgba[2] += int64(coeff) * int64(uint16(row[xi+4])<<8|uint16(row[xi+5]))
  141. rgba[3] += int64(coeff) * int64(uint16(row[xi+6])<<8|uint16(row[xi+7]))
  142. sum += int64(coeff)
  143. }
  144. }
  145. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
  146. value := clampUint16(rgba[0] / sum)
  147. out.Pix[xo+0] = uint8(value >> 8)
  148. out.Pix[xo+1] = uint8(value)
  149. value = clampUint16(rgba[1] / sum)
  150. out.Pix[xo+2] = uint8(value >> 8)
  151. out.Pix[xo+3] = uint8(value)
  152. value = clampUint16(rgba[2] / sum)
  153. out.Pix[xo+4] = uint8(value >> 8)
  154. out.Pix[xo+5] = uint8(value)
  155. value = clampUint16(rgba[3] / sum)
  156. out.Pix[xo+6] = uint8(value >> 8)
  157. out.Pix[xo+7] = uint8(value)
  158. }
  159. }
  160. }
  161. func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, offset []int, filterLength int) {
  162. newBounds := out.Bounds()
  163. maxX := in.Bounds().Dx() - 1
  164. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  165. row := in.Pix[(x-newBounds.Min.X)*in.Stride:]
  166. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  167. var gray int32
  168. var sum int32
  169. start := offset[y]
  170. ci := y * filterLength
  171. for i := 0; i < filterLength; i++ {
  172. coeff := coeffs[ci+i]
  173. if coeff != 0 {
  174. xi := start + i
  175. switch {
  176. case xi < 0:
  177. xi = 0
  178. case xi >= maxX:
  179. xi = maxX
  180. }
  181. gray += int32(coeff) * int32(row[xi])
  182. sum += int32(coeff)
  183. }
  184. }
  185. offset := (y-newBounds.Min.Y)*out.Stride + (x - newBounds.Min.X)
  186. out.Pix[offset] = clampUint8(gray / sum)
  187. }
  188. }
  189. }
  190. func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []int32, offset []int, filterLength int) {
  191. newBounds := out.Bounds()
  192. maxX := in.Bounds().Dx() - 1
  193. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  194. row := in.Pix[x*in.Stride:]
  195. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  196. var gray int64
  197. var sum int64
  198. start := offset[y]
  199. ci := y * filterLength
  200. for i := 0; i < filterLength; i++ {
  201. coeff := coeffs[ci+i]
  202. if coeff != 0 {
  203. xi := start + i
  204. switch {
  205. case xi < 0:
  206. xi = 0
  207. case xi >= maxX:
  208. xi = 2 * maxX
  209. default:
  210. xi *= 2
  211. }
  212. gray += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
  213. sum += int64(coeff)
  214. }
  215. }
  216. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*2
  217. value := clampUint16(gray / sum)
  218. out.Pix[offset+0] = uint8(value >> 8)
  219. out.Pix[offset+1] = uint8(value)
  220. }
  221. }
  222. }
  223. func resizeYCbCr(in *ycc, out *ycc, scale float64, coeffs []int16, offset []int, filterLength int) {
  224. newBounds := out.Bounds()
  225. maxX := in.Bounds().Dx() - 1
  226. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  227. row := in.Pix[x*in.Stride:]
  228. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  229. var p [3]int32
  230. var sum int32
  231. start := offset[y]
  232. ci := y * filterLength
  233. for i := 0; i < filterLength; i++ {
  234. coeff := coeffs[ci+i]
  235. if coeff != 0 {
  236. xi := start + i
  237. switch {
  238. case xi < 0:
  239. xi = 0
  240. case xi >= maxX:
  241. xi = 3 * maxX
  242. default:
  243. xi *= 3
  244. }
  245. p[0] += int32(coeff) * int32(row[xi+0])
  246. p[1] += int32(coeff) * int32(row[xi+1])
  247. p[2] += int32(coeff) * int32(row[xi+2])
  248. sum += int32(coeff)
  249. }
  250. }
  251. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
  252. out.Pix[xo+0] = clampUint8(p[0] / sum)
  253. out.Pix[xo+1] = clampUint8(p[1] / sum)
  254. out.Pix[xo+2] = clampUint8(p[2] / sum)
  255. }
  256. }
  257. }
  258. func nearestYCbCr(in *ycc, out *ycc, scale float64, coeffs []bool, offset []int, filterLength int) {
  259. newBounds := out.Bounds()
  260. maxX := in.Bounds().Dx() - 1
  261. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  262. row := in.Pix[x*in.Stride:]
  263. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  264. var p [3]float32
  265. var sum float32
  266. start := offset[y]
  267. ci := y * filterLength
  268. for i := 0; i < filterLength; i++ {
  269. if coeffs[ci+i] {
  270. xi := start + i
  271. switch {
  272. case xi < 0:
  273. xi = 0
  274. case xi >= maxX:
  275. xi = 3 * maxX
  276. default:
  277. xi *= 3
  278. }
  279. p[0] += float32(row[xi+0])
  280. p[1] += float32(row[xi+1])
  281. p[2] += float32(row[xi+2])
  282. sum++
  283. }
  284. }
  285. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
  286. out.Pix[xo+0] = floatToUint8(p[0] / sum)
  287. out.Pix[xo+1] = floatToUint8(p[1] / sum)
  288. out.Pix[xo+2] = floatToUint8(p[2] / sum)
  289. }
  290. }
  291. }