Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 // See docs in ../ops/string_ops.cc.
     17 
     18 #include <string>
     19 
     20 #include "tensorflow/core/framework/op_kernel.h"
     21 #include "tensorflow/core/framework/tensor.h"
     22 #include "tensorflow/core/lib/strings/base64.h"
     23 
     24 namespace tensorflow {
     25 namespace {
     26 
     27 class EncodeBase64Op : public OpKernel {
     28  public:
     29   explicit EncodeBase64Op(OpKernelConstruction* context) : OpKernel(context) {
     30     OP_REQUIRES_OK(context, context->GetAttr("pad", &pad_));
     31   }
     32 
     33   void Compute(OpKernelContext* context) override {
     34     const Tensor& input_tensor = context->input(0);
     35     Tensor* output_tensor = nullptr;
     36     OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
     37                                                      &output_tensor));
     38 
     39     auto input = input_tensor.flat<string>();
     40     auto output = output_tensor->flat<string>();
     41 
     42     for (int64 i = 0; i < input.dimension(0); ++i) {
     43       OP_REQUIRES_OK(context, Base64Encode(input(i), pad_, &output(i)));
     44     }
     45   }
     46 
     47  private:
     48   bool pad_;
     49 };
     50 
     51 REGISTER_KERNEL_BUILDER(Name("EncodeBase64").Device(DEVICE_CPU),
     52                         EncodeBase64Op);
     53 
     54 class DecodeBase64Op : public OpKernel {
     55  public:
     56   using OpKernel::OpKernel;
     57 
     58   void Compute(OpKernelContext* context) override {
     59     const Tensor& input_tensor = context->input(0);
     60     Tensor* output_tensor = nullptr;
     61     OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
     62                                                      &output_tensor));
     63 
     64     auto input = input_tensor.flat<string>();
     65     auto output = output_tensor->flat<string>();
     66 
     67     for (int64 i = 0; i < input.dimension(0); ++i) {
     68       OP_REQUIRES_OK(context, Base64Decode(input(i), &output(i)));
     69     }
     70   }
     71 };
     72 
     73 REGISTER_KERNEL_BUILDER(Name("DecodeBase64").Device(DEVICE_CPU),
     74                         DecodeBase64Op);
     75 
     76 }  // namespace
     77 }  // namespace tensorflow
     78