33using Synercoding . FileFormats . Pdf . LowLevel ;
44using Synercoding . FileFormats . Pdf . LowLevel . Colors . ColorSpaces ;
55using Synercoding . FileFormats . Pdf . LowLevel . XRef ;
6- using System . IO . Compression ;
76
87namespace Synercoding . FileFormats . Pdf ;
98
@@ -14,7 +13,7 @@ public class Image : IDisposable
1413{
1514 private protected bool _disposed ;
1615
17- internal Image ( PdfReference id , Stream jpgStream , int width , int height , ColorSpace colorSpace , Image ? softMask , params StreamFilter [ ] filters )
16+ internal Image ( PdfReference id , Stream jpgStream , int width , int height , ColorSpace colorSpace , Image ? softMask , double [ ] ? decodeArray , params StreamFilter [ ] filters )
1817 {
1918 Reference = id ;
2019
@@ -23,13 +22,16 @@ internal Image(PdfReference id, Stream jpgStream, int width, int height, ColorSp
2322 RawStream = jpgStream ;
2423 ColorSpace = colorSpace ;
2524 SoftMask = softMask ;
25+ DecodeArray = decodeArray ;
2626 Filters = filters ;
2727 }
2828
2929 internal Image ? SoftMask { get ; private set ; }
3030
3131 internal Stream RawStream { get ; private set ; }
3232
33+ internal double [ ] ? DecodeArray { get ; private set ; }
34+
3335 /// <summary>
3436 /// A pdf reference object that can be used to reference to this object
3537 /// </summary>
@@ -77,23 +79,46 @@ private static Stream _encodeToJpg(SixLabors.ImageSharp.Image image)
7779
7880 internal static Image Get ( TableBuilder tableBuilder , Image < Rgba32 > image )
7981 {
80- return new Image ( tableBuilder . ReserveId ( ) , _encodeToJpg ( image ) , image . Width , image . Height , DeviceRGB . Instance , GetMask ( tableBuilder , image ) , StreamFilter . DCTDecode ) ;
82+ return new Image ( tableBuilder . ReserveId ( ) , _encodeToJpg ( image ) , image . Width , image . Height , DeviceRGB . Instance , GetMask ( tableBuilder , image ) , null , StreamFilter . DCTDecode ) ;
8183 }
8284
8385 internal static Image ? GetMask ( TableBuilder tableBuilder , Image < Rgba32 > image )
8486 {
85- var hasTrans = image . Metadata . TryGetPngMetadata ( out var pngMeta )
86- &&
87- (
88- pngMeta . ColorType == SixLabors . ImageSharp . Formats . Png . PngColorType . RgbWithAlpha
89- || pngMeta . ColorType == SixLabors . ImageSharp . Formats . Png . PngColorType . GrayscaleWithAlpha
90- ) ;
91-
92- return hasTrans
93- ? new Image ( tableBuilder . ReserveId ( ) , AsImageByteStream ( image , GrayScaleMethod . AlphaChannel ) , image . Width , image . Height , DeviceGray . Instance , null , StreamFilter . FlateDecode )
87+ return _hasTransparancy ( image )
88+ ? new Image ( tableBuilder . ReserveId ( ) , AsImageByteStream ( image , GrayScaleMethod . AlphaChannel ) , image . Width , image . Height , DeviceGray . Instance , null , null , StreamFilter . FlateDecode )
9489 : null ;
9590 }
9691
92+ private static bool _hasTransparancy ( Image < Rgba32 > image )
93+ {
94+ if ( image . Metadata . TryGetPngMetadata ( out var pngMeta ) )
95+ {
96+ if ( pngMeta . ColorType == SixLabors . ImageSharp . Formats . Png . PngColorType . RgbWithAlpha )
97+ return true ;
98+ if ( pngMeta . ColorType == SixLabors . ImageSharp . Formats . Png . PngColorType . GrayscaleWithAlpha )
99+ return true ;
100+ }
101+
102+ bool hasTransparancy = false ;
103+ image . ProcessPixelRows ( accessor =>
104+ {
105+ for ( int y = 0 ; y < accessor . Height ; y ++ )
106+ {
107+ var row = accessor . GetRowSpan ( y ) ;
108+ for ( int x = 0 ; x < row . Length ; x ++ )
109+ {
110+ ref Rgba32 pixel = ref row [ x ] ;
111+ if ( pixel . A != 0xFF )
112+ {
113+ hasTransparancy = true ;
114+ return ;
115+ }
116+ }
117+ }
118+ } ) ;
119+ return hasTransparancy ;
120+ }
121+
97122 internal static Stream AsImageByteStream ( Image < Rgba32 > image , GrayScaleMethod grayScaleMethod )
98123 {
99124 using ( var byteStream = new MemoryStream ( ) )
@@ -118,6 +143,8 @@ internal static Stream AsImageByteStream(Image<Rgba32> image, GrayScaleMethod gr
118143 GrayScaleMethod . GreenChannel => pixel . G ,
119144 GrayScaleMethod . BlueChannel => pixel . B ,
120145 GrayScaleMethod . AverageOfRGBChannels => ( byte ) ( ( pixel . R + pixel . G + pixel . B ) / 3 ) ,
146+ GrayScaleMethod . BT601 => ( byte ) ( ( pixel . R * 0.299 ) + ( pixel . G * 0.587 ) + ( pixel . B * 0.114 ) ) ,
147+ GrayScaleMethod . BT709 => ( byte ) ( ( pixel . R * 0.2126 ) + ( pixel . G * 0.7152 ) + ( pixel . B * 0.0722 ) ) ,
121148 _ => throw new NotImplementedException ( )
122149 } ;
123150
0 commit comments