11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
33
4+ using SixLabors . ImageSharp . Formats . Tiff . Constants ;
45using SixLabors . ImageSharp . PixelFormats ;
56
67namespace SixLabors . ImageSharp . Formats . Tiff ;
@@ -25,6 +26,11 @@ private TiffMetadata(TiffMetadata other)
2526 {
2627 this . ByteOrder = other . ByteOrder ;
2728 this . FormatType = other . FormatType ;
29+ this . BitsPerPixel = other . BitsPerPixel ;
30+ this . BitsPerSample = other . BitsPerSample ;
31+ this . Compression = other . Compression ;
32+ this . PhotometricInterpretation = other . PhotometricInterpretation ;
33+ this . Predictor = other . Predictor ;
2834 }
2935
3036 /// <summary>
@@ -37,14 +43,142 @@ private TiffMetadata(TiffMetadata other)
3743 /// </summary>
3844 public TiffFormatType FormatType { get ; set ; }
3945
46+ /// <summary>
47+ /// Gets or sets the bits per pixel. Derived from the root frame.
48+ /// </summary>
49+ public TiffBitsPerPixel BitsPerPixel { get ; set ; } = TiffConstants . DefaultBitsPerPixel ;
50+
51+ /// <summary>
52+ /// Gets or sets number of bits per component. Derived from the root frame.
53+ /// </summary>
54+ public TiffBitsPerSample BitsPerSample { get ; set ; } = TiffConstants . DefaultBitsPerSample ;
55+
56+ /// <summary>
57+ /// Gets or sets the compression scheme used on the image data. Derived from the root frame.
58+ /// </summary>
59+ public TiffCompression Compression { get ; set ; } = TiffConstants . DefaultCompression ;
60+
61+ /// <summary>
62+ /// Gets or sets the color space of the image data. Derived from the root frame.
63+ /// </summary>
64+ public TiffPhotometricInterpretation PhotometricInterpretation { get ; set ; } = TiffConstants . DefaultPhotometricInterpretation ;
65+
66+ /// <summary>
67+ /// Gets or sets a mathematical operator that is applied to the image data before an encoding scheme is applied.
68+ /// Derived from the root frame.
69+ /// </summary>
70+ public TiffPredictor Predictor { get ; set ; } = TiffConstants . DefaultPredictor ;
71+
4072 /// <inheritdoc/>
41- public static TiffMetadata FromFormatConnectingMetadata ( FormatConnectingMetadata metadata ) => throw new NotImplementedException ( ) ;
73+ public static TiffMetadata FromFormatConnectingMetadata ( FormatConnectingMetadata metadata )
74+ {
75+ int bpp = metadata . PixelTypeInfo . BitsPerPixel ;
76+ return bpp switch
77+ {
78+ 1 => new TiffMetadata
79+ {
80+ BitsPerPixel = TiffBitsPerPixel . Bit1 ,
81+ BitsPerSample = TiffConstants . BitsPerSample1Bit ,
82+ PhotometricInterpretation = TiffPhotometricInterpretation . WhiteIsZero ,
83+ Compression = TiffCompression . CcittGroup4Fax ,
84+ Predictor = TiffPredictor . None
85+ } ,
86+ <= 4 => new TiffMetadata
87+ {
88+ BitsPerPixel = TiffBitsPerPixel . Bit4 ,
89+ BitsPerSample = TiffConstants . BitsPerSample4Bit ,
90+ PhotometricInterpretation = TiffPhotometricInterpretation . PaletteColor ,
91+ Compression = TiffCompression . Deflate ,
92+ Predictor = TiffPredictor . None // Best match for low bit depth
93+ } ,
94+ 8 => new TiffMetadata
95+ {
96+ BitsPerPixel = TiffBitsPerPixel . Bit8 ,
97+ BitsPerSample = TiffConstants . BitsPerSample8Bit ,
98+ PhotometricInterpretation = TiffPhotometricInterpretation . PaletteColor ,
99+ Compression = TiffCompression . Deflate ,
100+ Predictor = TiffPredictor . Horizontal
101+ } ,
102+ 16 => new TiffMetadata
103+ {
104+ BitsPerPixel = TiffBitsPerPixel . Bit16 ,
105+ BitsPerSample = TiffConstants . BitsPerSample16Bit ,
106+ PhotometricInterpretation = TiffPhotometricInterpretation . BlackIsZero ,
107+ Compression = TiffCompression . Deflate ,
108+ Predictor = TiffPredictor . Horizontal
109+ } ,
110+ 32 or 64 => new TiffMetadata
111+ {
112+ BitsPerPixel = TiffBitsPerPixel . Bit32 ,
113+ BitsPerSample = TiffConstants . BitsPerSampleRgb8Bit ,
114+ PhotometricInterpretation = TiffPhotometricInterpretation . Rgb ,
115+ Compression = TiffCompression . Deflate ,
116+ Predictor = TiffPredictor . Horizontal
117+ } ,
118+ _ => new TiffMetadata
119+ {
120+ BitsPerPixel = TiffBitsPerPixel . Bit24 ,
121+ BitsPerSample = TiffConstants . BitsPerSampleRgb8Bit ,
122+ PhotometricInterpretation = TiffPhotometricInterpretation . Rgb ,
123+ Compression = TiffCompression . Deflate ,
124+ Predictor = TiffPredictor . Horizontal
125+ }
126+ } ;
127+ }
42128
43129 /// <inheritdoc/>
44- public PixelTypeInfo GetPixelTypeInfo ( ) => throw new NotImplementedException ( ) ;
130+ public PixelTypeInfo GetPixelTypeInfo ( )
131+ {
132+ int bpp = ( int ) this . BitsPerPixel ;
133+
134+ TiffBitsPerSample samples = this . BitsPerSample ;
135+ PixelComponentInfo info = samples . Channels switch
136+ {
137+ 1 => PixelComponentInfo . Create ( 1 , bpp , bpp ) ,
138+ 2 => PixelComponentInfo . Create ( 2 , bpp , bpp , samples . Channel0 , samples . Channel1 ) ,
139+ 3 => PixelComponentInfo . Create ( 3 , bpp , samples . Channel0 , samples . Channel1 , samples . Channel2 ) ,
140+ _ => PixelComponentInfo . Create ( 4 , bpp , samples . Channel0 , samples . Channel1 , samples . Channel2 , samples . Channel3 )
141+ } ;
142+
143+ PixelColorType colorType ;
144+ PixelAlphaRepresentation alpha = PixelAlphaRepresentation . None ;
145+ switch ( this . BitsPerPixel )
146+ {
147+ case TiffBitsPerPixel . Bit1 :
148+ colorType = PixelColorType . Binary ;
149+ break ;
150+ case TiffBitsPerPixel . Bit4 :
151+ case TiffBitsPerPixel . Bit6 :
152+ case TiffBitsPerPixel . Bit8 :
153+ colorType = PixelColorType . Indexed ;
154+ break ;
155+ case TiffBitsPerPixel . Bit16 :
156+ colorType = PixelColorType . Luminance ;
157+ break ;
158+ case TiffBitsPerPixel . Bit32 :
159+ case TiffBitsPerPixel . Bit64 :
160+ colorType = PixelColorType . RGB | PixelColorType . Alpha ;
161+ alpha = PixelAlphaRepresentation . Unassociated ;
162+ break ;
163+ default :
164+ colorType = PixelColorType . RGB ;
165+ break ;
166+ }
167+
168+ return new PixelTypeInfo ( bpp )
169+ {
170+ ColorType = colorType ,
171+ ComponentInfo = info ,
172+ AlphaRepresentation = alpha
173+ } ;
174+ }
45175
46176 /// <inheritdoc/>
47- public FormatConnectingMetadata ToFormatConnectingMetadata ( ) => throw new NotImplementedException ( ) ;
177+ public FormatConnectingMetadata ToFormatConnectingMetadata ( )
178+ => new ( )
179+ {
180+ PixelTypeInfo = this . GetPixelTypeInfo ( )
181+ } ;
48182
49183 /// <inheritdoc/>
50184 IDeepCloneable IDeepCloneable . DeepClone ( ) => this . DeepClone ( ) ;
0 commit comments