forked from disintegration/imaging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imaging.go
911 lines (737 loc) · 23.7 KB
/
imaging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
// Package imaging provides basic image manipulation functions
// (resize, rotate, flip, crop, etc.) as well as simplified image loading and saving.
//
// This package is based on the standard Go image package. All the image
// manipulation functions provided by the package take any image type that
// implements image.Image interface, and return a new image of
// *image.NRGBA type (32 bit RGBA colors, not premultiplied by alpha).
//
package imaging
import (
"fmt"
"image"
"image/color"
_ "image/gif"
"image/jpeg"
"image/png"
"math"
"os"
"path/filepath"
"strings"
)
// Open loads an image from file
func Open(filename string) (img image.Image, err error) {
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
img, _, err = image.Decode(file)
return
}
// Save saves the image to file with the specified filename.
// The format is determined from the filename extension, "jpg" (or "jpeg") and "png" are supported.
func Save(img image.Image, filename string) (err error) {
format := strings.ToLower(filepath.Ext(filename))
if format != ".jpg" && format != ".jpeg" && format != ".png" {
err = fmt.Errorf("unknown image format: %s", format)
return
}
file, err := os.Create(filename)
if err != nil {
return
}
defer file.Close()
switch format {
case ".jpg", ".jpeg":
var rgba *image.RGBA
if nrgba, ok := img.(*image.NRGBA); ok {
if nrgba.Opaque() {
rgba = &image.RGBA{
Pix: nrgba.Pix,
Stride: nrgba.Stride,
Rect: nrgba.Rect,
}
}
}
if rgba != nil {
err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: 95})
} else {
err = jpeg.Encode(file, img, &jpeg.Options{Quality: 95})
}
case ".png":
err = png.Encode(file, img)
}
return
}
// New creates a new image with the specified width and height, and fills it with the specified color.
func New(width, height int, fillColor color.Color) *image.NRGBA {
dst := image.NewNRGBA(image.Rect(0, 0, width, height))
c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
i0 := dst.PixOffset(0, 0)
for y := 0; y < height; y, i0 = y+1, i0+dst.Stride {
for x, i := 0, i0; x < width; x, i = x+1, i+4 {
dst.Pix[i+0] = c.R
dst.Pix[i+1] = c.G
dst.Pix[i+2] = c.B
dst.Pix[i+3] = c.A
}
}
return dst
}
// This function converts any image type to *image.NRGBA for faster pixel access
// Optimized for most standard image types: NRGBA64, RGBA, RGBA64, YCbCr, Gray, Gray16
// If clone is true, the new image bounds will start at (0,0), also, a new copy
// will be created even if the source image's type is already NRGBA
func toNRGBA(src image.Image, clone bool) *image.NRGBA {
if !clone {
if src0, ok := src.(*image.NRGBA); ok {
return src0
}
}
srcBounds := src.Bounds()
dstBounds := srcBounds
// if we need a copy - translate Min point to (0, 0)
if clone {
dstBounds = dstBounds.Sub(dstBounds.Min)
}
dst := image.NewNRGBA(dstBounds)
dstMinX := dstBounds.Min.X
dstMinY := dstBounds.Min.Y
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
srcMaxX := srcBounds.Max.X
srcMaxY := srcBounds.Max.Y
switch src0 := src.(type) {
case *image.NRGBA:
rowSize := srcBounds.Dx() * 4
numRows := srcBounds.Dy()
i0 := dst.PixOffset(dstMinX, dstMinY)
j0 := src0.PixOffset(srcMinX, srcMinY)
di := dst.Stride
dj := src0.Stride
for row := 0; row < numRows; row++ {
copy(dst.Pix[i0:i0+rowSize], src0.Pix[j0:j0+rowSize])
i0 += di
j0 += dj
}
case *image.NRGBA64:
i0 := dst.PixOffset(dstMinX, dstMinY)
for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
j := src0.PixOffset(x, y)
dst.Pix[i+0] = src0.Pix[j+0]
dst.Pix[i+1] = src0.Pix[j+2]
dst.Pix[i+2] = src0.Pix[j+4]
dst.Pix[i+3] = src0.Pix[j+6]
}
}
case *image.RGBA:
i0 := dst.PixOffset(dstMinX, dstMinY)
for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
j := src0.PixOffset(x, y)
a := src0.Pix[j+3]
dst.Pix[i+3] = a
switch a {
case 0:
dst.Pix[i+0] = 0
dst.Pix[i+1] = 0
dst.Pix[i+2] = 0
case 0xff:
dst.Pix[i+0] = src0.Pix[j+0]
dst.Pix[i+1] = src0.Pix[j+1]
dst.Pix[i+2] = src0.Pix[j+2]
default:
dst.Pix[i+0] = uint8(uint16(src0.Pix[j+0]) * 0xff / uint16(a))
dst.Pix[i+1] = uint8(uint16(src0.Pix[j+1]) * 0xff / uint16(a))
dst.Pix[i+2] = uint8(uint16(src0.Pix[j+2]) * 0xff / uint16(a))
}
}
}
case *image.RGBA64:
i0 := dst.PixOffset(dstMinX, dstMinY)
for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
j := src0.PixOffset(x, y)
a := src0.Pix[j+6]
dst.Pix[i+3] = a
switch a {
case 0:
dst.Pix[i+0] = 0
dst.Pix[i+1] = 0
dst.Pix[i+2] = 0
case 0xff:
dst.Pix[i+0] = src0.Pix[j+0]
dst.Pix[i+1] = src0.Pix[j+2]
dst.Pix[i+2] = src0.Pix[j+4]
default:
dst.Pix[i+0] = uint8(uint16(src0.Pix[j+0]) * 0xff / uint16(a))
dst.Pix[i+1] = uint8(uint16(src0.Pix[j+2]) * 0xff / uint16(a))
dst.Pix[i+2] = uint8(uint16(src0.Pix[j+4]) * 0xff / uint16(a))
}
}
}
case *image.Gray:
i0 := dst.PixOffset(dstMinX, dstMinY)
for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
j := src0.PixOffset(x, y)
c := src0.Pix[j]
dst.Pix[i+0] = c
dst.Pix[i+1] = c
dst.Pix[i+2] = c
dst.Pix[i+3] = 0xff
}
}
case *image.Gray16:
i0 := dst.PixOffset(dstMinX, dstMinY)
for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
j := src0.PixOffset(x, y)
c := src0.Pix[j]
dst.Pix[i+0] = c
dst.Pix[i+1] = c
dst.Pix[i+2] = c
dst.Pix[i+3] = 0xff
}
}
case *image.YCbCr:
i0 := dst.PixOffset(dstMinX, dstMinY)
for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
yj := src0.YOffset(x, y)
cj := src0.COffset(x, y)
r, g, b := color.YCbCrToRGB(src0.Y[yj], src0.Cb[cj], src0.Cr[cj])
dst.Pix[i+0] = r
dst.Pix[i+1] = g
dst.Pix[i+2] = b
dst.Pix[i+3] = 0xff
}
}
default:
i0 := dst.PixOffset(dstMinX, dstMinY)
for y := srcMinY; y < srcMaxY; y, i0 = y+1, i0+dst.Stride {
for x, i := srcMinX, i0; x < srcMaxX; x, i = x+1, i+4 {
c := color.NRGBAModel.Convert(src.At(x, y)).(color.NRGBA)
dst.Pix[i+0] = c.R
dst.Pix[i+1] = c.G
dst.Pix[i+2] = c.B
dst.Pix[i+3] = c.A
}
}
}
return dst
}
// This function is used internally to check if the image type is *image.NRGBA
// If not - converts any image type to *image.NRGBA for faster pixel access
func convertToNRGBA(img image.Image) *image.NRGBA {
// 'false' indicates that we don't need a new copy of img if it is already NRGBA
// and that the new image's bounds will be equal the bounds of the source image
return toNRGBA(img, false)
}
// Clone returns a copy of the img. New image bounds will be (0, 0)-(width, height).
func Clone(img image.Image) *image.NRGBA {
// 'true' indicates that we need a new copy of img even if it is already NRGBA
// and that the new image's bounds will start at point (0, 0)
return toNRGBA(img, true)
}
// Crop cuts out a rectangular region with the specified bounds
// from the image and returns the cropped image.
func Crop(img image.Image, rect image.Rectangle) *image.NRGBA {
src := convertToNRGBA(img)
sub := src.SubImage(rect)
return Clone(sub) // New image Bounds().Min point will be (0, 0)
}
// Crop cuts out a rectangular region with the specified size
// from the center of the image and returns the cropped image.
func CropCenter(img image.Image, width, height int) *image.NRGBA {
cropW, cropH := width, height
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
centerX := srcMinX + srcW/2
centerY := srcMinY + srcH/2
x0 := centerX - cropW/2
y0 := centerY - cropH/2
x1 := x0 + cropW
y1 := y0 + cropH
return Crop(img, image.Rect(x0, y0, x1, y1))
}
// Paste pastes the src image to the img image at the specified position and returns the combined image.
func Paste(img, src image.Image, pos image.Point) *image.NRGBA {
srcBounds := src.Bounds()
src0 := convertToNRGBA(src)
dst := Clone(img) // cloned image bounds start at (0, 0)
startPt := pos.Sub(img.Bounds().Min) // so we should translate start point
endPt := startPt.Add(srcBounds.Size())
pasteBounds := image.Rectangle{startPt, endPt}
if dst.Bounds().Overlaps(pasteBounds) {
intersectBounds := dst.Bounds().Intersect(pasteBounds)
rowSize := intersectBounds.Dx() * 4
numRows := intersectBounds.Dy()
srcStartX := intersectBounds.Min.X - pasteBounds.Min.X + srcBounds.Min.X
srcStartY := intersectBounds.Min.Y - pasteBounds.Min.Y + srcBounds.Min.Y
i0 := dst.PixOffset(intersectBounds.Min.X, intersectBounds.Min.Y)
j0 := src0.PixOffset(srcStartX, srcStartY)
di := dst.Stride
dj := src0.Stride
for row := 0; row < numRows; row++ {
copy(dst.Pix[i0:i0+rowSize], src0.Pix[j0:j0+rowSize])
i0 += di
j0 += dj
}
}
return dst
}
// Paste pastes the src image to the center of the img image and returns the combined image.
func PasteCenter(img, src image.Image) *image.NRGBA {
imgBounds := img.Bounds()
imgW := imgBounds.Dx()
imgH := imgBounds.Dy()
imgMinX := imgBounds.Min.X
imgMinY := imgBounds.Min.Y
centerX := imgMinX + imgW/2
centerY := imgMinY + imgH/2
x0 := centerX - src.Bounds().Dx()/2
y0 := centerY - src.Bounds().Dy()/2
return Paste(img, src, image.Pt(x0, y0))
}
// Overlay draws the source image over the background image at given position
// and returns the combined image. Opacity parameter is the opacity of the source
// image layer, used to compose the images, it must be from 0.0 to 1.0.
//
// Usage examples:
//
// // draw the sprite over the background at position (50, 50)
// dstImage := imaging.Overlay(backgroundImage, spriteImage, image.Pt(50, 50), 1.0)
//
// // blend two opaque images of the same size
// dstImage := imaging.Overlay(imageOne, imageTwo, image.Pt(0, 0), 0.5)
//
func Overlay(background, source image.Image, pos image.Point, opacity float64) *image.NRGBA {
opacity = math.Min(math.Max(opacity, 0.0), 1.0) // check: 0.0 <= opacity <= 1.0
src := convertToNRGBA(source)
srcBounds := src.Bounds()
dst := Clone(background) // cloned image bounds start at (0, 0)
startPt := pos.Sub(background.Bounds().Min) // so we should translate start point
endPt := startPt.Add(srcBounds.Size())
pasteBounds := image.Rectangle{startPt, endPt}
if dst.Bounds().Overlaps(pasteBounds) {
intersectBounds := dst.Bounds().Intersect(pasteBounds)
for y := intersectBounds.Min.Y; y < intersectBounds.Max.Y; y++ {
for x := intersectBounds.Min.X; x < intersectBounds.Max.X; x++ {
i := dst.PixOffset(x, y)
srcX := x - pasteBounds.Min.X + srcBounds.Min.X
srcY := y - pasteBounds.Min.Y + srcBounds.Min.Y
j := src.PixOffset(srcX, srcY)
a1 := float64(dst.Pix[i+3])
a2 := float64(src.Pix[j+3])
coef2 := opacity * a2 / 255.0
coef1 := (1 - coef2) * a1 / 255.0
coefSum := coef1 + coef2
coef1 /= coefSum
coef2 /= coefSum
dst.Pix[i+0] = uint8(float64(dst.Pix[i+0])*coef1 + float64(src.Pix[j+0])*coef2)
dst.Pix[i+1] = uint8(float64(dst.Pix[i+1])*coef1 + float64(src.Pix[j+1])*coef2)
dst.Pix[i+2] = uint8(float64(dst.Pix[i+2])*coef1 + float64(src.Pix[j+2])*coef2)
dst.Pix[i+3] = uint8(math.Min(a1+a2*opacity*(255.0-a1)/255.0, 255.0))
}
}
}
return dst
}
// Rotate90 rotates the image 90 degrees clockwise and returns the transformed image.
func Rotate90(img image.Image) *image.NRGBA {
src := convertToNRGBA(img)
srcBounds := src.Bounds()
srcMaxX := srcBounds.Max.X
srcMinY := srcBounds.Min.Y
dstW := srcBounds.Dy()
dstH := srcBounds.Dx()
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for dstY := 0; dstY < dstH; dstY++ {
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMaxX - dstY - 1
srcY := srcMinY + dstX
srcOff := src.PixOffset(srcX, srcY)
dstOff := dst.PixOffset(dstX, dstY)
dst.Pix[dstOff+0] = src.Pix[srcOff+0]
dst.Pix[dstOff+1] = src.Pix[srcOff+1]
dst.Pix[dstOff+2] = src.Pix[srcOff+2]
dst.Pix[dstOff+3] = src.Pix[srcOff+3]
}
}
return dst
}
// Rotate180 rotates the image 180 degrees clockwise and returns the transformed image.
func Rotate180(img image.Image) *image.NRGBA {
src := convertToNRGBA(img)
srcBounds := src.Bounds()
srcMaxX := srcBounds.Max.X
srcMaxY := srcBounds.Max.Y
dstW := srcBounds.Dx()
dstH := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for dstY := 0; dstY < dstH; dstY++ {
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMaxX - dstX - 1
srcY := srcMaxY - dstY - 1
srcOff := src.PixOffset(srcX, srcY)
dstOff := dst.PixOffset(dstX, dstY)
dst.Pix[dstOff+0] = src.Pix[srcOff+0]
dst.Pix[dstOff+1] = src.Pix[srcOff+1]
dst.Pix[dstOff+2] = src.Pix[srcOff+2]
dst.Pix[dstOff+3] = src.Pix[srcOff+3]
}
}
return dst
}
// Rotate270 rotates the image 270 degrees clockwise and returns the transformed image.
func Rotate270(img image.Image) *image.NRGBA {
src := convertToNRGBA(img)
srcBounds := src.Bounds()
srcMaxY := srcBounds.Max.Y
srcMinX := srcBounds.Min.X
dstW := srcBounds.Dy()
dstH := srcBounds.Dx()
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for dstY := 0; dstY < dstH; dstY++ {
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMinX + dstY
srcY := srcMaxY - dstX - 1
srcOff := src.PixOffset(srcX, srcY)
dstOff := dst.PixOffset(dstX, dstY)
dst.Pix[dstOff+0] = src.Pix[srcOff+0]
dst.Pix[dstOff+1] = src.Pix[srcOff+1]
dst.Pix[dstOff+2] = src.Pix[srcOff+2]
dst.Pix[dstOff+3] = src.Pix[srcOff+3]
}
}
return dst
}
// FlipH flips the image horizontally (from left to right) and returns the transformed image.
func FlipH(img image.Image) *image.NRGBA {
src := convertToNRGBA(img)
srcBounds := src.Bounds()
srcMaxX := srcBounds.Max.X
srcMinY := srcBounds.Min.Y
dstW := srcBounds.Dx()
dstH := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for dstY := 0; dstY < dstH; dstY++ {
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMaxX - dstX - 1
srcY := srcMinY + dstY
srcOff := src.PixOffset(srcX, srcY)
dstOff := dst.PixOffset(dstX, dstY)
dst.Pix[dstOff+0] = src.Pix[srcOff+0]
dst.Pix[dstOff+1] = src.Pix[srcOff+1]
dst.Pix[dstOff+2] = src.Pix[srcOff+2]
dst.Pix[dstOff+3] = src.Pix[srcOff+3]
}
}
return dst
}
// FlipV flips the image vertically (from top to bottom) and returns the transformed image.
func FlipV(img image.Image) *image.NRGBA {
src := convertToNRGBA(img)
srcBounds := src.Bounds()
srcMaxY := srcBounds.Max.Y
srcMinX := srcBounds.Min.X
dstW := srcBounds.Dx()
dstH := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for dstY := 0; dstY < dstH; dstY++ {
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMinX + dstX
srcY := srcMaxY - dstY - 1
srcOff := src.PixOffset(srcX, srcY)
dstOff := dst.PixOffset(dstX, dstY)
dst.Pix[dstOff+0] = src.Pix[srcOff+0]
dst.Pix[dstOff+1] = src.Pix[srcOff+1]
dst.Pix[dstOff+2] = src.Pix[srcOff+2]
dst.Pix[dstOff+3] = src.Pix[srcOff+3]
}
}
return dst
}
// Resize resizes the image to the specified width and height using the specified resampling
// filter and returns the transformed image. If one of width or height is 0, the image aspect
// ratio is preserved.
//
// Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
// CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
//
// Usage example:
//
// dstImage := imaging.Resize(srcImage, 800, 600, imaging.Lanczos)
//
func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA {
if filter.Support <= 0.0 { // nearest-neighbor special case
return resizeNearest(img, width, height)
}
dstW, dstH := width, height
if dstW < 0 || dstH < 0 {
return &image.NRGBA{}
}
if dstW == 0 && dstH == 0 {
return &image.NRGBA{}
}
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
if srcW <= 0 || srcH <= 0 {
return &image.NRGBA{}
}
// if new width or height is 0 then preserve aspect ratio, minimum 1px
if dstW == 0 {
tmpW := float64(dstH) * float64(srcW) / float64(srcH)
dstW = int(math.Max(1.0, math.Floor(tmpW+0.5)))
}
if dstH == 0 {
tmpH := float64(dstW) * float64(srcH) / float64(srcW)
dstH = int(math.Max(1.0, math.Floor(tmpH+0.5)))
}
src := convertToNRGBA(img)
var tmp, dst *image.NRGBA
// two-pass resize
if srcW != dstW {
tmp = resizeHorizontal(src, dstW, filter)
} else {
tmp = src
}
if srcH != dstH {
dst = resizeVertical(tmp, dstH, filter)
} else {
dst = tmp
}
return dst
}
func resizeHorizontal(src *image.NRGBA, width int, filter ResampleFilter) *image.NRGBA {
srcBounds := src.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
srcMaxX := srcBounds.Max.X
dstW := width
dstH := srcH
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
dX := float64(srcW) / float64(dstW)
scaleX := math.Max(dX, 1.0)
rX := math.Ceil(scaleX * filter.Support)
weights := make([]float64, int(rX+2)*2)
for dstX := 0; dstX < dstW; dstX++ {
fX := float64(srcMinX) + (float64(dstX)+0.5)*dX - 0.5
startX := int(math.Ceil(fX - rX))
if startX < srcMinX {
startX = srcMinX
}
endX := int(math.Floor(fX + rX))
if endX > srcMaxX-1 {
endX = srcMaxX - 1
}
// cache weights
weightSum := 0.0
for x := startX; x <= endX; x++ {
w := filter.Kernel((float64(x) - fX) / scaleX)
weightSum += w
weights[x-startX] = w
}
for dstY := 0; dstY < dstH; dstY++ {
srcY := srcMinY + dstY
r, g, b, a := 0.0, 0.0, 0.0, 0.0
for x := startX; x <= endX; x++ {
weight := weights[x-startX]
i := src.PixOffset(x, srcY)
r += float64(src.Pix[i+0]) * weight
g += float64(src.Pix[i+1]) * weight
b += float64(src.Pix[i+2]) * weight
a += float64(src.Pix[i+3]) * weight
}
r = math.Min(math.Max(r/weightSum, 0.0), 255.0)
g = math.Min(math.Max(g/weightSum, 0.0), 255.0)
b = math.Min(math.Max(b/weightSum, 0.0), 255.0)
a = math.Min(math.Max(a/weightSum, 0.0), 255.0)
j := dst.PixOffset(dstX, dstY)
dst.Pix[j+0] = uint8(r + 0.5)
dst.Pix[j+1] = uint8(g + 0.5)
dst.Pix[j+2] = uint8(b + 0.5)
dst.Pix[j+3] = uint8(a + 0.5)
}
}
return dst
}
func resizeVertical(src *image.NRGBA, height int, filter ResampleFilter) *image.NRGBA {
srcBounds := src.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
srcMaxY := srcBounds.Max.Y
dstW := srcW
dstH := height
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
dY := float64(srcH) / float64(dstH)
scaleY := math.Max(dY, 1.0)
rY := math.Ceil(scaleY * filter.Support)
weights := make([]float64, int(rY+2)*2)
for dstY := 0; dstY < dstH; dstY++ {
fY := float64(srcMinY) + (float64(dstY)+0.5)*dY - 0.5
startY := int(math.Ceil(fY - rY))
if startY < srcMinY {
startY = srcMinY
}
endY := int(math.Floor(fY + rY))
if endY > srcMaxY-1 {
endY = srcMaxY - 1
}
// cache weights
weightSum := 0.0
for y := startY; y <= endY; y++ {
w := filter.Kernel((float64(y) - fY) / scaleY)
weightSum += w
weights[y-startY] = w
}
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMinX + dstX
r, g, b, a := 0.0, 0.0, 0.0, 0.0
for y := startY; y <= endY; y++ {
weight := weights[y-startY]
i := src.PixOffset(srcX, y)
r += float64(src.Pix[i+0]) * weight
g += float64(src.Pix[i+1]) * weight
b += float64(src.Pix[i+2]) * weight
a += float64(src.Pix[i+3]) * weight
}
r = math.Min(math.Max(r/weightSum, 0.0), 255.0)
g = math.Min(math.Max(g/weightSum, 0.0), 255.0)
b = math.Min(math.Max(b/weightSum, 0.0), 255.0)
a = math.Min(math.Max(a/weightSum, 0.0), 255.0)
j := dst.PixOffset(dstX, dstY)
dst.Pix[j+0] = uint8(r + 0.5)
dst.Pix[j+1] = uint8(g + 0.5)
dst.Pix[j+2] = uint8(b + 0.5)
dst.Pix[j+3] = uint8(a + 0.5)
}
}
return dst
}
// fast nearest-neighbor resize, no filtering
func resizeNearest(img image.Image, width, height int) *image.NRGBA {
dstW, dstH := width, height
if dstW < 0 || dstH < 0 {
return &image.NRGBA{}
}
if dstW == 0 && dstH == 0 {
return &image.NRGBA{}
}
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
srcMaxX := srcBounds.Max.X
srcMaxY := srcBounds.Max.Y
if srcW <= 0 || srcH <= 0 {
return &image.NRGBA{}
}
// if new width or height is 0 then preserve aspect ratio, minimum 1px
if dstW == 0 {
tmpW := float64(dstH) * float64(srcW) / float64(srcH)
dstW = int(math.Max(1.0, math.Floor(tmpW+0.5)))
}
if dstH == 0 {
tmpH := float64(dstW) * float64(srcH) / float64(srcW)
dstH = int(math.Max(1.0, math.Floor(tmpH+0.5)))
}
src := convertToNRGBA(img)
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
dx := float64(srcW) / float64(dstW)
dy := float64(srcH) / float64(dstH)
for dstY := 0; dstY < dstH; dstY++ {
fy := float64(srcMinY) + (float64(dstY)+0.5)*dy - 0.5
for dstX := 0; dstX < dstW; dstX++ {
fx := float64(srcMinX) + (float64(dstX)+0.5)*dx - 0.5
srcX := int(math.Min(math.Max(math.Floor(fx+0.5), float64(srcMinX)), float64(srcMaxX)))
srcY := int(math.Min(math.Max(math.Floor(fy+0.5), float64(srcMinY)), float64(srcMaxY)))
srcOffset := src.PixOffset(srcX, srcY)
dstOffset := dst.PixOffset(dstX, dstY)
dst.Pix[dstOffset+0] = src.Pix[srcOffset+0]
dst.Pix[dstOffset+1] = src.Pix[srcOffset+1]
dst.Pix[dstOffset+2] = src.Pix[srcOffset+2]
dst.Pix[dstOffset+3] = src.Pix[srcOffset+3]
}
}
return dst
}
// Fit scales down the image using the specified resample filter to fit the specified
// maximum width and height and returns the transformed image.
//
// Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
// CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
//
// Usage example:
//
// dstImage := imaging.Fit(srcImage, 800, 600, imaging.Lanczos)
//
func Fit(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA {
maxW, maxH := width, height
if maxW <= 0 || maxH <= 0 {
return &image.NRGBA{}
}
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
if srcW <= 0 || srcH <= 0 {
return &image.NRGBA{}
}
if srcW <= maxW && srcH <= maxH {
return Clone(img)
}
srcAspectRatio := float64(srcW) / float64(srcH)
maxAspectRatio := float64(maxW) / float64(maxH)
var newW, newH int
if srcAspectRatio > maxAspectRatio {
newW = maxW
newH = int(float64(newW) / srcAspectRatio)
} else {
newH = maxH
newW = int(float64(newH) * srcAspectRatio)
}
return Resize(img, newW, newH, filter)
}
// Thumbnail scales the image up or down using the specified resample filter, crops it
// to the specified width and hight and returns the transformed image.
//
// Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
// CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
//
// Usage example:
//
// dstImage := imaging.Fit(srcImage, 100, 100, imaging.Lanczos)
//
func Thumbnail(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA {
thumbW, thumbH := width, height
if thumbW <= 0 || thumbH <= 0 {
return &image.NRGBA{}
}
srcBounds := img.Bounds()
srcW := srcBounds.Dx()
srcH := srcBounds.Dy()
if srcW <= 0 || srcH <= 0 {
return &image.NRGBA{}
}
srcAspectRatio := float64(srcW) / float64(srcH)
thumbAspectRatio := float64(thumbW) / float64(thumbH)
var tmp image.Image
if srcAspectRatio > thumbAspectRatio {
tmp = Resize(img, 0, thumbH, filter)
} else {
tmp = Resize(img, thumbW, 0, filter)
}
return CropCenter(tmp, thumbW, thumbH)
}