mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-30 13:48:51 +08:00
12 lines
46 KiB
Go
12 lines
46 KiB
Go
//
|
|
// Copyright 2020 FoxyUtils ehf. All rights reserved.
|
|
//
|
|
// This is a commercial product and requires a license to operate.
|
|
// A trial license can be obtained at https://unidoc.io
|
|
//
|
|
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
|
|
//
|
|
// Use of this source code is governed by the UniDoc End User License Agreement
|
|
// terms that can be accessed at https://unidoc.io/eula/
|
|
|
|
package imageutil ;import (_b "errors";_g "fmt";_gd "github.com/unidoc/unipdf/v3/internal/bitwise";_d "image";_bf "image/color";_fb "image/draw";_f "math";);func (_fbe *Gray2 )ColorModel ()_bf .Model {return Gray2Model };func MonochromeThresholdConverter (threshold uint8 )ColorConverter {return &monochromeThresholdConverter {Threshold :threshold };};var _ Image =&Gray16 {};func _cde (_cddf _bf .Gray ,_bce monochromeModel )_bf .Gray {if _cddf .Y > uint8 (_bce ){return _bf .Gray {Y :_f .MaxUint8 };};return _bf .Gray {};};func _bec (_eae _d .Image ,_becc Image ,_gbdf _d .Rectangle ){for _ede :=0;_ede < _gbdf .Max .X ;_ede ++{for _fad :=0;_fad < _gbdf .Max .Y ;_fad ++{_cf :=_eae .At (_ede ,_fad );_becc .Set (_ede ,_fad ,_cf );};};};func (_cc *CMYK32 )SetCMYK (x ,y int ,c _bf .CMYK ){_ba :=4*(y *_cc .Width +x );if _ba +3>=len (_cc .Data ){return ;};_cc .Data [_ba ]=c .C ;_cc .Data [_ba +1]=c .M ;_cc .Data [_ba +2]=c .Y ;_cc .Data [_ba +3]=c .K ;};func (_cad *Monochrome )clearBit (_beb ,_dcgc int ){_cad .Data [_beb ]&=^(0x80>>uint (_dcgc &7))};type SMasker interface{HasAlpha ()bool ;GetAlpha ()[]byte ;MakeAlpha ();};func (_ega *NRGBA32 )setRGBA (_cge int ,_fadaf _bf .NRGBA ){_dgae :=3*_cge ;_ega .Data [_dgae ]=_fadaf .R ;_ega .Data [_dgae +1]=_fadaf .G ;_ega .Data [_dgae +2]=_fadaf .B ;if _cge < len (_ega .Alpha ){_ega .Alpha [_cge ]=_fadaf .A ;};};func (_bbeb *ImageBase )HasAlpha ()bool {return _bbeb .Alpha !=nil };func ImgToBinary (i _d .Image ,threshold uint8 )*_d .Gray {switch _gedg :=i .(type ){case *_d .Gray :if _fde (_gedg ){return _gedg ;};return _aaega (_gedg ,threshold );case *_d .Gray16 :return _cgfc (_gedg ,threshold );default:return _gfdf (_gedg ,threshold );};};func (_bgc *ImageBase )GetAlpha ()[]byte {return _bgc .Alpha };type colorConverter struct{_df func (_faab _d .Image )(Image ,error );};func (_gcf *NRGBA16 )Set (x ,y int ,c _bf .Color ){_dcbb :=y *_gcf .BytesPerLine +x *3/2;if _dcbb +1>=len (_gcf .Data ){return ;};_aaeg :=NRGBA16Model .Convert (c ).(_bf .NRGBA );_gcf .setNRGBA (x ,y ,_dcbb ,_aaeg );};func ConverterFunc (converterFunc func (_egb _d .Image )(Image ,error ))ColorConverter {return colorConverter {_df :converterFunc };};func (_bbdd *Monochrome )Set (x ,y int ,c _bf .Color ){_fec :=y *_bbdd .BytesPerLine +x >>3;if _fec > len (_bbdd .Data )-1{return ;};_adb :=_bbdd .ColorModel ().Convert (c ).(_bf .Gray );_bbdd .setGray (x ,_adb ,_fec );};func _dffd (_agag _bf .Color )_bf .Color {_def :=_bf .GrayModel .Convert (_agag ).(_bf .Gray );return _eefe (_def );};func ColorAtNRGBA16 (x ,y ,width ,bytesPerLine int ,data ,alpha []byte ,decode []float64 )(_bf .NRGBA ,error ){_bdfc :=y *bytesPerLine +x *3/2;if _bdfc +1>=len (data ){return _bf .NRGBA {},_fgb (x ,y );};const (_ebfd =0xf;_debe =uint8 (0xff););_fdab :=_debe ;if alpha !=nil {_bfee :=y *BytesPerLine (width ,4,1);if _bfee < len (alpha ){if x %2==0{_fdab =(alpha [_bfee ]>>uint (4))&_ebfd ;}else {_fdab =alpha [_bfee ]&_ebfd ;};_fdab |=_fdab <<4;};};var _faed ,_edfd ,_gcg uint8 ;if x *3%2==0{_faed =(data [_bdfc ]>>uint (4))&_ebfd ;_edfd =data [_bdfc ]&_ebfd ;_gcg =(data [_bdfc +1]>>uint (4))&_ebfd ;}else {_faed =data [_bdfc ]&_ebfd ;_edfd =(data [_bdfc +1]>>uint (4))&_ebfd ;_gcg =data [_bdfc +1]&_ebfd ;};if len (decode )==6{_faed =uint8 (uint32 (LinearInterpolate (float64 (_faed ),0,15,decode [0],decode [1]))&0xf);_edfd =uint8 (uint32 (LinearInterpolate (float64 (_edfd ),0,15,decode [2],decode [3]))&0xf);_gcg =uint8 (uint32 (LinearInterpolate (float64 (_gcg ),0,15,decode [4],decode [5]))&0xf);};return _bf .NRGBA {R :(_faed <<4)|(_faed &0xf),G :(_edfd <<4)|(_edfd &0xf),B :(_gcg <<4)|(_gcg &0xf),A :_fdab },nil ;};func (_edb *NRGBA16 )Validate ()error {if len (_edb .Data )!=3*_edb .Width *_edb .Height /2{return _b .New ("i\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0069\u006da\u0067\u0065\u0020\u0064\u0061\u0074\u0061 s\u0069\u007a\u0065\u0020f\u006f\u0072\u0020\u0070\u0072\u006f\u0076\u0069\u0064ed\u0020\u0064i\u006d\u0065\u006e\u0073\u0069\u006f\u006e\u0073");};return nil ;};func (_cdad *Gray8 )Copy ()Image {return &Gray8 {ImageBase :_cdad .copy ()}};func BytesPerLine (width ,bitsPerComponent ,colorComponents int )int {return ((width *bitsPerComponent )*colorComponents +7)>>3;};func (_cdfa *Monochrome )SetGray (x ,y int ,g _bf .Gray ){_dff :=y *_cdfa .BytesPerLine +x >>3;if _dff > len (_cdfa .Data )-1{return ;};g =_cde (g ,monochromeModel (_cdfa .ModelThreshold ));_cdfa .setGray (x ,g ,_dff );};func (_dgfd *NRGBA16 )Copy ()Image {return &NRGBA16 {ImageBase :_dgfd .copy ()}};var _ Gray =&Gray4 {};func _aec (_dec _bf .CMYK )_bf .NRGBA {_ag ,_ga ,_cfd :=_bf .CMYKToRGB (_dec .C ,_dec .M ,_dec .Y ,_dec .K );return _bf .NRGBA {R :_ag ,G :_ga ,B :_cfd ,A :0xff};};var _ _d .Image =&Monochrome {};var _ Gray =&Gray16 {};func (_afg *Gray16 )Set (x ,y int ,c _bf .Color ){_afb :=(y *_afg .BytesPerLine /2+x )*2;if _afb +1>=len (_afg .Data ){return ;};_febf :=_bf .Gray16Model .Convert (c ).(_bf .Gray16 );_afg .Data [_afb ],_afg .Data [_afb +1]=uint8 (_febf .Y >>8),uint8 (_febf .Y &0xff);};func _dad (_cdc _bf .Gray )_bf .NRGBA {return _bf .NRGBA {R :_cdc .Y ,G :_cdc .Y ,B :_cdc .Y ,A :0xff}};type NRGBA16 struct{ImageBase };func _dca (_edg _bf .RGBA )_bf .Gray {_eea :=(19595*uint32 (_edg .R )+38470*uint32 (_edg .G )+7471*uint32 (_edg .B )+1<<7)>>16;return _bf .Gray {Y :uint8 (_eea )};};func (_eff *NRGBA32 )ColorModel ()_bf .Model {return _bf .NRGBAModel };var _ Image =&NRGBA32 {};func (_bceb *Monochrome )setGray (_bacd int ,_cgc _bf .Gray ,_ada int ){if _cgc .Y ==0{_bceb .clearBit (_ada ,_bacd );}else {_bceb .setBit (_ada ,_bacd );};};func ColorAtGray8BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_bf .Gray ,error ){_ccce :=y *bytesPerLine +x ;if _ccce >=len (data ){return _bf .Gray {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};_dfc :=data [_ccce ];if len (decode )==2{_dfc =uint8 (uint32 (LinearInterpolate (float64 (_dfc ),0,255,decode [0],decode [1]))&0xff);};return _bf .Gray {Y :_dfc },nil ;};var (MonochromeConverter =ConverterFunc (_gad );Gray2Converter =ConverterFunc (_ggd );Gray4Converter =ConverterFunc (_cef );GrayConverter =ConverterFunc (_cfe );Gray16Converter =ConverterFunc (_ffc );NRGBA16Converter =ConverterFunc (_ebeb );NRGBAConverter =ConverterFunc (_ebb );NRGBA64Converter =ConverterFunc (_gebb );CMYKConverter =ConverterFunc (_dab ););func (_dega *ImageBase )Pix ()[]byte {return _dega .Data };func (_abfc *Monochrome )Base ()*ImageBase {return &_abfc .ImageBase };func (_bfec *Gray4 )At (x ,y int )_bf .Color {_ddd ,_ :=_bfec .ColorAt (x ,y );return _ddd };func init (){_bbag ()};func _cbb (_degb CMYK ,_gbee NRGBA ,_dfae _d .Rectangle ){for _gegc :=0;_gegc < _dfae .Max .X ;_gegc ++{for _gedf :=0;_gedf < _dfae .Max .Y ;_gedf ++{_age :=_degb .CMYKAt (_gegc ,_gedf );_gbee .SetNRGBA (_gegc ,_gedf ,_aec (_age ));};};};var _ NRGBA =&NRGBA16 {};func _fbd (_aca _bf .NRGBA )_bf .Gray {_fcd ,_ecf ,_aee ,_ :=_aca .RGBA ();_bfg :=(19595*_fcd +38470*_ecf +7471*_aee +1<<15)>>24;return _bf .Gray {Y :uint8 (_bfg )};};func (_afdf *Gray16 )Histogram ()(_daeb [256]int ){for _add :=0;_add < _afdf .Width ;_add ++{for _abcg :=0;_abcg < _afdf .Height ;_abcg ++{_daeb [_afdf .GrayAt (_add ,_abcg ).Y ]++;};};return _daeb ;};func ImgToGray (i _d .Image )*_d .Gray {if _ege ,_adbf :=i .(*_d .Gray );_adbf {return _ege ;};_ceda :=i .Bounds ();_dgd :=_d .NewGray (_ceda );for _cbgg :=0;_cbgg < _ceda .Max .X ;_cbgg ++{for _cdfb :=0;_cdfb < _ceda .Max .Y ;_cdfb ++{_fbac :=i .At (_cbgg ,_cdfb );_dgd .Set (_cbgg ,_cdfb ,_fbac );};};return _dgd ;};func _dab (_gb _d .Image )(Image ,error ){if _bfe ,_bg :=_gb .(*CMYK32 );_bg {return _bfe .Copy (),nil ;};_ed :=_gb .Bounds ();_cd ,_be :=NewImage (_ed .Max .X ,_ed .Max .Y ,8,4,nil ,nil ,nil );if _be !=nil {return nil ,_be ;};switch _aaf :=_gb .(type ){case CMYK :_fae (_aaf ,_cd .(CMYK ),_ed );case Gray :_edc (_aaf ,_cd .(CMYK ),_ed );case NRGBA :_ea (_aaf ,_cd .(CMYK ),_ed );case RGBA :_bbf (_aaf ,_cd .(CMYK ),_ed );default:_bec (_gb ,_cd ,_ed );};return _cd ,nil ;};func (_abe *Gray16 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_abe .Width ,Y :_abe .Height }};};func (_baa *Gray2 )SetGray (x ,y int ,gray _bf .Gray ){_geb :=_eefe (gray );_adaf :=y *_baa .BytesPerLine ;_ade :=_adaf +(x >>2);if _ade >=len (_baa .Data ){return ;};_gba :=_geb .Y >>6;_baa .Data [_ade ]=(_baa .Data [_ade ]&(^(0xc0>>uint (2*((x )&3)))))|(_gba <<uint (6-2*(x &3)));};func (_fgf *Monochrome )Copy ()Image {return &Monochrome {ImageBase :_fgf .ImageBase .copy (),ModelThreshold :_fgf .ModelThreshold };};func IsGrayImgBlackAndWhite (i *_d .Gray )bool {return _fde (i )};var _ Image =&Gray8 {};func (_cdag *NRGBA64 )Base ()*ImageBase {return &_cdag .ImageBase };func (_eefg *Gray2 )Histogram ()(_dcad [256]int ){for _bebb :=0;_bebb < _eefg .Width ;_bebb ++{for _feb :=0;_feb < _eefg .Height ;_feb ++{_dcad [_eefg .GrayAt (_bebb ,_feb ).Y ]++;};};return _dcad ;};func (_cgd *Gray8 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_cgd .Width ,Y :_cgd .Height }};};func _fae (_ge ,_cb CMYK ,_ee _d .Rectangle ){for _ef :=0;_ef < _ee .Max .X ;_ef ++{for _ab :=0;_ab < _ee .Max .Y ;_ab ++{_cb .SetCMYK (_ef ,_ab ,_ge .CMYKAt (_ef ,_ab ));};};};func _dffg (_ffea NRGBA ,_aecc Gray ,_cdb _d .Rectangle ){for _aea :=0;_aea < _cdb .Max .X ;_aea ++{for _ffed :=0;_ffed < _cdb .Max .Y ;_ffed ++{_dba :=_fbd (_ffea .NRGBAAt (_aea ,_ffed ));_aecc .SetGray (_aea ,_ffed ,_dba );};};};func (_bebg *Gray8 )GrayAt (x ,y int )_bf .Gray {_cccf ,_ :=ColorAtGray8BPC (x ,y ,_bebg .BytesPerLine ,_bebg .Data ,_bebg .Decode );return _cccf ;};func _fceg (_fbda _d .Image ,_gffc Image ,_agcd _d .Rectangle ){if _ebdc ,_gagf :=_fbda .(SMasker );_gagf &&_ebdc .HasAlpha (){_gffc .(SMasker ).MakeAlpha ();};switch _aeb :=_fbda .(type ){case Gray :_fcga (_aeb ,_gffc .(NRGBA ),_agcd );case NRGBA :_gce (_aeb ,_gffc .(NRGBA ),_agcd );case CMYK :_cbb (_aeb ,_gffc .(NRGBA ),_agcd );case RGBA :_cffd (_aeb ,_gffc .(NRGBA ),_agcd );default:_bec (_fbda ,_gffc ,_agcd );};};func (_fbdg *Gray4 )setGray (_cbc int ,_agb int ,_dcade _bf .Gray ){_cgf :=_agb *_fbdg .BytesPerLine ;_cce :=_cgf +(_cbc >>1);if _cce >=len (_fbdg .Data ){return ;};_dddf :=_dcade .Y >>4;_fbdg .Data [_cce ]=(_fbdg .Data [_cce ]&(^(0xf0>>uint (4*(_cbc &1)))))|(_dddf <<uint (4-4*(_cbc &1)));};func (_dag *Gray8 )Set (x ,y int ,c _bf .Color ){_bea :=y *_dag .BytesPerLine +x ;if _bea > len (_dag .Data )-1{return ;};_dddfa :=_bf .GrayModel .Convert (c );_dag .Data [_bea ]=_dddfa .(_bf .Gray ).Y ;};var ErrInvalidImage =_b .New ("i\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0069\u006da\u0067\u0065\u0020\u0064\u0061\u0074\u0061 s\u0069\u007a\u0065\u0020f\u006f\u0072\u0020\u0070\u0072\u006f\u0076\u0069\u0064ed\u0020\u0064i\u006d\u0065\u006e\u0073\u0069\u006f\u006e\u0073");func (_gab *monochromeThresholdConverter )Convert (img _d .Image )(Image ,error ){if _edd ,_edf :=img .(*Monochrome );_edf {return _edd .Copy (),nil ;};_deg :=img .Bounds ();_fagb ,_bd :=NewImage (_deg .Max .X ,_deg .Max .Y ,1,1,nil ,nil ,nil );if _bd !=nil {return nil ,_bd ;};_fagb .(*Monochrome ).ModelThreshold =_gab .Threshold ;for _bac :=0;_bac < _deg .Max .X ;_bac ++{for _dcb :=0;_dcb < _deg .Max .Y ;_dcb ++{_ccb :=img .At (_bac ,_dcb );_fagb .Set (_bac ,_dcb ,_ccb );};};return _fagb ,nil ;};var _ Image =&Gray4 {};func (_fga *Gray8 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtGray8BPC (x ,y ,_fga .BytesPerLine ,_fga .Data ,_fga .Decode );};func ColorAtGray16BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_bf .Gray16 ,error ){_gbdb :=(y *bytesPerLine /2+x )*2;if _gbdb +1>=len (data ){return _bf .Gray16 {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};_gdf :=uint16 (data [_gbdb ])<<8|uint16 (data [_gbdb +1]);if len (decode )==2{_gdf =uint16 (uint64 (LinearInterpolate (float64 (_gdf ),0,65535,decode [0],decode [1])));};return _bf .Gray16 {Y :_gdf },nil ;};func (_febd *NRGBA64 )Validate ()error {if len (_febd .Data )!=3*2*_febd .Width *_febd .Height {return _b .New ("i\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0069\u006da\u0067\u0065\u0020\u0064\u0061\u0074\u0061 s\u0069\u007a\u0065\u0020f\u006f\u0072\u0020\u0070\u0072\u006f\u0076\u0069\u0064ed\u0020\u0064i\u006d\u0065\u006e\u0073\u0069\u006f\u006e\u0073");};return nil ;};var _bcg [256]uint8 ;type ImageBase struct{Width ,Height int ;BitsPerComponent ,ColorComponents int ;Data ,Alpha []byte ;Decode []float64 ;BytesPerLine int ;};func (_fge *Gray16 )ColorModel ()_bf .Model {return _bf .Gray16Model };func (_bfgf *NRGBA64 )ColorModel ()_bf .Model {return _bf .NRGBA64Model };var _ Image =&Monochrome {};func _bdec (_dbge RGBA ,_fbg Gray ,_dbd _d .Rectangle ){for _ecff :=0;_ecff < _dbd .Max .X ;_ecff ++{for _fbcb :=0;_fbcb < _dbd .Max .Y ;_fbcb ++{_cbgb :=_dca (_dbge .RGBAAt (_ecff ,_fbcb ));_fbg .SetGray (_ecff ,_fbcb ,_cbgb );};};};type Gray16 struct{ImageBase };func (_acgd *Monochrome )Histogram ()(_dga [256]int ){for _ ,_gc :=range _acgd .Data {_dga [0xff]+=int (_bcg [_acgd .Data [_gc ]]);};return _dga ;};func NewImage (width ,height ,bitsPerComponent ,colorComponents int ,data ,alpha []byte ,decode []float64 )(Image ,error ){_begg :=NewImageBase (width ,height ,bitsPerComponent ,colorComponents ,data ,alpha ,decode );var _fab Image ;switch colorComponents {case 1:switch bitsPerComponent {case 1:_fab =&Monochrome {ImageBase :_begg ,ModelThreshold :0x0f};case 2:_fab =&Gray2 {ImageBase :_begg };case 4:_fab =&Gray4 {ImageBase :_begg };case 8:_fab =&Gray8 {ImageBase :_begg };case 16:_fab =&Gray16 {ImageBase :_begg };};case 3:switch bitsPerComponent {case 4:_fab =&NRGBA16 {ImageBase :_begg };case 8:_fab =&NRGBA32 {ImageBase :_begg };case 16:_fab =&NRGBA64 {ImageBase :_begg };};case 4:_fab =&CMYK32 {ImageBase :_begg };};if _fab ==nil {return nil ,ErrInvalidImage ;};return _fab ,nil ;};var _ Image =&Gray2 {};func ColorAtCMYK (x ,y ,width int ,data []byte ,decode []float64 )(_bf .CMYK ,error ){_dg :=4*(y *width +x );if _dg +3>=len (data ){return _bf .CMYK {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};C :=data [_dg ]&0xff;M :=data [_dg +1]&0xff;Y :=data [_dg +2]&0xff;K :=data [_dg +3]&0xff;if len (decode )==8{C =uint8 (uint32 (LinearInterpolate (float64 (C ),0,255,decode [0],decode [1]))&0xff);M =uint8 (uint32 (LinearInterpolate (float64 (M ),0,255,decode [2],decode [3]))&0xff);Y =uint8 (uint32 (LinearInterpolate (float64 (Y ),0,255,decode [4],decode [5]))&0xff);K =uint8 (uint32 (LinearInterpolate (float64 (K ),0,255,decode [6],decode [7]))&0xff);};return _bf .CMYK {C :C ,M :M ,Y :Y ,K :K },nil ;};func MonochromeModel (threshold uint8 )_bf .Model {return monochromeModel (threshold )};func (_gggc *Gray4 )Histogram ()(_dggc [256]int ){for _feca :=0;_feca < _gggc .Width ;_feca ++{for _gcdb :=0;_gcdb < _gggc .Height ;_gcdb ++{_dggc [_gggc .GrayAt (_feca ,_gcdb ).Y ]++;};};return _dggc ;};func (_aga *Gray2 )Validate ()error {if len (_aga .Data )!=_aga .Height *_aga .BytesPerLine {return ErrInvalidImage ;};return nil ;};type NRGBA interface{NRGBAAt (_edfe ,_ebg int )_bf .NRGBA ;SetNRGBA (_gae ,_faf int ,_cbga _bf .NRGBA );};func _ebeb (_feed _d .Image )(Image ,error ){if _ffbga ,_daae :=_feed .(*NRGBA16 );_daae {return _ffbga .Copy (),nil ;};_dgaf :=_feed .Bounds ();_bbg ,_ebd :=NewImage (_dgaf .Max .X ,_dgaf .Max .Y ,4,3,nil ,nil ,nil );if _ebd !=nil {return nil ,_ebd ;};_fceg (_feed ,_bbg ,_dgaf );return _bbg ,nil ;};func (_cdde *NRGBA32 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_cdde .Width ,Y :_cdde .Height }};};func _bbd (_dadf _bf .CMYK )_bf .Gray {_cg ,_fg ,_egg :=_bf .CMYKToRGB (_dadf .C ,_dadf .M ,_dadf .Y ,_dadf .K );_cgb :=(19595*uint32 (_cg )+38470*uint32 (_fg )+7471*uint32 (_egg )+1<<7)>>16;return _bf .Gray {Y :uint8 (_cgb )};};func (_fbf *CMYK32 )CMYKAt (x ,y int )_bf .CMYK {_fc ,_ :=ColorAtCMYK (x ,y ,_fbf .Width ,_fbf .Data ,_fbf .Decode );return _fc ;};func _aefc (_edea _bf .Color )_bf .Color {_gbe :=_bf .GrayModel .Convert (_edea ).(_bf .Gray );return _eeb (_gbe );};func _ebb (_gbaa _d .Image )(Image ,error ){if _efe ,_baaa :=_gbaa .(*NRGBA32 );_baaa {return _efe .Copy (),nil ;};_gea :=_gbaa .Bounds ();_gdgf ,_bbagf :=NewImage (_gea .Max .X ,_gea .Max .Y ,8,3,nil ,nil ,nil );if _bbagf !=nil {return nil ,_bbagf ;};_fceg (_gbaa ,_gdgf ,_gea );return _gdgf ,nil ;};var _ Gray =&Monochrome {};func (_bfc *Gray4 )Validate ()error {if len (_bfc .Data )!=_bfc .Height *_bfc .BytesPerLine {return ErrInvalidImage ;};return nil ;};func (_bad *NRGBA32 )SetNRGBA (x ,y int ,c _bf .NRGBA ){_caga :=y *_bad .Width +x ;_bcdg :=3*_caga ;if _bcdg +2>=len (_bad .Data ){return ;};_bad .setRGBA (_caga ,c );return ;};func (_bafb *NRGBA16 )Base ()*ImageBase {return &_bafb .ImageBase };func (_dbe *Gray16 )Validate ()error {if len (_dbe .Data )!=_dbe .Height *_dbe .BytesPerLine {return ErrInvalidImage ;};return nil ;};func (_gcdd *NRGBA64 )Copy ()Image {return &NRGBA64 {ImageBase :_gcdd .copy ()}};func (_bb *CMYK32 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtCMYK (x ,y ,_bb .Width ,_bb .Data ,_bb .Decode );};func (_cca *Monochrome )Validate ()error {if len (_cca .Data )!=_cca .Height *_cca .BytesPerLine {return ErrInvalidImage ;};return nil ;};func (_fdf *Gray8 )Validate ()error {if len (_fdf .Data )!=_fdf .Height *_fdf .BytesPerLine {return ErrInvalidImage ;};return nil ;};type NRGBA64 struct{ImageBase };func (_gfg *NRGBA32 )Copy ()Image {return &NRGBA32 {ImageBase :_gfg .copy ()}};func ColorAtGrayscale (x ,y ,bitsPerColor ,bytesPerLine int ,data []byte ,decode []float64 )(_bf .Color ,error ){switch bitsPerColor {case 1:return ColorAtGray1BPC (x ,y ,bytesPerLine ,data ,decode );case 2:return ColorAtGray2BPC (x ,y ,bytesPerLine ,data ,decode );case 4:return ColorAtGray4BPC (x ,y ,bytesPerLine ,data ,decode );case 8:return ColorAtGray8BPC (x ,y ,bytesPerLine ,data ,decode );case 16:return ColorAtGray16BPC (x ,y ,bytesPerLine ,data ,decode );default:return nil ,_g .Errorf ("\u0075\u006e\u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0067\u0072\u0061\u0079\u0020\u0073c\u0061\u006c\u0065\u0020\u0062\u0069\u0074s\u0020\u0070\u0065\u0072\u0020\u0063\u006f\u006c\u006f\u0072\u0020a\u006d\u006f\u0075\u006e\u0074\u003a\u0020\u0027\u0025\u0064\u0027",bitsPerColor );};};func (_bcb colorConverter )Convert (src _d .Image )(Image ,error ){return _bcb ._df (src )};type NRGBA32 struct{ImageBase };var _ NRGBA =&NRGBA32 {};type monochromeModel uint8 ;func _aaega (_dccd *_d .Gray ,_dfbc uint8 )*_d .Gray {_dadfg :=_dccd .Bounds ();_dedg :=_d .NewGray (_dadfg );for _gbfg :=0;_gbfg < _dadfg .Dx ();_gbfg ++{for _cade :=0;_cade < _dadfg .Dy ();_cade ++{_ecd :=_dccd .GrayAt (_gbfg ,_cade );_dedg .SetGray (_gbfg ,_cade ,_bf .Gray {Y :_dfge (_ecd .Y ,_dfbc )});};};return _dedg ;};func _bgef (_fdcf _bf .NRGBA )_bf .NRGBA {_fdcf .R =_fdcf .R >>4|(_fdcf .R >>4)<<4;_fdcf .G =_fdcf .G >>4|(_fdcf .G >>4)<<4;_fdcf .B =_fdcf .B >>4|(_fdcf .B >>4)<<4;return _fdcf ;};func _acae (_dace _d .Image ,_bee Image ,_ebe _d .Rectangle ){switch _bgdc :=_dace .(type ){case Gray :_dee (_bgdc ,_bee .(Gray ),_ebe );case NRGBA :_dffg (_bgdc ,_bee .(Gray ),_ebe );case CMYK :_fdad (_bgdc ,_bee .(Gray ),_ebe );case RGBA :_bdec (_bgdc ,_bee .(Gray ),_ebe );default:_bec (_dace ,_bee .(Image ),_ebe );};};var _ Image =&NRGBA64 {};func (_ace *Gray8 )Histogram ()(_gdg [256]int ){for _fecb :=0;_fecb < len (_ace .Data );_fecb ++{_gdg [_ace .Data [_fecb ]]++;};return _gdg ;};func ColorAtNRGBA64 (x ,y ,width int ,data ,alpha []byte ,decode []float64 )(_bf .NRGBA64 ,error ){_dadc :=(y *width +x )*2;_abfd :=_dadc *3;if _abfd +5>=len (data ){return _bf .NRGBA64 {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};const _aacdb =0xffff;_bead :=uint16 (_aacdb );if alpha !=nil &&len (alpha )> _dadc +1{_bead =uint16 (alpha [_dadc ])<<8|uint16 (alpha [_dadc +1]);};_geeb :=uint16 (data [_abfd ])<<8|uint16 (data [_abfd +1]);_ggdc :=uint16 (data [_abfd +2])<<8|uint16 (data [_abfd +3]);_abad :=uint16 (data [_abfd +4])<<8|uint16 (data [_abfd +5]);if len (decode )==6{_geeb =uint16 (uint64 (LinearInterpolate (float64 (_geeb ),0,65535,decode [0],decode [1]))&_aacdb );_ggdc =uint16 (uint64 (LinearInterpolate (float64 (_ggdc ),0,65535,decode [2],decode [3]))&_aacdb );_abad =uint16 (uint64 (LinearInterpolate (float64 (_abad ),0,65535,decode [4],decode [5]))&_aacdb );};return _bf .NRGBA64 {R :_geeb ,G :_ggdc ,B :_abad ,A :_bead },nil ;};func FromGoImage (i _d .Image )(Image ,error ){switch _ddfa :=i .(type ){case Image :return _ddfa .Copy (),nil ;case Gray :return GrayConverter .Convert (i );case CMYK :return CMYKConverter .Convert (i );default:return NRGBAConverter .Convert (i );};};var _ _d .Image =&NRGBA16 {};func (_ddf *Gray8 )ColorModel ()_bf .Model {return _bf .GrayModel };func (_ce *CMYK32 )ColorModel ()_bf .Model {return _bf .CMYKModel };func (_cee *Gray2 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtGray2BPC (x ,y ,_cee .BytesPerLine ,_cee .Data ,_cee .Decode );};func (_dagf *NRGBA64 )At (x ,y int )_bf .Color {_fff ,_ :=_dagf .ColorAt (x ,y );return _fff };type Gray2 struct{ImageBase };func (_ggc *ImageBase )newAlpha (){_ccea :=BytesPerLine (_ggc .Width ,_ggc .BitsPerComponent ,1);_ggc .Alpha =make ([]byte ,_ggc .Height *_ccea );};func (_ecc *NRGBA32 )NRGBAAt (x ,y int )_bf .NRGBA {_fbad ,_ :=ColorAtNRGBA32 (x ,y ,_ecc .Width ,_ecc .Data ,_ecc .Alpha ,_ecc .Decode );return _fbad ;};type RGBA interface{RGBAAt (_eeg ,_bede int )_bf .RGBA ;SetRGBA (_efda ,_gbgb int ,_cfg _bf .RGBA );};type monochromeThresholdConverter struct{Threshold uint8 ;};func (_fce *NRGBA16 )SetNRGBA (x ,y int ,c _bf .NRGBA ){_bafd :=y *_fce .BytesPerLine +x *3/2;if _bafd +1>=len (_fce .Data ){return ;};c =_bgef (c );_fce .setNRGBA (x ,y ,_bafd ,c );};func (_baff *Gray16 )Base ()*ImageBase {return &_baff .ImageBase };var _ Image =&NRGBA16 {};func ColorAtNRGBA32 (x ,y ,width int ,data ,alpha []byte ,decode []float64 )(_bf .NRGBA ,error ){_bff :=y *width +x ;_fadac :=3*_bff ;if _fadac +2>=len (data ){return _bf .NRGBA {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};_daeg :=uint8 (0xff);if alpha !=nil &&len (alpha )> _bff {_daeg =alpha [_bff ];};_dbdg ,_baad ,_dfcc :=data [_fadac ],data [_fadac +1],data [_fadac +2];if len (decode )==6{_dbdg =uint8 (uint32 (LinearInterpolate (float64 (_dbdg ),0,255,decode [0],decode [1]))&0xff);_baad =uint8 (uint32 (LinearInterpolate (float64 (_baad ),0,255,decode [2],decode [3]))&0xff);_dfcc =uint8 (uint32 (LinearInterpolate (float64 (_dfcc ),0,255,decode [4],decode [5]))&0xff);};return _bf .NRGBA {R :_dbdg ,G :_baad ,B :_dfcc ,A :_daeg },nil ;};func (_bfbc *Gray16 )GrayAt (x ,y int )_bf .Gray {_gbg ,_ :=_bfbc .ColorAt (x ,y );return _bf .Gray {Y :uint8 (_gbg .(_bf .Gray16 ).Y >>8)};};var _ _d .Image =&Gray16 {};func ColorAt (x ,y ,width ,bitsPerColor ,colorComponents ,bytesPerLine int ,data ,alpha []byte ,decode []float64 )(_bf .Color ,error ){switch colorComponents {case 1:return ColorAtGrayscale (x ,y ,bitsPerColor ,bytesPerLine ,data ,decode );case 3:return ColorAtNRGBA (x ,y ,width ,bytesPerLine ,bitsPerColor ,data ,alpha ,decode );case 4:return ColorAtCMYK (x ,y ,width ,data ,decode );default:return nil ,_g .Errorf ("\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064\u0020\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0063o\u006c\u006f\u0072\u0020\u0063\u006f\u006dp\u006f\u006e\u0065\u006e\u0074\u0020\u0066\u006f\u0072\u0020\u0074h\u0065\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0064",colorComponents );};};func (_cae *Gray16 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtGray16BPC (x ,y ,_cae .BytesPerLine ,_cae .Data ,_cae .Decode );};func (_eg *CMYK32 )At (x ,y int )_bf .Color {_aa ,_ :=_eg .ColorAt (x ,y );return _aa };func (_gcga *NRGBA64 )SetNRGBA64 (x ,y int ,c _bf .NRGBA64 ){_edeg :=(y *_gcga .Width +x )*2;_cacg :=_edeg *3;if _cacg +5>=len (_gcga .Data ){return ;};_gcga .setNRGBA64 (_cacg ,c ,_edeg );};var _ _d .Image =&Gray4 {};func (_afd *Gray4 )GrayAt (x ,y int )_bf .Gray {_fcdf ,_ :=ColorAtGray4BPC (x ,y ,_afd .BytesPerLine ,_afd .Data ,_afd .Decode );return _fcdf ;};func (_aabc *NRGBA32 )Set (x ,y int ,c _bf .Color ){_fabc :=y *_aabc .Width +x ;_fcc :=3*_fabc ;if _fcc +2>=len (_aabc .Data ){return ;};_agc :=_bf .NRGBAModel .Convert (c ).(_bf .NRGBA );_aabc .setRGBA (_fabc ,_agc );};func (_edba *NRGBA64 )Set (x ,y int ,c _bf .Color ){_ffaa :=(y *_edba .Width +x )*2;_acga :=_ffaa *3;if _acga +5>=len (_edba .Data ){return ;};_daaa :=_bf .NRGBA64Model .Convert (c ).(_bf .NRGBA64 );_edba .setNRGBA64 (_acga ,_daaa ,_ffaa );};func (_fbdb monochromeModel )Convert (c _bf .Color )_bf .Color {_eca :=_bf .GrayModel .Convert (c ).(_bf .Gray );return _cde (_eca ,_fbdb );};func _bfed (_fgfb _bf .Color )_bf .Color {_dgc :=_bf .NRGBAModel .Convert (_fgfb ).(_bf .NRGBA );return _bgef (_dgc );};var _ _d .Image =&NRGBA32 {};func ColorAtGray1BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_bf .Gray ,error ){_gbb :=y *bytesPerLine +x >>3;if _gbb >=len (data ){return _bf .Gray {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};_ecg :=data [_gbb ]>>uint (7-(x &7))&1;if len (decode )==2{_ecg =uint8 (LinearInterpolate (float64 (_ecg ),0.0,1.0,decode [0],decode [1]))&1;};return _bf .Gray {Y :_ecg *255},nil ;};func (_aba *NRGBA32 )At (x ,y int )_bf .Color {_acd ,_ :=_aba .ColorAt (x ,y );return _acd };var _ Image =&CMYK32 {};var (Gray2Model =_bf .ModelFunc (_dffd );Gray4Model =_bf .ModelFunc (_aefc );NRGBA16Model =_bf .ModelFunc (_bfed ););func (_bbc *Gray8 )At (x ,y int )_bf .Color {_ebc ,_ :=_bbc .ColorAt (x ,y );return _ebc };func (_faabd *Gray16 )SetGray (x ,y int ,g _bf .Gray ){_abfcb :=(y *_faabd .BytesPerLine /2+x )*2;if _abfcb +1>=len (_faabd .Data ){return ;};_faabd .Data [_abfcb ]=g .Y ;_faabd .Data [_abfcb +1]=g .Y ;};func (_egba *Gray2 )GrayAt (x ,y int )_bf .Gray {_egce ,_ :=ColorAtGray2BPC (x ,y ,_egba .BytesPerLine ,_egba .Data ,_egba .Decode );return _egce ;};var _ _d .Image =&Gray2 {};func (_ccc *Gray4 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_ccc .Width ,Y :_ccc .Height }};};func (_bcfe *NRGBA16 )At (x ,y int )_bf .Color {_fagd ,_ :=_bcfe .ColorAt (x ,y );return _fagd };var _ Gray =&Gray2 {};func NewImageBase (width int ,height int ,bitsPerComponent int ,colorComponents int ,data []byte ,alpha []byte ,decode []float64 )ImageBase {_ffbg :=ImageBase {Width :width ,Height :height ,BitsPerComponent :bitsPerComponent ,ColorComponents :colorComponents ,Data :data ,Alpha :alpha ,Decode :decode ,BytesPerLine :BytesPerLine (width ,bitsPerComponent ,colorComponents )};if data ==nil {_ffbg .Data =make ([]byte ,height *_ffbg .BytesPerLine );};return _ffbg ;};func _gfdf (_befa _d .Image ,_fcggb uint8 )*_d .Gray {_febfb :=_befa .Bounds ();_bffg :=_d .NewGray (_febfb );var (_dce _bf .Color ;_daad _bf .Gray ;);for _feea :=0;_feea < _febfb .Max .X ;_feea ++{for _fddc :=0;_fddc < _febfb .Max .Y ;_fddc ++{_dce =_befa .At (_feea ,_fddc );_bffg .Set (_feea ,_fddc ,_dce );_daad =_bffg .GrayAt (_feea ,_fddc );_bffg .SetGray (_feea ,_fddc ,_bf .Gray {Y :_dfge (_daad .Y ,_fcggb )});};};return _bffg ;};func _gef (_bgd _bf .RGBA )_bf .CMYK {_ccg ,_adca ,_gee ,_dd :=_bf .RGBToCMYK (_bgd .R ,_bgd .G ,_bgd .B );return _bf .CMYK {C :_ccg ,M :_adca ,Y :_gee ,K :_dd };};func (_gfd *NRGBA16 )setNRGBA (_fbb ,_ccf ,_ecffb int ,_cdcf _bf .NRGBA ){if _fbb *3%2==0{_gfd .Data [_ecffb ]=(_cdcf .R >>4)<<4|(_cdcf .G >>4);_gfd .Data [_ecffb +1]=(_cdcf .B >>4)<<4|(_gfd .Data [_ecffb +1]&0xf);}else {_gfd .Data [_ecffb ]=(_gfd .Data [_ecffb ]&0xf0)|(_cdcf .R >>4);_gfd .Data [_ecffb +1]=(_cdcf .G >>4)<<4|(_cdcf .B >>4);};if _gfd .Alpha !=nil {_deb :=_ccf *BytesPerLine (_gfd .Width ,4,1);if _deb < len (_gfd .Alpha ){if _fbb %2==0{_gfd .Alpha [_deb ]=(_cdcf .A >>uint (4))<<uint (4)|(_gfd .Alpha [_ecffb ]&0xf);}else {_gfd .Alpha [_deb ]=(_gfd .Alpha [_deb ]&0xf0)|(_cdcf .A >>uint (4));};};};};func _fde (_gdb *_d .Gray )bool {for _edbe :=0;_edbe < len (_gdb .Pix );_edbe ++{if !_gfbg (_gdb .Pix [_edbe ]){return false ;};};return true ;};func (_bfgb *Monochrome )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtGray1BPC (x ,y ,_bfgb .BytesPerLine ,_bfgb .Data ,_bfgb .Decode );};func (_ebf *Monochrome )GrayAt (x ,y int )_bf .Gray {_bdc ,_ :=ColorAtGray1BPC (x ,y ,_ebf .BytesPerLine ,_ebf .Data ,_ebf .Decode );return _bdc ;};func _cfe (_cfeg _d .Image )(Image ,error ){if _aff ,_ffe :=_cfeg .(*Gray8 );_ffe {return _aff .Copy (),nil ;};_dfab :=_cfeg .Bounds ();_bed ,_fdbb :=NewImage (_dfab .Max .X ,_dfab .Max .Y ,8,1,nil ,nil ,nil );if _fdbb !=nil {return nil ,_fdbb ;};_acae (_cfeg ,_bed ,_dfab );return _bed ,nil ;};func LinearInterpolate (x ,xmin ,xmax ,ymin ,ymax float64 )float64 {if _f .Abs (xmax -xmin )< 0.000001{return ymin ;};_abff :=ymin +(x -xmin )*(ymax -ymin )/(xmax -xmin );return _abff ;};func _eeb (_cec _bf .Gray )_bf .Gray {_cec .Y >>=4;_cec .Y |=_cec .Y <<4;return _cec };func _gce (_adec ,_bda NRGBA ,_bbcd _d .Rectangle ){for _cgde :=0;_cgde < _bbcd .Max .X ;_cgde ++{for _cbf :=0;_cbf < _bbcd .Max .Y ;_cbf ++{_bda .SetNRGBA (_cgde ,_cbf ,_adec .NRGBAAt (_cgde ,_cbf ));};};};func _bbf (_dge RGBA ,_ec CMYK ,_egc _d .Rectangle ){for _dgg :=0;_dgg < _egc .Max .X ;_dgg ++{for _aef :=0;_aef < _egc .Max .Y ;_aef ++{_adg :=_dge .RGBAAt (_dgg ,_aef );_ec .SetCMYK (_dgg ,_aef ,_gef (_adg ));};};};func (_fd *Gray4 )Base ()*ImageBase {return &_fd .ImageBase };func (_gcdg *NRGBA16 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtNRGBA16 (x ,y ,_gcdg .Width ,_gcdg .BytesPerLine ,_gcdg .Data ,_gcdg .Alpha ,_gcdg .Decode );};func (_ceg *Gray4 )Copy ()Image {return &Gray4 {ImageBase :_ceg .copy ()}};func GetConverter (bitsPerComponent ,colorComponents int )(ColorConverter ,error ){switch colorComponents {case 1:switch bitsPerComponent {case 1:return MonochromeConverter ,nil ;case 2:return Gray2Converter ,nil ;case 4:return Gray4Converter ,nil ;case 8:return GrayConverter ,nil ;case 16:return Gray16Converter ,nil ;};case 3:switch bitsPerComponent {case 4:return NRGBA16Converter ,nil ;case 8:return NRGBAConverter ,nil ;case 16:return NRGBA64Converter ,nil ;};case 4:return CMYKConverter ,nil ;};return nil ,_g .Errorf ("\u0070\u0072\u006f\u0076\u0069\u0064\u0065\u0064\u0020\u0069\u006e\u0076\u0061l\u0069\u0064\u0020\u0063\u006f\u006c\u006f\u0072\u0043o\u006e\u0076\u0065\u0072\u0074\u0065\u0072\u0020\u0070\u0061\u0072\u0061\u006d\u0065t\u0065\u0072\u0073\u002e\u0020\u0042\u0069\u0074\u0073\u0050\u0065\u0072\u0043\u006f\u006d\u0070\u006f\u006e\u0065\u006e\u0074\u003a\u0020\u0025\u0064\u002c\u0020\u0043\u006f\u006co\u0072\u0043\u006f\u006d\u0070\u006f\u006e\u0065\u006et\u0073\u003a \u0025\u0064",bitsPerComponent ,colorComponents );};var _ _d .Image =&Gray8 {};func (_adgg *NRGBA64 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_adgg .Width ,Y :_adgg .Height }};};func (_gde *Monochrome )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_gde .Width ,Y :_gde .Height }};};func _cef (_cbd _d .Image )(Image ,error ){if _cffg ,_gffb :=_cbd .(*Gray4 );_gffb {return _cffg .Copy (),nil ;};_aecg :=_cbd .Bounds ();_ffa ,_edaf :=NewImage (_aecg .Max .X ,_aecg .Max .Y ,4,1,nil ,nil ,nil );if _edaf !=nil {return nil ,_edaf ;};_acae (_cbd ,_ffa ,_aecg );return _ffa ,nil ;};func (_aacd *Gray4 )ColorModel ()_bf .Model {return Gray4Model };func _gfbg (_caf uint8 )bool {if _caf ==0||_caf ==255{return true ;};return false ;};type Gray interface{GrayAt (_dcfe ,_cba int )_bf .Gray ;SetGray (_eed ,_gaf int ,_gge _bf .Gray );};func _dfge (_dbag ,_dece uint8 )uint8 {if _dbag < _dece {return 255;};return 0;};func (_cgce *Gray2 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_cgce .Width ,Y :_cgce .Height }};};func AddDataPadding (width ,height ,bitsPerComponent ,colorComponents int ,data []byte )([]byte ,error ){_fada :=BytesPerLine (width ,bitsPerComponent ,colorComponents );if _fada ==width *colorComponents *bitsPerComponent /8{return data ,nil ;};_dde :=width *colorComponents *bitsPerComponent ;_bbef :=_fada *8;_dccc :=8-(_bbef -_dde );_abffb :=_gd .NewReader (data );_dfd :=_fada -1;_fbcd :=make ([]byte ,_dfd );_feg :=make ([]byte ,height *_fada );_gdcd :=_gd .NewWriterMSB (_feg );var _abbe uint64 ;var _gca error ;for _dggce :=0;_dggce < height ;_dggce ++{_ ,_gca =_abffb .Read (_fbcd );if _gca !=nil {return nil ,_gca ;};_ ,_gca =_gdcd .Write (_fbcd );if _gca !=nil {return nil ,_gca ;};_abbe ,_gca =_abffb .ReadBits (byte (_dccc ));if _gca !=nil {return nil ,_gca ;};_ ,_gca =_gdcd .WriteBits (_abbe ,_dccc );if _gca !=nil {return nil ,_gca ;};_gdcd .FinishByte ();};return _feg ,nil ;};func _aad (_fca _bf .NRGBA )_bf .CMYK {_af ,_fe ,_gf ,_ :=_fca .RGBA ();_aac ,_aae ,_dfa ,_gfb :=_bf .RGBToCMYK (uint8 (_af >>8),uint8 (_fe >>8),uint8 (_gf >>8));return _bf .CMYK {C :_aac ,M :_aae ,Y :_dfa ,K :_gfb };};var _ _d .Image =&NRGBA64 {};func (_fda *Gray4 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtGray4BPC (x ,y ,_fda .BytesPerLine ,_fda .Data ,_fda .Decode );};func (_bc *CMYK32 )Base ()*ImageBase {return &_bc .ImageBase };func (_bdd *NRGBA64 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtNRGBA64 (x ,y ,_bdd .Width ,_bdd .Data ,_bdd .Alpha ,_bdd .Decode );};func ColorAtNRGBA (x ,y ,width ,bytesPerLine ,bitsPerColor int ,data ,alpha []byte ,decode []float64 )(_bf .Color ,error ){switch bitsPerColor {case 4:return ColorAtNRGBA16 (x ,y ,width ,bytesPerLine ,data ,alpha ,decode );case 8:return ColorAtNRGBA32 (x ,y ,width ,data ,alpha ,decode );case 16:return ColorAtNRGBA64 (x ,y ,width ,data ,alpha ,decode );default:return nil ,_g .Errorf ("\u0075\u006e\u0073\u0075\u0070\u0070\u006fr\u0074\u0065\u0064 \u0072\u0067\u0062\u0020b\u0069\u0074\u0073\u0020\u0070\u0065\u0072\u0020\u0063\u006f\u006c\u006f\u0072\u0020\u0061\u006d\u006f\u0075\u006e\u0074\u003a\u0020\u0027\u0025\u0064\u0027",bitsPerColor );};};func (_dfe *Gray2 )At (x ,y int )_bf .Color {_dac ,_ :=_dfe .ColorAt (x ,y );return _dac };func _cffd (_gddf RGBA ,_eegd NRGBA ,_debb _d .Rectangle ){for _bcbc :=0;_bcbc < _debb .Max .X ;_bcbc ++{for _fegg :=0;_fegg < _debb .Max .Y ;_fegg ++{_gfe :=_gddf .RGBAAt (_bcbc ,_fegg );_eegd .SetNRGBA (_bcbc ,_fegg ,_gfbf (_gfe ));};};};func (_faa *CMYK32 )Copy ()Image {return &CMYK32 {ImageBase :_faa .copy ()}};func (_edcb *Gray16 )At (x ,y int )_bf .Color {_gdd ,_ :=_edcb .ColorAt (x ,y );return _gdd };func (_gg *CMYK32 )Validate ()error {if len (_gg .Data )!=4*_gg .Width *_gg .Height {return _b .New ("i\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0069\u006da\u0067\u0065\u0020\u0064\u0061\u0074\u0061 s\u0069\u007a\u0065\u0020f\u006f\u0072\u0020\u0070\u0072\u006f\u0076\u0069\u0064ed\u0020\u0064i\u006d\u0065\u006e\u0073\u0069\u006f\u006e\u0073");};return nil ;};func (_eda *Monochrome )setBit (_dcc ,_fac int ){_eda .Data [_dcc ]|=0x80>>uint (_fac &7)};func AutoThresholdTriangle (histogram [256]int )uint8 {var _fegf ,_daf ,_cbe ,_bdg int ;for _aded :=0;_aded < len (histogram );_aded ++{if histogram [_aded ]> 0{_fegf =_aded ;break ;};};if _fegf > 0{_fegf --;};for _gcc :=255;_gcc > 0;_gcc --{if histogram [_gcc ]> 0{_bdg =_gcc ;break ;};};if _bdg < 255{_bdg ++;};for _fgc :=0;_fgc < 256;_fgc ++{if histogram [_fgc ]> _daf {_cbe =_fgc ;_daf =histogram [_fgc ];};};var _efag bool ;if (_cbe -_fegf )< (_bdg -_cbe ){_efag =true ;var _dbaa int ;_cbfb :=255;for _dbaa < _cbfb {_ggfb :=histogram [_dbaa ];histogram [_dbaa ]=histogram [_cbfb ];histogram [_cbfb ]=_ggfb ;_dbaa ++;_cbfb --;};_fegf =255-_bdg ;_cbe =255-_cbe ;};if _fegf ==_cbe {return uint8 (_fegf );};_ecgf :=float64 (histogram [_cbe ]);_gaa :=float64 (_fegf -_cbe );_gcea :=_f .Sqrt (_ecgf *_ecgf +_gaa *_gaa );_ecgf /=_gcea ;_gaa /=_gcea ;_gcea =_ecgf *float64 (_fegf )+_gaa *float64 (histogram [_fegf ]);_cdcc :=_fegf ;var _bcgc float64 ;for _dgba :=_fegf +1;_dgba <=_cbe ;_dgba ++{_feeb :=_ecgf *float64 (_dgba )+_gaa *float64 (histogram [_dgba ])-_gcea ;if _feeb > _bcgc {_cdcc =_dgba ;_bcgc =_feeb ;};};_cdcc --;if _efag {var _bcgb int ;_bbge :=255;for _bcgb < _bbge {_ceb :=histogram [_bcgb ];histogram [_bcgb ]=histogram [_bbge ];histogram [_bbge ]=_ceb ;_bcgb ++;_bbge --;};return uint8 (255-_cdcc );};return uint8 (_cdcc );};func (_acc *Gray4 )SetGray (x ,y int ,g _bf .Gray ){if x >=_acc .Width ||y >=_acc .Height {return ;};g =_eeb (g );_acc .setGray (x ,y ,g );};func (_fgfg *Gray16 )Copy ()Image {return &Gray16 {ImageBase :_fgfg .copy ()}};var _ Gray =&Gray8 {};func _fdad (_gbf CMYK ,_fcb Gray ,_cffge _d .Rectangle ){for _agbd :=0;_agbd < _cffge .Max .X ;_agbd ++{for _cgcc :=0;_cgcc < _cffge .Max .Y ;_cgcc ++{_fdbd :=_bbd (_gbf .CMYKAt (_agbd ,_cgcc ));_fcb .SetGray (_agbd ,_cgcc ,_fdbd );};};};func (_gfc *Gray8 )Base ()*ImageBase {return &_gfc .ImageBase };func _eefe (_cac _bf .Gray )_bf .Gray {_bfb :=_cac .Y >>6;_bfb |=_bfb <<2;_cac .Y =_bfb |_bfb <<4;return _cac ;};func (_dgf *Monochrome )ColorModel ()_bf .Model {return MonochromeModel (_dgf .ModelThreshold )};func GrayHistogram (g Gray )(_efdd [256]int ){switch _gafc :=g .(type ){case Histogramer :return _gafc .Histogram ();case _d .Image :_cfgd :=_gafc .Bounds ();for _bcge :=0;_bcge < _cfgd .Max .X ;_bcge ++{for _cbaab :=0;_cbaab < _cfgd .Max .Y ;_cbaab ++{_efdd [g .GrayAt (_bcge ,_cbaab ).Y ]++;};};return _efdd ;default:return [256]int {};};};func _gfbf (_cdfe _bf .RGBA )_bf .NRGBA {switch _cdfe .A {case 0xff:return _bf .NRGBA {R :_cdfe .R ,G :_cdfe .G ,B :_cdfe .B ,A :0xff};case 0x00:return _bf .NRGBA {};default:_cdca ,_db ,_eb ,_adc :=_cdfe .RGBA ();_cdca =(_cdca *0xffff)/_adc ;_db =(_db *0xffff)/_adc ;_eb =(_eb *0xffff)/_adc ;return _bf .NRGBA {R :uint8 (_cdca >>8),G :uint8 (_db >>8),B :uint8 (_eb >>8),A :uint8 (_adc >>8)};};};func _abc (_cdf _bf .Gray )_bf .CMYK {return _bf .CMYK {K :0xff-_cdf .Y }};func _ffc (_edge _d .Image )(Image ,error ){if _dfee ,_ddcf :=_edge .(*Gray16 );_ddcf {return _dfee .Copy (),nil ;};_bcf :=_edge .Bounds ();_fdc ,_ecb :=NewImage (_bcf .Max .X ,_bcf .Max .Y ,16,1,nil ,nil ,nil );if _ecb !=nil {return nil ,_ecb ;};_acae (_edge ,_fdc ,_bcf );return _fdc ,nil ;};func (_bccg *NRGBA32 )ColorAt (x ,y int )(_bf .Color ,error ){return ColorAtNRGBA32 (x ,y ,_bccg .Width ,_bccg .Data ,_bccg .Alpha ,_bccg .Decode );};type Gray8 struct{ImageBase };func (_gebd *NRGBA32 )Base ()*ImageBase {return &_gebd .ImageBase };type Monochrome struct{ImageBase ;ModelThreshold uint8 ;};func (_befg *ImageBase )copy ()ImageBase {_aaba :=*_befg ;_aaba .Data =make ([]byte ,len (_befg .Data ));copy (_aaba .Data ,_befg .Data );return _aaba ;};func (_fbc *CMYK32 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_fbc .Width ,Y :_fbc .Height }};};func _bbag (){for _adgd :=0;_adgd < 256;_adgd ++{_bcg [_adgd ]=uint8 (_adgd &0x1)+(uint8 (_adgd >>1)&0x1)+(uint8 (_adgd >>2)&0x1)+(uint8 (_adgd >>3)&0x1)+(uint8 (_adgd >>4)&0x1)+(uint8 (_adgd >>5)&0x1)+(uint8 (_adgd >>6)&0x1)+(uint8 (_adgd >>7)&0x1);};};func (_ca *Monochrome )At (x ,y int )_bf .Color {_daa ,_ :=_ca .ColorAt (x ,y );return _daa };type Histogramer interface{Histogram ()[256]int ;};func _edc (_de Gray ,_eef CMYK ,_abf _d .Rectangle ){for _ged :=0;_ged < _abf .Max .X ;_ged ++{for _gbd :=0;_gbd < _abf .Max .Y ;_gbd ++{_dcf :=_de .GrayAt (_ged ,_gbd );_eef .SetCMYK (_ged ,_gbd ,_abc (_dcf ));};};};func (_baf *Gray8 )SetGray (x ,y int ,g _bf .Gray ){_beg :=y *_baf .BytesPerLine +x ;if _beg > len (_baf .Data )-1{return ;};_baf .Data [_beg ]=g .Y ;};func (_ded *NRGBA64 )setNRGBA64 (_fbfb int ,_dbgc _bf .NRGBA64 ,_ecce int ){_ded .Data [_fbfb ]=uint8 (_dbgc .R >>8);_ded .Data [_fbfb +1]=uint8 (_dbgc .R &0xff);_ded .Data [_fbfb +2]=uint8 (_dbgc .G >>8);_ded .Data [_fbfb +3]=uint8 (_dbgc .G &0xff);_ded .Data [_fbfb +4]=uint8 (_dbgc .B >>8);_ded .Data [_fbfb +5]=uint8 (_dbgc .B &0xff);if _ecce +1< len (_ded .Alpha ){_ded .Alpha [_ecce ]=uint8 (_dbgc .A >>8);_ded .Alpha [_ecce +1]=uint8 (_dbgc .A &0xff);};};func (_ggf *Gray2 )Set (x ,y int ,c _bf .Color ){if x >=_ggf .Width ||y >=_ggf .Height {return ;};_fee :=Gray2Model .Convert (c ).(_bf .Gray );_gff :=y *_ggf .BytesPerLine ;_cfdgf :=_gff +(x >>2);_fcg :=_fee .Y >>6;_ggf .Data [_cfdgf ]=(_ggf .Data [_cfdgf ]&(^(0xc0>>uint (2*((x )&3)))))|(_fcg <<uint (6-2*(x &3)));};func _fgb (_dfg int ,_edbc int )error {return _g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",_dfg ,_edbc );};type Image interface{_fb .Image ;Base ()*ImageBase ;Copy ()Image ;Pix ()[]byte ;ColorAt (_ggfa ,_dccf int )(_bf .Color ,error );Validate ()error ;};func (_eedc *NRGBA16 )ColorModel ()_bf .Model {return NRGBA16Model };func _dee (_cffe ,_ebfa Gray ,_bde _d .Rectangle ){for _beaf :=0;_beaf < _bde .Max .X ;_beaf ++{for _gag :=0;_gag < _bde .Max .Y ;_gag ++{_ebfa .SetGray (_beaf ,_gag ,_cffe .GrayAt (_beaf ,_gag ));};};};type Gray4 struct{ImageBase };func (_ff *Gray2 )Base ()*ImageBase {return &_ff .ImageBase };func ColorAtGray2BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_bf .Gray ,error ){_bcd :=y *bytesPerLine +x >>2;if _bcd >=len (data ){return _bf .Gray {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};_gac :=data [_bcd ]>>uint (6-(x &3)*2)&3;if len (decode )==2{_gac =uint8 (uint32 (LinearInterpolate (float64 (_gac ),0,3.0,decode [0],decode [1]))&3);};return _bf .Gray {Y :_gac *85},nil ;};func (_bbcf *ImageBase )MakeAlpha (){_bbcf .newAlpha ()};func _ggd (_acag _d .Image )(Image ,error ){if _ffb ,_abb :=_acag .(*Gray2 );_abb {return _ffb .Copy (),nil ;};_adea :=_acag .Bounds ();_fed ,_agf :=NewImage (_adea .Max .X ,_adea .Max .Y ,2,1,nil ,nil ,nil );if _agf !=nil {return nil ,_agf ;};_acae (_acag ,_fed ,_adea );return _fed ,nil ;};type CMYK interface{CMYKAt (_dc ,_a int )_bf .CMYK ;SetCMYK (_fa ,_ac int ,_c _bf .CMYK );};func _cgfc (_cbbb *_d .Gray16 ,_gaea uint8 )*_d .Gray {_fbge :=_cbbb .Bounds ();_ffbe :=_d .NewGray (_fbge );for _fcgg :=0;_fcgg < _fbge .Dx ();_fcgg ++{for _efea :=0;_efea < _fbge .Dy ();_efea ++{_ffg :=_cbbb .Gray16At (_fcgg ,_efea );_ffbe .SetGray (_fcgg ,_efea ,_bf .Gray {Y :_dfge (uint8 (_ffg .Y /256),_gaea )});};};return _ffbe ;};func (_gdc *CMYK32 )Set (x ,y int ,c _bf .Color ){_bbe :=4*(y *_gdc .Width +x );if _bbe +3>=len (_gdc .Data ){return ;};_acf :=_bf .CMYKModel .Convert (c ).(_bf .CMYK );_gdc .Data [_bbe ]=_acf .C ;_gdc .Data [_bbe +1]=_acf .M ;_gdc .Data [_bbe +2]=_acf .Y ;_gdc .Data [_bbe +3]=_acf .K ;};func _ea (_cda NRGBA ,_fag CMYK ,_ae _d .Rectangle ){for _ad :=0;_ad < _ae .Max .X ;_ad ++{for _efa :=0;_efa < _ae .Max .Y ;_efa ++{_cdg :=_cda .NRGBAAt (_ad ,_efa );_fag .SetCMYK (_ad ,_efa ,_aad (_cdg ));};};};func (_eggb *Gray4 )Set (x ,y int ,c _bf .Color ){if x >=_eggb .Width ||y >=_eggb .Height {return ;};_ccba :=Gray4Model .Convert (c ).(_bf .Gray );_eggb .setGray (x ,y ,_ccba );};func (_aag *NRGBA16 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_aag .Width ,Y :_aag .Height }};};func (_adf *Gray2 )Copy ()Image {return &Gray2 {ImageBase :_adf .copy ()}};func (_afdc *NRGBA32 )Validate ()error {if len (_afdc .Data )!=3*_afdc .Width *_afdc .Height {return _b .New ("i\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0069\u006da\u0067\u0065\u0020\u0064\u0061\u0074\u0061 s\u0069\u007a\u0065\u0020f\u006f\u0072\u0020\u0070\u0072\u006f\u0076\u0069\u0064ed\u0020\u0064i\u006d\u0065\u006e\u0073\u0069\u006f\u006e\u0073");};return nil ;};func ColorAtGray4BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_bf .Gray ,error ){_aadg :=y *bytesPerLine +x >>1;if _aadg >=len (data ){return _bf .Gray {},_g .Errorf ("\u0069\u006d\u0061\u0067\u0065\u0020\u0063\u006f\u006f\u0072\u0064\u0069\u006ea\u0074\u0065\u0073\u0020\u006f\u0075t\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065\u0020\u0028\u0025\u0064,\u0020\u0025\u0064\u0029",x ,y );};_bdf :=data [_aadg ]>>uint (4-(x &1)*4)&0xf;if len (decode )==2{_bdf =uint8 (uint32 (LinearInterpolate (float64 (_bdf ),0,15,decode [0],decode [1]))&0xf);};return _bf .Gray {Y :_bdf *17&0xff},nil ;};func (_efed *NRGBA64 )NRGBA64At (x ,y int )_bf .NRGBA64 {_bfba ,_ :=ColorAtNRGBA64 (x ,y ,_efed .Width ,_efed .Data ,_efed .Alpha ,_efed .Decode );return _bfba ;};func _fcga (_cbfc Gray ,_cfega NRGBA ,_aeba _d .Rectangle ){for _fcbf :=0;_fcbf < _aeba .Max .X ;_fcbf ++{for _ced :=0;_ced < _aeba .Max .Y ;_ced ++{_fgg :=_cbfc .GrayAt (_fcbf ,_ced );_cfega .SetNRGBA (_fcbf ,_ced ,_dad (_fgg ));};};};func (_bgdd *NRGBA16 )NRGBAAt (x ,y int )_bf .NRGBA {_eec ,_ :=ColorAtNRGBA16 (x ,y ,_bgdd .Width ,_bgdd .BytesPerLine ,_bgdd .Data ,_bgdd .Alpha ,_bgdd .Decode );return _eec ;};type CMYK32 struct{ImageBase };func _gad (_efd _d .Image )(Image ,error ){if _aab ,_gec :=_efd .(*Monochrome );_gec {return _aab ,nil ;};_bba ,_acaa :=_efd .(Gray );if !_acaa {_cdd ,_bcc :=GrayConverter .Convert (_efd );if _bcc !=nil {return nil ,_bcc ;};_bba =_cdd .(Gray );};_ggg :=_efd .Bounds ();_dbg ,_acg :=NewImage (_ggg .Max .X ,_ggg .Max .Y ,1,1,nil ,nil ,nil );if _acg !=nil {return nil ,_acg ;};_edgc :=_dbg .(*Monochrome );_cfdg :=AutoThresholdTriangle (GrayHistogram (_bba ));for _dgb :=0;_dgb < _ggg .Max .X ;_dgb ++{for _dgbc :=0;_dgbc < _ggg .Max .Y ;_dgbc ++{_gga :=_cde (_bba .GrayAt (_dgb ,_dgbc ),monochromeModel (_cfdg ));_edgc .SetGray (_dgb ,_dgbc ,_gga );};};return _dbg ,nil ;};type ColorConverter interface{Convert (_dae _d .Image )(Image ,error );};func _gebb (_eee _d .Image )(Image ,error ){if _gegb ,_fdd :=_eee .(*NRGBA64 );_fdd {return _gegb .Copy (),nil ;};_dcce :=_eee .Bounds ();_gace ,_fabf :=NewImage (_dcce .Max .X ,_dcce .Max .Y ,16,3,nil ,nil ,nil );if _fabf !=nil {return nil ,_fabf ;};_fceg (_eee ,_gace ,_dcce );return _gace ,nil ;}; |