converter.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. oldBounds := in.Bounds()
  38. newBounds := out.Bounds()
  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 - newBounds.Min.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 uint(xi) < uint(oldBounds.Max.X):
  51. break
  52. case xi >= oldBounds.Max.X:
  53. xi = oldBounds.Min.X
  54. default:
  55. xi = oldBounds.Max.X - 1
  56. }
  57. r, g, b, a := in.At(xi, x).RGBA()
  58. rgba[0] += int64(coeff) * int64(r)
  59. rgba[1] += int64(coeff) * int64(g)
  60. rgba[2] += int64(coeff) * int64(b)
  61. rgba[3] += int64(coeff) * int64(a)
  62. sum += int64(coeff)
  63. }
  64. }
  65. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
  66. value := clampUint16(rgba[0] / sum)
  67. out.Pix[offset+0] = uint8(value >> 8)
  68. out.Pix[offset+1] = uint8(value)
  69. value = clampUint16(rgba[1] / sum)
  70. out.Pix[offset+2] = uint8(value >> 8)
  71. out.Pix[offset+3] = uint8(value)
  72. value = clampUint16(rgba[2] / sum)
  73. out.Pix[offset+4] = uint8(value >> 8)
  74. out.Pix[offset+5] = uint8(value)
  75. value = clampUint16(rgba[3] / sum)
  76. out.Pix[offset+6] = uint8(value >> 8)
  77. out.Pix[offset+7] = uint8(value)
  78. }
  79. }
  80. }
  81. func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) {
  82. oldBounds := in.Bounds()
  83. newBounds := out.Bounds()
  84. minX := oldBounds.Min.X * 4
  85. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 4
  86. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  87. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  88. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  89. var rgba [4]int32
  90. var sum int32
  91. start := offset[y]
  92. ci := (y - newBounds.Min.Y) * filterLength
  93. for i := 0; i < filterLength; i++ {
  94. coeff := coeffs[ci+i]
  95. if coeff != 0 {
  96. xi := start + i
  97. switch {
  98. case uint(xi) < uint(oldBounds.Max.X):
  99. xi *= 4
  100. case xi >= oldBounds.Max.X:
  101. xi = maxX
  102. default:
  103. xi = minX
  104. }
  105. rgba[0] += int32(coeff) * int32(row[xi+0])
  106. rgba[1] += int32(coeff) * int32(row[xi+1])
  107. rgba[2] += int32(coeff) * int32(row[xi+2])
  108. rgba[3] += int32(coeff) * int32(row[xi+3])
  109. sum += int32(coeff)
  110. }
  111. }
  112. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*4
  113. out.Pix[xo+0] = clampUint8(rgba[0] / sum)
  114. out.Pix[xo+1] = clampUint8(rgba[1] / sum)
  115. out.Pix[xo+2] = clampUint8(rgba[2] / sum)
  116. out.Pix[xo+3] = clampUint8(rgba[3] / sum)
  117. }
  118. }
  119. }
  120. func resizeRGBA64(in *image.RGBA64, out *image.RGBA64, scale float64, coeffs []int32, offset []int, filterLength int) {
  121. oldBounds := in.Bounds()
  122. newBounds := out.Bounds()
  123. minX := oldBounds.Min.X * 8
  124. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 8
  125. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  126. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  127. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  128. var rgba [4]int64
  129. var sum int64
  130. start := offset[y]
  131. ci := (y - newBounds.Min.Y) * filterLength
  132. for i := 0; i < filterLength; i++ {
  133. coeff := coeffs[ci+i]
  134. if coeff != 0 {
  135. xi := start + i
  136. switch {
  137. case uint(xi) < uint(oldBounds.Max.X):
  138. xi *= 8
  139. case xi >= oldBounds.Max.X:
  140. xi = maxX
  141. default:
  142. xi = minX
  143. }
  144. rgba[0] += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
  145. rgba[1] += int64(coeff) * int64(uint16(row[xi+2])<<8|uint16(row[xi+3]))
  146. rgba[2] += int64(coeff) * int64(uint16(row[xi+4])<<8|uint16(row[xi+5]))
  147. rgba[3] += int64(coeff) * int64(uint16(row[xi+6])<<8|uint16(row[xi+7]))
  148. sum += int64(coeff)
  149. }
  150. }
  151. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*8
  152. value := clampUint16(rgba[0] / sum)
  153. out.Pix[xo+0] = uint8(value >> 8)
  154. out.Pix[xo+1] = uint8(value)
  155. value = clampUint16(rgba[1] / sum)
  156. out.Pix[xo+2] = uint8(value >> 8)
  157. out.Pix[xo+3] = uint8(value)
  158. value = clampUint16(rgba[2] / sum)
  159. out.Pix[xo+4] = uint8(value >> 8)
  160. out.Pix[xo+5] = uint8(value)
  161. value = clampUint16(rgba[3] / sum)
  162. out.Pix[xo+6] = uint8(value >> 8)
  163. out.Pix[xo+7] = uint8(value)
  164. }
  165. }
  166. }
  167. func resizeGray(in *image.Gray, out *image.Gray, scale float64, coeffs []int16, offset []int, filterLength int) {
  168. oldBounds := in.Bounds()
  169. newBounds := out.Bounds()
  170. minX := oldBounds.Min.X
  171. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1)
  172. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  173. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  174. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  175. var gray int32
  176. var sum int32
  177. start := offset[y]
  178. ci := (y - newBounds.Min.Y) * filterLength
  179. for i := 0; i < filterLength; i++ {
  180. coeff := coeffs[ci+i]
  181. if coeff != 0 {
  182. xi := start + i
  183. switch {
  184. case uint(xi) < uint(oldBounds.Max.X):
  185. break
  186. case xi >= oldBounds.Max.X:
  187. xi = maxX
  188. default:
  189. xi = minX
  190. }
  191. gray += int32(coeff) * int32(row[xi])
  192. sum += int32(coeff)
  193. }
  194. }
  195. offset := (y-newBounds.Min.Y)*out.Stride + (x - newBounds.Min.X)
  196. out.Pix[offset] = clampUint8(gray / sum)
  197. }
  198. }
  199. }
  200. func resizeGray16(in *image.Gray16, out *image.Gray16, scale float64, coeffs []int32, offset []int, filterLength int) {
  201. oldBounds := in.Bounds()
  202. newBounds := out.Bounds()
  203. minX := oldBounds.Min.X * 2
  204. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 2
  205. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  206. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  207. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  208. var gray int64
  209. var sum int64
  210. start := offset[y]
  211. ci := (y - newBounds.Min.Y) * filterLength
  212. for i := 0; i < filterLength; i++ {
  213. coeff := coeffs[ci+i]
  214. if coeff != 0 {
  215. xi := start + i
  216. switch {
  217. case uint(xi) < uint(oldBounds.Max.X):
  218. xi *= 2
  219. case xi >= oldBounds.Max.X:
  220. xi = maxX
  221. default:
  222. xi = minX
  223. }
  224. gray += int64(coeff) * int64(uint16(row[xi+0])<<8|uint16(row[xi+1]))
  225. sum += int64(coeff)
  226. }
  227. }
  228. offset := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*2
  229. value := clampUint16(gray / sum)
  230. out.Pix[offset+0] = uint8(value >> 8)
  231. out.Pix[offset+1] = uint8(value)
  232. }
  233. }
  234. }
  235. func resizeYCbCr(in *ycc, out *ycc, scale float64, coeffs []int16, offset []int, filterLength int) {
  236. oldBounds := in.Bounds()
  237. newBounds := out.Bounds()
  238. minX := oldBounds.Min.X * 3
  239. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 3
  240. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  241. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  242. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  243. var p [3]int32
  244. var sum int32
  245. start := offset[y]
  246. ci := (y - newBounds.Min.Y) * filterLength
  247. for i := 0; i < filterLength; i++ {
  248. coeff := coeffs[ci+i]
  249. if coeff != 0 {
  250. xi := start + i
  251. switch {
  252. case uint(xi) < uint(oldBounds.Max.X):
  253. xi *= 3
  254. case xi >= oldBounds.Max.X:
  255. xi = maxX
  256. default:
  257. xi = minX
  258. }
  259. p[0] += int32(coeff) * int32(row[xi+0])
  260. p[1] += int32(coeff) * int32(row[xi+1])
  261. p[2] += int32(coeff) * int32(row[xi+2])
  262. sum += int32(coeff)
  263. }
  264. }
  265. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
  266. out.Pix[xo+0] = clampUint8(p[0] / sum)
  267. out.Pix[xo+1] = clampUint8(p[1] / sum)
  268. out.Pix[xo+2] = clampUint8(p[2] / sum)
  269. }
  270. }
  271. }
  272. func nearestYCbCr(in *ycc, out *ycc, scale float64, coeffs []bool, offset []int, filterLength int) {
  273. oldBounds := in.Bounds()
  274. newBounds := out.Bounds()
  275. minX := oldBounds.Min.X * 3
  276. maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 3
  277. for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
  278. row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
  279. for y := newBounds.Min.Y; y < newBounds.Max.Y; y++ {
  280. var p [3]float32
  281. var sum float32
  282. start := offset[y]
  283. ci := (y - newBounds.Min.Y) * filterLength
  284. for i := 0; i < filterLength; i++ {
  285. if coeffs[ci+i] {
  286. xi := start + i
  287. switch {
  288. case uint(xi) < uint(oldBounds.Max.X):
  289. xi *= 3
  290. case xi >= oldBounds.Max.X:
  291. xi = maxX
  292. default:
  293. xi = minX
  294. }
  295. p[0] += float32(row[xi+0])
  296. p[1] += float32(row[xi+1])
  297. p[2] += float32(row[xi+2])
  298. sum++
  299. }
  300. }
  301. xo := (y-newBounds.Min.Y)*out.Stride + (x-newBounds.Min.X)*3
  302. out.Pix[xo+0] = floatToUint8(p[0] / sum)
  303. out.Pix[xo+1] = floatToUint8(p[1] / sum)
  304. out.Pix[xo+2] = floatToUint8(p[2] / sum)
  305. }
  306. }
  307. }