Skip to content

Commit

Permalink
draw: add fast paths for RGBA64Image
Browse files Browse the repository at this point in the history
Go 1.17 added image.RGBA64Image and draw.RGBA64Image interfaces which
use color.RGBA64 instead of color.Color in order to avoid heap
allocations on pixel operations. Fast paths were added to image/draw
for drawing with these images, but not to x/image/draw. This CL
adds them.

Fixes golang/go#62423

goos: windows
goarch: amd64
pkg: golang.org/x/image/draw
cpu: AMD Ryzen 9 7900X 12-Core Processor
                        │  old.bench   │              new.bench              │
                        │    sec/op    │   sec/op     vs base                │
SimpleScaleCopy-24         7.425µ ± 0%   7.581µ ± 0%   +2.10% (p=0.000 n=20)
SimpleTransformCopy-24     7.473µ ± 1%   7.678µ ± 1%   +2.74% (p=0.000 n=20)
SimpleTransformScale-24    440.7µ ± 0%   442.6µ ± 0%   +0.44% (p=0.012 n=20)
ScaleNNLargeDown-24        155.3µ ± 4%   156.9µ ± 2%        ~ (p=0.495 n=20)
ScaleABLargeDown-24        843.4µ ± 0%   850.7µ ± 1%   +0.87% (p=0.000 n=20)
ScaleBLLargeDown-24        102.7m ± 0%   102.7m ± 1%        ~ (p=0.779 n=20)
ScaleCRLargeDown-24        165.6m ± 0%   168.7m ± 1%   +1.84% (p=0.000 n=20)
ScaleNNDown-24             42.53µ ± 1%   43.00µ ± 1%   +1.10% (p=0.000 n=20)
ScaleABDown-24             149.3µ ± 0%   151.5µ ± 1%   +1.52% (p=0.000 n=20)
ScaleBLDown-24             1.347m ± 1%   1.367m ± 1%   +1.43% (p=0.000 n=20)
ScaleCRDown-24             2.635m ± 1%   2.663m ± 1%   +1.09% (p=0.007 n=20)
ScaleNNUp-24               2.108m ± 0%   2.128m ± 2%   +0.93% (p=0.023 n=20)
ScaleABUp-24               7.493m ± 0%   7.594m ± 1%   +1.35% (p=0.000 n=20)
ScaleBLUp-24               6.105m ± 3%   6.410m ± 3%   +5.01% (p=0.001 n=20)
ScaleCRUp-24              10.297m ± 9%   9.640m ± 3%        ~ (p=0.121 n=20)
ScaleNNSrcRGBA-24          111.3µ ± 1%   108.6µ ± 2%   -2.37% (p=0.000 n=20)
ScaleNNSrcUnif-24          1.410µ ± 1%   1.397µ ± 1%        ~ (p=0.103 n=20)
ScaleNNOverRGBA-24         174.0µ ± 1%   170.5µ ± 2%   -1.98% (p=0.004 n=20)
ScaleNNOverUnif-24         90.17µ ± 1%   90.12µ ± 1%        ~ (p=0.490 n=20)
TformNNSrcRGBA-24          103.4µ ± 1%   105.8µ ± 1%   +2.25% (p=0.000 n=20)
TformNNSrcUnif-24          38.61µ ± 1%   40.80µ ± 1%   +5.69% (p=0.000 n=20)
TformNNOverRGBA-24         150.3µ ± 0%   155.5µ ± 2%   +3.49% (p=0.000 n=20)
TformNNOverUnif-24         35.16µ ± 2%   34.75µ ± 2%   -1.18% (p=0.015 n=20)
ScaleABSrcGray-24          154.5µ ± 2%   151.8µ ± 1%   -1.69% (p=0.006 n=20)
ScaleABSrcNRGBA-24         482.0µ ± 1%   475.9µ ± 1%   -1.26% (p=0.002 n=20)
ScaleABSrcRGBA-24          418.2µ ± 0%   416.0µ ± 1%        ~ (p=0.108 n=20)
ScaleABSrcYCbCr-24         849.4µ ± 0%   845.6µ ± 1%   -0.45% (p=0.015 n=20)
ScaleABSrcRGBA64-24       1616.6µ ± 2%   467.8µ ± 2%  -71.06% (p=0.000 n=20)
ScaleABOverGray-24         152.0µ ± 1%   148.2µ ± 1%   -2.51% (p=0.000 n=20)
ScaleABOverNRGBA-24        511.8µ ± 1%   510.2µ ± 1%        ~ (p=0.640 n=20)
ScaleABOverRGBA-24         480.1µ ± 1%   475.5µ ± 0%   -0.94% (p=0.000 n=20)
ScaleABOverYCbCr-24        861.1µ ± 1%   843.2µ ± 0%   -2.08% (p=0.000 n=20)
ScaleABOverRGBA64-24      1723.2µ ± 2%   538.0µ ± 0%  -68.78% (p=0.000 n=20)
TformABSrcGray-24          148.6µ ± 1%   142.6µ ± 0%   -4.01% (p=0.000 n=20)
TformABSrcNRGBA-24         363.3µ ± 2%   356.2µ ± 0%   -1.95% (p=0.000 n=20)
TformABSrcRGBA-24          301.0µ ± 0%   296.5µ ± 0%   -1.49% (p=0.000 n=20)
TformABSrcYCbCr-24         415.1µ ± 0%   409.1µ ± 0%   -1.45% (p=0.000 n=20)
TformABSrcRGBA64-24       1068.9µ ± 2%   337.7µ ± 1%  -68.41% (p=0.000 n=20)
TformABOverGray-24         146.0µ ± 1%   143.2µ ± 0%   -1.88% (p=0.000 n=20)
TformABOverNRGBA-24        394.6µ ± 1%   389.0µ ± 0%   -1.43% (p=0.000 n=20)
TformABOverRGBA-24         341.0µ ± 1%   338.1µ ± 0%   -0.83% (p=0.001 n=20)
TformABOverYCbCr-24        414.4µ ± 1%   410.6µ ± 1%   -0.91% (p=0.049 n=20)
TformABOverRGBA64-24      1108.7µ ± 2%   389.4µ ± 1%  -64.88% (p=0.000 n=20)
ScaleCRSrcGray-24          4.065m ± 2%   3.979m ± 0%   -2.12% (p=0.000 n=20)
ScaleCRSrcNRGBA-24         13.66m ± 1%   13.41m ± 1%   -1.79% (p=0.007 n=20)
ScaleCRSrcRGBA-24          9.258m ± 3%   9.091m ± 1%   -1.80% (p=0.002 n=20)
ScaleCRSrcYCbCr-24         23.53m ± 0%   22.85m ± 1%   -2.89% (p=0.000 n=20)
ScaleCRSrcRGBA64-24        42.76m ± 2%   13.13m ± 1%  -69.31% (p=0.000 n=20)
ScaleCROverGray-24         4.110m ± 1%   3.973m ± 1%   -3.33% (p=0.000 n=20)
ScaleCROverNRGBA-24        14.74m ± 1%   13.50m ± 0%   -8.41% (p=0.000 n=20)
ScaleCROverRGBA-24         9.504m ± 1%   9.301m ± 1%   -2.14% (p=0.000 n=20)
ScaleCROverYCbCr-24        23.42m ± 1%   22.86m ± 0%   -2.38% (p=0.000 n=20)
ScaleCROverRGBA64-24       43.47m ± 1%   13.07m ± 1%  -69.93% (p=0.000 n=20)
TformCRSrcGray-24          1.253m ± 1%   1.225m ± 0%   -2.24% (p=0.000 n=20)
TformCRSrcNRGBA-24         2.141m ± 2%   2.050m ± 1%   -4.24% (p=0.000 n=20)
TformCRSrcRGBA-24          1.810m ± 1%   1.771m ± 0%   -2.15% (p=0.002 n=20)
TformCRSrcYCbCr-24         2.404m ± 1%   2.403m ± 1%        ~ (p=0.698 n=20)
TformCRSrcRGBA64-24        5.150m ± 1%   2.197m ± 1%  -57.34% (p=0.000 n=20)
TformCROverGray-24         1.251m ± 0%   1.223m ± 0%   -2.23% (p=0.000 n=20)
TformCROverNRGBA-24        2.081m ± 1%   2.037m ± 0%   -2.13% (p=0.000 n=20)
TformCROverRGBA-24         1.809m ± 1%   1.793m ± 2%        ~ (p=0.149 n=20)
TformCROverYCbCr-24        2.444m ± 0%   2.400m ± 1%   -1.82% (p=0.000 n=20)
TformCROverRGBA64-24       5.303m ± 2%   2.221m ± 1%  -58.12% (p=0.000 n=20)
geomean                    930.7µ        804.7µ       -13.54%

                     │     old.bench      │                 new.bench                  │
                     │        B/op        │     B/op       vs base                     │
ScaleNNLargeDown-24         0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABLargeDown-24         0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleBLLargeDown-24       1.407Mi ±  0%     1.407Mi ±  0%         ~ (p=0.283 n=20)
ScaleCRLargeDown-24       2.010Mi ±  0%     2.345Mi ± 14%   +16.67% (p=0.000 n=20)
ScaleNNDown-24              0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABDown-24              0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleBLDown-24            1.704Ki ±  1%     1.721Ki ±  3%         ~ (p=0.109 n=20)
ScaleCRDown-24            3.309Ki ±  2%     3.342Ki ±  2%         ~ (p=0.568 n=20)
ScaleNNUp-24                0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABUp-24                0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleBLUp-24              50.96Ki ±  2%     52.73Ki ±  4%    +3.47% (p=0.002 n=20)
ScaleCRUp-24              86.54Ki ± 14%     79.09Ki ±  2%         ~ (p=0.061 n=20)
ScaleNNSrcRGBA-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleNNSrcUnif-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleNNOverRGBA-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleNNOverUnif-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformNNSrcRGBA-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformNNSrcUnif-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformNNOverRGBA-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformNNOverUnif-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABSrcGray-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABSrcNRGBA-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABSrcRGBA-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABSrcYCbCr-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABSrcRGBA64-24       937.5Ki ±  0%       0.0Ki ±  0%  -100.00% (p=0.000 n=20)
ScaleABOverGray-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABOverNRGBA-24         0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABOverRGBA-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABOverYCbCr-24         0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
ScaleABOverRGBA64-24      937.5Ki ±  0%       0.0Ki ±  0%  -100.00% (p=0.000 n=20)
TformABSrcGray-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABSrcNRGBA-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABSrcRGBA-24           0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABSrcYCbCr-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABSrcRGBA64-24       600.8Ki ±  0%       0.0Ki ±  0%  -100.00% (p=0.000 n=20)
TformABOverGray-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABOverNRGBA-24         0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABOverRGBA-24          0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABOverYCbCr-24         0.000 ±  0%       0.000 ±  0%         ~ (p=1.000 n=20) ¹
TformABOverRGBA64-24      600.8Ki ±  0%       0.0Ki ±  0%  -100.00% (p=0.000 n=20)
ScaleCRSrcGray-24         16.51Ki ±  1%     15.97Ki ±  1%    -3.32% (p=0.000 n=20)
ScaleCRSrcNRGBA-24        59.33Ki ±  5%     57.21Ki ±  4%         ~ (p=0.142 n=20)
ScaleCRSrcRGBA-24         37.55Ki ±  2%     36.41Ki ±  2%    -3.03% (p=0.001 n=20)
ScaleCRSrcYCbCr-24        98.08Ki ±  2%     98.08Ki ±  2%         ~ (p=0.952 n=20)
ScaleCRSrcRGBA64-24    24624.95Ki ±  0%     55.21Ki ±  4%   -99.78% (p=0.000 n=20)
ScaleCROverGray-24        16.46Ki ±  1%     15.97Ki ±  1%    -2.99% (p=0.000 n=20)
ScaleCROverNRGBA-24       61.62Ki ±  3%     59.70Ki ±  5%    -3.10% (p=0.015 n=20)
ScaleCROverRGBA-24        38.76Ki ±  2%     37.55Ki ±  1%    -3.12% (p=0.000 n=20)
ScaleCROverYCbCr-24       98.08Ki ±  2%     98.08Ki ±  2%         ~ (p=0.232 n=20)
ScaleCROverRGBA64-24   24624.95Ki ±  0%     55.21Ki ±  2%   -99.78% (p=0.000 n=20)
TformCRSrcGray-24           96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCRSrcNRGBA-24          96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCRSrcRGBA-24           96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCRSrcYCbCr-24          96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCRSrcRGBA64-24    2396479.00 ±  0%       96.00 ±  0%  -100.00% (p=0.000 n=20)
TformCROverGray-24          96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCROverNRGBA-24         96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCROverRGBA-24          96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCROverYCbCr-24         96.00 ±  0%       96.00 ±  0%         ~ (p=1.000 n=20) ¹
TformCROverRGBA64-24   2396479.00 ±  0%       96.00 ±  0%  -100.00% (p=0.000 n=20)
geomean                                 ²                  ?                       ² ³
¹ all samples are equal
² summaries must be >0 to compute geomean
³ ratios must be >0 to compute geomean

                     │     old.bench     │               new.bench                │
                     │     allocs/op     │  allocs/op   vs base                   │
ScaleNNLargeDown-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABLargeDown-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleBLLargeDown-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCRLargeDown-24         0.000 ± 0%      1.000 ±  ?         ? (p=0.000 n=20)
ScaleNNDown-24              0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABDown-24              0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleBLDown-24              0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCRDown-24              0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleNNUp-24                0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABUp-24                0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleBLUp-24                0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCRUp-24                0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleNNSrcRGBA-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleNNSrcUnif-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleNNOverRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleNNOverUnif-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformNNSrcRGBA-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformNNSrcUnif-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformNNOverRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformNNOverUnif-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABSrcGray-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABSrcNRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABSrcRGBA-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABSrcYCbCr-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABSrcRGBA64-24        120.0k ± 0%       0.0k ± 0%  -100.00% (p=0.000 n=20)
ScaleABOverGray-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABOverNRGBA-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABOverRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABOverYCbCr-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleABOverRGBA64-24       120.0k ± 0%       0.0k ± 0%  -100.00% (p=0.000 n=20)
TformABSrcGray-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABSrcNRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABSrcRGBA-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABSrcYCbCr-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABSrcRGBA64-24        76.90k ± 0%      0.00k ± 0%  -100.00% (p=0.000 n=20)
TformABOverGray-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABOverNRGBA-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABOverRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABOverYCbCr-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
TformABOverRGBA64-24       76.90k ± 0%      0.00k ± 0%  -100.00% (p=0.000 n=20)
ScaleCRSrcGray-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCRSrcNRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCRSrcRGBA-24           0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCRSrcYCbCr-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCRSrcRGBA64-24        3.129M ± 0%     0.000M ± 0%  -100.00% (p=0.000 n=20)
ScaleCROverGray-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCROverNRGBA-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCROverRGBA-24          0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCROverYCbCr-24         0.000 ± 0%      0.000 ± 0%         ~ (p=1.000 n=20) ¹
ScaleCROverRGBA64-24       3.129M ± 0%     0.000M ± 0%  -100.00% (p=0.000 n=20)
TformCRSrcGray-24           2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCRSrcNRGBA-24          2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCRSrcRGBA-24           2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCRSrcYCbCr-24          2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCRSrcRGBA64-24    299549.000 ± 0%      2.000 ± 0%  -100.00% (p=0.000 n=20)
TformCROverGray-24          2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCROverNRGBA-24         2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCROverRGBA-24          2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCROverYCbCr-24         2.000 ± 0%      2.000 ± 0%         ~ (p=1.000 n=20) ¹
TformCROverRGBA64-24   299549.000 ± 0%      2.000 ± 0%  -100.00% (p=0.000 n=20)
geomean                                ²                ?                       ²
¹ all samples are equal
² summaries must be >0 to compute geomean

Change-Id: I37778e925cce13c4fec65c9e6d57e205440e2a06
Reviewed-on: https://go-review.googlesource.com/c/image/+/525255
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
Reviewed-by: Nigel Tao (INACTIVE; USE @golang.org INSTEAD) <nigeltao@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
zephyrtronium authored and gopherbot committed Sep 7, 2023
1 parent fa10be5 commit 3aac9c6
Show file tree
Hide file tree
Showing 3 changed files with 2,515 additions and 501 deletions.
207 changes: 195 additions & 12 deletions draw/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -45,7 +44,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile("impl.go", out, 0660); err != nil {
if err := os.WriteFile("impl.go", out, 0660); err != nil {
log.Fatal(err)
}
}
Expand All @@ -62,7 +61,9 @@ var (
{"*image.RGBA", "*image.NRGBA"},
{"*image.RGBA", "*image.RGBA"},
{"*image.RGBA", "*image.YCbCr"},
{"*image.RGBA", "image.RGBA64Image"},
{"*image.RGBA", "image.Image"},
{"RGBA64Image", "image.RGBA64Image"},
{"Image", "image.Image"},
}
dTypes, sTypes []string
Expand Down Expand Up @@ -234,13 +235,21 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
return ";"
case "Image":
s := ""
if d.sType == "image.Image" {
if d.sType == "image.Image" || d.sType == "image.RGBA64Image" {
s = "srcMask, smp := opts.SrcMask, opts.SrcMaskP\n"
}
return s +
"dstMask, dmp := opts.DstMask, opts.DstMaskP\n" +
"dstColorRGBA64 := &color.RGBA64{}\n" +
"dstColor := color.Color(dstColorRGBA64)"
case "RGBA64Image":
s := ""
if d.sType == "image.Image" || d.sType == "image.RGBA64Image" {
s = "srcMask, smp := opts.SrcMask, opts.SrcMaskP\n"
}
return s +
"dstMask, dmp := opts.DstMask, opts.DstMaskP\n" +
"dstColorRGBA64 := color.RGBA64{}\n"
}

case "preInner":
Expand All @@ -255,7 +264,7 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
switch d.sType {
default:
return ";"
case "image.Image":
case "image.Image", "image.RGBA64Image":
return "srcMask, smp := opts.SrcMask, opts.SrcMaskP"
}

Expand Down Expand Up @@ -334,6 +343,10 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
"$0g := uint32($1g)\n"+
"$0b := uint32($1b)",
)
case "image.RGBA64Image":
return argf(args, ""+
"$0 := color.RGBA64{uint16($1r), uint16($1g), uint16($1b), uint16($1a)}",
)
}

case "outputu":
Expand Down Expand Up @@ -364,14 +377,62 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
"dstColorRGBA64.A = uint16(qa*$2a1/0xffff + $2a)\n"+
"dst.Set($0, $1, dstColor)",
)
case "RGBA64Image":
switch d.sType {
default:
return argf(args, ""+
"q := dst.RGBA64At($0, $1)\n"+
"if dstMask != nil {\n"+
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
" $2r = $2r * ma / 0xffff\n"+
" $2g = $2g * ma / 0xffff\n"+
" $2b = $2b * ma / 0xffff\n"+
" $2a = $2a * ma / 0xffff\n"+
"}\n"+
"$2a1 := 0xffff - $2a\n"+
"dstColorRGBA64.R = uint16(uint32(q.R)*$2a1/0xffff + $2r)\n"+
"dstColorRGBA64.G = uint16(uint32(q.G)*$2a1/0xffff + $2g)\n"+
"dstColorRGBA64.B = uint16(uint32(q.B)*$2a1/0xffff + $2b)\n"+
"dstColorRGBA64.A = uint16(uint32(q.A)*$2a1/0xffff + $2a)\n"+
"dst.Set($0, $1, dstColorRGBA64)",
)
case "image.RGBA64Image":
return argf(args, ""+
"q := dst.RGBA64At($0, $1)\n"+
"if dstMask != nil {\n"+
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
" $2.R = uint16(uint32($2.R) * ma / 0xffff)\n"+
" $2.G = uint16(uint32($2.G) * ma / 0xffff)\n"+
" $2.B = uint16(uint32($2.B) * ma / 0xffff)\n"+
" $2.A = uint16(uint32($2.A) * ma / 0xffff)\n"+
"}\n"+
"$2a1 := 0xffff - uint32($2.A)\n"+
"dstColorRGBA64.R = uint16(uint32(q.R)*$2a1/0xffff + uint32($2.R))\n"+
"dstColorRGBA64.G = uint16(uint32(q.G)*$2a1/0xffff + uint32($2.G))\n"+
"dstColorRGBA64.B = uint16(uint32(q.B)*$2a1/0xffff + uint32($2.B))\n"+
"dstColorRGBA64.A = uint16(uint32(q.A)*$2a1/0xffff + uint32($2.A))\n"+
"dst.Set($0, $1, dstColorRGBA64)",
)
}
case "*image.RGBA":
return argf(args, ""+
"$2a1 := (0xffff - $2a) * 0x101\n"+
"dst.Pix[d+0] = uint8((uint32(dst.Pix[d+0])*$2a1/0xffff + $2r) >> 8)\n"+
"dst.Pix[d+1] = uint8((uint32(dst.Pix[d+1])*$2a1/0xffff + $2g) >> 8)\n"+
"dst.Pix[d+2] = uint8((uint32(dst.Pix[d+2])*$2a1/0xffff + $2b) >> 8)\n"+
"dst.Pix[d+3] = uint8((uint32(dst.Pix[d+3])*$2a1/0xffff + $2a) >> 8)",
)
switch d.sType {
default:
return argf(args, ""+
"$2a1 := (0xffff - $2a) * 0x101\n"+
"dst.Pix[d+0] = uint8((uint32(dst.Pix[d+0])*$2a1/0xffff + $2r) >> 8)\n"+
"dst.Pix[d+1] = uint8((uint32(dst.Pix[d+1])*$2a1/0xffff + $2g) >> 8)\n"+
"dst.Pix[d+2] = uint8((uint32(dst.Pix[d+2])*$2a1/0xffff + $2b) >> 8)\n"+
"dst.Pix[d+3] = uint8((uint32(dst.Pix[d+3])*$2a1/0xffff + $2a) >> 8)",
)
case "image.RGBA64Image":
return argf(args, ""+
"$2a1 := (0xffff - uint32($2.A)) * 0x101\n"+
"dst.Pix[d+0] = uint8((uint32(dst.Pix[d+0])*$2a1/0xffff + uint32($2.R)) >> 8)\n"+
"dst.Pix[d+1] = uint8((uint32(dst.Pix[d+1])*$2a1/0xffff + uint32($2.G)) >> 8)\n"+
"dst.Pix[d+2] = uint8((uint32(dst.Pix[d+2])*$2a1/0xffff + uint32($2.B)) >> 8)\n"+
"dst.Pix[d+3] = uint8((uint32(dst.Pix[d+3])*$2a1/0xffff + uint32($2.A)) >> 8)",
)
}
}

case "Src":
Expand Down Expand Up @@ -401,6 +462,51 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
" dst.Set($0, $1, dstColor)\n"+
"}",
)
case "RGBA64Image":
switch d.sType {
default:
return argf(args, ""+
"if dstMask != nil {\n"+
" q := dst.RGBA64At($0, $1)\n"+
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
" pr = pr * ma / 0xffff\n"+
" pg = pg * ma / 0xffff\n"+
" pb = pb * ma / 0xffff\n"+
" pa = pa * ma / 0xffff\n"+
" $2a1 := 0xffff - ma\n"+ // Note that this is ma, not $2a.
" dstColorRGBA64.R = uint16(uint32(q.R)*$2a1/0xffff + $2r)\n"+
" dstColorRGBA64.G = uint16(uint32(q.G)*$2a1/0xffff + $2g)\n"+
" dstColorRGBA64.B = uint16(uint32(q.B)*$2a1/0xffff + $2b)\n"+
" dstColorRGBA64.A = uint16(uint32(q.A)*$2a1/0xffff + $2a)\n"+
" dst.Set($0, $1, dstColorRGBA64)\n"+
"} else {\n"+
" dstColorRGBA64.R = uint16($2r)\n"+
" dstColorRGBA64.G = uint16($2g)\n"+
" dstColorRGBA64.B = uint16($2b)\n"+
" dstColorRGBA64.A = uint16($2a)\n"+
" dst.Set($0, $1, dstColorRGBA64)\n"+
"}",
)
case "image.RGBA64Image":
return argf(args, ""+
"if dstMask != nil {\n"+
" q := dst.RGBA64At($0, $1)\n"+
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
" p.R = uint16(uint32(p.R) * ma / 0xffff)\n"+
" p.G = uint16(uint32(p.G) * ma / 0xffff)\n"+
" p.B = uint16(uint32(p.B) * ma / 0xffff)\n"+
" p.A = uint16(uint32(p.A) * ma / 0xffff)\n"+
" $2a1 := 0xffff - ma\n"+ // Note that this is ma, not $2a.
" dstColorRGBA64.R = uint16(uint32(q.R)*$2a1/0xffff + uint32($2.R))\n"+
" dstColorRGBA64.G = uint16(uint32(q.G)*$2a1/0xffff + uint32($2.G))\n"+
" dstColorRGBA64.B = uint16(uint32(q.B)*$2a1/0xffff + uint32($2.B))\n"+
" dstColorRGBA64.A = uint16(uint32(q.A)*$2a1/0xffff + uint32($2.A))\n"+
" dst.Set($0, $1, dstColorRGBA64)\n"+
"} else {\n"+
" dst.Set($0, $1, $2)\n"+
"}",
)
}
case "*image.RGBA":
switch d.sType {
default:
Expand All @@ -425,6 +531,13 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
"dst.Pix[d+2] = uint8($2b >> 8)\n"+
"dst.Pix[d+3] = 0xff",
)
case "image.RGBA64Image":
return argf(args, ""+
"dst.Pix[d+0] = uint8($2.R >> 8)\n"+
"dst.Pix[d+1] = uint8($2.G >> 8)\n"+
"dst.Pix[d+2] = uint8($2.B >> 8)\n"+
"dst.Pix[d+3] = uint8($2.A >> 8)",
)
}
}
}
Expand Down Expand Up @@ -462,6 +575,27 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
"dstColorRGBA64.A = uint16(qa*$3a1/0xffff + $3a0)\n"+
"dst.Set($0, $1, dstColor)",
)
case "RGBA64Image":
ret = argf(args, ""+
"q := dst.RGBA64At($0, $1)\n"+
"$3r0 := uint32($2($3r * $4))\n"+
"$3g0 := uint32($2($3g * $4))\n"+
"$3b0 := uint32($2($3b * $4))\n"+
"$3a0 := uint32($2($3a * $4))\n"+
"if dstMask != nil {\n"+
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
" $3r0 = $3r0 * ma / 0xffff\n"+
" $3g0 = $3g0 * ma / 0xffff\n"+
" $3b0 = $3b0 * ma / 0xffff\n"+
" $3a0 = $3a0 * ma / 0xffff\n"+
"}\n"+
"$3a1 := 0xffff - $3a0\n"+
"dstColorRGBA64.R = uint16(uint32(q.R)*$3a1/0xffff + $3r0)\n"+
"dstColorRGBA64.G = uint16(uint32(q.G)*$3a1/0xffff + $3g0)\n"+
"dstColorRGBA64.B = uint16(uint32(q.B)*$3a1/0xffff + $3b0)\n"+
"dstColorRGBA64.A = uint16(uint32(q.A)*$3a1/0xffff + $3a0)\n"+
"dst.SetRGBA64($0, $1, dstColorRGBA64)",
)
case "*image.RGBA":
ret = argf(args, ""+
"$3r0 := uint32($2($3r * $4))\n"+
Expand Down Expand Up @@ -503,6 +637,29 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
" dst.Set($0, $1, dstColor)\n"+
"}",
)
case "RGBA64Image":
ret = argf(args, ""+
"if dstMask != nil {\n"+
" q := dst.RGBA64At($0, $1)\n"+
" _, _, _, ma := dstMask.At(dmp.X + $0, dmp.Y + $1).RGBA()\n"+
" pr := uint32($2($3r * $4)) * ma / 0xffff\n"+
" pg := uint32($2($3g * $4)) * ma / 0xffff\n"+
" pb := uint32($2($3b * $4)) * ma / 0xffff\n"+
" pa := uint32($2($3a * $4)) * ma / 0xffff\n"+
" pa1 := 0xffff - ma\n"+ // Note that this is ma, not pa.
" dstColorRGBA64.R = uint16(uint32(q.R)*pa1/0xffff + pr)\n"+
" dstColorRGBA64.G = uint16(uint32(q.G)*pa1/0xffff + pg)\n"+
" dstColorRGBA64.B = uint16(uint32(q.B)*pa1/0xffff + pb)\n"+
" dstColorRGBA64.A = uint16(uint32(q.A)*pa1/0xffff + pa)\n"+
" dst.SetRGBA64($0, $1, dstColorRGBA64)\n"+
"} else {\n"+
" dstColorRGBA64.R = $2($3r * $4)\n"+
" dstColorRGBA64.G = $2($3g * $4)\n"+
" dstColorRGBA64.B = $2($3b * $4)\n"+
" dstColorRGBA64.A = $2($3a * $4)\n"+
" dst.SetRGBA64($0, $1, dstColorRGBA64)\n"+
"}",
)
case "*image.RGBA":
switch d.sType {
default:
Expand Down Expand Up @@ -560,7 +717,7 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
"%sr%s, %sg%s, %sb%s, %sa%s := src.At(%s, %s).RGBA()\n",
lhs, tmp, lhs, tmp, lhs, tmp, lhs, tmp, args[0], args[1],
)
if d.dType == "" || d.dType == "Image" {
if d.dType == "" || d.dType == "Image" || d.dType == "RGBA64Image" {
fmt.Fprintf(buf, ""+
"if srcMask != nil {\n"+
" _, _, _, ma := srcMask.At(smp.X+%s, smp.Y+%s).RGBA()\n"+
Expand All @@ -576,6 +733,24 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
lhs, tmp, lhs, tmp,
)
}
case "image.RGBA64Image":
fmt.Fprintf(buf, ""+
"%s%s := src.RGBA64At(%s, %s)\n",
lhs, tmp, args[0], args[1],
)
if d.dType == "" || d.dType == "Image" || d.dType == "RGBA64Image" {
fmt.Fprintf(buf, ""+
"if srcMask != nil {\n"+
" _, _, _, ma := srcMask.At(smp.X+%[1]s, smp.Y+%[2]s).RGBA()\n"+
" %[3]s%[4]s.R = uint16(uint32(%[3]s%[4]s.R) * ma / 0xffff)\n"+
" %[3]s%[4]s.G = uint16(uint32(%[3]s%[4]s.G) * ma / 0xffff)\n"+
" %[3]s%[4]s.B = uint16(uint32(%[3]s%[4]s.B) * ma / 0xffff)\n"+
" %[3]s%[4]s.A = uint16(uint32(%[3]s%[4]s.A) * ma / 0xffff)\n"+
"}\n",
args[0], args[1],
lhs, tmp,
)
}
case "*image.Gray":
fmt.Fprintf(buf, ""+
"%si := %s\n"+
Expand Down Expand Up @@ -647,6 +822,14 @@ func expnDollar(prefix, dollar, suffix string, d *data) string {
lhs, eqOp, lhs, extra,
lhs, eqOp, lhs, extra,
)
case "image.RGBA64Image":
fmt.Fprintf(buf, ""+
"%[1]sr %[2]s float64(%[3]su.R)%[4]s\n"+
"%[1]sg %[2]s float64(%[3]su.G)%[4]s\n"+
"%[1]sb %[2]s float64(%[3]su.B)%[4]s\n"+
"%[1]sa %[2]s float64(%[3]su.A)%[4]s\n",
lhs, eqOp, lhs, extra,
)
}
}

Expand Down
Loading

0 comments on commit 3aac9c6

Please sign in to comment.