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 (_e "errors";_b "fmt";_fg "github.com/unidoc/unipdf/v3/internal/bitwise";_d "image";_g "image/color";_gd "image/draw";_c "math";);var _ _d .Image =&Gray16 {};func _ceb (_dbcd _g .Color )_g .Color {_cebf :=_g .GrayModel .Convert (_dbcd ).(_g .Gray );return _bdee (_cebf );};func (_fgf *Gray2 )Copy ()Image {return &Gray2 {ImageBase :_fgf .copy ()}};func (_cde *Gray16 )GrayAt (x ,y int )_g .Gray {_cfcf ,_ :=_cde .ColorAt (x ,y );return _g .Gray {Y :uint8 (_cfcf .(_g .Gray16 ).Y >>8)};};func _ace (_ced _d .Image )(Image ,error ){if _dcgf ,_ccge :=_ced .(*Gray4 );_ccge {return _dcgf .Copy (),nil ;};_bca :=_ced .Bounds ();_gag ,_ded :=NewImage (_bca .Max .X ,_bca .Max .Y ,4,1,nil ,nil ,nil );if _ded !=nil {return nil ,_ded ;};_dggc (_ced ,_gag ,_bca );return _gag ,nil ;};var _ Image =&Gray16 {};var (Gray2Model =_g .ModelFunc (_ceb );Gray4Model =_g .ModelFunc (_fcce );NRGBA16Model =_g .ModelFunc (_fabe ););type Gray2 struct{ImageBase };var _ Gray =&Gray2 {};func ColorAtGray2BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_g .Gray ,error ){_dabf :=y *bytesPerLine +x >>2;if _dabf >=len (data ){return _g .Gray {},_b .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 );};_bgc :=data [_dabf ]>>uint (6-(x &3)*2)&3;if len (decode )==2{_bgc =uint8 (uint32 (LinearInterpolate (float64 (_bgc ),0,3.0,decode [0],decode [1]))&3);};return _g .Gray {Y :_bgc *85},nil ;};func (_agaa *Gray16 )Histogram ()(_feeb [256]int ){for _eeg :=0;_eeg < _agaa .Width ;_eeg ++{for _ceg :=0;_ceg < _agaa .Height ;_ceg ++{_feeb [_agaa .GrayAt (_eeg ,_ceg ).Y ]++;};};return _feeb ;};func (_geef *ImageBase )MakeAlpha (){_geef .newAlpha ()};func _ebg (_fdc _d .Image ,_bg Image ,_ff _d .Rectangle ){for _fab :=0;_fab < _ff .Max .X ;_fab ++{for _dg :=0;_dg < _ff .Max .Y ;_dg ++{_dgc :=_fdc .At (_fab ,_dg );_bg .Set (_fab ,_dg ,_dgc );};};};func _gfcc (_fgfg _g .NRGBA )_g .NRGBA {_fgfg .R =_fgfg .R >>4|(_fgfg .R >>4)<<4;_fgfg .G =_fgfg .G >>4|(_fgfg .G >>4)<<4;_fgfg .B =_fgfg .B >>4|(_fgfg .B >>4)<<4;return _fgfg ;};func (_badf *Gray8 )ColorModel ()_g .Model {return _g .GrayModel };func _gbf (_fb _g .NRGBA )_g .CMYK {_aa ,_ecgd ,_aga ,_ :=_fb .RGBA ();_bab ,_ab ,_caa ,_dfe :=_g .RGBToCMYK (uint8 (_aa >>8),uint8 (_ecgd >>8),uint8 (_aga >>8));return _g .CMYK {C :_bab ,M :_ab ,Y :_caa ,K :_dfe };};var _ Gray =&Monochrome {};func (_cfac *NRGBA64 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_cfac .Width ,Y :_cfac .Height }};};func MonochromeModel (threshold uint8 )_g .Model {return monochromeModel (threshold )};type Gray4 struct{ImageBase };func (_afce *Gray8 )Copy ()Image {return &Gray8 {ImageBase :_afce .copy ()}};func _bdee (_dffb _g .Gray )_g .Gray {_eaa :=_dffb .Y >>6;_eaa |=_eaa <<2;_dffb .Y =_eaa |_eaa <<4;return _dffb ;};func _ebfa (_aefa *_d .Gray16 ,_bec uint8 )*_d .Gray {_aacc :=_aefa .Bounds ();_bged :=_d .NewGray (_aacc );for _cbab :=0;_cbab < _aacc .Dx ();_cbab ++{for _gbgc :=0;_gbgc < _aacc .Dy ();_gbgc ++{_bbd :=_aefa .Gray16At (_cbab ,_gbgc );_bged .SetGray (_cbab ,_gbgc ,_g .Gray {Y :_gdfd (uint8 (_bbd .Y /256),_bec )});};};return _bged ;};func _fa (_adg Gray ,_ecf CMYK ,_beg _d .Rectangle ){for _bde :=0;_bde < _beg .Max .X ;_bde ++{for _fef :=0;_fef < _beg .Max .Y ;_fef ++{_fcf :=_adg .GrayAt (_bde ,_fef );_ecf .SetCMYK (_bde ,_fef ,_fdcc (_fcf ));};};};var _ _d .Image =&Gray4 {};func _fdcc (_fbg _g .Gray )_g .CMYK {return _g .CMYK {K :0xff-_fbg .Y }};func ImgToBinary (i _d .Image ,threshold uint8 )*_d .Gray {switch _bea :=i .(type ){case *_d .Gray :if _dgfb (_bea ){return _bea ;};return _cfab (_bea ,threshold );case *_d .Gray16 :return _ebfa (_bea ,threshold );default:return _aggaa (_bea ,threshold );};};func (_bbba *Gray8 )GrayAt (x ,y int )_g .Gray {_cac ,_ :=ColorAtGray8BPC (x ,y ,_bbba .BytesPerLine ,_bbba .Data ,_bbba .Decode );return _cac ;};var _ Image =&Gray8 {};func ColorAtGray8BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_g .Gray ,error ){_fec :=y *bytesPerLine +x ;if _fec >=len (data ){return _g .Gray {},_b .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 );};_eac :=data [_fec ];if len (decode )==2{_eac =uint8 (uint32 (LinearInterpolate (float64 (_eac ),0,255,decode [0],decode [1]))&0xff);};return _g .Gray {Y :_eac },nil ;};func _fcce (_dcg _g .Color )_g .Color {_bgf :=_g .GrayModel .Convert (_dcg ).(_g .Gray );return _cgcg (_bgf )};func ColorAtNRGBA64 (x ,y ,width int ,data ,alpha []byte ,decode []float64 )(_g .NRGBA64 ,error ){_eccc :=(y *width +x )*2;_cdfa :=_eccc *3;if _cdfa +5>=len (data ){return _g .NRGBA64 {},_b .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 _agfb =0xffff;_dbba :=uint16 (_agfb );if alpha !=nil &&len (alpha )> _eccc +1{_dbba =uint16 (alpha [_eccc ])<<8|uint16 (alpha [_eccc +1]);};_faag :=uint16 (data [_cdfa ])<<8|uint16 (data [_cdfa +1]);_adcf :=uint16 (data [_cdfa +2])<<8|uint16 (data [_cdfa +3]);_fccg :=uint16 (data [_cdfa +4])<<8|uint16 (data [_cdfa +5]);if len (decode )==6{_faag =uint16 (uint64 (LinearInterpolate (float64 (_faag ),0,65535,decode [0],decode [1]))&_agfb );_adcf =uint16 (uint64 (LinearInterpolate (float64 (_adcf ),0,65535,decode [2],decode [3]))&_agfb );_fccg =uint16 (uint64 (LinearInterpolate (float64 (_fccg ),0,65535,decode [4],decode [5]))&_agfb );};return _g .NRGBA64 {R :_faag ,G :_adcf ,B :_fccg ,A :_dbba },nil ;};func _cgcg (_afd _g .Gray )_g .Gray {_afd .Y >>=4;_afd .Y |=_afd .Y <<4;return _afd };func _age (_dca _g .Gray )_g .NRGBA {return _g .NRGBA {R :_dca .Y ,G :_dca .Y ,B :_dca .Y ,A :0xff}};type CMYK interface{CMYKAt (_de ,_ec int )_g .CMYK ;SetCMYK (_cf ,_cg int ,_fd _g .CMYK );};func (_fcab *NRGBA16 )NRGBAAt (x ,y int )_g .NRGBA {_edd ,_ :=ColorAtNRGBA16 (x ,y ,_fcab .Width ,_fcab .BytesPerLine ,_fcab .Data ,_fcab .Alpha ,_fcab .Decode );return _edd ;};func (_afcg *Gray2 )Validate ()error {if len (_afcg .Data )!=_afcg .Height *_afcg .BytesPerLine {return ErrInvalidImage ;};return nil ;};func (_egbg *Gray4 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_egbg .Width ,Y :_egbg .Height }};};var (MonochromeConverter =ConverterFunc (_cbc );Gray2Converter =ConverterFunc (_gda );Gray4Converter =ConverterFunc (_ace );GrayConverter =ConverterFunc (_efda );Gray16Converter =ConverterFunc (_bfb );NRGBA16Converter =ConverterFunc (_ddgb );NRGBAConverter =ConverterFunc (_fddd );NRGBA64Converter =ConverterFunc (_badc );CMYKConverter =ConverterFunc (_a ););func init (){_ffg ()};func (_cbdd *NRGBA32 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_cbdd .Width ,Y :_cbdd .Height }};};type monochromeModel uint8 ;func FromGoImage (i _d .Image )(Image ,error ){switch _dcb :=i .(type ){case Image :return _dcb .Copy (),nil ;case Gray :return GrayConverter .Convert (i );case CMYK :return CMYKConverter .Convert (i );default:return NRGBAConverter .Convert (i );};};func (_eaba *NRGBA32 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtNRGBA32 (x ,y ,_eaba .Width ,_eaba .Data ,_eaba .Alpha ,_eaba .Decode );};var _ _d .Image =&NRGBA16 {};func _badc (_affa _d .Image )(Image ,error ){if _edgb ,_ceaa :=_affa .(*NRGBA64 );_ceaa {return _edgb .Copy (),nil ;};_agga :=_affa .Bounds ();_gfbbg ,_gbgd :=NewImage (_agga .Max .X ,_agga .Max .Y ,16,3,nil ,nil ,nil );if _gbgd !=nil {return nil ,_gbgd ;};_dfgc (_affa ,_gfbbg ,_agga );return _gfbbg ,nil ;};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 ,_b .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 =&NRGBA32 {};func (_gae *Gray4 )GrayAt (x ,y int )_g .Gray {_efd ,_ :=ColorAtGray4BPC (x ,y ,_gae .BytesPerLine ,_gae .Data ,_gae .Decode );return _efd ;};func _dcdb (_daf NRGBA ,_fdd Gray ,_ffab _d .Rectangle ){for _bdc :=0;_bdc < _ffab .Max .X ;_bdc ++{for _cfbd :=0;_cfbd < _ffab .Max .Y ;_cfbd ++{_fgd :=_egb (_daf .NRGBAAt (_bdc ,_cfbd ));_fdd .SetGray (_bdc ,_cfbd ,_fgd );};};};func (_ddcg *NRGBA64 )ColorModel ()_g .Model {return _g .NRGBA64Model };func _cfab (_ceae *_d .Gray ,_gfbe uint8 )*_d .Gray {_aabbf :=_ceae .Bounds ();_bgce :=_d .NewGray (_aabbf );for _bgfc :=0;_bgfc < _aabbf .Dx ();_bgfc ++{for _beda :=0;_beda < _aabbf .Dy ();_beda ++{_gade :=_ceae .GrayAt (_bgfc ,_beda );_bgce .SetGray (_bgfc ,_beda ,_g .Gray {Y :_gdfd (_gade .Y ,_gfbe )});};};return _bgce ;};var _ Image =&NRGBA32 {};func (_eef *Monochrome )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtGray1BPC (x ,y ,_eef .BytesPerLine ,_eef .Data ,_eef .Decode );};func (_afc *Monochrome )setBit (_geg ,_efc int ){_afc .Data [_geg ]|=0x80>>uint (_efc &7)};func _fccc (_gegac RGBA ,_cdab NRGBA ,_aade _d .Rectangle ){for _fgdf :=0;_fgdf < _aade .Max .X ;_fgdf ++{for _eacc :=0;_eacc < _aade .Max .Y ;_eacc ++{_eea :=_gegac .RGBAAt (_fgdf ,_eacc );_cdab .SetNRGBA (_fgdf ,_eacc ,_dge (_eea ));};};};func (_dcac *Gray4 )Set (x ,y int ,c _g .Color ){if x >=_dcac .Width ||y >=_dcac .Height {return ;};_dfg :=Gray4Model .Convert (c ).(_g .Gray );_dcac .setGray (x ,y ,_dfg );};func (_edc *NRGBA64 )Base ()*ImageBase {return &_edc .ImageBase };func (_efb *Monochrome )Histogram ()(_bbb [256]int ){for _ ,_fcd :=range _efb .Data {_bbb [0xff]+=int (_gde [_efb .Data [_fcd ]]);};return _bbb ;};type ImageBase struct{Width ,Height int ;BitsPerComponent ,ColorComponents int ;Data ,Alpha []byte ;Decode []float64 ;BytesPerLine int ;};func _bce (_dga ,_bccb Gray ,_cbcf _d .Rectangle ){for _ccgc :=0;_ccgc < _cbcf .Max .X ;_ccgc ++{for _eecc :=0;_eecc < _cbcf .Max .Y ;_eecc ++{_bccb .SetGray (_ccgc ,_eecc ,_dga .GrayAt (_ccgc ,_eecc ));};};};func _gda (_agg _d .Image )(Image ,error ){if _aef ,_cfc :=_agg .(*Gray2 );_cfc {return _aef .Copy (),nil ;};_ccg :=_agg .Bounds ();_bbg ,_dfee :=NewImage (_ccg .Max .X ,_ccg .Max .Y ,2,1,nil ,nil ,nil );if _dfee !=nil {return nil ,_dfee ;};_dggc (_agg ,_bbg ,_ccg );return _bbg ,nil ;};func _daca (_deaf uint8 )bool {if _deaf ==0||_deaf ==255{return true ;};return false ;};func (_dccb monochromeModel )Convert (c _g .Color )_g .Color {_aee :=_g .GrayModel .Convert (c ).(_g .Gray );return _fdcd (_aee ,_dccb );};func (_gcd *Gray4 )ColorModel ()_g .Model {return Gray4Model };var _ Image =&Gray2 {};func (_acb *Monochrome )ColorModel ()_g .Model {return MonochromeModel (_acb .ModelThreshold )};func (_gb *CMYK32 )Copy ()Image {return &CMYK32 {ImageBase :_gb .copy ()}};func (_eae *NRGBA16 )Copy ()Image {return &NRGBA16 {ImageBase :_eae .copy ()}};func ColorAtGrayscale (x ,y ,bitsPerColor ,bytesPerLine int ,data []byte ,decode []float64 )(_g .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 ,_b .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 ImgToGray (i _d .Image )*_d .Gray {if _cebca ,_bgfa :=i .(*_d .Gray );_bgfa {return _cebca ;};_cega :=i .Bounds ();_eedb :=_d .NewGray (_cega );for _bcg :=0;_bcg < _cega .Max .X ;_bcg ++{for _gefb :=0;_gefb < _cega .Max .Y ;_gefb ++{_gedd :=i .At (_bcg ,_gefb );_eedb .Set (_bcg ,_gefb ,_gedd );};};return _eedb ;};func ColorAtNRGBA32 (x ,y ,width int ,data ,alpha []byte ,decode []float64 )(_g .NRGBA ,error ){_bed :=y *width +x ;_ffgb :=3*_bed ;if _ffgb +2>=len (data ){return _g .NRGBA {},_b .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 );};_gaga :=uint8 (0xff);if alpha !=nil &&len (alpha )> _bed {_gaga =alpha [_bed ];};_caeg ,_cebc ,_cgbb :=data [_ffgb ],data [_ffgb +1],data [_ffgb +2];if len (decode )==6{_caeg =uint8 (uint32 (LinearInterpolate (float64 (_caeg ),0,255,decode [0],decode [1]))&0xff);_cebc =uint8 (uint32 (LinearInterpolate (float64 (_cebc ),0,255,decode [2],decode [3]))&0xff);_cgbb =uint8 (uint32 (LinearInterpolate (float64 (_cgbb ),0,255,decode [4],decode [5]))&0xff);};return _g .NRGBA {R :_caeg ,G :_cebc ,B :_cgbb ,A :_gaga },nil ;};func _dggc (_adda _d .Image ,_dac Image ,_caee _d .Rectangle ){switch _dedc :=_adda .(type ){case Gray :_bce (_dedc ,_dac .(Gray ),_caee );case NRGBA :_dcdb (_dedc ,_dac .(Gray ),_caee );case CMYK :_efbe (_dedc ,_dac .(Gray ),_caee );case RGBA :_cagb (_dedc ,_dac .(Gray ),_caee );default:_ebg (_adda ,_dac .(Image ),_caee );};};func (_bda *Gray4 )SetGray (x ,y int ,g _g .Gray ){if x >=_bda .Width ||y >=_bda .Height {return ;};g =_cgcg (g );_bda .setGray (x ,y ,g );};func _egb (_bb _g .NRGBA )_g .Gray {_cd ,_fefa ,_fed ,_ :=_bb .RGBA ();_fcge :=(19595*_cd +38470*_fefa +7471*_fed +1<<15)>>24;return _g .Gray {Y :uint8 (_fcge )};};type SMasker interface{HasAlpha ()bool ;GetAlpha ()[]byte ;MakeAlpha ();};type Gray8 struct{ImageBase };func (_agffb *NRGBA64 )Copy ()Image {return &NRGBA64 {ImageBase :_agffb .copy ()}};func _cbc (_gf _d .Image )(Image ,error ){if _bba ,_edg :=_gf .(*Monochrome );_edg {return _bba ,nil ;};_fgg ,_cbd :=_gf .(Gray );if !_cbd {_fgca ,_ge :=GrayConverter .Convert (_gf );if _ge !=nil {return nil ,_ge ;};_fgg =_fgca .(Gray );};_ccfe :=_gf .Bounds ();_fgee ,_bae :=NewImage (_ccfe .Max .X ,_ccfe .Max .Y ,1,1,nil ,nil ,nil );if _bae !=nil {return nil ,_bae ;};_feg :=_fgee .(*Monochrome );_aab :=AutoThresholdTriangle (GrayHistogram (_fgg ));for _dec :=0;_dec < _ccfe .Max .X ;_dec ++{for _gga :=0;_gga < _ccfe .Max .Y ;_gga ++{_afe :=_fdcd (_fgg .GrayAt (_dec ,_gga ),monochromeModel (_aab ));_feg .SetGray (_dec ,_gga ,_afe );};};return _fgee ,nil ;};func (_gfbb *NRGBA32 )NRGBAAt (x ,y int )_g .NRGBA {_eccf ,_ :=ColorAtNRGBA32 (x ,y ,_gfbb .Width ,_gfbb .Data ,_gfbb .Alpha ,_gfbb .Decode );return _eccf ;};func AutoThresholdTriangle (histogram [256]int )uint8 {var _gcg ,_agdda ,_dbbg ,_gef int ;for _dddg :=0;_dddg < len (histogram );_dddg ++{if histogram [_dddg ]> 0{_gcg =_dddg ;break ;};};if _gcg > 0{_gcg --;};for _aaba :=255;_aaba > 0;_aaba --{if histogram [_aaba ]> 0{_gef =_aaba ;break ;};};if _gef < 255{_gef ++;};for _cgbg :=0;_cgbg < 256;_cgbg ++{if histogram [_cgbg ]> _agdda {_dbbg =_cgbg ;_agdda =histogram [_cgbg ];};};var _ggb bool ;if (_dbbg -_gcg )< (_gef -_dbbg ){_ggb =true ;var _ebf int ;_cbf :=255;for _ebf < _cbf {_bag :=histogram [_ebf ];histogram [_ebf ]=histogram [_cbf ];histogram [_cbf ]=_bag ;_ebf ++;_cbf --;};_gcg =255-_gef ;_dbbg =255-_dbbg ;};if _gcg ==_dbbg {return uint8 (_gcg );};_eabeg :=float64 (histogram [_dbbg ]);_baa :=float64 (_gcg -_dbbg );_dbdf :=_c .Sqrt (_eabeg *_eabeg +_baa *_baa );_eabeg /=_dbdf ;_baa /=_dbdf ;_dbdf =_eabeg *float64 (_gcg )+_baa *float64 (histogram [_gcg ]);_ffaa :=_gcg ;var _dbffa float64 ;for _ggbd :=_gcg +1;_ggbd <=_dbbg ;_ggbd ++{_gbdf :=_eabeg *float64 (_ggbd )+_baa *float64 (histogram [_ggbd ])-_dbdf ;if _gbdf > _dbffa {_ffaa =_ggbd ;_dbffa =_gbdf ;};};_ffaa --;if _ggb {var _feda int ;_dbg :=255;for _feda < _dbg {_bbbf :=histogram [_feda ];histogram [_feda ]=histogram [_dbg ];histogram [_dbg ]=_bbbf ;_feda ++;_dbg --;};return uint8 (255-_ffaa );};return uint8 (_ffaa );};func _gc (_gbg RGBA ,_af CMYK ,_ccf _d .Rectangle ){for _fac :=0;_fac < _ccf .Max .X ;_fac ++{for _dd :=0;_dd < _ccf .Max .Y ;_dd ++{_fcg :=_gbg .RGBAAt (_fac ,_dd );_af .SetCMYK (_fac ,_dd ,_fcfa (_fcg ));};};};var _ Gray =&Gray4 {};func _acba (_bbea ,_fbbg NRGBA ,_dfgge _d .Rectangle ){for _badg :=0;_badg < _dfgge .Max .X ;_badg ++{for _bcec :=0;_bcec < _dfgge .Max .Y ;_bcec ++{_fbbg .SetNRGBA (_badg ,_bcec ,_bbea .NRGBAAt (_badg ,_bcec ));};};};type colorConverter struct{_ag func (_gg _d .Image )(Image ,error );};func (_ecg *CMYK32 )Set (x ,y int ,c _g .Color ){_bf :=4*(y *_ecg .Width +x );if _bf +3>=len (_ecg .Data ){return ;};_bd :=_g .CMYKModel .Convert (c ).(_g .CMYK );_ecg .Data [_bf ]=_bd .C ;_ecg .Data [_bf +1]=_bd .M ;_ecg .Data [_bf +2]=_bd .Y ;_ecg .Data [_bf +3]=_bd .K ;};func (_egda *NRGBA16 )ColorModel ()_g .Model {return NRGBA16Model };func _ccc (_cdf _g .CMYK )_g .Gray {_cec ,_bga ,_adf :=_g .CMYKToRGB (_cdf .C ,_cdf .M ,_cdf .Y ,_cdf .K );_gce :=(19595*uint32 (_cec )+38470*uint32 (_bga )+7471*uint32 (_adf )+1<<7)>>16;return _g .Gray {Y :uint8 (_gce )};};func (_bdcg *NRGBA16 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_bdcg .Width ,Y :_bdcg .Height }};};func (_ecgg *NRGBA16 )setNRGBA (_cedg ,_bfeb ,_ead int ,_dbe _g .NRGBA ){if _cedg *3%2==0{_ecgg .Data [_ead ]=(_dbe .R >>4)<<4|(_dbe .G >>4);_ecgg .Data [_ead +1]=(_dbe .B >>4)<<4|(_ecgg .Data [_ead +1]&0xf);}else {_ecgg .Data [_ead ]=(_ecgg .Data [_ead ]&0xf0)|(_dbe .R >>4);_ecgg .Data [_ead +1]=(_dbe .G >>4)<<4|(_dbe .B >>4);};if _ecgg .Alpha !=nil {_eabe :=_bfeb *BytesPerLine (_ecgg .Width ,4,1);if _eabe < len (_ecgg .Alpha ){if _cedg %2==0{_ecgg .Alpha [_eabe ]=(_dbe .A >>uint (4))<<uint (4)|(_ecgg .Alpha [_ead ]&0xf);}else {_ecgg .Alpha [_eabe ]=(_ecgg .Alpha [_eabe ]&0xf0)|(_dbe .A >>uint (4));};};};};func (_cgc *Monochrome )setGray (_cdg int ,_fedf _g .Gray ,_dgcc int ){if _fedf .Y ==0{_cgc .clearBit (_dgcc ,_cdg );}else {_cgc .setBit (_dgcc ,_cdg );};};type NRGBA64 struct{ImageBase };func _dge (_acg _g .RGBA )_g .NRGBA {switch _acg .A {case 0xff:return _g .NRGBA {R :_acg .R ,G :_acg .G ,B :_acg .B ,A :0xff};case 0x00:return _g .NRGBA {};default:_cda ,_ae ,_ddg ,_bge :=_acg .RGBA ();_cda =(_cda *0xffff)/_bge ;_ae =(_ae *0xffff)/_bge ;_ddg =(_ddg *0xffff)/_bge ;return _g .NRGBA {R :uint8 (_cda >>8),G :uint8 (_ae >>8),B :uint8 (_ddg >>8),A :uint8 (_bge >>8)};};};func (_adb *Gray2 )Base ()*ImageBase {return &_adb .ImageBase };func ColorAtGray16BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_g .Gray16 ,error ){_ecdg :=(y *bytesPerLine /2+x )*2;if _ecdg +1>=len (data ){return _g .Gray16 {},_b .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 );};_adbg :=uint16 (data [_ecdg ])<<8|uint16 (data [_ecdg +1]);if len (decode )==2{_adbg =uint16 (uint64 (LinearInterpolate (float64 (_adbg ),0,65535,decode [0],decode [1])));};return _g .Gray16 {Y :_adbg },nil ;};func (_fc *CMYK32 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_fc .Width ,Y :_fc .Height }};};func _ffc (_dff _g .CMYK )_g .NRGBA {_fge ,_bbe ,_bfd :=_g .CMYKToRGB (_dff .C ,_dff .M ,_dff .Y ,_dff .K );return _g .NRGBA {R :_fge ,G :_bbe ,B :_bfd ,A :0xff};};type NRGBA16 struct{ImageBase };func _gfce (_ddfg int ,_gedc int )error {return _b .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",_ddfg ,_gedc );};type RGBA interface{RGBAAt (_gfa ,_fgge int )_g .RGBA ;SetRGBA (_deae ,_fedd int ,_faa _g .RGBA );};func (_cba *Gray4 )Copy ()Image {return &Gray4 {ImageBase :_cba .copy ()}};func ColorAtNRGBA16 (x ,y ,width ,bytesPerLine int ,data ,alpha []byte ,decode []float64 )(_g .NRGBA ,error ){_eda :=y *bytesPerLine +x *3/2;if _eda +1>=len (data ){return _g .NRGBA {},_gfce (x ,y );};const (_abe =0xf;_efeb =uint8 (0xff););_ggag :=_efeb ;if alpha !=nil {_cab :=y *BytesPerLine (width ,4,1);if _cab < len (alpha ){if x %2==0{_ggag =(alpha [_cab ]>>uint (4))&_abe ;}else {_ggag =alpha [_cab ]&_abe ;};_ggag |=_ggag <<4;};};var _aeae ,_cdd ,_degb uint8 ;if x *3%2==0{_aeae =(data [_eda ]>>uint (4))&_abe ;_cdd =data [_eda ]&_abe ;_degb =(data [_eda +1]>>uint (4))&_abe ;}else {_aeae =data [_eda ]&_abe ;_cdd =(data [_eda +1]>>uint (4))&_abe ;_degb =data [_eda +1]&_abe ;};if len (decode )==6{_aeae =uint8 (uint32 (LinearInterpolate (float64 (_aeae ),0,15,decode [0],decode [1]))&0xf);_cdd =uint8 (uint32 (LinearInterpolate (float64 (_cdd ),0,15,decode [2],decode [3]))&0xf);_degb =uint8 (uint32 (LinearInterpolate (float64 (_degb ),0,15,decode [4],decode [5]))&0xf);};return _g .NRGBA {R :(_aeae <<4)|(_aeae &0xf),G :(_cdd <<4)|(_cdd &0xf),B :(_degb <<4)|(_degb &0xf),A :_ggag },nil ;};func _efda (_gcc _d .Image )(Image ,error ){if _gdf ,_ffa :=_gcc .(*Gray8 );_ffa {return _gdf .Copy (),nil ;};_ddf :=_gcc .Bounds ();_fdgg ,_gfe :=NewImage (_ddf .Max .X ,_ddf .Max .Y ,8,1,nil ,nil ,nil );if _gfe !=nil {return nil ,_gfe ;};_dggc (_gcc ,_fdgg ,_ddf );return _fdgg ,nil ;};type Image interface{_gd .Image ;Base ()*ImageBase ;Copy ()Image ;Pix ()[]byte ;ColorAt (_fbb ,_acge int )(_g .Color ,error );Validate ()error ;};func (_add *Gray2 )GrayAt (x ,y int )_g .Gray {_gfb ,_ :=ColorAtGray2BPC (x ,y ,_add .BytesPerLine ,_add .Data ,_add .Decode );return _gfb ;};func (_bdaa *NRGBA16 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtNRGBA16 (x ,y ,_bdaa .Width ,_bdaa .BytesPerLine ,_bdaa .Data ,_bdaa .Alpha ,_bdaa .Decode );};var _ Gray =&Gray16 {};func MonochromeThresholdConverter (threshold uint8 )ColorConverter {return &monochromeThresholdConverter {Threshold :threshold };};func (_ffca *NRGBA64 )Validate ()error {if len (_ffca .Data )!=3*2*_ffca .Width *_ffca .Height {return _e .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 (_adge *monochromeThresholdConverter )Convert (img _d .Image )(Image ,error ){if _bgg ,_cdaf :=img .(*Monochrome );_cdaf {return _bgg .Copy (),nil ;};_cga :=img .Bounds ();_ddb ,_gfc :=NewImage (_cga .Max .X ,_cga .Max .Y ,1,1,nil ,nil ,nil );if _gfc !=nil {return nil ,_gfc ;};_ddb .(*Monochrome ).ModelThreshold =_adge .Threshold ;for _ggd :=0;_ggd < _cga .Max .X ;_ggd ++{for _cgd :=0;_cgd < _cga .Max .Y ;_cgd ++{_aed :=img .At (_ggd ,_cgd );_ddb .Set (_ggd ,_cgd ,_aed );};};return _ddb ,nil ;};func (_cca *ImageBase )Pix ()[]byte {return _cca .Data };var _gde [256]uint8 ;func (_fgfe *Gray2 )ColorModel ()_g .Model {return Gray2Model };func (_bbce *Monochrome )Set (x ,y int ,c _g .Color ){_acf :=y *_bbce .BytesPerLine +x >>3;if _acf > len (_bbce .Data )-1{return ;};_dbb :=_bbce .ColorModel ().Convert (c ).(_g .Gray );_bbce .setGray (x ,_dbb ,_acf );};func _a (_dc _d .Image )(Image ,error ){if _cae ,_fcb :=_dc .(*CMYK32 );_fcb {return _cae .Copy (),nil ;};_cfb :=_dc .Bounds ();_gbd ,_bad :=NewImage (_cfb .Max .X ,_cfb .Max .Y ,8,4,nil ,nil ,nil );if _bad !=nil {return nil ,_bad ;};switch _da :=_dc .(type ){case CMYK :_cce (_da ,_gbd .(CMYK ),_cfb );case Gray :_fa (_da ,_gbd .(CMYK ),_cfb );case NRGBA :_ef (_da ,_gbd .(CMYK ),_cfb );case RGBA :_gc (_da ,_gbd .(CMYK ),_cfb );default:_ebg (_dc ,_gbd ,_cfb );};return _gbd ,nil ;};var _ Image =&NRGBA64 {};func (_fce *NRGBA16 )Validate ()error {if len (_fce .Data )!=3*_fce .Width *_fce .Height /2{return _e .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 (_bgd *Monochrome )clearBit (_agf ,_ddba int ){_bgd .Data [_agf ]&=^(0x80>>uint (_ddba &7))};type Monochrome struct{ImageBase ;ModelThreshold uint8 ;};func _edaa (_gebcc CMYK ,_aeg NRGBA ,_fbgcb _d .Rectangle ){for _bgda :=0;_bgda < _fbgcb .Max .X ;_bgda ++{for _fdad :=0;_fdad < _fbgcb .Max .Y ;_fdad ++{_dgb :=_gebcc .CMYKAt (_bgda ,_fdad );_aeg .SetNRGBA (_bgda ,_fdad ,_ffc (_dgb ));};};};func (_cage *Gray16 )SetGray (x ,y int ,g _g .Gray ){_abcc :=(y *_cage .BytesPerLine /2+x )*2;if _abcc +1>=len (_cage .Data ){return ;};_cage .Data [_abcc ]=g .Y ;_cage .Data [_abcc +1]=g .Y ;};func (_fcag *Gray4 )Base ()*ImageBase {return &_fcag .ImageBase };type Gray interface{GrayAt (_aad ,_cgf int )_g .Gray ;SetGray (_egd ,_afb int ,_abg _g .Gray );};func (_fcgd *Gray16 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_fcgd .Width ,Y :_fcgd .Height }};};var _ Image =&Gray4 {};func (_eecf *Gray16 )At (x ,y int )_g .Color {_gega ,_ :=_eecf .ColorAt (x ,y );return _gega };func (_dfgb *NRGBA32 )Copy ()Image {return &NRGBA32 {ImageBase :_dfgb .copy ()}};func AddDataPadding (width ,height ,bitsPerComponent ,colorComponents int ,data []byte )([]byte ,error ){_feff :=BytesPerLine (width ,bitsPerComponent ,colorComponents );if _feff ==width *colorComponents *bitsPerComponent /8{return data ,nil ;};_ffdg :=width *colorComponents *bitsPerComponent ;_afg :=_feff *8;_bbcf :=8-(_afg -_ffdg );_bgcg :=_fg .NewReader (data );_cfca :=_feff -1;_gbga :=make ([]byte ,_cfca );_cbae :=make ([]byte ,height *_feff );_ecda :=_fg .NewWriterMSB (_cbae );var _ddbd uint64 ;var _gbc error ;for _aedgg :=0;_aedgg < height ;_aedgg ++{_ ,_gbc =_bgcg .Read (_gbga );if _gbc !=nil {return nil ,_gbc ;};_ ,_gbc =_ecda .Write (_gbga );if _gbc !=nil {return nil ,_gbc ;};_ddbd ,_gbc =_bgcg .ReadBits (byte (_bbcf ));if _gbc !=nil {return nil ,_gbc ;};_ ,_gbc =_ecda .WriteBits (_ddbd ,_bbcf );if _gbc !=nil {return nil ,_gbc ;};_ecda .FinishByte ();};return _cbae ,nil ;};func GrayHistogram (g Gray )(_fggc [256]int ){switch _dfda :=g .(type ){case Histogramer :return _dfda .Histogram ();case _d .Image :_eeeg :=_dfda .Bounds ();for _adeg :=0;_adeg < _eeeg .Max .X ;_adeg ++{for _bdaf :=0;_bdaf < _eeeg .Max .Y ;_bdaf ++{_fggc [g .GrayAt (_adeg ,_bdaf ).Y ]++;};};return _fggc ;default:return [256]int {};};};func _dgfb (_bbeg *_d .Gray )bool {for _beac :=0;_beac < len (_bbeg .Pix );_beac ++{if !_daca (_bbeg .Pix [_beac ]){return false ;};};return true ;};func ColorAtGray4BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_g .Gray ,error ){_eab :=y *bytesPerLine +x >>1;if _eab >=len (data ){return _g .Gray {},_b .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 );};_ede :=data [_eab ]>>uint (4-(x &1)*4)&0xf;if len (decode )==2{_ede =uint8 (uint32 (LinearInterpolate (float64 (_ede ),0,15,decode [0],decode [1]))&0xf);};return _g .Gray {Y :_ede *17&0xff},nil ;};func (_bc *CMYK32 )Base ()*ImageBase {return &_bc .ImageBase };func (_agdd *NRGBA32 )Set (x ,y int ,c _g .Color ){_adeb :=y *_agdd .Width +x ;_feee :=3*_adeb ;if _feee +2>=len (_agdd .Data ){return ;};_cbcff :=_g .NRGBAModel .Convert (c ).(_g .NRGBA );_agdd .setRGBA (_adeb ,_cbcff );};func LinearInterpolate (x ,xmin ,xmax ,ymin ,ymax float64 )float64 {if _c .Abs (xmax -xmin )< 0.000001{return ymin ;};_aec :=ymin +(x -xmin )*(ymax -ymin )/(xmax -xmin );return _aec ;};func ColorAt (x ,y ,width ,bitsPerColor ,colorComponents ,bytesPerLine int ,data ,alpha []byte ,decode []float64 )(_g .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 ,_b .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 _efbe (_dfa CMYK ,_cgb Gray ,_fde _d .Rectangle ){for _bggg :=0;_bggg < _fde .Max .X ;_bggg ++{for _bdb :=0;_bdb < _fde .Max .Y ;_bdb ++{_dea :=_ccc (_dfa .CMYKAt (_bggg ,_bdb ));_cgb .SetGray (_bggg ,_bdb ,_dea );};};};func (_ebe colorConverter )Convert (src _d .Image )(Image ,error ){return _ebe ._ag (src )};func (_eag *Gray4 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtGray4BPC (x ,y ,_eag .BytesPerLine ,_eag .Data ,_eag .Decode );};func (_fda *Gray4 )setGray (_efdd int ,_cbgc int ,_gad _g .Gray ){_efbd :=_cbgc *_fda .BytesPerLine ;_fcagg :=_efbd +(_efdd >>1);if _fcagg >=len (_fda .Data ){return ;};_dgg :=_gad .Y >>4;_fda .Data [_fcagg ]=(_fda .Data [_fcagg ]&(^(0xf0>>uint (4*(_efdd &1)))))|(_dgg <<uint (4-4*(_efdd &1)));};func (_abgc *Gray8 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtGray8BPC (x ,y ,_abgc .BytesPerLine ,_abgc .Data ,_abgc .Decode );};func (_eed *Gray2 )At (x ,y int )_g .Color {_cgg ,_ :=_eed .ColorAt (x ,y );return _cgg };func _gdfd (_dgbf ,_egg uint8 )uint8 {if _dgbf < _egg {return 255;};return 0;};func (_fca *Gray2 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtGray2BPC (x ,y ,_fca .BytesPerLine ,_fca .Data ,_fca .Decode );};func (_aedg *Gray8 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_aedg .Width ,Y :_aedg .Height }};};type NRGBA interface{NRGBAAt (_gcca ,_ddc int )_g .NRGBA ;SetNRGBA (_dbda ,_fege int ,_ege _g .NRGBA );};func (_gec *NRGBA32 )setRGBA (_fefc int ,_gaf _g .NRGBA ){_bcdg :=3*_fefc ;_gec .Data [_bcdg ]=_gaf .R ;_gec .Data [_bcdg +1]=_gaf .G ;_gec .Data [_bcdg +2]=_gaf .B ;if _fefc < len (_gec .Alpha ){_gec .Alpha [_fefc ]=_gaf .A ;};};func _fdcd (_bdec _g .Gray ,_ffcg monochromeModel )_g .Gray {if _bdec .Y > uint8 (_ffcg ){return _g .Gray {Y :_c .MaxUint8 };};return _g .Gray {};};func (_edae *NRGBA32 )Validate ()error {if len (_edae .Data )!=3*_edae .Width *_edae .Height {return _e .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 (_efcb *Gray16 )Set (x ,y int ,c _g .Color ){_acgd :=(y *_efcb .BytesPerLine /2+x )*2;if _acgd +1>=len (_efcb .Data ){return ;};_aff :=_g .Gray16Model .Convert (c ).(_g .Gray16 );_efcb .Data [_acgd ],_efcb .Data [_acgd +1]=uint8 (_aff .Y >>8),uint8 (_aff .Y &0xff);};func (_geb *Monochrome )At (x ,y int )_g .Color {_gff ,_ :=_geb .ColorAt (x ,y );return _gff };func (_aac *Gray16 )Copy ()Image {return &Gray16 {ImageBase :_aac .copy ()}};func (_abd *Gray8 )At (x ,y int )_g .Color {_dcf ,_ :=_abd .ColorAt (x ,y );return _dcf };func (_cc *CMYK32 )SetCMYK (x ,y int ,c _g .CMYK ){_bdf :=4*(y *_cc .Width +x );if _bdf +3>=len (_cc .Data ){return ;};_cc .Data [_bdf ]=c .C ;_cc .Data [_bdf +1]=c .M ;_cc .Data [_bdf +2]=c .Y ;_cc .Data [_bdf +3]=c .K ;};func (_bgb *NRGBA32 )Base ()*ImageBase {return &_bgb .ImageBase };func IsGrayImgBlackAndWhite (i *_d .Gray )bool {return _dgfb (i )};func _bfb (_bddc _d .Image )(Image ,error ){if _dfed ,_gdd :=_bddc .(*Gray16 );_gdd {return _dfed .Copy (),nil ;};_dbf :=_bddc .Bounds ();_eeba ,_ddd :=NewImage (_dbf .Max .X ,_dbf .Max .Y ,16,1,nil ,nil ,nil );if _ddd !=nil {return nil ,_ddd ;};_dggc (_bddc ,_eeba ,_dbf );return _eeba ,nil ;};func (_ebgf *NRGBA16 )At (x ,y int )_g .Color {_gdc ,_ :=_ebgf .ColorAt (x ,y );return _gdc };func (_ba *CMYK32 )Validate ()error {if len (_ba .Data )!=4*_ba .Width *_ba .Height {return _e .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 ColorAtCMYK (x ,y ,width int ,data []byte ,decode []float64 )(_g .CMYK ,error ){_fe :=4*(y *width +x );if _fe +3>=len (data ){return _g .CMYK {},_b .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 [_fe ]&0xff;M :=data [_fe +1]&0xff;Y :=data [_fe +2]&0xff;K :=data [_fe +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 _g .CMYK {C :C ,M :M ,Y :Y ,K :K },nil ;};var _ _d .Image =&Gray8 {};func (_dae *ImageBase )GetAlpha ()[]byte {return _dae .Alpha };type monochromeThresholdConverter struct{Threshold uint8 ;};func _fddd (_ebae _d .Image )(Image ,error ){if _dafc ,_bgdb :=_ebae .(*NRGBA32 );_bgdb {return _dafc .Copy (),nil ;};_aeaa :=_ebae .Bounds ();_eff ,_fbga :=NewImage (_aeaa .Max .X ,_aeaa .Max .Y ,8,3,nil ,nil ,nil );if _fbga !=nil {return nil ,_fbga ;};_dfgc (_ebae ,_eff ,_aeaa );return _eff ,nil ;};func (_gffb *NRGBA64 )At (x ,y int )_g .Color {_cabf ,_ :=_gffb .ColorAt (x ,y );return _cabf };func _ef (_ac NRGBA ,_ed CMYK ,_dcc _d .Rectangle ){for _fgc :=0;_fgc < _dcc .Max .X ;_fgc ++{for _ad :=0;_ad < _dcc .Max .Y ;_ad ++{_fcc :=_ac .NRGBAAt (_fgc ,_ad );_ed .SetCMYK (_fgc ,_ad ,_gbf (_fcc ));};};};var _ _d .Image =&Monochrome {};type NRGBA32 struct{ImageBase };var _ Image =&NRGBA16 {};func _aggaa (_agbf _d .Image ,_fbe uint8 )*_d .Gray {_fdcg :=_agbf .Bounds ();_bbab :=_d .NewGray (_fdcg );var (_ccab _g .Color ;_cfec _g .Gray ;);for _bfebd :=0;_bfebd < _fdcg .Max .X ;_bfebd ++{for _faf :=0;_faf < _fdcg .Max .Y ;_faf ++{_ccab =_agbf .At (_bfebd ,_faf );_bbab .Set (_bfebd ,_faf ,_ccab );_cfec =_bbab .GrayAt (_bfebd ,_faf );_bbab .SetGray (_bfebd ,_faf ,_g .Gray {Y :_gdfd (_cfec .Y ,_fbe )});};};return _bbab ;};func _cagb (_gba RGBA ,_agee Gray ,_ffbb _d .Rectangle ){for _eaga :=0;_eaga < _ffbb .Max .X ;_eaga ++{for _bbgc :=0;_bbgc < _ffbb .Max .Y ;_bbgc ++{_bccd :=_dag (_gba .RGBAAt (_eaga ,_bbgc ));_agee .SetGray (_eaga ,_bbgc ,_bccd );};};};func (_bcd *CMYK32 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtCMYK (x ,y ,_bcd .Width ,_bcd .Data ,_bcd .Decode );};var _ _d .Image =&NRGBA64 {};func (_efe *Monochrome )Copy ()Image {return &Monochrome {ImageBase :_efe .ImageBase .copy (),ModelThreshold :_efe .ModelThreshold };};func (_ecgc *NRGBA64 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtNRGBA64 (x ,y ,_ecgc .Width ,_ecgc .Data ,_ecgc .Alpha ,_ecgc .Decode );};func (_dcff *NRGBA64 )setNRGBA64 (_aggc int ,_gfab _g .NRGBA64 ,_geee int ){_dcff .Data [_aggc ]=uint8 (_gfab .R >>8);_dcff .Data [_aggc +1]=uint8 (_gfab .R &0xff);_dcff .Data [_aggc +2]=uint8 (_gfab .G >>8);_dcff .Data [_aggc +3]=uint8 (_gfab .G &0xff);_dcff .Data [_aggc +4]=uint8 (_gfab .B >>8);_dcff .Data [_aggc +5]=uint8 (_gfab .B &0xff);if _geee +1< len (_dcff .Alpha ){_dcff .Alpha [_geee ]=uint8 (_gfab .A >>8);_dcff .Alpha [_geee +1]=uint8 (_gfab .A &0xff);};};func (_cede *NRGBA16 )SetNRGBA (x ,y int ,c _g .NRGBA ){_gfd :=y *_cede .BytesPerLine +x *3/2;if _gfd +1>=len (_cede .Data ){return ;};c =_gfcc (c );_cede .setNRGBA (x ,y ,_gfd ,c );};func (_bdab *NRGBA64 )Set (x ,y int ,c _g .Color ){_bgcgc :=(y *_bdab .Width +x )*2;_dace :=_bgcgc *3;if _dace +5>=len (_bdab .Data ){return ;};_ccd :=_g .NRGBA64Model .Convert (c ).(_g .NRGBA64 );_bdab .setNRGBA64 (_dace ,_ccd ,_bgcgc );};func (_agff *Gray8 )SetGray (x ,y int ,g _g .Gray ){_eabd :=y *_agff .BytesPerLine +x ;if _eabd > len (_agff .Data )-1{return ;};_agff .Data [_eabd ]=g .Y ;};func (_bdbe *NRGBA64 )SetNRGBA64 (x ,y int ,c _g .NRGBA64 ){_gaeb :=(y *_bdbe .Width +x )*2;_aada :=_gaeb *3;if _aada +5>=len (_bdbe .Data ){return ;};_bdbe .setNRGBA64 (_aada ,c ,_gaeb );};func NewImageBase (width int ,height int ,bitsPerComponent int ,colorComponents int ,data []byte ,alpha []byte ,decode []float64 )ImageBase {_bfed :=ImageBase {Width :width ,Height :height ,BitsPerComponent :bitsPerComponent ,ColorComponents :colorComponents ,Data :data ,Alpha :alpha ,Decode :decode ,BytesPerLine :BytesPerLine (width ,bitsPerComponent ,colorComponents )};if data ==nil {_bfed .Data =make ([]byte ,height *_bfed .BytesPerLine );};return _bfed ;};var _ Image =&Monochrome {};func ConverterFunc (converterFunc func (_ccfd _d .Image )(Image ,error ))ColorConverter {return colorConverter {_ag :converterFunc };};func (_eec *Gray4 )Histogram ()(_eba [256]int ){for _gfg :=0;_gfg < _eec .Width ;_gfg ++{for _deg :=0;_deg < _eec .Height ;_deg ++{_eba [_eec .GrayAt (_gfg ,_deg ).Y ]++;};};return _eba ;};func (_dfgg *NRGBA32 )At (x ,y int )_g .Color {_gfcb ,_ :=_dfgg .ColorAt (x ,y );return _gfcb };func _dag (_ecaf _g .RGBA )_g .Gray {_bfc :=(19595*uint32 (_ecaf .R )+38470*uint32 (_ecaf .G )+7471*uint32 (_ecaf .B )+1<<7)>>16;return _g .Gray {Y :uint8 (_bfc )};};var ErrInvalidImage =_e .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 _ddgb (_gfec _d .Image )(Image ,error ){if _cgga ,_eaeg :=_gfec .(*NRGBA16 );_eaeg {return _cgga .Copy (),nil ;};_ddgc :=_gfec .Bounds ();_ccb ,_bgeg :=NewImage (_ddgc .Max .X ,_ddgc .Max .Y ,4,3,nil ,nil ,nil );if _bgeg !=nil {return nil ,_bgeg ;};_dfgc (_gfec ,_ccb ,_ddgc );return _ccb ,nil ;};func _dfgc (_cgce _d .Image ,_egdc Image ,_abdd _d .Rectangle ){if _babd ,_ebc :=_cgce .(SMasker );_ebc &&_babd .HasAlpha (){_egdc .(SMasker ).MakeAlpha ();};switch _ccdb :=_cgce .(type ){case Gray :_gdg (_ccdb ,_egdc .(NRGBA ),_abdd );case NRGBA :_acba (_ccdb ,_egdc .(NRGBA ),_abdd );case CMYK :_edaa (_ccdb ,_egdc .(NRGBA ),_abdd );case RGBA :_fccc (_ccdb ,_egdc .(NRGBA ),_abdd );default:_ebg (_cgce ,_egdc ,_abdd );};};func ColorAtGray1BPC (x ,y ,bytesPerLine int ,data []byte ,decode []float64 )(_g .Gray ,error ){_ffd :=y *bytesPerLine +x >>3;if _ffd >=len (data ){return _g .Gray {},_b .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 );};_caaa :=data [_ffd ]>>uint (7-(x &7))&1;if len (decode )==2{_caaa =uint8 (LinearInterpolate (float64 (_caaa ),0.0,1.0,decode [0],decode [1]))&1;};return _g .Gray {Y :_caaa *255},nil ;};func (_ca *CMYK32 )ColorModel ()_g .Model {return _g .CMYKModel };func (_df *CMYK32 )CMYKAt (x ,y int )_g .CMYK {_eca ,_ :=ColorAtCMYK (x ,y ,_df .Width ,_df .Data ,_df .Decode );return _eca ;};func (_ged *Gray8 )Histogram ()(_dgf [256]int ){for _acec :=0;_acec < len (_ged .Data );_acec ++{_dgf [_ged .Data [_acec ]]++;};return _dgf ;};func (_cfa *Monochrome )Validate ()error {if len (_cfa .Data )!=_cfa .Height *_cfa .BytesPerLine {return ErrInvalidImage ;};return nil ;};func _gdg (_dbff Gray ,_dfd NRGBA ,_cecc _d .Rectangle ){for _ecggb :=0;_ecggb < _cecc .Max .X ;_ecggb ++{for _gac :=0;_gac < _cecc .Max .Y ;_gac ++{_efcde :=_dbff .GrayAt (_ecggb ,_gac );_dfd .SetNRGBA (_ecggb ,_gac ,_age (_efcde ));};};};func (_adff *ImageBase )copy ()ImageBase {_efbdd :=*_adff ;_efbdd .Data =make ([]byte ,len (_adff .Data ));copy (_efbdd .Data ,_adff .Data );return _efbdd ;};type Gray16 struct{ImageBase };func (_ddbc *Gray16 )ColorModel ()_g .Model {return _g .Gray16Model };var _ _d .Image =&Gray2 {};func (_gebc *Gray4 )Validate ()error {if len (_gebc .Data )!=_gebc .Height *_gebc .BytesPerLine {return ErrInvalidImage ;};return nil ;};type Histogramer interface{Histogram ()[256]int ;};var _ Image =&CMYK32 {};func (_dfgd *Gray16 )ColorAt (x ,y int )(_g .Color ,error ){return ColorAtGray16BPC (x ,y ,_dfgd .BytesPerLine ,_dfgd .Data ,_dfgd .Decode );};func (_fbgg *Monochrome )SetGray (x ,y int ,g _g .Gray ){_fgea :=y *_fbgg .BytesPerLine +x >>3;if _fgea > len (_fbgg .Data )-1{return ;};g =_fdcd (g ,monochromeModel (_fbgg .ModelThreshold ));_fbgg .setGray (x ,g ,_fgea );};func (_afdd *NRGBA16 )Set (x ,y int ,c _g .Color ){_fcffd :=y *_afdd .BytesPerLine +x *3/2;if _fcffd +1>=len (_afdd .Data ){return ;};_agb :=NRGBA16Model .Convert (c ).(_g .NRGBA );_afdd .setNRGBA (x ,y ,_fcffd ,_agb );};func (_egbf *Monochrome )GrayAt (x ,y int )_g .Gray {_cbg ,_ :=ColorAtGray1BPC (x ,y ,_egbf .BytesPerLine ,_egbf .Data ,_egbf .Decode );return _cbg ;};func (_abgg *NRGBA64 )NRGBA64At (x ,y int )_g .NRGBA64 {_cdfg ,_ :=ColorAtNRGBA64 (x ,y ,_abgg .Width ,_abgg .Data ,_abgg .Alpha ,_abgg .Decode );return _cdfg ;};func (_bdd *Gray2 )Histogram ()(_dcd [256]int ){for _agd :=0;_agd < _bdd .Width ;_agd ++{for _fdg :=0;_fdg < _bdd .Height ;_fdg ++{_dcd [_bdd .GrayAt (_agd ,_fdg ).Y ]++;};};return _dcd ;};func _cce (_ce ,_bef CMYK ,_ea _d .Rectangle ){for _ga :=0;_ga < _ea .Max .X ;_ga ++{for _ecc :=0;_ecc < _ea .Max .Y ;_ecc ++{_bef .SetCMYK (_ga ,_ecc ,_ce .CMYKAt (_ga ,_ecc ));};};};type ColorConverter interface{Convert (_eg _d .Image )(Image ,error );};func (_cad *Gray2 )Set (x ,y int ,c _g .Color ){if x >=_cad .Width ||y >=_cad .Height {return ;};_dbc :=Gray2Model .Convert (c ).(_g .Gray );_eee :=y *_cad .BytesPerLine ;_fgb :=_eee +(x >>2);_efcd :=_dbc .Y >>6;_cad .Data [_fgb ]=(_cad .Data [_fgb ]&(^(0xc0>>uint (2*((x )&3)))))|(_efcd <<uint (6-2*(x &3)));};func (_adfd *Gray16 )Base ()*ImageBase {return &_adfd .ImageBase };func _fabe (_eege _g .Color )_g .Color {_ccea :=_g .NRGBAModel .Convert (_eege ).(_g .NRGBA );return _gfcc (_ccea );};func (_cbge *Gray4 )At (x ,y int )_g .Color {_abc ,_ :=_cbge .ColorAt (x ,y );return _abc };func (_aea *Gray8 )Set (x ,y int ,c _g .Color ){_efa :=y *_aea .BytesPerLine +x ;if _efa > len (_aea .Data )-1{return ;};_aefb :=_g .GrayModel .Convert (c );_aea .Data [_efa ]=_aefb .(_g .Gray ).Y ;};func (_dggcc *NRGBA16 )Base ()*ImageBase {return &_dggcc .ImageBase };var _ NRGBA =&NRGBA16 {};func (_ffb *Gray16 )Validate ()error {if len (_ffb .Data )!=_ffb .Height *_ffb .BytesPerLine {return ErrInvalidImage ;};return nil ;};func (_fgcc *Gray8 )Base ()*ImageBase {return &_fgcc .ImageBase };func NewImage (width ,height ,bitsPerComponent ,colorComponents int ,data ,alpha []byte ,decode []float64 )(Image ,error ){_cfe :=NewImageBase (width ,height ,bitsPerComponent ,colorComponents ,data ,alpha ,decode );var _fcgg Image ;switch colorComponents {case 1:switch bitsPerComponent {case 1:_fcgg =&Monochrome {ImageBase :_cfe ,ModelThreshold :0x0f};case 2:_fcgg =&Gray2 {ImageBase :_cfe };case 4:_fcgg =&Gray4 {ImageBase :_cfe };case 8:_fcgg =&Gray8 {ImageBase :_cfe };case 16:_fcgg =&Gray16 {ImageBase :_cfe };};case 3:switch bitsPerComponent {case 4:_fcgg =&NRGBA16 {ImageBase :_cfe };case 8:_fcgg =&NRGBA32 {ImageBase :_cfe };case 16:_fcgg =&NRGBA64 {ImageBase :_cfe };};case 4:_fcgg =&CMYK32 {ImageBase :_cfe };};if _fcgg ==nil {return nil ,ErrInvalidImage ;};return _fcgg ,nil ;};func (_be *CMYK32 )At (x ,y int )_g .Color {_db ,_ :=_be .ColorAt (x ,y );return _db };func (_aca *Monochrome )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_aca .Width ,Y :_aca .Height }};};var _ Gray =&Gray8 {};func ColorAtNRGBA (x ,y ,width ,bytesPerLine ,bitsPerColor int ,data ,alpha []byte ,decode []float64 )(_g .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 ,_b .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 (_bbc *Monochrome )Base ()*ImageBase {return &_bbc .ImageBase };func _ffg (){for _acfe :=0;_acfe < 256;_acfe ++{_gde [_acfe ]=uint8 (_acfe &0x1)+(uint8 (_acfe >>1)&0x1)+(uint8 (_acfe >>2)&0x1)+(uint8 (_acfe >>3)&0x1)+(uint8 (_acfe >>4)&0x1)+(uint8 (_acfe >>5)&0x1)+(uint8 (_acfe >>6)&0x1)+(uint8 (_acfe >>7)&0x1);};};func (_bbeb *NRGBA32 )ColorModel ()_g .Model {return _g .NRGBAModel };type CMYK32 struct{ImageBase };var _ NRGBA =&NRGBA32 {};func (_addc *ImageBase )newAlpha (){_eeca :=BytesPerLine (_addc .Width ,_addc .BitsPerComponent ,1);_addc .Alpha =make ([]byte ,_addc .Height *_eeca );};func (_ggg *Gray2 )Bounds ()_d .Rectangle {return _d .Rectangle {Max :_d .Point {X :_ggg .Width ,Y :_ggg .Height }};};func BytesPerLine (width ,bitsPerComponent ,colorComponents int )int {return ((width *bitsPerComponent )*colorComponents +7)>>3;};func (_gaef *Gray8 )Validate ()error {if len (_gaef .Data )!=_gaef .Height *_gaef .BytesPerLine {return ErrInvalidImage ;};return nil ;};func (_eacd *ImageBase )HasAlpha ()bool {return _eacd .Alpha !=nil };func (_ebgb *Gray2 )SetGray (x ,y int ,gray _g .Gray ){_dcad :=_bdee (gray );_ade :=y *_ebgb .BytesPerLine ;_ecb :=_ade +(x >>2);if _ecb >=len (_ebgb .Data ){return ;};_gegc :=_dcad .Y >>6;_ebgb .Data [_ecb ]=(_ebgb .Data [_ecb ]&(^(0xc0>>uint (2*((x )&3)))))|(_gegc <<uint (6-2*(x &3)));};func (_gdaf *NRGBA32 )SetNRGBA (x ,y int ,c _g .NRGBA ){_gdae :=y *_gdaf .Width +x ;_facf :=3*_gdae ;if _facf +2>=len (_gdaf .Data ){return ;};_gdaf .setRGBA (_gdae ,c );return ;};func _fcfa (_cdaa _g .RGBA )_g .CMYK {_daa ,_agc ,_fcgf ,_fbgc :=_g .RGBToCMYK (_cdaa .R ,_cdaa .G ,_cdaa .B );return _g .CMYK {C :_daa ,M :_agc ,Y :_fcgf ,K :_fbgc };}; |