Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2017 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/image_ops.cc
     17 
     18 #include <memory>
     19 #include "tensorflow/core/framework/op_kernel.h"
     20 #include "tensorflow/core/framework/register_types.h"
     21 #include "tensorflow/core/framework/tensor.h"
     22 #include "tensorflow/core/framework/tensor_shape.h"
     23 #include "tensorflow/core/framework/types.h"
     24 #include "tensorflow/core/kernels/bounds_check.h"
     25 #include "tensorflow/core/lib/core/status.h"
     26 #include "tensorflow/core/lib/jpeg/jpeg_mem.h"
     27 #include "tensorflow/core/platform/logging.h"
     28 
     29 namespace tensorflow {
     30 
     31 // Extract the shape of a JPEG image.
     32 template <typename T>
     33 class ExtractJpegShapeOp : public OpKernel {
     34  public:
     35   explicit ExtractJpegShapeOp(OpKernelConstruction* context)
     36       : OpKernel(context) {}
     37 
     38   void Compute(OpKernelContext* context) override {
     39     // Get input content.
     40     const Tensor& contents = context->input(0);
     41     OP_REQUIRES(context, TensorShapeUtils::IsScalar(contents.shape()),
     42                 errors::InvalidArgument("contents must be scalar, got shape ",
     43                                         contents.shape().DebugString()));
     44     const StringPiece input = contents.scalar<string>()();
     45     OP_REQUIRES(context, input.size() <= std::numeric_limits<int>::max(),
     46                 errors::InvalidArgument("JPEG contents are too large for int: ",
     47                                         input.size()));
     48 
     49     // Call GetImageInfo to get image shape.
     50     int width, height, components;
     51     OP_REQUIRES(
     52         context,
     53         jpeg::GetImageInfo(input.data(), input.size(), &width, &height,
     54                            &components),
     55         errors::InvalidArgument("Invalid JPEG data, size ", input.size()));
     56     // Allocate tensor and set shape size.
     57     Tensor* image_shape = nullptr;
     58     OP_REQUIRES_OK(context,
     59                    context->allocate_output(0, TensorShape({3}), &image_shape));
     60     auto image_shape_data = image_shape->tensor<T, 1>();
     61     image_shape_data(0) = height;
     62     image_shape_data(1) = width;
     63     image_shape_data(2) = components;
     64   }
     65 };
     66 
     67 #define REGISTER_KERNELS(type)                                      \
     68   REGISTER_KERNEL_BUILDER(Name("ExtractJpegShape")                  \
     69                               .Device(DEVICE_CPU)                   \
     70                               .TypeConstraint<type>("output_type"), \
     71                           ExtractJpegShapeOp<type>)
     72 
     73 TF_CALL_int32(REGISTER_KERNELS);
     74 TF_CALL_int64(REGISTER_KERNELS);
     75 #undef REGISTER_KERNELS
     76 
     77 }  // namespace tensorflow
     78