-
-
Notifications
You must be signed in to change notification settings - Fork 382
/
processor.go
790 lines (710 loc) · 19.6 KB
/
processor.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
package caire
import (
_ "embed"
"errors"
"fmt"
"image"
"image/color"
"image/color/palette"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"io"
"math"
"os"
"path/filepath"
"strings"
"github.com/disintegration/imaging"
"github.com/esimov/caire/utils"
pigo "github.com/esimov/pigo/core"
"golang.org/x/image/bmp"
)
//go:embed data/facefinder
var cascadeFile []byte
var (
g *gif.GIF
rCount int
)
var (
resizeXY = false // the image is resized both vertically and horizontally
isGif = false
imgWorker = make(chan worker) // channel used to transfer the image to the GUI
errs = make(chan error)
)
// worker struct contains all the information needed for transferring the resized image to the Gio GUI.
type worker struct {
carver *Carver
img *image.NRGBA
debug *image.NRGBA
done bool
}
// SeamCarver interface defines the Resize method.
// This needs to be implemented by every struct which declares a Resize method.
type SeamCarver interface {
Resize(*image.NRGBA) (image.Image, error)
}
// shrinkFn is a generic function used to shrink an image.
type shrinkFn func(*Carver, *image.NRGBA) (*image.NRGBA, error)
// enlargeFn is a generic function used to enlarge an image.
type enlargeFn func(*Carver, *image.NRGBA) (*image.NRGBA, error)
// Processor options
type Processor struct {
SobelThreshold int
BlurRadius int
NewWidth int
NewHeight int
Percentage bool
Square bool
Debug bool
Preview bool
FaceDetect bool
ShapeType string
SeamColor string
MaskPath string
RMaskPath string
Mask *image.NRGBA
RMask *image.NRGBA
GuiDebug *image.NRGBA
FaceAngle float64
FaceDetector *pigo.Pigo
Spinner *utils.Spinner
vRes bool
}
var (
shrinkHorizFn shrinkFn
shrinkVertFn shrinkFn
enlargeHorizFn enlargeFn
enlargeVertFn enlargeFn
)
// resize implements the Resize method of the Carver interface.
// It returns the concrete resize operation method.
func resize(s SeamCarver, img *image.NRGBA) (image.Image, error) {
return s.Resize(img)
}
// Resize is the main entry point for the image resize operation.
// The new image can be resized either horizontally or vertically (or both).
// Depending on the provided options the image can be either reduced or enlarged.
func (p *Processor) Resize(img *image.NRGBA) (image.Image, error) {
var c = NewCarver(img.Bounds().Dx(), img.Bounds().Dy())
var (
newImg image.Image
newWidth int
newHeight int
pw, ph int
err error
)
rCount = 0
if p.NewWidth > c.Width {
newWidth = p.NewWidth - (p.NewWidth - (p.NewWidth - c.Width))
} else {
newWidth = c.Width - (c.Width - (c.Width - p.NewWidth))
}
if p.NewHeight > c.Height {
newHeight = p.NewHeight - (p.NewHeight - (p.NewHeight - c.Height))
} else {
newHeight = c.Height - (c.Height - (c.Height - p.NewHeight))
}
if p.NewWidth == 0 {
newWidth = p.NewWidth
}
if p.NewHeight == 0 {
newHeight = p.NewHeight
}
// shrinkHorizFn calls itself recursively to shrink the image horizontally.
// If the image is resized on both X and Y axis it calls the shrink and enlarge
// function intermittently up until the desired dimension is reached.
// We are opting for this solution instead of resizing the image sequentially,
// because this way the horizontal and vertical seams are merged together seamlessly.
shrinkHorizFn = func(c *Carver, img *image.NRGBA) (*image.NRGBA, error) {
p.vRes = false
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
if dx > p.NewWidth {
img, err = p.shrink(c, img)
if err != nil {
return nil, err
}
if p.NewHeight > 0 && p.NewHeight != dy {
if p.NewHeight <= dy {
img, err = shrinkVertFn(c, img)
if err != nil {
return nil, err
}
} else {
img, err = enlargeVertFn(c, img)
if err != nil {
return nil, err
}
}
} else {
img, err = shrinkHorizFn(c, img)
if err != nil {
return nil, err
}
}
}
rCount++
return img, nil
}
// enlargeHorizFn calls itself recursively to enlarge the image horizontally.
enlargeHorizFn = func(c *Carver, img *image.NRGBA) (*image.NRGBA, error) {
p.vRes = false
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
if dx < p.NewWidth {
img, err = p.enlarge(c, img)
if err != nil {
return nil, err
}
if p.NewHeight > 0 && p.NewHeight != dy {
if p.NewHeight <= dy {
img, err = shrinkVertFn(c, img)
if err != nil {
return nil, err
}
} else {
img, err = enlargeVertFn(c, img)
if err != nil {
return nil, err
}
}
} else {
img, err = enlargeHorizFn(c, img)
if err != nil {
return nil, err
}
}
}
rCount++
return img, nil
}
// shrinkVertFn calls itself recursively to shrink the image vertically.
shrinkVertFn = func(c *Carver, img *image.NRGBA) (*image.NRGBA, error) {
p.vRes = true
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
// If the image is resized both horizontally and vertically we need
// to rotate the image each time we are invoking the shrink function.
// Otherwise we rotate the image only once, right before calling this function.
if resizeXY {
dx, dy = img.Bounds().Dy(), img.Bounds().Dx()
img = c.RotateImage90(img)
}
if dx > p.NewHeight {
img, err = p.shrink(c, img)
if err != nil {
return nil, err
}
if resizeXY {
img = c.RotateImage270(img)
}
if p.NewWidth > 0 && p.NewWidth != dy {
if p.NewWidth <= dy {
img, err = shrinkHorizFn(c, img)
if err != nil {
return nil, err
}
} else {
img, err = enlargeHorizFn(c, img)
if err != nil {
return nil, err
}
}
} else {
img, err = shrinkVertFn(c, img)
if err != nil {
return nil, err
}
}
} else {
if resizeXY {
img = c.RotateImage270(img)
}
}
rCount++
return img, nil
}
// enlargeVertFn calls itself recursively to enlarge the image vertically.
enlargeVertFn = func(c *Carver, img *image.NRGBA) (*image.NRGBA, error) {
p.vRes = true
dx, dy := img.Bounds().Dx(), img.Bounds().Dy()
if resizeXY {
dx, dy = img.Bounds().Dy(), img.Bounds().Dx()
img = c.RotateImage90(img)
}
if dx < p.NewHeight {
img, err = p.enlarge(c, img)
if err != nil {
return nil, err
}
if resizeXY {
img = c.RotateImage270(img)
}
if p.NewWidth > 0 && p.NewWidth != dy {
if p.NewWidth <= dy {
img, err = shrinkHorizFn(c, img)
if err != nil {
return nil, err
}
} else {
img, err = enlargeHorizFn(c, img)
if err != nil {
return nil, err
}
}
} else {
img, err = enlargeVertFn(c, img)
if err != nil {
return nil, err
}
}
} else {
if resizeXY {
img = c.RotateImage270(img)
}
}
rCount++
return img, nil
}
if p.Percentage || p.Square {
pw = c.Width - c.Height
ph = c.Height - c.Width
// In case pw and ph is zero, it means that the target image is square.
// In this case we can simply resize the image without running the carving operation.
if p.Percentage && pw == 0 && ph == 0 {
pw = c.Width - int(float64(c.Width)-(float64(p.NewWidth)/100*float64(c.Width)))
ph = c.Height - int(float64(c.Height)-(float64(p.NewHeight)/100*float64(c.Height)))
p.NewWidth = utils.Abs(c.Width - pw)
p.NewHeight = utils.Abs(c.Height - ph)
resImgSize := utils.Min(p.NewWidth, p.NewHeight)
return imaging.Resize(img, resImgSize, 0, imaging.Lanczos), nil
}
// When the square option is used the image will be resized to a square based on the shortest edge.
if p.Square {
// Calling the image rescale method only when both a new width and height is provided.
if p.NewWidth != 0 && p.NewHeight != 0 {
p.NewWidth = utils.Min(p.NewWidth, p.NewHeight)
p.NewHeight = p.NewWidth
newImg = p.calculateFitness(img, c)
dst := image.NewNRGBA(newImg.Bounds())
draw.Draw(dst, newImg.Bounds(), newImg, image.Point{}, draw.Src)
img = dst
nw, nh := img.Bounds().Dx(), img.Bounds().Dy()
p.NewWidth = utils.Min(nw, nh)
p.NewHeight = p.NewWidth
} else {
return nil, errors.New("please provide a new WIDTH and HEIGHT when using the square option")
}
}
// Use the Percentage flag only for shrinking the image.
if p.Percentage {
// Calculate the new image size based on the provided percentage.
pw = c.Width - int(float64(c.Width)-(float64(p.NewWidth)/100*float64(c.Width)))
ph = c.Height - int(float64(c.Height)-(float64(p.NewHeight)/100*float64(c.Height)))
if p.NewWidth != 0 {
p.NewWidth = utils.Abs(c.Width - pw)
}
if p.NewHeight != 0 {
p.NewHeight = utils.Abs(c.Height - ph)
}
if pw >= c.Width || ph >= c.Height {
return nil, errors.New("cannot use the percentage flag for image enlargement")
}
}
}
// Rescale the image when it is resized both horizontally and vertically.
// First the image is scaled down or up by preserving the image aspect ratio,
// then the seam carving algorithm is applied only to the remaining pixels.
// Scale the width and height by the smaller factor (i.e Min(wScaleFactor, hScaleFactor))
// Example: input: 5000x2500, scale: 2160x1080, final target: 1920x1080
if (c.Width > p.NewWidth && c.Height > p.NewHeight) &&
(p.NewWidth != 0 && p.NewHeight != 0) {
newImg = p.calculateFitness(img, c)
dx0, dy0 := img.Bounds().Max.X, img.Bounds().Max.Y
dx1, dy1 := newImg.Bounds().Max.X, newImg.Bounds().Max.Y
// Rescale the image when the new image width or height are preserved, otherwise
// it might happen, that the generated image size does not match with the requested image size.
if !((p.NewWidth == 0 && dx0 == dx1) || (p.NewHeight == 0 && dy0 == dy1)) {
dst := image.NewNRGBA(newImg.Bounds())
draw.Draw(dst, newImg.Bounds(), newImg, image.Point{}, draw.Src)
img = dst
}
}
// Run the carver function if the desired image width is not identical with the rescaled image width.
if newWidth > 0 && p.NewWidth != c.Width {
if p.NewWidth > c.Width {
img, err = enlargeHorizFn(c, img)
if err != nil {
return nil, err
}
} else {
img, err = shrinkHorizFn(c, img)
if err != nil {
return nil, err
}
}
}
// Run the carver function if the desired image height is not identical with the rescaled image height.
if newHeight > 0 && p.NewHeight != c.Height {
if !resizeXY {
img = c.RotateImage90(img)
if len(p.MaskPath) > 0 {
p.Mask = c.RotateImage90(p.Mask)
}
if len(p.RMaskPath) > 0 {
p.RMask = c.RotateImage90(p.RMask)
}
}
if p.NewHeight > c.Height {
img, err = enlargeVertFn(c, img)
if err != nil {
return nil, err
}
} else {
img, err = shrinkVertFn(c, img)
if err != nil {
return nil, err
}
}
if !resizeXY {
img = c.RotateImage270(img)
if len(p.MaskPath) > 0 {
p.Mask = c.RotateImage270(p.Mask)
}
if len(p.RMaskPath) > 0 {
p.RMask = c.RotateImage270(p.RMask)
}
}
}
// Signal that the process is done and no more data is sent through the channel.
go func() {
imgWorker <- worker{
carver: nil,
img: nil,
done: true,
}
}()
return img, nil
}
// calculateFitness iteratively try to find the best image aspect ratio for the rescale.
func (p *Processor) calculateFitness(img *image.NRGBA, c *Carver) *image.NRGBA {
var (
w = float64(c.Width)
h = float64(c.Height)
nw = float64(p.NewWidth)
nh = float64(p.NewHeight)
newImg *image.NRGBA
)
wsf := w / nw
hsf := h / nh
sw := math.Round(w / math.Min(wsf, hsf))
sh := math.Round(h / math.Min(wsf, hsf))
if sw <= sh {
newImg = imaging.Resize(img, 0, int(sw), imaging.Lanczos)
if len(p.MaskPath) > 0 {
p.Mask = imaging.Resize(p.Mask, 0, int(sw), imaging.Lanczos)
}
if len(p.RMaskPath) > 0 {
p.RMask = imaging.Resize(p.RMask, 0, int(sw), imaging.Lanczos)
}
} else {
newImg = imaging.Resize(img, 0, int(sh), imaging.Lanczos)
if len(p.MaskPath) > 0 {
p.Mask = imaging.Resize(p.Mask, 0, int(sh), imaging.Lanczos)
}
if len(p.RMaskPath) > 0 {
p.RMask = imaging.Resize(p.RMask, 0, int(sh), imaging.Lanczos)
}
}
dx, dy := newImg.Bounds().Max.X, newImg.Bounds().Max.Y
c.Width = dx
c.Height = dy
if int(sw) < p.NewWidth || int(sh) < p.NewHeight {
newImg = p.calculateFitness(newImg, c)
}
return newImg
}
// Process encodes the resized image into an io.Writer interface.
// We are using the io package, since we can provide different input and output types,
// as long as they implement the io.Reader and io.Writer interface.
func (p *Processor) Process(r io.Reader, w io.Writer) error {
var err error
if p.FaceDetect {
// Instantiate a new Pigo object in case the face detection option is used.
p.FaceDetector = pigo.NewPigo()
// Unpack the binary file. This will return the number of cascade trees,
// the tree depth, the threshold and the prediction from tree's leaf nodes.
p.FaceDetector, err = p.FaceDetector.Unpack(cascadeFile)
if err != nil {
return fmt.Errorf("error unpacking the cascade file: %v", err)
}
}
if p.NewWidth != 0 && p.NewHeight != 0 {
resizeXY = true
}
src, _, err := image.Decode(r)
if err != nil {
return err
}
img := p.imgToNRGBA(src)
p.GuiDebug = image.NewNRGBA(img.Bounds())
if len(p.MaskPath) > 0 {
mf, err := os.Open(p.MaskPath)
if err != nil {
return fmt.Errorf("could not open the mask file: %v", err)
}
ctype, err := utils.DetectContentType(mf.Name())
if err != nil {
return err
}
if !strings.Contains(ctype.(string), "image") {
return fmt.Errorf("the mask should be an image file")
}
mask, _, err := image.Decode(mf)
if err != nil {
return fmt.Errorf("could not decode the mask file: %v", err)
}
p.Mask = p.Dither(p.imgToNRGBA(mask))
p.GuiDebug = p.Mask
}
if len(p.RMaskPath) > 0 {
rmf, err := os.Open(p.RMaskPath)
if err != nil {
return fmt.Errorf("could not open the mask file: %v", err)
}
ctype, err := utils.DetectContentType(rmf.Name())
if err != nil {
return err
}
if !strings.Contains(ctype.(string), "image") {
return fmt.Errorf("the mask should be an image file")
}
rmask, _, err := image.Decode(rmf)
if err != nil {
return fmt.Errorf("could not decode the mask file: %v", err)
}
p.RMask = p.Dither(p.imgToNRGBA(rmask))
p.GuiDebug = p.RMask
}
if p.Preview {
guiWidth := img.Bounds().Max.X
guiHeight := img.Bounds().Max.Y
if p.NewWidth > guiWidth {
guiWidth = p.NewWidth
}
if p.NewHeight > guiHeight {
guiHeight = p.NewHeight
}
if resizeXY {
guiWidth = 1024
guiHeight = 640
}
guiParams := struct {
width int
height int
}{
width: guiWidth,
height: guiHeight,
}
// Lunch Gio GUI thread.
go p.showPreview(imgWorker, errs, guiParams)
}
switch w := w.(type) {
case *os.File:
ext := filepath.Ext(w.Name())
switch ext {
case "", ".jpg", ".jpeg":
res, err := resize(p, img)
if err != nil {
return err
}
return jpeg.Encode(w, res, &jpeg.Options{Quality: 100})
case ".png":
res, err := resize(p, img)
if err != nil {
return err
}
return png.Encode(w, res)
case ".bmp":
res, err := resize(p, img)
if err != nil {
return err
}
return bmp.Encode(w, res)
case ".gif":
g = new(gif.GIF)
isGif = true
_, err := resize(p, img)
if err != nil {
return err
}
return writeGifToFile(w.Name(), g)
default:
return errors.New("unsupported image format")
}
default:
res, err := resize(p, img)
if err != nil {
return err
}
return jpeg.Encode(w, res, &jpeg.Options{Quality: 100})
}
}
// shrink reduces the image dimension either horizontally or vertically.
func (p *Processor) shrink(c *Carver, img *image.NRGBA) (*image.NRGBA, error) {
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
c = NewCarver(width, height)
if _, err := c.ComputeSeams(p, img); err != nil {
return nil, err
}
seams := c.FindLowestEnergySeams(p)
img = c.RemoveSeam(img, seams, p.Debug)
if len(p.MaskPath) > 0 {
p.Mask = c.RemoveSeam(p.Mask, seams, false)
draw.Draw(p.GuiDebug, img.Bounds(), p.Mask, image.Point{}, draw.Over)
}
if len(p.RMaskPath) > 0 {
p.RMask = c.RemoveSeam(p.RMask, seams, false)
draw.Draw(p.GuiDebug, img.Bounds(), p.RMask, image.Point{}, draw.Over)
}
if isGif {
p.encodeImgToGif(c, img, g)
}
go func() {
select {
case imgWorker <- worker{
carver: c,
img: img,
debug: p.GuiDebug,
done: false,
}:
case <-errs:
return
}
}()
return img, nil
}
// enlarge increases the image dimension either horizontally or vertically.
func (p *Processor) enlarge(c *Carver, img *image.NRGBA) (*image.NRGBA, error) {
width, height := img.Bounds().Max.X, img.Bounds().Max.Y
c = NewCarver(width, height)
if _, err := c.ComputeSeams(p, img); err != nil {
return nil, err
}
seams := c.FindLowestEnergySeams(p)
img = c.AddSeam(img, seams, p.Debug)
if len(p.MaskPath) > 0 {
p.Mask = c.AddSeam(p.Mask, seams, false)
p.GuiDebug = p.Mask
}
if len(p.RMaskPath) > 0 {
p.RMask = c.AddSeam(p.RMask, seams, false)
p.GuiDebug = p.RMask
}
if isGif {
p.encodeImgToGif(c, img, g)
}
go func() {
select {
case imgWorker <- worker{
carver: c,
img: img,
debug: p.GuiDebug,
done: false,
}:
case <-errs:
return
}
}()
return img, nil
}
// imgToNRGBA converts any image type to *image.NRGBA with min-point at (0, 0).
func (p *Processor) imgToNRGBA(img image.Image) *image.NRGBA {
srcBounds := img.Bounds()
if srcBounds.Min.X == 0 && srcBounds.Min.Y == 0 {
if src0, ok := img.(*image.NRGBA); ok {
return src0
}
}
srcMinX := srcBounds.Min.X
srcMinY := srcBounds.Min.Y
dstBounds := srcBounds.Sub(srcBounds.Min)
dstW := dstBounds.Dx()
dstH := dstBounds.Dy()
dst := image.NewNRGBA(dstBounds)
switch src := img.(type) {
case *image.NRGBA:
rowSize := srcBounds.Dx() * 4
for dstY := 0; dstY < dstH; dstY++ {
di := dst.PixOffset(0, dstY)
si := src.PixOffset(srcMinX, srcMinY+dstY)
for dstX := 0; dstX < dstW; dstX++ {
copy(dst.Pix[di:di+rowSize], src.Pix[si:si+rowSize])
}
}
case *image.YCbCr:
for dstY := 0; dstY < dstH; dstY++ {
di := dst.PixOffset(0, dstY)
for dstX := 0; dstX < dstW; dstX++ {
srcX := srcMinX + dstX
srcY := srcMinY + dstY
siy := src.YOffset(srcX, srcY)
sic := src.COffset(srcX, srcY)
r, g, b := color.YCbCrToRGB(src.Y[siy], src.Cb[sic], src.Cr[sic])
dst.Pix[di+0] = r
dst.Pix[di+1] = g
dst.Pix[di+2] = b
dst.Pix[di+3] = 0xff
di += 4
}
}
default:
for dstY := 0; dstY < dstH; dstY++ {
di := dst.PixOffset(0, dstY)
for dstX := 0; dstX < dstW; dstX++ {
c := color.NRGBAModel.Convert(img.At(srcMinX+dstX, srcMinY+dstY)).(color.NRGBA)
dst.Pix[di+0] = c.R
dst.Pix[di+1] = c.G
dst.Pix[di+2] = c.B
dst.Pix[di+3] = c.A
di += 4
}
}
}
return dst
}
// encodeImgToGif encodes the provided image to a Gif file.
func (p *Processor) encodeImgToGif(c *Carver, src image.Image, g *gif.GIF) {
dx, dy := src.Bounds().Max.X, src.Bounds().Max.Y
dst := image.NewPaletted(image.Rect(0, 0, dx, dy), palette.Plan9)
if p.NewHeight != 0 {
dst = image.NewPaletted(image.Rect(0, 0, dy, dx), palette.Plan9)
}
if p.NewWidth > dx {
dx += rCount
g.Config.Width = dst.Bounds().Max.X + 1
g.Config.Height = dst.Bounds().Max.Y + 1
} else {
dx -= rCount
}
if p.NewHeight > dx {
dx += rCount
g.Config.Width = dst.Bounds().Max.X + 1
g.Config.Height = dst.Bounds().Max.Y + 1
} else {
dx -= rCount
}
if p.NewHeight != 0 {
src = c.RotateImage270(src.(*image.NRGBA))
}
draw.Draw(dst, src.Bounds(), src, image.Point{}, draw.Src)
g.Image = append(g.Image, dst)
g.Delay = append(g.Delay, 0)
}
// writeGifToFile writes the encoded Gif file to the destination file.
func writeGifToFile(path string, g *gif.GIF) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return gif.EncodeAll(f, g)
}