forked from janelia-flyem/dvid
/
image.go
1232 lines (1130 loc) · 32.3 KB
/
image.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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file supports image operations in DVID. Images can act as containers for elements that
can have a number of values per element.
Standard images are convenient ways to transmit simple data types in 2d/3d arrays because
clients have good implementations of reading and writing them. For example, javascript web clients
can easily GET compressed images. The Janelia Raveler program used PNG images to hold 24+ bit labels
so some of their use was through legacy applications.
DVID supports packaging of data into standard images to some extent. Once the data being stored per
pixel/voxel becomes sufficiently complex, it makes no sense to force the data into a standard image
container. At that point, a generic binary container, e.g., schema + binary data, or a standard like
HDF5 should be used. HDF5 suffers from complexity and the lack of support within javascript clients.
*/
package dvid
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"io/ioutil"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"github.com/janelia-flyem/go/go.image/bmp"
"github.com/janelia-flyem/go/go.image/tiff"
"github.com/janelia-flyem/go/freetype-go/freetype"
"github.com/janelia-flyem/go/freetype-go/freetype/raster"
"github.com/janelia-flyem/go/freetype-go/freetype/truetype"
)
var (
Font *truetype.Font
)
func init() {
// Need to register types that will be used to fulfill interfaces.
gob.Register(&Image{})
gob.Register(&image.Gray{})
gob.Register(&image.Gray16{})
gob.Register(&image.NRGBA{})
gob.Register(&image.NRGBA64{})
// Initialize font from inlined ttf font data
var err error
Font, err = freetype.ParseFont(fontBytes)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to create font from font bytes!")
}
}
// DefaultJPEGQuality is the quality of images returned if requesting JPEG images
// and an explicit Quality amount is omitted.
const DefaultJPEGQuality = 80
// Image contains a standard Go image as well as a data format description so non-standard
// image values like uint64 labels or uint32 intensities can be handled. A DVID image also
// knows whether it should be interpolated on resizing or must keep pixel values without
// interpolation, e.g., when using labels. Better Gob serialization is handled by a union of
// possible image types compared to a generic image.Image interface:
// see https://groups.google.com/d/msg/golang-dev/_t4pqoeuflE/DbqSf41wr5EJ
type Image struct {
DataFormat DataValues
Interpolable bool
Which uint8
Gray *image.Gray
Gray16 *image.Gray16
NRGBA *image.NRGBA
NRGBA64 *image.NRGBA64
}
// Get returns an image.Image from the union struct.
func (img Image) Get() image.Image {
switch img.Which {
case 0:
return img.Gray
case 1:
return img.Gray16
case 2:
return img.NRGBA
case 3:
return img.NRGBA64
default:
return nil
}
}
// Get returns an image.Image from the union struct.
func (img Image) GetDrawable() draw.Image {
switch img.Which {
case 0:
return img.Gray
case 1:
return img.Gray16
case 2:
return img.NRGBA
case 3:
return img.NRGBA64
default:
return nil
}
}
// GetPNG returns bytes in PNG format.
func (img Image) GetPNG() ([]byte, error) {
var goImg image.Image
switch img.Which {
case 0:
goImg = img.Gray
case 1:
goImg = img.Gray16
case 2:
goImg = img.NRGBA
case 3:
goImg = img.NRGBA64
default:
return nil, fmt.Errorf("Unknown image type %d in GetPng()", img.Which)
}
var buffer bytes.Buffer
if err := png.Encode(&buffer, goImg); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// GetJPEG returns bytes in JPEG format where quality is 1-100, higher is better,
// and quality 0 is default (50)
func (img Image) GetJPEG(quality int) ([]byte, error) {
if quality == 0 { // default
quality = 50
}
var goImg image.Image
switch img.Which {
case 0:
goImg = img.Gray
case 1:
goImg = img.Gray16
case 2:
goImg = img.NRGBA
case 3:
goImg = img.NRGBA64
default:
return nil, fmt.Errorf("Unknown image type %d in GetJPEG()", img.Which)
}
var buffer bytes.Buffer
if err := jpeg.Encode(&buffer, goImg, &jpeg.Options{quality}); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// Set initializes a DVID image from a go image and a data format specification. DVID images
// must have identical data type values within a pixel..
func (img *Image) Set(src image.Image, format DataValues, interpolable bool) error {
img.DataFormat = format
img.Interpolable = interpolable
valuesPerElement := format.ValuesPerElement()
bytesPerValue, err := format.BytesPerValue()
if err != nil {
return err
}
switch s := src.(type) {
case *image.Gray:
img.Which = 0
img.Gray = s
if valuesPerElement != 1 || bytesPerValue != 1 {
return fmt.Errorf("Tried to use image.Gray (8-bit) to represent %d values of %d bytes/value",
valuesPerElement, bytesPerValue)
}
case *image.Gray16:
img.Which = 1
img.Gray16 = s
if valuesPerElement != 1 || bytesPerValue != 2 {
return fmt.Errorf("Tried to use image.Gray16 (16-bit) to represent %d values of %d bytes/value",
valuesPerElement, bytesPerValue)
}
case *image.NRGBA:
img.Which = 2
img.NRGBA = s
if !((valuesPerElement == 1 && bytesPerValue == 4) || (valuesPerElement == 4 && bytesPerValue == 1)) {
return fmt.Errorf("Tried to use image.NRGBA (32-bit) to represent %d values of %d bytes/value",
valuesPerElement, bytesPerValue)
}
case *image.NRGBA64:
img.Which = 3
img.NRGBA64 = s
if !((valuesPerElement == 1 && bytesPerValue == 8) || (valuesPerElement == 4 && bytesPerValue == 2)) {
return fmt.Errorf("Tried to use image.NRGBA64 (64-bit) to represent %d values of %d bytes/value",
valuesPerElement, bytesPerValue)
}
default:
return fmt.Errorf("No valid image type received by image.Set(): %s", reflect.TypeOf(src))
}
return nil
}
// Data returns a slice of bytes corresponding to the image pixels.
func (img *Image) Data() []uint8 {
switch img.Which {
case 0:
return img.Gray.Pix
case 1:
return img.Gray16.Pix
case 2:
return img.NRGBA.Pix
case 3:
return img.NRGBA64.Pix
default:
return nil
}
}
// SubImage returns an image representing the portion of the image p visible through r.
// The returned image shares pixels with the original image.
func (img *Image) SubImage(r image.Rectangle) (*Image, error) {
result := new(Image)
result.DataFormat = img.DataFormat
result.Which = img.Which
switch img.Which {
case 0:
result.Gray = img.Gray.SubImage(r).(*image.Gray)
case 1:
result.Gray16 = img.Gray16.SubImage(r).(*image.Gray16)
case 2:
result.NRGBA = img.NRGBA.SubImage(r).(*image.NRGBA)
case 3:
result.NRGBA64 = img.NRGBA64.SubImage(r).(*image.NRGBA64)
default:
return nil, fmt.Errorf("Unsupported image type %d asked for SubImage()", img.Which)
}
return result, nil
}
// Serialize writes optional compressed and checksummed bytes representing image data.
func (img *Image) Serialize(compress Compression, checksum Checksum) ([]byte, error) {
b, err := img.MarshalBinary()
if err != nil {
return nil, err
}
return SerializeData(b, compress, checksum)
}
// Deserialze deserializes an Image from a possibly compressioned, checksummed byte slice.
func (img *Image) Deserialize(b []byte) error {
if img == nil {
return fmt.Errorf("Error attempting to deserialize into nil Image")
}
// Unpackage the data using compression and built-in checksum.
data, _, err := DeserializeData(b, true)
if err != nil {
return err
}
return img.UnmarshalBinary(data)
}
// MarshalBinary fulfills the encoding.BinaryMarshaler interface.
func (img *Image) MarshalBinary() ([]byte, error) {
var buffer bytes.Buffer
// Serialize the data format
b, err := img.DataFormat.MarshalBinary()
if err != nil {
return nil, err
}
if err := binary.Write(&buffer, binary.LittleEndian, uint32(len(b))); err != nil {
return nil, err
}
_, err = buffer.Write(b)
// Serialize the image portion.
err = buffer.WriteByte(byte(img.Which))
if err != nil {
return nil, err
}
var stride, bytesPerPixel int
var rect image.Rectangle
var pix, src []uint8
var pixOffset func(x, y int) int
switch img.Which {
case 0:
stride = img.Gray.Stride
rect = img.Gray.Rect
bytesPerPixel = 1
src = img.Gray.Pix
pixOffset = img.Gray.PixOffset
case 1:
stride = img.Gray16.Stride
rect = img.Gray16.Rect
bytesPerPixel = 2
src = img.Gray16.Pix
pixOffset = img.Gray16.PixOffset
case 2:
stride = img.NRGBA.Stride
rect = img.NRGBA.Rect
bytesPerPixel = 4
src = img.NRGBA.Pix
pixOffset = img.NRGBA.PixOffset
case 3:
stride = img.NRGBA64.Stride
rect = img.NRGBA64.Rect
bytesPerPixel = 8
src = img.NRGBA64.Pix
pixOffset = img.NRGBA64.PixOffset
}
// Make sure the byte slice is compact and not harboring any offsets
if stride == bytesPerPixel*rect.Dx() && rect.Min.X == 0 && rect.Min.Y == 0 {
pix = src
} else {
dx := rect.Dx()
dy := rect.Dy()
rowbytes := bytesPerPixel * dx
totbytes := rowbytes * dy
pix = make([]uint8, totbytes)
dstI := 0
for y := rect.Min.Y; y < rect.Max.Y; y++ {
srcI := pixOffset(rect.Min.X, y)
copy(pix[dstI:dstI+rowbytes], src[srcI:srcI+rowbytes])
dstI += rowbytes
}
stride = rowbytes
rect = image.Rect(0, 0, dx, dy)
}
if err := binary.Write(&buffer, binary.LittleEndian, int32(stride)); err != nil {
return nil, err
}
if err := binary.Write(&buffer, binary.LittleEndian, int32(rect.Dx())); err != nil {
return nil, err
}
if err := binary.Write(&buffer, binary.LittleEndian, int32(rect.Dy())); err != nil {
return nil, err
}
_, err = buffer.Write(pix)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// UnmarshalBinary fulfills the encoding.BinaryUnmarshaler interface.
func (img *Image) UnmarshalBinary(b []byte) error {
// Deserialize the data format
lenDataFormat := int(binary.LittleEndian.Uint32(b[0:4]))
img.DataFormat.UnmarshalBinary(b[4 : 4+lenDataFormat])
// Get the image type.
buffer := bytes.NewBuffer(b[4+lenDataFormat:])
imageType, err := buffer.ReadByte()
if err != nil {
return err
}
img.Which = uint8(imageType)
// Get the stride and sizes.
var stride int32
if err = binary.Read(buffer, binary.LittleEndian, &stride); err != nil {
return err
}
var dx, dy int32
err = binary.Read(buffer, binary.LittleEndian, &dx)
if err != nil {
return err
}
err = binary.Read(buffer, binary.LittleEndian, &dy)
if err != nil {
return err
}
rect := image.Rect(0, 0, int(dx), int(dy))
pix := []uint8(buffer.Bytes())
switch img.Which {
case 0:
img.Gray = &image.Gray{
Stride: int(stride),
Rect: rect,
Pix: pix,
}
case 1:
img.Gray16 = &image.Gray16{
Stride: int(stride),
Rect: rect,
Pix: pix,
}
case 2:
img.NRGBA = &image.NRGBA{
Stride: int(stride),
Rect: rect,
Pix: pix,
}
case 3:
img.NRGBA64 = &image.NRGBA64{
Stride: int(stride),
Rect: rect,
Pix: pix,
}
}
return nil
}
// ScaleImage scales a DVID image to the destination geometry size, using nearest-neighbor or
// interpolation depending on the type of data.
func (img *Image) ScaleImage(dstW, dstH int) (*Image, error) {
var goImg image.Image
var err error
if img.Interpolable {
goImg, err = img.InterpolateImage(dstW, dstH)
} else {
goImg, err = img.ResizeImage(dstW, dstH)
}
if err != nil {
return nil, err
}
dst := new(Image)
if err = dst.Set(goImg, img.DataFormat, img.Interpolable); err != nil {
return nil, err
}
return dst, nil
}
// ResizeImage returns an image scaled to the given geometry without doing
// interpolation.
func (img *Image) ResizeImage(dstW, dstH int) (image.Image, error) {
if img == nil {
return nil, fmt.Errorf("Attempted to resize nil DVID image.")
}
if dstW <= 0 || dstH <= 0 {
return nil, fmt.Errorf("Attempted to resize to %d x %d pixels", dstW, dstH)
}
// Get dimensions
src := img.Get()
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
if srcW == dstW && srcH == dstH {
return src, nil
}
if srcW <= 0 || srcH <= 0 {
return nil, fmt.Errorf("Attempted to resize source image of %d x %d pixels", srcW, srcH)
}
// Perform interpolation based on # values and bytes/value.
valuesPerElement := img.DataFormat.ValuesPerElement()
bytesPerValue, err := img.DataFormat.BytesPerValue()
if err != nil {
return nil, err
}
switch valuesPerElement {
case 1:
switch bytesPerValue {
case 1:
return resize1x8(img.Gray, dstW, dstH), nil
case 2:
return resize1x16(img.Gray16, dstW, dstH), nil
case 4:
return resize32(img.NRGBA, dstW, dstH), nil
case 8:
return resize64(img.NRGBA64, dstW, dstH), nil
}
case 4:
switch bytesPerValue {
case 1:
return resize32(img.NRGBA, dstW, dstH), nil
case 2:
return resize64(img.NRGBA64, dstW, dstH), nil
}
}
return nil, fmt.Errorf("Illegal image format for interpolation: %d values with %d bytes/value",
valuesPerElement, bytesPerValue)
}
func resize1x8(src *image.Gray, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
dstW64, dstH64 := uint64(dstW), uint64(dstH)
srcW64, srcH64 := uint64(srcW), uint64(srcH)
dst := image.NewGray(image.Rect(0, 0, dstW, dstH))
var x, y uint64
dstI := 0
for y = 0; y < dstH64; y++ {
srcY := int(y * srcH64 / dstH64)
for x = 0; x < dstW64; x++ {
srcX := int(x * srcW64 / dstW64)
dst.Pix[dstI] = src.Pix[srcY*srcW+srcX]
dstI++
}
}
return dst
}
func resize1x16(src *image.Gray16, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
dstW64, dstH64 := uint64(dstW), uint64(dstH)
srcW64, srcH64 := uint64(srcW), uint64(srcH)
dst := image.NewGray16(image.Rect(0, 0, dstW, dstH))
var x, y uint64
dstI := 0
for y = 0; y < dstH64; y++ {
srcY := int(y * srcH64 / dstH64)
for x = 0; x < dstW64; x++ {
srcX := int(x * srcW64 / dstW64)
srcI := 2 * (srcY*srcW + srcX)
copy(dst.Pix[dstI:dstI+2], src.Pix[srcI:srcI+2])
dstI += 2
}
}
return dst
}
func resize32(src *image.NRGBA, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
dstW64, dstH64 := uint64(dstW), uint64(dstH)
srcW64, srcH64 := uint64(srcW), uint64(srcH)
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
var x, y uint64
dstI := 0
for y = 0; y < dstH64; y++ {
srcY := int(y * srcH64 / dstH64)
for x = 0; x < dstW64; x++ {
srcX := int(x * srcW64 / dstW64)
srcI := 4 * (srcY*srcW + srcX)
copy(dst.Pix[dstI:dstI+4], src.Pix[srcI:srcI+4])
dstI += 4
}
}
return dst
}
func resize64(src *image.NRGBA64, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
dstW64, dstH64 := uint64(dstW), uint64(dstH)
srcW64, srcH64 := uint64(srcW), uint64(srcH)
dst := image.NewNRGBA64(image.Rect(0, 0, dstW, dstH))
var x, y uint64
dstI := 0
for y = 0; y < dstH64; y++ {
srcY := int(y * srcH64 / dstH64)
for x = 0; x < dstW64; x++ {
srcX := int(x * srcW64 / dstW64)
srcI := 8 * (srcY*srcW + srcX)
copy(dst.Pix[dstI:dstI+8], src.Pix[srcI:srcI+8])
dstI += 8
}
}
return dst
}
// InterpolateImage returns an image scaled to the given geometry using simple
// nearest-neighbor interpolation.
func (img *Image) InterpolateImage(dstW, dstH int) (image.Image, error) {
if img == nil {
return nil, fmt.Errorf("Attempted to interpolate nil DVID image.")
}
if dstW <= 0 || dstH <= 0 {
return nil, fmt.Errorf("Attempted to interpolate to %d x %d pixels", dstW, dstH)
}
// Get dimensions
src := img.Get()
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
if srcW == dstW && srcH == dstH {
return src, nil
}
if srcW <= 0 || srcH <= 0 {
return nil, fmt.Errorf("Attempted to interpolate source image of %d x %d pixels", srcW, srcH)
}
// Perform interpolation based on # values and bytes/value.
valuesPerElement := img.DataFormat.ValuesPerElement()
bytesPerValue, err := img.DataFormat.BytesPerValue()
if err != nil {
return nil, err
}
switch valuesPerElement {
case 1:
switch bytesPerValue {
case 1:
return interpolate1x8(img.Gray, dstW, dstH), nil
case 2:
return interpolate1x16(img.Gray16, dstW, dstH), nil
case 4:
return interpolate1x32(img.NRGBA, dstW, dstH), nil
case 8:
return interpolate1x64(img.NRGBA64, dstW, dstH), nil
}
case 4:
switch bytesPerValue {
case 1:
return interpolate4x8(img.NRGBA, dstW, dstH), nil
case 2:
return interpolate4x16(img.NRGBA64, dstW, dstH), nil
}
}
return nil, fmt.Errorf("Illegal image format for interpolation: %d values with %d bytes/value",
valuesPerElement, bytesPerValue)
}
// The interpolate code below was adapted from the AppEngine moustachio example and should be among
// the more efficient resizers without resorting to more sophisticated interpolation than
// nearest-neighbor.
//
// http://code.google.com/p/appengine-go/source/browse/example/moustachio/resize/resize.go
//
// The scaling algorithm is to nearest-neighbor magnify the dx * dy source
// to a (ww*dx) * (hh*dy) intermediate image and then minify the intermediate
// image back down to a ww * hh destination with a simple box filter.
// The intermediate image is implied, we do not physically allocate a slice
// of length ww*dx*hh*dy.
// For example, consider a 4*3 source image. Label its pixels from a-l:
// abcd
// efgh
// ijkl
// To resize this to a 3*2 destination image, the intermediate is 12*6.
// Whitespace has been added to delineate the destination pixels:
// aaab bbcc cddd
// aaab bbcc cddd
// eeef ffgg ghhh
//
// eeef ffgg ghhh
// iiij jjkk klll
// iiij jjkk klll
// Thus, the 'b' source pixel contributes one third of its value to the
// (0, 0) destination pixel and two thirds to (1, 0).
// The implementation is a two-step process. First, the source pixels are
// iterated over and each source pixel's contribution to 1 or more
// destination pixels are summed. Second, the sums are divided by a scaling
// factor to yield the destination pixels.
// TODO: By interleaving the two steps, instead of doing all of
// step 1 first and all of step 2 second, we could allocate a smaller sum
// slice of length 4*w*2 instead of 4*w*h, although the resultant code
// would become more complicated.
//
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Interpolate uint8/pixel images.
func interpolate1x8(src *image.Gray, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
ww, hh := uint64(dstW), uint64(dstH)
dx, dy := uint64(srcW), uint64(srcH)
n, sum := dx*dy, make([]uint64, dstW*dstH)
for y := 0; y < srcH; y++ {
pixOffset := src.PixOffset(0, y)
for x := 0; x < srcW; x++ {
// Get the source pixel.
val64 := uint64(src.Pix[pixOffset])
pixOffset++
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := (py/dy)*ww + (px / dx)
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {
qx = remx
}
qxy := qx * qy
sum[index] += val64 * qxy
index++
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
dst := image.NewGray(image.Rect(0, 0, dstW, dstH))
index := 0
for y := 0; y < dstH; y++ {
pixOffset := dst.PixOffset(0, y)
for x := 0; x < dstW; x++ {
dst.Pix[pixOffset] = uint8(sum[index] / n)
pixOffset++
index++
}
}
return dst
}
// Interpolate uint16/pixel images.
func interpolate1x16(src *image.Gray16, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
ww, hh := uint64(dstW), uint64(dstH)
dx, dy := uint64(srcW), uint64(srcH)
n, sum := dx*dy, make([]uint64, dstW*dstH)
for y := 0; y < srcH; y++ {
pixOffset := src.PixOffset(0, y)
for x := 0; x < srcW; x++ {
// Get the source pixel.
val64 := uint64(binary.BigEndian.Uint16([]byte(src.Pix[pixOffset+0 : pixOffset+2])))
pixOffset += 2
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := (py/dy)*ww + (px / dx)
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {
qx = remx
}
qxy := qx * qy
sum[index] += val64 * qxy
index++
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
dst := image.NewGray16(image.Rect(0, 0, dstW, dstH))
index := 0
for y := 0; y < dstH; y++ {
pixOffset := dst.PixOffset(0, y)
for x := 0; x < dstW; x++ {
binary.BigEndian.PutUint16(dst.Pix[pixOffset+0:pixOffset+2], uint16(sum[index]/n))
pixOffset += 2
index++
}
}
return dst
}
// Interpolate uint32/pixel images.
func interpolate1x32(src *image.NRGBA, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
ww, hh := uint64(dstW), uint64(dstH)
dx, dy := uint64(srcW), uint64(srcH)
n, sum := dx*dy, make([]uint64, dstW*dstH)
for y := 0; y < srcH; y++ {
pixOffset := src.PixOffset(0, y)
for x := 0; x < srcW; x++ {
// Get the source pixel.
val64 := uint64(binary.BigEndian.Uint32([]byte(src.Pix[pixOffset+0 : pixOffset+4])))
pixOffset += 4
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := (py/dy)*ww + (px / dx)
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {
qx = remx
}
qxy := qx * qy
sum[index] += val64 * qxy
index++
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
index := 0
for y := 0; y < dstH; y++ {
pixOffset := dst.PixOffset(0, y)
for x := 0; x < dstW; x++ {
binary.BigEndian.PutUint32(dst.Pix[pixOffset+0:pixOffset+4], uint32(sum[index]/n))
pixOffset += 4
index++
}
}
return dst
}
// Interpolate uint64/pixel images.
func interpolate1x64(src *image.NRGBA64, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
ww, hh := uint64(dstW), uint64(dstH)
dx, dy := uint64(srcW), uint64(srcH)
n, sum := dx*dy, make([]uint64, dstW*dstH)
for y := 0; y < srcH; y++ {
pixOffset := src.PixOffset(0, y)
for x := 0; x < srcW; x++ {
// Get the source pixel.
val64 := binary.BigEndian.Uint64([]byte(src.Pix[pixOffset+0 : pixOffset+8]))
pixOffset += 8
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := (py/dy)*ww + (px / dx)
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {
qx = remx
}
qxy := qx * qy
sum[index] += val64 * qxy
index++
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
dst := image.NewNRGBA64(image.Rect(0, 0, dstW, dstH))
index := 0
for y := 0; y < dstH; y++ {
pixOffset := dst.PixOffset(0, y)
for x := 0; x < dstW; x++ {
binary.BigEndian.PutUint64(dst.Pix[pixOffset+0:pixOffset+8], sum[index]/n)
pixOffset += 8
index++
}
}
return dst
}
// Interpolate 4 interleaved uint8 per pixel images.
func interpolate4x8(src *image.NRGBA, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
ww, hh := uint64(dstW), uint64(dstH)
dx, dy := uint64(srcW), uint64(srcH)
n, sum := dx*dy, make([]uint64, 4*dstW*dstH)
for y := 0; y < srcH; y++ {
pixOffset := src.PixOffset(0, y)
for x := 0; x < srcW; x++ {
// Get the source pixel.
r64 := uint64(src.Pix[pixOffset+0])
g64 := uint64(src.Pix[pixOffset+1])
b64 := uint64(src.Pix[pixOffset+2])
a64 := uint64(src.Pix[pixOffset+3])
pixOffset += 4
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := 4 * ((py/dy)*ww + (px / dx))
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {
qx = remx
}
qxy := qx * qy
sum[index+0] += r64 * qxy
sum[index+1] += g64 * qxy
sum[index+2] += b64 * qxy
sum[index+3] += a64 * qxy
index += 4
px += qx
remx -= qx
}
py += qy
remy -= qy
}
}
}
dst := image.NewNRGBA(image.Rect(0, 0, dstW, dstH))
for y := 0; y < dstH; y++ {
pixOffset := dst.PixOffset(0, y)
for x := 0; x < dstW; x++ {
dst.Pix[pixOffset+0] = uint8(sum[pixOffset+0] / n)
dst.Pix[pixOffset+1] = uint8(sum[pixOffset+1] / n)
dst.Pix[pixOffset+2] = uint8(sum[pixOffset+2] / n)
dst.Pix[pixOffset+3] = uint8(sum[pixOffset+3] / n)
pixOffset += 4
}
}
return dst
}
// Interpolate 4 interleaved uint16 per pixel images.
func interpolate4x16(src *image.NRGBA64, dstW, dstH int) image.Image {
srcRect := src.Bounds()
srcW := srcRect.Dx()
srcH := srcRect.Dy()
ww, hh := uint64(dstW), uint64(dstH)
dx, dy := uint64(srcW), uint64(srcH)
n, sum := dx*dy, make([]uint64, 4*dstW*dstH)
for y := 0; y < srcH; y++ {
pixOffset := src.PixOffset(0, y)
for x := 0; x < srcW; x++ {
// Get the source pixel.
r64 := uint64(binary.BigEndian.Uint16([]byte(src.Pix[pixOffset+0 : pixOffset+2])))
g64 := uint64(binary.BigEndian.Uint16([]byte(src.Pix[pixOffset+2 : pixOffset+4])))
b64 := uint64(binary.BigEndian.Uint16([]byte(src.Pix[pixOffset+4 : pixOffset+6])))
a64 := uint64(binary.BigEndian.Uint16([]byte(src.Pix[pixOffset+6 : pixOffset+8])))
pixOffset += 8
// Spread the source pixel over 1 or more destination rows.
py := uint64(y) * hh
for remy := hh; remy > 0; {
qy := dy - (py % dy)
if qy > remy {
qy = remy
}
// Spread the source pixel over 1 or more destination columns.
px := uint64(x) * ww
index := 4 * ((py/dy)*ww + (px / dx))
for remx := ww; remx > 0; {
qx := dx - (px % dx)
if qx > remx {