MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / Compute

Method Compute

tensorflow/core/kernels/encode_jpeg_op.cc:77–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

75 }
76
77 void Compute(OpKernelContext* context) override {
78 const Tensor& image = context->input(0);
79 OP_REQUIRES(context, image.dims() == 3,
80 errors::InvalidArgument("image must be 3-dimensional",
81 image.shape().DebugString()));
82
83 OP_REQUIRES(
84 context,
85 FastBoundsCheck(image.NumElements(), std::numeric_limits<int32>::max()),
86 errors::InvalidArgument(
87 "Cannot encode images with >= max int32 elements"));
88
89 const int32 dim_size0 = static_cast<int32>(image.dim_size(0));
90 const int32 dim_size1 = static_cast<int32>(image.dim_size(1));
91 const int32 dim_size2 = static_cast<int32>(image.dim_size(2));
92
93 // Autodetect format if desired, otherwise make sure format and
94 // image channels are consistent.
95 int channels;
96 jpeg::CompressFlags adjusted_flags = flags_;
97 if (flags_.format == 0) {
98 channels = dim_size2;
99 if (channels == 1) {
100 adjusted_flags.format = jpeg::FORMAT_GRAYSCALE;
101 } else if (channels == 3) {
102 adjusted_flags.format = jpeg::FORMAT_RGB;
103 } else {
104 OP_REQUIRES(
105 context, false,
106 errors::InvalidArgument("image must have 1 or 3 channels, got ",
107 image.shape().DebugString()));
108 }
109 } else {
110 if (flags_.format == jpeg::FORMAT_GRAYSCALE) {
111 channels = 1;
112 } else { // RGB
113 channels = 3;
114 }
115 OP_REQUIRES(context, channels == dim_size2,
116 errors::InvalidArgument("format ", format_, " expects ",
117 channels, " channels, got ",
118 image.shape().DebugString()));
119 }
120
121 // Encode image to jpeg string
122 Tensor* output = nullptr;
123 OP_REQUIRES_OK(context,
124 context->allocate_output(0, TensorShape({}), &output));
125 OP_REQUIRES(context,
126 jpeg::Compress(image.flat<uint8>().data(), dim_size1, dim_size0,
127 adjusted_flags, &output->scalar<string>()()),
128 errors::Internal("JPEG encoding failed"));
129 }
130
131 private:
132 string format_;

Callers

nothing calls this directly

Calls 14

InvalidArgumentFunction · 0.85
FastBoundsCheckFunction · 0.85
CompressFunction · 0.85
InternalFunction · 0.85
allocate_outputMethod · 0.80
maxFunction · 0.50
TensorShapeClass · 0.50
inputMethod · 0.45
dimsMethod · 0.45
DebugStringMethod · 0.45
shapeMethod · 0.45
NumElementsMethod · 0.45

Tested by

no test coverage detected