@@ -17,11 +17,116 @@ limitations under the License.
1717using System ;
1818using System . Collections . Generic ;
1919using System . Text ;
20+ using static Tensorflow . Binding ;
2021
2122namespace Tensorflow
2223{
2324 public class image_ops_impl
2425 {
26+ public static Tensor decode_image ( Tensor contents , int channels = 0 , TF_DataType dtype = TF_DataType . TF_UINT8 ,
27+ string name = null , bool expand_animations = true )
28+ {
29+ Tensor substr = null ;
2530
31+ Func < ITensorOrOperation > _jpeg = ( ) =>
32+ {
33+ int jpeg_channels = channels ;
34+ var good_channels = math_ops . not_equal ( jpeg_channels , 4 , name : "check_jpeg_channels" ) ;
35+ string channels_msg = "Channels must be in (None, 0, 1, 3) when decoding JPEG 'images'" ;
36+ var assert_channels = control_flow_ops . Assert ( good_channels , new string [ ] { channels_msg } ) ;
37+ return tf_with ( ops . control_dependencies ( new [ ] { assert_channels } ) , delegate
38+ {
39+ return convert_image_dtype ( gen_image_ops . decode_jpeg ( contents , channels ) , dtype ) ;
40+ } ) ;
41+ } ;
42+
43+ Func < ITensorOrOperation > _gif = ( ) =>
44+ {
45+ int gif_channels = channels ;
46+ var good_channels = math_ops . logical_and (
47+ math_ops . not_equal ( gif_channels , 1 , name : "check_gif_channels" ) ,
48+ math_ops . not_equal ( gif_channels , 4 , name : "check_gif_channels" ) ) ;
49+
50+ string channels_msg = "Channels must be in (None, 0, 3) when decoding GIF images" ;
51+ var assert_channels = control_flow_ops . Assert ( good_channels , new string [ ] { channels_msg } ) ;
52+ return tf_with ( ops . control_dependencies ( new [ ] { assert_channels } ) , delegate
53+ {
54+ var result = convert_image_dtype ( gen_image_ops . decode_gif ( contents ) , dtype ) ;
55+ if ( ! expand_animations )
56+ // result = array_ops.gather(result, 0);
57+ throw new NotImplementedException ( "" ) ;
58+ return result ;
59+ } ) ;
60+ } ;
61+
62+ Func < ITensorOrOperation > _bmp = ( ) =>
63+ {
64+ int bmp_channels = channels ;
65+ var signature = string_ops . substr ( contents , 0 , 2 ) ;
66+ var is_bmp = math_ops . equal ( signature , "BM" , name : "is_bmp" ) ;
67+ string decode_msg = "Unable to decode bytes as JPEG, PNG, GIF, or BMP" ;
68+ var assert_decode = control_flow_ops . Assert ( is_bmp , new string [ ] { decode_msg } ) ;
69+ var good_channels = math_ops . not_equal ( bmp_channels , 1 , name : "check_channels" ) ;
70+ string channels_msg = "Channels must be in (None, 0, 3) when decoding BMP images" ;
71+ var assert_channels = control_flow_ops . Assert ( good_channels , new string [ ] { channels_msg } ) ;
72+ return tf_with ( ops . control_dependencies ( new [ ] { assert_decode , assert_channels } ) , delegate
73+ {
74+ return convert_image_dtype ( gen_image_ops . decode_bmp ( contents ) , dtype ) ;
75+ } ) ;
76+ } ;
77+
78+ Func < ITensorOrOperation > _png = ( ) =>
79+ {
80+ return convert_image_dtype ( gen_image_ops . decode_png (
81+ contents ,
82+ channels ,
83+ dtype : dtype ) ,
84+ dtype ) ;
85+ } ;
86+
87+ Func < ITensorOrOperation > check_gif = ( ) =>
88+ {
89+ var is_gif = math_ops . equal ( substr , "\x47 \x49 \x46 " , name : "is_gif" ) ;
90+ return control_flow_ops . cond ( is_gif , _gif , _bmp , name : "cond_gif" ) ;
91+ } ;
92+
93+ Func < ITensorOrOperation > check_png = ( ) =>
94+ {
95+ return control_flow_ops . cond ( _is_png ( contents ) , _png , check_gif , name : "cond_png" ) ;
96+ } ;
97+
98+ return tf_with ( ops . name_scope ( name , "decode_image" ) , scope =>
99+ {
100+ substr = string_ops . substr ( contents , 0 , 3 ) ;
101+ return control_flow_ops . cond ( is_jpeg ( contents ) , _jpeg , check_png , name : "cond_jpeg" ) ;
102+ } ) ;
103+ }
104+
105+ public static Tensor is_jpeg ( Tensor contents , string name = null )
106+ {
107+ return tf_with ( ops . name_scope ( name , "is_jpeg" ) , scope =>
108+ {
109+ var substr = string_ops . substr ( contents , 0 , 3 ) ;
110+ return math_ops . equal ( substr , "\xff \xd8 \xff " , name : name ) ;
111+ } ) ;
112+ }
113+
114+ public static Tensor _is_png ( Tensor contents , string name = null )
115+ {
116+ return tf_with ( ops . name_scope ( name , "is_png" ) , scope =>
117+ {
118+ var substr = string_ops . substr ( contents , 0 , 3 ) ;
119+ return math_ops . equal ( substr , @"\211PN" , name : name ) ;
120+ } ) ;
121+ }
122+
123+ public static Tensor convert_image_dtype ( Tensor image , TF_DataType dtype , bool saturate = false ,
124+ string name = null )
125+ {
126+ if ( dtype == image . dtype )
127+ return array_ops . identity ( image , name : name ) ;
128+
129+ throw new NotImplementedException ( "" ) ;
130+ }
26131 }
27132}
0 commit comments