nearest.go 8.7 KB

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