• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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/bounds_check.h"
20 #include "tensorflow/core/framework/op_kernel.h"
21 #include "tensorflow/core/framework/register_types.h"
22 #include "tensorflow/core/framework/tensor.h"
23 #include "tensorflow/core/framework/tensor_shape.h"
24 #include "tensorflow/core/framework/types.h"
25 #include "tensorflow/core/lib/core/status.h"
26 #include "tensorflow/core/lib/png/png_io.h"
27 #include "tensorflow/core/platform/logging.h"
28 
29 namespace tensorflow {
30 
31 // Encode an image to a PNG stream
32 class EncodePngOp : public OpKernel {
33  public:
EncodePngOp(OpKernelConstruction * context)34   explicit EncodePngOp(OpKernelConstruction* context) : OpKernel(context) {
35     OP_REQUIRES_OK(context, context->GetAttr("compression", &compression_));
36     OP_REQUIRES(context, -1 <= compression_ && compression_ <= 9,
37                 errors::InvalidArgument("compression should be in [-1,9], got ",
38                                         compression_));
39 
40     DataType dt = context->input_type(0);
41     OP_REQUIRES(context, dt == DataType::DT_UINT8 || dt == DataType::DT_UINT16,
42                 errors::InvalidArgument(
43                     "image must have type uint8 or uint16, got ", dt));
44 
45     if (dt == DataType::DT_UINT8) {
46       desired_channel_bits_ = 8;
47     } else {
48       desired_channel_bits_ = 16;
49     }
50   }
51 
Compute(OpKernelContext * context)52   void Compute(OpKernelContext* context) override {
53     const Tensor& image = context->input(0);
54     OP_REQUIRES(context, image.dims() == 3,
55                 errors::InvalidArgument("image must be 3-dimensional",
56                                         image.shape().DebugString()));
57     OP_REQUIRES(
58         context,
59         FastBoundsCheck(image.NumElements(), std::numeric_limits<int32>::max()),
60         errors::InvalidArgument("image cannot have >= int32 max elements"));
61     const int32 height = static_cast<int32>(image.dim_size(0));
62     const int32 width = static_cast<int32>(image.dim_size(1));
63     const int32 channels = static_cast<int32>(image.dim_size(2));
64 
65     // In some cases, we pass width*channels*2 to png.
66     const int32 max_row_width = std::numeric_limits<int32>::max() / 2;
67 
68     OP_REQUIRES(context, FastBoundsCheck(width * channels, max_row_width),
69                 errors::InvalidArgument("image too wide to encode"));
70 
71     OP_REQUIRES(context, channels >= 1 && channels <= 4,
72                 errors::InvalidArgument(
73                     "image must have 1, 2, 3, or 4 channels, got ", channels));
74 
75     // Encode image to png string
76     Tensor* output = nullptr;
77     OP_REQUIRES_OK(context,
78                    context->allocate_output(0, TensorShape({}), &output));
79     if (desired_channel_bits_ == 8) {
80       OP_REQUIRES(context,
81                   png::WriteImageToBuffer(
82                       image.flat<uint8>().data(), width, height,
83                       width * channels, channels, desired_channel_bits_,
84                       compression_, &output->scalar<tstring>()(), nullptr),
85                   errors::Internal("PNG encoding failed"));
86     } else {
87       OP_REQUIRES(context,
88                   png::WriteImageToBuffer(
89                       image.flat<uint16>().data(), width, height,
90                       width * channels * 2, channels, desired_channel_bits_,
91                       compression_, &output->scalar<tstring>()(), nullptr),
92                   errors::Internal("PNG encoding failed"));
93     }
94   }
95 
96  private:
97   int compression_;
98   int desired_channel_bits_;
99 };
100 REGISTER_KERNEL_BUILDER(Name("EncodePng").Device(DEVICE_CPU), EncodePngOp);
101 
102 }  // namespace tensorflow
103